1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 /**
4  * @brief Globally accessible stuff
5  * Contains implementation of synced and unsynced global stuff.
6  */
7 
8 #include "GlobalUnsynced.h"
9 
10 #include "Game/Players/Player.h"
11 #include "Game/Players/PlayerHandler.h"
12 #include "Sim/Misc/TeamHandler.h"
13 #include "Sim/Misc/GlobalConstants.h" // for RANDINT_MAX
14 #include "Sim/Units/Unit.h" // required by CREG
15 #include "System/Config/ConfigHandler.h"
16 #include "System/Exceptions.h"
17 #include "System/Util.h"
18 #include "System/creg/creg_cond.h"
19 #include "System/Misc/SpringTime.h"
20 #include "System/Sync/SyncTracer.h"
21 
22 #include <time.h>
23 
24 
25 /**
26  * @brief global unsynced
27  *
28  * Global instance of CGlobalUnsynced
29  */
30 CGlobalUnsynced* gu;
31 
32 const float CGlobalUnsynced::reconnectSimDrawBalance = 0.15f;
33 UnsyncedRNG CGlobalUnsynced::rng;
34 
35 CR_BIND(CGlobalUnsynced, )
36 
37 CR_REG_METADATA(CGlobalUnsynced, (
38 	CR_IGNORED(simFPS),
39 	CR_IGNORED(avgSimFrameTime),
40 	CR_IGNORED(avgDrawFrameTime),
41 	CR_IGNORED(avgFrameTime),
42 	CR_MEMBER(modGameTime),
43 	CR_MEMBER(gameTime),
44 	CR_MEMBER(startTime),
45 	CR_MEMBER(myPlayerNum),
46 	CR_MEMBER(myTeam),
47 	CR_MEMBER(myAllyTeam),
48 	CR_MEMBER(myPlayingTeam),
49 	CR_MEMBER(myPlayingAllyTeam),
50 	CR_MEMBER(spectating),
51 	CR_MEMBER(spectatingFullView),
52 	CR_MEMBER(spectatingFullSelect),
53 	CR_IGNORED(fpsMode),
54 	CR_IGNORED(globalQuit)
55 ))
56 
CGlobalUnsynced()57 CGlobalUnsynced::CGlobalUnsynced()
58 {
59 	unsigned seed = time(NULL) % ((spring_gettime().toNanoSecsi() + 1) * 9007);
60 	rng.Seed(seed);
61 
62 	simFPS = 0.0f;
63 
64 	avgSimFrameTime = 0.001f;
65 	avgDrawFrameTime = 0.001f;
66 	avgFrameTime = 0.001f;
67 
68 	modGameTime = 0;
69 	gameTime = 0;
70 	startTime = 0;
71 
72 	myPlayerNum = 0;
73 	myTeam = 1;
74 	myAllyTeam = 1;
75 	myPlayingTeam = -1;
76 	myPlayingAllyTeam = -1;
77 
78 	spectating           = false;
79 	spectatingFullView   = false;
80 	spectatingFullSelect = false;
81 
82 	fpsMode = false;
83 	globalQuit = false;
84 
85 	playerHandler = new CPlayerHandler();
86 }
87 
~CGlobalUnsynced()88 CGlobalUnsynced::~CGlobalUnsynced()
89 {
90 	SafeDelete(playerHandler);
91 }
92 
93 
94 
LoadFromSetup(const CGameSetup * setup)95 void CGlobalUnsynced::LoadFromSetup(const CGameSetup* setup)
96 {
97 	playerHandler->LoadFromSetup(setup);
98 }
99 
SetMyPlayer(const int myNumber)100 void CGlobalUnsynced::SetMyPlayer(const int myNumber)
101 {
102 	myPlayerNum = myNumber;
103 
104 #ifdef TRACE_SYNC
105 	tracefile.Initialize(myPlayerNum);
106 #endif
107 
108 	const CPlayer* myPlayer = playerHandler->Player(myPlayerNum);
109 
110 	myTeam = myPlayer->team;
111 	if (!teamHandler->IsValidTeam(myTeam)) {
112 		throw content_error("Invalid MyTeam in player setup");
113 	}
114 
115 	myAllyTeam = teamHandler->AllyTeam(myTeam);
116 	if (!teamHandler->IsValidAllyTeam(myAllyTeam)) {
117 		throw content_error("Invalid MyAllyTeam in player setup");
118 	}
119 
120 	spectating           = myPlayer->spectator;
121 	spectatingFullView   = myPlayer->spectator;
122 	spectatingFullSelect = myPlayer->spectator;
123 
124 	if (!spectating) {
125 		myPlayingTeam = myTeam;
126 		myPlayingAllyTeam = myAllyTeam;
127 	}
128 }
129 
GetMyPlayer()130 CPlayer* CGlobalUnsynced::GetMyPlayer() {
131 	return (playerHandler->Player(myPlayerNum));
132 }
133