A dirty first cut at a single random horiz/vert split for the BSP algorithm.
This commit is contained in:
parent
6cb3366912
commit
62195e6eea
3 changed files with 83 additions and 33 deletions
72
map.cpp
72
map.cpp
|
@ -1,6 +1,8 @@
|
|||
#include "map.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include <vector>
|
||||
#include <fmt/core.h>
|
||||
#include <random>
|
||||
|
||||
using std::vector, std::pair;
|
||||
using namespace fmt;
|
||||
|
@ -86,20 +88,72 @@ void Map::make_paths() {
|
|||
m_paths = new_arr;
|
||||
}
|
||||
|
||||
void Map::make_room(size_t origin_x, size_t origin_y, size_t width, size_t height) {
|
||||
for(size_t y = origin_y; y < origin_y + height; ++y) {
|
||||
for(size_t x = origin_x; x < origin_x + width; ++x) {
|
||||
void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
|
||||
dbc::check(origin_x < width(), "x out of bounds");
|
||||
dbc::check(origin_y < height(), "x out of bounds");
|
||||
dbc::check(w < width(), "x out of bounds");
|
||||
dbc::check(h < height(), "x out of bounds");
|
||||
|
||||
for(size_t y = origin_y; y < origin_y + h; ++y) {
|
||||
for(size_t x = origin_x; x < origin_x + w; ++x) {
|
||||
m_walls[y][x] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Map::generate() {
|
||||
size_t w = width();
|
||||
size_t h = height();
|
||||
struct Partition;
|
||||
|
||||
size_t half_w = w / 2;
|
||||
size_t half_h = h / 2;
|
||||
struct Partition {
|
||||
size_t x = 0;
|
||||
size_t y = 0;
|
||||
size_t width = 0;
|
||||
size_t height = 0;
|
||||
|
||||
make_room(half_w - 5, half_h - 5, 5, 5);
|
||||
std::vector<Partition> children;
|
||||
};
|
||||
|
||||
inline int make_split(std::mt19937 &gen, Partition &cur, bool horiz) {
|
||||
if(horiz) {
|
||||
size_t quarter = cur.height / 4;
|
||||
// vertical split, pick a random horizontal location
|
||||
std::uniform_int_distribution<int> rhoriz(cur.y + quarter, cur.height - quarter);
|
||||
return rhoriz(gen);
|
||||
} else {
|
||||
size_t quarter = cur.width / 4;
|
||||
// horizontal split, pick a random vertical location
|
||||
std::uniform_int_distribution<int> rvert(cur.x + quarter, cur.width - quarter);
|
||||
return rvert(gen);
|
||||
}
|
||||
}
|
||||
|
||||
void Map::generate() {
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<int> rsplit(0, 1);
|
||||
|
||||
Partition root{
|
||||
.x = 1,
|
||||
.y = 1,
|
||||
.width = width() - 2,
|
||||
.height = height() - 2
|
||||
};
|
||||
|
||||
Partition &cur = root;
|
||||
bool horiz = rsplit(gen);
|
||||
|
||||
for(int i = 0; i < 1; i++) {
|
||||
int split = make_split(gen, cur, horiz);
|
||||
|
||||
if(horiz) {
|
||||
println("HORIZ split={}, x={}, y={}, w={}, h={}", split, cur.x, cur.y, cur.width, cur.height);
|
||||
|
||||
make_room(cur.x, cur.y, cur.width, split - 1);
|
||||
make_room(cur.x, cur.y + split, cur.width, cur.height - split);
|
||||
} else {
|
||||
println("VERT split={}, x={}, y={}, w={}, h={}", split, cur.x, cur.y, cur.width, cur.height);
|
||||
make_room(cur.x, cur.y, split-1, cur.height);
|
||||
make_room(cur.x + split, cur.y, cur.width - split, cur.height);
|
||||
}
|
||||
horiz = !horiz;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue