summaryrefslogtreecommitdiff
path: root/dungeon/src/bsp.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dungeon/src/bsp.rs')
-rw-r--r--dungeon/src/bsp.rs33
1 files changed, 18 insertions, 15 deletions
diff --git a/dungeon/src/bsp.rs b/dungeon/src/bsp.rs
index 971f5fe..6fb2e51 100644
--- a/dungeon/src/bsp.rs
+++ b/dungeon/src/bsp.rs
@@ -2,13 +2,16 @@
//! Produces a tile array and a player start position for a given seed.
use core::panic;
+use rand::Rng;
use rand::prelude::IndexedRandom;
-use rand::{Rng, rngs::SmallRng};
use std::cmp::{self, Ordering}; // for min/max
-use crate::Floor;
-use crate::map::{MAP_SIZE, TILE_COUNT, Tile};
-use crate::{const_pos, pos::Pos};
+use crate::{
+ const_pos,
+ map::{Floor, MAP_SIZE, TILE_COUNT, Tile},
+ pos::Pos,
+ rng::DungeonRng,
+};
/// `MIN_LEAF_SIZE` is the minimum width/height for a leaf to be considered "splittable".
const MIN_LEAF_SIZE: u16 = 8;
@@ -40,7 +43,7 @@ impl Rect {
}
/// Returns a random point in this rectangle.
- fn random_point(self, rng: &mut SmallRng) -> Pos {
+ fn random_point(self, rng: &mut DungeonRng) -> Pos {
let rx = rng.random_range(self.x..(self.x + self.w));
let ry = rng.random_range(self.y..(self.y + self.h));
Pos::new(rx, ry).unwrap_or(self.center())
@@ -70,7 +73,7 @@ impl Node {
/// Try to split the node. Returns true if split happened.
/// Splitting is done either horizontally or vertically,
/// depending on the dimensions of the node.
- fn split(&mut self, rng: &mut SmallRng) -> bool {
+ fn split(&mut self, rng: &mut DungeonRng) -> bool {
// Already split
if self.left.is_some() || self.right.is_some() {
return false;
@@ -148,7 +151,7 @@ impl Node {
/// Create a room inside this node (called for leaves).
/// Room size and position chosen randomly.
- fn create_room(&mut self, rng: &mut SmallRng) {
+ fn create_room(&mut self, rng: &mut DungeonRng) {
if self.left.is_some() || self.right.is_some() {
// This is not a leaf
if let Some(left) = &mut self.left {
@@ -211,7 +214,7 @@ impl Node {
/// Return a random point for a room in this subtree:
/// if node has a room, return a randiom point in it, else try left then right.
- fn random_point_in_room(&self, rng: &mut SmallRng) -> Pos {
+ fn random_point_in_room(&self, rng: &mut DungeonRng) -> Pos {
// Base case: if this node has a room, return a random point in it
if let Some(room) = &self.room {
return room.random_point(rng);
@@ -228,7 +231,7 @@ impl Node {
/// Connect rooms of child nodes recursively and collect corridors to carve.
/// returns corridors: output vector of (Pos, Pos) pairs to connect
- fn connect_children(&self, rng: &mut SmallRng) -> Vec<(Pos, Pos)> {
+ fn connect_children(&self, rng: &mut DungeonRng) -> Vec<(Pos, Pos)> {
let mut corridors = Vec::new();
if let (Some(left), Some(right)) = (&self.left, &self.right) {
@@ -286,7 +289,7 @@ fn carve_v_corridor(tiles: &mut [Tile; TILE_COUNT], y1: u16, y2: u16, x: u16) {
/// Top-level generator function for the dungeon using BSP.
/// Returns a `Floor`
-pub fn generate(rng: &mut SmallRng) -> Floor {
+pub fn generate(rng: &mut DungeonRng) -> Floor {
// Initialize all tiles to walls
let mut tiles_box: Box<[Tile; TILE_COUNT]> = Box::new([Tile::Wall; TILE_COUNT]);
@@ -413,7 +416,7 @@ mod tests {
fn test_node_split() {
let rect = Rect::new(0, 0, 20, 20);
let mut node = Node::new(rect);
- let mut rng = SmallRng::seed_from_u64(12345);
+ let mut rng = DungeonRng::seed_from_u64(12345);
let splitted = node.split(&mut rng);
assert!(splitted);
assert!(node.left.is_some());
@@ -424,7 +427,7 @@ mod tests {
fn test_node_create_room() {
let rect = Rect::new(0, 0, 20, 20);
let mut node = Node::new(rect);
- let mut rng = SmallRng::seed_from_u64(12345);
+ let mut rng = DungeonRng::seed_from_u64(12345);
node.create_room(&mut rng);
assert!(node.room.is_some());
match &node.room {
@@ -442,7 +445,7 @@ mod tests {
fn test_node_collect_leaves() {
let rect = Rect::new(0, 0, 20, 20);
let mut node = Node::new(rect);
- let mut rng = SmallRng::seed_from_u64(12345);
+ let mut rng = DungeonRng::seed_from_u64(12345);
node.split(&mut rng);
let mut leaves = Vec::new();
node.collect_leaves(&mut leaves);
@@ -453,7 +456,7 @@ mod tests {
fn test_node_connect_children() {
let rect = Rect::new(0, 0, 20, 20);
let mut node = Node::new(rect);
- let mut rng = SmallRng::seed_from_u64(12345);
+ let mut rng = DungeonRng::seed_from_u64(12345);
node.split(&mut rng);
node.create_room(&mut rng);
let corridors = node.connect_children(&mut rng);
@@ -463,7 +466,7 @@ mod tests {
#[test]
fn test_generate() {
let seed = 12345u64;
- let mut rng = SmallRng::seed_from_u64(seed);
+ let mut rng = DungeonRng::seed_from_u64(seed);
let floor = generate(&mut rng);
// Check that tiles contain some Room tiles
let room_count = floor.tiles().iter().filter(|&&t| t == Tile::Room).count();