[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 |
|
---|
[62ee2ce] | 47 | WorldMap::TerrainType WorldMap::getElement(int x, int y)
|
---|
| 48 | {
|
---|
| 49 | return (*(*vctMap)[x])[y];
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void WorldMap::setElement(int x, int y, TerrainType t)
|
---|
[60b77d2] | 53 | {
|
---|
| 54 | (*(*vctMap)[x])[y] = t;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[05051c7] | 57 | WorldMap::StructureType WorldMap::getStructure(int x, int y)
|
---|
[a1a3bd5] | 58 | {
|
---|
[05051c7] | 59 | return (*(*vctStructures)[x])[y];
|
---|
[a1a3bd5] | 60 | }
|
---|
| 61 |
|
---|
[05051c7] | 62 | void WorldMap::setStructure(int x, int y, StructureType t)
|
---|
[a1a3bd5] | 63 | {
|
---|
[05051c7] | 64 | (*(*vctStructures)[x])[y] = t;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[e4c60ba] | 67 | POSITION WorldMap::getStructureLocation(StructureType t)
|
---|
| 68 | {
|
---|
| 69 | POSITION pos;
|
---|
| 70 | pos.x = 0;
|
---|
| 71 | pos.y = 0;
|
---|
| 72 |
|
---|
| 73 | for (int x=0; x<vctStructures->size(); x++) {
|
---|
| 74 | for (int y=0; y<(*vctStructures)[x]->size(); y++) {
|
---|
| 75 | if ((*(*vctStructures)[x])[y] == t) {
|
---|
| 76 | pos.x = x;
|
---|
| 77 | pos.y = y;
|
---|
| 78 | return pos;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return pos;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[e487381] | 86 | vector<WorldMap::Object>* WorldMap::getObjects() {
|
---|
| 87 | return vctObjects;
|
---|
[5f868c0] | 88 | }
|
---|
[7511a2b] | 89 |
|
---|
[05051c7] | 90 | vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
|
---|
| 91 | vector<WorldMap::Object> vctObjectsInRegion;
|
---|
| 92 |
|
---|
[6e66ffd] | 93 | vector<WorldMap::Object>::iterator it;
|
---|
[7511a2b] | 94 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
[6e66ffd] | 95 | if (it->pos.x/25 == x && it->pos.y/25 == y)
|
---|
| 96 | vctObjectsInRegion.push_back(*it);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[05051c7] | 99 | return vctObjectsInRegion;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[6e66ffd] | 102 | // used by the server to create new objects
|
---|
| 103 | void WorldMap::addObject(WorldMap::ObjectType t, int x, int y) {
|
---|
[7511a2b] | 104 | int id;
|
---|
| 105 | vector<WorldMap::Object>::iterator it;
|
---|
| 106 |
|
---|
| 107 | for (id = 0; id < vctObjects->size(); id++) {
|
---|
| 108 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
| 109 | if (id == it->id)
|
---|
| 110 | break;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if (it == vctObjects->end()) // if no objects with this id exists
|
---|
| 114 | break;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | WorldMap::Object o(id, t, x, y);
|
---|
[05051c7] | 118 | vctObjects->push_back(o);
|
---|
[a1a3bd5] | 119 | }
|
---|
| 120 |
|
---|
[6e66ffd] | 121 | // used by the client to update object positions or create objects it has not seen before
|
---|
| 122 | void WorldMap::updateObject(int id, WorldMap::ObjectType t, int x, int y) {
|
---|
| 123 | vector<WorldMap::Object>::iterator it;
|
---|
| 124 | bool foundObject = false;
|
---|
| 125 |
|
---|
[7511a2b] | 126 | cout << "Searching for obbject to update" << endl;
|
---|
| 127 | switch (t) {
|
---|
| 128 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
| 129 | cout << "BLUE_FLAG" << endl;
|
---|
| 130 | break;
|
---|
| 131 | case WorldMap::OBJECT_RED_FLAG:
|
---|
| 132 | cout << "RED_FLAG" << endl;
|
---|
| 133 | break;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[6e66ffd] | 136 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
| 137 | if (it->id == id) {
|
---|
| 138 | foundObject = true;
|
---|
[7511a2b] | 139 | cout << "Found object with id " << id << endl;
|
---|
| 140 | switch (it->type) {
|
---|
| 141 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
| 142 | cout << "BLUE_FLAG" << endl;
|
---|
| 143 | break;
|
---|
| 144 | case WorldMap::OBJECT_RED_FLAG:
|
---|
| 145 | cout << "RED_FLAG" << endl;
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
[6e66ffd] | 148 | it->pos.x = x;
|
---|
| 149 | it->pos.y = y;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | if (!foundObject) {
|
---|
[5f868c0] | 154 | WorldMap::Object o(id, t, x, y);
|
---|
[6e66ffd] | 155 | vctObjects->push_back(o);
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[5f868c0] | 159 | bool WorldMap::removeObject(int id) {
|
---|
| 160 | vector<WorldMap::Object>::iterator it;
|
---|
| 161 |
|
---|
| 162 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
| 163 | if (it->id == id) {
|
---|
| 164 | vctObjects->erase(it);
|
---|
| 165 | return true;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | return false; // no object with that id was found
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[62ee2ce] | 172 | WorldMap* WorldMap::createDefaultMap()
|
---|
[60b77d2] | 173 | {
|
---|
[62ee2ce] | 174 | WorldMap* m = new WorldMap(12l, 12);
|
---|
[60b77d2] | 175 |
|
---|
[62ee2ce] | 176 | for(int x=0; x<12; x++)
|
---|
[60b77d2] | 177 | {
|
---|
[62ee2ce] | 178 | for(int y=0; y<12; y++)
|
---|
[60b77d2] | 179 | {
|
---|
[62ee2ce] | 180 | if (x ==0 || y == 0 || x == 11 || y == 11)
|
---|
[60b77d2] | 181 | m->setElement(x, y, TERRAIN_OCEAN);
|
---|
| 182 | else
|
---|
| 183 | m->setElement(x, y, TERRAIN_GRASS);
|
---|
[a1a3bd5] | 184 |
|
---|
[05051c7] | 185 | m->setStructure(x, y, STRUCTURE_NONE);
|
---|
[60b77d2] | 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[62ee2ce] | 189 | m->setElement(5, 5, TERRAIN_ROCK);
|
---|
| 190 |
|
---|
[60b77d2] | 191 | return m;
|
---|
| 192 | }
|
---|
[384b7e0] | 193 |
|
---|
| 194 | WorldMap* WorldMap::loadMapFromFile(string filename)
|
---|
| 195 | {
|
---|
[b650f8a] | 196 | WorldMap* m = NULL;
|
---|
[384b7e0] | 197 |
|
---|
[f401cac] | 198 | ifstream file(filename.c_str());
|
---|
[384b7e0] | 199 |
|
---|
| 200 | if (file.is_open())
|
---|
| 201 | {
|
---|
| 202 | string line;
|
---|
| 203 | int width, height;
|
---|
| 204 |
|
---|
| 205 | // read the map dimensions
|
---|
| 206 | getline(file, line);
|
---|
| 207 | if (line.size() > 0)
|
---|
| 208 | {
|
---|
| 209 | istringstream iss(line);
|
---|
| 210 | string token;
|
---|
| 211 | getline(iss, token, 'x');
|
---|
| 212 | width = atoi(token.c_str());
|
---|
| 213 | getline(iss, token, 'x');
|
---|
| 214 | height = atoi(token.c_str());
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | cout << "width: " << width << endl;
|
---|
| 218 | cout << "height: " << height << endl;
|
---|
| 219 |
|
---|
[b650f8a] | 220 | m = new WorldMap(width, height);
|
---|
| 221 |
|
---|
[384b7e0] | 222 | // read the map contents
|
---|
| 223 | int row = 0;
|
---|
| 224 | while ( file.good() )
|
---|
| 225 | {
|
---|
| 226 | getline(file, line);
|
---|
| 227 | if (line.size() > 0)
|
---|
| 228 | {
|
---|
| 229 | cout << "line: " << line << endl;
|
---|
| 230 |
|
---|
| 231 | istringstream iss(line);
|
---|
| 232 | string token;
|
---|
| 233 |
|
---|
[a1a3bd5] | 234 | if (row < height) {
|
---|
| 235 | // load terrain
|
---|
| 236 |
|
---|
| 237 | int type;
|
---|
| 238 | TerrainType terrain;
|
---|
| 239 |
|
---|
| 240 | for(int x=0; x<width; x++)
|
---|
| 241 | {
|
---|
| 242 | getline(iss, token, ',');
|
---|
| 243 | cout << "token: " << token << endl;
|
---|
| 244 | type = atoi(token.c_str());
|
---|
| 245 | cout << "type: " << type << endl;
|
---|
| 246 |
|
---|
| 247 | switch(type) {
|
---|
| 248 | case 1:
|
---|
| 249 | terrain = TERRAIN_GRASS;
|
---|
| 250 | break;
|
---|
| 251 | case 2:
|
---|
| 252 | terrain = TERRAIN_OCEAN;
|
---|
| 253 | break;
|
---|
| 254 | case 3:
|
---|
| 255 | terrain = TERRAIN_ROCK;
|
---|
| 256 | break;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | cout << "About to set element" << endl;
|
---|
| 260 | cout << "x: " << x << endl;
|
---|
| 261 | cout << "row: " << row << endl;
|
---|
| 262 | m->setElement(x, row, terrain);
|
---|
| 263 | }
|
---|
| 264 | }else {
|
---|
[b81cea1] | 265 | // load structure
|
---|
[a1a3bd5] | 266 |
|
---|
| 267 | int x, y, type;
|
---|
[05051c7] | 268 | StructureType structure;
|
---|
[a1a3bd5] | 269 |
|
---|
| 270 | getline(iss, token, ',');
|
---|
| 271 | cout << "token(x): " << token << endl;
|
---|
| 272 | x = atoi(token.c_str());
|
---|
| 273 |
|
---|
[384b7e0] | 274 | getline(iss, token, ',');
|
---|
[a1a3bd5] | 275 | cout << "token(y): " << token << endl;
|
---|
| 276 | y = atoi(token.c_str());
|
---|
| 277 |
|
---|
| 278 | getline(iss, token, ',');
|
---|
| 279 | cout << "token(type): " << token << endl;
|
---|
[384b7e0] | 280 | type = atoi(token.c_str());
|
---|
| 281 |
|
---|
| 282 | switch(type) {
|
---|
[a1a3bd5] | 283 | case 0:
|
---|
[05051c7] | 284 | structure = STRUCTURE_NONE;
|
---|
[a1a3bd5] | 285 | break;
|
---|
[384b7e0] | 286 | case 1:
|
---|
[05051c7] | 287 | structure = STRUCTURE_BLUE_FLAG;
|
---|
[384b7e0] | 288 | break;
|
---|
| 289 | case 2:
|
---|
[05051c7] | 290 | structure = STRUCTURE_RED_FLAG;
|
---|
[384b7e0] | 291 | break;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[05051c7] | 294 | m->setStructure(x, y, structure);
|
---|
[384b7e0] | 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | row++;
|
---|
| 299 | }
|
---|
| 300 | file.close();
|
---|
| 301 | }
|
---|
| 302 | else
|
---|
| 303 | cout << "Could not open the file" << endl;
|
---|
| 304 |
|
---|
| 305 | return m;
|
---|
| 306 | }
|
---|
[05051c7] | 307 |
|
---|
| 308 |
|
---|
| 309 | /*** Functions for Object ***/
|
---|
| 310 |
|
---|
[5f868c0] | 311 | WorldMap::Object::Object(int id, ObjectType type, int x, int y) {
|
---|
[05051c7] | 312 | this->type = type;
|
---|
[6e66ffd] | 313 | this->id = id;
|
---|
| 314 | this->pos.x = x;
|
---|
| 315 | this->pos.y = y;
|
---|
[05051c7] | 316 | }
|
---|
| 317 |
|
---|
[5f868c0] | 318 | WorldMap::Object::Object(int id, ObjectType type, POSITION pos) {
|
---|
[05051c7] | 319 | this->type = type;
|
---|
[6e66ffd] | 320 | this->id = id;
|
---|
| 321 | this->pos = pos;
|
---|
[05051c7] | 322 | }
|
---|
| 323 |
|
---|
| 324 | WorldMap::Object::~Object() {
|
---|
| 325 | }
|
---|
[5f868c0] | 326 |
|
---|
| 327 | void WorldMap::Object::serialize(char* buffer) {
|
---|
| 328 | memcpy(buffer, &this->type, 4);
|
---|
| 329 | memcpy(buffer+4, &this->id, 4);
|
---|
| 330 | memcpy(buffer+8, &this->pos.x, 4);
|
---|
| 331 | memcpy(buffer+12, &this->pos.y, 4);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | void WorldMap::Object::deserialize(char* buffer) {
|
---|
| 335 | memcpy(&this->type, buffer, 4);
|
---|
| 336 | memcpy(&this->id, buffer+4, 4);
|
---|
| 337 | memcpy(&this->pos.x, buffer+8, 4);
|
---|
| 338 | memcpy(&this->pos.y, buffer+12, 4);
|
---|
| 339 | }
|
---|