It's working way better now, with correct internal dimensions, but I'm drawing the wrong nodes, need to go depth first.

main
Zed A. Shaw 2 months ago
parent 56cc38006b
commit 61d2747441
  1. 74
      map.cpp

@ -93,8 +93,8 @@ 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); println("MAKE ROOM x={}, y={}, w={}, h={}", origin_x, origin_y, w, h);
dbc::pre("x out of bounds", origin_x < width()); dbc::pre("x out of bounds", origin_x < width());
dbc::pre("y out of bounds", origin_y < height()); dbc::pre("y out of bounds", origin_y < height());
dbc::pre("w out of bounds", w < width()); dbc::pre("w out of bounds", w <= width());
dbc::pre("h out of bounds", h < height()); dbc::pre("h out of bounds", h <= height());
for(size_t y = origin_y; y < origin_y + h; ++y) { for(size_t y = origin_y; y < origin_y + h; ++y) {
dbc::check(y < m_walls.size(), "y is out of bounds"); dbc::check(y < m_walls.size(), "y is out of bounds");
@ -117,28 +117,18 @@ struct Partition {
}; };
inline int make_split(std::mt19937 &gen, Partition &cur, bool horiz) { inline int make_split(std::mt19937 &gen, Partition &cur, bool horiz) {
if(horiz) { println("MAKE SPLIT horiz={}, y={}, w={}, h={}", horiz,
println("MAKE SPLIT HORIZ, y={}, y+h={}, h={}", cur.y, cur.width, cur.height);
cur.y, cur.y + cur.height, cur.height); size_t dimension = horiz ? cur.height : cur.width;
int min = dimension / 4; // 25% of the dimension
size_t quarter = cur.height / 4; int max = dimension - min; // 25% off the other side
// vertical split, pick a random horizontal location println("dimension={}, min={}, max={}", dimension, min, max);
std::uniform_int_distribution<int> rhoriz(cur.y + quarter, cur.y + cur.height - quarter); std::uniform_int_distribution<int> rand_dim(min, max);
return rand_dim(gen);
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) { void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
println(">>>> DEPTH: {}", depth);
std::uniform_int_distribution<int> rsplit(0, 1); std::uniform_int_distribution<int> rsplit(0, 1);
bool horiz = rsplit(gen); bool horiz = rsplit(gen);
int split = make_split(gen, cur, horiz); int split = make_split(gen, cur, horiz);
@ -150,7 +140,7 @@ void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
split, cur.x, cur.y, cur.width, cur.height); split, cur.x, cur.y, cur.width, cur.height);
dbc::check(split > 0, "split is not > 0"); dbc::check(split > 0, "split is not > 0");
dbc::check(split < int(cur.y + cur.height), "split is too big!"); dbc::check(split < int(cur.height), "split is too big!");
left = { left = {
.x = cur.x, .x = cur.x,
@ -163,13 +153,13 @@ void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
.x = cur.x, .x = cur.x,
.y = cur.y + split, .y = cur.y + split,
.width = cur.width, .width = cur.width,
.height = size_t(cur.y + cur.height - split) .height = size_t(cur.height - split)
}; };
} else { } else {
println("VERT split={}, x={}, y={}, w={}, h={}", split, cur.x, cur.y, cur.width, cur.height); 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 > 0, "split is not > 0");
dbc::check(split < int(cur.x + cur.width), "split is too big!"); dbc::check(split < int(cur.width), "split is too big!");
left = { left = {
.x = cur.x, .x = cur.x,
@ -186,31 +176,28 @@ void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
}; };
} }
println("CUR NEXT SIZE={}", cur.next.size()); if(depth > 0 && left.width > 5 && left.height > 5) {
println("### LEFT h={}, w={}", left.width, left.height);
if(depth > 0 && left.width > 10 && left.height > 10) {
partition_map(gen, left, depth-1); partition_map(gen, left, depth-1);
cur.next.push_back(left);
} }
if(depth > 0 && right.width > 10 && right.height > 10) { if(depth > 0 && right.width > 5 && right.height > 5) {
println("### RIGHT h={}, w={}", right.width, right.height);
partition_map(gen, right, depth-1); partition_map(gen, right, depth-1);
}
println("PUSH CHILD!");
cur.next.push_back(left);
println("PUSH CHILD!");
cur.next.push_back(right); cur.next.push_back(right);
}
} }
void draw_map(Map *map, Partition &root, Partition &cur) { void draw_map(Map *map, Partition &cur) {
if(cur.x + cur.width <= map->width() if(cur.x + cur.width <= map->width()
&& cur.y + cur.height <= map->height()) && cur.y + cur.height <= map->height())
{ {
map->make_room(cur.x, cur.y, cur.width, cur.height); map->make_room(cur.x, cur.y, cur.width, cur.height);
if(cur.next.size() == 2) { if(cur.next.size() == 2) {
draw_map(map, root, cur.next[0]); // left draw_map(map, cur.next[0]); // left
draw_map(map, root, cur.next[1]); // right draw_map(map, cur.next[1]); // right
} else { } else {
println("LEAF NODE NO CHILDREN"); println("LEAF NODE NO CHILDREN");
} }
@ -225,15 +212,12 @@ void Map::generate() {
std::mt19937 gen(rd()); std::mt19937 gen(rd());
Partition root{ Partition root{
.x = 1, .x = 0,
.y = 1, .y = 0,
.width = width() - 2, .width = width(),
.height = height() - 2 .height = height()
}; };
partition_map(gen, root, 3); partition_map(gen, root, 5);
println("ROOT LEFT HAS {} CHILD", root.next[0].next.size()); draw_map(this, root); // left
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
} }

Loading…
Cancel
Save