[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] | 10 | using namespace std;
|
---|
| 11 |
|
---|
[62ee2ce] | 12 | WorldMap::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] | 35 | WorldMap::~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] | 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 |
|
---|
[62ee2ce] | 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)
|
---|
[60b77d2] | 70 | {
|
---|
| 71 | (*(*vctMap)[x])[y] = t;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[05051c7] | 74 | WorldMap::StructureType WorldMap::getStructure(int x, int y)
|
---|
[a1a3bd5] | 75 | {
|
---|
[05051c7] | 76 | return (*(*vctStructures)[x])[y];
|
---|
[a1a3bd5] | 77 | }
|
---|
| 78 |
|
---|
[05051c7] | 79 | void WorldMap::setStructure(int x, int y, StructureType t)
|
---|
[a1a3bd5] | 80 | {
|
---|
[05051c7] | 81 | (*(*vctStructures)[x])[y] = t;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[e4c60ba] | 84 | POSITION 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] | 103 | vector<WorldMap::Object>* WorldMap::getObjects() {
|
---|
| 104 | return vctObjects;
|
---|
[5f868c0] | 105 | }
|
---|
[7511a2b] | 106 |
|
---|
[05051c7] | 107 | vector<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
|
---|
| 120 | void WorldMap::addObject(WorldMap::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
|
---|
[9ba9b96] | 139 | void WorldMap::updateObject(unsigned int id, WorldMap::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) {
|
---|
| 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;
|
---|
[0678d60] | 151 | case WorldMap::OBJECT_NONE:
|
---|
| 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) {
|
---|
| 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;
|
---|
[0678d60] | 167 | case WorldMap::OBJECT_NONE:
|
---|
| 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] | 182 | bool 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] | 195 | WorldMap* 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 |
|
---|
| 217 | WorldMap* 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);
|
---|
| 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 |
|
---|
[b650f8a] | 247 | m = new WorldMap(width, height);
|
---|
| 248 |
|
---|
[384b7e0] | 249 | // read the map contents
|
---|
| 250 | int row = 0;
|
---|
| 251 | while ( file.good() )
|
---|
| 252 | {
|
---|
| 253 | getline(file, line);
|
---|
| 254 | if (line.size() > 0)
|
---|
| 255 | {
|
---|
[88258c9] | 256 | //cout << "row: " << row << endl;
|
---|
| 257 | //cout << "line: " << line << endl;
|
---|
[384b7e0] | 258 |
|
---|
| 259 | istringstream iss(line);
|
---|
| 260 | string token;
|
---|
| 261 |
|
---|
[a1a3bd5] | 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());
|
---|
[b72ed16] | 272 |
|
---|
[88258c9] | 273 | //cout << "x: " << x << endl;
|
---|
| 274 | //cout << "token: " << token << endl;
|
---|
| 275 | //cout << "type: " << type << endl;
|
---|
[a1a3bd5] | 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 {
|
---|
[b81cea1] | 292 | // load structure
|
---|
[a1a3bd5] | 293 |
|
---|
| 294 | int x, y, type;
|
---|
[05051c7] | 295 | StructureType structure;
|
---|
[a1a3bd5] | 296 |
|
---|
| 297 | getline(iss, token, ',');
|
---|
[88258c9] | 298 | //cout << "token(x): " << token << endl;
|
---|
[a1a3bd5] | 299 | x = atoi(token.c_str());
|
---|
| 300 |
|
---|
[384b7e0] | 301 | getline(iss, token, ',');
|
---|
[88258c9] | 302 | //cout << "token(y): " << token << endl;
|
---|
[a1a3bd5] | 303 | y = atoi(token.c_str());
|
---|
| 304 |
|
---|
| 305 | getline(iss, token, ',');
|
---|
[88258c9] | 306 | //cout << "token(type): " << token << endl;
|
---|
[384b7e0] | 307 | type = atoi(token.c_str());
|
---|
| 308 |
|
---|
| 309 | switch(type) {
|
---|
[a1a3bd5] | 310 | case 0:
|
---|
[05051c7] | 311 | structure = STRUCTURE_NONE;
|
---|
[a1a3bd5] | 312 | break;
|
---|
[384b7e0] | 313 | case 1:
|
---|
[05051c7] | 314 | structure = STRUCTURE_BLUE_FLAG;
|
---|
[384b7e0] | 315 | break;
|
---|
| 316 | case 2:
|
---|
[05051c7] | 317 | structure = STRUCTURE_RED_FLAG;
|
---|
[384b7e0] | 318 | break;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[05051c7] | 321 | m->setStructure(x, y, structure);
|
---|
[384b7e0] | 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | row++;
|
---|
| 326 | }
|
---|
| 327 | file.close();
|
---|
[233e736] | 328 | cout << filename << " closed" << endl;
|
---|
[384b7e0] | 329 | }
|
---|
| 330 | else
|
---|
| 331 | cout << "Could not open the file" << endl;
|
---|
| 332 |
|
---|
[233e736] | 333 | cout << "Finished file processing" << endl;
|
---|
| 334 |
|
---|
[384b7e0] | 335 | return m;
|
---|
| 336 | }
|
---|
[05051c7] | 337 |
|
---|
| 338 |
|
---|
| 339 | /*** Functions for Object ***/
|
---|
| 340 |
|
---|
[9ba9b96] | 341 | WorldMap::Object::Object(unsigned int id, ObjectType type, int x, int y) {
|
---|
[05051c7] | 342 | this->type = type;
|
---|
[6e66ffd] | 343 | this->id = id;
|
---|
| 344 | this->pos.x = x;
|
---|
| 345 | this->pos.y = y;
|
---|
[05051c7] | 346 | }
|
---|
| 347 |
|
---|
[9ba9b96] | 348 | WorldMap::Object::Object(unsigned int id, ObjectType type, POSITION pos) {
|
---|
[05051c7] | 349 | this->type = type;
|
---|
[6e66ffd] | 350 | this->id = id;
|
---|
| 351 | this->pos = pos;
|
---|
[05051c7] | 352 | }
|
---|
| 353 |
|
---|
| 354 | WorldMap::Object::~Object() {
|
---|
| 355 | }
|
---|
[5f868c0] | 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 | }
|
---|