-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.cpp
267 lines (222 loc) · 5.15 KB
/
world.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Written by Colton Jelsema and Vincent Vermilya
// Content definition for the grovnik storage
#include "world.h"
#include <fstream>
using namespace std;
World::World(): width(128), height(128) {
tiles = new grovnik*[height];
for(unsigned int y = 0; y < height; y++) {
tiles[y] = new grovnik[width];
for(unsigned int x = 0; x < width; x++) {
tiles[y][x] = grovnik();
}
}
fog = new bool*[height];
for(unsigned int y = 0; y < height; y++) {
fog[y] = new bool[width];
for(unsigned int x = 0; x < width; x++) {
fog[y][x] = false;
}
}
}
World::World(unsigned int width1, unsigned int length1): width(width1), height(length1) {
tiles = new grovnik*[height];
for(unsigned int y = 0; y < height; y++) {
tiles[y] = new grovnik[width];
for(unsigned int x = 0; x < width; x++) {
tiles[y][x] = grovnik();
}
}
fog = new bool*[height];
for(unsigned int y = 0; y < height; y++) {
fog[y] = new bool[width];
for(unsigned int x = 0; x < width; x++) {
fog[y][x] = false;
}
}
}
World::~World() {
for(unsigned int y = 0; y < height; y++) {
delete [] tiles[y];
}
delete [] tiles;
for(unsigned int y = 0; y < height; y++) {
delete [] fog[y];
}
delete [] fog;
}
char ** World::fileRead(int * heroy, int * herox) {
//Read the map data
ifstream inf;
inf.open("map.txt");
char* input = new char[width + 1];
//Read each line
for (unsigned int y=0; y < height; ++y) {
inf.get(input, width + 1, '\n');
inf.ignore(width + 1, '\n');
//Interpret each value
for (unsigned int x=0; x < width; ++x) {
tiles[y][x].terrain = input[x] - 48;
}
inf.peek();
if (inf.eof())
y = height + 1;
}
delete [] input;
inf.close();
//Prepare obstacle type definition
char ** obstypes = new char*[10];
for (int i=0; i<10; ++i){
obstypes[i] = new char[128];
}
//Read the data for points of interest
int x;
int y;
int t;
int cost;
int value;
int power;
int type;
inf.open("mapdata.txt");
inf.peek();
while (!inf.eof()) {
inf >> y;
inf.ignore(5, ' ');
inf >> x;
inf.ignore(5, ' ');
inf >> t;
// Read the remaining data
switch (t) {
case 0: // Hero.
*herox = x;
*heroy = y;
break;
case 1: // treasure: Returned value
inf.ignore(5, ' ');
inf >> cost;
tiles[y][x].poi = new treasure(cost, false);
break;
case 2: // food: cost, value, name
inf.ignore(5, ' ');
inf >> cost;
inf.ignore(100, ' ');
inf >> value;
inf.ignore(100, ' ');
inf.get(input, 256, '\n');
tiles[y][x].poi = new food(cost, input, value);
break;
case 3: // tools: cost, power, obstype, name
inf.ignore(5, ' ');
inf >> cost;
inf.ignore(100, ' ');
inf >> power;
inf.ignore(100, ' ');
inf >> type;
inf.ignore(100, ' ');
inf.get(input, 256, '\n');
tiles[y][x].poi = new tool(cost, input, power, type);
break;
case 4: // clues
inf.ignore(5, ' ');
inf.get(input, 256, '\n');
tiles[y][x].poi = new clue(input);
break;
case 5: // ship
inf.ignore(5, ' ');
inf >> cost;
tiles[y][x].poi = new object(5, cost);
break;
case 6: // Binoculars
inf.ignore(5, ' ');
inf >> cost;
tiles[y][x].poi = new object(6, cost);
break;
case 7: // Obsticle: cost, type
inf.ignore(5, ' ');
inf >> cost;
inf.ignore(100, ' ');
inf >> type;
tiles[y][x].poi = new obstacle(cost, type);
break;
case 9: //define an obstacle type
inf.ignore(5, ' ');
inf.get(obstypes[x], 128, '\n');
break;
}
inf.ignore(2048, '\n'); // Ignore to the next line
inf.peek();
}
inf.close();
return obstypes;
}
void World::setGrovnik(unsigned int y, unsigned int x, int terr_type, object * data) {
if(y > height || x > width) {
return;
}
tiles[y][x].setData(terr_type, data);
}
grovnik * World::getAt(unsigned int y, unsigned int x) {
if(y > height || x > width) {
return NULL;
}
return & tiles[y][x];
}
// Clears the POI. MAKE SURE TO RETRIEVE THE ITEM FIRST
bool World::clearPOI(int y, int x) {
if (tiles[y][x].poi) {
delete tiles[y][x].poi;
tiles[y][x].poi = NULL;
return true;
}
return false;
}
// Clear fog[y1][x1] from false to true
// Basecase bounds check to avoid segfault
void World::clearfog(unsigned int y1, unsigned int x1) {
if(x1 >= width || y1 >= height) {
return;
}
fog[y1][x1] = true;
return;
}
// Call clearfog for a square radius centered on fog[y1][x1]
void World::clearfog_rad(unsigned int y1, unsigned int x1, unsigned int radius) {
for(int y = (int)radius * -1; y <= (int)radius; y++) {
for(int x = (int)radius * -1; x <= (int)radius; x++) {
clearfog(y1 + x, x1 + y);
}
}
return;
}
bool World::getfog(unsigned int y1, unsigned int x1) {
if(x1 >= width || y1 >= height) {
return false;
}
return fog[y1][x1];
}
#ifdef TEST
bool ** World::getfog() {
return fog;
}
#endif
grovnik::grovnik() {
terrain = 0;
poi = NULL;
}
grovnik::~grovnik() {
if(poi) {
delete poi;
}
}
grovnik::grovnik(const grovnik & copy) {
terrain = copy.terrain;
}
// if there is nothing of interest on this tile, set data to null
grovnik::grovnik(int terr_type, object * data) {
terrain = terr_type;
poi = data;
}
void grovnik::setData(int terr_type, object * data) {
terrain = terr_type;
poi = data;
}