LCOV - code coverage report
Current view: top level - source/tools/atlas/GameInterface/Handlers - ObjectHandlers.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 542 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 100 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 "MessageHandler.h"
      21             : 
      22             : #include "graphics/Camera.h"
      23             : #include "graphics/Color.h"
      24             : #include "graphics/GameView.h"
      25             : #include "graphics/Terrain.h"
      26             : #include "lib/debug.h"
      27             : #include "lib/utf8.h"
      28             : #include "maths/Fixed.h"
      29             : #include "maths/FixedVector3D.h"
      30             : #include "maths/MathUtil.h"
      31             : #include "maths/Matrix3D.h"
      32             : #include "maths/Vector2D.h"
      33             : #include "maths/Vector3D.h"
      34             : #include "ps/CLogger.h"
      35             : #include "ps/CStr.h"
      36             : #include "ps/Game.h"
      37             : #include "ps/World.h"
      38             : #include "ps/XML/XMLWriter.h"
      39             : #include "simulation2/Simulation2.h"
      40             : #include "simulation2/components/ICmpObstruction.h"
      41             : #include "simulation2/components/ICmpOwnership.h"
      42             : #include "simulation2/components/ICmpPlayer.h"
      43             : #include "simulation2/components/ICmpPlayerManager.h"
      44             : #include "simulation2/components/ICmpPosition.h"
      45             : #include "simulation2/components/ICmpSelectable.h"
      46             : #include "simulation2/components/ICmpTemplateManager.h"
      47             : #include "simulation2/components/ICmpUnitMotion.h"
      48             : #include "simulation2/components/ICmpVisual.h"
      49             : #include "simulation2/helpers/Player.h"
      50             : #include "simulation2/helpers/Position.h"
      51             : #include "simulation2/helpers/Selection.h"
      52             : #include "simulation2/system/Component.h"
      53             : #include "simulation2/system/Entity.h"
      54             : #include "tools/atlas/GameInterface/CommandProc.h"
      55             : #include "tools/atlas/GameInterface/Messages.h"
      56             : #include "tools/atlas/GameInterface/Shareable.h"
      57             : #include "tools/atlas/GameInterface/SharedTypes.h"
      58             : #include "tools/atlas/GameInterface/View.h"
      59             : 
      60             : #include <algorithm>
      61             : #include <cmath>
      62             : #include <cstdint>
      63             : #include <cwchar>
      64             : #include <map>
      65             : #include <set>
      66             : #include <string>
      67             : #include <unordered_map>
      68             : #include <utility>
      69             : #include <vector>
      70             : 
      71             : namespace AtlasMessage
      72             : {
      73             : 
      74             : namespace
      75             : {
      76           0 :     bool SortObjectsList(const sObjectsListItem& a, const sObjectsListItem& b)
      77             :     {
      78           0 :         return wcscmp(a.name.c_str(), b.name.c_str()) < 0;
      79             :     }
      80             : } // anonymous namespace
      81             : 
      82             : // Helpers for object constraints
      83           0 : bool CheckEntityObstruction(entity_id_t ent)
      84             : {
      85           0 :     CmpPtr<ICmpObstruction> cmpObstruction(*g_Game->GetSimulation2(), ent);
      86           0 :     if (cmpObstruction)
      87             :     {
      88           0 :         std::string passClassName = "default";
      89           0 :         CmpPtr<ICmpUnitMotion> cmpUnitMotion(*g_Game->GetSimulation2(), ent);
      90           0 :         if (cmpUnitMotion)
      91           0 :             passClassName = cmpUnitMotion->GetPassabilityClassName();
      92             : 
      93           0 :         ICmpObstruction::EFoundationCheck result = cmpObstruction->CheckFoundation(passClassName);
      94           0 :         if (result != ICmpObstruction::FOUNDATION_CHECK_SUCCESS)
      95           0 :             return false;
      96           0 :     }
      97             :     return true;
      98             : }
      99             : 
     100           0 : void CheckObstructionAndUpdateVisual(entity_id_t id)
     101             : {
     102           0 :     CmpPtr<ICmpVisual> cmpVisual(*g_Game->GetSimulation2(), id);
     103           0 :     if (cmpVisual)
     104             :     {
     105           0 :         if (!CheckEntityObstruction(id))
     106           0 :             cmpVisual->SetShadingColor(fixed::FromDouble(1.4), fixed::FromDouble(0.4), fixed::FromDouble(0.4), fixed::FromDouble(1));
     107             :         else
     108           0 :             cmpVisual->SetShadingColor(fixed::FromDouble(1), fixed::FromDouble(1), fixed::FromDouble(1), fixed::FromDouble(1));
     109             :     }
     110           0 : }
     111             : 
     112           0 : QUERYHANDLER(GetObjectsList)
     113             : {
     114           0 :     std::vector<sObjectsListItem> objects;
     115             : 
     116           0 :     CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     117           0 :     if (cmpTemplateManager)
     118             :     {
     119           0 :         std::vector<std::string> names = cmpTemplateManager->FindAllTemplates(true);
     120             : 
     121           0 :         for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it)
     122             :         {
     123           0 :             std::wstring name(it->begin(), it->end());
     124             : 
     125           0 :             sObjectsListItem e;
     126           0 :             e.id = name;
     127           0 :             if (name.substr(0, 6) == L"actor|")
     128             :             {
     129           0 :                 e.name = name.substr(6);
     130           0 :                 e.type = 1;
     131             :             }
     132             :             else
     133             :             {
     134           0 :                 e.name = name;
     135           0 :                 e.type = 0;
     136             :             }
     137           0 :             objects.push_back(e);
     138           0 :         }
     139           0 :     }
     140             : 
     141           0 :     std::sort(objects.begin(), objects.end(), SortObjectsList);
     142           0 :     msg->objects = objects;
     143           0 : }
     144             : 
     145             : 
     146             : static std::vector<entity_id_t> g_Selection;
     147             : typedef std::map<player_id_t, CColor> PlayerColorMap;
     148             : 
     149             : // Helper function to find color of player owning the given entity,
     150             : //  returns white if entity has no owner. Uses caching to avoid
     151             : //  expensive script calls.
     152           0 : static CColor GetOwnerPlayerColor(PlayerColorMap& colorMap, entity_id_t id)
     153             : {
     154             :     // Default color - white
     155           0 :     CColor color(1.0f, 1.0f, 1.0f, 1.0f);
     156             : 
     157           0 :     CSimulation2& sim = *g_Game->GetSimulation2();
     158           0 :     CmpPtr<ICmpOwnership> cmpOwnership(sim, id);
     159           0 :     if (cmpOwnership)
     160             :     {
     161           0 :         player_id_t owner = cmpOwnership->GetOwner();
     162           0 :         if (colorMap.find(owner) != colorMap.end())
     163           0 :             return colorMap[owner];
     164             :         else
     165             :         {
     166           0 :             CmpPtr<ICmpPlayerManager> cmpPlayerManager(sim, SYSTEM_ENTITY);
     167           0 :             entity_id_t playerEnt = cmpPlayerManager->GetPlayerByID(owner);
     168           0 :             CmpPtr<ICmpPlayer> cmpPlayer(sim, playerEnt);
     169           0 :             if (cmpPlayer)
     170             :             {
     171           0 :                 colorMap[owner] = cmpPlayer->GetDisplayedColor();
     172           0 :                 color = colorMap[owner];
     173             :             }
     174             :         }
     175             :     }
     176           0 :     return color;
     177             : }
     178             : 
     179           0 : MESSAGEHANDLER(SetSelectionPreview)
     180             : {
     181           0 :     CSimulation2& sim = *g_Game->GetSimulation2();
     182             : 
     183             :     // Cache player colors for performance
     184           0 :     PlayerColorMap playerColors;
     185             : 
     186             :     // Clear old selection rings
     187           0 :     for (size_t i = 0; i < g_Selection.size(); ++i)
     188             :     {
     189             :         // We can't set only alpha here, because that won't trigger desaturation
     190             :         //  so we set the complete color (not too evil since it's cached)
     191           0 :         CmpPtr<ICmpSelectable> cmpSelectable(sim, g_Selection[i]);
     192           0 :         if (cmpSelectable)
     193             :         {
     194           0 :             CColor color = GetOwnerPlayerColor(playerColors, g_Selection[i]);
     195           0 :             color.a = 0.0f;
     196           0 :             cmpSelectable->SetSelectionHighlight(color, false);
     197             :         }
     198             :     }
     199             : 
     200           0 :     g_Selection = *msg->ids;
     201             : 
     202             :     // Set new selection rings
     203           0 :     for (size_t i = 0; i < g_Selection.size(); ++i)
     204             :     {
     205           0 :         CmpPtr<ICmpSelectable> cmpSelectable(sim, g_Selection[i]);
     206           0 :         if (cmpSelectable)
     207           0 :             cmpSelectable->SetSelectionHighlight(GetOwnerPlayerColor(playerColors, g_Selection[i]), true);
     208             :     }
     209           0 : }
     210             : 
     211           0 : QUERYHANDLER(GetObjectSettings)
     212             : {
     213           0 :     AtlasView* view = AtlasView::GetView(msg->view);
     214           0 :     CSimulation2* simulation = view->GetSimulation2();
     215             : 
     216           0 :     sObjectSettings settings;
     217           0 :     settings.player = 0;
     218             : 
     219           0 :     CmpPtr<ICmpOwnership> cmpOwnership(*simulation, view->GetEntityId(msg->id));
     220           0 :     if (cmpOwnership)
     221             :     {
     222           0 :         int32_t player = cmpOwnership->GetOwner();
     223           0 :         if (player != -1)
     224           0 :             settings.player = player;
     225             :     }
     226             : 
     227             :     // TODO: selections
     228             : 
     229             : /*
     230             :     // Get the unit's possible variants and selected variants
     231             :     std::vector<std::vector<CStr> > groups = unit->GetObject().m_Base->GetVariantGroups();
     232             :     const std::set<CStr>& selections = unit->GetActorSelections();
     233             : 
     234             :     // Iterate over variant groups
     235             :     std::vector<std::vector<std::wstring> > variantgroups;
     236             :     std::set<std::wstring> selections_set;
     237             :     variantgroups.reserve(groups.size());
     238             :     for (size_t i = 0; i < groups.size(); ++i)
     239             :     {
     240             :         // Copy variants into output structure
     241             : 
     242             :         std::vector<std::wstring> group;
     243             :         group.reserve(groups[i].size());
     244             :         int choice = -1;
     245             : 
     246             :         for (size_t j = 0; j < groups[i].size(); ++j)
     247             :         {
     248             :             group.push_back(CStrW(groups[i][j]));
     249             : 
     250             :             // Find the first string in 'selections' that matches one of this
     251             :             // group's variants
     252             :             if (choice == -1)
     253             :                 if (selections.find(groups[i][j]) != selections.end())
     254             :                     choice = (int)j;
     255             :         }
     256             : 
     257             :         // Assuming one of the variants was selected (which it really ought
     258             :         // to be), remember that one's name
     259             :         if (choice != -1)
     260             :             selections_set.insert(CStrW(groups[i][choice]));
     261             : 
     262             :         variantgroups.push_back(group);
     263             :     }
     264             : 
     265             :     settings.variantgroups = variantgroups;
     266             :     settings.selections = std::vector<std::wstring> (selections_set.begin(), selections_set.end()); // convert set->vector
     267             : */
     268             : 
     269           0 :     msg->settings = settings;
     270           0 : }
     271             : 
     272           0 : QUERYHANDLER(GetObjectMapSettings)
     273             : {
     274           0 :     std::vector<entity_id_t> ids = *msg->ids;
     275             : 
     276           0 :     CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     277           0 :     ENSURE(cmpTemplateManager);
     278             : 
     279           0 :     XMLWriter_File exampleFile;
     280           0 :     {
     281           0 :         XMLWriter_Element entitiesTag(exampleFile, "Entities");
     282           0 :         {
     283           0 :             for (entity_id_t id : ids)
     284             :             {
     285           0 :                 XMLWriter_Element entityTag(exampleFile, "Entity");
     286           0 :                 {
     287             :                     //Template name
     288           0 :                     entityTag.Setting("Template", cmpTemplateManager->GetCurrentTemplateName(id));
     289             : 
     290             :                     //Player
     291           0 :                     CmpPtr<ICmpOwnership> cmpOwnership(*g_Game->GetSimulation2(), id);
     292           0 :                     if (cmpOwnership)
     293           0 :                         entityTag.Setting("Player", static_cast<int>(cmpOwnership->GetOwner()));
     294             : 
     295             :                     //Adding position to make some relative position later
     296           0 :                     CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     297           0 :                     if (cmpPosition)
     298             :                     {
     299           0 :                         CFixedVector3D pos = cmpPosition->GetPosition();
     300           0 :                         CFixedVector3D rot = cmpPosition->GetRotation();
     301           0 :                         {
     302           0 :                             XMLWriter_Element positionTag(exampleFile, "Position");
     303           0 :                             positionTag.Attribute("x", pos.X);
     304           0 :                             positionTag.Attribute("z", pos.Z);
     305             :                             // TODO: height offset etc
     306           0 :                         }
     307           0 :                         {
     308           0 :                             XMLWriter_Element orientationTag(exampleFile, "Orientation");
     309           0 :                             orientationTag.Attribute("y", rot.Y);
     310             :                             // TODO: X, Z maybe
     311           0 :                         }
     312             :                     }
     313             : 
     314             :                     // Adding actor seed
     315           0 :                     CmpPtr<ICmpVisual> cmpVisual(*g_Game->GetSimulation2(), id);
     316           0 :                     if (cmpVisual)
     317           0 :                         entityTag.Setting("ActorSeed", static_cast<unsigned int>(cmpVisual->GetActorSeed()));
     318             :                 }
     319           0 :             }
     320             :         }
     321           0 :     }
     322             : 
     323           0 :     const CStr& data = exampleFile.GetOutput();
     324           0 :     msg->xmldata = data.FromUTF8();
     325           0 : }
     326             : 
     327             : 
     328           0 : BEGIN_COMMAND(SetObjectSettings)
     329             : {
     330             :     player_id_t m_PlayerOld, m_PlayerNew;
     331             :     std::set<CStr> m_SelectionsOld, m_SelectionsNew;
     332             : 
     333           0 :     void Do()
     334             :     {
     335           0 :         sObjectSettings settings = msg->settings;
     336             : 
     337           0 :         AtlasView* view = AtlasView::GetView(msg->view);
     338           0 :         CSimulation2* simulation = view->GetSimulation2();
     339             : 
     340           0 :         CmpPtr<ICmpOwnership> cmpOwnership(*simulation, view->GetEntityId(msg->id));
     341           0 :         m_PlayerOld = 0;
     342           0 :         if (cmpOwnership)
     343             :         {
     344           0 :             int32_t player = cmpOwnership->GetOwner();
     345           0 :             if (player != -1)
     346           0 :                 m_PlayerOld = player;
     347             :         }
     348             : 
     349             :         // TODO: selections
     350             : //      m_SelectionsOld = unit->GetActorSelections();
     351             : 
     352           0 :         m_PlayerNew = (player_id_t)settings.player;
     353             : 
     354           0 :         std::vector<std::wstring> selections = *settings.selections;
     355           0 :         for (std::vector<std::wstring>::iterator it = selections.begin(); it != selections.end(); ++it)
     356             :         {
     357           0 :             m_SelectionsNew.insert(CStrW(*it).ToUTF8());
     358             :         }
     359             : 
     360           0 :         Redo();
     361           0 :     }
     362             : 
     363           0 :     void Redo()
     364             :     {
     365           0 :         Set(m_PlayerNew, m_SelectionsNew);
     366           0 :     }
     367             : 
     368           0 :     void Undo()
     369             :     {
     370           0 :         Set(m_PlayerOld, m_SelectionsOld);
     371           0 :     }
     372             : 
     373             : private:
     374           0 :     void Set(player_id_t player, const std::set<CStr>& /*selections*/)
     375             :     {
     376           0 :         AtlasView* view = AtlasView::GetView(msg->view);
     377           0 :         CSimulation2* simulation = view->GetSimulation2();
     378             : 
     379           0 :         CmpPtr<ICmpOwnership> cmpOwnership(*simulation, view->GetEntityId(msg->id));
     380           0 :         if (cmpOwnership)
     381           0 :             cmpOwnership->SetOwner(player);
     382             : 
     383             :         // TODO: selections
     384             : //      unit->SetActorSelections(selections);
     385           0 :     }
     386             : };
     387           0 : END_COMMAND(SetObjectSettings);
     388             : 
     389             : //////////////////////////////////////////////////////////////////////////
     390             : 
     391             : static CStrW g_PreviewUnitName;
     392             : static entity_id_t g_PreviewEntityID = INVALID_ENTITY;
     393             : static std::vector<entity_id_t> g_PreviewEntitiesID;
     394             : 
     395           0 : static CVector3D GetUnitPos(const Position& pos, bool floating)
     396             : {
     397           0 :     static CVector3D vec;
     398           0 :     vec = pos.GetWorldSpace(vec, floating); // if msg->pos is 'Unchanged', use the previous pos
     399             : 
     400             :     // Clamp the position to the edges of the world:
     401             : 
     402             :     // Use 'Clamp' with a value slightly less than the width, so that converting
     403             :     // to integer (rounding towards zero) will put it on the tile inside the edge
     404             :     // instead of just outside
     405           0 :     const float mapWidth = (g_Game->GetWorld()->GetTerrain().GetVerticesPerSide()-1)*TERRAIN_TILE_SIZE;
     406           0 :     float delta = 1e-6f; // fraction of map width - must be > FLT_EPSILON
     407             : 
     408           0 :     float xOnMap = Clamp(vec.X, 0.f, mapWidth * (1.f - delta));
     409           0 :     float zOnMap = Clamp(vec.Z, 0.f, mapWidth * (1.f - delta));
     410             : 
     411             :     // Don't waste time with GetExactGroundLevel unless we've changed
     412           0 :     if (xOnMap != vec.X || zOnMap != vec.Z)
     413             :     {
     414           0 :         vec.X = xOnMap;
     415           0 :         vec.Z = zOnMap;
     416           0 :         vec.Y = g_Game->GetWorld()->GetTerrain().GetExactGroundLevel(xOnMap, zOnMap);
     417             :     }
     418             : 
     419           0 :     return vec;
     420             : }
     421             : 
     422           0 : QUERYHANDLER(GetCurrentSelection)
     423             : {
     424           0 :     msg->ids = g_Selection;
     425           0 : }
     426             : 
     427           0 : MESSAGEHANDLER(ObjectPreviewToEntity)
     428             : {
     429           0 :     if (g_PreviewEntitiesID.size() == 0)
     430           0 :         return;
     431             : 
     432           0 :     CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     433           0 :     ENSURE(cmpTemplateManager);
     434             : 
     435           0 :     PlayerColorMap playerColor;
     436             : 
     437             :     //I need to re create the objects finally delete preview objects
     438           0 :     for (entity_id_t ent : g_PreviewEntitiesID)
     439             :     {
     440             :         //Get template name (without the "preview|" prefix)
     441           0 :         std::wstring wTemplateName = wstring_from_utf8(cmpTemplateManager->GetCurrentTemplateName(ent).substr(8));
     442             :         //Create new entity
     443           0 :         entity_id_t new_ent = g_Game->GetSimulation2()->AddEntity(wTemplateName);
     444           0 :         if (new_ent == INVALID_ENTITY)
     445           0 :             continue;
     446             : 
     447             :         //get position, get rotation
     448           0 :         CmpPtr<ICmpPosition> cmpPositionNew(*g_Game->GetSimulation2(), new_ent);
     449           0 :         CmpPtr<ICmpPosition> cmpPositionOld(*g_Game->GetSimulation2(), ent);
     450             : 
     451           0 :         if (cmpPositionNew && cmpPositionOld)
     452             :         {
     453           0 :             CVector3D pos = cmpPositionOld->GetPosition();
     454           0 :             cmpPositionNew->JumpTo(entity_pos_t::FromFloat(pos.X), entity_pos_t::FromFloat(pos.Z));
     455             : 
     456             :             //now rotate
     457           0 :             CFixedVector3D rotation = cmpPositionOld->GetRotation();
     458           0 :             cmpPositionNew->SetYRotation(rotation.Y);
     459             :         }
     460             : 
     461             :         //get owner
     462           0 :         CmpPtr<ICmpOwnership> cmpOwnershipNew(*g_Game->GetSimulation2(), new_ent);
     463           0 :         CmpPtr<ICmpOwnership> cmpOwnershipOld(*g_Game->GetSimulation2(), ent);
     464           0 :         if (cmpOwnershipNew && cmpOwnershipOld)
     465           0 :             cmpOwnershipNew->SetOwner(cmpOwnershipOld->GetOwner());
     466             : 
     467             :         //getVisual
     468           0 :         CmpPtr<ICmpVisual> cmpVisualNew(*g_Game->GetSimulation2(), new_ent);
     469           0 :         CmpPtr<ICmpVisual> cmpVisualOld(*g_Game->GetSimulation2(), ent);
     470           0 :         if (cmpVisualNew && cmpVisualOld)
     471           0 :             cmpVisualNew->SetActorSeed(cmpVisualOld->GetActorSeed());
     472             : 
     473             :         //Update g_selectedObject and higligth
     474           0 :         g_Selection.push_back(new_ent);
     475           0 :         CmpPtr<ICmpSelectable> cmpSelectable(*g_Game->GetSimulation2(), new_ent);
     476           0 :         if (cmpSelectable)
     477           0 :             cmpSelectable->SetSelectionHighlight(GetOwnerPlayerColor(playerColor, new_ent), true);
     478             : 
     479           0 :         g_Game->GetSimulation2()->DestroyEntity(ent);
     480           0 :     }
     481           0 :     g_PreviewEntitiesID.clear();
     482             : 
     483             : }
     484             : 
     485           0 : MESSAGEHANDLER(MoveObjectPreview)
     486             : {
     487           0 :     if (g_PreviewEntitiesID.size()==0)
     488           0 :         return;
     489             : 
     490             :     //TODO:Change pivot
     491           0 :     entity_id_t referenceEntity = *g_PreviewEntitiesID.begin();
     492             : 
     493             :     // All selected objects move relative to a pivot object,
     494             :     //  so get its position and whether it's floating
     495           0 :     CFixedVector3D referencePos;
     496             : 
     497           0 :     CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), referenceEntity);
     498           0 :     if (cmpPosition && cmpPosition->IsInWorld())
     499           0 :         referencePos = cmpPosition->GetPosition();
     500             : 
     501             : 
     502             :     // Calculate directional vector of movement for pivot object,
     503             :     //  we apply the same movement to all objects
     504           0 :     CVector3D targetPos = GetUnitPos(msg->pos, true);
     505           0 :     CFixedVector3D fTargetPos(entity_pos_t::FromFloat(targetPos.X), entity_pos_t::FromFloat(targetPos.Y), entity_pos_t::FromFloat(targetPos.Z));
     506           0 :     CFixedVector3D dir = fTargetPos - referencePos;
     507             : 
     508           0 :     for (const entity_id_t id : g_PreviewEntitiesID)
     509             :     {
     510           0 :         CmpPtr<ICmpPosition> cmpPreviewPosition(*g_Game->GetSimulation2(), id);
     511           0 :         if (cmpPreviewPosition)
     512             :         {
     513           0 :             CFixedVector3D posFinal;
     514           0 :             if (cmpPreviewPosition->IsInWorld())
     515             :             {
     516             :                 // Calculate this object's position
     517           0 :                 CFixedVector3D posFixed = cmpPreviewPosition->GetPosition();
     518           0 :                 posFinal = posFixed + dir;
     519             :             }
     520           0 :             cmpPreviewPosition->JumpTo(posFinal.X, posFinal.Z);
     521             :         }
     522             : 
     523           0 :         CheckObstructionAndUpdateVisual(id);
     524             :     }
     525             : }
     526             : 
     527           0 : MESSAGEHANDLER(ObjectPreview)
     528             : {
     529             :     // If the selection has changed...
     530           0 :     if (*msg->id != g_PreviewUnitName || (!msg->cleanObjectPreviews))
     531             :     {
     532             :         // Delete old entity
     533           0 :         if (g_PreviewEntityID != INVALID_ENTITY && msg->cleanObjectPreviews)
     534             :         {
     535             :             //Time to delete all preview objects
     536           0 :             for (entity_id_t ent : g_PreviewEntitiesID)
     537           0 :                 g_Game->GetSimulation2()->DestroyEntity(ent);
     538           0 :             g_PreviewEntitiesID.clear();
     539             :         }
     540             : 
     541             :         // Create the new entity
     542           0 :         if ((*msg->id).empty())
     543           0 :             g_PreviewEntityID = INVALID_ENTITY;
     544             :         else
     545             :         {
     546           0 :             g_PreviewEntityID = g_Game->GetSimulation2()->AddLocalEntity(L"preview|" + *msg->id);
     547           0 :             g_PreviewEntitiesID.push_back(g_PreviewEntityID);
     548             :         }
     549             : 
     550             : 
     551           0 :         g_PreviewUnitName = *msg->id;
     552             :     }
     553             : 
     554           0 :     if (g_PreviewEntityID != INVALID_ENTITY)
     555             :     {
     556             :         // Update the unit's position and orientation:
     557             : 
     558           0 :         CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), g_PreviewEntityID);
     559           0 :         if (cmpPosition)
     560             :         {
     561           0 :             CVector3D pos = GetUnitPos(msg->pos, cmpPosition->CanFloat());
     562           0 :             cmpPosition->JumpTo(entity_pos_t::FromFloat(pos.X), entity_pos_t::FromFloat(pos.Z));
     563             : 
     564           0 :             float angle;
     565           0 :             if (msg->usetarget)
     566             :             {
     567             :                 // Aim from pos towards msg->target
     568           0 :                 CVector3D target = msg->target->GetWorldSpace(pos.Y);
     569           0 :                 angle = atan2(target.X-pos.X, target.Z-pos.Z);
     570             :             }
     571             :             else
     572             :             {
     573           0 :                 angle = msg->angle;
     574             :             }
     575             : 
     576           0 :             cmpPosition->SetYRotation(entity_angle_t::FromFloat(angle));
     577             :         }
     578             : 
     579             :         // TODO: handle random variations somehow
     580             : 
     581           0 :         CmpPtr<ICmpVisual> cmpVisual(*g_Game->GetSimulation2(), g_PreviewEntityID);
     582           0 :         if (cmpVisual)
     583           0 :             cmpVisual->SetActorSeed(msg->actorseed);
     584             : 
     585           0 :         CmpPtr<ICmpOwnership> cmpOwnership(*g_Game->GetSimulation2(), g_PreviewEntityID);
     586           0 :         if (cmpOwnership)
     587           0 :             cmpOwnership->SetOwner((player_id_t)msg->settings->player);
     588             : 
     589           0 :         CheckObstructionAndUpdateVisual(g_PreviewEntityID);
     590             :     }
     591           0 : }
     592             : 
     593           0 : BEGIN_COMMAND(CreateObject)
     594             : {
     595             :     CVector3D m_Pos;
     596             :     float m_Angle;
     597             :     player_id_t m_Player;
     598             :     entity_id_t m_EntityID;
     599             :     std::uint32_t m_ActorSeed;
     600             : 
     601           0 :     void Do()
     602             :     {
     603             :         // Calculate the position/orientation to create this unit with
     604             : 
     605           0 :         m_Pos = GetUnitPos(msg->pos, true); // don't really care about floating
     606             : 
     607           0 :         if (msg->usetarget)
     608             :         {
     609             :             // Aim from m_Pos towards msg->target
     610           0 :             CVector3D target = msg->target->GetWorldSpace(m_Pos.Y);
     611           0 :             m_Angle = atan2(target.X-m_Pos.X, target.Z-m_Pos.Z);
     612             :         }
     613             :         else
     614             :         {
     615           0 :             m_Angle = msg->angle;
     616             :         }
     617             : 
     618           0 :         m_Player = (player_id_t)msg->settings->player;
     619           0 :         m_ActorSeed = msg->actorseed;
     620             :         // TODO: variation/selection strings
     621             : 
     622           0 :         Redo();
     623           0 :     }
     624             : 
     625           0 :     void Redo()
     626             :     {
     627           0 :         m_EntityID = g_Game->GetSimulation2()->AddEntity(*msg->id);
     628           0 :         if (m_EntityID == INVALID_ENTITY)
     629           0 :             return;
     630             : 
     631           0 :         CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), m_EntityID);
     632           0 :         if (cmpPosition)
     633             :         {
     634           0 :             cmpPosition->JumpTo(entity_pos_t::FromFloat(m_Pos.X), entity_pos_t::FromFloat(m_Pos.Z));
     635           0 :             cmpPosition->SetYRotation(entity_angle_t::FromFloat(m_Angle));
     636             :         }
     637             : 
     638           0 :         CmpPtr<ICmpOwnership> cmpOwnership(*g_Game->GetSimulation2(), m_EntityID);
     639           0 :         if (cmpOwnership)
     640           0 :             cmpOwnership->SetOwner(m_Player);
     641             : 
     642           0 :         CmpPtr<ICmpVisual> cmpVisual(*g_Game->GetSimulation2(), m_EntityID);
     643           0 :         if (cmpVisual)
     644             :         {
     645           0 :             cmpVisual->SetActorSeed(m_ActorSeed);
     646             :             // TODO: variation/selection strings
     647             :         }
     648             :     }
     649             : 
     650           0 :     void Undo()
     651             :     {
     652           0 :         if (m_EntityID != INVALID_ENTITY)
     653             :         {
     654           0 :             g_Game->GetSimulation2()->DestroyEntity(m_EntityID);
     655           0 :             m_EntityID = INVALID_ENTITY;
     656             :         }
     657           0 :     }
     658             : };
     659           0 : END_COMMAND(CreateObject)
     660             : 
     661             : 
     662           0 : QUERYHANDLER(PickObject)
     663             : {
     664           0 :     float x, y;
     665           0 :     msg->pos->GetScreenSpace(x, y);
     666             : 
     667             :     // Normally this function would be called with a player ID to check LOS,
     668             :     //  but in Atlas the entire map is revealed, so just pass INVALID_PLAYER
     669           0 :     entity_id_t ent = EntitySelection::PickEntityAtPoint(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), x, y, INVALID_PLAYER, msg->selectActors);;
     670             : 
     671           0 :     if (ent == INVALID_ENTITY)
     672           0 :         msg->id = INVALID_ENTITY;
     673             :     else
     674             :     {
     675           0 :         msg->id = ent;
     676             :         // Calculate offset of object from original mouse click position
     677             :         //  so it gets moved by that offset
     678           0 :         CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), ent);
     679           0 :         if (!cmpPosition || !cmpPosition->IsInWorld())
     680             :         {
     681             :             // error
     682           0 :             msg->offsetx = msg->offsety = 0;
     683             :         }
     684             :         else
     685             :         {
     686           0 :             CFixedVector3D fixed = cmpPosition->GetPosition();
     687           0 :             CVector3D centre = CVector3D(fixed.X.ToFloat(), fixed.Y.ToFloat(), fixed.Z.ToFloat());
     688             : 
     689           0 :             const CVector2D screenPos{g_Game->GetView()->GetCamera().GetScreenCoordinates(centre)};
     690             : 
     691           0 :             msg->offsetx = static_cast<int>(screenPos.X - x);
     692           0 :             msg->offsety = static_cast<int>(screenPos.Y - y);
     693             :         }
     694             :     }
     695           0 : }
     696             : 
     697             : 
     698           0 : QUERYHANDLER(PickObjectsInRect)
     699             : {
     700           0 :     float x0, y0, x1, y1;
     701           0 :     msg->start->GetScreenSpace(x0, y0);
     702           0 :     msg->end->GetScreenSpace(x1, y1);
     703             : 
     704             :     // Since owner selections are meaningless in Atlas, use INVALID_PLAYER
     705           0 :     msg->ids = EntitySelection::PickEntitiesInRect(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), x0, y0, x1, y1, INVALID_PLAYER, msg->selectActors);
     706           0 : }
     707             : 
     708             : 
     709           0 : QUERYHANDLER(PickSimilarObjects)
     710             : {
     711           0 :     CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
     712           0 :     ENSURE(cmpTemplateManager);
     713             : 
     714           0 :     entity_id_t ent = msg->id;
     715           0 :     std::string templateName = cmpTemplateManager->GetCurrentTemplateName(ent);
     716             : 
     717             :     // If unit has ownership, only pick units from the same player
     718           0 :     player_id_t owner = INVALID_PLAYER;
     719           0 :     CmpPtr<ICmpOwnership> cmpOwnership(*g_Game->GetSimulation2(), ent);
     720           0 :     if (cmpOwnership)
     721           0 :         owner = cmpOwnership->GetOwner();
     722             : 
     723           0 :     msg->ids = EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), g_Game->GetView()->GetCamera(), templateName, owner, false, true, true, false);
     724           0 : }
     725             : 
     726           0 : MESSAGEHANDLER(ResetSelectionColor)
     727             : {
     728           0 :     for (entity_id_t ent : g_Selection)
     729             :     {
     730           0 :         CmpPtr<ICmpVisual> cmpVisual(*g_Game->GetSimulation2(), ent);
     731           0 :         if (cmpVisual)
     732           0 :             cmpVisual->SetShadingColor(fixed::FromDouble(1), fixed::FromDouble(1), fixed::FromDouble(1), fixed::FromDouble(1));
     733             :     }
     734           0 : }
     735             : 
     736           0 : BEGIN_COMMAND(MoveObjects)
     737             : {
     738             :     // Mapping from object to position
     739             :     std::map<entity_id_t, CVector3D> m_PosOld, m_PosNew;
     740             : 
     741           0 :     void Do()
     742             :     {
     743           0 :         std::vector<entity_id_t> ids = *msg->ids;
     744             : 
     745             :         // All selected objects move relative to a pivot object,
     746             :         //  so get its position and whether it's floating
     747           0 :         CVector3D pivotPos(0, 0, 0);
     748           0 :         bool pivotFloating = false;
     749             : 
     750           0 :         CmpPtr<ICmpPosition> cmpPositionPivot(*g_Game->GetSimulation2(), (entity_id_t)msg->pivot);
     751           0 :         if (cmpPositionPivot && cmpPositionPivot->IsInWorld())
     752             :         {
     753           0 :             pivotFloating = cmpPositionPivot->CanFloat();
     754           0 :             CFixedVector3D pivotFixed = cmpPositionPivot->GetPosition();
     755           0 :             pivotPos = CVector3D(pivotFixed.X.ToFloat(), pivotFixed.Y.ToFloat(), pivotFixed.Z.ToFloat());
     756             :         }
     757             : 
     758             :         // Calculate directional vector of movement for pivot object,
     759             :         //  we apply the same movement to all objects
     760           0 :         CVector3D targetPos = GetUnitPos(msg->pos, pivotFloating);
     761           0 :         CVector3D dir = targetPos - pivotPos;
     762             : 
     763           0 :         for (entity_id_t id : ids)
     764             :         {
     765           0 :             CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     766           0 :             if (!cmpPosition || !cmpPosition->IsInWorld())
     767             :             {
     768             :                 // error
     769           0 :                 m_PosOld[id] = m_PosNew[id] = CVector3D(0, 0, 0);
     770             :             }
     771             :             else
     772             :             {
     773             :                 // Calculate this object's position
     774           0 :                 CFixedVector3D posFixed = cmpPosition->GetPosition();
     775           0 :                 CVector3D pos = CVector3D(posFixed.X.ToFloat(), posFixed.Y.ToFloat(), posFixed.Z.ToFloat());
     776           0 :                 m_PosNew[id] = pos + dir;
     777           0 :                 m_PosOld[id] = pos;
     778             :             }
     779             :         }
     780             : 
     781           0 :         SetPos(m_PosNew);
     782           0 :     }
     783             : 
     784           0 :     void SetPos(const std::map<entity_id_t, CVector3D>& map)
     785             :     {
     786           0 :         for (const std::pair<const entity_id_t, CVector3D>& p : map)
     787             :         {
     788           0 :             CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), p.first);
     789           0 :             if (!cmpPosition)
     790           0 :                 return;
     791             : 
     792             :             // Set 2D position, ignoring height
     793           0 :             cmpPosition->JumpTo(entity_pos_t::FromFloat(p.second.X), entity_pos_t::FromFloat(p.second.Z));
     794             : 
     795           0 :             CheckObstructionAndUpdateVisual(p.first);
     796             :         }
     797             :     }
     798             : 
     799           0 :     void Redo()
     800             :     {
     801           0 :         SetPos(m_PosNew);
     802           0 :     }
     803             : 
     804           0 :     void Undo()
     805             :     {
     806           0 :         SetPos(m_PosOld);
     807           0 :     }
     808             : 
     809           0 :     void MergeIntoPrevious(cMoveObjects* prev)
     810             :     {
     811             :         // TODO: do something valid if prev selection != this selection
     812           0 :         ENSURE(*(prev->msg->ids) == *(msg->ids));
     813           0 :         prev->m_PosNew = m_PosNew;
     814           0 :     }
     815             : };
     816           0 : END_COMMAND(MoveObjects)
     817             : 
     818           0 : BEGIN_COMMAND(RotateObjectsFromCenterPoint)
     819             : {
     820             :     std::map<entity_id_t, CVector3D> m_PosOld, m_PosNew;
     821             :     std::map<entity_id_t, float> m_AngleOld, m_AngleNew;
     822             :     CVector3D m_CenterPoint;
     823             :     float m_AngleInitialRotation;
     824             : 
     825           0 :     void Do()
     826             :     {
     827           0 :         std::vector<entity_id_t> ids = *msg->ids;
     828             : 
     829           0 :         CVector3D minPos;
     830           0 :         CVector3D maxPos;
     831             : 
     832           0 :         bool first = true;
     833             : 
     834             :         // Compute min position and max position
     835           0 :         for (entity_id_t id : ids)
     836             :         {
     837           0 :             CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     838           0 :             if (!cmpPosition)
     839           0 :                 continue;
     840             : 
     841           0 :             CVector3D pos = cmpPosition->GetPosition();
     842             : 
     843           0 :             m_PosOld[id] = cmpPosition->GetPosition();
     844           0 :             m_AngleOld[id] = cmpPosition->GetRotation().Y.ToFloat();
     845             : 
     846           0 :             if (first)
     847             :             {
     848           0 :                 first = false;
     849           0 :                 minPos = pos;
     850           0 :                 maxPos = pos;
     851           0 :                 m_CenterPoint.Y = pos.Y;
     852           0 :                 continue;
     853             :             }
     854             : 
     855           0 :             if (pos.X < minPos.X)
     856           0 :                 minPos.X = pos.X;
     857             : 
     858           0 :             if (pos.X > maxPos.X)
     859           0 :                 maxPos.X = pos.X;
     860             : 
     861           0 :             if (pos.Z < minPos.Z)
     862           0 :                 minPos.Z = pos.Z;
     863             : 
     864           0 :             if (pos.Z > maxPos.Z)
     865           0 :                 maxPos.Z = pos.Z;
     866             :         }
     867             : 
     868             :         // Calculate objects center point
     869           0 :         m_CenterPoint.X = minPos.X + ((maxPos.X - minPos.X) * 0.5);
     870           0 :         m_CenterPoint.Z = minPos.Z + ((maxPos.Z - minPos.Z) * 0.5);
     871             : 
     872           0 :         CVector3D target = msg->target->GetWorldSpace(m_CenterPoint.Y);
     873           0 :         m_AngleInitialRotation = atan2(target.X-m_CenterPoint.X, target.Z-m_CenterPoint.Z);
     874           0 :     }
     875             : 
     876           0 :     void SetPos(const std::map<entity_id_t, CVector3D>& position, const std::map<entity_id_t, float>& angle)
     877             :     {
     878           0 :         for (const std::pair<const entity_id_t, CVector3D>& p : position)
     879             :         {
     880           0 :             CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), p.first);
     881           0 :             if (!cmpPosition)
     882           0 :                 return;
     883             : 
     884             :             // Set 2D position, ignoring height
     885           0 :             cmpPosition->JumpTo(entity_pos_t::FromFloat(p.second.X), entity_pos_t::FromFloat(p.second.Z));
     886             : 
     887           0 :             if (msg->rotateObject)
     888           0 :                 cmpPosition->SetYRotation(entity_angle_t::FromFloat(angle.at(p.first)));
     889             : 
     890             :         }
     891             : 
     892           0 :         for (const std::pair<const entity_id_t, CVector3D>& p : position)
     893           0 :             CheckObstructionAndUpdateVisual(p.first);
     894             :     }
     895             : 
     896           0 :     void Redo()
     897             :     {
     898           0 :         SetPos(m_PosNew, m_AngleNew);
     899           0 :     }
     900             : 
     901           0 :     void RecalculateRotation(Position newPoint)
     902             :     {
     903           0 :         std::vector<entity_id_t> ids = *msg->ids;
     904             : 
     905           0 :         CVector3D target = newPoint.GetWorldSpace(m_CenterPoint.Y);
     906           0 :         float newAngle = atan2(target.X-m_CenterPoint.X, target.Z-m_CenterPoint.Z);
     907             : 
     908           0 :         float globalAngle = m_AngleInitialRotation - newAngle;
     909             : 
     910             :         // Recalculate positions
     911           0 :         for (entity_id_t id : ids)
     912             :         {
     913           0 :             CVector3D pos = m_PosOld[id];
     914           0 :             float angle = atan2(pos.X - m_CenterPoint.X, pos.Z - m_CenterPoint.Z);
     915           0 :             float localAngle = angle + (globalAngle - angle);
     916           0 :             float xCos = cosf(localAngle);
     917           0 :             float xSin = sinf(localAngle);
     918             : 
     919           0 :             pos.X -= m_CenterPoint.X;
     920           0 :             pos.Z -= m_CenterPoint.Z;
     921             : 
     922           0 :             float newX = pos.X * xCos - pos.Z * xSin;
     923           0 :             float newZ = pos.X * xSin + pos.Z * xCos;
     924             : 
     925           0 :             pos.X = newX + m_CenterPoint.X;
     926           0 :             pos.Z = newZ + m_CenterPoint.Z;
     927             : 
     928           0 :             m_PosNew[id] = pos;
     929             : 
     930           0 :             m_AngleNew[id] = m_AngleOld[id] - globalAngle;
     931             :         }
     932             : 
     933           0 :         SetPos(m_PosNew, m_AngleNew);
     934           0 :     }
     935             : 
     936           0 :     void Undo()
     937             :     {
     938           0 :         SetPos(m_PosOld, m_AngleOld);
     939           0 :     }
     940             : 
     941           0 :     void MergeIntoPrevious(cRotateObjectsFromCenterPoint* prev)
     942             :     {
     943             :         // TODO: do something valid if prev unit != this unit
     944           0 :         ENSURE(*prev->msg->ids == *msg->ids);
     945           0 :         m_PosOld = prev->m_PosOld;
     946           0 :         m_AngleInitialRotation = prev->m_AngleInitialRotation;
     947           0 :         m_AngleOld = prev->m_AngleOld;
     948           0 :         m_CenterPoint = prev->m_CenterPoint;
     949             : 
     950           0 :         RecalculateRotation(msg->target);
     951           0 :     }
     952             : };
     953           0 : END_COMMAND(RotateObjectsFromCenterPoint)
     954             : 
     955           0 : BEGIN_COMMAND(RotateObject)
     956             : {
     957             :     std::map<entity_id_t, float> m_AngleOld, m_AngleNew;
     958             : 
     959           0 :     void Do()
     960             :     {
     961           0 :         std::vector<entity_id_t> ids = *msg->ids;
     962             : 
     963           0 :         for (entity_id_t id : ids)
     964             :         {
     965           0 :             CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), id);
     966           0 :             if (!cmpPosition)
     967           0 :                 return;
     968             : 
     969           0 :             m_AngleOld[id] = cmpPosition->GetRotation().Y.ToFloat();
     970             : 
     971           0 :             CMatrix3D transform = cmpPosition->GetInterpolatedTransform(0.f);
     972           0 :             CVector3D pos = transform.GetTranslation();
     973           0 :             CVector3D target = msg->target->GetWorldSpace(pos.Y);
     974           0 :             m_AngleNew[id] = atan2(target.X-pos.X, target.Z-pos.Z);
     975             :         }
     976             : 
     977           0 :         SetAngle(m_AngleNew);
     978           0 :     }
     979             : 
     980           0 :     void SetAngle(const std::map<entity_id_t, float>& angles)
     981             :     {
     982           0 :         for (const std::pair<const entity_id_t, float>& p : angles)
     983             :         {
     984           0 :             CmpPtr<ICmpPosition> cmpPosition(*g_Game->GetSimulation2(), p.first);
     985           0 :             if (!cmpPosition)
     986           0 :                 return;
     987             : 
     988           0 :             cmpPosition->SetYRotation(entity_angle_t::FromFloat(p.second));
     989             :         }
     990             :     }
     991             : 
     992           0 :     void Redo()
     993             :     {
     994           0 :         SetAngle(m_AngleNew);
     995           0 :     }
     996             : 
     997           0 :     void Undo()
     998             :     {
     999           0 :         SetAngle(m_AngleOld);
    1000           0 :     }
    1001             : 
    1002           0 :     void MergeIntoPrevious(cRotateObject* prev)
    1003             :     {
    1004             :         // TODO: do something valid if prev unit != this unit
    1005           0 :         ENSURE(*prev->msg->ids == *msg->ids);
    1006           0 :         prev->m_AngleNew = m_AngleNew;
    1007           0 :     }
    1008             : };
    1009           0 : END_COMMAND(RotateObject)
    1010             : 
    1011             : 
    1012           0 : BEGIN_COMMAND(DeleteObjects)
    1013             : {
    1014             :     // Saved copy of the important aspects of a unit, to allow undo
    1015           0 :     struct OldObject
    1016             :     {
    1017             :         entity_id_t entityID;
    1018             :         CStr templateName;
    1019             :         player_id_t owner;
    1020             :         CFixedVector3D pos;
    1021             :         CFixedVector3D rot;
    1022             :         std::uint32_t actorSeed;
    1023             :     };
    1024             : 
    1025             :     std::vector<OldObject> oldObjects;
    1026             : 
    1027           0 :     cDeleteObjects()
    1028           0 :     {
    1029             :     }
    1030             : 
    1031           0 :     void Do()
    1032             :     {
    1033           0 :         Redo();
    1034           0 :     }
    1035             : 
    1036           0 :     void Redo()
    1037             :     {
    1038           0 :         CSimulation2& sim = *g_Game->GetSimulation2();
    1039           0 :         CmpPtr<ICmpTemplateManager> cmpTemplateManager(sim, SYSTEM_ENTITY);
    1040           0 :         ENSURE(cmpTemplateManager);
    1041             : 
    1042           0 :         std::vector<entity_id_t> ids = *msg->ids;
    1043           0 :         for (size_t i = 0; i < ids.size(); ++i)
    1044             :         {
    1045           0 :             OldObject obj;
    1046             : 
    1047           0 :             obj.entityID = (entity_id_t)ids[i];
    1048           0 :             obj.templateName = cmpTemplateManager->GetCurrentTemplateName(obj.entityID);
    1049             : 
    1050           0 :             CmpPtr<ICmpOwnership> cmpOwnership(sim, obj.entityID);
    1051           0 :             if (cmpOwnership)
    1052           0 :                 obj.owner = cmpOwnership->GetOwner();
    1053             : 
    1054           0 :             CmpPtr<ICmpPosition> cmpPosition(sim, obj.entityID);
    1055           0 :             if (cmpPosition)
    1056             :             {
    1057           0 :                 obj.pos = cmpPosition->GetPosition();
    1058           0 :                 obj.rot = cmpPosition->GetRotation();
    1059             :             }
    1060             : 
    1061           0 :             CmpPtr<ICmpVisual> cmpVisual(sim, obj.entityID);
    1062           0 :             if (cmpVisual)
    1063           0 :                 obj.actorSeed = cmpVisual->GetActorSeed();
    1064             : 
    1065           0 :             oldObjects.push_back(obj);
    1066           0 :             g_Game->GetSimulation2()->DestroyEntity(obj.entityID);
    1067           0 :         }
    1068           0 :         g_Game->GetSimulation2()->FlushDestroyedEntities();
    1069           0 :     }
    1070             : 
    1071           0 :     void Undo()
    1072             :     {
    1073           0 :         CSimulation2& sim = *g_Game->GetSimulation2();
    1074             : 
    1075           0 :         for (size_t i = 0; i < oldObjects.size(); ++i)
    1076             :         {
    1077           0 :             entity_id_t ent = sim.AddEntity(oldObjects[i].templateName.FromUTF8(), oldObjects[i].entityID);
    1078           0 :             if (ent == INVALID_ENTITY)
    1079             :             {
    1080           0 :                 LOGERROR("Failed to load entity template '{}'", oldObjects[i].templateName);
    1081             :             }
    1082             :             else
    1083             :             {
    1084           0 :                 CmpPtr<ICmpPosition> cmpPosition(sim, oldObjects[i].entityID);
    1085           0 :                 if (cmpPosition)
    1086             :                 {
    1087           0 :                     cmpPosition->JumpTo(oldObjects[i].pos.X, oldObjects[i].pos.Z);
    1088           0 :                     cmpPosition->SetXZRotation(oldObjects[i].rot.X, oldObjects[i].rot.Z);
    1089           0 :                     cmpPosition->SetYRotation(oldObjects[i].rot.Y);
    1090             :                 }
    1091             : 
    1092           0 :                 CmpPtr<ICmpOwnership> cmpOwnership(sim, oldObjects[i].entityID);
    1093           0 :                 if (cmpOwnership)
    1094           0 :                     cmpOwnership->SetOwner(oldObjects[i].owner);
    1095             : 
    1096           0 :                 CmpPtr<ICmpVisual> cmpVisual(sim, oldObjects[i].entityID);
    1097           0 :                 if (cmpVisual)
    1098           0 :                     cmpVisual->SetActorSeed(oldObjects[i].actorSeed);
    1099             :             }
    1100             :         }
    1101             : 
    1102           0 :         oldObjects.clear();
    1103           0 :     }
    1104             : };
    1105           0 : END_COMMAND(DeleteObjects)
    1106             : 
    1107           0 : QUERYHANDLER(GetPlayerObjects)
    1108             : {
    1109           0 :     std::vector<entity_id_t> ids;
    1110           0 :     player_id_t playerID = msg->player;
    1111             : 
    1112           0 :     const CSimulation2::InterfaceListUnordered& cmps = g_Game->GetSimulation2()->GetEntitiesWithInterfaceUnordered(IID_Ownership);
    1113           0 :     for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit)
    1114             :     {
    1115           0 :         if (static_cast<ICmpOwnership*>(eit->second)->GetOwner() == playerID)
    1116             :         {
    1117           0 :             ids.push_back(eit->first);
    1118             :         }
    1119             :     }
    1120             : 
    1121           0 :     msg->ids = ids;
    1122           0 : }
    1123             : 
    1124           0 : MESSAGEHANDLER(SetBandbox)
    1125             : {
    1126           0 :     AtlasView::GetView_Game()->SetBandbox(msg->show, (float)msg->sx0, (float)msg->sy0, (float)msg->sx1, (float)msg->sy1);
    1127           0 : }
    1128             : 
    1129           0 : QUERYHANDLER(GetSelectedObjectsTemplateNames)
    1130             : {
    1131           0 :     std::vector<entity_id_t> ids = *msg->ids;
    1132           0 :     std::vector<std::string> names;
    1133             : 
    1134           0 :     CmpPtr<ICmpTemplateManager> cmpTemplateManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
    1135           0 :     ENSURE(cmpTemplateManager);
    1136             : 
    1137           0 :     for (size_t i = 0; i < ids.size(); ++i)
    1138             :     {
    1139           0 :         entity_id_t id = (entity_id_t)ids[i];
    1140           0 :         std::string templateName = cmpTemplateManager->GetCurrentTemplateName(id);
    1141           0 :         names.push_back(templateName);
    1142           0 :     }
    1143             : 
    1144           0 :     std::sort(names.begin(), names.end());
    1145           0 :     msg->names = names;
    1146           0 : }
    1147             : 
    1148             : } // namespace AtlasMessage

Generated by: LCOV version 1.16