source: network-game/common/WorldMap.cpp@ 5f868c0

Last change on this file since 5f868c0 was 5f868c0, checked in by dportnoy <dmp1488@…>, 11 years ago

Added partial server support for new messages for sending item info

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