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: 1 72 1.4 %
Date: 2023-01-19 00:18:29 Functions: 2 11 18.2 %

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

Generated by: LCOV version 1.13