Pyrogenesis  trunk
Game.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_GAME
19 #define INCLUDED_GAME
20 
21 #include "ps/Errors.h"
22 #include <vector>
23 
26 
27 class CWorld;
28 class CSimulation2;
29 class CGameView;
30 class CTurnManager;
31 class IReplayLogger;
32 struct CColor;
33 
34 /**
35  * The container that holds the rules, resources and attributes of the game.
36  * The CGame object is responsible for creating a game that is defined by
37  * a set of attributes provided. The CGame object is also responsible for
38  * maintaining the relations between CPlayer and CWorld, CSimulation and CWorld.
39  **/
40 class CGame
41 {
43 
44  /**
45  * pointer to the CWorld object representing the game world.
46  **/
48 
49  /**
50  * pointer to the CSimulation2 object operating on the game world.
51  **/
53 
54  /**
55  * pointer to the CGameView object representing the view into the game world.
56  **/
58 
59  /**
60  * the game has been initialized and ready for use if true.
61  **/
63 
64  /**
65  * Timescale multiplier for simulation rate.
66  **/
67  float m_SimRate;
68 
69  /**
70  * Index assigned to the current player.
71  * 1-8 to control players, 0 for gaia, -1 for observer.
72  */
74 
75  /**
76  * Differs from m_PlayerID if a defeated player or observer views another player.
77  */
79 
81 
82 public:
83  CGame(bool disableGraphics = false, bool replayLog = true);
84  ~CGame();
85 
86  /**
87  * the game is paused and no updates will be performed if true.
88  **/
89  bool m_Paused;
90 
91  void StartGame(JS::MutableHandleValue attribs, const std::string& savedState);
93 
94  bool StartVisualReplay(const std::string& replayPath);
95 
96  /**
97  * Periodic heartbeat that controls the process. performs all per-frame updates.
98  * Simulation update is called and game status update is called.
99  *
100  * @param deltaRealTime Elapsed real time since last beat/frame, in seconds.
101  * @param doInterpolate Perform graphics interpolation if true.
102  * @return bool false if it can't keep up with the desired simulation rate
103  * indicating that you might want to render less frequently.
104  */
105  void Update(const double deltaRealTime, bool doInterpolate = true);
106 
107  void Interpolate(float simFrameLength, float realFrameLength);
108 
109  int GetPlayerID();
110  void SetPlayerID(player_id_t playerID);
111 
112  int GetViewedPlayerID();
113  void SetViewedPlayerID(player_id_t playerID);
114 
115  /**
116  * Retrieving player colors from scripts is slow, so this updates an
117  * internal cache of all players' colors.
118  * Call this just before rendering, so it will always have the latest
119  * colors.
120  */
121  void CachePlayerColors();
122 
123  CColor GetPlayerColor(player_id_t player) const;
124 
125  /**
126  * Get m_GameStarted.
127  *
128  * @return bool the value of m_GameStarted.
129  **/
130  inline bool IsGameStarted() const
131  {
132  return m_GameStarted;
133  }
134 
135  /**
136  * Get m_IsVisualReplay.
137  *
138  * @return bool the value of m_IsVisualReplay.
139  **/
140  inline bool IsVisualReplay() const
141  { return m_IsVisualReplay; }
142 
143  /**
144  * Get the pointer to the game world object.
145  *
146  * @return CWorld * the value of m_World.
147  **/
148  inline CWorld *GetWorld()
149  { return m_World; }
150 
151  /**
152  * Get the pointer to the game view object.
153  *
154  * @return CGameView * the value of m_GameView.
155  **/
156  inline CGameView *GetView()
157  { return m_GameView; }
158 
159  /**
160  * Get the pointer to the simulation2 object.
161  *
162  * @return CSimulation2 * the value of m_Simulation2.
163  **/
165  { return m_Simulation2; }
166 
167  /**
168  * Set the simulation scale multiplier.
169  *
170  * @param simRate Float value to set m_SimRate to.
171  * Because m_SimRate is also used to
172  * scale TimeSinceLastFrame it must be
173  * clamped to 0.0f.
174  **/
175  inline void SetSimRate(float simRate)
176  { if (isfinite(simRate)) m_SimRate = std::max(simRate, 0.0f); }
177 
178  inline float GetSimRate() const
179  { return m_SimRate; }
180 
181  inline std::string GetReplayPath() const
182  { return m_ReplayPath; }
183 
184  /**
185  * Replace the current turn manager.
186  * This class will take ownership of the pointer.
187  */
188  void SetTurnManager(CTurnManager* turnManager);
189 
191  { return m_TurnManager; }
192 
194  { return *m_ReplayLogger; }
195 
196 private:
197  void RegisterInit(const JS::HandleValue attribs, const std::string& savedState);
199 
200  std::vector<CColor> m_PlayerColors;
201 
202  int LoadInitialState();
203  std::string m_InitialSavedState; // valid between RegisterInit and LoadInitialState
204  bool m_IsSavedGame; // true if loading a saved game; false for a new game
205 
206  int LoadVisualReplayData();
207  std::string m_ReplayPath;
209  std::istream* m_ReplayStream;
211 };
212 
213 extern CGame *g_Game;
214 
215 #endif
The container that holds the rules, resources and attributes of the game.
Definition: Game.h:40
CColor GetPlayerColor(player_id_t player) const
Definition: Game.cpp:457
player_id_t m_PlayerID
Index assigned to the current player.
Definition: Game.h:73
bool IsVisualReplay() const
Get m_IsVisualReplay.
Definition: Game.h:140
CTurnManager * m_TurnManager
Definition: Game.h:80
void CachePlayerColors()
Retrieving player colors from scripts is slow, so this updates an internal cache of all players&#39; colo...
Definition: Game.cpp:435
void RegisterInit(const JS::HandleValue attribs, const std::string &savedState)
Initializes the game with the set of attributes provided.
Definition: Game.cpp:204
Definition: Shapes.h:36
#define isfinite
Definition: posix.h:127
int LoadInitialState()
Definition: Game.cpp:266
CWorld * m_World
pointer to the CWorld object representing the game world.
Definition: Game.h:47
bool m_IsSavedGame
Definition: Game.h:204
void SetPlayerID(player_id_t playerID)
Definition: Game.cpp:350
Replay log recorder interface.
Definition: Replay.h:30
bool m_IsVisualReplay
Definition: Game.h:208
~CGame()
Destructor.
Definition: Game.cpp:99
This file defines the base class of the turn managers for clients, local games and replays...
Definition: TurnManager.h:57
CGameView * m_GameView
pointer to the CGameView object representing the view into the game world.
Definition: Game.h:57
Public API for simulation system.
Definition: Simulation2.h:47
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
int GetPlayerID()
Definition: Game.cpp:345
float GetSimRate() const
Definition: Game.h:178
CTurnManager * GetTurnManager() const
Definition: Game.h:190
std::vector< CColor > m_PlayerColors
Definition: Game.h:200
void Interpolate(float simFrameLength, float realFrameLength)
Definition: Game.cpp:424
uint32_t u32
Definition: types.h:39
CWorld is a general data class containing whatever is needed to accurately represent the world...
Definition: World.h:47
u32 PSRETURN
Definition: Errors.h:75
bool StartVisualReplay(const std::string &replayPath)
Definition: Game.cpp:171
player_id_t m_ViewedPlayerID
Differs from m_PlayerID if a defeated player or observer views another player.
Definition: Game.h:78
bool m_Paused
the game is paused and no updates will be performed if true.
Definition: Game.h:89
std::string m_ReplayPath
Definition: Game.h:207
std::string m_InitialSavedState
Definition: Game.h:203
Definition: GameView.h:37
CGame(bool disableGraphics=false, bool replayLog=true)
Constructor.
Definition: Game.cpp:66
void SetTurnManager(CTurnManager *turnManager)
Replace the current turn manager.
Definition: Game.cpp:113
void SetSimRate(float simRate)
Set the simulation scale multiplier.
Definition: Game.h:175
CSimulation2 * GetSimulation2()
Get the pointer to the simulation2 object.
Definition: Game.h:164
std::string GetReplayPath() const
Definition: Game.h:181
CGameView * GetView()
Get the pointer to the game view object.
Definition: Game.h:156
void StartGame(JS::MutableHandleValue attribs, const std::string &savedState)
Definition: Game.cpp:369
bool IsGameStarted() const
Get m_GameStarted.
Definition: Game.h:130
int LoadVisualReplayData()
Definition: Game.cpp:124
std::istream * m_ReplayStream
Definition: Game.h:209
CWorld * GetWorld()
Get the pointer to the game world object.
Definition: Game.h:148
void Update(const double deltaRealTime, bool doInterpolate=true)
Periodic heartbeat that controls the process.
Definition: Game.cpp:381
u32 m_FinalReplayTurn
Definition: Game.h:210
float m_SimRate
Timescale multiplier for simulation rate.
Definition: Game.h:67
PSRETURN ReallyStartGame()
Game initialization has been completed.
Definition: Game.cpp:291
IReplayLogger * m_ReplayLogger
Definition: Game.h:198
int GetViewedPlayerID()
Definition: Game.cpp:359
bool m_GameStarted
the game has been initialized and ready for use if true.
Definition: Game.h:62
void SetViewedPlayerID(player_id_t playerID)
Definition: Game.cpp:364
IReplayLogger & GetReplayLogger() const
Definition: Game.h:193
NONCOPYABLE(CGame)
CSimulation2 * m_Simulation2
pointer to the CSimulation2 object operating on the game world.
Definition: Game.h:52
CGame * g_Game
Globally accessible pointer to the CGame object.
Definition: Game.cpp:60