Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 584f75d62bc78680e073844b8e390cee10a3d558 (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
/*
   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::find_zone(std::string const & searchname)
{
	std::stringstream str(aux::lowercase(searchname));
	unsigned int id;
	Zone *zone = 0;
	if (str >> id) {
		zone = find(id);
	}
	
	if (!zone) {
		zone = find(searchname);
	}

	return zone;
}

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

void Zone::list_zone(std::string const & searchname)
{
	std::stringstream str(aux::lowercase(searchname));
	unsigned int id;
	Zone *zone = 0;
	if (str >> id) {
		zone = find(id);
	}
	
	if (!zone) {
		zone = find(searchname);
	}

	if (!zone) {
		con_print << "Unknown zone '" << searchname << "'" << std::endl;
	} else { 
		con_print << "zone " << zone->id() << " " << zone->label() << " " << zone->name() << std::endl;

		for (Content::iterator it = zone->zone_content.begin(); it != zone->zone_content.end(); it++) {
			Entity *entity = (*it);
			con_print << "  id " << std::setw(4) << entity->id()
				<< " type " << std::setw(4) << entity->type()
				<< ":" << std::setw(4) << entity->moduletype()
				<< " " << entity->label() << std::endl;
		}
		con_print << zone->zone_content.size() << " zone entities" << std::endl;
	}
}

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

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

Zone::Zone(std::string const & label) :
	zone_label(label)
{
	zone_id = 0;
	zone_name.clear();
	zone_sky.clear();
	zone_sky_texture = 0;
}

Zone::Zone(std::istream & is)
{
	zone_id = 0;
	zone_sky_texture = 0;
	receive_server_update(is);
}

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

	zone_content.clear();
}

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(Entity *entity)
{
	for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) {
		if ((*it) == entity) {
			return entity;
		}
	}

	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;
}

void Zone::serialize_server_update(std::ostream & os) const
{
	os << zone_id << " ";
	os << zone_label << " ";
	os << "\"" << zone_name << "\" ";
	os << "\"" << zone_sky << "\" ";

}

void Zone::receive_server_update(std::istream &is)
{
	is >> zone_label;
	
	std::string n;
	char c;

	while ( (is.get(c)) && (c != '"'));
	while ( (is.get(c)) && (c != '"'))
		n += c;	
	zone_name.assign(n);
	n.clear();

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

}