Move the random gen to a global for now.

main
Zed A. Shaw 3 months ago
parent a37a40d45f
commit b100950877
  1. 21
      map.cpp
  2. 2
      map.hpp

@ -5,6 +5,10 @@
#include <random>
#include <utility>
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<int> 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<int> 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
}

@ -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();
};

Loading…
Cancel
Save