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
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
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);
[a1a3bd5]76}
77
[62ee2ce]78WorldMap* WorldMap::createDefaultMap()
[60b77d2]79{
[62ee2ce]80 WorldMap* m = new WorldMap(12l, 12);
[60b77d2]81
[62ee2ce]82 for(int x=0; x<12; x++)
[60b77d2]83 {
[62ee2ce]84 for(int y=0; y<12; y++)
[60b77d2]85 {
[62ee2ce]86 if (x ==0 || y == 0 || x == 11 || y == 11)
[60b77d2]87 m->setElement(x, y, TERRAIN_OCEAN);
88 else
89 m->setElement(x, y, TERRAIN_GRASS);
[a1a3bd5]90
[05051c7]91 m->setStructure(x, y, STRUCTURE_NONE);
[60b77d2]92 }
93 }
94
[62ee2ce]95 m->setElement(5, 5, TERRAIN_ROCK);
96
[60b77d2]97 return m;
98}
[384b7e0]99
100WorldMap* WorldMap::loadMapFromFile(string filename)
101{
102 WorldMap* m = new WorldMap(12l, 12);
103
[f401cac]104 ifstream file(filename.c_str());
[384b7e0]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
[a1a3bd5]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;
[05051c7]172 StructureType structure;
[a1a3bd5]173
174 getline(iss, token, ',');
175 cout << "token(x): " << token << endl;
176 x = atoi(token.c_str());
177
[384b7e0]178 getline(iss, token, ',');
[a1a3bd5]179 cout << "token(y): " << token << endl;
180 y = atoi(token.c_str());
181
182 getline(iss, token, ',');
183 cout << "token(type): " << token << endl;
[384b7e0]184 type = atoi(token.c_str());
185
186 switch(type) {
[a1a3bd5]187 case 0:
[05051c7]188 structure = STRUCTURE_NONE;
[a1a3bd5]189 break;
[384b7e0]190 case 1:
[05051c7]191 structure = STRUCTURE_BLUE_FLAG;
[384b7e0]192 break;
193 case 2:
[05051c7]194 structure = STRUCTURE_RED_FLAG;
[384b7e0]195 break;
196 }
197
[05051c7]198 m->setStructure(x, y, structure);
[384b7e0]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}
[05051c7]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.