Refactor into the class for more work.

main
Zed A. Shaw 2 months ago
parent 04b16c75ad
commit 44f11d5ddd
  1. 39
      map.cpp
  2. 40
      map.hpp

@ -105,18 +105,7 @@ void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
} }
} }
struct Partition; inline int make_split(std::mt19937 &gen, Room &cur, bool horiz) {
struct Partition {
size_t x = 0;
size_t y = 0;
size_t width = 0;
size_t height = 0;
std::vector<Partition> next;
};
inline int make_split(std::mt19937 &gen, Partition &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;
@ -127,13 +116,13 @@ inline int make_split(std::mt19937 &gen, Partition &cur, bool horiz) {
return rand_dim(gen); return rand_dim(gen);
} }
void partition_map(std::mt19937 &gen, Partition &cur, int depth) { void Map::partition_map(std::mt19937 &gen, Room &cur, int depth) {
println(">>>> DEPTH: {}", depth); println(">>>> DEPTH: {}", 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(gen, cur, horiz);
Partition left; Room left;
Partition right; Room right;
if(horiz) { if(horiz) {
println("HORIZ split={}, x={}, y={}, w={}, h={}", println("HORIZ split={}, x={}, y={}, w={}, h={}",
@ -193,23 +182,23 @@ void partition_map(std::mt19937 &gen, Partition &cur, int depth) {
} }
} }
void draw_map(Map *map, Partition &cur) { void Map::draw_map(Room &cur) {
if(cur.x + cur.width <= map->width() if(cur.x + cur.width <= width()
&& cur.y + cur.height <= map->height()) && cur.y + cur.height <= height())
{ {
println("CUR NEXT SIZE: {}", cur.next.size()); println("CUR NEXT SIZE: {}", cur.next.size());
if(cur.next.size() == 1) { if(cur.next.size() == 1) {
draw_map(map, cur.next[0]); // left draw_map(cur.next[0]); // left
} else if(cur.next.size() == 2) { } else if(cur.next.size() == 2) {
draw_map(map, cur.next[0]); // left draw_map(cur.next[0]); // left
draw_map(map, cur.next[1]); // right draw_map(cur.next[1]); // right
} else { } else {
println("LEAF NODE NO CHILDREN x={}, y={}, w={}, h={}", cur.x, cur.y, cur.width, cur.height); println("LEAF NODE NO CHILDREN x={}, y={}, w={}, h={}", cur.x, cur.y, cur.width, cur.height);
map->make_room(cur.x+1, cur.y+1, cur.width-2, cur.height-2); make_room(cur.x+1, cur.y+1, cur.width-2, cur.height-2);
} }
} else { } else {
println("ABORT in draw_map, x={}, y={}, w={}, h={}, map.w={}, map.h={}", 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()); cur.x, cur.y, cur.width, cur.height, width(), height());
} }
} }
@ -217,7 +206,7 @@ void Map::generate() {
std::random_device rd; std::random_device rd;
std::mt19937 gen(rd()); std::mt19937 gen(rd());
Partition root{ Room root{
.x = 0, .x = 0,
.y = 0, .y = 0,
.width = width(), .width = width(),
@ -225,5 +214,5 @@ void Map::generate() {
}; };
partition_map(gen, root, 6); partition_map(gen, root, 6);
draw_map(this, root); // left draw_map(root); // left
} }

@ -2,12 +2,24 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
#include <string> #include <string>
#include <random>
struct Pair { struct Pair {
size_t j = 0; size_t j = 0;
size_t i = 0; size_t i = 0;
}; };
struct Room;
struct Room {
size_t x = 0;
size_t y = 0;
size_t width = 0;
size_t height = 0;
std::vector<Room> next;
};
typedef std::vector<Pair> PairList; typedef std::vector<Pair> PairList;
typedef std::vector<int> MatrixRow; typedef std::vector<int> MatrixRow;
typedef std::vector<MatrixRow> Matrix; typedef std::vector<MatrixRow> Matrix;
@ -22,7 +34,17 @@ class Map {
int m_limit = 0; int m_limit = 0;
public: public:
void make_paths(); // make explicit
Map(Matrix input_map, Matrix walls_map, int limit) :
m_input_map(input_map),
m_walls(walls_map), m_limit(limit) {
}
// make random
Map(size_t width, size_t height);
// disable copying
Map(Map &map) = delete;
Matrix& paths() { return m_paths; } Matrix& paths() { return m_paths; }
Matrix& input_map() { return m_input_map; } Matrix& input_map() { return m_input_map; }
@ -33,20 +55,14 @@ public:
void make_room(size_t origin_y, size_t origin_x, size_t width, size_t height); void make_room(size_t origin_y, size_t origin_x, size_t width, size_t height);
void generate();
void draw_map(Room &root);
void make_paths();
void partition_map(std::mt19937 &gen, Room &cur, int depth);
void dump() { void dump() {
dump_map("PATHS", m_paths); dump_map("PATHS", m_paths);
dump_map("WALLS", m_walls); dump_map("WALLS", m_walls);
dump_map("INPUT", m_input_map); dump_map("INPUT", m_input_map);
} }
void generate();
Map(Matrix input_map, Matrix walls_map, int limit) :
m_input_map(input_map),
m_walls(walls_map), m_limit(limit) {
}
Map(size_t width, size_t height);
Map(Map &map) = delete;
}; };

Loading…
Cancel
Save