LCOV - code coverage report
Current view: top level - source/simulation2/components - CCmpVision.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 8 58 13.8 %
Date: 2023-01-19 00:18:29 Functions: 5 19 26.3 %

          Line data    Source code
       1             : /* Copyright (C) 2022 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 "simulation2/system/Component.h"
      21             : #include "ICmpVision.h"
      22             : 
      23             : #include "simulation2/MessageTypes.h"
      24             : #include "simulation2/components/ICmpPlayerManager.h"
      25             : #include "simulation2/components/ICmpRangeManager.h"
      26             : #include "simulation2/components/ICmpValueModificationManager.h"
      27             : 
      28           0 : class CCmpVision final : public ICmpVision
      29             : {
      30             : public:
      31         116 :     static void ClassInit(CComponentManager& componentManager)
      32             :     {
      33         116 :         componentManager.SubscribeToMessageType(MT_OwnershipChanged);
      34         116 :         componentManager.SubscribeToMessageType(MT_ValueModification);
      35         116 :         componentManager.SubscribeToMessageType(MT_Deserialized);
      36         116 :     }
      37             : 
      38           0 :     DEFAULT_COMPONENT_ALLOCATOR(Vision)
      39             : 
      40             :     // Template state:
      41             : 
      42             :     entity_pos_t m_Range, m_BaseRange;
      43             : 
      44             :     // TODO: The reveal shore system should be replaced by a general
      45             :     // system of "special" vision methods that are not ranges.
      46             :     bool m_RevealShore;
      47             : 
      48         116 :     static std::string GetSchema()
      49             :     {
      50             :         return
      51             :             "<element name='Range'>"
      52             :                 "<data type='nonNegativeInteger'/>"
      53             :             "</element>"
      54             :             "<optional>"
      55             :                 "<element name='RevealShore'>"
      56             :                     "<data type='boolean'/>"
      57             :                 "</element>"
      58         116 :             "</optional>";
      59             :     }
      60             : 
      61           0 :     void Init(const CParamNode& paramNode) override
      62             :     {
      63           0 :         m_BaseRange = m_Range = paramNode.GetChild("Range").ToFixed();
      64             : 
      65           0 :         if (paramNode.GetChild("RevealShore").IsOk())
      66           0 :             m_RevealShore = paramNode.GetChild("RevealShore").ToBool();
      67             :         else
      68           0 :             m_RevealShore = false;
      69           0 :     }
      70             : 
      71           0 :     void Deinit() override
      72             :     {
      73           0 :     }
      74             : 
      75           0 :     void Serialize(ISerializer& UNUSED(serialize)) override
      76             :     {
      77             :         // No dynamic state to serialize
      78           0 :     }
      79             : 
      80           0 :     void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize)) override
      81             :     {
      82           0 :         Init(paramNode);
      83           0 :     }
      84             : 
      85           0 :     void HandleMessage(const CMessage& msg, bool UNUSED(global)) override
      86             :     {
      87           0 :         switch (msg.GetType())
      88             :         {
      89           0 :         case MT_OwnershipChanged:
      90             :         {
      91           0 :             const CMessageOwnershipChanged& msgData = static_cast<const CMessageOwnershipChanged&> (msg);
      92           0 :             if (msgData.entity != GetEntityId())
      93           0 :                 break;
      94             : 
      95           0 :             ReloadRange();
      96           0 :             break;
      97             :         }
      98           0 :         case MT_ValueModification:
      99             :         {
     100           0 :             const CMessageValueModification& msgData = static_cast<const CMessageValueModification&> (msg);
     101           0 :             if (msgData.component != L"Vision")
     102           0 :                 break;
     103           0 :             ReloadRange();
     104           0 :             break;
     105             :         }
     106           0 :         case MT_Deserialized:
     107             :         {
     108           0 :             CmpPtr<ICmpValueModificationManager> cmpValueModificationManager(GetSystemEntity());
     109           0 :             if (cmpValueModificationManager)
     110           0 :                 m_Range = cmpValueModificationManager->ApplyModifications(L"Vision/Range", m_BaseRange, GetEntityId());
     111           0 :             break;
     112             :         }
     113             :         }
     114           0 :     }
     115             : 
     116           0 :     virtual void ReloadRange()
     117             :     {
     118           0 :         CmpPtr<ICmpValueModificationManager> cmpValueModificationManager(GetSystemEntity());
     119           0 :         if (!cmpValueModificationManager)
     120           0 :             return;
     121             : 
     122           0 :         entity_pos_t newRange = cmpValueModificationManager->ApplyModifications(L"Vision/Range", m_BaseRange, GetEntityId());
     123           0 :         if (newRange == m_Range)
     124           0 :             return;
     125             : 
     126             :         // Update our vision range and broadcast message
     127           0 :         entity_pos_t oldRange = m_Range;
     128           0 :         m_Range = newRange;
     129           0 :         CMessageVisionRangeChanged msg(GetEntityId(), oldRange, newRange);
     130           0 :         GetSimContext().GetComponentManager().BroadcastMessage(msg);
     131             :     }
     132             : 
     133           0 :     entity_pos_t GetRange() const override
     134             :     {
     135           0 :         return m_Range;
     136             :     }
     137             : 
     138           0 :     bool GetRevealShore() const override
     139             :     {
     140           0 :         return m_RevealShore;
     141             :     }
     142             : };
     143             : 
     144         119 : REGISTER_COMPONENT_TYPE(Vision)

Generated by: LCOV version 1.13