Refactor into the class for more work.
This commit is contained in:
parent
04b16c75ad
commit
44f11d5ddd
2 changed files with 42 additions and 37 deletions
39
map.cpp
39
map.cpp
|
@ -105,18 +105,7 @@ void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
|
|||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
inline int make_split(std::mt19937 &gen, Room &cur, bool horiz) {
|
||||
println("MAKE SPLIT horiz={}, y={}, w={}, h={}", horiz,
|
||||
cur.y, cur.width, cur.height);
|
||||
size_t dimension = horiz ? cur.height : cur.width;
|
||||
|
@ -127,13 +116,13 @@ inline int make_split(std::mt19937 &gen, Partition &cur, bool horiz) {
|
|||
return rand_dim(gen);
|
||||
}
|
||||
|
||||
void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
|
||||
void Map::partition_map(std::mt19937 &gen, Room &cur, int depth) {
|
||||
println(">>>> DEPTH: {}", depth);
|
||||
std::uniform_int_distribution<int> rsplit(0, 1);
|
||||
bool horiz = cur.width > cur.height ? false : true;
|
||||
int split = make_split(gen, cur, horiz);
|
||||
Partition left;
|
||||
Partition right;
|
||||
Room left;
|
||||
Room right;
|
||||
|
||||
if(horiz) {
|
||||
println("HORIZ split={}, x={}, y={}, w={}, h={}",
|
||||
|
@ -193,23 +182,23 @@ void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
|
|||
}
|
||||
}
|
||||
|
||||
void draw_map(Map *map, Partition &cur) {
|
||||
if(cur.x + cur.width <= map->width()
|
||||
&& cur.y + cur.height <= map->height())
|
||||
void Map::draw_map(Room &cur) {
|
||||
if(cur.x + cur.width <= width()
|
||||
&& cur.y + cur.height <= height())
|
||||
{
|
||||
println("CUR NEXT SIZE: {}", cur.next.size());
|
||||
if(cur.next.size() == 1) {
|
||||
draw_map(map, cur.next[0]); // left
|
||||
draw_map(cur.next[0]); // left
|
||||
} else if(cur.next.size() == 2) {
|
||||
draw_map(map, cur.next[0]); // left
|
||||
draw_map(map, cur.next[1]); // right
|
||||
draw_map(cur.next[0]); // left
|
||||
draw_map(cur.next[1]); // right
|
||||
} else {
|
||||
println("LEAF NODE NO CHILDREN x={}, y={}, w={}, h={}", cur.x, cur.y, cur.width, cur.height);
|
||||
map->make_room(cur.x+1, cur.y+1, cur.width-2, cur.height-2);
|
||||
make_room(cur.x+1, cur.y+1, cur.width-2, cur.height-2);
|
||||
}
|
||||
} 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());
|
||||
cur.x, cur.y, cur.width, cur.height, width(), height());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -217,7 +206,7 @@ void Map::generate() {
|
|||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
|
||||
Partition root{
|
||||
Room root{
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.width = width(),
|
||||
|
@ -225,5 +214,5 @@ void Map::generate() {
|
|||
};
|
||||
|
||||
partition_map(gen, root, 6);
|
||||
draw_map(this, root); // left
|
||||
draw_map(root); // left
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue