The next little game in the series where I make a fancy rogue game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
roguish/map.cpp

240 lines
6.2 KiB

#include "map.hpp"
#include "dbc.hpp"
#include <vector>
#include <fmt/core.h>
#include <random>
#include <utility>
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(PairList &neighbors, Matrix &closed, size_t j, size_t i) {
size_t h = closed.size();
size_t w = closed[0].size();
vector<size_t> rows{j - 1, j, j + 1};
vector<size_t> cols{i - 1, i, i + 1};
for(auto row : rows) {
for(auto col : cols) {
if((0 <= row && row < h) &&
(0 <= col && col < w) &&
closed[row][col] == 0)
{
closed[row][col] = 1;
neighbors.push_back({.j=row, .i=col});
}
}
}
}
Map::Map(size_t width, size_t height) : m_limit(1000) {
m_walls = Matrix(height, MatrixRow(width, 1));
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;
PairList starting_pixels;
PairList open_pixels;
// First pass: Add starting pixels and put them in closed
for(size_t counter = 0; counter < h * w; counter++) {
size_t i = counter % w;
size_t j = counter / w;
if(m_input_map[j][i] == 0) {
new_arr[j][i] = 0;
closed[j][i] = 1;
starting_pixels.push_back({.j=j,.i=i});
}
}
// Second pass: Add border to open
for(auto sp : starting_pixels) {
add_neighbors(open_pixels, closed, sp.j, sp.i);
}
// 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) {
PairList next_open;
for(auto sp : open_pixels) {
new_arr[sp.j][sp.i] = counter;
add_neighbors(next_open, closed, sp.j, sp.i);
}
open_pixels = next_open;
}
// Last pass: flood last pixels
for(auto sp : open_pixels) {
new_arr[sp.j][sp.i] = counter;
}
m_paths = new_arr;
}
void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
println("MAKE ROOM x={}, y={}, w={}, h={}", origin_x, origin_y, w, 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] = 0;
}
}
}
struct Partition;
struct Partition {
size_t x = 0;
size_t y = 0;
size_t width = 0;
size_t height = 0;
std::vector<Partition> next;
};
inline int make_split(std::mt19937 &gen, Partition &cur, bool horiz) {
if(horiz) {
println("MAKE SPLIT HORIZ, y={}, y+h={}, h={}",
cur.y, cur.y + cur.height, cur.height);
size_t quarter = cur.height / 4;
// vertical split, pick a random horizontal location
std::uniform_int_distribution<int> rhoriz(cur.y + quarter, cur.y + cur.height - quarter);
return rhoriz(gen);
} else {
// horizontal split, pick a random vertical location
println("MAKE SPLIT VERT, x={}, x+w={}, w={}",
cur.x, cur.x + cur.width, cur.width);
size_t quarter = cur.width / 4;
std::uniform_int_distribution<int> rvert(cur.x + quarter, cur.x + cur.width - quarter);
return rvert(gen);
}
}
void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
std::uniform_int_distribution<int> rsplit(0, 1);
bool horiz = rsplit(gen);
int split = make_split(gen, cur, horiz);
Partition left;
Partition right;
if(horiz) {
println("HORIZ split={}, x={}, y={}, w={}, h={}",
split, cur.x, cur.y, cur.width, cur.height);
dbc::check(split > 0, "split is not > 0");
dbc::check(split < int(cur.y + cur.height), "split is too big!");
left = {
.x = cur.x,
.y = cur.y,
.width = cur.width,
.height = size_t(split - 1)
};
right = {
.x = cur.x,
.y = cur.y + split,
.width = cur.width,
.height = size_t(cur.y + cur.height - split)
};
} else {
println("VERT split={}, x={}, y={}, w={}, h={}", split, cur.x, cur.y, cur.width, cur.height);
dbc::check(split > 0, "split is not > 0");
dbc::check(split < int(cur.x + cur.width), "split is too big!");
left = {
.x = cur.x,
.y = cur.y,
.width = size_t(split-1),
.height = cur.height
};
right = {
.x = cur.x + split,
.y = cur.y,
.width = size_t(cur.width - split),
.height = cur.height
};
}
println("CUR NEXT SIZE={}", cur.next.size());
if(depth > 0 && left.width > 10 && left.height > 10) {
partition_map(gen, left, depth-1);
}
if(depth > 0 && right.width > 10 && right.height > 10) {
partition_map(gen, right, depth-1);
}
println("PUSH CHILD!");
cur.next.push_back(left);
println("PUSH CHILD!");
cur.next.push_back(right);
}
void draw_map(Map *map, Partition &root, Partition &cur) {
if(cur.x + cur.width <= map->width()
&& cur.y + cur.height <= map->height())
{
map->make_room(cur.x, cur.y, cur.width, cur.height);
if(cur.next.size() == 2) {
draw_map(map, root, cur.next[0]); // left
draw_map(map, root, cur.next[1]); // right
} else {
println("LEAF NODE NO CHILDREN");
}
} else {
println("ABORT in draw_map, x={}, y={}, w={}, h={}, map.w={}, map.h={}",
cur.x, cur.y, cur.width, cur.height, map->width(), map->height());
}
}
void Map::generate() {
std::random_device rd;
std::mt19937 gen(rd());
Partition root{
.x = 1,
.y = 1,
.width = width() - 2,
.height = height() - 2
};
partition_map(gen, root, 3);
println("ROOT LEFT HAS {} CHILD", root.next[0].next.size());
draw_map(this, root, root.next[0]); // left
println("ROOT RIGHT HAS {} CHILD", root.next[1].next.size());
draw_map(this, root, root.next[1]); // right
}