[Windows] Automated build.
[0ad.git] / source / simulation2 / Simulation2.h
blob5385c30ca1904ed870385d916aa2dddf28ff9366
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_SIMULATION2
19 #define INCLUDED_SIMULATION2
21 #include "lib/file/vfs/vfs_path.h"
22 #include "simulation2/helpers/SimulationCommand.h"
23 #include "simulation2/system/CmpPtr.h"
24 #include "simulation2/system/Components.h"
26 #include <ostream>
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
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;
42 /**
43 * Public API for simulation system.
44 * Most code should interact with the simulation only through this API.
46 class CSimulation2
48 NONCOPYABLE(CSimulation2);
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();
56 void EnableSerializationTest();
57 void EnableRejoinTest(int rejoinTestTurn);
58 void EnableOOSLog();
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
66 bool LoadScripts(const VfsPath& path);
68 /**
69 * Call LoadScripts for each of the game's standard simulation script paths.
70 * @return false on failure
72 bool LoadDefaultScripts();
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)
79 void LoadPlayerSettings(bool newPlayers);
81 /**
82 * Loads the map settings script (called after map is loaded)
84 void LoadMapSettings();
86 /**
87 * Set a startup script, which will get executed before the first turn.
89 void SetStartupScript(const std::string& script);
91 /**
92 * Get the current startup script.
94 const std::string& GetStartupScript();
96 /**
97 * Set the attributes identifying the scenario/RMS used to initialise this
98 * simulation.
100 void SetInitAttributes(JS::HandleValue settings);
103 * Get the data passed to SetInitAttributes.
105 JS::Value GetInitAttributes();
106 void GetInitAttributes(JS::MutableHandleValue ret);
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.
113 void SetMapSettings(const std::string& settings);
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).
120 void SetMapSettings(JS::HandleValue settings);
123 * Get the current map settings as a UTF-8 JSON string.
125 std::string GetMapSettingsString();
128 * Get the current map settings.
130 void GetMapSettings(JS::MutableHandleValue ret);
133 * RegMemFun incremental loader function.
135 int ProgressiveLoad();
138 * Reload any scripts that were loaded from the given filename.
139 * (This is used to implement hotloading.)
141 Status ReloadChangedFile(const VfsPath& path);
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)
153 void ResetState(bool skipScriptedComponents = false, bool skipAI = false);
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.
161 void PreInitGame();
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.
168 void InitGame();
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);
176 * Returns the last frame offset passed to Interpolate(), i.e. the offset corresponding
177 * to the currently-rendered scene.
179 float GetLastFrameOffset() const;
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
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);
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.
194 void DestroyEntity(entity_id_t ent);
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)
202 void FlushDestroyedEntities();
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;
208 using InterfaceList =
209 std::vector<std::pair<entity_id_t, IComponent*> >;
211 using InterfaceListUnordered =
212 std::unordered_map<entity_id_t, IComponent*>;
215 * Returns a list of components implementing the given interface, and their
216 * associated entities, sorted by entity ID.
218 InterfaceList GetEntitiesWithInterface(int iid);
221 * Returns a list of components implementing the given interface, and their
222 * associated entities, as an unordered map.
224 const InterfaceListUnordered& GetEntitiesWithInterfaceUnordered(int iid);
226 const CSimContext& GetSimContext() const;
227 ScriptInterface& GetScriptInterface() const;
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);
235 * Activate the rejoin-test feature for turn @param turn.
237 void ActivateRejoinTest(int turn);
239 std::string GenerateSchema();
241 /////////////////////////////////////////////////////////////////////////////
242 // Some functions for Atlas UI to be able to access VFS data
245 * Get random map script data
247 * @return vector of strings containing JSON format data
249 std::vector<std::string> GetRMSData();
252 * Get victory condition data
254 * @return vector of strings containing JSON format data
256 std::vector<std::string> GetVictoryConditiondData();
259 * Get player default data
261 * @return string containing JSON format data
263 std::string GetPlayerDefaults();
266 * Get map sizes data
268 * @return string containing JSON format data
270 std::string GetMapSizes();
273 * Get AI data
275 * @return string containing JSON format data
277 std::string GetAIData();
279 private:
280 CSimulation2Impl* m;
283 #endif // INCLUDED_SIMULATION2