-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaze.c
183 lines (141 loc) · 4.9 KB
/
Maze.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "Maze.h"
#include "Error.h"
void removeItemFromMap(Room* room) {
if (room != NULL && room->loot != NULL) {
free(room->loot);
room->loot = NULL;
return;
}
fprintf(stderr, "Could not remove item from map!\n"); // Add to error handling???
return;
}
bool deleteEnemyFromMap(Room* room, bool deleteGear) {
if (!room || !room->enemy.enemy || !room->enemy.boss) return false;
bool del;
if (room->hasBoss) {
del = deleteBoss(room->enemy.boss, deleteGear);
room->enemy.boss = NULL;
room->hasBoss = false;
return del;
}
del = deleteEnemy(room->enemy.enemy);
room->enemy.enemy = NULL;
return del;
}
/**
* Prints the cleaned-up version of the maze name
* @param name The maze name to print
*/
static void printMazeName(str name) {
int nameLen = strlen(name);
str _name = (str) malloc(nameLen + 1);
if (!_name) {
handleError(ERR_MEM, WARNING, "Could not allocate space for map name display! Defaulting to maze name.\n");
_name = name;
} else {
strcpy(_name, name);
str temp = _name;
// First non-null character will always get capitalized
if (*temp != '\0') *temp = toupper(*temp);
temp++;
while (*temp != '\0') {
// Change the current character of _ to space
if (*temp == '_') *temp = ' ';
// Uppercase characters that followed _ (now space)
// Guaranteed that *(temp - 1) will not be out of bounds by the increase prior to looping
if (*(temp - 1) == ' ') *temp = toupper(*temp);
temp++;
}
}
printf("%s%s%s: ", PURPLE, _name, RESET);
// In the case that _name is not malloc'd, _name is assigned to name, thus they point to the same thing
if (_name != name) free(_name);
}
static void placeRoomOnGrid(Room* room, char** grid, int x, int y, bool** visited, uint gridSize, Room* playerRoom) {
if (room == ((void*) ((long long) NO_EXIT)) || visited[y][x]) return;
visited[y][x] = true;
if (room->hasBoss) grid[y][x] = '!';
else if (room->enemy.enemy != NULL) grid[y][x] = '%';
else if (room->loot != NULL) grid[y][x] = '$';
else if (room->id == 0) grid[y][x] = '@';
else grid[y][x] = '#';
if (room == playerRoom) grid[y][x] = 'o';
if (room->exits[0] != ((void*) ((long long) NO_EXIT)) && y > 1) {
grid[y-1][x] = '|';
placeRoomOnGrid(room->exits[0], grid, x, y-2, visited, gridSize, playerRoom);
}
if (room->exits[1] != ((void*) ((long long) NO_EXIT)) && x < gridSize-2) {
grid[y][x+1] = '-';
placeRoomOnGrid(room->exits[1], grid, x+2, y, visited, gridSize, playerRoom);
}
if (room->exits[2] != ((void*) ((long long) NO_EXIT)) && y < gridSize-2) {
grid[y+1][x] = '|';
placeRoomOnGrid(room->exits[2], grid, x, y+2, visited, gridSize, playerRoom);
}
if (room->exits[3] != ((void*) ((long long) NO_EXIT)) && x > 1) {
grid[y][x-1] = '-';
placeRoomOnGrid(room->exits[3], grid, x-2, y, visited, gridSize, playerRoom);
}
}
void showMap(Maze* maze, Room* playerRoom) {
// TODO: Fix map size printing
uint gridSize = (int) (sqrt(maze->size) * 6);
if (gridSize < 5) gridSize = 5;
char** grid = malloc(gridSize * sizeof(char*));
bool** visited = malloc(gridSize * sizeof(bool*));
for (uint i = 0; i < gridSize; i++) {
grid[i] = malloc(gridSize * sizeof(char));
visited[i] = malloc(gridSize * sizeof(bool));
memset(grid[i], ' ', gridSize * sizeof(char));
memset(visited[i], false, gridSize * sizeof(bool));
}
placeRoomOnGrid(maze->entry, grid, gridSize/2, gridSize/2, visited, gridSize, playerRoom);
printMazeName(maze->name);
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
char room = grid[i][j];
if (room == 'o') { // player
printf("%s%c%s", GREEN, room, RESET);
} else if (room == '$') { // loot
printf("%s%c%s", YELLOW, room, RESET);
} else if (room == '@') { // entry
printf("%s%c%s", CYAN, room, RESET);
} else if (room == '%') { // enemy
printf("%s%c%s", PURPLE, room, RESET);
} else if (room == '!') { // boss
printf("%s%c%s", RED, room, RESET);
} else { // connections and empty rooms
printf("%s%c", RESET, room);
}
// else putchar(room);
}
putchar('\n');
}
}
void deleteRoom(Room* room) {
if (room->loot != NULL) deleteItem(room->loot);
deleteEnemyFromMap(room, true);
free(room->info);
if (room->storyFile != NULL) free(room->storyFile);
if (room->file != NULL) fclose(room->file);
free(room);
room = NULL;
}
void deleteMaze(Maze* maze) {
Table* table = initTableL(maze->size);
if (!table) handleError(ERR_MEM, FATAL, "Could not allocate space for the table!\n");
Room* room = maze->entry;
addAndRecurse(room, table);
room = NULL;
for (int i = 0; i < table->len; i++) {
deleteRoom(table->rooms[i]);
}
deleteTable(table);
free(maze->name);
free(maze);
}