Pyrogenesis  trunk
Simulation2.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 
21 #include "lib/file/vfs/vfs_path.h"
25 
26 #include <ostream>
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
30 
31 class CFrustum;
32 class CMessage;
33 class CSimContext;
34 class CSimulation2Impl;
35 class CTerrain;
36 class CUnitManager;
37 class IComponent;
38 class SceneCollector;
39 class ScriptInterface;
40 class ScriptContext;
41 
42 /**
43  * Public API for simulation system.
44  * Most code should interact with the simulation only through this API.
45  */
47 {
49 
50 public:
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, std::shared_ptr<ScriptContext> cx, CTerrain* terrain);
54  ~CSimulation2();
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  */
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  */
202  void FlushDestroyedEntities();
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 
208  using InterfaceList =
209  std::vector<std::pair<entity_id_t, IComponent*> >;
210 
211  using InterfaceListUnordered =
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 
279 private:
281 };
282 
283 #endif // INCLUDED_SIMULATION2
Definition: IComponent.h:32
JS::Value GetInitAttributes()
Get the data passed to SetInitAttributes.
Definition: Simulation2.cpp:804
IComponent * QueryInterface(entity_id_t ent, int iid) const
Definition: Simulation2.cpp:696
std::unordered_map< entity_id_t, IComponent * > InterfaceListUnordered
Definition: Simulation2.h:212
bool DeserializeState(std::istream &stream)
Definition: Simulation2.cpp:894
void ActivateRejoinTest(int turn)
Activate the rejoin-test feature for turn.
Definition: Simulation2.cpp:900
float GetLastFrameOffset() const
Returns the last frame offset passed to Interpolate(), i.e.
Definition: Simulation2.cpp:774
entity_id_t AddLocalEntity(const std::wstring &templateName)
Definition: Simulation2.cpp:681
void Interpolate(float simFrameLength, float frameOffset, float realFrameLength)
Definition: Simulation2.cpp:761
void ResetState(bool skipScriptedComponents=false, bool skipAI=false)
Initialise (or re-initialise) the complete simulation state.
Definition: Simulation2.cpp:873
Definition: Frustum.h:36
Definition: Terrain.h:51
const CSimContext & GetSimContext() const
Definition: Simulation2.cpp:721
void BroadcastMessage(const CMessage &msg) const
Definition: Simulation2.cpp:706
std::string GenerateSchema()
Definition: Simulation2.cpp:908
CSimulation2(CUnitManager *unitManager, std::shared_ptr< ScriptContext > cx, CTerrain *terrain)
Definition: Simulation2.cpp:638
void RenderSubmit(SceneCollector &collector, const CFrustum &frustum, bool culling)
Definition: Simulation2.cpp:766
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptContext.h:40
std::vector< std::string > GetVictoryConditiondData()
Get victory condition data.
Definition: Simulation2.cpp:948
const InterfaceListUnordered & GetEntitiesWithInterfaceUnordered(int iid)
Returns a list of components implementing the given interface, and their associated entities...
Definition: Simulation2.cpp:716
Contains pointers to various &#39;global&#39; objects that are needed by the simulation code, to allow easy access without using real (evil) global variables.
Definition: SimContext.h:32
std::vector< std::string > GetRMSData()
Get random map script data.
Definition: Simulation2.cpp:943
bool LoadDefaultScripts()
Call LoadScripts for each of the game&#39;s standard simulation script paths.
Definition: Simulation2.cpp:784
std::string GetMapSettingsString()
Get the current map settings as a UTF-8 JSON string.
Definition: Simulation2.cpp:827
~CSimulation2()
Definition: Simulation2.cpp:643
void Update(int turnLength)
Definition: Simulation2.cpp:750
Status ReloadChangedFile(const VfsPath &path)
Reload any scripts that were loaded from the given filename.
Definition: Simulation2.cpp:868
void PreInitGame()
Replace/destroy some entities (e.g.
Definition: Simulation2.cpp:731
Public API for simulation system.
Definition: Simulation2.h:46
This interface accepts renderable objects.
Definition: Scene.h:89
void InitGame()
Initialise a new game, based on some script data.
Definition: Simulation2.cpp:738
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:814
NONCOPYABLE(CSimulation2)
CSimulation2Impl * m
Definition: Simulation2.h:280
Definition: Simulation2.cpp:54
Config::Value_type Value
Definition: json_spirit_value.h:182
Definition: path.h:79
std::string GetPlayerDefaults()
Get player default data.
Definition: Simulation2.cpp:973
ScriptInterface & GetScriptInterface() const
Definition: Simulation2.cpp:726
bool LoadScripts(const VfsPath &path)
Load all scripts in the specified directory (non-recursively), so they can register new component typ...
Definition: Simulation2.cpp:779
Definition: UnitManager.h:35
bool ComputeStateHash(std::string &outHash, bool quick)
Definition: Simulation2.cpp:878
i64 Status
Error handling system.
Definition: status.h:169
std::string GetAIData()
Get AI data.
Definition: Simulation2.cpp:983
std::vector< std::pair< entity_id_t, IComponent * > > InterfaceList
Definition: Simulation2.h:209
bool SerializeState(std::ostream &stream)
Definition: Simulation2.cpp:889
void LoadMapSettings()
Loads the map settings script (called after map is loaded)
Definition: Simulation2.cpp:844
std::string GetMapSizes()
Get map sizes data.
Definition: Simulation2.cpp:978
InterfaceList GetEntitiesWithInterface(int iid)
Returns a list of components implementing the given interface, and their associated entities...
Definition: Simulation2.cpp:711
void FlushDestroyedEntities()
Does the actual destruction of entities from DestroyEntity.
Definition: Simulation2.cpp:691
entity_id_t AddEntity(const std::wstring &templateName)
Construct a new entity and add it to the world.
Definition: Simulation2.cpp:671
void DestroyEntity(entity_id_t ent)
Destroys the specified entity, once FlushDestroyedEntities is called.
Definition: Simulation2.cpp:686
void SetInitAttributes(JS::HandleValue settings)
Set the attributes identifying the scenario/RMS used to initialise this simulation.
Definition: Simulation2.cpp:799
void EnableOOSLog()
Definition: Simulation2.cpp:660
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
void PostMessage(entity_id_t ent, const CMessage &msg) const
Definition: Simulation2.cpp:701
int ProgressiveLoad()
RegMemFun incremental loader function.
Definition: Simulation2.cpp:863
void SetStartupScript(const std::string &script)
Set a startup script, which will get executed before the first turn.
Definition: Simulation2.cpp:789
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
void EnableRejoinTest(int rejoinTestTurn)
Definition: Simulation2.cpp:655
const std::string & GetStartupScript()
Get the current startup script.
Definition: Simulation2.cpp:794
void EnableSerializationTest()
Definition: Simulation2.cpp:650
bool DumpDebugState(std::ostream &stream)
Definition: Simulation2.cpp:883
void GetMapSettings(JS::MutableHandleValue ret)
Get the current map settings.
Definition: Simulation2.cpp:832
Definition: Message.h:23
void LoadPlayerSettings(bool newPlayers)
Loads the player settings script (called before map is loaded)
Definition: Simulation2.cpp:837