Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpMinimap.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 "ICmpMinimap.h"
22 
27 
28 #include "ps/Overlay.h"
29 
30 class CCmpMinimap : public ICmpMinimap
31 {
32 public:
33  static void ClassInit(CComponentManager& componentManager)
34  {
37  componentManager.SubscribeToMessageType(MT_MinimapPing);
38  }
39 
41 
42  // Template state:
43 
45 
46  u8 m_R, m_G, m_B; // static template state if m_UsePlayerColour false; dynamic state if true
47 
48  // Dynamic state:
49 
50  bool m_Active;
51  entity_pos_t m_X, m_Z; // cache the latest position for more efficient rendering; only valid when m_Active true
52 
53  // Not serialized (based on renderer timing):
54  // TODO: eventually ping state should be serialized and tied into simulation time, but currently lag causes too many problems
55  double m_PingEndTime;
57 
58  static std::string GetSchema()
59  {
60  return
61  "<element name='Type'>"
62  "<choice>"
63  "<value>food</value>"
64  "<value>wood</value>"
65  "<value>stone</value>"
66  "<value>metal</value>"
67  "<value>structure</value>"
68  "<value>settlement</value>"
69  "<value>unit</value>"
70  "<value>support</value>"
71  "<value>hero</value>"
72  "</choice>"
73  "</element>"
74  "<optional>"
75  "<element name='Colour'>"
76  "<attribute name='r'>"
77  "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
78  "</attribute>"
79  "<attribute name='g'>"
80  "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
81  "</attribute>"
82  "<attribute name='b'>"
83  "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
84  "</attribute>"
85  "</element>"
86  "</optional>";
87  }
88 
89  virtual void Init(const CParamNode& paramNode)
90  {
91  m_Active = true;
92  m_IsPinging = false;
93  m_PingEndTime = 0.0;
94 
95  const CParamNode& colour = paramNode.GetChild("Colour");
96  if (colour.IsOk())
97  {
98  m_UsePlayerColour = false;
99  m_R = (u8)colour.GetChild("@r").ToInt();
100  m_G = (u8)colour.GetChild("@g").ToInt();
101  m_B = (u8)colour.GetChild("@b").ToInt();
102  }
103  else
104  {
105  m_UsePlayerColour = true;
106  // Choose a bogus colour which will get replaced once we have an owner
107  m_R = 255;
108  m_G = 0;
109  m_B = 255;
110  }
111  }
112 
113  virtual void Deinit()
114  {
115  }
116 
117  template<typename S>
118  void SerializeCommon(S& serialize)
119  {
120  if (m_UsePlayerColour)
121  {
122  serialize.NumberU8_Unbounded("r", m_R);
123  serialize.NumberU8_Unbounded("g", m_G);
124  serialize.NumberU8_Unbounded("b", m_B);
125  }
126 
127  serialize.Bool("active", m_Active);
128 
129  if (m_Active)
130  {
131  serialize.NumberFixed_Unbounded("x", m_X);
132  serialize.NumberFixed_Unbounded("z", m_Z);
133  }
134  }
135 
136  virtual void Serialize(ISerializer& serialize)
137  {
138  SerializeCommon(serialize);
139  }
140 
141  virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize)
142  {
143  Init(paramNode);
144 
145  SerializeCommon(deserialize);
146  }
147 
148  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
149  {
150  switch (msg.GetType())
151  {
152  case MT_PositionChanged:
153  {
154  const CMessagePositionChanged& data = static_cast<const CMessagePositionChanged&> (msg);
155 
156  if (data.inWorld)
157  {
158  m_Active = true;
159  m_X = data.x;
160  m_Z = data.z;
161  }
162  else
163  {
164  m_Active = false;
165  }
166 
167  break;
168  }
169  case MT_OwnershipChanged:
170  {
171  if (!m_UsePlayerColour)
172  break;
173 
174  const CMessageOwnershipChanged& msgData = static_cast<const CMessageOwnershipChanged&> (msg);
175 
176  // If there's no new owner (e.g. the unit is dying) then don't try updating the colour
177  if (msgData.to == -1)
178  break;
179 
180  // Find the new player's colour
181  CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
182  if (!cmpPlayerManager)
183  break;
184  CmpPtr<ICmpPlayer> cmpPlayer(GetSimContext(), cmpPlayerManager->GetPlayerByID(msgData.to));
185  if (!cmpPlayer)
186  break;
187  CColor colour = cmpPlayer->GetColour();
188  m_R = (u8)(colour.r*255.0);
189  m_G = (u8)(colour.g*255.0);
190  m_B = (u8)(colour.b*255.0);
191  // TODO: probably should avoid using floating-point here
192 
193  break;
194  }
195  case MT_MinimapPing:
196  {
198  if (!cmpOwnership || cmpOwnership->GetOwner() != (player_id_t)GetSimContext().GetCurrentDisplayedPlayer())
199  break;
200 
201  m_Active = true;
202  m_IsPinging = true;
203  m_PingEndTime = 0.0;
204 
205  break;
206  }
207  }
208  }
209 
210  virtual bool GetRenderData(u8& r, u8& g, u8& b, entity_pos_t& x, entity_pos_t& z)
211  {
212  if (!m_Active)
213  return false;
214 
215  r = m_R;
216  g = m_G;
217  b = m_B;
218  x = m_X;
219  z = m_Z;
220  return true;
221  }
222 
223  virtual bool CheckPing(double currentTime, double pingDuration)
224  {
225  if (!m_Active || !m_IsPinging)
226  return false;
227 
228  // We're currently pinging
229  if (m_PingEndTime == 0.0)
230  m_PingEndTime = currentTime + pingDuration;
231  else if (currentTime > m_PingEndTime)
232  {
233  m_IsPinging = false;
234  m_PingEndTime = 0;
235  }
236 
237  return m_IsPinging;
238  }
239 };
240 
An entity initialisation parameter node.
Definition: ParamNode.h:112
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
#define u8
Definition: types.h:39
A simple fixed-point number class.
Definition: Fixed.h:115
static void ClassInit(CComponentManager &componentManager)
Definition: CCmpMinimap.cpp:33
virtual bool CheckPing(double currentTime, double pingDuration)
Return true if entity is actively pinging based on the current time.
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
float g
Definition: Overlay.h:57
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
bool IsOk() const
Returns true if this is a valid CParamNode, false if it represents a non-existent node...
Definition: ParamNode.cpp:193
Definition: Overlay.h:34
entity_pos_t m_X
Definition: CCmpMinimap.cpp:51
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
int ToInt() const
Parses the content of this node as an integer.
Definition: ParamNode.cpp:213
double m_PingEndTime
Definition: CCmpMinimap.cpp:55
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:24
virtual void Init(const CParamNode &paramNode)
Definition: CCmpMinimap.cpp:89
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
float b
Definition: Overlay.h:57
void SerializeCommon(S &serialize)
static std::string GetSchema()
Definition: CCmpMinimap.cpp:58
virtual entity_id_t GetPlayerByID(int32_t id)=0
virtual player_id_t GetOwner()=0
entity_pos_t m_Z
Definition: CCmpMinimap.cpp:51
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
const CSimContext & GetSimContext() const
Definition: IComponent.h:52
Sent during TurnStart.
Definition: MessageTypes.h:245
virtual void HandleMessage(const CMessage &msg, bool global)
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:55
int GetCurrentDisplayedPlayer() const
Returns the player ID that the current display is being rendered for.
Definition: SimContext.cpp:68
CEntityHandle GetSystemEntity() const
Definition: IComponent.h:50
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
bool m_IsPinging
Definition: CCmpMinimap.cpp:56
Per-unit minimap data.
Definition: ICmpMinimap.h:28
virtual void Serialize(ISerializer &serialize)
virtual void Deinit()
virtual bool GetRenderData(u8 &r, u8 &g, u8 &b, entity_pos_t &x, entity_pos_t &z)
Get the data for rendering this entity on the minimap.
bool m_UsePlayerColour
Definition: CCmpMinimap.cpp:44
float r
Definition: Overlay.h:57
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34