source: network-game/common/WorldMap.cpp@ 05051c7

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

Added support for objects that can be at any pixel on the map, not just one per grid cell. What used to be called objects are now caled structures

  • Property mode set to 100644
File size: 5.2 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 return vctObjectsInRegion;
70}
71
72void WorldMap::addObject(int x, int y, WorldMap::ObjectType t) {
73 WorldMap::Object o(t, x, y);
74
75 vctObjects->push_back(o);
76}
77
78WorldMap* WorldMap::createDefaultMap()
79{
80 WorldMap* m = new WorldMap(12l, 12);
81
82 for(int x=0; x<12; x++)
83 {
84 for(int y=0; y<12; y++)
85 {
86 if (x ==0 || y == 0 || x == 11 || y == 11)
87 m->setElement(x, y, TERRAIN_OCEAN);
88 else
89 m->setElement(x, y, TERRAIN_GRASS);
90
91 m->setStructure(x, y, STRUCTURE_NONE);
92 }
93 }
94
95 m->setElement(5, 5, TERRAIN_ROCK);
96
97 return m;
98}
99
100WorldMap* WorldMap::loadMapFromFile(string filename)
101{
102 WorldMap* m = new WorldMap(12l, 12);
103
104 ifstream file(filename.c_str());
105
106 if (file.is_open())
107 {
108 string line;
109 int width, height;
110
111 // read the map dimensions
112 getline(file, line);
113 if (line.size() > 0)
114 {
115 istringstream iss(line);
116 string token;
117 getline(iss, token, 'x');
118 width = atoi(token.c_str());
119 getline(iss, token, 'x');
120 height = atoi(token.c_str());
121 }
122
123 cout << "width: " << width << endl;
124 cout << "height: " << height << endl;
125
126 // read the map contents
127 int row = 0;
128 while ( file.good() )
129 {
130 getline(file, line);
131 if (line.size() > 0)
132 {
133 cout << "line: " << line << endl;
134
135 istringstream iss(line);
136 string token;
137
138 if (row < height) {
139 // load terrain
140
141 int type;
142 TerrainType terrain;
143
144 for(int x=0; x<width; x++)
145 {
146 getline(iss, token, ',');
147 cout << "token: " << token << endl;
148 type = atoi(token.c_str());
149 cout << "type: " << type << endl;
150
151 switch(type) {
152 case 1:
153 terrain = TERRAIN_GRASS;
154 break;
155 case 2:
156 terrain = TERRAIN_OCEAN;
157 break;
158 case 3:
159 terrain = TERRAIN_ROCK;
160 break;
161 }
162
163 cout << "About to set element" << endl;
164 cout << "x: " << x << endl;
165 cout << "row: " << row << endl;
166 m->setElement(x, row, terrain);
167 }
168 }else {
169 // load objects
170
171 int x, y, type;
172 StructureType structure;
173
174 getline(iss, token, ',');
175 cout << "token(x): " << token << endl;
176 x = atoi(token.c_str());
177
178 getline(iss, token, ',');
179 cout << "token(y): " << token << endl;
180 y = atoi(token.c_str());
181
182 getline(iss, token, ',');
183 cout << "token(type): " << token << endl;
184 type = atoi(token.c_str());
185
186 switch(type) {
187 case 0:
188 structure = STRUCTURE_NONE;
189 break;
190 case 1:
191 structure = STRUCTURE_BLUE_FLAG;
192 break;
193 case 2:
194 structure = STRUCTURE_RED_FLAG;
195 break;
196 }
197
198 m->setStructure(x, y, structure);
199 }
200 }
201
202 row++;
203 }
204 file.close();
205 }
206 else
207 cout << "Could not open the file" << endl;
208
209 return m;
210}
211
212
213/*** Functions for Object ***/
214
215WorldMap::Object::Object(ObjectType type, POSITION pos) {
216 this->type = type;
217 this->pos = pos;
218}
219
220WorldMap::Object::Object(ObjectType type, int x, int y) {
221 this->type = type;
222 this->pos.x = x;
223 this->pos.y = y;
224}
225
226WorldMap::Object::~Object() {
227}
Note: See TracBrowser for help on using the repository browser.