Skip to content

klangner/mapgen.rs

Repository files navigation

Game Map Generator

Build Status Crates.io mapgen.rs

Generate procedural maps for games. Try it in the browser using WebAssembly.

Map filters

This library consists of different map filters which can be combined to create custom map generator.

Implemented filters

  • Area exit point
  • Area starting point
  • BSP Interior
  • BSP Rooms
  • Cellular automata
  • Cull unreachable areas
  • Diffusion-Limited Aggregation (DLA)
  • Drunkard's walk
  • Maze
  • Noise generator
  • Prefabs
  • Room corridors nearest
  • Simple rooms
  • Voronoi hive
  • Wave Function Collapse

Usage

Add dependency to your project

mapgen = "0.5"

Generate room based map

use rand::prelude::*;
use mapgen::{
    poi::{AreaStartingPosition, DistantExit, XStart, YStart}, 
    rooms::BspInterior};


fn main() {
    let mut rng = StdRng::seed_from_u64(907647352);
    let bsp = BspInterior::default();
    let map = bsp.generate_rooms(20, 10, &mut rng);
    let starting_point = AreaStartingPosition::find(XStart::LEFT, YStart::TOP, &map.walkable_layer);
    let exit_point = DistantExit::find(&starting_point, &map.walkable_layer);

    println!("{:}", &map);
    println!("Start: {:?}, exit: {:?}", starting_point, exit_point);
}

Using single dungeon generators:

use mapgen::dungeon::{CellularAutomata, NoiseGenerator};
use mapgen::{poi::*, MapBuilder};


fn main() {
    let map = MapBuilder::new(20, 20)
        .with(NoiseGenerator::uniform())
        .with(CellularAutomata::new())
        .build();  
    
    let starting_point = AreaStartingPosition::find(XStart::CENTER, YStart::CENTER, &map.walkable_layer);
    let walkables = CullUnreachable::remove_walkable_tiles(&starting_point, &map.walkable_layer);
    
    println!("{:}", &walkables);
}

For more information check the doc

This library is based on the code from the Roguelike tutorial. I highly recommend it for learning how to write Roguelike in Rust.

License

Licensed under either of

at your option.

Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.