From b10095087713e597e7a13ed912744a5c668cd40d Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 28 Sep 2024 16:52:38 -0400 Subject: [PATCH] Move the random gen to a global for now. --- map.cpp | 21 +++++++++++---------- map.hpp | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/map.cpp b/map.cpp index 742ee75..c50c1b9 100644 --- a/map.cpp +++ b/map.cpp @@ -5,6 +5,10 @@ #include #include + +std::random_device g_rng; +std::mt19937 g_generator(g_rng()); + using std::vector, std::pair; using namespace fmt; @@ -105,7 +109,7 @@ void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) { } } -inline int make_split(std::mt19937 &gen, Room &cur, bool horiz) { +inline int make_split(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; @@ -113,10 +117,10 @@ inline int make_split(std::mt19937 &gen, Room &cur, bool horiz) { int max = dimension - min; println("dimension={}, min={}, max={}", dimension, min, max); std::uniform_int_distribution rand_dim(min, max); - return rand_dim(gen); + return rand_dim(g_generator); } -void Map::partition_map(std::mt19937 &gen, Room &cur, int depth) { +void Map::partition_map(Room &cur, int depth) { if(cur.width >= 5 && cur.width <= 10 && cur.height >= 5 && cur.height <= 10) { @@ -126,7 +130,7 @@ void Map::partition_map(std::mt19937 &gen, Room &cur, int depth) { std::uniform_int_distribution rsplit(0, 1); bool horiz = cur.width > cur.height ? false : true; - int split = make_split(gen, cur, horiz); + int split = make_split(cur, horiz); Room left = cur; Room right = cur; @@ -146,11 +150,11 @@ void Map::partition_map(std::mt19937 &gen, Room &cur, int depth) { } if(depth > 0 && left.width > 5 && left.height > 5) { - partition_map(gen, left, depth-1); + partition_map(left, depth-1); } if(depth > 0 && right.width > 5 && right.height > 5) { - partition_map(gen, right, depth-1); + partition_map(right, depth-1); } } @@ -161,9 +165,6 @@ void Map::draw_map(Room &cur) { } void Map::generate() { - std::random_device rd; - std::mt19937 gen(rd()); - Room root{ .x = 0, .y = 0, @@ -171,7 +172,7 @@ void Map::generate() { .height = height() }; - partition_map(gen, root, 6); + partition_map(root, 6); draw_map(root); // left } diff --git a/map.hpp b/map.hpp index bb656cb..bb0fcaa 100644 --- a/map.hpp +++ b/map.hpp @@ -59,6 +59,6 @@ public: void generate(); void draw_map(Room &root); void make_paths(); - void partition_map(std::mt19937 &gen, Room &cur, int depth); + void partition_map(Room &cur, int depth); void dump(); };