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 "ICmpOwnership.h" 21 : 22 : #include "simulation2/MessageTypes.h" 23 : #include "simulation2/helpers/Player.h" 24 : #include "simulation2/system/Component.h" 25 : #include "simulation2/system/Message.h" 26 : 27 : #include <string> 28 : 29 : /** 30 : * Basic ICmpOwnership implementation. 31 : */ 32 195 : class CCmpOwnership final : public ICmpOwnership 33 : { 34 : public: 35 137 : static void ClassInit(CComponentManager& componentManager) 36 : { 37 137 : componentManager.SubscribeToMessageType(MT_Destroy); 38 : } 39 : 40 390 : DEFAULT_COMPONENT_ALLOCATOR(Ownership) 41 : 42 : player_id_t m_Owner; 43 : 44 137 : static std::string GetSchema() 45 : { 46 137 : return 47 : "<a:example/>" 48 : "<a:help>Allows this entity to be owned by players.</a:help>" 49 137 : "<empty/>"; 50 : } 51 : 52 195 : void Init(const CParamNode&) override 53 : { 54 195 : m_Owner = INVALID_PLAYER; 55 195 : } 56 : 57 195 : void Deinit() override 58 : { 59 195 : } 60 : 61 1032 : void Serialize(ISerializer& serialize) override 62 : { 63 1032 : serialize.NumberI32_Unbounded("owner", m_Owner); 64 1032 : } 65 : 66 0 : void Deserialize(const CParamNode&, IDeserializer& deserialize) override 67 : { 68 0 : deserialize.NumberI32_Unbounded("owner", m_Owner); 69 0 : } 70 : 71 30 : void HandleMessage(const CMessage& msg, bool /*global*/) override 72 : { 73 30 : switch (msg.GetType()) 74 : { 75 30 : case MT_Destroy: 76 30 : { 77 : // Reset the owner so this entity is e.g. removed from population counts 78 30 : SetOwner(INVALID_PLAYER); 79 30 : break; 80 : } 81 : } 82 30 : } 83 : 84 19866 : player_id_t GetOwner() const override 85 : { 86 19866 : return m_Owner; 87 : } 88 : 89 225 : void SetOwner(player_id_t playerID) override 90 : { 91 225 : if (playerID == m_Owner) 92 0 : return; 93 : 94 225 : player_id_t old = m_Owner; 95 225 : m_Owner = playerID; 96 : 97 225 : CMessageOwnershipChanged msg(GetEntityId(), old, playerID); 98 225 : GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg); 99 225 : } 100 : 101 0 : void SetOwnerQuiet(player_id_t playerID) override 102 : { 103 0 : if (playerID != m_Owner) 104 0 : m_Owner = playerID; 105 0 : } 106 : }; 107 : 108 274 : REGISTER_COMPONENT_TYPE(Ownership)