source: network-game/common/WorldMap.cpp@ e487381

Last change on this file since e487381 was e487381, checked in by dportnoy <dmp1488@…>, 12 years ago

The server removes objects from its map when they are picked up by players and processes DROP_FLAG messages

  • Property mode set to 100644
File size: 6.9 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}
70vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
71 vector<WorldMap::Object> vctObjectsInRegion;
72
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
79 return vctObjectsInRegion;
80}
81
82// used by the server to create new objects
83void WorldMap::addObject(WorldMap::ObjectType t, int x, int y) {
84 WorldMap::Object o(vctObjects->size(), t, x, y);
85 vctObjects->push_back(o);
86}
87
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) {
102 WorldMap::Object o(id, t, x, y);
103 vctObjects->push_back(o);
104 }
105}
106
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
120WorldMap* WorldMap::createDefaultMap()
121{
122 WorldMap* m = new WorldMap(12l, 12);
123
124 for(int x=0; x<12; x++)
125 {
126 for(int y=0; y<12; y++)
127 {
128 if (x ==0 || y == 0 || x == 11 || y == 11)
129 m->setElement(x, y, TERRAIN_OCEAN);
130 else
131 m->setElement(x, y, TERRAIN_GRASS);
132
133 m->setStructure(x, y, STRUCTURE_NONE);
134 }
135 }
136
137 m->setElement(5, 5, TERRAIN_ROCK);
138
139 return m;
140}
141
142WorldMap* WorldMap::loadMapFromFile(string filename)
143{
144 WorldMap* m = new WorldMap(12l, 12);
145
146 ifstream file(filename.c_str());
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
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 structure
212
213 int x, y, type;
214 StructureType structure;
215
216 getline(iss, token, ',');
217 cout << "token(x): " << token << endl;
218 x = atoi(token.c_str());
219
220 getline(iss, token, ',');
221 cout << "token(y): " << token << endl;
222 y = atoi(token.c_str());
223
224 getline(iss, token, ',');
225 cout << "token(type): " << token << endl;
226 type = atoi(token.c_str());
227
228 switch(type) {
229 case 0:
230 structure = STRUCTURE_NONE;
231 break;
232 case 1:
233 structure = STRUCTURE_BLUE_FLAG;
234 cout << "Should have added blue flag object" << endl;
235 break;
236 case 2:
237 structure = STRUCTURE_RED_FLAG;
238 cout << "Should have added red flag object" << endl;
239 break;
240 }
241
242 m->setStructure(x, y, structure);
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}
255
256
257/*** Functions for Object ***/
258
259WorldMap::Object::Object(int id, ObjectType type, int x, int y) {
260 this->type = type;
261 this->id = id;
262 this->pos.x = x;
263 this->pos.y = y;
264}
265
266WorldMap::Object::Object(int id, ObjectType type, POSITION pos) {
267 this->type = type;
268 this->id = id;
269 this->pos = pos;
270}
271
272WorldMap::Object::~Object() {
273}
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.