1 | #include "WorldMap.h"
|
---|
2 |
|
---|
3 | #include <string>
|
---|
4 | #include <iostream>
|
---|
5 | #include <fstream>
|
---|
6 | #include <sstream>
|
---|
7 | #include <cstdlib>
|
---|
8 | #include <cstring>
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | WorldMap::WorldMap(int width, int height)
|
---|
13 | {
|
---|
14 | this->width = width;
|
---|
15 | this->height = height;
|
---|
16 |
|
---|
17 | vctMap = new vector<vector<TerrainType>*>(width);
|
---|
18 | vctStructures = new vector<vector<StructureType>*>(width);
|
---|
19 | vctObjects = new vector<Object>();
|
---|
20 |
|
---|
21 | for (int x=0; x<width; x++) {
|
---|
22 | vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
|
---|
23 | vector<StructureType>* newStructureVector = new vector<StructureType>(height);
|
---|
24 |
|
---|
25 | for (int y=0; y<height; y++) {
|
---|
26 | (*newMapVector)[y] = TERRAIN_NONE;
|
---|
27 | (*newStructureVector)[y] = STRUCTURE_NONE;
|
---|
28 | }
|
---|
29 |
|
---|
30 | (*vctMap)[x] = newMapVector;
|
---|
31 | (*vctStructures)[x] = newStructureVector;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | WorldMap::~WorldMap()
|
---|
36 | {
|
---|
37 | for (int x=0; x<width; x++) {
|
---|
38 | delete (*vctMap)[x];
|
---|
39 | delete (*vctStructures)[x];
|
---|
40 | }
|
---|
41 |
|
---|
42 | delete vctMap;
|
---|
43 | delete vctStructures;
|
---|
44 | delete vctObjects;
|
---|
45 | }
|
---|
46 |
|
---|
47 | void WorldMap::createObjectsFromStructures() {
|
---|
48 | for (int y=0; y<this->height; y++) {
|
---|
49 | for (int x=0; x<this->width; x++) {
|
---|
50 | switch (this->getStructure(x, y)) {
|
---|
51 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
52 | this->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
53 | break;
|
---|
54 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
55 | this->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
56 | break;
|
---|
57 | case WorldMap::STRUCTURE_NONE:
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | WorldMap::TerrainType WorldMap::getElement(int x, int y)
|
---|
65 | {
|
---|
66 | return (*(*vctMap)[x])[y];
|
---|
67 | }
|
---|
68 |
|
---|
69 | void WorldMap::setElement(int x, int y, TerrainType t)
|
---|
70 | {
|
---|
71 | (*(*vctMap)[x])[y] = t;
|
---|
72 | }
|
---|
73 |
|
---|
74 | WorldMap::StructureType WorldMap::getStructure(int x, int y)
|
---|
75 | {
|
---|
76 | return (*(*vctStructures)[x])[y];
|
---|
77 | }
|
---|
78 |
|
---|
79 | void WorldMap::setStructure(int x, int y, StructureType t)
|
---|
80 | {
|
---|
81 | (*(*vctStructures)[x])[y] = t;
|
---|
82 | }
|
---|
83 |
|
---|
84 | POSITION WorldMap::getStructureLocation(StructureType t)
|
---|
85 | {
|
---|
86 | POSITION pos;
|
---|
87 | pos.x = 0;
|
---|
88 | pos.y = 0;
|
---|
89 |
|
---|
90 | for (unsigned int x=0; x<vctStructures->size(); x++) {
|
---|
91 | for (unsigned int y=0; y<(*vctStructures)[x]->size(); y++) {
|
---|
92 | if ((*(*vctStructures)[x])[y] == t) {
|
---|
93 | pos.x = x;
|
---|
94 | pos.y = y;
|
---|
95 | return pos;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | return pos;
|
---|
101 | }
|
---|
102 |
|
---|
103 | vector<WorldMap::Object>* WorldMap::getObjects() {
|
---|
104 | return vctObjects;
|
---|
105 | }
|
---|
106 |
|
---|
107 | vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
|
---|
108 | vector<WorldMap::Object> vctObjectsInRegion;
|
---|
109 |
|
---|
110 | vector<WorldMap::Object>::iterator it;
|
---|
111 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
112 | if (it->pos.x/25 == x && it->pos.y/25 == y)
|
---|
113 | vctObjectsInRegion.push_back(*it);
|
---|
114 | }
|
---|
115 |
|
---|
116 | return vctObjectsInRegion;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // used by the server to create new objects
|
---|
120 | void WorldMap::addObject(WorldMap::ObjectType t, int x, int y) {
|
---|
121 | unsigned int id;
|
---|
122 | vector<WorldMap::Object>::iterator it;
|
---|
123 |
|
---|
124 | for (id = 0; id < vctObjects->size(); id++) {
|
---|
125 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
126 | if (id == it->id)
|
---|
127 | break;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (it == vctObjects->end()) // if no objects with this id exist
|
---|
131 | break;
|
---|
132 | }
|
---|
133 |
|
---|
134 | WorldMap::Object o(id, t, x, y);
|
---|
135 | vctObjects->push_back(o);
|
---|
136 | }
|
---|
137 |
|
---|
138 | // used by the client to update object positions or create objects it has not seen before
|
---|
139 | void WorldMap::updateObject(unsigned int id, WorldMap::ObjectType t, int x, int y) {
|
---|
140 | vector<WorldMap::Object>::iterator it;
|
---|
141 | bool foundObject = false;
|
---|
142 |
|
---|
143 | cout << "Searching for object to update" << endl;
|
---|
144 | switch (t) {
|
---|
145 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
146 | cout << "BLUE_FLAG" << endl;
|
---|
147 | break;
|
---|
148 | case WorldMap::OBJECT_RED_FLAG:
|
---|
149 | cout << "RED_FLAG" << endl;
|
---|
150 | break;
|
---|
151 | case WorldMap::OBJECT_NONE:
|
---|
152 | cout << "OBJECY_NONE" << endl;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 |
|
---|
156 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
157 | if (it->id == id) {
|
---|
158 | foundObject = true;
|
---|
159 | cout << "Found object with id " << id << endl;
|
---|
160 | switch (it->type) {
|
---|
161 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
162 | cout << "BLUE_FLAG" << endl;
|
---|
163 | break;
|
---|
164 | case WorldMap::OBJECT_RED_FLAG:
|
---|
165 | cout << "RED_FLAG" << endl;
|
---|
166 | break;
|
---|
167 | case WorldMap::OBJECT_NONE:
|
---|
168 | cout << "OBJECY_NONE" << endl;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | it->pos.x = x;
|
---|
172 | it->pos.y = y;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (!foundObject) {
|
---|
177 | WorldMap::Object o(id, t, x, y);
|
---|
178 | vctObjects->push_back(o);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | bool WorldMap::removeObject(unsigned int id) {
|
---|
183 | vector<WorldMap::Object>::iterator it;
|
---|
184 |
|
---|
185 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
186 | if (it->id == id) {
|
---|
187 | vctObjects->erase(it);
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | return false; // no object with that id was found
|
---|
193 | }
|
---|
194 |
|
---|
195 | WorldMap* WorldMap::createDefaultMap()
|
---|
196 | {
|
---|
197 | WorldMap* m = new WorldMap(12l, 12);
|
---|
198 |
|
---|
199 | for(int x=0; x<12; x++)
|
---|
200 | {
|
---|
201 | for(int y=0; y<12; y++)
|
---|
202 | {
|
---|
203 | if (x ==0 || y == 0 || x == 11 || y == 11)
|
---|
204 | m->setElement(x, y, TERRAIN_OCEAN);
|
---|
205 | else
|
---|
206 | m->setElement(x, y, TERRAIN_GRASS);
|
---|
207 |
|
---|
208 | m->setStructure(x, y, STRUCTURE_NONE);
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | m->setElement(5, 5, TERRAIN_ROCK);
|
---|
213 |
|
---|
214 | return m;
|
---|
215 | }
|
---|
216 |
|
---|
217 | WorldMap* WorldMap::loadMapFromFile(string filename)
|
---|
218 | {
|
---|
219 | WorldMap* m = NULL;
|
---|
220 |
|
---|
221 | ifstream file(filename.c_str());
|
---|
222 |
|
---|
223 | cout << "Trying to open file: " << filename << endl;
|
---|
224 |
|
---|
225 | if (file.is_open())
|
---|
226 | {
|
---|
227 | cout << filename << " opened successfully" << endl;
|
---|
228 |
|
---|
229 | string line;
|
---|
230 | int width, height;
|
---|
231 |
|
---|
232 | // read the map dimensions
|
---|
233 | getline(file, line);
|
---|
234 | if (line.size() > 0)
|
---|
235 | {
|
---|
236 | istringstream iss(line);
|
---|
237 | string token;
|
---|
238 | getline(iss, token, 'x');
|
---|
239 | width = atoi(token.c_str());
|
---|
240 | getline(iss, token, 'x');
|
---|
241 | height = atoi(token.c_str());
|
---|
242 | }
|
---|
243 |
|
---|
244 | cout << "width: " << width << endl;
|
---|
245 | cout << "height: " << height << endl;
|
---|
246 |
|
---|
247 | m = new WorldMap(width, height);
|
---|
248 |
|
---|
249 | // read the map contents
|
---|
250 | int row = 0;
|
---|
251 | while ( file.good() )
|
---|
252 | {
|
---|
253 | getline(file, line);
|
---|
254 | if (line.size() > 0)
|
---|
255 | {
|
---|
256 | //cout << "row: " << row << endl;
|
---|
257 | //cout << "line: " << line << endl;
|
---|
258 |
|
---|
259 | istringstream iss(line);
|
---|
260 | string token;
|
---|
261 |
|
---|
262 | if (row < height) {
|
---|
263 | // load terrain
|
---|
264 |
|
---|
265 | int type;
|
---|
266 | TerrainType terrain;
|
---|
267 |
|
---|
268 | for(int x=0; x<width; x++)
|
---|
269 | {
|
---|
270 | getline(iss, token, ',');
|
---|
271 | type = atoi(token.c_str());
|
---|
272 |
|
---|
273 | //cout << "x: " << x << endl;
|
---|
274 | //cout << "token: " << token << endl;
|
---|
275 | //cout << "type: " << type << endl;
|
---|
276 |
|
---|
277 | switch(type) {
|
---|
278 | case 1:
|
---|
279 | terrain = TERRAIN_GRASS;
|
---|
280 | break;
|
---|
281 | case 2:
|
---|
282 | terrain = TERRAIN_OCEAN;
|
---|
283 | break;
|
---|
284 | case 3:
|
---|
285 | terrain = TERRAIN_ROCK;
|
---|
286 | break;
|
---|
287 | }
|
---|
288 |
|
---|
289 | m->setElement(x, row, terrain);
|
---|
290 | }
|
---|
291 | }else {
|
---|
292 | // load structure
|
---|
293 |
|
---|
294 | int x, y, type;
|
---|
295 | StructureType structure;
|
---|
296 |
|
---|
297 | getline(iss, token, ',');
|
---|
298 | //cout << "token(x): " << token << endl;
|
---|
299 | x = atoi(token.c_str());
|
---|
300 |
|
---|
301 | getline(iss, token, ',');
|
---|
302 | //cout << "token(y): " << token << endl;
|
---|
303 | y = atoi(token.c_str());
|
---|
304 |
|
---|
305 | getline(iss, token, ',');
|
---|
306 | //cout << "token(type): " << token << endl;
|
---|
307 | type = atoi(token.c_str());
|
---|
308 |
|
---|
309 | switch(type) {
|
---|
310 | case 0:
|
---|
311 | structure = STRUCTURE_NONE;
|
---|
312 | break;
|
---|
313 | case 1:
|
---|
314 | structure = STRUCTURE_BLUE_FLAG;
|
---|
315 | break;
|
---|
316 | case 2:
|
---|
317 | structure = STRUCTURE_RED_FLAG;
|
---|
318 | break;
|
---|
319 | }
|
---|
320 |
|
---|
321 | m->setStructure(x, y, structure);
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | row++;
|
---|
326 | }
|
---|
327 | file.close();
|
---|
328 | cout << filename << " closed" << endl;
|
---|
329 | }
|
---|
330 | else
|
---|
331 | cout << "Could not open the file" << endl;
|
---|
332 |
|
---|
333 | cout << "Finished file processing" << endl;
|
---|
334 |
|
---|
335 | return m;
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | /*** Functions for Object ***/
|
---|
340 |
|
---|
341 | WorldMap::Object::Object(unsigned int id, ObjectType type, int x, int y) {
|
---|
342 | this->type = type;
|
---|
343 | this->id = id;
|
---|
344 | this->pos.x = x;
|
---|
345 | this->pos.y = y;
|
---|
346 | }
|
---|
347 |
|
---|
348 | WorldMap::Object::Object(unsigned int id, ObjectType type, POSITION pos) {
|
---|
349 | this->type = type;
|
---|
350 | this->id = id;
|
---|
351 | this->pos = pos;
|
---|
352 | }
|
---|
353 |
|
---|
354 | WorldMap::Object::~Object() {
|
---|
355 | }
|
---|
356 |
|
---|
357 | void WorldMap::Object::serialize(char* buffer) {
|
---|
358 | memcpy(buffer, &this->type, 4);
|
---|
359 | memcpy(buffer+4, &this->id, 4);
|
---|
360 | memcpy(buffer+8, &this->pos.x, 4);
|
---|
361 | memcpy(buffer+12, &this->pos.y, 4);
|
---|
362 | }
|
---|
363 |
|
---|
364 | void WorldMap::Object::deserialize(char* buffer) {
|
---|
365 | memcpy(&this->type, buffer, 4);
|
---|
366 | memcpy(&this->id, buffer+4, 4);
|
---|
367 | memcpy(&this->pos.x, buffer+8, 4);
|
---|
368 | memcpy(&this->pos.y, buffer+12, 4);
|
---|
369 | }
|
---|