Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Game.h
Go to the documentation of this file.
1/* Copyright (C) 2023 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
30class CGameView;
31class CSimulation2;
32class CTurnManager;
33class CWorld;
34class 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 **/
42class 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
84public:
85 CGame(bool replayLog);
86 ~CGame();
87
88 /**
89 * the game is paused and no updates will be performed if true.
90 **/
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 **/
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
206private:
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(const std::string& savedState);
215 bool m_IsSavedGame; // true if loading a saved game; false for a new game
216
220 std::istream* m_ReplayStream;
222};
223
224extern CGame *g_Game;
225
226#endif
u32 PSRETURN
Definition: Errors.h:75
CGame * g_Game
Globally accessible pointer to the CGame object.
Definition: Game.cpp:62
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
Definition: GameView.h:37
The container that holds the rules, resources and attributes of the game.
Definition: Game.h:43
CGameView * GetView()
Get the pointer to the game view object.
Definition: Game.h:166
CGameView * m_GameView
pointer to the CGameView object representing the view into the game world.
Definition: Game.h:59
player_id_t m_ViewedPlayerID
Differs from m_PlayerID if a defeated player or observer views another player.
Definition: Game.h:80
bool m_Paused
the game is paused and no updates will be performed if true.
Definition: Game.h:91
CSimulation2 * m_Simulation2
pointer to the CSimulation2 object operating on the game world.
Definition: Game.h:54
CGame(bool replayLog)
Constructor.
Definition: Game.cpp:70
NONCOPYABLE(CGame)
static const CStr EventNameSimulationUpdate
Definition: Game.h:207
void SetViewedPlayerID(player_id_t playerID)
Definition: Game.cpp:377
bool IsGameStarted() const
Get m_GameStarted.
Definition: Game.h:140
void SetSimRate(float simRate)
Set the simulation scale multiplier.
Definition: Game.h:185
void Interpolate(float simFrameLength, float realFrameLength)
Definition: Game.cpp:427
~CGame()
Destructor.
Definition: Game.cpp:105
void RegisterInit(const JS::HandleValue attribs, const std::string &savedState)
Initializes the game with the set of attributes provided.
Definition: Game.cpp:212
void StartGame(JS::MutableHandleValue attribs, const std::string &savedState)
Definition: Game.cpp:382
int LoadVisualReplayData()
Definition: Game.cpp:133
IReplayLogger & GetReplayLogger() const
Definition: Game.h:203
u32 m_FinalReplayTurn
Definition: Game.h:221
OsPath GetReplayPath() const
Definition: Game.h:191
PSRETURN ReallyStartGame()
Game initialization has been completed.
Definition: Game.cpp:310
std::istream * m_ReplayStream
Definition: Game.h:220
int LoadInitialState(const std::string &savedState)
Definition: Game.cpp:289
void CachePlayerColors()
Retrieving player colors from scripts is slow, so this updates an internal cache of all players' colo...
Definition: Game.cpp:438
int GetPlayerID()
Definition: Game.cpp:358
bool StartVisualReplay(const OsPath &replayPath)
Definition: Game.cpp:180
float GetSimRate() const
Definition: Game.h:188
void Update(const double deltaRealTime, bool doInterpolate=true)
Periodic heartbeat that controls the process.
Definition: Game.cpp:394
bool m_IsSavedGame
Definition: Game.h:215
player_id_t m_PlayerID
Index assigned to the current player.
Definition: Game.h:75
CSimulation2 * GetSimulation2()
Get the pointer to the simulation2 object.
Definition: Game.h:174
CWorld * m_World
pointer to the CWorld object representing the game world.
Definition: Game.h:49
OsPath m_ReplayPath
Definition: Game.h:218
bool IsVisualReplay() const
Get m_IsVisualReplay.
Definition: Game.h:150
float m_SimRate
Timescale multiplier for simulation rate.
Definition: Game.h:69
CTurnManager * m_TurnManager
Definition: Game.h:82
int GetViewedPlayerID()
Definition: Game.cpp:372
CWorld * GetWorld()
Get the pointer to the game world object.
Definition: Game.h:158
const CColor & GetPlayerColor(player_id_t player) const
Definition: Game.cpp:459
bool m_IsVisualReplay
Definition: Game.h:219
std::vector< CColor > m_PlayerColors
Definition: Game.h:212
bool m_GameStarted
the game has been initialized and ready for use if true.
Definition: Game.h:64
bool IsGameFinished() const
Check if the game is finished by testing if there's a winner.
Definition: Game.cpp:467
IReplayLogger * m_ReplayLogger
Definition: Game.h:210
void SetPlayerID(player_id_t playerID)
Definition: Game.cpp:363
CTurnManager * GetTurnManager() const
Definition: Game.h:200
void SetTurnManager(CTurnManager *turnManager)
Replace the current turn manager.
Definition: Game.cpp:122
Public API for simulation system.
Definition: Simulation2.h:47
Common turn system (used by clients and offline games).
Definition: TurnManager.h:80
CWorld is a general data class containing whatever is needed to accurately represent the world.
Definition: World.h:50
Replay log recorder interface.
Definition: Replay.h:36
Definition: path.h:80
Definition: Color.h:43
uint32_t u32
Definition: types.h:39