This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapGenerator.cpp
223 lines (169 loc) · 5.95 KB
/
MapGenerator.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
#include "MapGenerator.hpp"
MapGenerator::~MapGenerator() {
// free memmory
}
MapGenerator::MapGenerator() {
}
MapGenerator::MapGenerator(int height, int width) {
Init(height, width);
}
void MapGenerator::Init(int height, int width) {
MapGenerator::height = height;
MapGenerator::width = width;
MapGenerator::mapHeight = height * 2 + 1;
MapGenerator::mapWidth = width * 2 + 1;
openGLReadyMatrix = NULL;
map = (Cell**) malloc(height * sizeof(Cell*));
for (int i = 0; i < height; i++) {
map[i] = (Cell*) malloc(width * sizeof(Cell));
for (int j = 0; j < width; j++) {
Cell cell;
cell.x = i; // Why not Why yes :)
cell.y = j; // Why not Why yes :) We can use the position i, j but its Okay
cell.visited = false;
map[i][j] = cell;
}
}
charMaze = (char**) malloc((height * 2 - 1 ) * sizeof(char*));
for (int i = 0; i < height * 2 - 1; i++) {
charMaze[i] = (char*) malloc((width * 2 - 1 ) * sizeof(char));
for (int j = 0; j < width * 2 - 1; j++)
charMaze[i][j] = 'X';
charMaze[i][width * 2] = '\0';
}
//charMaze[height * 2] = '\0';
srand(time(NULL));
int random = rand() % width;
// Prepare algoritm
map[random][0].visited = true;
start_point = map[random][0];
tot_options = 0;
tot_path = 0;
path = NULL;
path = (Cell*) realloc(path, (++tot_path) * sizeof(Cell));
path[tot_path - 1] = map[random][0];
charMaze[random*2][0] = 'S';
buildMapDFS();
}
void MapGenerator::buildMapDFS() {
Cell current = path[tot_path - 1];
Cell* options = getMovementOptions(current);
if (options == NULL)
if (allVisited())
return;
else
while (options == NULL) { // ** Backtrack ** Chegamos a um fim sem saida
path = (Cell*) realloc(path, (--tot_path) * sizeof(Cell));
current = path[tot_path - 1];
if (allVisited())
return;
options = getMovementOptions(current);
}
// Vamos escolher uma célula candidata aleatoriamente
int random = rand() % tot_options;
Cell chosed = options[random];
path = (Cell*) realloc(path, (++tot_path) * sizeof(Cell));
path[tot_path - 1] = chosed;
map[chosed.x][chosed.y].visited = true;
int xChar = chosed.x*2, yChar = chosed.y*2;
charMaze[xChar][yChar] = '-';
if (chosed.y > current.y) // cima
charMaze[xChar][yChar-1] = '-';
if (chosed.y < current.y) // baixo
charMaze[xChar][yChar+1] = '-';
if (chosed.x > current.x) // direita
charMaze[xChar-1][yChar] = '-';
if (chosed.x < current.x) // esquerda
charMaze[xChar+1][yChar] = '-';
buildMapDFS();
}
bool MapGenerator::allVisited() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (map[i][j].visited == false) {
return false;
}
}
}
return true;
}
Cell* MapGenerator::getMovementOptions(Cell atual) {
Cell* options = NULL;
tot_options = 0;
if (atual.y + 1 < height) { // Cima
if (map[atual.x][atual.y + 1].visited == false) {
options = (Cell*) realloc(options, (++tot_options) * sizeof(Cell));
options[tot_options - 1] = map[atual.x][atual.y + 1];
}
}
if (atual.y - 1 > -1) { // Baixo
if (map[atual.x][atual.y - 1].visited == false) {
options = (Cell*) realloc(options, (++tot_options) * sizeof(Cell));
options[tot_options - 1] = map[atual.x][atual.y - 1];
}
}
if (atual.x + 1 < width) { // Direita
if (map[atual.x + 1][atual.y].visited == false) {
options = (Cell*) realloc(options, (++tot_options) * sizeof(Cell));
options[tot_options - 1] = map[atual.x + 1][atual.y];
}
}
if (atual.x - 1 > -1) { // Esquerda
if (map[atual.x - 1][atual.y].visited == false) {
options = (Cell*) realloc(options, (++tot_options) * sizeof(Cell));
options[tot_options - 1] = map[atual.x - 1][atual.y];
}
}
return(options);
}
char** MapGenerator::getMatrixForOpenGL() {
if (openGLReadyMatrix != NULL)
return openGLReadyMatrix;
char** newMatrix = NULL;
newMatrix = (char**) malloc((height * 2 + 2) * sizeof(char*));
for (int i = 0; i < height * 2 + 1; i++) {
newMatrix[i] = (char*) malloc((width * 2 + 2) * sizeof(char));
if (i == 0 || i == height * 2) {
for (int j = 0; j < width * 2 + 1; j++)
newMatrix[i][j] = 'X';
newMatrix[i][(width * 2 + 1)] = '\0';
continue;
}
newMatrix[i][0] = 'X';
for (int j = 1; j < width * 2; j++) {
// GERA BURACOS
if (charMaze[i - 1][j - 1] == 'X') {
int random = rand() % 100;
if (random < 30) {
newMatrix[i][j] = 'B';
} else {
newMatrix[i][j] = charMaze[i - 1][j - 1];
}
} else {
newMatrix[i][j] = charMaze[i - 1][j - 1];
}
}
newMatrix[i][(width * 2)] = 'X';
newMatrix[i][(width * 2 + 1)] = '\0';
}
// GERA FIM
int random = rand() % height*2;
while (newMatrix[random][(width * 2)-1] != '-') {
random = rand() % height*2;
std::cout << "END " << newMatrix[random][(width * 2)-1] << std::endl;
}
std::cout << "END " << newMatrix[random][(width * 2)-1] << std::endl;
newMatrix[random][(width * 2)-1] = 'E';
openGLReadyMatrix = newMatrix;
return newMatrix;
}
void MapGenerator::printMazeMap() {
if (openGLReadyMatrix == NULL)
getMatrixForOpenGL();
for (int i = 0; i < height * 2 + 1; i++) {
for (int j = 0; j < width * 2 + 1; j++) {
std::cout << openGLReadyMatrix[i][j];
}
std::cout << '\n';
}
}