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
40
main.cpp
40
main.cpp
|
@ -17,31 +17,21 @@
|
|||
#include "dbc.hpp"
|
||||
|
||||
using std::string;
|
||||
using namespace fmt;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
Matrix input = {
|
||||
{1,1,1,1,1,1,1,1,1,1,1,1},
|
||||
{1,1,1,1,1,1,1,1,1,1,1,1},
|
||||
{1,1,1,0,1,1,1,1,1,1,1,1},
|
||||
{1,1,1,1,1,1,1,1,1,1,1,1},
|
||||
{1,1,1,1,1,1,1,1,1,1,1,1},
|
||||
};
|
||||
|
||||
Matrix walls = {
|
||||
{1,1,1,1,1,1,1,1,1,1,1,1},
|
||||
{1,0,0,0,0,1,1,0,0,0,0,1},
|
||||
{1,0,0,0,0,0,0,0,0,1,0,1},
|
||||
{1,0,0,0,0,1,1,0,0,1,0,1},
|
||||
{1,1,1,1,1,1,1,1,1,1,1,1},
|
||||
};
|
||||
|
||||
int main() {
|
||||
using namespace ftxui;
|
||||
std::string reset_position;
|
||||
|
||||
auto c = Canvas(150, 100);
|
||||
|
||||
Map game_map = Map(input, walls, 1000);
|
||||
Map game_map(50, 20);
|
||||
game_map.generate();
|
||||
Matrix &input_map = game_map.input_map();
|
||||
Matrix &walls = game_map.walls();
|
||||
|
||||
input_map[10][10] = 0;
|
||||
|
||||
auto map = Renderer([&] {
|
||||
game_map.make_paths();
|
||||
|
@ -49,15 +39,21 @@ int main() {
|
|||
|
||||
for(size_t x = 0; x < result[0].size(); ++x) {
|
||||
for(size_t y = 0; y < result.size(); ++y) {
|
||||
auto val = result[y][x];
|
||||
const string tile = val == 1000 ? "#" : fmt::format("{}", result[y][x]);
|
||||
|
||||
|
||||
auto path = result[y][x];
|
||||
if(path == 1000) {
|
||||
// it's a wall or unreachable, use the wall_map
|
||||
const string tile = walls[y][x] == 1 ? "#" : ".";
|
||||
c.DrawText(x*2, y*4, tile);
|
||||
} else {
|
||||
// it's a path number, show it
|
||||
const string tile = format("{}", path);
|
||||
c.DrawText(x*2, y*4, tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.DrawText(4*2, 3*4, "@", Color::Blue);
|
||||
// draw the character
|
||||
c.DrawText(10*2, 10*4, "@", Color::Blue);
|
||||
|
||||
return canvas(c) | border;
|
||||
});
|
||||
|
|
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ TEST_CASE("dijkstra algo test", "[map]") {
|
|||
}
|
||||
|
||||
TEST_CASE("bsp algo test", "[map]") {
|
||||
Map map(20, 20);
|
||||
Map map(50, 20);
|
||||
|
||||
map.generate();
|
||||
map.dump();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue