Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: f33fc601ebaea099a6138577ca397b01dc764059 (plain)
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
/*
   core/zone.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <iomanip>

#include "auxiliary/functions.h"
#include "core/zone.h"
#include "sys/sys.h"

namespace core
{

/* ---- Static functions for the Zone registry ------------------- */

Zone::Registry Zone::zone_registry;

void Zone::add(Zone *zone)
{
	unsigned int id = 1;
	for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end() && id == (*it).second->zone_id; it++) {
		id++;
	}
	zone->zone_id = id;
	zone_registry[id] = zone;
}

void Zone::add(Zone *zone, unsigned int id)
{
	Registry::iterator it = zone_registry.find(id);
	if (it == zone_registry.end()) {
		zone->zone_id = id;
		zone_registry[id] = zone;
	}
}

Zone *Zone::find(unsigned int id)
{
	Registry::iterator it = zone_registry.find(id);
	if (it == zone_registry.end())
		return 0;
	else
		return (*it).second;
}

Zone *Zone::find(std::string const & name)
{
	for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) {
		if ((*it).second->label().compare(name) == 0) {
			return (*it).second;
		}
	}
	return 0;
}


Zone *Zone::search(std::string const & searchname)
{
	std::string strsearchkey(aux::lowercase(searchname));
	std::stringstream str(strsearchkey);

	unsigned int id;
	if (str >> id)
		return find(id);

	if (strsearchkey.size() < 3) {
		return 0;
	}

	std::string label;
	std::string name;

	for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) {
		Zone *zone = (*it).second;

		label.assign(zone->label());
		if (label.size() && (label.find(strsearchkey) != std::string::npos)) {
			return zone;
		}

		name.assign(aux::lowercase(zone->name()));
		if (name.size() && (name.find(strsearchkey) != std::string::npos)) {
			return zone;
		}
	}

	return 0;
}

void Zone::list()
{
	con_print << "  "
		<< "  id "
		<< "label                    "
		<< "name" << std::endl;
		
	for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) {
		Zone *zone = (*it).second;
		con_print << "  "
			<< "^B" << std::setw(4) << zone->id() << " "
			<< "^N" << aux::pad_right(zone->label(), 24) << " "
			<< "^N" << zone->name() << std::endl;
	}
	con_print << "^B  " << zone_registry.size() << " zones" << std::endl;
}

void Zone ::clear()
{
	for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) {
		delete(*it).second;
	}
	zone_registry.clear();

}
/* ---- class Zone ------------------------------------------------- */

Zone::Zone(std::string const & label) : 
	Label(label),
	zone_ambient_color(1.0f, 1.0f, 1.0f)
{
	zone_id = 0;	
	zone_defaultview = 0;

	btVector3 worldAabbMin(-10000, -10000, -10000);
	btVector3 worldAabbMax(10000, 10000, 10000);
	const int maxProxies = 1024;

	zone_bullet_cache = new btAxisSweep3(worldAabbMin, worldAabbMax, maxProxies);
	zone_bullet_world = new btDiscreteDynamicsWorld(Physics::dispatcher(), zone_bullet_cache, Physics::solver(), Physics::configuration());
	zone_bullet_raycaster = new btDefaultVehicleRaycaster(zone_bullet_world);

	// disable gravity
	zone_bullet_world->setGravity(btVector3(0.0f, 0.0f, 0.0f));

}

Zone::Zone(std::istream & is) : 
	Label(),
	zone_ambient_color(1.0f, 1.0f, 1.0f)
{
	zone_id = 0;
	zone_defaultview = 0;
	// client side does not setup a bullet physics environment
	zone_bullet_cache = 0;
	zone_bullet_world = 0;
	zone_bullet_raycaster = 0;
	
	receive_server_update(is);
}

Zone::~Zone()
{
	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		(*it) = 0;
	}

	zone_content.clear();
	
	if (zone_bullet_world)
		delete zone_bullet_world;

	if (zone_bullet_raycaster)
		delete zone_bullet_raycaster;
	

	if (zone_bullet_cache)
		delete zone_bullet_cache;
}

void Zone::print()
{
	con_print << "  zone id ^B" << id() << " ^Nlabel ^B" << label() << " ^Nname ^B" << name() << std::endl;
	Entity::list_header();
	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		Entity *entity = (*it);
		Entity::list(entity);
	}
	con_print << "  ^B" << zone_content.size() << " zone entities" << std::endl;
}

void Zone::add(Entity *entity)
{
	zone_content.push_back(entity);
}

void Zone::remove(Entity *entity)
{
	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		if ((*it) == entity) {
			zone_content.erase(it);
			return;
		}
	}
}

Entity *Zone::find_entity(const Entity *entity)
{
	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		if ((*it) == entity) {
			return (*it);
		}
	}

	return 0;
}

Entity *Zone::find_entity(unsigned int id)
{
	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		if ((*it)->id() == id) {
			return (*it);
		}
	}

	return 0;
}

Entity *Zone::find_entity(const std::string & label)
{
	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		if ((*it)->label().compare(label) == 0) {
			return (*it);
		}
	}

	return 0;
}

Entity *Zone::search_entity(const std::string & searchname)
{
	std::string strsearchkey(aux::lowercase(searchname));
	std::stringstream str(strsearchkey);

	unsigned int id;
	if (str >> id)
		return find_entity(id);

	if (strsearchkey.size() < 3) {
		return 0;
	}

	std::string label;
	std::string name;

	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		Entity *entity = (*it);

		label.assign(entity->label());
		if (label.size() && (label.find(strsearchkey) != std::string::npos)) {
			return entity;
		}

		name.assign(aux::lowercase(entity->name()));
		if (name.size() && (name.find(strsearchkey) != std::string::npos)) {
			return entity;
		}
	}

	return 0;
}

void Zone::serialize_server_update(std::ostream & os) const
{
	os << label() << " ";
	os << "\"" << name() << "\" ";
	os << "\"" << zone_sky << "\" ";
	os << zone_ambient_color.r << " "; 
	os << zone_ambient_color.g << " ";
	os << zone_ambient_color.b << " ";
	os << (zone_defaultview ? zone_defaultview->id() : 0);

}

void Zone::receive_server_update(std::istream &is)
{
	unsigned int id = 0;
	float r = 1.0f, g = 1.0f, b = 1.0f;
	std::string n;
	char c;
	
	// zone label
	is >> n;
	set_label(n);
	n.clear();
	
	// zone name
	while ((is.get(c)) && (c != '"'));
	while ((is.get(c)) && (c != '"'))
		n += c;
	set_name(n);
	n.clear();

	// sky name
	while ((is.get(c)) && (c != '"'));
	while ((is.get(c)) && (c != '"'))
		n += c;
	zone_sky.assign(n);
	n.clear();

	// ambient light color
	is >> r >> g >> b;
	zone_ambient_color.assign(r, g, b);
	
	// default view
	is >> id;
	zone_defaultview = Entity::find(id);
}

}