diff options
Diffstat (limited to 'dungeon/src/bsp.rs')
| -rw-r--r-- | dungeon/src/bsp.rs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/dungeon/src/bsp.rs b/dungeon/src/bsp.rs index db9ff99..971f5fe 100644 --- a/dungeon/src/bsp.rs +++ b/dungeon/src/bsp.rs @@ -4,7 +4,7 @@ use core::panic; use rand::prelude::IndexedRandom; use rand::{Rng, rngs::SmallRng}; -use std::cmp; // for min/max +use std::cmp::{self, Ordering}; // for min/max use crate::Floor; use crate::map::{MAP_SIZE, TILE_COUNT, Tile}; @@ -28,19 +28,19 @@ struct Rect { } // since this is all "internal", you likely have to use unit tests and not doctests impl Rect { - fn new(x: u16, y: u16, w: u16, h: u16) -> Self { + const fn new(x: u16, y: u16, w: u16, h: u16) -> Self { Self { x, y, w, h } } /// Returns the center point (cx, cy) of the rectangle as a Pos. - fn center(&self) -> Pos { + fn center(self) -> Pos { let cx = self.x + self.w / 2; let cy = self.y + self.h / 2; Pos::new(cx, cy).unwrap_or(const_pos!(1, 1)) } /// Returns a random point in this rectangle. - fn random_point(&self, rng: &mut SmallRng) -> Pos { + fn random_point(self, rng: &mut SmallRng) -> 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()) @@ -58,7 +58,7 @@ struct Node { } impl Node { - fn new(rect: Rect) -> Self { + const fn new(rect: Rect) -> Self { Self { rect, left: None, @@ -85,12 +85,10 @@ impl Node { } // Choose orientation: prefer the longer side - let split_h = if self.rect.w > self.rect.h { - false - } else if self.rect.h > self.rect.w { - true - } else { - rng.random_bool(0.5) + let split_h = match self.rect.w.cmp(&self.rect.h) { + Ordering::Greater => false, + Ordering::Less => true, + Ordering::Equal => rng.random(), }; // Choose split coordinate with margin so each side can contain a room @@ -251,7 +249,7 @@ impl Node { } /// Carve a room rectangle into the map (tile array) by setting tiles inside to Room. -fn carve_room(tiles: &mut [Tile; TILE_COUNT], room: &Rect) { +fn carve_room(tiles: &mut [Tile; TILE_COUNT], room: Rect) { for y in room.y..(room.y + room.h) { for x in room.x..(room.x + room.w) { let idx = x + y * MAP_SIZE; @@ -327,7 +325,7 @@ pub fn generate(rng: &mut SmallRng) -> Floor { let mut splitted_any = false; // Try splitting each leaf in order - for node_ptr in leaves.iter_mut() { + for node_ptr in &mut leaves { // Attempt to split if possible if node_ptr.split(rng) { splitted_any = true; @@ -346,8 +344,8 @@ pub fn generate(rng: &mut SmallRng) -> Floor { // Carve all rooms into the tile array let mut leaves = vec![]; root.collect_leaves(&mut leaves); - for leaf in leaves.iter() { - if let Some(room) = &leaf.room { + for leaf in &leaves { + if let Some(room) = leaf.room { carve_room(&mut tiles_box, room); } } |