source: network-game/common/WorldMap.cpp@ 6e66ffd

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

Add functions to the WorldMap class to allow the server to notify clients of object creation and movement

  • Property mode set to 100644
File size: 6.1 KB
Line 
1#include "WorldMap.h"
2
3#include <string>
4#include <iostream>
5#include <fstream>
6#include <sstream>
7#include <cstdlib>
8
9using namespace std;
10
11WorldMap::WorldMap(int width, int height)
12{
13 this->width = width;
14 this->height = height;
15
16 vctMap = new vector<vector<TerrainType>*>(width);
17 vctStructures = new vector<vector<StructureType>*>(width);
18 vctObjects = new vector<Object>();
19
20 for (int x=0; x<width; x++) {
21 vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
22 vector<StructureType>* newStructureVector = new vector<StructureType>(height);
23
24 for (int y=0; y<height; y++) {
25 (*newMapVector)[y] = TERRAIN_NONE;
26 (*newStructureVector)[y] = STRUCTURE_NONE;
27 }
28
29 (*vctMap)[x] = newMapVector;
30 (*vctStructures)[x] = newStructureVector;
31 }
32}
33
34WorldMap::~WorldMap()
35{
36 for (int x=0; x<width; x++) {
37 delete (*vctMap)[x];
38 delete (*vctStructures)[x];
39 }
40
41 delete vctMap;
42 delete vctStructures;
43 delete vctObjects;
44}
45
46WorldMap::TerrainType WorldMap::getElement(int x, int y)
47{
48 return (*(*vctMap)[x])[y];
49}
50
51void WorldMap::setElement(int x, int y, TerrainType t)
52{
53 (*(*vctMap)[x])[y] = t;
54}
55
56WorldMap::StructureType WorldMap::getStructure(int x, int y)
57{
58 return (*(*vctStructures)[x])[y];
59}
60
61void WorldMap::setStructure(int x, int y, StructureType t)
62{
63 (*(*vctStructures)[x])[y] = t;
64}
65
66vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
67 vector<WorldMap::Object> vctObjectsInRegion;
68
69 vector<WorldMap::Object>::iterator it;
70 for(it = vctObjects->begin(); it != vctObjects->end(); it++) {
71 if (it->pos.x/25 == x && it->pos.y/25 == y)
72 vctObjectsInRegion.push_back(*it);
73 }
74
75 return vctObjectsInRegion;
76}
77
78// used by the server to create new objects
79void WorldMap::addObject(WorldMap::ObjectType t, int x, int y) {
80 WorldMap::Object o(t, vctObjects->size(), x, y);
81 vctObjects->push_back(o);
82}
83
84// used by the client to update object positions or create objects it has not seen before
85void WorldMap::updateObject(int id, WorldMap::ObjectType t, int x, int y) {
86 vector<WorldMap::Object>::iterator it;
87 bool foundObject = false;
88
89 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
90 if (it->id == id) {
91 foundObject = true;
92 it->pos.x = x;
93 it->pos.y = y;
94 }
95 }
96
97 if (!foundObject) {
98 WorldMap::Object o(t, id, x, y);
99 vctObjects->push_back(o);
100 }
101}
102
103WorldMap* WorldMap::createDefaultMap()
104{
105 WorldMap* m = new WorldMap(12l, 12);
106
107 for(int x=0; x<12; x++)
108 {
109 for(int y=0; y<12; y++)
110 {
111 if (x ==0 || y == 0 || x == 11 || y == 11)
112 m->setElement(x, y, TERRAIN_OCEAN);
113 else
114 m->setElement(x, y, TERRAIN_GRASS);
115
116 m->setStructure(x, y, STRUCTURE_NONE);
117 }
118 }
119
120 m->setElement(5, 5, TERRAIN_ROCK);
121
122 return m;
123}
124
125WorldMap* WorldMap::loadMapFromFile(string filename)
126{
127 WorldMap* m = new WorldMap(12l, 12);
128
129 ifstream file(filename.c_str());
130
131 if (file.is_open())
132 {
133 string line;
134 int width, height;
135
136 // read the map dimensions
137 getline(file, line);
138 if (line.size() > 0)
139 {
140 istringstream iss(line);
141 string token;
142 getline(iss, token, 'x');
143 width = atoi(token.c_str());
144 getline(iss, token, 'x');
145 height = atoi(token.c_str());
146 }
147
148 cout << "width: " << width << endl;
149 cout << "height: " << height << endl;
150
151 // read the map contents
152 int row = 0;
153 while ( file.good() )
154 {
155 getline(file, line);
156 if (line.size() > 0)
157 {
158 cout << "line: " << line << endl;
159
160 istringstream iss(line);
161 string token;
162
163 if (row < height) {
164 // load terrain
165
166 int type;
167 TerrainType terrain;
168
169 for(int x=0; x<width; x++)
170 {
171 getline(iss, token, ',');
172 cout << "token: " << token << endl;
173 type = atoi(token.c_str());
174 cout << "type: " << type << endl;
175
176 switch(type) {
177 case 1:
178 terrain = TERRAIN_GRASS;
179 break;
180 case 2:
181 terrain = TERRAIN_OCEAN;
182 break;
183 case 3:
184 terrain = TERRAIN_ROCK;
185 break;
186 }
187
188 cout << "About to set element" << endl;
189 cout << "x: " << x << endl;
190 cout << "row: " << row << endl;
191 m->setElement(x, row, terrain);
192 }
193 }else {
194 // load objects
195
196 int x, y, type;
197 StructureType structure;
198
199 getline(iss, token, ',');
200 cout << "token(x): " << token << endl;
201 x = atoi(token.c_str());
202
203 getline(iss, token, ',');
204 cout << "token(y): " << token << endl;
205 y = atoi(token.c_str());
206
207 getline(iss, token, ',');
208 cout << "token(type): " << token << endl;
209 type = atoi(token.c_str());
210
211 switch(type) {
212 case 0:
213 structure = STRUCTURE_NONE;
214 break;
215 case 1:
216 structure = STRUCTURE_BLUE_FLAG;
217 cout << "Should have added blue flag object" << endl;
218 break;
219 case 2:
220 structure = STRUCTURE_RED_FLAG;
221 cout << "Should have added red flag object" << endl;
222 break;
223 }
224
225 m->setStructure(x, y, structure);
226 }
227 }
228
229 row++;
230 }
231 file.close();
232 }
233 else
234 cout << "Could not open the file" << endl;
235
236 return m;
237}
238
239
240/*** Functions for Object ***/
241
242WorldMap::Object::Object(ObjectType type, int id, int x, int y) {
243 this->type = type;
244 this->id = id;
245 this->pos.x = x;
246 this->pos.y = y;
247}
248
249WorldMap::Object::Object(ObjectType type, int id, POSITION pos) {
250 this->type = type;
251 this->id = id;
252 this->pos = pos;
253}
254
255WorldMap::Object::~Object() {
256}
Note: See TracBrowser for help on using the repository browser.