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