diff options
Diffstat (limited to 'dungeon/src/map.rs')
| -rw-r--r-- | dungeon/src/map.rs | 80 |
1 files changed, 13 insertions, 67 deletions
diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index 7a6cc45..b1c5f16 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -1,7 +1,7 @@ //! The `map` module contains structures of the dungeon game map //! including the current `Floor`, and map `Tile`. -use rand::{Rng, SeedableRng, TryRngCore, rngs::StdRng}; +use rand::{Rng, rngs::SmallRng}; use strum::IntoEnumIterator; use strum_macros::EnumIter; @@ -11,8 +11,7 @@ use std::{ hash::{DefaultHasher, Hash, Hasher}, }; -use crate::bsp; -use crate::{const_pos, pos::Pos}; +use crate::pos::Pos; /// `MAP_SIZE` is the size of the size of the dungeon grid. pub const MAP_SIZE: u16 = 48; @@ -71,7 +70,7 @@ impl Display for Tile { /// The `Floor` type represents the current playing /// grid of the dungeon. It contains the tiles of the /// grid, and the starting position of the player. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Debug, Clone)] pub struct Floor { /// The dungeon grid tiles: Box<[Tile; TILE_COUNT]>, @@ -80,7 +79,7 @@ pub struct Floor { /// The seed used when generating the dungeon grid seed: u64, /// Seeded rng by `seed` - rng: StdRng, + rng: SmallRng, /// The computed hash of the tile map hash: RefCell<u64>, /// If the tiles are dirty (hash needs to be recomputed) @@ -92,7 +91,7 @@ impl Floor { tiles: Box<[Tile; TILE_COUNT]>, player_start: Pos, seed: u64, - rng: StdRng, + rng: SmallRng, ) -> Self { Self { tiles, @@ -104,43 +103,6 @@ impl Floor { } } - /// Generates a dungeon `Floor` using binary space partitioning. - /// - /// # Examples - /// - /// ```no_run - /// use dungeon::Floor; - /// - /// let floor = Floor::generate(); - /// ``` - #[must_use] - pub fn generate() -> Self { - let mut rng = rand::rngs::OsRng; - let seed = rng.try_next_u64().unwrap_or(0); - Self::generate_seeded(seed) - } - - /// Generates a dungeon `Floor` using binary space partitioning provided with a seed. - /// - /// The provided seed is used for randomness in the binary space partitioning - /// algorithm. - /// - /// # Examples - /// - /// ```no_run - /// use dungeon::Floor; - /// - /// /// here is our very seedy seed - /// let seed = 2893249402u64; - /// let floor_1 = Floor::generate_seeded(seed); - /// let floor_2 = Floor::generate_seeded(seed); - /// assert_eq!(floor_1, floor_2); // both floors will be identical - /// ``` - #[must_use] - pub fn generate_seeded(seed: u64) -> Self { - bsp::generate(seed) - } - /// Returns the start position of the player #[must_use] pub const fn player_start(&self) -> Pos { @@ -214,7 +176,7 @@ impl Floor { #[must_use] pub fn random_walkable_pos(&mut self) -> Pos { loop { - let pos = self.rand().random(); + let pos = self.rng().random(); if !self.get(pos).is_walkable() { continue; } @@ -224,35 +186,18 @@ impl Floor { /// Returns the random number gen for the `Floor` #[must_use] - pub fn rand(&mut self) -> &mut impl Rng { + pub fn rng(&mut self) -> &mut SmallRng { &mut self.rng } } -impl Default for Floor { - /// Returns a floor with a single set of walls on the map border - fn default() -> Self { - let player_start = const_pos!(1, 1); - let mut tiles = Box::new([Tile::Room; TILE_COUNT]); - let seed = 0u64; - - for pos in Pos::values() { - if pos.is_border() { - tiles[pos.idx()] = Tile::Wall; - } - } - - let rng = rand::rngs::StdRng::seed_from_u64(seed); - - Self::from_parts(tiles, player_start, seed, rng) - } -} impl Display for Floor { /// Display the floor as a string for debugging /// /// # Examples /// ```no_run - /// use dungeon::Floor; - /// let floor = Floor::generate(); + /// use dungeon::Dungeon; + /// let dungeon = Dungeon::default(); + /// let floor = &dungeon.floor; /// println!("{floor}"); /// ``` fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -278,12 +223,13 @@ impl Display for Floor { /// Tests #[cfg(test)] mod tests { - use super::*; + use crate::Dungeon; // Test floor printing #[test] fn test_floor_display() { - let floor = Floor::generate(); + let dungeon = Dungeon::default(); + let floor = &dungeon.floor; // Print the display for visual inspection println!("{floor}"); } |