Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CCmpTest.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 "ICmpTest.h"
22 
25 
26 class CCmpTest1A : public ICmpTest1
27 {
28 public:
29  static void ClassInit(CComponentManager& componentManager)
30  {
31  componentManager.SubscribeToMessageType(MT_TurnStart);
32  componentManager.SubscribeToMessageType(MT_Interpolate);
33  }
34 
36 
37  int32_t m_x;
38 
39  static std::string GetSchema()
40  {
41  return "<a:component type='test'/><ref name='anything'/>";
42  }
43 
44  virtual void Init(const CParamNode& paramNode)
45  {
46  if (paramNode.GetChild("x").IsOk())
47  m_x = paramNode.GetChild("x").ToInt();
48  else
49  m_x = 11000;
50  }
51 
52  virtual void Deinit()
53  {
54  }
55 
56  virtual void Serialize(ISerializer& serialize)
57  {
58  serialize.NumberI32_Unbounded("x", m_x);
59  }
60 
61  virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
62  {
63  deserialize.NumberI32_Unbounded("x", m_x);
64  }
65 
66  virtual int GetX()
67  {
68  return m_x;
69  }
70 
71  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
72  {
73  switch (msg.GetType())
74  {
75  case MT_TurnStart:
76  m_x += 1;
77  break;
78  case MT_Interpolate:
79  m_x += 2;
80  break;
81  default:
82  m_x = 0;
83  break;
84  }
85  }
86 };
87 
89 
90 class CCmpTest1B : public ICmpTest1
91 {
92 public:
93  static void ClassInit(CComponentManager& componentManager)
94  {
95  componentManager.SubscribeToMessageType(MT_Update);
97  }
98 
100 
101  int32_t m_x;
102 
103  static std::string GetSchema()
104  {
105  return "<a:component type='test'/><empty/>";
106  }
107 
108  virtual void Init(const CParamNode&)
109  {
110  m_x = 12000;
111  }
112 
113  virtual void Deinit()
114  {
115  }
116 
117  virtual void Serialize(ISerializer& serialize)
118  {
119  serialize.NumberI32_Unbounded("x", m_x);
120  }
121 
122  virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
123  {
124  deserialize.NumberI32_Unbounded("x", m_x);
125  }
126 
127  virtual int GetX()
128  {
129  return m_x;
130  }
131 
132  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
133  {
134  switch (msg.GetType())
135  {
136  case MT_Update:
137  m_x += 10;
138  break;
139  case MT_Interpolate:
140  m_x += 20;
141  break;
142  default:
143  m_x = 0;
144  break;
145  }
146  }
147 };
148 
150 
151 class CCmpTest2A : public ICmpTest2
152 {
153 public:
154  static void ClassInit(CComponentManager& componentManager)
155  {
156  componentManager.SubscribeToMessageType(MT_TurnStart);
157  componentManager.SubscribeToMessageType(MT_Update);
158  }
159 
161 
162  int32_t m_x;
163 
164  static std::string GetSchema()
165  {
166  return "<a:component type='test'/><empty/>";
167  }
168 
169  virtual void Init(const CParamNode&)
170  {
171  m_x = 21000;
172  }
173 
174  virtual void Deinit()
175  {
176  }
177 
178  virtual void Serialize(ISerializer& serialize)
179  {
180  serialize.NumberI32_Unbounded("x", m_x);
181  }
182 
183  virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize)
184  {
185  deserialize.NumberI32_Unbounded("x", m_x);
186  }
187 
188  virtual int GetX()
189  {
190  return m_x;
191  }
192 
193  virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
194  {
195  switch (msg.GetType())
196  {
197  case MT_TurnStart:
198  m_x += 50;
199  break;
200  case MT_Update:
201  m_x += static_cast<const CMessageUpdate&> (msg).turnLength.ToInt_RoundToZero();
202  break;
203  default:
204  m_x = 0;
205  break;
206  }
207  }
208 };
209 
211 
212 ////////////////////////////////////////////////////////////////
213 
214 class CCmpTest1Scripted : public ICmpTest1
215 {
216 public:
217  DEFAULT_SCRIPT_WRAPPER(Test1Scripted)
218 
219  virtual int GetX()
220  {
221  return m_Script.Call<int> ("GetX");
222  }
223 };
224 
226 
227 ////////////////////////////////////////////////////////////////
228 
229 class CCmpTest2Scripted : public ICmpTest2
230 {
231 public:
232  DEFAULT_SCRIPT_WRAPPER(Test2Scripted)
233 
234  virtual int GetX()
235  {
236  return m_Script.Call<int> ("GetX");
237  }
238 };
239 
An entity initialisation parameter node.
Definition: ParamNode.h:112
void SubscribeToMessageType(MessageTypeId mtid)
Subscribe the current component type to the given message type.
Component for testing the simulation system.
Definition: ICmpTest.h:26
Generic per-turn update message, for things that don&#39;t care much about ordering.
Definition: MessageTypes.h:57
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:30
virtual void Serialize(ISerializer &serialize)
Definition: CCmpTest.cpp:117
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static void ClassInit(CComponentManager &componentManager)
Definition: CCmpTest.cpp:93
static std::string GetSchema()
Definition: CCmpTest.cpp:39
bool IsOk() const
Returns true if this is a valid CParamNode, false if it represents a non-existent node...
Definition: ParamNode.cpp:193
virtual void Init(const CParamNode &paramNode)
Definition: CCmpTest.cpp:44
Serialization interface; see serialization overview.
Definition: ISerializer.h:120
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
Definition: CCmpTest.cpp:183
virtual void Init(const CParamNode &)
Definition: CCmpTest.cpp:108
virtual void Deinit()
Definition: CCmpTest.cpp:174
virtual int GetX()
Definition: CCmpTest.cpp:66
static void ClassInit(CComponentManager &componentManager)
Definition: CCmpTest.cpp:154
int ToInt() const
Parses the content of this node as an integer.
Definition: ParamNode.cpp:213
virtual void HandleMessage(const CMessage &msg, bool global)
Definition: CCmpTest.cpp:132
virtual void Deinit()
Definition: CCmpTest.cpp:113
int32_t m_x
Definition: CCmpTest.cpp:37
virtual void Serialize(ISerializer &serialize)
Definition: CCmpTest.cpp:178
virtual int GetX()
Definition: CCmpTest.cpp:188
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
#define DEFAULT_SCRIPT_WRAPPER(cname)
Definition: Component.h:48
virtual void Deinit()
Definition: CCmpTest.cpp:52
virtual void HandleMessage(const CMessage &msg, bool global)
Definition: CCmpTest.cpp:193
virtual void Init(const CParamNode &)
Definition: CCmpTest.cpp:169
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:44
void SubscribeGloballyToMessageType(MessageTypeId mtid)
Subscribe the current component type to all messages of the given message type.
static void ClassInit(CComponentManager &componentManager)
Definition: CCmpTest.cpp:29
Component for testing the simulation system.
Definition: ICmpTest.h:37
virtual void Serialize(ISerializer &serialize)
Definition: CCmpTest.cpp:56
void NumberI32_Unbounded(const char *name, int32_t value)
Serialize a number.
Definition: ISerializer.h:176
#define REGISTER_COMPONENT_SCRIPT_WRAPPER(cname)
Definition: Component.h:37
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
Definition: CCmpTest.cpp:122
virtual void HandleMessage(const CMessage &msg, bool global)
Definition: CCmpTest.cpp:71
virtual void Deserialize(const CParamNode &paramNode, IDeserializer &deserialize)
Definition: CCmpTest.cpp:61
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:34
virtual void NumberI32_Unbounded(const char *name, int32_t &out)
virtual int GetX()
Definition: CCmpTest.cpp:127