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