LCOV - code coverage report
Current view: top level - source/ps - World.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 20 42 47.6 %
Date: 2026-12-03 01:19:39 Functions: 4 7 57.1 %

          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             : #include "precompiled.h"
      19             : 
      20             : #include "World.h"
      21             : 
      22             : #include "graphics/GameView.h"
      23             : #include "graphics/HeightMipmap.h"
      24             : #include "graphics/LightEnv.h"
      25             : #include "graphics/MapReader.h"
      26             : #include "graphics/Terrain.h"
      27             : #include "graphics/UnitManager.h"
      28             : #include "lib/path.h"
      29             : #include "ps/CLogger.h"
      30             : #include "ps/CStr.h"
      31             : #include "ps/FileIo.h"
      32             : #include "ps/Game.h"
      33             : #include "ps/Loader.h"
      34             : #include "renderer/Renderer.h"
      35             : #include "renderer/SceneRenderer.h"
      36             : #include "simulation2/Simulation2.h"
      37             : 
      38             : #include <js/RootingAPI.h>
      39             : #include <string>
      40             : 
      41             : class CTriggerManager;
      42             : 
      43             : /**
      44             :  * Global light settings.
      45             :  * It is not a member of CWorld because it is passed
      46             :  * to the renderer before CWorld exists.
      47             :  **/
      48             : CLightEnv g_LightEnv;
      49             : 
      50             : 
      51             : /**
      52             :  * Constructor.
      53             :  *
      54             :  * @param game CGame& game reference to the container game object.
      55             :  **/
      56           3 : CWorld::CWorld(CGame& game):
      57           3 :     m_Game{game},
      58           3 :     m_Terrain{std::make_unique<CTerrain>()},
      59           3 :     m_UnitManager{std::make_unique<CUnitManager>()},
      60           3 :     m_MapReader{std::make_unique<CMapReader>()}
      61             : {
      62           3 : }
      63             : 
      64           3 : CWorld::~CWorld() = default;
      65             : 
      66             : /**
      67             :  * Initializes the game world with the attributes provided.
      68             :  **/
      69           3 : void CWorld::RegisterInit(const CStrW& mapFile, const Script::Context& cx, JS::HandleValue settings, int playerID)
      70             : {
      71             :     // Load the map, if one was specified
      72           3 :     if (mapFile.length())
      73             :     {
      74           9 :         VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp");
      75             : 
      76           3 :         try
      77             :         {
      78           3 :             CTriggerManager* pTriggerManager = nullptr;
      79           3 :             m_MapReader->LoadMap(mapfilename, cx, settings, m_Terrain.get(),
      80           0 :                 CRenderer::IsInitialised() ? &g_Renderer.GetSceneRenderer().GetWaterManager() :
      81             :                     nullptr,
      82           0 :                 CRenderer::IsInitialised() ? &g_Renderer.GetSceneRenderer().GetSkyManager() :
      83           3 :                     nullptr, &g_LightEnv, m_Game.GetView(),
      84           3 :                 m_Game.GetView() ? m_Game.GetView()->GetCinema() : nullptr, pTriggerManager,
      85           0 :                 CRenderer::IsInitialised() ? &g_Renderer.GetPostprocManager() : nullptr,
      86           3 :                 m_Game.GetSimulation2(), &m_Game.GetSimulation2()->GetSimContext(), playerID,
      87             :                 false);
      88             :                 // fails immediately, or registers for delay loading
      89           6 :             PS::Loader::Register(std::bind_front([](CWorld* world) -> PS::Loader::Task
      90             :             {
      91             :                 co_return world->DeleteMapReader();
      92           6 :             }, this), L"CWorld::DeleteMapReader", 5);
      93             :         }
      94           0 :         catch (PSERROR_File& err)
      95             :         {
      96           0 :             m_MapReader.reset();
      97           0 :             LOGERROR("Failed to load map {}: {}", mapfilename.string8(), err.what());
      98           0 :             throw PSERROR_Game_World_MapLoadFailed("Failed to load map.\nCheck application log for details.");
      99           0 :         }
     100           3 :     }
     101           3 : }
     102             : 
     103           0 : void CWorld::RegisterInitRMS(const CStrW& scriptFile, const Script::Context& cx, JS::HandleValue settings, int playerID)
     104             : {
     105             :     // If scriptFile is empty, a blank map will be generated using settings (no RMS run)
     106           0 :     CTriggerManager* pTriggerManager = nullptr;
     107           0 :     m_MapReader->LoadRandomMap(scriptFile, cx, settings, m_Terrain.get(),
     108           0 :         CRenderer::IsInitialised() ? &g_Renderer.GetSceneRenderer().GetWaterManager() : nullptr,
     109           0 :         CRenderer::IsInitialised() ? &g_Renderer.GetSceneRenderer().GetSkyManager() : nullptr,
     110           0 :         &g_LightEnv, m_Game.GetView(), m_Game.GetView() ? m_Game.GetView()->GetCinema() : nullptr,
     111           0 :         pTriggerManager, CRenderer::IsInitialised() ? &g_Renderer.GetPostprocManager() : nullptr,
     112           0 :         m_Game.GetSimulation2(), playerID);
     113             :         // registers for delay loading
     114           0 :     PS::Loader::Register(std::bind_front([](CWorld* world) -> PS::Loader::Task
     115             :     {
     116             :         co_return world->DeleteMapReader();
     117           0 :     }, this), L"CWorld::DeleteMapReader", 5);
     118           0 : }
     119             : 
     120           0 : int CWorld::DeleteMapReader()
     121             : {
     122           0 :     m_MapReader.reset();
     123           0 :     return 0;
     124             : }

Generated by: LCOV version 1.16