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