From 56cc38006b12c45560c2c9fa8ab73bf2fc85322d Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 28 Sep 2024 02:44:36 -0400 Subject: [PATCH] NOTWORKING: Committing this so I can show the changes I made on the next stream. --- main.cpp | 2 +- map.cpp | 134 ++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 108 insertions(+), 28 deletions(-) diff --git a/main.cpp b/main.cpp index 634c5ab..858bfb0 100644 --- a/main.cpp +++ b/main.cpp @@ -55,7 +55,7 @@ int main() { // draw the character c.DrawText(10*2, 10*4, "@", Color::Blue); - return canvas(c) | border; + return canvas(c); }); while (true) { diff --git a/map.cpp b/map.cpp index 6955da8..4e0ca02 100644 --- a/map.cpp +++ b/map.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using std::vector, std::pair; using namespace fmt; @@ -89,13 +90,16 @@ void Map::make_paths() { } 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"); + 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; } } @@ -109,27 +113,116 @@ struct Partition { size_t width = 0; size_t height = 0; - std::vector children; + std::vector 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 rhoriz(cur.y + quarter, cur.height - quarter); + std::uniform_int_distribution rhoriz(cur.y + quarter, cur.y + cur.height - quarter); + return rhoriz(gen); } else { - size_t quarter = cur.width / 4; // horizontal split, pick a random vertical location - std::uniform_int_distribution rvert(cur.x + quarter, cur.width - quarter); + 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 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 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()); - std::uniform_int_distribution rsplit(0, 1); Partition root{ .x = 1, @@ -138,22 +231,9 @@ void Map::generate() { .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; - } + 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 }