Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpVision.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 "ICmpVision.h"
22 
27 
28 class CCmpVision : public ICmpVision
29 {
30 public:
31  static void ClassInit(CComponentManager& componentManager)
32  {
34  }
35 
37 
38  // Template state:
39 
43 
44  static std::string GetSchema()
45  {
46  return
47  "<element name='Range'>"
48  "<data type='nonNegativeInteger'/>"
49  "</element>"
50  "<element name='RetainInFog'>"
51  "<data type='boolean'/>"
52  "</element>"
53  "<element name='AlwaysVisible'>"
54  "<data type='boolean'/>"
55  "</element>";
56  }
57 
58  virtual void Init(const CParamNode& paramNode)
59  {
60  m_BaseRange = m_Range = paramNode.GetChild("Range").ToFixed();
61  m_RetainInFog = paramNode.GetChild("RetainInFog").ToBool();
62  m_AlwaysVisible = paramNode.GetChild("AlwaysVisible").ToBool();
63  }
64 
65  virtual void Deinit()
66  {
67  }
68 
69  virtual void Serialize(ISerializer& UNUSED(serialize))
70  {
71  // No dynamic state to serialize
72  }
73 
74  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
75  {
76  Init(paramNode);
77  }
78 
79  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
80  {
81  switch (msg.GetType())
82  {
84  {
85  const CMessageTechnologyModification& msgData = static_cast<const CMessageTechnologyModification&> (msg);
86  if (msgData.component == L"Vision")
87  {
88  CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
89  if (cmpOwnership)
90  {
91  player_id_t owner = cmpOwnership->GetOwner();
92  if (owner != INVALID_PLAYER && owner == msgData.player)
93  {
94  CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
95  entity_id_t playerEnt = cmpPlayerManager->GetPlayerByID(owner);
96  CmpPtr<ICmpTechnologyManager> cmpTechnologyManager(GetSimContext(), playerEnt);
97  if (playerEnt != INVALID_ENTITY && cmpTechnologyManager)
98  {
99  entity_pos_t newRange = cmpTechnologyManager->ApplyModifications(L"Vision/Range", m_BaseRange, GetEntityId());
100  if (newRange != m_Range)
101  {
102  // Update our vision range and broadcast message
103  entity_pos_t oldRange = m_Range;
104  m_Range = newRange;
105  CMessageVisionRangeChanged msg(GetEntityId(), oldRange, newRange);
107  }
108  }
109  }
110  }
111  }
112  break;
113  }
114  }
115  }
116 
118  {
119  return m_Range;
120  }
121 
122  virtual bool GetRetainInFog()
123  {
124  return m_RetainInFog;
125  }
126 
127  virtual bool GetAlwaysVisible()
128  {
129  return m_AlwaysVisible;
130  }
131 };
132 
An entity initialisation parameter node.
Definition: ParamNode.h:112
A simple fixed-point number class.
Definition: Fixed.h:115
entity_pos_t m_BaseRange
Definition: CCmpVision.cpp:40
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
bool ToBool() const
Parses the content of this node as a boolean (&quot;true&quot; == true, anything else == false) ...
Definition: ParamNode.cpp:236
Sent by CCmpVision when an entity&#39;s vision range changes.
Definition: MessageTypes.h:388
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
static void ClassInit(CComponentManager &componentManager)
Definition: CCmpVision.cpp:31
virtual void Serialize(ISerializer &serialize)
Definition: CCmpVision.cpp:69
virtual bool GetAlwaysVisible()
Definition: CCmpVision.cpp:127
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
CEntityHandle GetEntityHandle() const
Definition: IComponent.h:45
fixed ToFixed() const
Parses the content of this node as a fixed-point number.
Definition: ParamNode.cpp:222
entity_id_t GetEntityId() const
Definition: IComponent.h:48
virtual int GetType() const =0
const CParamNode & GetChild(const char *name) const
Returns the (unique) child node with the given name, or a node with IsOk() == false if there is none...
Definition: ParamNode.cpp:185
virtual fixed ApplyModifications(std::wstring valueName, fixed currentValue, entity_id_t entity)=0
virtual void Init(const CParamNode &paramNode)
Definition: CCmpVision.cpp:58
virtual entity_id_t GetPlayerByID(int32_t id)=0
bool m_AlwaysVisible
Definition: CCmpVision.cpp:42
virtual player_id_t GetOwner()=0
Vision (LOS etc) interface (typically implemented by a scripted component).
Definition: ICmpVision.h:28
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
const CSimContext & GetSimContext() const
Definition: IComponent.h:52
void SubscribeGloballyToMessageType(MessageTypeId mtid)
Subscribe the current component type to all messages of the given message type.
static std::string GetSchema()
Definition: CCmpVision.cpp:44
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:55
CEntityHandle GetSystemEntity() const
Definition: IComponent.h:50
virtual bool GetRetainInFog()
Definition: CCmpVision.cpp:122
virtual entity_pos_t GetRange()
Definition: CCmpVision.cpp:117
Sent by technology manager when a technology is researched that modifies a component.
Definition: MessageTypes.h:371
void BroadcastMessage(const CMessage &msg) const
Send a message, not targeted at any particular entity.
virtual void HandleMessage(const CMessage &msg, bool global)
Definition: CCmpVision.cpp:79
const entity_id_t INVALID_ENTITY
Invalid entity ID.
Definition: Entity.h:36
u32 entity_id_t
Entity ID type.
Definition: Entity.h:24
entity_pos_t m_Range
Definition: CCmpVision.cpp:40
static const player_id_t INVALID_PLAYER
Definition: Player.h:26
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
Definition: CCmpVision.cpp:74
CComponentManager & GetComponentManager() const
Definition: SimContext.cpp:35
bool m_RetainInFog
Definition: CCmpVision.cpp:41
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
virtual void Deinit()
Definition: CCmpVision.cpp:65