File: gameobject.cpp

package info (click to toggle)
monopd 0.9.3-4.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 788 kB
  • ctags: 575
  • sloc: cpp: 5,666; sh: 712; makefile: 27
file content (344 lines) | stat: -rw-r--r-- 11,894 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright (c) 2001-2003 Rob Kaper <cap@capsi.com>,
//               2001 Erik Bourget <ebourg@cs.mcgill.ca>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

#include <cassert>

#include "gameobject.h"
#include "gameproperty.h"
#include "io.h"

GameObject::GameObject(int id, Type type, Game *game)
{
	m_id = id;
	m_type = type;
	m_game = game;
}

GameObject::~GameObject()
{
	while (!m_stringProperties.empty()) { delete *m_stringProperties.begin(); m_stringProperties.erase(m_stringProperties.begin()); }
	while (!m_intProperties.empty()) { delete *m_intProperties.begin(); m_intProperties.erase(m_intProperties.begin()); }
	while (!m_boolProperties.empty()) { delete *m_boolProperties.begin(); m_boolProperties.erase(m_boolProperties.begin()); }
}

int GameObject::id()
{
	return m_id;
}

std::string GameObject::name()
{
	return escapeXML(getStringProperty("name"));
}

const enum GameObject::Type GameObject::type()
{
	return m_type;
}

Game *GameObject::game()
{
	return m_game;
}

void GameObject::setProperty(const std::string &key, const std::string &value, GameObject *scope)
{
	GameStringProperty *stringProperty = 0;
	for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
		if (stringProperty->key() == key)
		{
			stringProperty->setValue(value);
			return;
		}

	stringProperty = new GameStringProperty(key, value, scope ? scope : this);
	m_stringProperties.push_back(stringProperty);
}

void GameObject::setProperty(const std::string &key, int value, GameObject *scope)
{
	GameIntProperty *intProperty = 0;
	for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
	{
		if (intProperty->key() == key)
		{
			intProperty->setValue(value);
			return;
		}
	}
	intProperty = new GameIntProperty(key, value, scope ? scope : this);
	m_intProperties.push_back(intProperty);
}

void GameObject::setBoolProperty(const std::string &key, const bool &value, GameObject *scope)
{
	GameBoolProperty *boolProperty = 0;
	for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
	{
		if (boolProperty->key() == key)
		{
			boolProperty->setValue(value);
			return;
		}
	}
	boolProperty = new GameBoolProperty(key, value, scope ? scope : this);
	m_boolProperties.push_back(boolProperty);
}

std::string GameObject::getStringProperty(const std::string &key)
{
	GameStringProperty *stringProperty = 0;
	for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
		if (stringProperty->key() == key)
			return stringProperty->value();
	return std::string();
}

bool GameObject::hasIntProperty(const std::string &key)
{
	GameIntProperty *intProperty = 0;
	for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
		if (intProperty->key() == key)
			return true;
	return false;
}

int GameObject::getIntProperty(const std::string &key)
{
	GameIntProperty *intProperty = 0;
	for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
		if (intProperty->key() == key)
			return intProperty->value();
	return 0;
}

bool GameObject::getBoolProperty(const std::string &key)
{
	GameBoolProperty *boolProperty = 0;
	for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
		if (boolProperty->key() == key)
			return boolProperty->value();
	return 0;
}

void GameObject::removeProperty(const std::string &key)
{
	GameStringProperty *stringProperty = 0;
	for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
		if (stringProperty->key() == key)
		{
			m_stringProperties.erase(it);
			delete stringProperty;
			// FIXME: send xml update to erase stringProperty
			return;
		}

	GameIntProperty *intProperty = 0;
	for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
		if (intProperty->key() == key)
		{
			m_intProperties.erase(it);
			delete intProperty;
			// FIXME: send xml update to erase intProperty
			return;
		}

	GameBoolProperty *boolProperty = 0;
	for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
		if (boolProperty->key() == key)
		{
			m_boolProperties.erase(it);
			delete boolProperty;
			// FIXME: send xml update to erase boolProperty
			return;
		}
}

std::string GameObject::xmlUpdate(GameObject *target, const bool &fullUpdate)
{
	std::string xmlString;
	bool emptyUpdate = true;

	GameStringProperty *stringProperty = 0;
	for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
		if ( (fullUpdate || stringProperty->changed()) && (stringProperty->scope()->hasInScope(target)) )
		{
			if (emptyUpdate)
			{
				xmlString = "<update object=\"" + itoa(m_type) + "\" id=\"" + itoa(m_id) + "\">";
				emptyUpdate = false;
			}
			xmlString += "<string key=\"" + stringProperty->key() + "\" value=\"" + escapeXML(stringProperty->value()) + "\"/>";
		}

	GameIntProperty *intProperty = 0;
	for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
		if ( (fullUpdate || intProperty->changed()) && (intProperty->scope()->hasInScope(target)) )
		{
			if (emptyUpdate)
			{
				xmlString = "<update object=\"" + itoa(m_type) + "\" id=\"" + itoa(m_id) + "\">";
				emptyUpdate = false;
			}
			xmlString += "<int key=\"" + intProperty->key() + "\" value=\"" + itoa(intProperty->value()) + "\"/>";
		}

	GameBoolProperty *boolProperty = 0;
	for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
		if ( (fullUpdate || boolProperty->changed()) && (boolProperty->scope()->hasInScope(target)) )
		{
			if (emptyUpdate)
			{
				xmlString = "<update object=\"" + itoa(m_type) + "\" id=\"" + itoa(m_id) + "\">";
				emptyUpdate = false;
			}
			xmlString += "<bool key=\"" + boolProperty->key() + "\" value=\"" + itoa(boolProperty->value()) + "\"/>";
		}

	if (!emptyUpdate)
		xmlString += "</update>";
	return xmlString;
}

