Added doc
This commit is contained in:
parent
3805372cf2
commit
51e0d585df
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "mapgen"
|
name = "mapgen"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Krzysztof Langner <klangner@gmail.com>"]
|
authors = ["Krzysztof Langner <klangner@gmail.com>"]
|
||||||
description = "Map generator for games (dungeons, worlds etc.)"
|
description = "Map generator for games (dungeons, worlds etc.)"
|
||||||
keywords = ["game", "map", "map-generator"]
|
keywords = ["game", "map", "map-generator"]
|
||||||
|
|
49
README.md
49
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.
|
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
|
## Features
|
||||||
|
|
||||||
### Dungeons
|
### 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
|
# License
|
||||||
|
|
||||||
|
|
|
@ -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 rand::prelude::*;
|
||||||
use super::{MapGenerator, MapModifier};
|
use super::{MapGenerator, MapModifier};
|
||||||
use super::map::{Map, TileType};
|
use super::map::{Map, TileType};
|
||||||
|
|
||||||
|
|
||||||
|
/// Map generator and modifier
|
||||||
pub struct CellularAutomataGen {
|
pub struct CellularAutomataGen {
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize
|
height: usize
|
||||||
|
@ -15,6 +42,7 @@ impl MapGenerator for CellularAutomataGen {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CellularAutomataGen {
|
impl CellularAutomataGen {
|
||||||
|
/// Create generator which will create map with the given dimension.
|
||||||
pub fn new(width: usize, height: usize) -> CellularAutomataGen {
|
pub fn new(width: usize, height: usize) -> CellularAutomataGen {
|
||||||
CellularAutomataGen {width, height}
|
CellularAutomataGen {width, height}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
pub enum TileType {
|
||||||
Wall, Floor
|
Wall, Floor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map data
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
pub tiles : Vec<TileType>,
|
pub tiles : Vec<TileType>,
|
||||||
pub width : usize,
|
pub width : usize,
|
||||||
pub height : usize,
|
pub height : usize,
|
||||||
|
pub starting_point: Option<Position>,
|
||||||
|
pub exit_point: Option<Position>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
|
@ -18,7 +44,9 @@ impl Map {
|
||||||
Map{
|
Map{
|
||||||
tiles : vec![TileType::Wall; map_tile_count],
|
tiles : vec![TileType::Wall; map_tile_count],
|
||||||
width,
|
width,
|
||||||
height
|
height,
|
||||||
|
starting_point: None,
|
||||||
|
exit_point: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 map;
|
||||||
pub mod cellular_automata;
|
pub mod cellular_automata;
|
||||||
|
|
||||||
|
@ -5,14 +14,19 @@ use rand::prelude::*;
|
||||||
use map::Map;
|
use map::Map;
|
||||||
|
|
||||||
|
|
||||||
|
/// Trait which should be implemented by any map generator which want to be used
|
||||||
|
/// by MapBuilder
|
||||||
pub trait MapGenerator {
|
pub trait MapGenerator {
|
||||||
fn generate_map(&self, rng: &mut StdRng) -> Map;
|
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 {
|
pub trait MapModifier {
|
||||||
fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map;
|
fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Used to chain MapBuilder and MapModifiers to create the final map.
|
||||||
pub struct MapBuilder {
|
pub struct MapBuilder {
|
||||||
generator: Box<dyn MapGenerator>,
|
generator: Box<dyn MapGenerator>,
|
||||||
modifiers: Vec<Box<dyn MapModifier>>,
|
modifiers: Vec<Box<dyn MapModifier>>,
|
||||||
|
|
|
@ -1 +1,7 @@
|
||||||
|
//! A collection of map generators.
|
||||||
|
//!
|
||||||
|
//! The generators are implemented for the following types of maps:
|
||||||
|
//! * Dungeon maps
|
||||||
|
//!
|
||||||
|
|
||||||
pub mod dungeon;
|
pub mod dungeon;
|
Loading…
Reference in New Issue
Block a user