Line data Source code
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"
25 : #include "scriptinterface/ScriptTypes.h"
26 : #include "simulation2/helpers/Player.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 : {
44 : NONCOPYABLE(CGame);
45 :
46 : /**
47 : * pointer to the CWorld object representing the game world.
48 : **/
49 : CWorld *m_World;
50 :
51 : /**
52 : * pointer to the CSimulation2 object operating on the game world.
53 : **/
54 : CSimulation2 *m_Simulation2;
55 :
56 : /**
57 : * pointer to the CGameView object representing the view into the game world.
58 : **/
59 : CGameView *m_GameView;
60 :
61 : /**
62 : * the game has been initialized and ready for use if true.
63 : **/
64 : bool m_GameStarted;
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 : */
75 : player_id_t m_PlayerID;
76 :
77 : /**
78 : * Differs from m_PlayerID if a defeated player or observer views another player.
79 : */
80 : player_id_t m_ViewedPlayerID;
81 :
82 : CTurnManager* m_TurnManager;
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);
94 : PSRETURN ReallyStartGame();
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 0 : inline bool IsGameStarted() const
141 : {
142 0 : return m_GameStarted;
143 : }
144 :
145 : /**
146 : * Get m_IsVisualReplay.
147 : *
148 : * @return bool the value of m_IsVisualReplay.
149 : **/
150 0 : inline bool IsVisualReplay() const
151 0 : { 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 0 : inline CWorld *GetWorld()
159 0 : { 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 0 : inline CGameView *GetView()
167 0 : { return m_GameView; }
168 :
169 : /**
170 : * Get the pointer to the simulation2 object.
171 : *
172 : * @return CSimulation2 * the value of m_Simulation2.
173 : **/
174 0 : inline CSimulation2 *GetSimulation2()
175 0 : { 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 0 : inline void SetSimRate(float simRate)
186 0 : { if (std::isfinite(simRate)) m_SimRate = std::max(simRate, 0.0f); }
187 :
188 0 : inline float GetSimRate() const
189 0 : { return m_SimRate; }
190 :
191 0 : inline OsPath GetReplayPath() const
192 0 : { 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 :
200 0 : CTurnManager* GetTurnManager() const
201 0 : { return m_TurnManager; }
202 :
203 0 : IReplayLogger& GetReplayLogger() const
204 0 : { return *m_ReplayLogger; }
205 :
206 : private:
207 : static const CStr EventNameSimulationUpdate;
208 :
209 : void RegisterInit(const JS::HandleValue attribs, const std::string& savedState);
210 : IReplayLogger* m_ReplayLogger;
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();
219 : OsPath m_ReplayPath;
220 : bool m_IsVisualReplay;
221 : std::istream* m_ReplayStream;
222 : u32 m_FinalReplayTurn;
223 : };
224 :
225 : extern CGame *g_Game;
226 :
227 : #endif
|