diff --git a/src/filter/bsp_interior.rs b/src/filter/bsp_interior.rs index b42f147..8999b9a 100644 --- a/src/filter/bsp_interior.rs +++ b/src/filter/bsp_interior.rs @@ -21,10 +21,10 @@ //! use rand::prelude::*; -use super::MapFilter; +use crate::MapFilter; use crate::geometry::{Point, Rect}; use crate::random::Rng; -use crate::map::Map; +use crate::Map; pub struct BspInterior { diff --git a/src/filter/bsp_rooms.rs b/src/filter/bsp_rooms.rs index 8c6fa91..4df3248 100644 --- a/src/filter/bsp_rooms.rs +++ b/src/filter/bsp_rooms.rs @@ -19,10 +19,10 @@ //! use rand::prelude::*; -use super::MapFilter; +use crate::MapFilter; use crate::geometry::Rect; use crate::random::Rng; -use crate::map::{Map, TileType}; +use crate::{Map, TileType}; pub struct BspRooms { diff --git a/src/filter/cellular_automata.rs b/src/filter/cellular_automata.rs index a7df128..2a8332a 100644 --- a/src/filter/cellular_automata.rs +++ b/src/filter/cellular_automata.rs @@ -22,8 +22,8 @@ //! use rand::prelude::*; -use super::MapFilter; -use crate::map::{Map, TileType}; +use crate::MapFilter; +use crate::{Map, TileType}; /// Map generator and modifier diff --git a/src/filter/cull_unreachable.rs b/src/filter/cull_unreachable.rs index 159f6c8..e5b1390 100644 --- a/src/filter/cull_unreachable.rs +++ b/src/filter/cull_unreachable.rs @@ -5,8 +5,8 @@ //! use rand::prelude::StdRng; -use super::MapFilter; -use crate::map::{Map, TileType}; +use crate::MapFilter; +use crate::{Map, TileType}; use crate::dijkstra::DijkstraMap; diff --git a/src/filter/distant_exit.rs b/src/filter/distant_exit.rs index 0aa659f..498d577 100644 --- a/src/filter/distant_exit.rs +++ b/src/filter/distant_exit.rs @@ -7,8 +7,8 @@ use std::f32; use rand::prelude::StdRng; use crate::geometry::Point; -use super::MapFilter; -use crate::map::Map; +use crate::MapFilter; +use crate::Map; use crate::dijkstra::DijkstraMap; diff --git a/src/filter/drunkard.rs b/src/filter/drunkard.rs index 607cc0c..3f028a6 100644 --- a/src/filter/drunkard.rs +++ b/src/filter/drunkard.rs @@ -14,7 +14,7 @@ //! use rand::prelude::*; -use super::MapFilter; +use crate::MapFilter; use crate::{ map::{Map, Symmetry, TileType}, geometry::Point, diff --git a/src/filter/mod.rs b/src/filter/mod.rs index 2b60d65..53e91bd 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -44,77 +44,5 @@ pub use distant_exit::DistantExit; pub use drunkard::DrunkardsWalk; pub use simple_rooms::SimpleRooms; pub use rooms_corridors_nearest::NearestCorridors; -pub use starting_point::AreaStartingPosition; +pub use starting_point::{AreaStartingPosition, XStart, YStart}; -use std::time::{SystemTime, UNIX_EPOCH}; -use rand::prelude::*; -use crate::map::Map; - - -/// Trait which should be implemented by map modifier. -/// Modifier takes initiall map and apply changes to it. -pub trait MapFilter { - fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map; -} - -/// Used to chain MapBuilder and MapModifiers to create the final map. -pub struct MapBuilder { - modifiers: Vec>, -} - -impl MapBuilder { - /// Create Map Builder with initial map generator - pub fn new() -> MapBuilder { - MapBuilder { - modifiers: Vec::new(), - } - } - - pub fn with(&mut self, modifier : Box) -> &mut MapBuilder { - self.modifiers.push(modifier); - self - } - - /// Build map using random number seeded with system time - pub fn build_map(&mut self, width: usize, height: usize) -> Map { - let system_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Can't access system time"); - let mut rng = StdRng::seed_from_u64(system_time.as_millis() as u64); - self.build_map_with_rng(width, height, &mut rng) - } - - /// Build map using provided random number generator - pub fn build_map_with_rng(&mut self, width: usize, height: usize, rng: &mut StdRng) -> Map { - let mut map = Map::new(width, height); - - // Build additional layers in turn - for modifier in self.modifiers.iter() { - map = modifier.modify_map(rng, &map); - } - - map - } - -} - -/// ------------------------------------------------------------------------------------------------ -/// Module unit tests -/// ------------------------------------------------------------------------------------------------ -#[cfg(test)] -mod tests { - use super::*; - use cellular_automata::CellularAutomata; - use starting_point::{AreaStartingPosition, XStart, YStart}; - - #[test] - fn test_ca_map() { - let map = MapBuilder::new() - .with(CellularAutomata::new()) - .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) - .build_map(80, 50); - - assert_eq!(map.width, 80); - assert_eq!(map.height, 50); - assert_eq!(map.starting_point.is_some(), true); - } - -} \ No newline at end of file diff --git a/src/filter/rooms_corridors_nearest.rs b/src/filter/rooms_corridors_nearest.rs index 0d92954..7bbe717 100644 --- a/src/filter/rooms_corridors_nearest.rs +++ b/src/filter/rooms_corridors_nearest.rs @@ -1,8 +1,8 @@ //! Connect nearest rooms on the map with corridors //! use rand::prelude::StdRng; -use super::MapFilter; -use crate::map::Map; +use crate::MapFilter; +use crate::Map; use std::collections::HashSet; diff --git a/src/filter/simple_rooms.rs b/src/filter/simple_rooms.rs index 24b7c4b..4335547 100644 --- a/src/filter/simple_rooms.rs +++ b/src/filter/simple_rooms.rs @@ -19,10 +19,10 @@ //! use rand::prelude::*; -use super::MapFilter; +use crate::MapFilter; use crate::geometry::Rect; use crate::random::Rng; -use crate::map::{Map}; +use crate::Map; pub struct SimpleRooms { diff --git a/src/filter/starting_point.rs b/src/filter/starting_point.rs index 672e58c..e087846 100644 --- a/src/filter/starting_point.rs +++ b/src/filter/starting_point.rs @@ -21,9 +21,9 @@ //! use rand::prelude::StdRng; -use super::MapFilter; +use crate::MapFilter; use crate::geometry::Point; -use crate::map::{Map, TileType}; +use crate::{Map, TileType}; /// Initial x region position diff --git a/src/lib.rs b/src/lib.rs index 64f5b56..ef4e10e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,108 @@ -//! A collection of map generators. +//! Generators for dungeon type maps. //! -//! The generators are implemented for the following types of maps: -//! * Dungeon maps +//! Generators can bu used directly or they can be combined with +//! `MapGenerator`s and `MapModifier`s //! +//! * MapGenerators are use to create initial map. +//! * MapModifiers modify existing map. +//! +//! Example +//! ``` +//! use mapgen::{MapFilter, MapBuilder, Map, TileType}; +//! use mapgen::filter::{ +//! CellularAutomata, +//! starting_point::{AreaStartingPosition, XStart, YStart} +//! }; +//! use mapgen::geometry::Point; +//! +//! let map = MapBuilder::new() +//! .with(CellularAutomata::new()) +//! .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) +//! .build_map(80, 50); +//! +//! assert_eq!(map.width, 80); +//! assert_eq!(map.height, 50); +//! assert_eq!(map.starting_point.is_some(), true); +//! ``` +//! pub mod filter; pub mod geometry; pub mod map; -pub use map::{Map, TileType}; -pub use filter::{MapFilter, MapBuilder}; +pub use map::{Map, Symmetry, TileType}; pub (crate) mod dijkstra; -pub (crate) mod random; \ No newline at end of file +pub (crate) mod random; + +use std::time::{SystemTime, UNIX_EPOCH}; +use rand::prelude::*; + + +/// Trait which should be implemented by map modifier. +/// Modifier takes initiall map and apply changes to it. +pub trait MapFilter { + fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map; +} + +/// Used to chain MapBuilder and MapModifiers to create the final map. +pub struct MapBuilder { + modifiers: Vec>, +} + +impl MapBuilder { + /// Create Map Builder with initial map generator + pub fn new() -> MapBuilder { + MapBuilder { + modifiers: Vec::new(), + } + } + + pub fn with(&mut self, modifier : Box) -> &mut MapBuilder { + self.modifiers.push(modifier); + self + } + + /// Build map using random number seeded with system time + pub fn build_map(&mut self, width: usize, height: usize) -> Map { + let system_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Can't access system time"); + let mut rng = StdRng::seed_from_u64(system_time.as_millis() as u64); + self.build_map_with_rng(width, height, &mut rng) + } + + /// Build map using provided random number generator + pub fn build_map_with_rng(&mut self, width: usize, height: usize, rng: &mut StdRng) -> Map { + let mut map = Map::new(width, height); + + // Build additional layers in turn + for modifier in self.modifiers.iter() { + map = modifier.modify_map(rng, &map); + } + + map + } + +} + +/// ------------------------------------------------------------------------------------------------ +/// Module unit tests +/// ------------------------------------------------------------------------------------------------ +#[cfg(test)] +mod tests { + use super::*; + use filter::CellularAutomata; + use filter::{AreaStartingPosition, XStart, YStart}; + + #[test] + fn test_ca_map() { + let map = MapBuilder::new() + .with(CellularAutomata::new()) + .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) + .build_map(80, 50); + + assert_eq!(map.width, 80); + assert_eq!(map.height, 50); + assert_eq!(map.starting_point.is_some(), true); + } + +} \ No newline at end of file