Debian Codegraph @

src/gameobject.cpp

raw
1 // Copyright © 2001-2003 Rob Kaper <[email protected]>,
2 // 2001 Erik Bourget <[email protected]>
3 // 2010-2015 Sylvain Rochet <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // version 2 as published by the Free Software Foundation.
8 //
9 // This program 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 GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, see
16 // <http://www.gnu.org/licenses/>.
17
18 #include "config.h"
19
20 #include <cassert>
21
22 #include "gameobject.h"
23 #include "gameproperty.h"
24 #include "io.h"
25 #include "game.h"
26
27 GameObject::GameObject(int id, Type type, Game *game)
28 {
29 m_id = id;
30 m_type = type;
31 m_game = game;
32 }
33
34 GameObject::~GameObject()
35 {
36 while (!m_stringProperties.empty()) { delete *m_stringProperties.begin(); m_stringProperties.erase(m_stringProperties.begin()); }
37 while (!m_intProperties.empty()) { delete *m_intProperties.begin(); m_intProperties.erase(m_intProperties.begin()); }
38 while (!m_boolProperties.empty()) { delete *m_boolProperties.begin(); m_boolProperties.erase(m_boolProperties.begin()); }
39 }
40
41 int GameObject::id()
42 {
43 return m_id;
44 }
45
46 std::string GameObject::name()
47 {
48 return escapeXML(getStringProperty("name"));
49 }
50
51 enum GameObject::Type GameObject::type()
52 {
53 return m_type;
54 }
55
56 Game *GameObject::game()
57 {
58 return m_game;
59 }
60
61 void GameObject::setProperty(const std::string &key, const std::string &value, GameObject *scope)
62 {
63 GameStringProperty *stringProperty = 0;
64 for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
65 if (stringProperty->key() == key)
66 {
67 stringProperty->setValue(value);
68 return;
69 }
70
71 stringProperty = new GameStringProperty(key, value, scope ? scope : this);
72 m_stringProperties.push_back(stringProperty);
73 }
74
75 void GameObject::setProperty(const std::string &key, int value, GameObject *scope)
76 {
77 GameIntProperty *intProperty = 0;
78 for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
79 {
80 if (intProperty->key() == key)
81 {
82 intProperty->setValue(value);
83 return;
84 }
85 }
86 intProperty = new GameIntProperty(key, value, scope ? scope : this);
87 m_intProperties.push_back(intProperty);
88 }
89
90 void GameObject::setBoolProperty(const std::string &key, const bool &value, GameObject *scope)
91 {
92 GameBoolProperty *boolProperty = 0;
93 for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
94 {
95 if (boolProperty->key() == key)
96 {
97 boolProperty->setValue(value);
98 return;
99 }
100 }
101 boolProperty = new GameBoolProperty(key, value, scope ? scope : this);
102 m_boolProperties.push_back(boolProperty);
103 }
104
105 std::string GameObject::getStringProperty(const std::string &key)
106 {
107 GameStringProperty *stringProperty = 0;
108 for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
109 if (stringProperty->key() == key)
110 return stringProperty->value();
111 return std::string();
112 }
113
114 bool GameObject::hasIntProperty(const std::string &key)
115 {
116 GameIntProperty *intProperty = 0;
117 for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
118 if (intProperty->key() == key)
119 return true;
120 return false;
121 }
122
123 int GameObject::getIntProperty(const std::string &key)
124 {
125 GameIntProperty *intProperty = 0;
126 for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
127 if (intProperty->key() == key)
128 return intProperty->value();
129 return 0;
130 }
131
132 bool GameObject::getBoolProperty(const std::string &key)
133 {
134 GameBoolProperty *boolProperty = 0;
135 for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
136 if (boolProperty->key() == key)
137 return boolProperty->value();
138 return 0;
139 }
140
141 void GameObject::removeProperty(const std::string &key)
142 {
143 GameStringProperty *stringProperty = 0;
144 for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
145 if (stringProperty->key() == key)
146 {
147 m_stringProperties.erase(it);
148 delete stringProperty;
149 // FIXME: send xml update to erase stringProperty
150 return;
151 }
152
153 GameIntProperty *intProperty = 0;
154 for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
155 if (intProperty->key() == key)
156 {
157 m_intProperties.erase(it);
158 delete intProperty;
159 // FIXME: send xml update to erase intProperty
160 return;
161 }
162
163 GameBoolProperty *boolProperty = 0;
164 for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
165 if (boolProperty->key() == key)
166 {
167 m_boolProperties.erase(it);
168 delete boolProperty;
169 // FIXME: send xml update to erase boolProperty
170 return;
171 }
172 }
173
174 std::string GameObject::xmlUpdate(GameObject *target, const bool &fullUpdate)
175 {
176 std::string xmlString;
177 bool emptyUpdate = true;
178
179 GameStringProperty *stringProperty = 0;
180 for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
181 if ( (fullUpdate || stringProperty->changed()) && (stringProperty->scope()->hasInScope(target)) )
182 {
183 if (emptyUpdate)
184 {
185 xmlString = "<update object=\"" + itoa(m_type) + "\" id=\"" + itoa(m_id) + "\">";
186 emptyUpdate = false;
187 }
188 xmlString += "<string key=\"" + stringProperty->key() + "\" value=\"" + escapeXML(stringProperty->value()) + "\"/>";
189 }
190
191 GameIntProperty *intProperty = 0;
192 for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
193 if ( (fullUpdate || intProperty->changed()) && (intProperty->scope()->hasInScope(target)) )
194 {
195 if (emptyUpdate)
196 {
197 xmlString = "<update object=\"" + itoa(m_type) + "\" id=\"" + itoa(m_id) + "\">";
198 emptyUpdate = false;
199 }
200 xmlString += "<int key=\"" + intProperty->key() + "\" value=\"" + itoa(intProperty->value()) + "\"/>";
201 }
202
203 GameBoolProperty *boolProperty = 0;
204 for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
205 if ( (fullUpdate || boolProperty->changed()) && (boolProperty->scope()->hasInScope(target)) )
206 {
207 if (emptyUpdate)
208 {
209 xmlString = "<update object=\"" + itoa(m_type) + "\" id=\"" + itoa(m_id) + "\">";
210 emptyUpdate = false;
211 }
212 xmlString += "<bool key=\"" + boolProperty->key() + "\" value=\"" + itoa(boolProperty->value()) + "\"/>";
213 }
214
215 if (!emptyUpdate)
216 xmlString += "</update>";
217 return xmlString;
218 }
219
220 std::string GameObject::oldXMLUpdate(GameObject *target, const bool &fullUpdate)
221 {
222 std::string xmlString;
223 bool emptyUpdate = true;
224
225 GameStringProperty *stringProperty = 0;
226 for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
227 if ( (fullUpdate || stringProperty->changed()) && (stringProperty->scope()->hasInScope(target)) )
228 {
229 if (emptyUpdate)
230 {
231 if (m_type == GPlayer)
232 xmlString = "<playerupdate playerid=\"" + itoa(m_id) + "\"";
233 else if (m_type == GEstate)
234 xmlString = "<estateupdate estateid=\"" + itoa(m_id) + "\"";
235 else if (m_type == GEstateGroup)
236 xmlString = "<estategroupupdate groupid=\"" + itoa(m_id) + "\"";
237 else if (m_type == GGame)
238 xmlString = "<gameupdate gameid=\"" + itoa(m_id) + "\"";
239 else if (m_type == ConfigOption)
240 xmlString = "<configupdate configid=\"" + itoa(m_id) + "\" gameid=\"" + itoa(m_game->m_id) + "\"";
241 else
242 assert(0); // don't use use this on non-supported objects yet! :-p~
243
244 emptyUpdate = false;
245 }
246 xmlString += std::string(" ") + stringProperty->key() + "=\"" + escapeXML(stringProperty->value()) + "\"";
247 }
248
249 GameIntProperty *intProperty = 0;
250 for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
251 if ( (fullUpdate || intProperty->changed()) && (intProperty->scope()->hasInScope(target)) )
252 {
253 if (emptyUpdate)
254 {
255 if (m_type == GPlayer)
256 xmlString = "<playerupdate playerid=\"" + itoa(m_id) + "\"";
257 else if (m_type == GEstate)
258 xmlString = "<estateupdate estateid=\"" + itoa(m_id) + "\"";
259 else if (m_type == GEstateGroup)
260 xmlString = "<estategroupupdate groupid=\"" + itoa(m_id) + "\"";
261 else if (m_type == GGame)
262 xmlString = "<gameupdate gameid=\"" + itoa(m_id) + "\"";
263 else if (m_type == ConfigOption)
264 xmlString = "<configupdate configid=\"" + itoa(m_id) + "\" gameid=\"" + itoa(m_game->m_id) + "\"";
265 else
266 assert(0); // don't use use this on non-supported objects yet! :-p~
267
268 emptyUpdate = false;
269 }
270 xmlString += std::string(" ") + intProperty->key() + "=\"" + itoa(intProperty->value()) + "\"";
271 }
272
273 GameBoolProperty *boolProperty = 0;
274 for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
275 if ( (fullUpdate || boolProperty->changed()) && (boolProperty->scope()->hasInScope(target)) )
276 {
277 if (emptyUpdate)
278 {
279 if (m_type == GPlayer)
280 xmlString = "<playerupdate playerid=\"" + itoa(m_id) + "\"";
281 else if (m_type == GEstate)
282 xmlString = "<estateupdate estateid=\"" + itoa(m_id) + "\"";
283 else if (m_type == GEstateGroup)
284 xmlString = "<estategroupupdate groupid=\"" + itoa(m_id) + "\"";
285 else if (m_type == GGame)
286 xmlString = "<gameupdate gameid=\"" + itoa(m_id) + "\"";
287 else if (m_type == ConfigOption)
288 xmlString = "<configupdate configid=\"" + itoa(m_id) + "\" gameid=\"" + itoa(m_game->m_id) + "\"";
289 else
290 assert(0); // don't use use this on non-supported objects yet! :-p~
291
292 emptyUpdate = false;
293 }
294 xmlString += std::string(" ") + boolProperty->key() + "=\"" + itoa(boolProperty->value()) + "\"";
295 }
296
297 if (!emptyUpdate)
298 xmlString += "/>";
299 return xmlString;
300 }
301
302 void GameObject::unsetPropertiesChanged()
303 {
304 GameStringProperty *stringProperty = 0;
305 for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
306 if (stringProperty->changed())
307 stringProperty->setChanged(false);
308
309 GameIntProperty *intProperty = 0;
310 for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
311 if (intProperty->changed())
312 intProperty->setChanged(false);
313
314 GameBoolProperty *boolProperty = 0;
315 for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
316 if (boolProperty->changed())
317 boolProperty->setChanged(false);
318 }
319
320 void GameObject::addToScope(GameObject *object)
321 {
322 m_scope.push_back(object);
323 }
324
325 bool GameObject::hasInScope(GameObject *object)
326 {
327 if (object == this)
328 return true;
329
330 for(std::vector<GameObject *>::iterator it = m_scope.begin() ; it != m_scope.end() && (*it) ; ++it)
331 {
332 if ((*it) == object || (*it)->hasInScope(object))
333 return true;
334 }
335 return false;
336 }
337
338 void GameObject::removeFromScope(GameObject *object)
339 {
340 GameObject *oTmp = 0;
341 for(std::vector<GameObject *>::iterator it = m_scope.begin() ; it != m_scope.end() && (oTmp = *it) ; ++it)
342 if (oTmp == object)
343 {
344 m_scope.erase(it);
345 return;
346 }
347 }