LCOV - code coverage report
Current view: top level - source/graphics - Unit.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 32 67 47.8 %
Date: 2026-12-03 01:19:39 Functions: 5 8 62.5 %

          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 "Unit.h"
      21             : 
      22             : #include "graphics/Model.h"
      23             : #include "graphics/ModelAbstract.h"
      24             : #include "graphics/ObjectBase.h"
      25             : #include "graphics/ObjectEntry.h"
      26             : #include "graphics/ObjectManager.h"
      27             : #include "graphics/UnitAnimation.h"
      28             : #include "lib/code_generation.h"
      29             : #include "ps/CLogger.h"
      30             : 
      31             : #include <string>
      32             : #include <utility>
      33             : #include <vector>
      34             : 
      35           1 : CUnit::CUnit(CObjectManager& objectManager, const CActorDef& actor, const entity_id_t id, const uint32_t seed)
      36           1 :     : m_ObjectManager(objectManager), m_Actor(actor), m_ID(id), m_Seed(seed), m_Animation(nullptr)
      37             : {
      38             :     /**
      39             :      * When entity selections change, we might end up with a different layout in terms of variants/groups,
      40             :      * which means the random key calculation might end up with different results for the same seed.
      41             :      * This is bad, as it means entities randomly change appearence when changing e.g. animation.
      42             :      * To fix this, we'll initially pick a random and complete specification based on our seed,
      43             :      * and then pass that as the lowest priority selections. Thus, if the actor files are properly specified,
      44             :      * we can ensure that the entities will look the same no matter what happens.
      45             :      */
      46           2 :     SetActorSelections(m_Actor.PickSelectionsAtRandom(m_Seed)); // Calls ReloadObject().
      47           1 : }
      48             : 
      49           1 : CUnit::~CUnit()
      50             : {
      51           1 :     delete m_Animation;
      52           1 : }
      53             : 
      54           1 : std::unique_ptr<CUnit> CUnit::Create(const CStrW& actorName, const entity_id_t id, const uint32_t seed, CObjectManager& objectManager)
      55             : {
      56           1 :     const CActorDef& actor{std::get<1>(objectManager.FindActorDef(actorName))};
      57             : 
      58           1 :     std::unique_ptr<CUnit> unit{new CUnit(objectManager, actor, id, seed)};
      59           1 :     if (!unit->m_Model)
      60           0 :         return nullptr;
      61             : 
      62           1 :     return unit;
      63           1 : }
      64             : 
      65           0 : void CUnit::UpdateModel(float frameTime)
      66             : {
      67           0 :     if (m_Animation)
      68           0 :         m_Animation->Update(frameTime*1000.0f);
      69           0 : }
      70             : 
      71           0 : void CUnit::SetEntitySelection(const CStr& key, const CStr& selection)
      72             : {
      73           0 :     CStr selection_lc = selection.LowerCase();
      74             : 
      75           0 :     if (m_EntitySelections[key] == selection_lc)
      76           0 :         return;
      77           0 :     m_EntitySelections[key] = selection_lc;
      78             : 
      79           0 :     ReloadObject();
      80           0 : }
      81             : 
      82           0 : void CUnit::SetEntitySelection(const std::map<CStr, CStr>& selections)
      83             : {
      84           0 :     for (const std::pair<const CStr, CStr>& s : selections)
      85           0 :         m_EntitySelections[s.first] = s.second.LowerCase();
      86             : 
      87           0 :     ReloadObject();
      88           0 : }
      89             : 
      90           2 : void CUnit::SetActorSelections(const std::set<CStr>& selections)
      91             : {
      92           2 :     m_ActorSelections = selections;
      93           2 :     ReloadObject();
      94           1 : }
      95             : 
      96           2 : void CUnit::ReloadObject()
      97             : {
      98           2 :     std::set<CStr> entitySelections;
      99           2 :     for (const std::pair<const CStr, CStr>& selection : m_EntitySelections)
     100           0 :         entitySelections.insert(selection.second);
     101           2 :     std::vector<std::set<CStr>> selections;
     102           2 :     selections.push_back(entitySelections);
     103           2 :     selections.push_back(m_ActorSelections);
     104             : 
     105             :     // randomly select any remain selections necessary to completely identify a variation (e.g., the new selection
     106             :     // made might define some additional props that require a random variant choice). Also, FindObjectVariation
     107             :     // expects the selectors passed to it to be complete.
     108             :     // see https://gitea.wildfiregames.com/0ad/0ad/issues/979
     109             : 
     110             :     // If these selections give a different object, change this unit to use it
     111             :     // Use the entity ID as randomization seed (same as when the unit was first created)
     112           2 :     CObjectEntry* newObject = m_ObjectManager.FindObjectVariation(&m_Actor, selections, m_Seed);
     113           2 :     if (!newObject)
     114             :     {
     115           0 :         LOGERROR("Error loading object variation (actor: {})", m_Actor.GetPathname().string8());
     116             :         // Don't delete the unit, don't override our current (valid) state.
     117           0 :         return;
     118             :     }
     119             : 
     120           2 :     if (!m_Object)
     121             :     {
     122           1 :         m_Object = newObject;
     123           1 :         m_Model = newObject->m_Model->Clone();
     124           1 :         if (m_Model->ToCModel())
     125           1 :             m_Animation = new CUnitAnimation(m_ID, m_Model->ToCModel(), m_Object);
     126             :     }
     127           1 :     else if (m_Object && newObject != m_Object)
     128             :     {
     129             :         // Clone the new object's base (non-instance) model
     130           0 :         std::unique_ptr<CModelAbstract> newModel = newObject->m_Model->Clone();
     131             : 
     132             :         // Copy the old instance-specific settings from the old model to the new instance
     133           0 :         newModel->SetTransform(m_Model->GetTransform());
     134           0 :         newModel->SetPlayerID(m_Model->GetPlayerID());
     135           0 :         if (newModel->ToCModel() && m_Model->ToCModel())
     136             :         {
     137           0 :             newModel->ToCModel()->CopyAnimationFrom(m_Model->ToCModel());
     138             : 
     139             :             // Copy flags that belong to this model instance (not those defined by the actor XML)
     140           0 :             int instanceFlags = (ModelFlag::SILHOUETTE_DISPLAY | ModelFlag::SILHOUETTE_OCCLUDER | ModelFlag::IGNORE_LOS) & m_Model->ToCModel()->GetFlags();
     141           0 :             newModel->ToCModel()->AddFlagsRec(instanceFlags);
     142             :         }
     143             : 
     144           0 :         m_Model = std::move(newModel);
     145           0 :         m_Object = newObject;
     146             : 
     147           0 :         if (m_Model->ToCModel())
     148             :         {
     149           0 :             if (m_Animation)
     150           0 :                 m_Animation->ReloadUnit(m_Model->ToCModel(), m_Object); // TODO: maybe this should try to preserve animation state?
     151             :             else
     152           0 :                 m_Animation = new CUnitAnimation(m_ID, m_Model->ToCModel(), m_Object);
     153             :         }
     154             :         else
     155             :         {
     156           0 :             SAFE_DELETE(m_Animation);
     157             :         }
     158           0 :     }
     159           2 : }

Generated by: LCOV version 1.16