-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.cpp
More file actions
358 lines (326 loc) · 10.9 KB
/
map.cpp
File metadata and controls
358 lines (326 loc) · 10.9 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "map.h"
#include "utils.h"
#include "player.h"
#include "block_fill.h"
#include "elfin.h"
#include <cstdio>
#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
// class Block {
// public:
// void init(int, int[]);
// int x, y;
// int overall_property;
// int content[BLOCK_H][BLOCK_W];
// };
//
// class Map {
// public:
// void init(int);
// void print(void);
// void inspect(int);
// void check(Player&);
// void update(Player);
// void converter(Player&);
// Block get_tar_portal(int, int, int, bool&);
// int content[MAP_R][MAP_C];
// int portal_color[8];
// Block blocks[MAP_H][MAP_W];
// int gravity; // verticle gravity only
// std::vector<Elfin> elfins;
// };
/*
fill the block content[5][10] and attach property
input: int variable, property of the block; int array, storing the randomly generated arrangement of portal colors
no return value
*/
void Block::init(int property, int portal_color[]) {
fill(property, (int*)content, 0, portal_color);
overall_property = property;
}
/*
print the properties (and contents) of all blocks/the map, for debugging
input: int value indicating the debugging level
no return value
*/
void Map::inspect(int level) {
if (level >= 1) {
for (int i = 0; i < MAP_H; i++) {
for (int j = 0; j < MAP_W; j++)
printf("%d ", blocks[i][j].overall_property);
printf("\n");
}
}
if (level >= 2) {
for (int i = 0; i < MAP_R; i++) {
for (int j = 0; j < MAP_C; j++)
printf("%d ", content[i][j]);
printf("\n");
}
}
}
/*
fill all blocks with corresponding properties according to the pre-saved specific map file
input: the map number
no return value
*/
void Map::init(int map_num) {
for (int i = 0; i < MAP_R; i++)
for (int j = 0; j < MAP_C; j++)
content[i][j] = 0;
std::string path = "lib/maps/" + std::to_string(map_num) + ".txt";
std::ifstream map_file(path);
for (int i = MAP_H - 1; i >= 0; i--) {
for (int j = 0; j < MAP_W; j++) {
int serial;
int temp;
char x;
map_file >> x;
if (x == '\n') map_file >> x;
switch (x) {
case '0':
blocks[i][j].overall_property = 0;
break;
case '#': // ground
blocks[i][j].overall_property = 1;
break;
case '(': // up portal
map_file >> serial >> x;
blocks[i][j].overall_property = serial * 10 + 1;
break;
case ')': // down portal
map_file >> serial >> x;
blocks[i][j].overall_property = serial * 10 + 2;
break;
case '[': // left portal
map_file >> serial >> x;
blocks[i][j].overall_property = serial * 10 + 3;
break;
case ']': // right portal
map_file >> serial >> x;
blocks[i][j].overall_property = serial * 10 + 4;
break;
case '|': // gate
blocks[i][j].overall_property = 4;
break;
case 'i': // ice ground
blocks[i][j].overall_property = 21;
break;
case 'f': // fire ground
blocks[i][j].overall_property = 22;
break;
case 'g': // lower gravity-converter
blocks[i][j].overall_property = 51;
break;
case 'G': // upper gravity-converter
blocks[i][j].overall_property = 52;
break;
case 'b': // elfin
map_file >> serial >> x;
Elfin elfin;
int content[4][6];
fill(serial, (int*)content, 0, portal_color);
elfin.init(i * BLOCK_H + 2, j * BLOCK_W + 4, 4, 6, content, serial, i);
elfins.push_back(elfin);
blocks[i][j].overall_property = 1;
break;
}
}
}
map_file.close();
//generate a random arrangement of portal colors
bool judge_repeat[8];
for (int i = 0; i < 8; i++)
judge_repeat[i] = false;
srand(time(NULL));
for (int i = 0; i < 8; i++) {
int c = rand() % 8;
while(judge_repeat[c])
c = rand() % 8;
judge_repeat[c] = true;
portal_color[i] = c;
}
for (int i = 0; i < MAP_H; i++)
for (int j = 0; j < MAP_W; j++) {
blocks[i][j].x = i, blocks[i][j].y = j;
blocks[i][j].init(blocks[i][j].overall_property, portal_color);
}
gravity = -1;
}
/*
display the whole map
no input
no return value
*/
void Map::print(void) {
for (int i = MAP_R - 1; i >= 0; i--) {
for (int j = 0; j < MAP_C; j++)
super_print(content[i][j]);
printf("\n");
}
}
/*
convert the world to the opposite:
· reverse the direction of world converters
· swap the color of grounds and air
· reverse the properties of property grounds and elfins
input: an object of class Player
no return value
*/
void Map::converter(Player& u) {
for (int i = 0; i < MAP_H; i++) {
for (int j = 0; j < MAP_W; j++) {
if (blocks[i][j].overall_property / 10 >= 6) { // world converter
bool flag = false;
Block block = get_tar_portal(blocks[i][j].overall_property / 10, i, j, flag);
if (!flag) {
int k = blocks[i][j].overall_property % 10;
int serial = blocks[i][j].overall_property / 10;
int new_direc = 4 * ((k + 1) / 2) - 1 - k;
blocks[i][j].overall_property = serial * 10 + new_direc;
}
}
else if (blocks[i][j].overall_property / 10 == 2) {
int k = blocks[i][j].overall_property % 10;
k = 3 - k;
blocks[i][j].overall_property = 20 + k;
}
fill(blocks[i][j].overall_property, (int*)blocks[i][j].content, u.state, portal_color);
}
}
for (int i = 0; i < elfins.size(); i++) {
elfins[i].property = 63 - elfins[i].property;
fill(elfins[i].property, (int*)elfins[i].content, u.state, portal_color);
}
}
/*
find the portal that is paired with a certain portal
input:
· int variable tar, the property of the certain portal
· int variables player_xx & player_yy, the block the player is in
· bool variable flag, whether the portal can be found
return value:
· an object of class Block
· bool variable flag, true if the certain portal is portal, false if it's world converter
*/
Block Map::get_tar_portal(int tar, int player_xx, int player_yy, bool& flag) {
Block block;
for (int i = 0; i < MAP_H; i++)
for (int j = 0; j < MAP_W; j++) {
if (player_xx == i && player_yy == j) continue; // omit itself
else if (blocks[i][j].overall_property / 10 == tar) {
flag = true;
return blocks[i][j];
}
}
return block;
}
/*
if the player reaches a gravity converter or a portal/world converter, reverse gravity/deliver the player
input: an object of class Player
no return value
*/
void Map::check(Player &u) {
u.touching_gravity = false;
int x = u.x, y = u.y;
int last_x = u.last_x, last_y = u.last_y;
for (int player_i = x; player_i < x + u.height; player_i++)
for (int player_j = y; player_j < y + u.width; player_j++) {
if (content[player_i][player_j] % 100 == 5) { // gravity converter
u.touching_gravity = true;
if (!u.last_touching_gravity) {
gravity *= -1;
u.last_touching_gravity = true;
}
}
else if (content[player_i][player_j] % 100 >= 6 && content[player_i][player_j] % 100 <= 20) { // portal or world converter
int xx = player_i / BLOCK_H, yy = player_j / BLOCK_W;
int direc_from = 0; // 0: back 1: front
Block portal = blocks[xx][yy];
bool flag = false;
Block tar_portal = get_tar_portal(content[player_i][player_j] % 100, xx, yy, flag);
int direc_orig = portal.overall_property % 10;
if (direc_orig <= 2) { // the original portal is towards up/down
if (last_x > x)
direc_from = direc_orig % 2; // front
else
direc_from = 1 - direc_orig % 2; // back
}
else { // the original portal is towards left/right
if (last_y < y)
direc_from = direc_orig % 2;
else
direc_from = 1 - direc_orig % 2;
}
int res_property; // the portal to go
Block res_portal;
if (flag) { // portal
res_property = tar_portal.overall_property % 10;
res_portal = tar_portal;
}
else { // world-converter
u.state = 1 - u.state;
converter(u);
int k = portal.overall_property % 10;
res_property = 4 * ((k + 1) / 2) - 1 - k;
res_portal = portal;
}
if (res_property <= 2) { // up and down
if (direc_orig <= 2) {
double cspeed = u.real_speed;
if (res_property == direc_orig) { // bounce
if (u.real_speed != 0)
cspeed = -0.9 * u.real_speed;
else
cspeed = -0.9 * u.speed;
}
if (abs(cspeed) <= 1 && abs(cspeed) > 0)
cspeed = (cspeed / abs(cspeed)) * ceil(abs(cspeed));
else if (abs(cspeed) > 0)
cspeed = (cspeed / abs(cspeed)) * floor(abs(cspeed));
u.speed = cspeed;
u.real_speed = cspeed;
}
u.x = res_portal.x * BLOCK_H + (BLOCK_H / 2 + ((1.5 - res_property) * 2) * ((direc_from - 0.5) * 2) * (u.height * (((res_property % 2) + (direc_from % 2)) % 2) + 2 * (1 - ((res_property % 2) + (direc_from % 2)) % 2)));
u.y = res_portal.y * BLOCK_W + ((BLOCK_W - u.width) / 2);
}
else { // left and right
u.x = res_portal.x * BLOCK_H + 1;
u.y = res_portal.y * BLOCK_W + (BLOCK_W / 2 + ((3.5 - res_property) * 2) * ((0.5 - direc_from) * 2) * (u.width * (1 - ((res_property % 2) + (direc_from % 2)) % 2) + 1 * (((res_property % 2) + (direc_from % 2)) % 2)));
}
return;
}
}
if (!u.touching_gravity) u.last_touching_gravity = 0;
}
/* update the map content
input: an object of class Player
no return value
*/
void Map::update(Player u) {
for (int i = 0; i < MAP_R; i++)
for (int j = 0; j < MAP_C; j++)
content[i][j] = 0;
for (int r = 0; r < MAP_H; r++)
for (int c = 0; c < MAP_W; c++)
for (int i = 0; i < BLOCK_H; i++)
for (int j = 0; j < BLOCK_W; j++) {
int x = r * BLOCK_H, y = c * BLOCK_W;
int cont = blocks[r][c].content[BLOCK_H - i - 1][j];
if (cont != 0) content[x + i][y + j] = cont;
}
//insert elfins into the map
for (int idx = 0; idx < elfins.size(); idx++)
for (int i = elfins[idx].x; i < elfins[idx].x + elfins[idx].height; i++)
for (int j = elfins[idx].y - 2; j < elfins[idx].y - 2 + elfins[idx].width; j++)
if (elfins[idx].content[i - elfins[idx].x][j - elfins[idx].y + 2] % 100 != 0)
content[i][j] = elfins[idx].content[i - elfins[idx].x][j - elfins[idx].y + 2];
//insert the player into the map
for (int i = u.x; i < u.x + u.height; i++)
for (int j = u.y; j < u.y + u.width; j++)
content[i][j] = u.content[i - u.x][j - u.y];
}