From 51e0d585df5bba113ca567138708911216589ef9 Mon Sep 17 00:00:00 2001 From: klangner Date: Tue, 1 Sep 2020 14:46:31 +0200 Subject: [PATCH] Added doc --- Cargo.toml | 2 +- README.md | 49 +++++++++++++++++++++++++++++++- src/dungeon/cellular_automata.rs | 28 ++++++++++++++++++ src/dungeon/map.rs | 32 +++++++++++++++++++-- src/dungeon/mod.rs | 14 +++++++++ src/lib.rs | 6 ++++ 6 files changed, 127 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 57e84f2..1bf1762 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mapgen" -version = "0.1.0" +version = "0.1.1" authors = ["Krzysztof Langner "] description = "Map generator for games (dungeons, worlds etc.)" keywords = ["game", "map", "map-generator"] diff --git a/README.md b/README.md index a15efcb..5a4c7f3 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,58 @@ This library is based on the code from the [Roguelike tutorial](https://github.c I highly recommend it for learning how to write Roguelike in Rust. +## Demo app + +If you want to check how the maps look like, then: + * Clone this rep + * Go to the demo folder + * Run demo app (`cargo run`) + + ## Features ### Dungeons - * [ ] Cellural automata + * Map generators + * [ ] BSP Interior + * [ ] BSP Room + * [x] Cellular automata + * [ ] Diffusion-Limited Aggregation (DLA) + * [ ] Drunkard's walk + * [ ] Maze + * [ ] Prefabs + * [ ] Voronoi hive + * [ ] Wave Function Collapse + * Map modifiers (filters) + * [ ] Area exit point + * [ ] Area starting point + * [x] Cellular automata + * [ ] Cull unreachable areas + * [ ] Voronoi spawning + + +## Usage + +Add dependency to your project +``` +mapgen = "0.1" +``` + +Using single map generator: + +```rust +use rand::prelude::*; +use mapgen::dungeon::{ + MapGenerator, + cellular_automata::CellularAutomataGen +}; + +let mut rng = StdRng::seed_from_u64(100); +let gen = CellularAutomataGen::new(80, 50); +let map = gen.generate_map(&mut rng) +``` + +For more information check the [doc](https://docs.rs/mapgen) # License diff --git a/src/dungeon/cellular_automata.rs b/src/dungeon/cellular_automata.rs index 6646f99..046dafa 100644 --- a/src/dungeon/cellular_automata.rs +++ b/src/dungeon/cellular_automata.rs @@ -1,8 +1,35 @@ +//! Cellular automata map generator and modifier. +//! +//! Check this [article](http://www.roguebasin.com/index.php?title=Cellular_Automata_Method_for_Generating_Random_Cave-Like_Levels) +//! for more information about the algorithm behind this generator. +//! +//! Since this algorithm works in interations it is possible to take existing map +//! and apply single interaction to it. This is the idea behind MapModifier implementation. +//! +//! Example generator usage: +//! ``` +//! use rand::prelude::*; +//! use mapgen::dungeon::{ +//! MapGenerator, +//! cellular_automata::CellularAutomataGen +//! }; +//! +//! let mut rng = StdRng::seed_from_u64(100); +//! let gen = CellularAutomataGen::new(80, 50); +//! let map = gen.generate_map(&mut rng); +//! +//! assert_eq!(map.width, 80); +//! assert_eq!(map.height, 50); +//! ``` +//! + + use rand::prelude::*; use super::{MapGenerator, MapModifier}; use super::map::{Map, TileType}; +/// Map generator and modifier pub struct CellularAutomataGen { width: usize, height: usize @@ -15,6 +42,7 @@ impl MapGenerator for CellularAutomataGen { } impl CellularAutomataGen { + /// Create generator which will create map with the given dimension. pub fn new(width: usize, height: usize) -> CellularAutomataGen { CellularAutomataGen {width, height} } diff --git a/src/dungeon/map.rs b/src/dungeon/map.rs index 1a52c20..7b64106 100644 --- a/src/dungeon/map.rs +++ b/src/dungeon/map.rs @@ -1,13 +1,39 @@ -#[derive(PartialEq, Copy, Clone, Eq, Hash, Debug)] +//! Map structure contains information about tiles and other elements on the map. +//! +//! Map is created with generators and then can by modified with MapModifiers. +//! +//! This structure is not intented to be your map in the game (But can be used as one). +//! Rather the information from this map will be copied to the structures required by +//! specific game. +//! + +/// Position on the map +#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)] +pub struct Position { + x: usize, + y: usize +} + +impl Position { + pub fn new(x: usize, y: usize) -> Position { + Position {x, y} + } +} + +/// Possible tile type on the map +#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)] pub enum TileType { Wall, Floor } +/// Map data #[derive(Default, Clone)] pub struct Map { pub tiles : Vec, pub width : usize, pub height : usize, + pub starting_point: Option, + pub exit_point: Option } impl Map { @@ -18,7 +44,9 @@ impl Map { Map{ tiles : vec![TileType::Wall; map_tile_count], width, - height + height, + starting_point: None, + exit_point: None, } } diff --git a/src/dungeon/mod.rs b/src/dungeon/mod.rs index f1b7a06..4966b1b 100644 --- a/src/dungeon/mod.rs +++ b/src/dungeon/mod.rs @@ -1,3 +1,12 @@ +//! 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. +//! + pub mod map; pub mod cellular_automata; @@ -5,14 +14,19 @@ use rand::prelude::*; use map::Map; +/// Trait which should be implemented by any map generator which want to be used +/// by MapBuilder pub trait MapGenerator { fn generate_map(&self, rng: &mut StdRng) -> Map; } +/// Trait which should be implemented by map modifier. +/// Modifier takes initiall map and apply changes to it. pub trait MapModifier { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map; } +/// Used to chain MapBuilder and MapModifiers to create the final map. pub struct MapBuilder { generator: Box, modifiers: Vec>, diff --git a/src/lib.rs b/src/lib.rs index 96e0f2c..aba5d47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,7 @@ +//! A collection of map generators. +//! +//! The generators are implemented for the following types of maps: +//! * Dungeon maps +//! + pub mod dungeon; \ No newline at end of file