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 STRUCTURE_BLUE_FLAG:
|
---|
52 | this->addObject(OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
53 | break;
|
---|
54 | case STRUCTURE_RED_FLAG:
|
---|
55 | this->addObject(OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
56 | break;
|
---|
57 | case STRUCTURE_NONE:
|
---|
58 | break;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | 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 | 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(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, 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 OBJECT_BLUE_FLAG:
|
---|
146 | cout << "BLUE_FLAG" << endl;
|
---|
147 | break;
|
---|
148 | case OBJECT_RED_FLAG:
|
---|
149 | cout << "RED_FLAG" << endl;
|
---|
150 | break;
|
---|
151 | case 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 OBJECT_BLUE_FLAG:
|
---|
162 | cout << "BLUE_FLAG" << endl;
|
---|
163 | break;
|
---|
164 | case OBJECT_RED_FLAG:
|
---|
165 | cout << "RED_FLAG" << endl;
|
---|
166 | break;
|
---|
167 | case 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 |
|
---|
235 | istringstream iss(line);
|
---|
236 | string token;
|
---|
237 | getline(iss, token, 'x');
|
---|
238 | width = atoi(token.c_str());
|
---|
239 | getline(iss, token, 'x');
|
---|
240 | height = atoi(token.c_str());
|
---|
241 |
|
---|
242 | cout << "width: " << width << endl;
|
---|
243 | cout << "height: " << height << endl;
|
---|
244 |
|
---|
245 | m = new WorldMap(width, height);
|
---|
246 |
|
---|
247 | // read the map contents
|
---|
248 | int row = 0;
|
---|
249 | while ( file.good() )
|
---|
250 | {
|
---|
251 | getline(file, line);
|
---|
252 | if (line.size() > 0)
|
---|
253 | {
|
---|
254 | //cout << "row: " << row << endl;
|
---|
255 | //cout << "line: " << line << endl;
|
---|
256 |
|
---|
257 | istringstream iss(line);
|
---|
258 | string token;
|
---|
259 |
|
---|
260 | if (row < height) {
|
---|
261 | // load terrain
|
---|
262 |
|
---|
263 | int type;
|
---|
264 | TerrainType terrain;
|
---|
265 |
|
---|
266 | for(int x=0; x<width; x++)
|
---|
267 | {
|
---|
268 | getline(iss, token, ',');
|
---|
269 | type = atoi(token.c_str());
|
---|
270 |
|
---|
271 | //cout << "x: " << x << endl;
|
---|
272 | //cout << "token: " << token << endl;
|
---|
273 | //cout << "type: " << type << endl;
|
---|
274 |
|
---|
275 | switch(type) {
|
---|
276 | case 1:
|
---|
277 | terrain = TERRAIN_GRASS;
|
---|
278 | break;
|
---|
279 | case 2:
|
---|
280 | terrain = TERRAIN_OCEAN;
|
---|
281 | break;
|
---|
282 | case 3:
|
---|
283 | terrain = TERRAIN_ROCK;
|
---|
284 | break;
|
---|
285 | }
|
---|
286 |
|
---|
287 | m->setElement(x, row, terrain);
|
---|
288 | }
|
---|
289 | }else {
|
---|
290 | // load structure
|
---|
291 |
|
---|
292 | int x, y, type;
|
---|
293 | StructureType structure;
|
---|
294 |
|
---|
295 | getline(iss, token, ',');
|
---|
296 | //cout << "token(x): " << token << endl;
|
---|
297 | x = atoi(token.c_str());
|
---|
298 |
|
---|
299 | getline(iss, token, ',');
|
---|
300 | //cout << "token(y): " << token << endl;
|
---|
301 | y = atoi(token.c_str());
|
---|
302 |
|
---|
303 | getline(iss, token, ',');
|
---|
304 | //cout << "token(type): " << token << endl;
|
---|
305 | type = atoi(token.c_str());
|
---|
306 |
|
---|
307 | switch(type) {
|
---|
308 | case 0:
|
---|
309 | structure = STRUCTURE_NONE;
|
---|
310 | break;
|
---|
311 | case 1:
|
---|
312 | structure = STRUCTURE_BLUE_FLAG;
|
---|
313 | break;
|
---|
314 | case 2:
|
---|
315 | structure = STRUCTURE_RED_FLAG;
|
---|
316 | break;
|
---|
317 | }
|
---|
318 |
|
---|
319 | m->setStructure(x, y, structure);
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | row++;
|
---|
324 | }
|
---|
325 | file.close();
|
---|
326 | cout << filename << " closed" << endl;
|
---|
327 | }
|
---|
328 | else
|
---|
329 | cout << "Could not open the file" << endl;
|
---|
330 |
|
---|
331 | cout << "Finished file processing" << endl;
|
---|
332 |
|
---|
333 | return m;
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | /*** Functions for Object ***/
|
---|
338 |
|
---|
339 | WorldMap::Object::Object(unsigned int id, ObjectType type, int x, int y) {
|
---|
340 | this->type = type;
|
---|
341 | this->id = id;
|
---|
342 | this->pos.x = x;
|
---|
343 | this->pos.y = y;
|
---|
344 | }
|
---|
345 |
|
---|
346 | WorldMap::Object::Object(unsigned int id, ObjectType type, POSITION pos) {
|
---|
347 | this->type = type;
|
---|
348 | this->id = id;
|
---|
349 | this->pos = pos;
|
---|
350 | }
|
---|
351 |
|
---|
352 | WorldMap::Object::~Object() {
|
---|
353 | }
|
---|
354 |
|
---|
355 | void WorldMap::Object::serialize(char* buffer) {
|
---|
356 | memcpy(buffer, &this->type, 4);
|
---|
357 | memcpy(buffer+4, &this->id, 4);
|
---|
358 | memcpy(buffer+8, &this->pos.x, 4);
|
---|
359 | memcpy(buffer+12, &this->pos.y, 4);
|
---|
360 | }
|
---|
361 |
|
---|
362 | void WorldMap::Object::deserialize(char* buffer) {
|
---|
363 | memcpy(&this->type, buffer, 4);
|
---|
364 | memcpy(&this->id, buffer+4, 4);
|
---|
365 | memcpy(&this->pos.x, buffer+8, 4);
|
---|
366 | memcpy(&this->pos.y, buffer+12, 4);
|
---|
367 | }
|
---|