Move the random gen to a global for now.
This commit is contained in:
parent
a37a40d45f
commit
b100950877
2 changed files with 12 additions and 11 deletions
21
map.cpp
21
map.cpp
|
@ -5,6 +5,10 @@
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
|
std::random_device g_rng;
|
||||||
|
std::mt19937 g_generator(g_rng());
|
||||||
|
|
||||||
using std::vector, std::pair;
|
using std::vector, std::pair;
|
||||||
using namespace fmt;
|
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,
|
println("MAKE SPLIT horiz={}, y={}, w={}, h={}", horiz,
|
||||||
cur.y, cur.width, cur.height);
|
cur.y, cur.width, cur.height);
|
||||||
size_t dimension = horiz ? cur.height : cur.width;
|
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;
|
int max = dimension - min;
|
||||||
println("dimension={}, min={}, max={}", dimension, min, max);
|
println("dimension={}, min={}, max={}", dimension, min, max);
|
||||||
std::uniform_int_distribution<int> rand_dim(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 &&
|
if(cur.width >= 5 && cur.width <= 10 &&
|
||||||
cur.height >= 5 && cur.height <= 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);
|
std::uniform_int_distribution<int> rsplit(0, 1);
|
||||||
bool horiz = cur.width > cur.height ? false : true;
|
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 left = cur;
|
||||||
Room right = 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) {
|
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) {
|
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() {
|
void Map::generate() {
|
||||||
std::random_device rd;
|
|
||||||
std::mt19937 gen(rd());
|
|
||||||
|
|
||||||
Room root{
|
Room root{
|
||||||
.x = 0,
|
.x = 0,
|
||||||
.y = 0,
|
.y = 0,
|
||||||
|
@ -171,7 +172,7 @@ void Map::generate() {
|
||||||
.height = height()
|
.height = height()
|
||||||
};
|
};
|
||||||
|
|
||||||
partition_map(gen, root, 6);
|
partition_map(root, 6);
|
||||||
draw_map(root); // left
|
draw_map(root); // left
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
map.hpp
2
map.hpp
|
@ -59,6 +59,6 @@ public:
|
||||||
void generate();
|
void generate();
|
||||||
void draw_map(Room &root);
|
void draw_map(Room &root);
|
||||||
void make_paths();
|
void make_paths();
|
||||||
void partition_map(std::mt19937 &gen, Room &cur, int depth);
|
void partition_map(Room &cur, int depth);
|
||||||
void dump();
|
void dump();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue