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