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