1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef _GLOBAL_SYNCED_H
4 #define _GLOBAL_SYNCED_H
5 
6 #include <string>
7 
8 #include "System/float3.h"
9 #include "System/creg/creg_cond.h"
10 #include "GlobalConstants.h"
11 
12 
13 class CGameSetup;
14 class CTeam;
15 class CPlayer;
16 
17 /**
18  * @brief Global synced data
19  *
20  * Class contains globally accessible
21  * data that remains synced.
22  */
23 class CGlobalSynced
24 {
25 public:
26 	CR_DECLARE_STRUCT(CGlobalSynced)
27 
28 	CGlobalSynced();  //!< Constructor
29 	~CGlobalSynced(); //!< Destructor
30 	void LoadFromSetup(const CGameSetup*);
31 
32 	int    randInt();    //!< synced random int
33 	float  randFloat();  //!< synced random float
34 	float3 randVector(); //!< synced random vector
35 
36 	void SetRandSeed(unsigned int seed, bool init = false) {
37 		randSeed = seed;
38 		if (init) { initRandSeed = randSeed; }
39 	}
GetRandSeed()40 	unsigned int GetRandSeed()     const { return randSeed; }
GetInitRandSeed()41 	unsigned int GetInitRandSeed() const { return initRandSeed; }
42 
43 public:
44 	/**
45 	* @brief frame number
46 	*
47 	* Stores the current frame number
48 	*/
49 	int frameNum;
50 
51 	/**
52 	* @brief speed factor
53 	*
54 	* Contains the actual gamespeed factor
55 	* used by the game. The real gamespeed
56 	* can be up to this but is lowered if
57 	* clients can't keep up (lag protection)
58 	*/
59 	float speedFactor;
60 
61 	/**
62 	* @brief wanted speed factor
63 	*
64 	* Contains the aimed speed factor.
65 	* The total simframes
66 	* per second calculate as follow:
67 	* wantedSimFPS = speedFactor * GAME_SPEED;
68 	*/
69 	float wantedSpeedFactor;
70 
71 	/**
72 	* @brief paused
73 	*
74 	* Holds whether the game is paused
75 	*/
76 	bool paused;
77 
78 	/**
79 	* @brief map x
80 	*
81 	* The map's number of squares in the x direction
82 	* (note that the number of vertices is one more)
83 	*/
84 	int mapx;
85 	int mapxm1; // mapx minus one
86 	int mapxp1; // mapx plus one
87 
88 	/**
89 	* @brief map y
90 	*
91 	* The map's number of squares in the y direction
92 	*/
93 	int mapy;
94 	int mapym1; // mapy minus one
95 	int mapyp1; // mapy plus one
96 
97 	/**
98 	* @brief map squares
99 	*
100 	* Total number of squares on the map
101 	*/
102 	int mapSquares;
103 
104 	/**
105 	* @brief half map x
106 	*
107 	* Contains half of the number of squares in the x direction
108 	*/
109 	int hmapx;
110 
111 	/**
112 	* @brief half map y
113 	*
114 	* Contains half of the number of squares in the y direction
115 	*/
116 	int hmapy;
117 
118 	/**
119 	* @brief map x power of 2
120 	*
121 	* Map's size in the x direction rounded
122 	* up to the next power of 2
123 	*/
124 	int pwr2mapx;
125 
126 	/**
127 	* @brief map y power of 2
128 	*
129 	* Map's size in the y direction rounded
130 	* up to the next power of 2
131 	*/
132 	int pwr2mapy;
133 
134 	/**
135 	* @brief temp num
136 	*
137 	* Used for getting temporary but unique numbers
138 	* (increase after each use)
139 	*/
140 	int tempNum;
141 
142 	/**
143 	* @brief god mode
144 	*
145 	* Whether god-mode is enabled, which allows all players (even spectators)
146 	* to control all units.
147 	*/
148 	bool godMode;
149 
150 	/**
151 	* @brief global line-of-sight
152 	*
153 	* Whether everything on the map is visible at all times to a given ALLYteam
154 	* There can never be more allyteams than teams, hence the size is MAX_TEAMS
155 	*/
156 	bool globalLOS[MAX_TEAMS];
157 
158 	/**
159 	* @brief cheat enabled
160 	*
161 	* Whether cheating is enabled
162 	*/
163 	bool cheatEnabled;
164 
165 	/**
166 	* @brief disable helper AIs
167 	*
168 	* Whether helper AIs are allowed, including LuaUI control widgets
169 	*/
170 	bool noHelperAIs;
171 
172 	/**
173 	* @brief definition editing enabled
174 	*
175 	* Whether editing of unit-, feature- and weapon-defs through Lua is enabled.
176 	*/
177 	bool editDefsEnabled;
178 
179 	/**
180 	* @brief LuaGaia control
181 	*
182 	* Whether or not LuaGaia is enabled
183 	*/
184 	bool useLuaGaia;
185 
186 private:
187 	/**
188 	* @brief random seed
189 	*
190 	* Holds the synced random seed
191 	*/
192 	int randSeed;
193 
194 	/**
195 	* @brief initial random seed
196 	*
197 	* Holds the synced initial random seed
198 	*/
199 	int initRandSeed;
200 };
201 
202 extern CGlobalSynced* gs;
203 
204 
205 class SyncedRNG
206 {
207 public:
operator()208 	int operator()(unsigned N)
209 	{
210 		return gs->randInt()%N;
211 	};
212 };
213 
214 #endif // _GLOBAL_SYNCED_H
215