|
|
|
#include "map.hpp"
|
|
|
|
#include "dbc.hpp"
|
|
|
|
#include <vector>
|
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <random>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
std::random_device g_rng;
|
|
|
|
std::mt19937 g_generator(g_rng());
|
|
|
|
|
|
|
|
using std::vector, std::pair;
|
|
|
|
using namespace fmt;
|
|
|
|
|
|
|
|
void dump_map(const std::string &msg, Matrix &map) {
|
|
|
|
println("----------------- {}", msg);
|
|
|
|
for(auto row : map) {
|
|
|
|
for(auto col : row) {
|
|
|
|
print("{} ", col);
|
|
|
|
}
|
|
|
|
print("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void add_neighbors(PointList &neighbors, Matrix &closed, size_t y, size_t x) {
|
|
|
|
size_t h = closed.size();
|
|
|
|
size_t w = closed[0].size();
|
|
|
|
vector<size_t> rows{y - 1, y, y + 1};
|
|
|
|
vector<size_t> cols{x - 1, x, x + 1};
|
|
|
|
|
|
|
|
for(size_t row : rows) {
|
|
|
|
for(size_t col : cols) {
|
|
|
|
if((0 <= row && row < h) &&
|
|
|
|
(0 <= col && col < w) &&
|
|
|
|
closed[row][col] == 0)
|
|
|
|
{
|
|
|
|
closed[row][col] = 1;
|
|
|
|
neighbors.push_back({.x=col, .y=row});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will create an _inverted_ map that you
|
|
|
|
* can run make_rooms and generate on. It will
|
|
|
|
* NOT be valid until you actually run generate.
|
|
|
|
*/
|
|
|
|
Map::Map(size_t width, size_t height) : m_limit(1000) {
|
|
|
|
m_walls = Matrix(height, MatrixRow(width, INV_WALL));
|
|
|
|
m_input_map = Matrix(height, MatrixRow(width, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::make_paths() {
|
|
|
|
size_t h = m_input_map.size();
|
|
|
|
size_t w = m_input_map[0].size();
|
|
|
|
|
|
|
|
// Initialize the new array with every pixel at limit distance
|
|
|
|
// NOTE: this is normally ones() * limit
|
|
|
|
int limit = m_limit == 0 ? h * w : m_limit;
|
|
|
|
Matrix new_arr = Matrix(h, MatrixRow(w, limit));
|
|
|
|
Matrix closed = m_walls;
|
|
|
|
PointList starting_pixels;
|
|
|
|
PointList open_pixels;
|
|
|
|
|
|
|
|
// First pass: Add starting pixels and put them in closed
|
|
|
|
for(size_t counter = 0; counter < h * w; counter++) {
|
|
|
|
size_t x = counter % w;
|
|
|
|
size_t y = counter / w;
|
|
|
|
if(m_input_map[y][x] == 0) {
|
|
|
|
new_arr[y][x] = 0;
|
|
|
|
closed[y][x] = 1;
|
|
|
|
starting_pixels.push_back({.x=x,.y=y});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass: Add border to open
|
|
|
|
for(auto sp : starting_pixels) {
|
|
|
|
add_neighbors(open_pixels, closed, sp.y, sp.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Third pass: Iterate filling in the open list
|
|
|
|
int counter = 1; // leave this here so it's available below
|
|
|
|
for(; counter < limit && !open_pixels.empty(); ++counter) {
|
|
|
|
PointList next_open;
|
|
|
|
for(auto sp : open_pixels) {
|
|
|
|
new_arr[sp.y][sp.x] = counter;
|
|
|
|
add_neighbors(next_open, closed, sp.y, sp.x);
|
|
|
|
}
|
|
|
|
open_pixels = next_open;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last pass: flood last pixels
|
|
|
|
for(auto sp : open_pixels) {
|
|
|
|
new_arr[sp.y][sp.x] = counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_paths = new_arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
|
|
|
|
dbc::pre("x out of bounds", origin_x < width());
|
|
|
|
dbc::pre("y out of bounds", origin_y < height());
|
|
|
|
dbc::pre("w out of bounds", w <= width());
|
|
|
|
dbc::pre("h out of bounds", h <= height());
|
|
|
|
|
|
|
|
for(size_t y = origin_y; y < origin_y + h; ++y) {
|
|
|
|
dbc::check(y < m_walls.size(), "y is out of bounds");
|
|
|
|
for(size_t x = origin_x; x < origin_x + w; ++x) {
|
|
|
|
dbc::check(x < m_walls[y].size(), "x is out of bounds");
|
|
|
|
m_walls[y][x] = INV_SPACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int make_split(Room &cur, bool horiz) {
|
|
|
|
size_t dimension = horiz ? cur.height : cur.width;
|
|
|
|
int min = dimension / 4;
|
|
|
|
int max = dimension - min;
|
|
|
|
std::uniform_int_distribution<int> rand_dim(min, max);
|
|
|
|
return rand_dim(g_generator);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::partition_map(Room &cur, int depth) {
|
|
|
|
if(cur.width >= 5 && cur.width <= 10 &&
|
|
|
|
cur.height >= 5 && cur.height <= 10) {
|
|
|
|
m_rooms.push_back(cur);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uniform_int_distribution<int> rsplit(0, 1);
|
|
|
|
bool horiz = cur.width > cur.height ? false : true;
|
|
|
|
int split = make_split(cur, horiz);
|
|
|
|
Room left = cur;
|
|
|
|
Room right = cur;
|
|
|
|
|
|
|
|
if(horiz) {
|
|
|
|
dbc::check(split > 0, "split is not > 0");
|
|
|
|
dbc::check(split < int(cur.height), "split is too big!");
|
|
|
|
left.height = size_t(split - 1);
|
|
|
|
right.y = cur.y + split;
|
|
|
|
right.height = size_t(cur.height - split);
|
|
|
|
} else {
|
|
|
|
dbc::check(split > 0, "split is not > 0");
|
|
|
|
dbc::check(split < int(cur.width), "split is too big!");
|
|
|
|
|
|
|
|
left.width = size_t(split-1);
|
|
|
|
right.x = cur.x + split,
|
|
|
|
right.width = size_t(cur.width - split);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(depth > 0 && left.width > 5 && left.height > 5) {
|
|
|
|
partition_map(left, depth-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(depth > 0 && right.width > 5 && right.height > 5) {
|
|
|
|
partition_map(right, depth-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::place_rooms(Room &cur) {
|
|
|
|
for(auto &cur : m_rooms) {
|
|
|
|
cur.x += 2;
|
|
|
|
cur.y += 2;
|
|
|
|
cur.width -= 4;
|
|
|
|
cur.height -= 4;
|
|
|
|
add_door(cur);
|
|
|
|
set_door(cur, INV_SPACE);
|
|
|
|
make_room(cur.x, cur.y, cur.width, cur.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Map::neighbors(Point &out, bool greater) {
|
|
|
|
std::array<Point, 4> dirs{{
|
|
|
|
{out.x,out.y-1},
|
|
|
|
{out.x+1,out.y},
|
|
|
|
{out.x,out.y+1},
|
|
|
|
{out.x-1,out.y}
|
|
|
|
}};
|
|
|
|
|
|
|
|
int zero_i = -1;
|
|
|
|
int cur = m_paths[out.y][out.x];
|
|
|
|
dbc::check(cur != 1000, "WRONG! start point is in a wall");
|
|
|
|
|
|
|
|
for(int i = 0; i < 4; ++i) {
|
|
|
|
Point dir = dirs[i];
|
|
|
|
int diff = inmap(dir.x, dir.y) ? cur - m_paths[dir.y][dir.x] : -1000;
|
|
|
|
|
|
|
|
if(diff == 1) {
|
|
|
|
out = {.x=dir.x, .y=dir.y};
|
|
|
|
return true;
|
|
|
|
} else if(diff == 0) {
|
|
|
|
zero_i = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(zero_i != -1) {
|
|
|
|
out = {.x=dirs[zero_i].x, .y=dirs[zero_i].y};
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Map::inmap(size_t x, size_t y) {
|
|
|
|
return x < width() && y < height();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::set_door(Room &room, int value) {
|
|
|
|
m_walls[room.entry.y][room.entry.x] = value;
|
|
|
|
m_walls[room.exit.y][room.exit.x] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::add_door(Room &room) {
|
|
|
|
room.entry.x = room.x;
|
|
|
|
room.entry.y = room.y-1;
|
|
|
|
room.exit.x = room.x + room.width - 1;
|
|
|
|
room.exit.y = room.y + room.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Map::walk(Point &src, Point &target) {
|
|
|
|
// this sets the target for the path
|
|
|
|
dbc::check(m_input_map[target.y][target.x] == 0, "target point not set to 0");
|
|
|
|
|
|
|
|
m_walls[src.y][src.x] = INV_WALL;
|
|
|
|
m_walls[target.y][target.x] = INV_WALL;
|
|
|
|
|
|
|
|
// for the walk this needs to be walls since it's inverted?
|
|
|
|
dbc::check(m_walls[src.y][src.x] == INV_WALL,
|
|
|
|
"src room has a wall at exit door");
|
|
|
|
dbc::check(m_walls[target.y][target.x] == INV_WALL,
|
|
|
|
"target room has a wall at entry door");
|
|
|
|
|
|
|
|
make_paths();
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
Point out{src.x, src.y};
|
|
|
|
|
|
|
|
do {
|
|
|
|
m_walls[out.y][out.x] = INV_SPACE;
|
|
|
|
found = neighbors(out, true);
|
|
|
|
|
|
|
|
if(m_paths[out.y][out.x] == 0) {
|
|
|
|
m_walls[out.y][out.x] = INV_SPACE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while(found && out.x > 0 && out.y > 0);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::set_target(Point &at, int value) {
|
|
|
|
m_input_map[at.y][at.x] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::clear_target(Point &at) {
|
|
|
|
m_input_map[at.y][at.x] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Map::generate() {
|
|
|
|
Room root{
|
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
.width = width(),
|
|
|
|
.height = height()
|
|
|
|
};
|
|
|
|
|
|
|
|
partition_map(root, 10);
|
|
|
|
place_rooms(root);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < m_rooms.size() - 1; i++) {
|
|
|
|
Room &src = m_rooms[i];
|
|
|
|
Room &target = m_rooms[i+1];
|
|
|
|
set_target(target.entry);
|
|
|
|
bool found = walk(src.exit, target.entry);
|
|
|
|
if(!found) {
|
|
|
|
println("ROOM NOT FOUND!");
|
|
|
|
}
|
|
|
|
clear_target(target.entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Room &src = m_rooms.back();
|
|
|
|
Room &target = m_rooms.front();
|
|
|
|
|
|
|
|
set_target(target.entry);
|
|
|
|
walk(src.exit, target.entry);
|
|
|
|
clear_target(target.entry);
|
|
|
|
|
|
|
|
for(size_t y = 0; y < height(); ++y) {
|
|
|
|
for(size_t x = 0; x < width(); ++x) {
|
|
|
|
m_walls[y][x] = !m_walls[y][x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Map::iswall(size_t x, size_t y) {
|
|
|
|
return m_walls[y][x] == WALL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Map::dump() {
|
|
|
|
dump_map("PATHS", m_paths);
|
|
|
|
dump_map("WALLS", m_walls);
|
|
|
|
dump_map("INPUT", m_input_map);
|
|
|
|
}
|