File: GlobalUnsynced.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (122 lines) | stat: -rw-r--r-- 2,682 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

/**
 * @brief Globally accessible stuff
 * Contains implementation of synced and unsynced global stuff.
 */

#include "GlobalUnsynced.h"

#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "Sim/Misc/TeamHandler.h"
#include "System/Config/ConfigHandler.h"
#include "System/Exceptions.h"
#include "System/SafeUtil.h"
#include "System/creg/creg_cond.h"
#include "System/Misc/SpringTime.h"

#include <ctime>


/**
 * @brief global unsynced
 *
 * Global instance of CGlobalUnsynced
 */

CGlobalUnsynced guOBJ;
CGlobalUnsyncedRNG guRNG;

CGlobalUnsynced* gu = &guOBJ;


CR_BIND(CGlobalUnsynced, )

CR_REG_METADATA(CGlobalUnsynced, (
	CR_IGNORED(simFPS),
	CR_IGNORED(avgSimFrameTime),
	CR_IGNORED(avgDrawFrameTime),
	CR_IGNORED(avgFrameTime),
	CR_MEMBER(modGameTime),
	CR_MEMBER(gameTime),
	CR_MEMBER(startTime),
	CR_MEMBER(myPlayerNum),
	CR_MEMBER(myTeam),
	CR_MEMBER(myAllyTeam),
	CR_MEMBER(myPlayingTeam),
	CR_MEMBER(myPlayingAllyTeam),
	CR_MEMBER(spectating),
	CR_MEMBER(spectatingFullView),
	CR_MEMBER(spectatingFullSelect),
	CR_IGNORED(fpsMode),
	CR_IGNORED(globalQuit),
	CR_IGNORED(globalReload)
))


void CGlobalUnsynced::ResetState()
{
	simFPS = 0.0f;

	avgSimFrameTime = 0.001f;
	avgDrawFrameTime = 0.001f;
	avgFrameTime = 0.001f;

	modGameTime = 0.0f;
	gameTime = 0.0f;
	startTime = 0.0f;

	myPlayerNum = 0;
	myTeam = 1;
	myAllyTeam = 1;
	myPlayingTeam = -1;
	myPlayingAllyTeam = -1;

	spectating           = false;
	spectatingFullView   = false;
	spectatingFullSelect = false;

	fpsMode = false;
	globalQuit = false;
	globalReload = false;

	guRNG.Seed(time(nullptr) % ((spring_gettime().toNanoSecsi() + 1) * 9007));
	playerHandler.ResetState();
}

void CGlobalUnsynced::LoadFromSetup(const CGameSetup* setup)
{
	// do not call here; AddPlayer can precede LoadFromSetup
	// playerHandler.ResetState();
	playerHandler.LoadFromSetup(setup);
}


void CGlobalUnsynced::SetMyPlayer(const int myNumber)
{
	const CPlayer* myPlayer = playerHandler.Player(myPlayerNum = myNumber);

	myTeam = myPlayer->team;
	if (!teamHandler.IsValidTeam(myTeam))
		throw content_error("Invalid MyTeam in player setup");

	myAllyTeam = teamHandler.AllyTeam(myTeam);
	if (!teamHandler.IsValidAllyTeam(myAllyTeam))
		throw content_error("Invalid MyAllyTeam in player setup");

	spectating           = myPlayer->spectator;
	spectatingFullView   = myPlayer->spectator;
	spectatingFullSelect = myPlayer->spectator;

	if (spectating)
		return;

	myPlayingTeam = myTeam;
	myPlayingAllyTeam = myAllyTeam;
}

CPlayer* CGlobalUnsynced::GetMyPlayer() {
	return (playerHandler.Player(myPlayerNum));
}