LCOV - code coverage report
Current view: top level - source/simulation2/components - CCmpMinimap.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 77 112 68.8 %
Date: 2026-12-02 01:21:50 Functions: 10 18 55.6 %

          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 "ICmpMinimap.h"
      21             : 
      22             : #include "graphics/Color.h"
      23             : #include "simulation2/MessageTypes.h"
      24             : #include "simulation2/components/ICmpOwnership.h"
      25             : #include "simulation2/components/ICmpPlayer.h"
      26             : #include "simulation2/components/ICmpPlayerManager.h"
      27             : #include "simulation2/helpers/Player.h"
      28             : #include "simulation2/helpers/Position.h"
      29             : #include "simulation2/system/Component.h"
      30             : #include "simulation2/system/Message.h"
      31             : 
      32             : #include <string>
      33             : 
      34         186 : class CCmpMinimap final : public ICmpMinimap
      35             : {
      36             : public:
      37         137 :     static void ClassInit(CComponentManager& componentManager)
      38             :     {
      39         137 :         componentManager.SubscribeToMessageType(MT_Deserialized);
      40         137 :         componentManager.SubscribeToMessageType(MT_PositionChanged);
      41         137 :         componentManager.SubscribeToMessageType(MT_OwnershipChanged);
      42         137 :         componentManager.SubscribeToMessageType(MT_PlayerColorChanged);
      43         137 :         componentManager.SubscribeToMessageType(MT_MinimapPing);
      44         137 :     }
      45             : 
      46         372 :     DEFAULT_COMPONENT_ALLOCATOR(Minimap)
      47             : 
      48             :     // Template state:
      49             : 
      50             :     bool m_UsePlayerColor;
      51             : 
      52             :     std::uint8_t m_R, m_G, m_B; // static template state if m_UsePlayerColor false; dynamic state if true
      53             : 
      54             :     // Dynamic state:
      55             : 
      56             :     bool m_Active;
      57             :     entity_pos_t m_X, m_Z; // cache the latest position for more efficient rendering; only valid when m_Active true
      58             : 
      59             :     // Not serialized (based on renderer timing):
      60             :     // TODO: eventually ping state should be serialized and tied into simulation time, but currently lag causes too many problems
      61             :     double m_PingEndTime;
      62             :     bool m_IsPinging;
      63             : 
      64             :     bool m_HasIcon = false;
      65             :     std::string m_IconPath;
      66             :     float m_IconSize = 16.0f;
      67             : 
      68         137 :     static std::string GetSchema()
      69             :     {
      70         137 :         return
      71             :             "<optional>"
      72             :                 "<element name='Color'>"
      73             :                     "<attribute name='r'>"
      74             :                         "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
      75             :                     "</attribute>"
      76             :                     "<attribute name='g'>"
      77             :                         "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
      78             :                     "</attribute>"
      79             :                     "<attribute name='b'>"
      80             :                         "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
      81             :                     "</attribute>"
      82             :                 "</element>"
      83             :             "</optional>"
      84             :             "<optional>"
      85             :                 "<element name='Icon' a:help='Icon texture that should be displayed on a minimap. Filepath relative to art/textures/ui/session/icons/minimap/.'>"
      86             :                     "<attribute name='size'>"
      87             :                         "<data type='float'><param name='minExclusive'>0</param></data>"
      88             :                     "</attribute>"
      89             :                     "<text/>"
      90             :                 "</element>"
      91         137 :             "</optional>";
      92             :     }
      93             : 
      94         186 :     void Init(const CParamNode& paramNode) override
      95             :     {
      96         186 :         m_Active = true;
      97         186 :         m_IsPinging = false;
      98         186 :         m_PingEndTime = 0.0;
      99         186 :         m_HasIcon = false;
     100             : 
     101         186 :         const CParamNode& color = paramNode.GetChild("Color");
     102         186 :         if (color.IsOk())
     103             :         {
     104          12 :             m_UsePlayerColor = false;
     105          12 :             m_R = static_cast<std::uint8_t>(color.GetChild("@r").ToInt());
     106          12 :             m_G = static_cast<std::uint8_t>(color.GetChild("@g").ToInt());
     107          12 :             m_B = static_cast<std::uint8_t>(color.GetChild("@b").ToInt());
     108             :         }
     109             :         else
     110             :         {
     111         174 :             m_UsePlayerColor = true;
     112             :             // Choose a bogus color which will get replaced once we have an owner
     113         174 :             m_R = 255;
     114         174 :             m_G = 0;
     115         174 :             m_B = 255;
     116             :         }
     117             : 
     118         186 :         const CParamNode& iconNode = paramNode.GetChild("Icon");
     119         186 :         if (iconNode.IsOk())
     120             :         {
     121           0 :             const CParamNode& iconSizeNode = iconNode.GetChild("@size");
     122           0 :             if (iconSizeNode.IsOk())
     123             :             {
     124           0 :                 m_HasIcon = true;
     125           0 :                 m_IconPath = "art/textures/ui/session/icons/minimap/" + iconNode.ToString();
     126           0 :                 m_IconSize = iconSizeNode.ToFloat();
     127             :             }
     128             :         }
     129         186 :     }
     130             : 
     131         186 :     void Deinit() override
     132             :     {
     133         186 :     }
     134             : 
     135             :     template<typename S>
     136        1032 :     void SerializeCommon(S& serialize)
     137             :     {
     138        1032 :         serialize.Bool("active", m_Active);
     139             : 
     140        1032 :         if (m_Active)
     141             :         {
     142        1032 :             serialize.NumberFixed_Unbounded("x", m_X);
     143        1032 :             serialize.NumberFixed_Unbounded("z", m_Z);
     144             :         }
     145        1032 :     }
     146             : 
     147        1032 :     void Serialize(ISerializer& serialize) override
     148             :     {
     149        1032 :         SerializeCommon(serialize);
     150        1032 :     }
     151             : 
     152           0 :     void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override
     153             :     {
     154           0 :         Init(paramNode);
     155             : 
     156           0 :         SerializeCommon(deserialize);
     157           0 :     }
     158             : 
     159        8328 :     void HandleMessage(const CMessage& msg, bool /*global*/) override
     160             :     {
     161        8328 :         switch (msg.GetType())
     162             :         {
     163        7923 :         case MT_PositionChanged:
     164        7923 :         {
     165        7923 :             const CMessagePositionChanged& data = static_cast<const CMessagePositionChanged&> (msg);
     166             : 
     167        7923 :             if (data.inWorld)
     168             :             {
     169        7923 :                 m_Active = true;
     170        7923 :                 m_X = data.x;
     171        7923 :                 m_Z = data.z;
     172             :             }
     173             :             else
     174             :             {
     175           0 :                 m_Active = false;
     176             :             }
     177             : 
     178             :             break;
     179             :         }
     180         216 :         case MT_Deserialized:
     181         216 :         case MT_OwnershipChanged:
     182         216 :         case MT_PlayerColorChanged:
     183         216 :         {
     184         216 :             UpdateColor();
     185         216 :             break;
     186             :         }
     187         189 :         case MT_MinimapPing:
     188         189 :         {
     189         189 :             CmpPtr<ICmpOwnership> cmpOwnership(GetSimContext(), GetEntityId());
     190         189 :             if (!cmpOwnership || cmpOwnership->GetOwner() != (player_id_t)GetSimContext().GetCurrentDisplayedPlayer())
     191             :                 break;
     192             : 
     193             :             // This depends on the viewing player, so don't alter the synchronized simulation state
     194           0 :             m_IsPinging = true;
     195           0 :             m_PingEndTime = 0.0;
     196             : 
     197           0 :             break;
     198             :         }
     199             :         }
     200        8328 :     }
     201             : 
     202           0 :     bool GetRenderData(std::uint8_t& r, std::uint8_t& g, std::uint8_t& b, entity_pos_t& x, entity_pos_t& z) const override
     203             :     {
     204           0 :         if (!m_Active)
     205             :             return false;
     206             : 
     207           0 :         r = m_R;
     208           0 :         g = m_G;
     209           0 :         b = m_B;
     210           0 :         x = m_X;
     211           0 :         z = m_Z;
     212           0 :         return true;
     213             :     }
     214             : 
     215           0 :     bool CheckPing(double currentTime, double pingDuration) override
     216             :     {
     217           0 :         if (!m_Active || !m_IsPinging)
     218             :             return false;
     219             : 
     220             :         // We're currently pinging
     221           0 :         if (m_PingEndTime == 0.0)
     222           0 :             m_PingEndTime = currentTime + pingDuration;
     223           0 :         else if (currentTime > m_PingEndTime)
     224             :         {
     225           0 :             m_IsPinging = false;
     226           0 :             m_PingEndTime = 0;
     227             :         }
     228             : 
     229           0 :         return m_IsPinging;
     230             :     }
     231             : 
     232         216 :     void UpdateColor() override
     233             :     {
     234         216 :         if (!m_UsePlayerColor)
     235          42 :             return;
     236             : 
     237         198 :         CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
     238         198 :         if (!cmpOwnership)
     239             :             return;
     240             : 
     241         198 :         player_id_t owner = cmpOwnership->GetOwner();
     242         198 :         if (owner == INVALID_PLAYER)
     243             :             return;
     244             : 
     245         174 :         CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
     246         174 :         if (!cmpPlayerManager)
     247             :             return;
     248             : 
     249         174 :         CmpPtr<ICmpPlayer> cmpPlayer(GetSimContext(), cmpPlayerManager->GetPlayerByID(owner));
     250         174 :         if (!cmpPlayer)
     251             :             return;
     252             : 
     253         174 :         CColor color = cmpPlayer->GetDisplayedColor();
     254         174 :         m_R = static_cast<std::uint8_t>(color.r * 255);
     255         174 :         m_G = static_cast<std::uint8_t>(color.g * 255);
     256         174 :         m_B = static_cast<std::uint8_t>(color.b * 255);
     257             :     }
     258             : 
     259           0 :     bool HasIcon() override
     260             :     {
     261           0 :         return m_HasIcon;
     262             :     }
     263             : 
     264           0 :     std::string GetIconPath() override
     265             :     {
     266           0 :         return m_IconPath;
     267             :     }
     268             : 
     269           0 :     float GetIconSize() override
     270             :     {
     271           0 :         return m_IconSize;
     272             :     }
     273             : };
     274             : 
     275         274 : REGISTER_COMPONENT_TYPE(Minimap)

Generated by: LCOV version 1.16