Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Simulation2.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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_SIMULATION2
19#define INCLUDED_SIMULATION2
20
25
26#include <ostream>
27#include <string>
28#include <unordered_map>
29#include <vector>
30
31class CFrustum;
32class CMessage;
33class CSimContext;
35class CTerrain;
36class CUnitManager;
37class IComponent;
38class SceneCollector;
39class ScriptInterface;
40class ScriptContext;
41
42/**
43 * Public API for simulation system.
44 * Most code should interact with the simulation only through this API.
45 */
47{
49
50public:
51 // TODO: CUnitManager should probably be handled automatically by this
52 // module, but for now we'll have it passed in externally instead
53 CSimulation2(CUnitManager* unitManager, ScriptContext& cx, CTerrain* terrain);
55
57 void EnableRejoinTest(int rejoinTestTurn);
58 void EnableOOSLog();
59
60 /**
61 * Load all scripts in the specified directory (non-recursively),
62 * so they can register new component types and functions. This
63 * should be called immediately after constructing the CSimulation2 object.
64 * @return false on failure
65 */
66 bool LoadScripts(const VfsPath& path);
67
68 /**
69 * Call LoadScripts for each of the game's standard simulation script paths.
70 * @return false on failure
71 */
72 bool LoadDefaultScripts();
73
74 /**
75 * Loads the player settings script (called before map is loaded)
76 * @param newPlayers will delete all the existing player entities (if any) and create new ones
77 * (needed for loading maps, but Atlas might want to update existing player data)
78 */
79 void LoadPlayerSettings(bool newPlayers);
80
81 /**
82 * Loads the map settings script (called after map is loaded)
83 */
84 void LoadMapSettings();
85
86 /**
87 * Set a startup script, which will get executed before the first turn.
88 */
89 void SetStartupScript(const std::string& script);
90
91 /**
92 * Get the current startup script.
93 */
94 const std::string& GetStartupScript();
95
96 /**
97 * Set the attributes identifying the scenario/RMS used to initialise this
98 * simulation.
99 */
100 void SetInitAttributes(JS::HandleValue settings);
101
102 /**
103 * Get the data passed to SetInitAttributes.
104 */
105 JS::Value GetInitAttributes();
106 void GetInitAttributes(JS::MutableHandleValue ret);
107
108 /**
109 * Set the initial map settings (as a UTF-8-encoded JSON string),
110 * which will be used to set up the simulation state.
111 * Called from atlas.
112 */
113 void SetMapSettings(const std::string& settings);
114
115 /**
116 * Set the initial map settings, which will be used
117 * to set up the simulation state.
118 * Called from MapReader (for all map-types).
119 */
120 void SetMapSettings(JS::HandleValue settings);
121
122 /**
123 * Get the current map settings as a UTF-8 JSON string.
124 */
125 std::string GetMapSettingsString();
126
127 /**
128 * Get the current map settings.
129 */
130 void GetMapSettings(JS::MutableHandleValue ret);
131
132 /**
133 * RegMemFun incremental loader function.
134 */
135 int ProgressiveLoad();
136
137 /**
138 * Reload any scripts that were loaded from the given filename.
139 * (This is used to implement hotloading.)
140 */
141 Status ReloadChangedFile(const VfsPath& path);
142
143 /**
144 * Initialise (or re-initialise) the complete simulation state.
145 * Must be called after LoadScripts, and must be called
146 * before any methods that depend on the simulation state.
147 * @param skipScriptedComponents don't load the scripted system components
148 * (this is intended for use by test cases that don't mount all of VFS)
149 * @param skipAI don't initialise the AI system
150 * (this is intended for use by test cases that don't want all entity
151 * templates loaded automatically)
152 */
153 void ResetState(bool skipScriptedComponents = false, bool skipAI = false);
154
155 /**
156 * Replace/destroy some entities (e.g. skirmish replacers)
157 * Called right before InitGame, on CGame instantiation.
158 * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
159 * This calls the PreInitGame function defined in helpers/InitGame.js.
160 */
161 void PreInitGame();
162
163 /**
164 * Initialise a new game, based on some script data. (Called on CGame instantiation)
165 * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
166 * This calls the InitGame function defined in helpers/InitGame.js.
167 */
168 void InitGame();
169
170 void Update(int turnLength);
171 void Update(int turnLength, const std::vector<SimulationCommand>& commands);
172 void Interpolate(float simFrameLength, float frameOffset, float realFrameLength);
173 void RenderSubmit(SceneCollector& collector, const CFrustum& frustum, bool culling);
174
175 /**
176 * Returns the last frame offset passed to Interpolate(), i.e. the offset corresponding
177 * to the currently-rendered scene.
178 */
179 float GetLastFrameOffset() const;
180
181 /**
182 * Construct a new entity and add it to the world.
183 * @param templateName see ICmpTemplateManager for syntax
184 * @return the new entity ID, or INVALID_ENTITY on error
185 */
186 entity_id_t AddEntity(const std::wstring& templateName);
187 entity_id_t AddEntity(const std::wstring& templateName, entity_id_t preferredId);
188 entity_id_t AddLocalEntity(const std::wstring& templateName);
189
190 /**
191 * Destroys the specified entity, once FlushDestroyedEntities is called.
192 * Has no effect if the entity does not exist, or has already been added to the destruction queue.
193 */
194 void DestroyEntity(entity_id_t ent);
195
196 /**
197 * Does the actual destruction of entities from DestroyEntity.
198 * This is called automatically by Update, but should also be called at other
199 * times when an entity might have been deleted and should be removed from
200 * any further processing (e.g. after editor UI message processing)
201 */
203
204 IComponent* QueryInterface(entity_id_t ent, int iid) const;
205 void PostMessage(entity_id_t ent, const CMessage& msg) const;
206 void BroadcastMessage(const CMessage& msg) const;
207
209 std::vector<std::pair<entity_id_t, IComponent*> >;
210
212 std::unordered_map<entity_id_t, IComponent*>;
213
214 /**
215 * Returns a list of components implementing the given interface, and their
216 * associated entities, sorted by entity ID.
217 */
219
220 /**
221 * Returns a list of components implementing the given interface, and their
222 * associated entities, as an unordered map.
223 */
225
226 const CSimContext& GetSimContext() const;
228
229 bool ComputeStateHash(std::string& outHash, bool quick);
230 bool DumpDebugState(std::ostream& stream);
231 bool SerializeState(std::ostream& stream);
232 bool DeserializeState(std::istream& stream);
233
234 /**
235 * Activate the rejoin-test feature for turn @param turn.
236 */
237 void ActivateRejoinTest(int turn);
238
239 std::string GenerateSchema();
240
241 /////////////////////////////////////////////////////////////////////////////
242 // Some functions for Atlas UI to be able to access VFS data
243
244 /**
245 * Get random map script data
246 *
247 * @return vector of strings containing JSON format data
248 */
249 std::vector<std::string> GetRMSData();
250
251 /**
252 * Get victory condition data
253 *
254 * @return vector of strings containing JSON format data
255 */
256 std::vector<std::string> GetVictoryConditiondData();
257
258 /**
259 * Get player default data
260 *
261 * @return string containing JSON format data
262 */
263 std::string GetPlayerDefaults();
264
265 /**
266 * Get map sizes data
267 *
268 * @return string containing JSON format data
269 */
270 std::string GetMapSizes();
271
272 /**
273 * Get AI data
274 *
275 * @return string containing JSON format data
276 */
277 std::string GetAIData();
278
279private:
281};
282
283#endif // INCLUDED_SIMULATION2
Definition: Frustum.h:37
Definition: Message.h:26
Contains pointers to various 'global' objects that are needed by the simulation code,...
Definition: SimContext.h:33
Definition: Simulation2.cpp:54
Public API for simulation system.
Definition: Simulation2.h:47
int ProgressiveLoad()
RegMemFun incremental loader function.
Definition: Simulation2.cpp:862
void PreInitGame()
Replace/destroy some entities (e.g.
Definition: Simulation2.cpp:730
bool LoadScripts(const VfsPath &path)
Load all scripts in the specified directory (non-recursively), so they can register new component typ...
Definition: Simulation2.cpp:778
const CSimContext & GetSimContext() const
Definition: Simulation2.cpp:720
void FlushDestroyedEntities()
Does the actual destruction of entities from DestroyEntity.
Definition: Simulation2.cpp:690
entity_id_t AddLocalEntity(const std::wstring &templateName)
Definition: Simulation2.cpp:680
float GetLastFrameOffset() const
Returns the last frame offset passed to Interpolate(), i.e.
Definition: Simulation2.cpp:773
void SetInitAttributes(JS::HandleValue settings)
Set the attributes identifying the scenario/RMS used to initialise this simulation.
Definition: Simulation2.cpp:798
std::unordered_map< entity_id_t, IComponent * > InterfaceListUnordered
Definition: Simulation2.h:212
entity_id_t AddEntity(const std::wstring &templateName)
Construct a new entity and add it to the world.
Definition: Simulation2.cpp:670
const std::string & GetStartupScript()
Get the current startup script.
Definition: Simulation2.cpp:793
std::vector< std::string > GetVictoryConditiondData()
Get victory condition data.
Definition: Simulation2.cpp:947
void RenderSubmit(SceneCollector &collector, const CFrustum &frustum, bool culling)
Definition: Simulation2.cpp:765
JS::Value GetInitAttributes()
Get the data passed to SetInitAttributes.
Definition: Simulation2.cpp:803
std::string GetAIData()
Get AI data.
Definition: Simulation2.cpp:982
void InitGame()
Initialise a new game, based on some script data.
Definition: Simulation2.cpp:737
CSimulation2(CUnitManager *unitManager, ScriptContext &cx, CTerrain *terrain)
Definition: Simulation2.cpp:637
void LoadPlayerSettings(bool newPlayers)
Loads the player settings script (called before map is loaded)
Definition: Simulation2.cpp:836
bool SerializeState(std::ostream &stream)
Definition: Simulation2.cpp:888
std::vector< std::pair< entity_id_t, IComponent * > > InterfaceList
Definition: Simulation2.h:209
ScriptInterface & GetScriptInterface() const
Definition: Simulation2.cpp:725
InterfaceList GetEntitiesWithInterface(int iid)
Returns a list of components implementing the given interface, and their associated entities,...
Definition: Simulation2.cpp:710
void BroadcastMessage(const CMessage &msg) const
Definition: Simulation2.cpp:705
std::string GenerateSchema()
Definition: Simulation2.cpp:907
void ActivateRejoinTest(int turn)
Activate the rejoin-test feature for turn.
Definition: Simulation2.cpp:899
bool LoadDefaultScripts()
Call LoadScripts for each of the game's standard simulation script paths.
Definition: Simulation2.cpp:783
void GetMapSettings(JS::MutableHandleValue ret)
Get the current map settings.
Definition: Simulation2.cpp:831
void SetMapSettings(const std::string &settings)
Set the initial map settings (as a UTF-8-encoded JSON string), which will be used to set up the simul...
Definition: Simulation2.cpp:813
~CSimulation2()
Definition: Simulation2.cpp:642
Status ReloadChangedFile(const VfsPath &path)
Reload any scripts that were loaded from the given filename.
Definition: Simulation2.cpp:867
void Interpolate(float simFrameLength, float frameOffset, float realFrameLength)
Definition: Simulation2.cpp:760
void PostMessage(entity_id_t ent, const CMessage &msg) const
Definition: Simulation2.cpp:700
void ResetState(bool skipScriptedComponents=false, bool skipAI=false)
Initialise (or re-initialise) the complete simulation state.
Definition: Simulation2.cpp:872
IComponent * QueryInterface(entity_id_t ent, int iid) const
Definition: Simulation2.cpp:695
void EnableRejoinTest(int rejoinTestTurn)
Definition: Simulation2.cpp:654
void EnableOOSLog()
Definition: Simulation2.cpp:659
bool DeserializeState(std::istream &stream)
Definition: Simulation2.cpp:893
std::string GetMapSettingsString()
Get the current map settings as a UTF-8 JSON string.
Definition: Simulation2.cpp:826
std::vector< std::string > GetRMSData()
Get random map script data.
Definition: Simulation2.cpp:942
void DestroyEntity(entity_id_t ent)
Destroys the specified entity, once FlushDestroyedEntities is called.
Definition: Simulation2.cpp:685
void EnableSerializationTest()
Definition: Simulation2.cpp:649
bool ComputeStateHash(std::string &outHash, bool quick)
Definition: Simulation2.cpp:877
NONCOPYABLE(CSimulation2)
bool DumpDebugState(std::ostream &stream)
Definition: Simulation2.cpp:882
CSimulation2Impl * m
Definition: Simulation2.h:280
void SetStartupScript(const std::string &script)
Set a startup script, which will get executed before the first turn.
Definition: Simulation2.cpp:788
void Update(int turnLength)
Definition: Simulation2.cpp:749
void LoadMapSettings()
Loads the map settings script (called after map is loaded)
Definition: Simulation2.cpp:843
std::string GetMapSizes()
Get map sizes data.
Definition: Simulation2.cpp:977
std::string GetPlayerDefaults()
Get player default data.
Definition: Simulation2.cpp:972
const InterfaceListUnordered & GetEntitiesWithInterfaceUnordered(int iid)
Returns a list of components implementing the given interface, and their associated entities,...
Definition: Simulation2.cpp:715
Definition: Terrain.h:52
Definition: UnitManager.h:38
Definition: IComponent.h:33
Definition: path.h:80
This interface accepts renderable objects.
Definition: Scene.h:90
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptContext.h:46
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:72
u32 entity_id_t
Entity ID type.
Definition: Entity.h:29
i64 Status
Error handling system.
Definition: status.h:173