Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpOwnership.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010 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 
21 #include "ICmpOwnership.h"
22 
24 
25 /**
26  * Basic ICmpOwnership implementation.
27  */
29 {
30 public:
31  static void ClassInit(CComponentManager& componentManager)
32  {
33  componentManager.SubscribeToMessageType(MT_Destroy);
34  }
35 
37 
39 
40  static std::string GetSchema()
41  {
42  return
43  "<a:example/>"
44  "<a:help>Allows this entity to be owned by players.</a:help>"
45  "<empty/>";
46  }
47 
48  virtual void Init(const CParamNode& UNUSED(paramNode))
49  {
51  }
52 
53  virtual void Deinit()
54  {
55  }
56 
57  virtual void Serialize(ISerializer& serialize)
58  {
59  serialize.NumberI32_Unbounded("owner", m_Owner);
60  }
61 
62  virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
63  {
64  deserialize.NumberI32_Unbounded("owner", m_Owner);
65  }
66 
67  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
68  {
69  switch (msg.GetType())
70  {
71  case MT_Destroy:
72  {
73  // Reset the owner so this entity is e.g. removed from population counts
75  break;
76  }
77  }
78  }
79 
81  {
82  return m_Owner;
83  }
84 
85  virtual void SetOwner(player_id_t playerID)
86  {
87  if (playerID == m_Owner)
88  return;
89 
90  player_id_t old = m_Owner;
91  m_Owner = playerID;
92 
93  CMessageOwnershipChanged msg(GetEntityId(), old, playerID);
95  }
96 
97  virtual void SetOwnerQuiet(player_id_t playerID)
98  {
99  if (playerID != m_Owner)
100  m_Owner = playerID;
101  }
102 };
103 
104 REGISTER_COMPONENT_TYPE(Ownership)
An entity initialisation parameter node.
Definition: ParamNode.h:112
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
static void ClassInit(CComponentManager &componentManager)
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static std::string GetSchema()
virtual void Init(const CParamNode &paramNode)
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
Player ownership.
Definition: ICmpOwnership.h:29
player_id_t m_Owner
Basic ICmpOwnership implementation.
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
virtual void SetOwner(player_id_t playerID)
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
entity_id_t GetEntityId() const
Definition: IComponent.h:48
virtual int GetType() const =0
virtual void Serialize(ISerializer &serialize)
virtual void Deinit()
virtual void SetOwnerQuiet(player_id_t playerID)
virtual void HandleMessage(const CMessage &msg, bool global)
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
const CSimContext & GetSimContext() const
Definition: IComponent.h:52
virtual player_id_t GetOwner()
void PostMessage(entity_id_t ent, const CMessage &msg) const
Send a message, targeted at a particular entity.
void NumberI32_Unbounded(const char *name, int32_t value)
Serialize a number.
Definition: ISerializer.h:176
static const player_id_t INVALID_PLAYER
Definition: Player.h:26
CComponentManager & GetComponentManager() const
Definition: SimContext.cpp:35
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
virtual void NumberI32_Unbounded(const char *name, int32_t &out)