here_be_dragons/src/dungeon/mod.rs

106 lines
3.1 KiB
Rust
Raw Normal View History

2020-09-01 12:46:31 +00:00
//! Generators for dungeon type 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.
//!
2020-09-02 10:01:16 +00:00
//! Example
//! ```
//! use mapgen::dungeon::{
//! MapBuilder,
//! map::{Map, Point, TileType},
//! cellular_automata::CellularAutomataGen,
//! starting_point::{AreaStartingPosition, XStart, YStart},
//! };
//!
//! let map = MapBuilder::new(Box::new(CellularAutomataGen::new(80, 50)))
//! .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
//! .build_map();
//!
//! assert_eq!(map.width, 80);
//! assert_eq!(map.height, 50);
//! assert_eq!(map.starting_point.is_some(), true);
//! ```
//!
2020-09-01 12:46:31 +00:00
2020-08-31 09:45:59 +00:00
pub mod map;
2020-08-31 20:03:48 +00:00
pub mod cellular_automata;
2020-09-03 19:54:24 +00:00
pub mod cull_unreachable;
2020-09-02 10:01:16 +00:00
pub mod starting_point;
2020-09-03 19:54:24 +00:00
mod dijkstra;
2020-08-31 20:03:48 +00:00
2020-09-02 10:01:16 +00:00
use std::time::{SystemTime, UNIX_EPOCH};
2020-08-31 20:03:48 +00:00
use rand::prelude::*;
use map::Map;
2020-09-01 12:46:31 +00:00
/// Trait which should be implemented by any map generator which want to be used
/// by MapBuilder
2020-08-31 20:03:48 +00:00
pub trait MapGenerator {
fn generate_map(&self, rng: &mut StdRng) -> Map;
}
2020-09-01 12:46:31 +00:00
/// Trait which should be implemented by map modifier.
/// Modifier takes initiall map and apply changes to it.
2020-08-31 20:03:48 +00:00
pub trait MapModifier {
fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map;
}
2020-09-01 12:46:31 +00:00
/// Used to chain MapBuilder and MapModifiers to create the final map.
2020-08-31 20:03:48 +00:00
pub struct MapBuilder {
generator: Box<dyn MapGenerator>,
modifiers: Vec<Box<dyn MapModifier>>,
rng: StdRng,
}
impl MapBuilder {
2020-09-02 10:01:16 +00:00
/// Create Map Builder with initial map generator
2020-08-31 20:03:48 +00:00
pub fn new(generator : Box<dyn MapGenerator>) -> MapBuilder {
2020-09-02 10:01:16 +00:00
let system_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Can't access system time");
2020-08-31 20:03:48 +00:00
MapBuilder {
generator,
modifiers: Vec::new(),
2020-09-02 10:01:16 +00:00
rng: StdRng::seed_from_u64(system_time.as_secs())
2020-08-31 20:03:48 +00:00
}
}
2020-09-02 10:01:16 +00:00
pub fn with(&mut self, modifier : Box<dyn MapModifier>) -> &mut MapBuilder {
2020-08-31 20:03:48 +00:00
self.modifiers.push(modifier);
2020-09-02 10:01:16 +00:00
self
2020-08-31 20:03:48 +00:00
}
pub fn build_map(&mut self) -> Map {
let mut map = self.generator.generate_map(&mut self.rng);
// Build additional layers in turn
for modifier in self.modifiers.iter() {
2020-09-02 10:01:16 +00:00
map = modifier.modify_map(&mut self.rng, &map);
2020-08-31 20:03:48 +00:00
}
map
}
}
2020-09-02 10:01:16 +00:00
/// ------------------------------------------------------------------------------------------------
/// Module unit tests
/// ------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use cellular_automata::CellularAutomataGen;
use starting_point::{AreaStartingPosition, XStart, YStart};
#[test]
fn test_ca_map() {
let map = MapBuilder::new(Box::new(CellularAutomataGen::new(80, 50)))
.with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER))
.build_map();
assert_eq!(map.width, 80);
assert_eq!(map.height, 50);
assert_eq!(map.starting_point.is_some(), true);
}
}