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
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>
[384b7e0]8
[60b77d2]9using namespace std;
10
[62ee2ce]11WorldMap::WorldMap(int width, int height)
[60b77d2]12{
13 this->width = width;
14 this->height = height;
15
16 vctMap = new vector<vector<TerrainType>*>(width);
[05051c7]17 vctStructures = new vector<vector<StructureType>*>(width);
18 vctObjects = new vector<Object>();
[60b77d2]19
20 for (int x=0; x<width; x++) {
[a1a3bd5]21 vector<TerrainType>* newMapVector = new vector<TerrainType>(height);
[05051c7]22 vector<StructureType>* newStructureVector = new vector<StructureType>(height);
[60b77d2]23
[a1a3bd5]24 for (int y=0; y<height; y++) {
25 (*newMapVector)[y] = TERRAIN_NONE;
[05051c7]26 (*newStructureVector)[y] = STRUCTURE_NONE;
[a1a3bd5]27 }
[60b77d2]28
[a1a3bd5]29 (*vctMap)[x] = newMapVector;
[05051c7]30 (*vctStructures)[x] = newStructureVector;
[60b77d2]31 }
32}
33
[62ee2ce]34WorldMap::~WorldMap()
[60b77d2]35{
[a1a3bd5]36 for (int x=0; x<width; x++) {
[60b77d2]37 delete (*vctMap)[x];
[05051c7]38 delete (*vctStructures)[x];
[a1a3bd5]39 }
[60b77d2]40
41 delete vctMap;
[05051c7]42 delete vctStructures;
[a1a3bd5]43 delete vctObjects;
[60b77d2]44}
45
[62ee2ce]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)
[60b77d2]52{
53 (*(*vctMap)[x])[y] = t;
54}
55
[05051c7]56WorldMap::StructureType WorldMap::getStructure(int x, int y)
[a1a3bd5]57{
[05051c7]58 return (*(*vctStructures)[x])[y];
[a1a3bd5]59}
60
[05051c7]61void WorldMap::setStructure(int x, int y, StructureType t)
[a1a3bd5]62{
[05051c7]63 (*(*vctStructures)[x])[y] = t;
64}
65
66vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
67 vector<WorldMap::Object> vctObjectsInRegion;
68
[6e66ffd]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
[05051c7]75 return vctObjectsInRegion;
76}
77
[6e66ffd]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);
[05051c7]81 vctObjects->push_back(o);
[a1a3bd5]82}
83
[6e66ffd]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
[62ee2ce]103WorldMap* WorldMap::createDefaultMap()
[60b77d2]104{
[62ee2ce]105 WorldMap* m = new WorldMap(12l, 12);
[60b77d2]106
[62ee2ce]107 for(int x=0; x<12; x++)
[60b77d2]108 {
[62ee2ce]109 for(int y=0; y<12; y++)
[60b77d2]110 {
[62ee2ce]111 if (x ==0 || y == 0 || x == 11 || y == 11)
[60b77d2]112 m->setElement(x, y, TERRAIN_OCEAN);
113 else
114 m->setElement(x, y, TERRAIN_GRASS);
[a1a3bd5]115
[05051c7]116 m->setStructure(x, y, STRUCTURE_NONE);
[60b77d2]117 }
118 }
119
[62ee2ce]120 m->setElement(5, 5, TERRAIN_ROCK);
121
[60b77d2]122 return m;
123}
[384b7e0]124
125WorldMap* WorldMap::loadMapFromFile(string filename)
126{
127 WorldMap* m = new WorldMap(12l, 12);
128
[f401cac]129 ifstream file(filename.c_str());
[384b7e0]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
[a1a3bd5]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;
[05051c7]197 StructureType structure;
[a1a3bd5]198
199 getline(iss, token, ',');
200 cout << "token(x): " << token << endl;
201 x = atoi(token.c_str());
202
[384b7e0]203 getline(iss, token, ',');
[a1a3bd5]204 cout << "token(y): " << token << endl;
205 y = atoi(token.c_str());
206
207 getline(iss, token, ',');
208 cout << "token(type): " << token << endl;
[384b7e0]209 type = atoi(token.c_str());
210
211 switch(type) {
[a1a3bd5]212 case 0:
[05051c7]213 structure = STRUCTURE_NONE;
[a1a3bd5]214 break;
[384b7e0]215 case 1:
[05051c7]216 structure = STRUCTURE_BLUE_FLAG;
[6e66ffd]217 cout << "Should have added blue flag object" << endl;
[384b7e0]218 break;
219 case 2:
[05051c7]220 structure = STRUCTURE_RED_FLAG;
[6e66ffd]221 cout << "Should have added red flag object" << endl;
[384b7e0]222 break;
223 }
224
[05051c7]225 m->setStructure(x, y, structure);
[384b7e0]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}
[05051c7]238
239
240/*** Functions for Object ***/
241
[6e66ffd]242WorldMap::Object::Object(ObjectType type, int id, int x, int y) {
[05051c7]243 this->type = type;
[6e66ffd]244 this->id = id;
245 this->pos.x = x;
246 this->pos.y = y;
[05051c7]247}
248
[6e66ffd]249WorldMap::Object::Object(ObjectType type, int id, POSITION pos) {
[05051c7]250 this->type = type;
[6e66ffd]251 this->id = id;
252 this->pos = pos;
[05051c7]253}
254
255WorldMap::Object::~Object() {
256}
Note: See TracBrowser for help on using the repository browser.