summaryrefslogtreecommitdiff
path: root/dungeon/src/map.rs
diff options
context:
space:
mode:
authorAudrey L Fuller <alf9310@g.rit.edu>2025-10-30 17:16:55 +0000
committerAudrey L Fuller <alf9310@g.rit.edu>2025-10-30 17:16:55 +0000
commitbe95ac14a8ca62c505028707bb9be1b3c71c5455 (patch)
tree9ce380df206cb456e3c62d7e50e170e699a3e1e6 /dungeon/src/map.rs
parentgraphics: add docs to AtlasTexture (diff)
downloadDungeonCrawl-be95ac14a8ca62c505028707bb9be1b3c71c5455.tar.gz
DungeonCrawl-be95ac14a8ca62c505028707bb9be1b3c71c5455.tar.bz2
DungeonCrawl-be95ac14a8ca62c505028707bb9be1b3c71c5455.zip
Wave function collapse
Diffstat (limited to 'dungeon/src/map.rs')
-rw-r--r--dungeon/src/map.rs72
1 files changed, 60 insertions, 12 deletions
diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs
index d185547..f4a8729 100644
--- a/dungeon/src/map.rs
+++ b/dungeon/src/map.rs
@@ -4,16 +4,16 @@
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
-use crate::wfc::Wfc;
use std::{
cell::RefCell,
hash::{DefaultHasher, Hash, Hasher},
};
+use crate::bsp;
use crate::{const_pos, pos::Pos};
/// `MAP_SIZE` is the size of the size of the dungeon grid.
-pub const MAP_SIZE: u16 = 100;
+pub const MAP_SIZE: u16 = 48;
/// `MAP_SIZE` as a usize
pub const MAP_SIZE_USIZE: usize = MAP_SIZE as usize;
@@ -77,7 +77,7 @@ impl Floor {
}
}
- /// Generates a dungeon `Floor` using wave function collapse.
+ /// Generates a dungeon `Floor` using binary space partitioning.
///
/// # Examples
///
@@ -92,10 +92,10 @@ impl Floor {
Self::generate_seeded(seed)
}
- /// Genreates a dungeon `Floor` using wave function collapse provided with a seed.
+ /// Generates a dungeon `Floor` using binary space partitioning provided with a seed.
///
- /// The provided seed is used for randomness in the wave function
- /// collapse algorithm.
+ /// The provided seed is used for randomness in the binary space partitioning
+ /// algorithm.
///
/// # Examples
///
@@ -104,16 +104,15 @@ impl Floor {
///
/// /// here is our very seedy seed
/// let seed = 2893249402u64;
- /// let floor = Floor::generate_seeded(seed);
+ /// 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 {
- let mut wfc = Wfc::new(seed, MAP_SIZE);
- wfc.initialize_states();
- wfc.run();
+ let (tiles, player_start) = bsp::generate(seed);
- // TODO
- unimplemented!()
+ Self::new(tiles, player_start, seed)
}
/// Returns the start position of the player
@@ -178,7 +177,41 @@ impl Floor {
*dirty = false;
*hash
}
+
+ /// Display the floor as a string for debugging
+ ///
+ /// # Examples
+ /// ```no_run
+ /// use dungeon::Floor;
+ /// let floor = Floor::generate();
+ /// println!("{}", floor.display());
+ /// ```
+ #[must_use]
+ pub fn display(&self) -> String {
+ let mut output = String::new();
+ for pos in Pos::values() {
+ // If it's the player start, show 'P'
+ if self.player_start == pos {
+ output.push('P');
+ continue;
+ }
+ // Otherwise, show the tile character
+ let tile = self.get(pos);
+ let char = match tile {
+ Tile::Wall => '#',
+ Tile::Air => '.',
+ Tile::Stairs => '>',
+ };
+ output.push(char);
+ // Newline at the end of each row
+ if pos.xy().0 == MAP_SIZE - 1 {
+ output.push('\n');
+ }
+ }
+ output
+ }
}
+
impl Default for Floor {
/// Returns a floor with a single set of walls on the map border
fn default() -> Self {
@@ -195,3 +228,18 @@ impl Default for Floor {
Self::new(tiles, player_start, seed)
}
}
+
+/// Tests
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ // Test floor printing
+ #[test]
+ fn test_floor_display() {
+ let floor = Floor::generate();
+ let display = floor.display();
+ // Print the display for visual inspection
+ println!("{display}");
+ }
+}