std::string GameObject::oldXMLUpdate(GameObject *target, const bool &fullUpdate)
{
	std::string xmlString;
	bool emptyUpdate = true;

	GameStringProperty *stringProperty = 0;
	for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
		if ( (fullUpdate || stringProperty->changed()) && (stringProperty->scope()->hasInScope(target)) )
		{
			if (emptyUpdate)
			{
				if (m_type == GPlayer)
					xmlString = "<playerupdate playerid=\"" + itoa(m_id) + "\"";
				else if (m_type == GEstate)
					xmlString = "<estateupdate estateid=\"" + itoa(m_id) + "\"";
				else if (m_type == GEstateGroup)
					xmlString = "<estategroupupdate groupid=\"" + itoa(m_id) + "\"";
				else if (m_type == GGame)
					xmlString = "<gameupdate gameid=\"" + itoa(m_id) + "\"";
				else if (m_type == ConfigOption)
					xmlString = "<configupdate configid=\"" + itoa(m_id) + "\"";
				else
					assert(0); // don't use use this on non-supported objects yet! :-p~

				emptyUpdate = false;
			}
			xmlString += std::string(" ") + stringProperty->key() + "=\"" + escapeXML(stringProperty->value()) + "\"";
		}

	GameIntProperty *intProperty = 0;
	for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
		if ( (fullUpdate || intProperty->changed()) && (intProperty->scope()->hasInScope(target)) )
		{
			if (emptyUpdate)
			{
				if (m_type == GPlayer)
					xmlString = "<playerupdate playerid=\"" + itoa(m_id) + "\"";
				else if (m_type == GEstate)
					xmlString = "<estateupdate estateid=\"" + itoa(m_id) + "\"";
				else if (m_type == GEstateGroup)
					xmlString = "<estategroupupdate groupid=\"" + itoa(m_id) + "\"";
				else if (m_type == GGame)
					xmlString = "<gameupdate gameid=\"" + itoa(m_id) + "\"";
				else if (m_type == ConfigOption)
					xmlString = "<configupdate configid=\"" + itoa(m_id) + "\"";
				else
					assert(0); // don't use use this on non-supported objects yet! :-p~

				emptyUpdate = false;
			}
			xmlString += std::string(" ") + intProperty->key() + "=\"" + itoa(intProperty->value()) + "\"";
		}

	GameBoolProperty *boolProperty = 0;
	for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
		if ( (fullUpdate || boolProperty->changed()) && (boolProperty->scope()->hasInScope(target)) )
		{
			if (emptyUpdate)
			{
				if (m_type == GPlayer)
					xmlString = "<playerupdate playerid=\"" + itoa(m_id) + "\"";
				else if (m_type == GEstate)
					xmlString = "<estateupdate estateid=\"" + itoa(m_id) + "\"";
				else if (m_type == GEstateGroup)
					xmlString = "<estategroupupdate groupid=\"" + itoa(m_id) + "\"";
				else if (m_type == GGame)
					xmlString = "<gameupdate gameid=\"" + itoa(m_id) + "\"";
				else if (m_type == ConfigOption)
					xmlString = "<configupdate configid=\"" + itoa(m_id) + "\"";
				else
					assert(0); // don't use use this on non-supported objects yet! :-p~

				emptyUpdate = false;
			}
			xmlString += std::string(" ") + boolProperty->key() + "=\"" + itoa(boolProperty->value()) + "\"";
		}

	if (!emptyUpdate)
		xmlString += "/>";
	return xmlString;
}

void GameObject::unsetPropertiesChanged()
{
	GameStringProperty *stringProperty = 0;
	for(std::vector<GameStringProperty *>::iterator it = m_stringProperties.begin() ; it != m_stringProperties.end() && (stringProperty = *it) ; ++it)
		if (stringProperty->changed())
			stringProperty->setChanged(false);

	GameIntProperty *intProperty = 0;
	for(std::vector<GameIntProperty *>::iterator it = m_intProperties.begin() ; it != m_intProperties.end() && (intProperty = *it) ; ++it)
		if (intProperty->changed())
			intProperty->setChanged(false);

	GameBoolProperty *boolProperty = 0;
	for(std::vector<GameBoolProperty *>::iterator it = m_boolProperties.begin() ; it != m_boolProperties.end() && (boolProperty = *it) ; ++it)
		if (boolProperty->changed())
			boolProperty->setChanged(false);
}

void GameObject::addToScope(GameObject *object)
{
	m_scope.push_back(object);
}

bool GameObject::hasInScope(GameObject *object)
{
	if (object == this)
		return true;

	for(std::vector<GameObject *>::iterator it = m_scope.begin() ; it != m_scope.end() && (*it) ; ++it)
	{
		if ((*it) == object || (*it)->hasInScope(object))
			return true;
	}
	return false;
}

void GameObject::removeFromScope(GameObject *object)
{
	GameObject *oTmp = 0;
	for(std::vector<GameObject *>::iterator it = m_scope.begin() ; it != m_scope.end() && (oTmp = *it) ; ++it)
		if (oTmp == object)
		{
			m_scope.erase(it);
			return;
		}
}