-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
executable file
·409 lines (342 loc) · 10.7 KB
/
map.c
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <ncurses.h>
#include <time.h>
char buffer[1024];
char mapName[1024];
char authorName[1024];
char castleStrength[1024];
char castleTouch[1024];
char attackerWin[1024];
char attackerShots[1024];
char attackerRespawn[1024];
char defenderWin[1024];
char defenderShots[1024];
char defenderRespawn[1024];
char map[1024];
int castle_strength[1024];
char castle[1024];
char attacker[1024];
char defender[1024];
int attackerRespawnPointCount = 0;
int defenderRespawnPointCount = 0;
int wallCount = 0;
struct percent_wall {
int x;
int y;
};
struct round_counter {
int x;
int y;
};
struct point_t {
int x;
int y;
};
struct row_t {
char content[1024];
};
struct round_counter round_counter_location[10];
struct percent_wall percent_wall_location[10];
struct point_t attacker_respawn_location_list[1024];
struct point_t defender_respawn_location_list[1024];
struct row_t list_row[20];
char* getMap() {
return map;
}
char* getMapName() {
return mapName;
}
char* getAuthor() {
return authorName;
}
int getDefaultCastleStrength() {
char* subBuffer = strstr(castle, "strength");
subBuffer += 9;
char* subBuffer2 = strstr(castle, "touch");
int diffBytes = subBuffer2 - subBuffer;
strncpy(castleStrength, subBuffer, diffBytes - 1);
int value = atoi(castleStrength);
return value;
}
void set_castle_strength(int str, int x, int y) {
castle_strength[x + 70 * y] = str;
}
int get_castle_strength(int x, int y) {
return castle_strength[x + 70 * y];
}
int getCastleTouch() {
char* subBuffer = strstr(castle, "touch");
subBuffer += 6;
strncpy(castleTouch, subBuffer, strlen(subBuffer) - 1);
int value = atoi(castleTouch);
return value;
}
int getAttackerWin() {
char* subBuffer = strstr(attacker, "destroy");
subBuffer += 8;
char* subBuffer2 = strstr(attacker, "shots");
int diffBytes = subBuffer2 - subBuffer;
strncpy(attackerWin, subBuffer, diffBytes - 2);
int value = atoi(attackerWin);
return value;
}
int getAttackerShots() {
char* subBuffer = strstr(attacker, "shot");
subBuffer += 6;
char* subBuffer2 = strstr(attacker, "respawn");
int diffBytes = subBuffer2 - subBuffer;
strncpy(attackerShots, subBuffer, diffBytes - 1);
int value = atoi(attackerShots);
return value;
}
int getAttackerRespawn() {
char* subBuffer = strstr(attacker, "respawn");
subBuffer += 8;
strncpy(attackerRespawn, subBuffer, strlen(subBuffer) - 1);
int value = atoi(attackerRespawn);
return value;
}
int getDefenderWin() {
char* subBuffer = strstr(defender, "survive");
subBuffer += 8;
char* subBuffer2 = strstr(defender, "shot");
int diffBytes = subBuffer2 - subBuffer;
strncpy(defenderWin, subBuffer, diffBytes - 2);
int value = atoi(defenderWin);
return value;
}
int getDefenderShots() {
char* subBuffer = strstr(defender, "shot");
subBuffer += 6;
char* subBuffer2 = strstr(defender, "respawn");
int diffBytes = subBuffer2 - subBuffer;
strncpy(defenderShots, subBuffer, diffBytes - 1);
int value = atoi(defenderShots);
return value;
}
int getDefenderRespawn() {
char* subBuffer = strstr(defender, "respawn");
subBuffer += 8;
strncpy(defenderRespawn, subBuffer, strlen(subBuffer) - 1);
int value = atoi(defenderRespawn);
return value;
}
char *replace_str(char *str, char *orig, char *rep) {
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
char getCharOnMap(int x, int y) {
return list_row[y].content[x];
}
//randomly get a respawn point
struct point_t getAttackerRespawnPoint() {
srand(time(NULL));
int ran = rand();
int rand_capped = ran % (attackerRespawnPointCount - 1); //between 0 respawnPointCount
return attacker_respawn_location_list[rand_capped];
}
struct point_t getDefenderRespawnPoint() {
srand(time(NULL));
int ran = rand();
int rand_capped = ran % (defenderRespawnPointCount - 1); //between 0 respawnPointCount
return defender_respawn_location_list[rand_capped];
}
struct percent_wall getPercentWall() {
return percent_wall_location[0];
}
struct round_counter getRoundCounter() {
return round_counter_location[0];
}
bool isCastleChar(char c) {
return c == '-' || c == '|' || c == '+' || c == '*' || c == '/' || c == '\\' || c == '&';
}
int get_initial_wall_count() {
return wallCount;
}
int get_current_wall_count() {
int i = 0;
for (int y = 0; y < 20; y++) {
struct row_t r = list_row[y];
for (int x = 0; x < 70; x++) {
char c = r.content[x];
if (isCastleChar(c)) {
i++;
}
}
}
return i;
}
void setCharOnMap(char replacement, int x, int y) {
list_row[y].content[x] = replacement;
}
void createColorPair() {
init_pair(0, COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_BLACK, COLOR_GREEN);
init_pair(2, COLOR_BLACK, COLOR_RED);
init_pair(3, COLOR_BLACK, COLOR_BLUE);
init_pair(4, COLOR_BLACK, COLOR_WHITE);
init_pair(5, COLOR_BLACK, COLOR_CYAN);
}
void initBoard(){
createColorPair();
//background team A (red)
for(int i = 70; i < 80; i++){
for(int j = 1; j <= 10; j++){
attron(COLOR_PAIR(2));
mvprintw(j, i, " ");
}
}
//background team B (blue)
for(int i = 70; i < 80; i++){
for(int j = 11; j <= 20; j++){
attron(COLOR_PAIR(3));
mvprintw(j, i, " ");
}
}
//description area (scrollable)
for(int i = 0; i < 80; i++){
for(int j = 21; j < 24; j++){
attron(COLOR_PAIR(4));
mvprintw(j, i, " ");
}
}
}
void teamInfoMap() {
attron(COLOR_PAIR(5));
int i = 0;
for (i = 0; i < 80; i++) {
mvprintw(0, i, " ");
}
char* mapText = "map: ";
char* authorText = "author: ";
mvprintw(0, 0, mapText);
mvprintw(0, strlen(mapText), getMapName());
mvprintw(0, strlen(mapText) + strlen(getMapName()) + 2, authorText);
mvprintw(0, strlen(mapText) + strlen(getMapName()) + strlen(authorText) + 2
, getAuthor());
}
void loadMapSecondRound(char mapFile[1024]) {
memset(castle_strength, 0, sizeof castle_strength);
FILE *file;
file = fopen(mapFile, "r");
// initialize map to empty
memset(&map, ' ', sizeof map);
if (file == NULL) {
perror ("Error opening file");
exit(EXIT_FAILURE);
} else {
int i = 0;
while (fgets(buffer, 1024, file) != NULL) {
if (i == 0) {
strncpy(mapName, buffer, strlen(buffer)-1);
} else if (i == 1) {
strncpy(authorName, buffer, strlen(buffer)-1);
} else if (i == 2) {
strncpy(castle, buffer, strlen(buffer)-1);
} else if (i == 3) {
strncpy(attacker, buffer, strlen(buffer)-1);
} else if (i == 4) {
strncpy(defender, buffer, strlen(buffer)-1);
} else if (i >= 7) {
int mapRow = i - 7;
char* printable_line = replace_str(&buffer[2], "%", "%%");
printable_line = replace_str(printable_line, "\n", " ");
struct row_t row;
strncpy(row.content, printable_line, strlen(printable_line) + 1);
list_row[mapRow] = row;
while (strlen(list_row[mapRow].content) < 70) {
strncat(list_row[mapRow].content, " ", 1);
}
mvprintw(i - 6, 0, list_row[mapRow].content);
}
i++;
if (i > 27) break;
}
}
fclose(file);
//fprintf(stdout, "%s", getMap());
}
void loadMap(char mapFile[1024]) {
memset(castle_strength, 0, sizeof castle_strength);
FILE *file;
file = fopen(mapFile, "r");
// initialize map to empty
memset(&map, ' ', sizeof map);
if (file == NULL) {
perror ("Error opening file");
exit(EXIT_FAILURE);
} else {
int i = 0;
while (fgets(buffer, 1024, file) != NULL) {
if (i == 0) {
strncpy(mapName, buffer, strlen(buffer)-1);
} else if (i == 1) {
strncpy(authorName, buffer, strlen(buffer)-1);
} else if (i == 2) {
strncpy(castle, buffer, strlen(buffer)-1);
} else if (i == 3) {
strncpy(attacker, buffer, strlen(buffer)-1);
} else if (i == 4) {
strncpy(defender, buffer, strlen(buffer)-1);
} else if (i >= 7) {
int mapRow = i - 7;
char* printable_line = replace_str(&buffer[2], "%", "%%");
printable_line = replace_str(printable_line, "\n", " ");
struct row_t row;
strncpy(row.content, printable_line, strlen(printable_line) + 1);
list_row[mapRow] = row;
while (strlen(list_row[mapRow].content) < 70) {
strncat(list_row[mapRow].content, " ", 1);
}
for (int j = 0; j < 70; j++) {
int actualLocation = j + 2;
if (buffer[actualLocation] == '@') {
attacker_respawn_location_list[attackerRespawnPointCount].x = j;
attacker_respawn_location_list[attackerRespawnPointCount].y = i - 7;
attackerRespawnPointCount++;
}
else if (buffer[actualLocation] == '*' || buffer[actualLocation] == '+') {
defender_respawn_location_list[defenderRespawnPointCount].x = j;
defender_respawn_location_list[defenderRespawnPointCount].y = i - 7;
defenderRespawnPointCount++;
set_castle_strength(getDefaultCastleStrength(), j-2, i-7);
wallCount++;
}
else if (buffer[actualLocation] == '-' || buffer[actualLocation] == '|' ||
buffer[actualLocation] == '/' || buffer[actualLocation] == '\\') {
set_castle_strength(getDefaultCastleStrength(), j-2, i-7);
wallCount++;
}
else if (buffer[actualLocation] == '%') {
percent_wall_location[0].x = j;
percent_wall_location[0].y = i - 7;
}
else if (buffer[actualLocation] == '#') {
round_counter_location[0].x = j;
round_counter_location[0].y = i - 7;
}
}
if (i < 27) {
mvprintw(i - 6, 0, list_row[mapRow].content);
}
}
i++;
if (i > 26) break;
}
fclose(file);
//fprintf(stdout, "%s", getMap());
}
}