Pyrogenesis  trunk
Game.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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 "graphics/Color.h"
22 #include "ps/CStr.h"
23 #include "ps/Errors.h"
24 #include "lib/os_path.h"
27 
28 #include <vector>
29 
30 class CGameView;
31 class CSimulation2;
32 class CTurnManager;
33 class CWorld;
34 class IReplayLogger;
35 
36 /**
37  * The container that holds the rules, resources and attributes of the game.
38  * The CGame object is responsible for creating a game that is defined by
39  * a set of attributes provided. The CGame object is also responsible for
40  * maintaining the relations between CPlayer and CWorld, CSimulation and CWorld.
41  **/
42 class CGame
43 {
45 
46  /**
47  * pointer to the CWorld object representing the game world.
48  **/
50 
51  /**
52  * pointer to the CSimulation2 object operating on the game world.
53  **/
55 
56  /**
57  * pointer to the CGameView object representing the view into the game world.
58  **/
60 
61  /**
62  * the game has been initialized and ready for use if true.
63  **/
65 
66  /**
67  * Timescale multiplier for simulation rate.
68  **/
69  float m_SimRate;
70 
71  /**
72  * Index assigned to the current player.
73  * 1-8 to control players, 0 for gaia, -1 for observer.
74  */
76 
77  /**
78  * Differs from m_PlayerID if a defeated player or observer views another player.
79  */
81 
83 
84 public:
85  CGame(bool replayLog);
86  ~CGame();
87 
88  /**
89  * the game is paused and no updates will be performed if true.
90  **/
91  bool m_Paused;
92 
93  void StartGame(JS::MutableHandleValue attribs, const std::string& savedState);
95 
96  bool StartVisualReplay(const OsPath& replayPath);
97 
98  /**
99  * Periodic heartbeat that controls the process. performs all per-frame updates.
100  * Simulation update is called and game status update is called.
101  *
102  * @param deltaRealTime Elapsed real time since last beat/frame, in seconds.
103  * @param doInterpolate Perform graphics interpolation if true.
104  * @return bool false if it can't keep up with the desired simulation rate
105  * indicating that you might want to render less frequently.
106  */
107  void Update(const double deltaRealTime, bool doInterpolate = true);
108 
109  void Interpolate(float simFrameLength, float realFrameLength);
110 
111  int GetPlayerID();
112  void SetPlayerID(player_id_t playerID);
113 
114  int GetViewedPlayerID();
115  void SetViewedPlayerID(player_id_t playerID);
116 
117  /**
118  * Check if the game is finished by testing if there's a winner.
119  * It is used to end a non visual autostarted game.
120  *
121  * @return true if there's a winner, false otherwise.
122  */
123  bool IsGameFinished() const;
124 
125  /**
126  * Retrieving player colors from scripts is slow, so this updates an
127  * internal cache of all players' colors.
128  * Call this just before rendering, so it will always have the latest
129  * colors.
130  */
131  void CachePlayerColors();
132 
133  const CColor& GetPlayerColor(player_id_t player) const;
134 
135  /**
136  * Get m_GameStarted.
137  *
138  * @return bool the value of m_GameStarted.
139  **/
140  inline bool IsGameStarted() const
141  {
142  return m_GameStarted;
143  }
144 
145  /**
146  * Get m_IsVisualReplay.
147  *
148  * @return bool the value of m_IsVisualReplay.
149  **/
150  inline bool IsVisualReplay() const
151  { return m_IsVisualReplay; }
152 
153  /**
154  * Get the pointer to the game world object.
155  *
156  * @return CWorld * the value of m_World.
157  **/
158  inline CWorld *GetWorld()
159  { return m_World; }
160 
161  /**
162  * Get the pointer to the game view object.
163  *
164  * @return CGameView * the value of m_GameView.
165  **/
166  inline CGameView *GetView()
167  { return m_GameView; }
168 
169  /**
170  * Get the pointer to the simulation2 object.
171  *
172  * @return CSimulation2 * the value of m_Simulation2.
173  **/
175  { return m_Simulation2; }
176 
177  /**
178  * Set the simulation scale multiplier.
179  *
180  * @param simRate Float value to set m_SimRate to.
181  * Because m_SimRate is also used to
182  * scale TimeSinceLastFrame it must be
183  * clamped to 0.0f.
184  **/
185  inline void SetSimRate(float simRate)
186  { if (std::isfinite(simRate)) m_SimRate = std::max(simRate, 0.0f); }
187 
188  inline float GetSimRate() const
189  { return m_SimRate; }
190 
191  inline OsPath GetReplayPath() const
192  { return m_ReplayPath; }
193 
194  /**
195  * Replace the current turn manager.
196  * This class will take ownership of the pointer.
197  */
198  void SetTurnManager(CTurnManager* turnManager);
199 
201  { return m_TurnManager; }
202 
204  { return *m_ReplayLogger; }
205 
206 private:
207  static const CStr EventNameSimulationUpdate;
208 
209  void RegisterInit(const JS::HandleValue attribs, const std::string& savedState);
211 
212  std::vector<CColor> m_PlayerColors;
213 
214  int LoadInitialState();
215  std::string m_InitialSavedState; // valid between RegisterInit and LoadInitialState
216  bool m_IsSavedGame; // true if loading a saved game; false for a new game
217 
218  int LoadVisualReplayData();
221  std::istream* m_ReplayStream;
223 };
224 
225 extern CGame *g_Game;
226 
227 #endif
The container that holds the rules, resources and attributes of the game.
Definition: Game.h:42
player_id_t m_PlayerID
Index assigned to the current player.
Definition: Game.h:75
CTurnManager * m_TurnManager
Definition: Game.h:82
void CachePlayerColors()
Retrieving player colors from scripts is slow, so this updates an internal cache of all players&#39; colo...
Definition: Game.cpp:429
void RegisterInit(const JS::HandleValue attribs, const std::string &savedState)
Initializes the game with the set of attributes provided.
Definition: Game.cpp:210
Definition: Color.h:42
int LoadInitialState()
Definition: Game.cpp:276
CWorld * m_World
pointer to the CWorld object representing the game world.
Definition: Game.h:49
bool m_IsSavedGame
Definition: Game.h:216
void SetPlayerID(player_id_t playerID)
Definition: Game.cpp:354
Replay log recorder interface.
Definition: Replay.h:35
bool m_IsVisualReplay
Definition: Game.h:220
~CGame()
Destructor.
Definition: Game.cpp:103
Common turn system (used by clients and offline games).
Definition: TurnManager.h:79
static const CStr EventNameSimulationUpdate
Definition: Game.h:207
CGameView * m_GameView
pointer to the CGameView object representing the view into the game world.
Definition: Game.h:59
Public API for simulation system.
Definition: Simulation2.h:46
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
int GetPlayerID()
Definition: Game.cpp:349
bool IsVisualReplay() const
Get m_IsVisualReplay.
Definition: Game.h:150
std::vector< CColor > m_PlayerColors
Definition: Game.h:212
void Interpolate(float simFrameLength, float realFrameLength)
Definition: Game.cpp:418
CGame(bool replayLog)
Constructor.
Definition: Game.cpp:70
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
player_id_t m_ViewedPlayerID
Differs from m_PlayerID if a defeated player or observer views another player.
Definition: Game.h:80
Definition: path.h:79
bool IsGameStarted() const
Get m_GameStarted.
Definition: Game.h:140
bool m_Paused
the game is paused and no updates will be performed if true.
Definition: Game.h:91
std::string m_InitialSavedState
Definition: Game.h:215
Definition: GameView.h:36
void SetTurnManager(CTurnManager *turnManager)
Replace the current turn manager.
Definition: Game.cpp:120
bool IsGameFinished() const
Check if the game is finished by testing if there&#39;s a winner.
Definition: Game.cpp:458
void SetSimRate(float simRate)
Set the simulation scale multiplier.
Definition: Game.h:185
const CColor & GetPlayerColor(player_id_t player) const
Definition: Game.cpp:450
CSimulation2 * GetSimulation2()
Get the pointer to the simulation2 object.
Definition: Game.h:174
OsPath GetReplayPath() const
Definition: Game.h:191
CGameView * GetView()
Get the pointer to the game view object.
Definition: Game.h:166
void StartGame(JS::MutableHandleValue attribs, const std::string &savedState)
Definition: Game.cpp:373
int LoadVisualReplayData()
Definition: Game.cpp:131
std::istream * m_ReplayStream
Definition: Game.h:221
CWorld * GetWorld()
Get the pointer to the game world object.
Definition: Game.h:158
OsPath m_ReplayPath
Definition: Game.h:219
void Update(const double deltaRealTime, bool doInterpolate=true)
Periodic heartbeat that controls the process.
Definition: Game.cpp:385
u32 m_FinalReplayTurn
Definition: Game.h:222
float m_SimRate
Timescale multiplier for simulation rate.
Definition: Game.h:69
PSRETURN ReallyStartGame()
Game initialization has been completed.
Definition: Game.cpp:301
IReplayLogger & GetReplayLogger() const
Definition: Game.h:203
IReplayLogger * m_ReplayLogger
Definition: Game.h:210
float GetSimRate() const
Definition: Game.h:188
int GetViewedPlayerID()
Definition: Game.cpp:363
bool m_GameStarted
the game has been initialized and ready for use if true.
Definition: Game.h:64
CTurnManager * GetTurnManager() const
Definition: Game.h:200
bool StartVisualReplay(const OsPath &replayPath)
Definition: Game.cpp:178
void SetViewedPlayerID(player_id_t playerID)
Definition: Game.cpp:368
NONCOPYABLE(CGame)
CSimulation2 * m_Simulation2
pointer to the CSimulation2 object operating on the game world.
Definition: Game.h:54
CGame * g_Game
Globally accessible pointer to the CGame object.
Definition: Game.cpp:62