source: network-game/common/WorldMap.cpp@ 7511a2b

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

Resolved a bug where objects with duplicate ids were getting created

  • Property mode set to 100644
File size: 7.7 KB
Line 
1#include "WorldMap.h"
2
3#include <string>
4#include <iostream>
5#include <fstream>
6#include <sstream>
7#include <cstdlib>
8#include <cstring>
9
10using namespace std;
11
12WorldMap::WorldMap(int width, int height)
13{
14 this->width = width;
15 this->height = height;
16
17 vctMap = new vector<vector<TerrainType>*>(width);
18 vctStructures = new vector<vector<StructureType>*>(width);
19 vctObjects = new vector<Object>();
20
21 for (int x=0; x<width; x++) {
22 vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
23 vector<StructureType>* newStructureVector = new vector<StructureType>(height);
24
25 for (int y=0; y<height; y++) {
26 (*newMapVector)[y] = TERRAIN_NONE;
27 (*newStructureVector)[y] = STRUCTURE_NONE;
28 }
29
30 (*vctMap)[x] = newMapVector;
31 (*vctStructures)[x] = newStructureVector;
32 }
33}
34
35WorldMap::~WorldMap()
36{
37 for (int x=0; x<width; x++) {
38 delete (*vctMap)[x];
39 delete (*vctStructures)[x];
40 }
41
42 delete vctMap;
43 delete vctStructures;
44 delete vctObjects;
45}
46
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)
53{
54 (*(*vctMap)[x])[y] = t;
55}
56
57WorldMap::StructureType WorldMap::getStructure(int x, int y)
58{
59 return (*(*vctStructures)[x])[y];
60}
61
62void WorldMap::setStructure(int x, int y, StructureType t)
63{
64 (*(*vctStructures)[x])[y] = t;
65}
66
67vector<WorldMap::Object>* WorldMap::getObjects() {
68 return vctObjects;
69}
70
71vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
72 vector<WorldMap::Object> vctObjectsInRegion;
73
74 vector<WorldMap::Object>::iterator it;
75 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
76 if (it->pos.x/25 == x && it->pos.y/25 == y)
77 vctObjectsInRegion.push_back(*it);
78 }
79
80 return vctObjectsInRegion;
81}
82
83// used by the server to create new objects
84void WorldMap::addObject(WorldMap::ObjectType t, int x, int y) {
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);
99 vctObjects->push_back(o);
100}
101
102// used by the client to update object positions or create objects it has not seen before
103void WorldMap::updateObject(int id, WorldMap::ObjectType t, int x, int y) {
104 vector<WorldMap::Object>::iterator it;
105 bool foundObject = false;
106
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
117 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
118 if (it->id == id) {
119 foundObject = true;
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 }
129 it->pos.x = x;
130 it->pos.y = y;
131 }
132 }
133
134 if (!foundObject) {
135 WorldMap::Object o(id, t, x, y);
136 vctObjects->push_back(o);
137 }
138}
139
140bool 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
153WorldMap* WorldMap::createDefaultMap()
154{
155 WorldMap* m = new WorldMap(12l, 12);
156
157 for(int x=0; x<12; x++)
158 {
159 for(int y=0; y<12; y++)
160 {
161 if (x ==0 || y == 0 || x == 11 || y == 11)
162 m->setElement(x, y, TERRAIN_OCEAN);
163 else
164 m->setElement(x, y, TERRAIN_GRASS);
165
166 m->setStructure(x, y, STRUCTURE_NONE);
167 }
168 }
169
170 m->setElement(5, 5, TERRAIN_ROCK);
171
172 return m;
173}
174
175WorldMap* WorldMap::loadMapFromFile(string filename)
176{
177 WorldMap* m = new WorldMap(12l, 12);
178
179 ifstream file(filename.c_str());
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
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 {
244 // load structure
245
246 int x, y, type;
247 StructureType structure;
248
249 getline(iss, token, ',');
250 cout << "token(x): " << token << endl;
251 x = atoi(token.c_str());
252
253 getline(iss, token, ',');
254 cout << "token(y): " << token << endl;
255 y = atoi(token.c_str());
256
257 getline(iss, token, ',');
258 cout << "token(type): " << token << endl;
259 type = atoi(token.c_str());
260
261 switch(type) {
262 case 0:
263 structure = STRUCTURE_NONE;
264 break;
265 case 1:
266 structure = STRUCTURE_BLUE_FLAG;
267 cout << "Should have added blue flag object" << endl;
268 break;
269 case 2:
270 structure = STRUCTURE_RED_FLAG;
271 cout << "Should have added red flag object" << endl;
272 break;
273 }
274
275 m->setStructure(x, y, structure);
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}
288
289
290/*** Functions for Object ***/
291
292WorldMap::Object::Object(int id, ObjectType type, int x, int y) {
293 this->type = type;
294 this->id = id;
295 this->pos.x = x;
296 this->pos.y = y;
297}
298
299WorldMap::Object::Object(int id, ObjectType type, POSITION pos) {
300 this->type = type;
301 this->id = id;
302 this->pos = pos;
303}
304
305WorldMap::Object::~Object() {
306}
307
308void 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
315void 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}
Note: See TracBrowser for help on using the repository browser.