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