LCOV - code coverage report
Current view: top level - source/simulation2 - Simulation2.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 1 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          Line data    Source code
       1             : /* Copyright (C) 2026 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/code_annotation.h"
      22             : #include "lib/file/vfs/vfs_path.h"
      23             : #include "lib/status.h"
      24             : #include "ps/Loader.h"
      25             : #include "simulation2/helpers/SimulationCommand.h"
      26             : #include "simulation2/system/DebugOptions.h"
      27             : #include "simulation2/system/Entity.h"
      28             : 
      29             : #include <array>
      30             : #include <js/TypeDecls.h>
      31             : #include <memory>
      32             : #include <ostream>
      33             : #include <span>
      34             : #include <string>
      35             : #include <unordered_map>
      36             : #include <utility>
      37             : #include <vector>
      38             : 
      39             : class CFrustum;
      40             : class CMessage;
      41             : class CSimContext;
      42             : class CSimulation2Impl;
      43             : class CTerrain;
      44             : class CUnitManager;
      45             : class IComponent;
      46             : class SceneCollector;
      47             : namespace Script { class Context; }
      48             : namespace JS { class Value; }
      49             : namespace Script { class Interface; }
      50             : struct SimulationCommand;
      51             : 
      52             : /**
      53             :  * Public API for simulation system.
      54             :  * Most code should interact with the simulation only through this API.
      55             :  */
      56             : class CSimulation2
      57             : {
      58             :     NONCOPYABLE(CSimulation2);
      59             : 
      60             : public:
      61             :     struct LoadScriptError : std::runtime_error
      62             :     {
      63           0 :         using std::runtime_error::runtime_error;
      64             :     };
      65             : 
      66             :     constexpr static auto DEFAULT_SCRIPTS{std::to_array({L"simulation/components/interfaces/",
      67             :         L"simulation/helpers/", L"simulation/components/"})};
      68             : 
      69             :     // TODO: CUnitManager should probably be handled automatically by this
      70             :     // module, but for now we'll have it passed in externally instead
      71             :     /**
      72             :      * @param scriptDirectories All scripts in the specified directory
      73             :      *  (non-recursively) are loaded, so that they can register new
      74             :      *  component types and functions.
      75             :      */
      76             :     CSimulation2(CUnitManager* unitManager, Script::Context& cx, CTerrain* terrain,
      77             :         const std::span<const wchar_t* const> scriptDirectories,
      78             :         const SimulationDebugOptions debugOptions = {});
      79             :     ~CSimulation2();
      80             : 
      81             :     /**
      82             :      * Loads the player settings script (called before map is loaded)
      83             :      * @param newPlayers will delete all the existing player entities (if any) and create new ones
      84             :      *  (needed for loading maps, but Atlas might want to update existing player data)
      85             :      */
      86             :     void LoadPlayerSettings(bool newPlayers);
      87             : 
      88             :     /**
      89             :      * Loads the map settings script (called after map is loaded)
      90             :      */
      91             :     void LoadMapSettings();
      92             : 
      93             :     /**
      94             :      * Set a startup script, which will get executed before the first turn.
      95             :      */
      96             :     void SetStartupScript(const std::string& script);
      97             : 
      98             :     /**
      99             :      * Get the current startup script.
     100             :      */
     101             :     const std::string& GetStartupScript();
     102             : 
     103             :     /**
     104             :      * Set the attributes identifying the scenario/RMS used to initialise this
     105             :      * simulation.
     106             :      */
     107             :     void SetInitAttributes(JS::HandleValue settings);
     108             : 
     109             :     /**
     110             :      * Get the data passed to SetInitAttributes.
     111             :      */
     112             :     JS::Value GetInitAttributes();
     113             :     void GetInitAttributes(JS::MutableHandleValue ret);
     114             : 
     115             :     /**
     116             :      * Set the initial map settings (as a UTF-8-encoded JSON string),
     117             :      * which will be used to set up the simulation state.
     118             :      * Called from atlas.
     119             :      */
     120             :     void SetMapSettings(const std::string& settings);
     121             : 
     122             :     /**
     123             :      * Set the initial map settings, which will be used
     124             :      * to set up the simulation state.
     125             :      * Called from MapReader (for all map-types).
     126             :      */
     127             :     void SetMapSettings(JS::HandleValue settings);
     128             : 
     129             :     /**
     130             :      * Get the current map settings as a UTF-8 JSON string.
     131             :      */
     132             :     std::string GetMapSettingsString();
     133             : 
     134             :     /**
     135             :      * Get the current map settings.
     136             :      */
     137             :     void GetMapSettings(JS::MutableHandleValue ret);
     138             : 
     139             :     /**
     140             :      * RegMemFun incremental loader function.
     141             :      */
     142             :     PS::Loader::Task ProgressiveLoad();
     143             : 
     144             :     /**
     145             :      * Reload any scripts that were loaded from the given filename.
     146             :      * (This is used to implement hotloading.)
     147             :      */
     148             :     Status ReloadChangedFile(const VfsPath& path);
     149             : 
     150             :     /**
     151             :      * Initialise (or re-initialise) the complete simulation state.
     152             :      * Must be called after LoadScripts, and must be called
     153             :      * before any methods that depend on the simulation state.
     154             :      * @param skipScriptedComponents don't load the scripted system components
     155             :      *   (this is intended for use by test cases that don't mount all of VFS)
     156             :      * @param skipAI don't initialise the AI system
     157             :      *   (this is intended for use by test cases that don't want all entity
     158             :      *   templates loaded automatically)
     159             :      */
     160             :     void ResetState(bool skipScriptedComponents = false, bool skipAI = false);
     161             : 
     162             :     /**
     163             :      * Replace/destroy some entities (e.g. skirmish replacers)
     164             :      * Called right before InitGame, on CGame instantiation.
     165             :      * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
     166             :      * This calls the PreInitGame function defined in helpers/InitGame.js.
     167             :      */
     168             :     void PreInitGame();
     169             : 
     170             :     /**
     171             :      * Initialise a new game, based on some script data. (Called on CGame instantiation)
     172             :      * (This mustn't be used when e.g. loading saved games, only when starting new ones.)
     173             :      * This calls the InitGame function defined in helpers/InitGame.js.
     174             :      */
     175             :     void InitGame();
     176             : 
     177             :     void Update(int turnLength);
     178             :     void Update(int turnLength, const std::vector<SimulationCommand>& commands);
     179             :     void Interpolate(float simFrameLength, float frameOffset, float realFrameLength);
     180             :     void RenderSubmit(SceneCollector& collector, const CFrustum& frustum, bool culling);
     181             : 
     182             :     /**
     183             :      * Returns the last frame offset passed to Interpolate(), i.e. the offset corresponding
     184             :      * to the currently-rendered scene.
     185             :      */
     186             :     float GetLastFrameOffset() const;
     187             : 
     188             :     /**
     189             :      * Construct a new entity and add it to the world.
     190             :      * @param templateName see ICmpTemplateManager for syntax
     191             :      * @return the new entity ID, or INVALID_ENTITY on error
     192             :      */
     193             :     entity_id_t AddEntity(const std::wstring& templateName);
     194             :     entity_id_t AddEntity(const std::wstring& templateName, entity_id_t preferredId);
     195             :     entity_id_t AddLocalEntity(const std::wstring& templateName);
     196             : 
     197             :     /**
     198             :      * Destroys the specified entity, once FlushDestroyedEntities is called.
     199             :      * Has no effect if the entity does not exist, or has already been added to the destruction queue.
     200             :      */
     201             :     void DestroyEntity(entity_id_t ent);
     202             : 
     203             :     /**
     204             :      * Does the actual destruction of entities from DestroyEntity.
     205             :      * This is called automatically by Update, but should also be called at other
     206             :      * times when an entity might have been deleted and should be removed from
     207             :      * any further processing (e.g. after editor UI message processing)
     208             :      */
     209             :     void FlushDestroyedEntities();
     210             : 
     211             :     IComponent* QueryInterface(entity_id_t ent, int iid) const;
     212             :     void PostMessage(entity_id_t ent, const CMessage& msg) const;
     213             :     void BroadcastMessage(const CMessage& msg) const;
     214             : 
     215             :     using InterfaceList =
     216             :         std::vector<std::pair<entity_id_t, IComponent*> >;
     217             : 
     218             :     using InterfaceListUnordered =
     219             :         std::unordered_map<entity_id_t, IComponent*>;
     220             : 
     221             :     /**
     222             :      * Returns a list of components implementing the given interface, and their
     223             :      * associated entities, sorted by entity ID.
     224             :      */
     225             :     InterfaceList GetEntitiesWithInterface(int iid);
     226             : 
     227             :     /**
     228             :      * Returns a list of components implementing the given interface, and their
     229             :      * associated entities, as an unordered map.
     230             :      */
     231             :     const InterfaceListUnordered& GetEntitiesWithInterfaceUnordered(int iid);
     232             : 
     233             :     const CSimContext& GetSimContext() const;
     234             :     Script::Interface& GetScriptInterface() const;
     235             : 
     236             :     bool ComputeStateHash(std::string& outHash, bool quick);
     237             :     bool DumpDebugState(std::ostream& stream);
     238             :     bool SerializeState(std::ostream& stream);
     239             :     bool DeserializeState(std::istream& stream);
     240             : 
     241             :     /**
     242             :      * Activate the rejoin-test feature for turn @param turn.
     243             :      */
     244             :     void ActivateRejoinTest(turn_id_t turn);
     245             : 
     246             :     std::string GenerateSchema();
     247             : 
     248             :     /////////////////////////////////////////////////////////////////////////////
     249             :     // Some functions for Atlas UI to be able to access VFS data
     250             : 
     251             :     /**
     252             :      * Get random map script data
     253             :      *
     254             :      * @return vector of strings containing JSON format data
     255             :      */
     256             :     std::vector<std::string> GetRMSData();
     257             : 
     258             :     /**
     259             :      * Get victory condition data
     260             :      *
     261             :      * @return vector of strings containing JSON format data
     262             :      */
     263             :     std::vector<std::string> GetVictoryConditiondData();
     264             : 
     265             :     /**
     266             :      * Get player default data
     267             :      *
     268             :      * @return string containing JSON format data
     269             :      */
     270             :     std::string GetPlayerDefaults();
     271             : 
     272             :     /**
     273             :      * Get map sizes data
     274             :      *
     275             :      * @return string containing JSON format data
     276             :      */
     277             :     std::string GetMapSizes();
     278             : 
     279             :     /**
     280             :      * Get AI data
     281             :      *
     282             :      * @return string containing JSON format data
     283             :      */
     284             :     std::string GetAIData();
     285             : 
     286             : private:
     287             :     const std::unique_ptr<CSimulation2Impl> m;
     288             : };
     289             : 
     290             : #endif // INCLUDED_SIMULATION2

Generated by: LCOV version 1.16