Package refactoring

This commit is contained in:
klangner 2020-09-16 09:57:59 +02:00
parent 3b08166867
commit 764233c7cf
18 changed files with 237 additions and 57 deletions

View File

@ -1,9 +1,9 @@
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys; use web_sys;
use rand::prelude::*; use rand::prelude::*;
use mapgen::dungeon::{ use mapgen::{
map_builder::{
MapBuilder, MapBuilder,
map::TileType,
cellular_automata::CellularAutomataGen, cellular_automata::CellularAutomataGen,
simple_rooms::SimpleRoomsGen, simple_rooms::SimpleRoomsGen,
bsp_interior::BspInteriorGen, bsp_interior::BspInteriorGen,
@ -11,6 +11,8 @@ use mapgen::dungeon::{
cull_unreachable::CullUnreachable, cull_unreachable::CullUnreachable,
distant_exit::DistantExit, distant_exit::DistantExit,
rooms_corridors_nearest::NearestCorridors, rooms_corridors_nearest::NearestCorridors,
},
map::TileType,
}; };

View File

@ -50,7 +50,7 @@ const renderLoop = () => {
drawGrid(); drawGrid();
drawCells(); drawCells();
requestAnimationFrame(renderLoop); // requestAnimationFrame(renderLoop);
}; };
const drawGrid = () => { const drawGrid = () => {

View File

@ -1,2 +0,0 @@
pub mod geometry;
pub mod random;

View File

@ -98,8 +98,8 @@ impl DijkstraMap {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::common::geometry::Point; use crate::geometry::Point;
use crate::dungeon::map::Map; use crate::map::Map;
#[test] #[test]
fn test_culling() { fn test_culling() {

View File

@ -4,5 +4,9 @@
//! * Dungeon maps //! * Dungeon maps
//! //!
pub mod common; pub mod map_builder;
pub mod dungeon; pub mod geometry;
pub mod map;
pub (crate) mod dijkstra;
pub (crate) mod random;

View File

@ -8,7 +8,7 @@
//! //!
use std::fmt; use std::fmt;
use crate::common::geometry::{Point, Rect}; use super::geometry::{Point, Rect};
#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)] #[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)]

View File

@ -6,7 +6,7 @@
//! Example generator usage: //! Example generator usage:
//! ``` //! ```
//! use rand::prelude::*; //! use rand::prelude::*;
//! use mapgen::dungeon::{ //! use mapgen::map_builder::{
//! MapGenerator, //! MapGenerator,
//! bsp_interior::BspInteriorGen //! bsp_interior::BspInteriorGen
//! }; //! };
@ -22,9 +22,9 @@
use rand::prelude::*; use rand::prelude::*;
use super::MapGenerator; use super::MapGenerator;
use crate::common::geometry::{Point, Rect}; use crate::geometry::{Point, Rect};
use crate::common::random::Rng; use crate::random::Rng;
use super::map::Map; use crate::map::Map;
pub struct BspInteriorGen { pub struct BspInteriorGen {

View File

@ -6,7 +6,7 @@
//! Example generator usage: //! Example generator usage:
//! ``` //! ```
//! use rand::prelude::*; //! use rand::prelude::*;
//! use mapgen::dungeon::{ //! use mapgen::map_builder::{
//! MapGenerator, //! MapGenerator,
//! bsp_rooms::BspRoomsGen //! bsp_rooms::BspRoomsGen
//! }; //! };
@ -22,9 +22,9 @@
use rand::prelude::*; use rand::prelude::*;
use super::MapGenerator; use super::MapGenerator;
use crate::common::geometry::Rect; use crate::geometry::Rect;
use crate::common::random::Rng; use crate::random::Rng;
use super::map::{Map, TileType}; use crate::map::{Map, TileType};
pub struct BspRoomsGen { pub struct BspRoomsGen {

View File

@ -9,7 +9,7 @@
//! Example generator usage: //! Example generator usage:
//! ``` //! ```
//! use rand::prelude::*; //! use rand::prelude::*;
//! use mapgen::dungeon::{ //! use mapgen::map_builder::{
//! MapGenerator, //! MapGenerator,
//! cellular_automata::CellularAutomataGen //! cellular_automata::CellularAutomataGen
//! }; //! };
@ -25,7 +25,7 @@
use rand::prelude::*; use rand::prelude::*;
use super::{MapGenerator, MapModifier}; use super::{MapGenerator, MapModifier};
use super::map::{Map, TileType}; use crate::map::{Map, TileType};
/// Map generator and modifier /// Map generator and modifier

View File

@ -6,8 +6,8 @@
use rand::prelude::StdRng; use rand::prelude::StdRng;
use super::MapModifier; use super::MapModifier;
use super::map::{Map, TileType}; use crate::map::{Map, TileType};
use super::dijkstra::DijkstraMap; use crate::dijkstra::DijkstraMap;
/// Remove unreachable areas from the map. /// Remove unreachable areas from the map.
@ -50,8 +50,8 @@ mod tests {
use rand::prelude::*; use rand::prelude::*;
use super::*; use super::*;
use super::MapModifier; use super::MapModifier;
use crate::common::geometry::Point; use crate::geometry::Point;
use crate::dungeon::map::Map; use crate::map::Map;
#[test] #[test]
fn test_culling() { fn test_culling() {

View File

@ -6,10 +6,10 @@
use std::f32; use std::f32;
use rand::prelude::StdRng; use rand::prelude::StdRng;
use crate::common::geometry::Point; use crate::geometry::Point;
use super::MapModifier; use super::MapModifier;
use super::map::Map; use crate::map::Map;
use super::dijkstra::DijkstraMap; use crate::dijkstra::DijkstraMap;
/// Add exist position to the map based on the distance from the start point. /// Add exist position to the map based on the distance from the start point.
@ -54,8 +54,8 @@ mod tests {
use rand::prelude::*; use rand::prelude::*;
use super::*; use super::*;
use super::MapModifier; use super::MapModifier;
use crate::common::geometry::Point; use crate::geometry::Point;
use crate::dungeon::map::Map; use crate::map::Map;
#[test] #[test]
fn test_exit() { fn test_exit() {

173
src/map_builder/drunkard.rs Normal file
View File

@ -0,0 +1,173 @@
use rltk::RandomNumberGenerator;
use crate::map::TileType;
use crate::components::Position;
use super::{InitialMapBuilder, MetaMapBuilder, BuilderMap};
use super::common::*;
#[derive(PartialEq, Copy, Clone)]
#[allow(dead_code)]
pub enum DrunkSpawnMode { StartingPoint, Random }
pub struct DrunkardSettings {
pub spawn_mode : DrunkSpawnMode,
pub drunken_lifetime : i32,
pub floor_percent: f32,
pub brush_size: i32,
pub symmetry: Symmetry
}
pub struct DrunkardsWalkBuilder {
settings : DrunkardSettings
}
impl InitialMapBuilder for DrunkardsWalkBuilder {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut rltk::RandomNumberGenerator, build_data : &mut BuilderMap) {
self.build(rng, build_data);
}
}
impl DrunkardsWalkBuilder {
#[allow(dead_code)]
pub fn new(settings: DrunkardSettings) -> DrunkardsWalkBuilder {
DrunkardsWalkBuilder{
settings
}
}
#[allow(dead_code)]
pub fn open_area() -> Box<DrunkardsWalkBuilder> {
Box::new(DrunkardsWalkBuilder{
settings : DrunkardSettings{
spawn_mode: DrunkSpawnMode::StartingPoint,
drunken_lifetime: 400,
floor_percent: 0.5,
brush_size: 1,
symmetry: Symmetry::None
}
})
}
#[allow(dead_code)]
pub fn open_halls() -> Box<DrunkardsWalkBuilder> {
Box::new(DrunkardsWalkBuilder{
settings : DrunkardSettings{
spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 400,
floor_percent: 0.5,
brush_size: 1,
symmetry: Symmetry::None
},
})
}
#[allow(dead_code)]
pub fn winding_passages() -> Box<DrunkardsWalkBuilder> {
Box::new(DrunkardsWalkBuilder{
settings : DrunkardSettings{
spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 100,
floor_percent: 0.4,
brush_size: 1,
symmetry: Symmetry::None
},
})
}
#[allow(dead_code)]
pub fn fat_passages() -> Box<DrunkardsWalkBuilder> {
Box::new(DrunkardsWalkBuilder{
settings : DrunkardSettings{
spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 100,
floor_percent: 0.4,
brush_size: 2,
symmetry: Symmetry::None
},
})
}
#[allow(dead_code)]
pub fn fearful_symmetry() -> Box<DrunkardsWalkBuilder> {
Box::new(DrunkardsWalkBuilder{
settings : DrunkardSettings{
spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 100,
floor_percent: 0.4,
brush_size: 1,
symmetry: Symmetry::Both
},
})
}
fn build(&mut self, rng : &mut RandomNumberGenerator, build_data : &mut BuilderMap) {
// Set a central starting point
let starting_position = Position{ x: build_data.map.width / 2, y: build_data.map.height / 2 };
let start_idx = build_data.map.xy_idx(starting_position.x, starting_position.y);
build_data.map.tiles[start_idx] = TileType::Floor;
let total_tiles = build_data.map.width * build_data.map.height;
let desired_floor_tiles = (self.settings.floor_percent * total_tiles as f32) as usize;
let mut floor_tile_count = build_data.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
let mut digger_count = 0;
while floor_tile_count < desired_floor_tiles {
let mut did_something = false;
let mut drunk_x;
let mut drunk_y;
match self.settings.spawn_mode {
DrunkSpawnMode::StartingPoint => {
drunk_x = starting_position.x;
drunk_y = starting_position.y;
}
DrunkSpawnMode::Random => {
if digger_count == 0 {
drunk_x = starting_position.x;
drunk_y = starting_position.y;
} else {
drunk_x = rng.roll_dice(1, build_data.map.width - 3) + 1;
drunk_y = rng.roll_dice(1, build_data.map.height - 3) + 1;
}
}
}
let mut drunk_life = self.settings.drunken_lifetime;
while drunk_life > 0 {
let drunk_idx = build_data.map.xy_idx(drunk_x, drunk_y);
if build_data.map.tiles[drunk_idx] == TileType::Wall {
did_something = true;
}
paint(&mut build_data.map, self.settings.symmetry, self.settings.brush_size, drunk_x, drunk_y);
build_data.map.tiles[drunk_idx] = TileType::DownStairs;
let stagger_direction = rng.roll_dice(1, 4);
match stagger_direction {
1 => { if drunk_x > 2 { drunk_x -= 1; } }
2 => { if drunk_x < build_data.map.width-2 { drunk_x += 1; } }
3 => { if drunk_y > 2 { drunk_y -=1; } }
_ => { if drunk_y < build_data.map.height-2 { drunk_y += 1; } }
}
drunk_life -= 1;
}
if did_something {
build_data.take_snapshot();
}
digger_count += 1;
for t in build_data.map.tiles.iter_mut() {
if *t == TileType::DownStairs {
*t = TileType::Floor;
}
}
floor_tile_count = build_data.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
}
}
}
impl MetaMapBuilder for DrunkardsWalkBuilder {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut rltk::RandomNumberGenerator, build_data : &mut BuilderMap) {
self.build(rng, build_data);
}
}

View File

@ -8,12 +8,14 @@
//! //!
//! Example //! Example
//! ``` //! ```
//! use mapgen::common::geometry::Point; //! use mapgen::{
//! use mapgen::dungeon::{ //! map_builder::{
//! MapBuilder, //! MapModifier, MapBuilder,
//! map::{Map, TileType},
//! cellular_automata::CellularAutomataGen, //! cellular_automata::CellularAutomataGen,
//! starting_point::{AreaStartingPosition, XStart, YStart}, //! starting_point::{AreaStartingPosition, XStart, YStart}
//! },
//! map::{Map, TileType},
//! geometry::Point,
//! }; //! };
//! //!
//! let map = MapBuilder::new(CellularAutomataGen::new()) //! let map = MapBuilder::new(CellularAutomataGen::new())
@ -26,20 +28,19 @@
//! ``` //! ```
//! //!
pub mod map;
pub mod cellular_automata;
pub mod cull_unreachable;
pub mod bsp_interior; pub mod bsp_interior;
pub mod bsp_rooms; pub mod bsp_rooms;
pub mod cellular_automata;
pub mod cull_unreachable;
pub mod distant_exit; pub mod distant_exit;
// pub mod drunkard;
pub mod simple_rooms; pub mod simple_rooms;
pub mod rooms_corridors_nearest; pub mod rooms_corridors_nearest;
pub mod starting_point; pub mod starting_point;
mod dijkstra;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use rand::prelude::*; use rand::prelude::*;
use map::Map; use crate::map::Map;
/// Trait which should be implemented by any map generator which want to be used /// Trait which should be implemented by any map generator which want to be used

View File

@ -2,7 +2,7 @@
//! //!
use rand::prelude::StdRng; use rand::prelude::StdRng;
use super::MapModifier; use super::MapModifier;
use super::map::Map; use crate::map::Map;
use std::collections::HashSet; use std::collections::HashSet;

View File

@ -6,7 +6,7 @@
//! Example generator usage: //! Example generator usage:
//! ``` //! ```
//! use rand::prelude::*; //! use rand::prelude::*;
//! use mapgen::dungeon::{ //! use mapgen::map_builder::{
//! MapGenerator, //! MapGenerator,
//! simple_rooms::SimpleRoomsGen //! simple_rooms::SimpleRoomsGen
//! }; //! };
@ -22,9 +22,9 @@
use rand::prelude::*; use rand::prelude::*;
use super::MapGenerator; use super::MapGenerator;
use crate::common::geometry::Rect; use crate::geometry::Rect;
use crate::common::random::Rng; use crate::random::Rng;
use super::map::{Map}; use crate::map::{Map};
pub struct SimpleRoomsGen { pub struct SimpleRoomsGen {

View File

@ -6,11 +6,13 @@
//! Example modifier usage: //! Example modifier usage:
//! ``` //! ```
//! use rand::prelude::*; //! use rand::prelude::*;
//! use mapgen::common::geometry::Point; //! use mapgen::{
//! use mapgen::dungeon::{ //! map_builder::{
//! MapModifier, //! MapModifier,
//! starting_point::{AreaStartingPosition, XStart, YStart},
//! },
//! map::{Map, TileType}, //! map::{Map, TileType},
//! starting_point::{AreaStartingPosition, XStart, YStart} //! geometry::Point,
//! }; //! };
//! //!
//! let mut rng = StdRng::seed_from_u64(100); //! let mut rng = StdRng::seed_from_u64(100);
@ -25,8 +27,8 @@
use rand::prelude::StdRng; use rand::prelude::StdRng;
use super::{MapModifier}; use super::{MapModifier};
use crate::common::geometry::Point; use crate::geometry::Point;
use super::map::{Map, TileType}; use crate::map::{Map, TileType};
/// Initial x region position /// Initial x region position