source: network-game/common/WorldMap.cpp@ 3ea1839

Last change on this file since 3ea1839 was 3ea1839, checked in by Dmitry Portnoy <dportnoy@…>, 10 years ago

Fix Mac compiler warnings

  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[62ee2ce]1#include "WorldMap.h"
[60b77d2]2
[384b7e0]3#include <string>
4#include <iostream>
5#include <fstream>
6#include <sstream>
[f401cac]7#include <cstdlib>
[5f868c0]8#include <cstring>
[384b7e0]9
[60b77d2]10using namespace std;
11
[62ee2ce]12WorldMap::WorldMap(int width, int height)
[60b77d2]13{
14 this->width = width;
15 this->height = height;
16
17 vctMap = new vector<vector<TerrainType>*>(width);
[05051c7]18 vctStructures = new vector<vector<StructureType>*>(width);
19 vctObjects = new vector<Object>();
[60b77d2]20
21 for (int x=0; x<width; x++) {
[a1a3bd5]22 vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
[05051c7]23 vector<StructureType>* newStructureVector = new vector<StructureType>(height);
[60b77d2]24
[a1a3bd5]25 for (int y=0; y<height; y++) {
26 (*newMapVector)[y] = TERRAIN_NONE;
[05051c7]27 (*newStructureVector)[y] = STRUCTURE_NONE;
[a1a3bd5]28 }
[60b77d2]29
[a1a3bd5]30 (*vctMap)[x] = newMapVector;
[05051c7]31 (*vctStructures)[x] = newStructureVector;
[60b77d2]32 }
33}
34
[62ee2ce]35WorldMap::~WorldMap()
[60b77d2]36{
[a1a3bd5]37 for (int x=0; x<width; x++) {
[60b77d2]38 delete (*vctMap)[x];
[05051c7]39 delete (*vctStructures)[x];
[a1a3bd5]40 }
[60b77d2]41
42 delete vctMap;
[05051c7]43 delete vctStructures;
[a1a3bd5]44 delete vctObjects;
[60b77d2]45}
46
[0678d60]47void 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)) {
[7f884ea]51 case STRUCTURE_BLUE_FLAG:
52 this->addObject(OBJECT_BLUE_FLAG, x*25+12, y*25+12);
[0678d60]53 break;
[7f884ea]54 case STRUCTURE_RED_FLAG:
55 this->addObject(OBJECT_RED_FLAG, x*25+12, y*25+12);
[0678d60]56 break;
[7f884ea]57 case STRUCTURE_NONE:
[0678d60]58 break;
59 }
60 }
61 }
62}
63
[7f884ea]64TerrainType WorldMap::getElement(int x, int y)
[62ee2ce]65{
66 return (*(*vctMap)[x])[y];
67}
68
69void WorldMap::setElement(int x, int y, TerrainType t)
[60b77d2]70{
71 (*(*vctMap)[x])[y] = t;
72}
73
[7f884ea]74StructureType WorldMap::getStructure(int x, int y)
[a1a3bd5]75{
[05051c7]76 return (*(*vctStructures)[x])[y];
[a1a3bd5]77}
78
[05051c7]79void WorldMap::setStructure(int x, int y, StructureType t)
[a1a3bd5]80{
[05051c7]81 (*(*vctStructures)[x])[y] = t;
82}
83
[e4c60ba]84POSITION WorldMap::getStructureLocation(StructureType t)
85{
86 POSITION pos;
87 pos.x = 0;
88 pos.y = 0;
89
[0678d60]90 for (unsigned int x=0; x<vctStructures->size(); x++) {
91 for (unsigned int y=0; y<(*vctStructures)[x]->size(); y++) {
[e4c60ba]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
[e487381]103vector<WorldMap::Object>* WorldMap::getObjects() {
104 return vctObjects;
[5f868c0]105}
[7511a2b]106
[05051c7]107vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
108 vector<WorldMap::Object> vctObjectsInRegion;
109
[6e66ffd]110 vector<WorldMap::Object>::iterator it;
[7511a2b]111 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
[6e66ffd]112 if (it->pos.x/25 == x && it->pos.y/25 == y)
113 vctObjectsInRegion.push_back(*it);
114 }
115
[05051c7]116 return vctObjectsInRegion;
117}
118
[6e66ffd]119// used by the server to create new objects
[7f884ea]120void WorldMap::addObject(ObjectType t, int x, int y) {
[9ba9b96]121 unsigned int id;
[7511a2b]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
[9ba9b96]130 if (it == vctObjects->end()) // if no objects with this id exist
[7511a2b]131 break;
132 }
133
134 WorldMap::Object o(id, t, x, y);
[05051c7]135 vctObjects->push_back(o);
[a1a3bd5]136}
137
[6e66ffd]138// used by the client to update object positions or create objects it has not seen before
[7f884ea]139void WorldMap::updateObject(unsigned int id, ObjectType t, int x, int y) {
[6e66ffd]140 vector<WorldMap::Object>::iterator it;
141 bool foundObject = false;
142
[233e736]143 cout << "Searching for object to update" << endl;
[7511a2b]144 switch (t) {
[7f884ea]145 case OBJECT_BLUE_FLAG:
[7511a2b]146 cout << "BLUE_FLAG" << endl;
147 break;
[7f884ea]148 case OBJECT_RED_FLAG:
[7511a2b]149 cout << "RED_FLAG" << endl;
150 break;
[7f884ea]151 case OBJECT_NONE:
[0678d60]152 cout << "OBJECY_NONE" << endl;
153 break;
[7511a2b]154 }
155
[6e66ffd]156 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
157 if (it->id == id) {
158 foundObject = true;
[7511a2b]159 cout << "Found object with id " << id << endl;
160 switch (it->type) {
[7f884ea]161 case OBJECT_BLUE_FLAG:
[7511a2b]162 cout << "BLUE_FLAG" << endl;
163 break;
[7f884ea]164 case OBJECT_RED_FLAG:
[7511a2b]165 cout << "RED_FLAG" << endl;
166 break;
[7f884ea]167 case OBJECT_NONE:
[0678d60]168 cout << "OBJECY_NONE" << endl;
169 break;
[7511a2b]170 }
[6e66ffd]171 it->pos.x = x;
172 it->pos.y = y;
173 }
174 }
175
176 if (!foundObject) {
[5f868c0]177 WorldMap::Object o(id, t, x, y);
[6e66ffd]178 vctObjects->push_back(o);
179 }
180}
181
[9ba9b96]182bool WorldMap::removeObject(unsigned int id) {
[5f868c0]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
[62ee2ce]195WorldMap* WorldMap::createDefaultMap()
[60b77d2]196{
[62ee2ce]197 WorldMap* m = new WorldMap(12l, 12);
[60b77d2]198
[62ee2ce]199 for(int x=0; x<12; x++)
[60b77d2]200 {
[62ee2ce]201 for(int y=0; y<12; y++)
[60b77d2]202 {
[62ee2ce]203 if (x ==0 || y == 0 || x == 11 || y == 11)
[60b77d2]204 m->setElement(x, y, TERRAIN_OCEAN);
205 else
206 m->setElement(x, y, TERRAIN_GRASS);
[a1a3bd5]207
[05051c7]208 m->setStructure(x, y, STRUCTURE_NONE);
[60b77d2]209 }
210 }
211
[62ee2ce]212 m->setElement(5, 5, TERRAIN_ROCK);
213
[60b77d2]214 return m;
215}
[384b7e0]216
217WorldMap* WorldMap::loadMapFromFile(string filename)
218{
[b92e6a7]219 WorldMap* m = NULL;
[384b7e0]220
[f401cac]221 ifstream file(filename.c_str());
[384b7e0]222
[233e736]223 cout << "Trying to open file: " << filename << endl;
224
[384b7e0]225 if (file.is_open())
226 {
[233e736]227 cout << filename << " opened successfully" << endl;
228
[384b7e0]229 string line;
230 int width, height;
231
232 // read the map dimensions
233 getline(file, line);
[3ea1839]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());
[384b7e0]241
242 cout << "width: " << width << endl;
243 cout << "height: " << height << endl;
244
[b650f8a]245 m = new WorldMap(width, height);
246
[384b7e0]247 // read the map contents
248 int row = 0;
249 while ( file.good() )
250 {
251 getline(file, line);
252 if (line.size() > 0)
253 {
[88258c9]254 //cout << "row: " << row << endl;
255 //cout << "line: " << line << endl;
[384b7e0]256
257 istringstream iss(line);
258 string token;
259
[a1a3bd5]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());
[b72ed16]270
[88258c9]271 //cout << "x: " << x << endl;
272 //cout << "token: " << token << endl;
273 //cout << "type: " << type << endl;
[a1a3bd5]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 {
[b81cea1]290 // load structure
[a1a3bd5]291
292 int x, y, type;
[05051c7]293 StructureType structure;
[a1a3bd5]294
295 getline(iss, token, ',');
[88258c9]296 //cout << "token(x): " << token << endl;
[a1a3bd5]297 x = atoi(token.c_str());
298
[384b7e0]299 getline(iss, token, ',');
[88258c9]300 //cout << "token(y): " << token << endl;
[a1a3bd5]301 y = atoi(token.c_str());
302
303 getline(iss, token, ',');
[88258c9]304 //cout << "token(type): " << token << endl;
[384b7e0]305 type = atoi(token.c_str());
306
307 switch(type) {
[a1a3bd5]308 case 0:
[05051c7]309 structure = STRUCTURE_NONE;
[a1a3bd5]310 break;
[384b7e0]311 case 1:
[05051c7]312 structure = STRUCTURE_BLUE_FLAG;
[384b7e0]313 break;
314 case 2:
[05051c7]315 structure = STRUCTURE_RED_FLAG;
[384b7e0]316 break;
317 }
318
[05051c7]319 m->setStructure(x, y, structure);
[384b7e0]320 }
321 }
322
323 row++;
324 }
325 file.close();
[233e736]326 cout << filename << " closed" << endl;
[384b7e0]327 }
328 else
329 cout << "Could not open the file" << endl;
330
[233e736]331 cout << "Finished file processing" << endl;
332
[384b7e0]333 return m;
334}
[05051c7]335
336
337/*** Functions for Object ***/
338
[9ba9b96]339WorldMap::Object::Object(unsigned int id, ObjectType type, int x, int y) {
[05051c7]340 this->type = type;
[6e66ffd]341 this->id = id;
342 this->pos.x = x;
343 this->pos.y = y;
[05051c7]344}
345
[9ba9b96]346WorldMap::Object::Object(unsigned int id, ObjectType type, POSITION pos) {
[05051c7]347 this->type = type;
[6e66ffd]348 this->id = id;
349 this->pos = pos;
[05051c7]350}
351
352WorldMap::Object::~Object() {
353}
[5f868c0]354
355void 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
362void 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}
Note: See TracBrowser for help on using the repository browser.