#include #include #include "rand.hpp" #include "constants.hpp" #include "maze.hpp" using std::string; using matrix::Matrix; inline size_t rand(size_t i, size_t j) { if(i < j) { return Random::uniform(i, j); } else if(j < i) { return Random::uniform(j, i); } else { return i; } } inline bool complete(Matrix& maze) { size_t width = matrix::width(maze); size_t height = matrix::height(maze); for(size_t row = 1; row < height; row += 2) { for(size_t col = 1; col < width; col += 2) { if(maze[row][col] != 0) return false; } } // dbc::sentinel("LOL it's complete eh?"); return true; } std::vector neighborsAB(Matrix& maze, Point on) { std::vector result; std::array points{{ {on.x, on.y - 2}, {on.x, on.y + 2}, {on.x - 2, on.y}, {on.x + 2, on.y} }}; for(auto point : points) { if(matrix::inbounds(maze, point.x, point.y)) { result.push_back(point); } } return result; } std::vector neighbors(Matrix& maze, Point on) { std::vector result; std::array points{{ {on.x, on.y - 2}, {on.x, on.y + 2}, {on.x - 2, on.y}, {on.x + 2, on.y} }}; for(auto point : points) { if(matrix::inbounds(maze, point.x, point.y)) { if(maze[point.y][point.x] == WALL_VALUE) { result.push_back(point); } } } return result; } inline std::pair find_coord(Matrix& maze) { size_t width = matrix::width(maze); size_t height = matrix::height(maze); for(size_t y = 1; y < height; y += 2) { for(size_t x = 1; x < width; x += 2) { if(maze[y][x] == WALL_VALUE) { auto found = neighborsAB(maze, {x, y}); for(auto point : found) { if(maze[point.y][point.x] == 0) { return {{x, y}, point}; } } } } } matrix::dump("BAD MAZE", maze); dbc::sentinel("failed to find coord?"); } void maze::hunt_and_kill(Matrix& maze, std::vector& rooms, std::vector& dead_ends) { matrix::assign(maze, WALL_VALUE); Point last_even{0,0}; for(auto& room : rooms) { if(room.x % 2 == 0 && room.y % 2 == 0) { last_even = {room.x, room.y}; } for(matrix::box it{maze, room.x, room.y, room.width}; it.next();) { maze[it.y][it.x] = 0; } } Point on{1,1}; while(!complete(maze)) { auto n = neighbors(maze, on); if(n.size() == 0) { dead_ends.push_back(on); auto t = find_coord(maze); on = t.first; maze[on.y][on.x] = 0; size_t row = (on.y + t.second.y) / 2; size_t col = (on.x + t.second.x) / 2; maze[row][col] = 0; } else { auto nb = n[rand(size_t(0), n.size() - 1)]; maze[nb.y][nb.x] = 0; size_t row = (nb.y + on.y) / 2; size_t col = (nb.x + on.x) / 2; maze[row][col] = 0; on = nb; } } for(auto at : dead_ends) { for(auto& room : rooms) { Point room_ul{room.x - room.width - 1, room.y - room.height - 1}; Point room_lr{room.x + room.width - 1, room.y + room.height - 1}; if(at.x >= room_ul.x && at.y >= room_ul.y && at.x <= room_lr.x && at.y <= room_lr.y) { for(matrix::compass it{maze, at.x, at.y}; it.next();) { if(maze[it.y][it.x] == 1) { maze[it.y][it.x] = 0; break; } } } } } }