LCOV - code coverage report
Current view: top level - source/graphics - MapWriter.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 298 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 6 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             : #include "precompiled.h"
      19             : 
      20             : #include "MapWriter.h"
      21             : 
      22             : #include "graphics/Camera.h"
      23             : #include "graphics/Color.h"
      24             : #include "graphics/LightEnv.h"
      25             : #include "graphics/MapIO.h"
      26             : #include "graphics/MiniPatch.h"
      27             : #include "graphics/Patch.h"
      28             : #include "graphics/Terrain.h"
      29             : #include "graphics/TerrainTextureEntry.h"
      30             : #include "lib/debug.h"
      31             : #include "lib/path.h"
      32             : #include "lib/posix/posix_types.h"
      33             : #include "maths/Fixed.h"
      34             : #include "maths/FixedVector3D.h"
      35             : #include "maths/MathUtil.h"
      36             : #include "maths/Matrix3D.h"
      37             : #include "maths/NUSpline.h"
      38             : #include "maths/Vector3D.h"
      39             : #include "ps/CLogger.h"
      40             : #include "ps/FileIo.h"
      41             : #include "ps/Filesystem.h"
      42             : #include "ps/XML/XMLWriter.h"
      43             : #include "renderer/PostprocManager.h"
      44             : #include "renderer/SkyManager.h"
      45             : #include "renderer/WaterManager.h"
      46             : #include "scriptinterface/Interface.h"
      47             : #include "simulation2/Simulation2.h"
      48             : #include "simulation2/components/ICmpCinemaManager.h"
      49             : #include "simulation2/components/ICmpGarrisonHolder.h"
      50             : #include "simulation2/components/ICmpObstruction.h"
      51             : #include "simulation2/components/ICmpOwnership.h"
      52             : #include "simulation2/components/ICmpPosition.h"
      53             : #include "simulation2/components/ICmpTemplateManager.h"
      54             : #include "simulation2/components/ICmpTurretHolder.h"
      55             : #include "simulation2/components/ICmpVisual.h"
      56             : #include "simulation2/components/ICmpWaterManager.h"
      57             : #include "simulation2/helpers/CinemaPath.h"
      58             : #include "simulation2/system/Component.h"
      59             : #include "simulation2/system/Entity.h"
      60             : 
      61             : #include <algorithm>
      62             : #include <cmath>
      63             : #include <cstddef>
      64             : #include <map>
      65             : #include <numbers>
      66             : #include <string>
      67             : #include <utility>
      68             : 
      69             : ///////////////////////////////////////////////////////////////////////////////////////////////////
      70             : // CMapWriter constructor: nothing to do at the minute
      71           0 : CMapWriter::CMapWriter()
      72             : {
      73           0 : }
      74             : 
      75             : ///////////////////////////////////////////////////////////////////////////////////////////////////
      76             : // SaveMap: try to save the current map to the given file
      77           0 : void CMapWriter::SaveMap(const VfsPath& pathname, CTerrain* pTerrain, WaterManager* pWaterMan,
      78             :     SkyManager* pSkyMan, CLightEnv* pLightEnv, const CCamera& camera, CCinemaManager*,
      79             :     CPostprocManager* pPostproc, CSimulation2* pSimulation2)
      80             : {
      81           0 :     CFilePacker packer(FILE_VERSION, "PSMP");
      82             : 
      83             :     // build necessary data
      84           0 :     PackMap(packer, pTerrain);
      85             : 
      86           0 :     try
      87             :     {
      88             :         // write it out
      89           0 :         packer.Write(pathname);
      90             :     }
      91           0 :     catch (PSERROR_File_WriteFailed&)
      92             :     {
      93           0 :         LOGERROR("Failed to write map '{}'", pathname.string8());
      94           0 :         return;
      95           0 :     }
      96             : 
      97           0 :     VfsPath pathnameXML = pathname.ChangeExtension(L".xml");
      98           0 :     WriteXML(pathnameXML, pWaterMan, pSkyMan, pLightEnv, camera, pPostproc, pSimulation2);
      99           0 : }
     100             : 
     101             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     102             : // GetHandleIndex: return the index of the given handle in the given list; or 0xFFFF if
     103             : // handle isn't in list
     104           0 : static std::uint16_t GetEntryIndex(const CTerrainTextureEntry* entry,
     105             :     const std::vector<CTerrainTextureEntry*>& entries)
     106             : {
     107           0 :     const size_t limit = std::min(entries.size(), size_t(0xFFFEu)); // paranoia
     108           0 :     for (size_t i=0;i<limit;i++) {
     109           0 :         if (entries[i]==entry) {
     110           0 :             return static_cast<std::uint16_t>(i);
     111             :         }
     112             :     }
     113             : 
     114             :     return 0xFFFF;
     115             : }
     116             : 
     117             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     118             : // EnumTerrainTextures: build lists of textures used by map, and tile descriptions for
     119             : // each tile on the terrain
     120           0 : void CMapWriter::EnumTerrainTextures(CTerrain *pTerrain,
     121             :                                      std::vector<CStr>& textures,
     122             :                                      std::vector<STileDesc>& tiles)
     123             : {
     124             :     // the list of all handles in use
     125           0 :     std::vector<CTerrainTextureEntry*> entries;
     126             : 
     127             :     // resize tile array to required size
     128           0 :     tiles.resize(SQR(pTerrain->GetVerticesPerSide()-1));
     129           0 :     STileDesc* tileptr=&tiles[0];
     130             : 
     131             :     // now iterate through all the tiles
     132           0 :     const ssize_t patchesPerSide=pTerrain->GetPatchesPerSide();
     133           0 :     for (ssize_t j=0;j<patchesPerSide;j++) {
     134           0 :         for (ssize_t i=0;i<patchesPerSide;i++) {
     135           0 :             for (ssize_t m=0;m<PATCH_SIZE;m++) {
     136           0 :                 for (ssize_t k=0;k<PATCH_SIZE;k++) {
     137           0 :                     CMiniPatch& mp=pTerrain->GetPatch(i,j)->m_MiniPatches[m][k];  // can't fail
     138           0 :                     std::uint16_t index = static_cast<std::uint16_t>(GetEntryIndex(mp.GetTextureEntry(),entries));
     139           0 :                     if (index==0xFFFF) {
     140           0 :                         index = static_cast<std::uint16_t>(entries.size());
     141           0 :                         entries.push_back(mp.GetTextureEntry());
     142             :                     }
     143             : 
     144           0 :                     tileptr->m_Tex1Index=index;
     145           0 :                     tileptr->m_Tex2Index=0xFFFF;
     146           0 :                     tileptr->m_Priority=mp.GetPriority();
     147           0 :                     tileptr++;
     148             :                 }
     149             :             }
     150             :         }
     151             :     }
     152             : 
     153             :     // now find the texture names for each handle
     154           0 :     for (size_t i=0;i<entries.size();i++) {
     155           0 :         CStr texturename;
     156           0 :         CTerrainTextureEntry* texentry=entries[i];
     157           0 :         if (!texentry) {
     158             :             // uh-oh, this shouldn't happen; set texturename to empty string
     159           0 :             texturename="";
     160             :         } else {
     161           0 :             texturename=texentry->GetTag();
     162             :         }
     163           0 :         textures.push_back(texturename);
     164           0 :     }
     165           0 : }
     166             : 
     167             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     168             : // PackMap: pack the current world into a raw data stream
     169           0 : void CMapWriter::PackMap(CFilePacker& packer, CTerrain* pTerrain)
     170             : {
     171             :     // now pack everything up
     172           0 :     PackTerrain(packer, pTerrain);
     173           0 : }
     174             : 
     175             : ///////////////////////////////////////////////////////////////////////////////////////////////////
     176             : // PackTerrain: pack the terrain onto the end of the output data stream
     177             : //      - data: map size, heightmap, list of textures used by map, texture tile assignments
     178           0 : void CMapWriter::PackTerrain(CFilePacker& packer, CTerrain* pTerrain)
     179             : {
     180             :     // pack map size
     181           0 :     const ssize_t mapsize = pTerrain->GetPatchesPerSide();
     182           0 :     packer.PackSize(mapsize);
     183             : 
     184             :     // pack heightmap
     185           0 :     packer.PackRaw(pTerrain->GetHeightMap(), sizeof(std::uint16_t) *
     186           0 :         SQR(pTerrain->GetVerticesPerSide()));
     187             : 
     188             :     // the list of textures used by map
     189           0 :     std::vector<CStr> terrainTextures;
     190             :     // descriptions of each tile
     191           0 :     std::vector<STileDesc> tiles;
     192             : 
     193             :     // build lists by scanning through the terrain
     194           0 :     EnumTerrainTextures(pTerrain, terrainTextures, tiles);
     195             : 
     196             :     // pack texture names
     197           0 :     const size_t numTextures = terrainTextures.size();
     198           0 :     packer.PackSize(numTextures);
     199           0 :     for (size_t i=0;i<numTextures;i++)
     200           0 :         packer.PackString(terrainTextures[i]);
     201             : 
     202             :     // pack tile data
     203           0 :     packer.PackRaw(&tiles[0],sizeof(STileDesc)*tiles.size());
     204           0 : }
     205             : 
     206           0 : void CMapWriter::WriteXML(const VfsPath& filename,
     207             :                           WaterManager* pWaterMan, SkyManager* pSkyMan,
     208             :                           CLightEnv* pLightEnv, const CCamera& camera,
     209             :                           CPostprocManager* pPostproc,
     210             :                           CSimulation2* pSimulation2)
     211             : {
     212           0 :     XMLWriter_File xmlMapFile;
     213           0 :     {
     214           0 :         XMLWriter_Element scenarioTag(xmlMapFile, "Scenario");
     215           0 :         scenarioTag.Attribute("version", static_cast<int>(FILE_VERSION));
     216             : 
     217           0 :         ENSURE(pSimulation2);
     218           0 :         CSimulation2& sim = *pSimulation2;
     219             : 
     220           0 :         if (!sim.GetStartupScript().empty())
     221             :         {
     222           0 :             XMLWriter_Element scriptTag(xmlMapFile, "Script");
     223           0 :             scriptTag.Text(sim.GetStartupScript().c_str(), true);
     224           0 :         }
     225             : 
     226           0 :         {
     227           0 :             XMLWriter_Element environmentTag(xmlMapFile, "Environment");
     228           0 :             environmentTag.Setting("SkySet", pSkyMan->GetSkySet());
     229           0 :             {
     230           0 :                 XMLWriter_Element sunColorTag(xmlMapFile, "SunColor");
     231           0 :                 sunColorTag.Attribute("r", pLightEnv->m_SunColor.X); // yes, it's X/Y/Z...
     232           0 :                 sunColorTag.Attribute("g", pLightEnv->m_SunColor.Y);
     233           0 :                 sunColorTag.Attribute("b", pLightEnv->m_SunColor.Z);
     234           0 :             }
     235           0 :             {
     236           0 :                 XMLWriter_Element SunElevationTag(xmlMapFile, "SunElevation");
     237           0 :                 SunElevationTag.Attribute("angle", pLightEnv->m_Elevation);
     238           0 :             }
     239           0 :             {
     240           0 :                 XMLWriter_Element sunRotationTag(xmlMapFile, "SunRotation");
     241           0 :                 sunRotationTag.Attribute("angle", pLightEnv->m_Rotation);
     242           0 :             }
     243           0 :             {
     244           0 :                 XMLWriter_Element ambientColorTag(xmlMapFile, "AmbientColor");
     245           0 :                 ambientColorTag.Attribute("r", pLightEnv->m_AmbientColor.X);
     246           0 :                 ambientColorTag.Attribute("g", pLightEnv->m_AmbientColor.Y);
     247           0 :                 ambientColorTag.Attribute("b", pLightEnv->m_AmbientColor.Z);
     248           0 :             }
     249           0 :             {
     250           0 :                 XMLWriter_Element fogTag(xmlMapFile, "Fog");
     251           0 :                 fogTag.Setting("FogFactor", pLightEnv->m_FogFactor);
     252           0 :                 fogTag.Setting("FogThickness", pLightEnv->m_FogMax);
     253           0 :                 {
     254           0 :                     XMLWriter_Element fogColorTag(xmlMapFile, "FogColor");
     255           0 :                     fogColorTag.Attribute("r", pLightEnv->m_FogColor.X);
     256           0 :                     fogColorTag.Attribute("g", pLightEnv->m_FogColor.Y);
     257           0 :                     fogColorTag.Attribute("b", pLightEnv->m_FogColor.Z);
     258           0 :                 }
     259           0 :             }
     260             : 
     261           0 :             {
     262           0 :                 XMLWriter_Element waterTag(xmlMapFile, "Water");
     263           0 :                 {
     264           0 :                     XMLWriter_Element waterBodyTag(xmlMapFile, "WaterBody");
     265           0 :                     CmpPtr<ICmpWaterManager> cmpWaterManager(sim, SYSTEM_ENTITY);
     266           0 :                     ENSURE(cmpWaterManager);
     267           0 :                     waterBodyTag.Setting("Type", pWaterMan->m_WaterType);
     268           0 :                     {
     269           0 :                         XMLWriter_Element colorTag(xmlMapFile, "Color");
     270           0 :                         colorTag.Attribute("r", pWaterMan->m_WaterColor.r);
     271           0 :                         colorTag.Attribute("g", pWaterMan->m_WaterColor.g);
     272           0 :                         colorTag.Attribute("b", pWaterMan->m_WaterColor.b);
     273           0 :                     }
     274           0 :                     {
     275           0 :                         XMLWriter_Element tintTag(xmlMapFile, "Tint");
     276           0 :                         tintTag.Attribute("r", pWaterMan->m_WaterTint.r);
     277           0 :                         tintTag.Attribute("g", pWaterMan->m_WaterTint.g);
     278           0 :                         tintTag.Attribute("b", pWaterMan->m_WaterTint.b);
     279           0 :                     }
     280           0 :                     waterBodyTag.Setting("Height", cmpWaterManager->GetExactWaterLevel(0, 0));
     281           0 :                     waterBodyTag.Setting("Waviness", pWaterMan->m_Waviness);
     282           0 :                     waterBodyTag.Setting("Murkiness", pWaterMan->m_Murkiness);
     283           0 :                     waterBodyTag.Setting("WindAngle", pWaterMan->m_WindAngle);
     284           0 :                 }
     285           0 :             }
     286             : 
     287           0 :             {
     288           0 :                 XMLWriter_Element postProcTag(xmlMapFile, "Postproc");
     289           0 :                 {
     290           0 :                     postProcTag.Setting("Brightness", pLightEnv->m_Brightness);
     291           0 :                     postProcTag.Setting("Contrast", pLightEnv->m_Contrast);
     292           0 :                     postProcTag.Setting("Saturation", pLightEnv->m_Saturation);
     293           0 :                     postProcTag.Setting("Bloom", pLightEnv->m_Bloom);
     294           0 :                     postProcTag.Setting("PostEffect", pPostproc->GetPostEffect());
     295             :                 }
     296           0 :             }
     297           0 :         }
     298             : 
     299           0 :         {
     300           0 :             XMLWriter_Element cameraTag(xmlMapFile, "Camera");
     301           0 :             {
     302           0 :                 XMLWriter_Element positionTag(xmlMapFile, "Position");
     303           0 :                 const CVector3D pos{camera.GetOrientation().GetTranslation()};
     304           0 :                 positionTag.Attribute("x", pos.X);
     305           0 :                 positionTag.Attribute("y", pos.Y);
     306           0 :                 positionTag.Attribute("z", pos.Z);
     307           0 :             }
     308             : 
     309           0 :             const CVector3D in{camera.GetOrientation().GetIn()};
     310             :             // Convert to spherical coordinates
     311           0 :             float rotation = atan2(in.X, in.Z);
     312           0 :             float declination = atan2(sqrt(in.X*in.X + in.Z*in.Z), in.Y) - std::numbers::pi_v<float> / 2.f;
     313             : 
     314           0 :             {
     315           0 :                 XMLWriter_Element rotationTag(xmlMapFile, "Rotation");
     316           0 :                 rotationTag.Attribute("angle", rotation);
     317           0 :             }
     318           0 :             {
     319           0 :                 XMLWriter_Element declinationTag(xmlMapFile, "Declination");
     320           0 :                 declinationTag.Attribute("angle", declination);
     321           0 :             }
     322           0 :         }
     323             : 
     324           0 :         {
     325           0 :             std::string settings = sim.GetMapSettingsString();
     326           0 :             if (!settings.empty())
     327             :             {
     328           0 :                 XMLWriter_Element scriptSettingsTag(xmlMapFile, "ScriptSettings");
     329           0 :                 scriptSettingsTag.Text(("\n" + settings + "\n").c_str(), true);
     330           0 :             }
     331           0 :         }
     332             : 
     333           0 :         {
     334           0 :             XMLWriter_Element entitiesTag(xmlMapFile, "Entities");
     335           0 :             CmpPtr<ICmpTemplateManager> cmpTemplateManager(sim, SYSTEM_ENTITY);
     336           0 :             ENSURE(cmpTemplateManager);
     337             : 
     338             :             // This will probably need to be changed in the future, but for now we'll
     339             :             // just save all entities that have a position
     340           0 :             CSimulation2::InterfaceList ents = sim.GetEntitiesWithInterface(IID_Position);
     341           0 :             for (CSimulation2::InterfaceList::const_iterator it = ents.begin(); it != ents.end(); ++it)
     342             :             {
     343           0 :                 entity_id_t ent = it->first;
     344             : 
     345             :                 // Don't save local entities (placement previews etc)
     346           0 :                 if (ENTITY_IS_LOCAL(ent))
     347           0 :                     continue;
     348             : 
     349           0 :                 XMLWriter_Element entityTag(xmlMapFile, "Entity");
     350           0 :                 entityTag.Attribute("uid", ent);
     351             : 
     352           0 :                 entityTag.Setting("Template", cmpTemplateManager->GetCurrentTemplateName(ent));
     353             : 
     354           0 :                 CmpPtr<ICmpOwnership> cmpOwnership(sim, ent);
     355           0 :                 if (cmpOwnership)
     356           0 :                     entityTag.Setting("Player", static_cast<int>(cmpOwnership->GetOwner()));
     357             : 
     358           0 :                 CmpPtr<ICmpGarrisonHolder> cmpGarrisonHolder(sim, ent);
     359           0 :                 if (cmpGarrisonHolder)
     360             :                 {
     361           0 :                     std::vector<entity_id_t> garrison = cmpGarrisonHolder->GetEntities();
     362           0 :                     if (!garrison.empty())
     363             :                     {
     364           0 :                         XMLWriter_Element garrisonTag(xmlMapFile, "Garrison");
     365           0 :                         for (const entity_id_t garr_ent_id : garrison)
     366             :                         {
     367           0 :                             XMLWriter_Element garrisonedEntityTag(xmlMapFile, "GarrisonedEntity");
     368           0 :                             garrisonedEntityTag.Attribute("uid", static_cast<int>(garr_ent_id));
     369           0 :                         }
     370           0 :                     }
     371           0 :                 }
     372             : 
     373           0 :                 CmpPtr<ICmpTurretHolder> cmpTurretHolder(sim, ent);
     374           0 :                 if (cmpTurretHolder)
     375             :                 {
     376           0 :                     std::vector<std::pair<std::string, entity_id_t> > turrets = cmpTurretHolder->GetTurrets();
     377           0 :                     if (!turrets.empty())
     378             :                     {
     379           0 :                         XMLWriter_Element turretTag(xmlMapFile, "Turrets");
     380           0 :                         for (const std::pair<std::string, entity_id_t>& turret : turrets)
     381             :                         {
     382           0 :                             XMLWriter_Element turretedEntityTag(xmlMapFile, "Turret");
     383           0 :                             turretedEntityTag.Attribute("turret", turret.first);
     384           0 :                             turretedEntityTag.Attribute("uid", static_cast<int>(turret.second));
     385           0 :                         }
     386           0 :                     }
     387           0 :                 }
     388             : 
     389           0 :                 CmpPtr<ICmpPosition> cmpPosition(sim, ent);
     390           0 :                 if (cmpPosition)
     391             :                 {
     392           0 :                     CFixedVector3D pos;
     393           0 :                     if (cmpPosition->IsInWorld())
     394           0 :                         pos = cmpPosition->GetPosition();
     395             : 
     396           0 :                     CFixedVector3D rot = cmpPosition->GetRotation();
     397           0 :                     {
     398           0 :                         XMLWriter_Element positionTag(xmlMapFile, "Position");
     399           0 :                         positionTag.Attribute("x", pos.X);
     400           0 :                         positionTag.Attribute("z", pos.Z);
     401             :                         // TODO: height offset etc
     402           0 :                     }
     403           0 :                     {
     404           0 :                         XMLWriter_Element orientationTag(xmlMapFile, "Orientation");
     405           0 :                         orientationTag.Attribute("y", rot.Y);
     406             :                         // TODO: X, Z maybe
     407           0 :                     }
     408             :                 }
     409             : 
     410           0 :                 CmpPtr<ICmpObstruction> cmpObstruction(sim, ent);
     411           0 :                 if (cmpObstruction)
     412             :                 {
     413             :                     // TODO: Currently only necessary because Atlas
     414             :                     // does not set up control groups for its walls.
     415           0 :                     cmpObstruction->ResolveFoundationCollisions();
     416             : 
     417           0 :                     entity_id_t group = cmpObstruction->GetControlGroup();
     418           0 :                     entity_id_t group2 = cmpObstruction->GetControlGroup2();
     419             : 
     420             :                     // Don't waste space writing the default control groups.
     421           0 :                     if (group != ent || group2 != INVALID_ENTITY)
     422             :                     {
     423           0 :                         XMLWriter_Element obstructionTag(xmlMapFile, "Obstruction");
     424           0 :                         if (group != ent)
     425           0 :                             obstructionTag.Attribute("group", group);
     426           0 :                         if (group2 != INVALID_ENTITY)
     427           0 :                             obstructionTag.Attribute("group2", group2);
     428           0 :                     }
     429             :                 }
     430             : 
     431           0 :                 CmpPtr<ICmpVisual> cmpVisual(sim, ent);
     432           0 :                 if (cmpVisual)
     433             :                 {
     434           0 :                     entity_id_t seed = static_cast<entity_id_t>(cmpVisual->GetActorSeed());
     435           0 :                     if (seed != ent)
     436             :                     {
     437           0 :                         XMLWriter_Element actorTag(xmlMapFile, "Actor");
     438           0 :                         actorTag.Attribute("seed",seed);
     439           0 :                     }
     440             :                     // TODO: variation/selection strings
     441             :                 }
     442           0 :             }
     443           0 :         }
     444             : 
     445             : 
     446           0 :         CmpPtr<ICmpCinemaManager> cmpCinemaManager(sim, SYSTEM_ENTITY);
     447           0 :         if (cmpCinemaManager)
     448             :         {
     449           0 :             const std::map<CStrW, CCinemaPath>& paths = cmpCinemaManager->GetPaths();
     450           0 :             std::map<CStrW, CCinemaPath>::const_iterator it = paths.begin();
     451           0 :             XMLWriter_Element pathsTag(xmlMapFile, "Paths");
     452             : 
     453           0 :             for ( ; it != paths.end(); ++it )
     454             :             {
     455           0 :                 fixed timescale = it->second.GetTimescale();
     456           0 :                 const std::vector<SplineData>& position_nodes = it->second.GetAllNodes();
     457           0 :                 const std::vector<SplineData>& target_nodes = it->second.GetTargetSpline().GetAllNodes();
     458           0 :                 const CCinemaData* data = it->second.GetData();
     459             : 
     460           0 :                 XMLWriter_Element pathTag(xmlMapFile, "Path");
     461           0 :                 pathTag.Attribute("name", data->m_Name);
     462           0 :                 pathTag.Attribute("timescale", timescale);
     463           0 :                 pathTag.Attribute("orientation", data->m_Orientation);
     464           0 :                 pathTag.Attribute("mode", data->m_Mode);
     465           0 :                 pathTag.Attribute("style", data->m_Style);
     466             : 
     467           0 :                 struct SEvent
     468             :                 {
     469             :                     fixed time;
     470             :                     const char* type;
     471             :                     CFixedVector3D value;
     472           0 :                     SEvent(fixed time, const char* type, CFixedVector3D value)
     473           0 :                         : time(time), type(type), value(value)
     474             :                     {}
     475           0 :                     bool operator<(const SEvent& another) const
     476             :                     {
     477           0 :                         return time < another.time;
     478             :                     }
     479             :                 };
     480             : 
     481             :                 // All events of a manipulating of camera (position/rotation/target)
     482           0 :                 std::vector<SEvent> events;
     483           0 :                 events.reserve(position_nodes.size() + target_nodes.size());
     484             : 
     485           0 :                 fixed last_position = fixed::Zero();
     486           0 :                 for (size_t i = 0; i < position_nodes.size(); ++i)
     487             :                 {
     488           0 :                     fixed distance = i > 0 ? position_nodes[i - 1].Distance : fixed::Zero();
     489           0 :                     last_position += distance;
     490           0 :                     events.emplace_back(last_position, "Position", position_nodes[i].Position);
     491             :                 }
     492             : 
     493           0 :                 fixed last_target = fixed::Zero();
     494           0 :                 for (size_t i = 0; i < target_nodes.size(); ++i)
     495             :                 {
     496           0 :                     fixed distance = i > 0 ? target_nodes[i - 1].Distance : fixed::Zero();
     497           0 :                     last_target += distance;
     498           0 :                     events.emplace_back(last_target, "Target", target_nodes[i].Position);
     499             :                 }
     500             : 
     501           0 :                 std::sort(events.begin(), events.end());
     502           0 :                 for (size_t i = 0; i < events.size();)
     503             :                 {
     504           0 :                     XMLWriter_Element nodeTag(xmlMapFile, "Node");
     505           0 :                     fixed deltatime = i > 0 ? (events[i].time - events[i - 1].time) : fixed::Zero();
     506           0 :                     nodeTag.Attribute("deltatime", deltatime);
     507           0 :                     size_t j = i;
     508           0 :                     for (; j < events.size() && events[j].time == events[i].time; ++j)
     509             :                     {
     510             :                         // Types: Position/Rotation/Target
     511           0 :                         XMLWriter_Element eventTypeTag(xmlMapFile, events[j].type);
     512           0 :                         eventTypeTag.Attribute("x", events[j].value.X);
     513           0 :                         eventTypeTag.Attribute("y", events[j].value.Y);
     514           0 :                         eventTypeTag.Attribute("z", events[j].value.Z);
     515           0 :                     }
     516           0 :                     i = j;
     517           0 :                 }
     518           0 :             }
     519           0 :         }
     520           0 :     }
     521           0 :     if (!xmlMapFile.StoreVFS(g_VFS, filename))
     522           0 :         LOGERROR("Failed to write map '{}'", filename.string8());
     523           0 : }

Generated by: LCOV version 1.16