in procgen/src/mazegen.cpp [112:187]
void MazeGen::generate_maze() {
for (int i = 0; i < array_dim; i++) {
for (int j = 0; j < array_dim; j++) {
grid.set(i, j, WALL_OBJ);
}
}
grid.set(MAZE_OFFSET, MAZE_OFFSET, 0);
std::vector<Wall> walls;
num_free_cells = 0;
free_cell_set.clear();
std::set<int> *s0 = &cell_sets[0];
s0->clear();
s0->insert(0);
cell_sets_idxs[0] = 0;
for (int i = 1; i < maze_dim * maze_dim; i++) {
std::set<int> *s1 = &cell_sets[i];
s1->clear();
s1->insert(i);
cell_sets_idxs[i] = i;
}
for (int i = 1; i < maze_dim; i += 2) {
for (int j = 0; j < maze_dim; j += 2) {
if (i > 0 && i < maze_dim - 1) {
walls.push_back(Wall({i - 1, j, i + 1, j}));
}
}
}
for (int i = 0; i < maze_dim; i += 2) {
for (int j = 1; j < maze_dim; j += 2) {
if (j > 0 && j < maze_dim - 1) {
walls.push_back(Wall({i, j - 1, i, j + 1}));
}
}
}
while (walls.size() > 0) {
int n = rand_gen->randn((int)(walls.size()));
Wall wall = walls[n];
int s0_idx = lookup(wall.x1, wall.y1);
s0 = &cell_sets[s0_idx];
int s1_idx = lookup(wall.x2, wall.y2);
std::set<int> *s1 = &cell_sets[s1_idx];
int x0 = (wall.x1 + wall.x2) / 2;
int y0 = (wall.y1 + wall.y2) / 2;
int center = maze_dim * y0 + x0;
bool can_remove =
(grid.get(x0 + MAZE_OFFSET, y0 + MAZE_OFFSET) == WALL_OBJ) &&
(s0_idx != s1_idx);
if (can_remove) {
set_free_cell(wall.x1, wall.y1);
set_free_cell(x0, y0);
set_free_cell(wall.x2, wall.y2);
s1->insert(s0->begin(), s0->end());
s1->insert(center);
std::set<int>::iterator it;
for (it = s1->begin(); it != s1->end(); ++it) {
cell_sets_idxs[*it] = s1_idx;
}
}
walls.erase(walls.begin() + n);
}
}