here_be_dragons/src/lib.rs

120 lines
3.4 KiB
Rust
Raw Normal View History

2020-09-22 18:44:54 +00:00
//! Generators for dungeon type maps.
2022-03-12 20:31:30 +00:00
//!
2020-09-22 18:44:54 +00:00
//! Generators can bu used directly or they can be combined with
//! `MapGenerator`s and `MapModifier`s
2022-03-12 20:31:30 +00:00
//!
2020-09-22 18:44:54 +00:00
//! * MapGenerators are use to create initial map.
//! * MapModifiers modify existing map.
2022-03-12 20:31:30 +00:00
//!
2020-09-22 18:44:54 +00:00
//! Example
//! ```
//! use mapgen::{MapFilter, MapBuilder, Map, NoData, Tile};
2020-09-22 18:44:54 +00:00
//! use mapgen::filter::{
2020-09-23 08:31:13 +00:00
//! NoiseGenerator,
2020-09-22 18:44:54 +00:00
//! CellularAutomata,
//! starting_point::{AreaStartingPosition, XStart, YStart}
//! };
//! use mapgen::geometry::Point;
2022-03-12 20:31:30 +00:00
//!
//! let map = MapBuilder::<NoData>::new(80, 50)
2020-09-23 08:31:13 +00:00
//! .with(NoiseGenerator::uniform())
2020-09-22 18:44:54 +00:00
//! .with(CellularAutomata::new())
//! .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
2020-09-25 13:09:08 +00:00
//! .build();
2022-03-12 20:31:30 +00:00
//!
2020-09-22 18:44:54 +00:00
//! assert_eq!(map.width, 80);
//! assert_eq!(map.height, 50);
//! assert_eq!(map.starting_point.is_some(), true);
//! ```
//!
2022-03-12 20:31:30 +00:00
2020-09-22 18:38:37 +00:00
pub mod filter;
2020-09-16 07:57:59 +00:00
pub mod geometry;
pub mod map;
2021-01-14 11:18:31 +00:00
pub mod metric;
2020-09-16 07:57:59 +00:00
2020-09-23 08:31:13 +00:00
pub use filter::*;
2022-03-14 02:14:59 +00:00
pub use map::{Map, NoData, Symmetry, Tile};
2020-09-22 18:38:37 +00:00
2022-03-12 20:31:30 +00:00
pub(crate) mod dijkstra;
pub(crate) mod random;
2020-09-22 18:44:54 +00:00
use rand::prelude::*;
2022-03-12 20:31:30 +00:00
use std::time::{SystemTime, UNIX_EPOCH};
2020-09-22 18:44:54 +00:00
2022-03-12 20:31:30 +00:00
/// Trait which should be implemented by map modifier.
2020-09-22 18:44:54 +00:00
/// Modifier takes initiall map and apply changes to it.
2022-03-14 02:14:59 +00:00
pub trait MapFilter<D: Clone + Default> {
fn modify_map(&self, rng: &mut StdRng, map: &Map<D>) -> Map<D>;
2020-09-22 18:44:54 +00:00
}
/// Used to chain MapBuilder and MapModifiers to create the final map.
2022-03-14 02:14:59 +00:00
pub struct MapBuilder<D: Clone + Default> {
2020-09-25 13:09:08 +00:00
width: usize,
height: usize,
modifiers: Vec<Box<dyn MapFilter<D>>>,
2020-09-22 18:44:54 +00:00
}
2022-03-14 02:14:59 +00:00
impl<D: Clone + Default> MapBuilder<D> {
2020-09-22 18:44:54 +00:00
/// Create Map Builder with initial map generator
pub fn new(width: usize, height: usize) -> MapBuilder<D> {
2022-03-12 20:31:30 +00:00
MapBuilder {
2020-09-25 13:09:08 +00:00
width,
height,
2020-09-22 18:44:54 +00:00
modifiers: Vec::new(),
}
}
2022-03-12 20:31:30 +00:00
pub fn with(&mut self, modifier: Box<dyn MapFilter<D>>) -> &mut MapBuilder<D> {
2020-09-22 18:44:54 +00:00
self.modifiers.push(modifier);
self
}
/// Build map using random number seeded with system time
pub fn build(&mut self) -> Map<D> {
2022-03-12 20:31:30 +00:00
let system_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Can't access system time");
2020-09-22 18:44:54 +00:00
let mut rng = StdRng::seed_from_u64(system_time.as_millis() as u64);
2020-09-25 13:09:08 +00:00
self.build_with_rng(&mut rng)
2020-09-22 18:44:54 +00:00
}
/// Build map using provided random number generator
pub fn build_with_rng(&mut self, rng: &mut StdRng) -> Map<D> {
2020-09-25 13:09:08 +00:00
let mut map = Map::new(self.width, self.height);
2022-03-12 20:31:30 +00:00
2020-09-22 18:44:54 +00:00
// 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 crate::map::NoData;
2020-09-22 18:44:54 +00:00
use super::*;
2020-09-23 08:31:13 +00:00
use filter::{
2022-03-12 20:31:30 +00:00
CellularAutomata, NoiseGenerator, {AreaStartingPosition, XStart, YStart},
2020-09-23 08:31:13 +00:00
};
2020-09-22 18:44:54 +00:00
#[test]
fn test_ca_map() {
let map = MapBuilder::<NoData>::new(80, 50)
2020-09-23 08:31:13 +00:00
.with(NoiseGenerator::new(0.55))
2020-09-22 18:44:54 +00:00
.with(CellularAutomata::new())
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
2020-09-25 13:09:08 +00:00
.build();
2020-09-22 18:44:54 +00:00
assert_eq!(map.width, 80);
assert_eq!(map.height, 50);
2022-03-14 16:46:48 +00:00
assert!(map.starting_point.is_some());
2020-09-22 18:44:54 +00:00
}
2022-03-12 20:31:30 +00:00
}