aboutsummaryrefslogtreecommitdiff
path: root/frontend/generate_dungeon.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/generate_dungeon.cpp')
-rw-r--r--frontend/generate_dungeon.cpp23
1 files changed, 19 insertions, 4 deletions
diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp
index 5484b2c..6393abb 100644
--- a/frontend/generate_dungeon.cpp
+++ b/frontend/generate_dungeon.cpp
@@ -1,4 +1,4 @@
-#include <map>
+#include <algorithm>
#include <memory>
#include "backend/Dungeon.h"
@@ -12,6 +12,12 @@
using namespace std;
+static vector<Direction> shuffled_directions() {
+ vector<Direction> out = { NORTH, EAST, SOUTH, WEST };
+ shuffle(out.begin(), out.end(), RNG::get_engine());
+ return out;
+}
+
unique_ptr<Dungeon> generate_dungeon() {
unique_ptr<Dungeon> dungeon = make_unique<Dungeon>();
GameData & gd = GameData::get_instance();
@@ -54,10 +60,19 @@ unique_ptr<Dungeon> generate_dungeon() {
auto & temp = temp_map[index];
temp.location = location;
- for (Direction direction : DIRECTIONS) {
- if (rng.rand_double() < 0.5) continue;
- temp.edges[direction] = rng.rand_int(location_count);
+ for (Direction direction : shuffled_directions()) {
+ // skip over already connected edges
+ if (temp.edges[direction] >= 0) continue;
+
+ // find another random location that is NOT here
+ int connection = rng.rand_int(location_count - 1);
+ if (connection == index) connection++;
+
+ // make a bidirectional connection
+ temp.edges[direction] = connection;
temp_map[temp.edges[direction]].edges[-direction] = index;
+
+ if (rng.rand_double() < 0.8) break;
}
dungeon->add_location(location);