From 2202540a43a4d5d1c115aa336cf5a6f6128ea486 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Sun, 13 Mar 2022 21:14:59 -0500 Subject: [PATCH] Drop `BuilderData` trait. --- examples/builder_data.rs | 8 +++----- src/dijkstra.rs | 6 +++--- src/filter/bsp_interior.rs | 8 ++++---- src/filter/bsp_rooms.rs | 13 +++++-------- src/filter/cellular_automata.rs | 9 ++++----- src/filter/cull_unreachable.rs | 8 ++++---- src/filter/distant_exit.rs | 7 +++---- src/filter/drunkard.rs | 7 +++---- src/filter/maze.rs | 12 ++++++------ src/filter/noise_generator.rs | 8 ++++---- src/filter/rooms_corridors_nearest.rs | 7 +++---- src/filter/simple_rooms.rs | 7 +++---- src/filter/starting_point.rs | 7 +++---- src/filter/voronoi.rs | 8 ++++---- src/lib.rs | 8 ++++---- src/map.rs | 11 +++-------- src/metric.rs | 5 ++--- 17 files changed, 61 insertions(+), 78 deletions(-) diff --git a/examples/builder_data.rs b/examples/builder_data.rs index 2d71c06..bb95534 100644 --- a/examples/builder_data.rs +++ b/examples/builder_data.rs @@ -2,7 +2,7 @@ use mapgen::{ filter::{ AreaStartingPosition, CellularAutomata, CullUnreachable, NoiseGenerator, XStart, YStart, }, - BuilderData, MapBuilder, MapFilter, + MapBuilder, MapFilter, }; #[derive(Clone, Default)] @@ -10,11 +10,9 @@ struct MyData { value: usize, } -impl BuilderData for MyData {} - struct IncrementData; -impl MapFilter for IncrementData { +impl MapFilter for IncrementData { fn modify_map(&self, rng: &mut rand::prelude::StdRng, map: &mapgen::Map) -> mapgen::Map { let map = map.clone(); map.data.value += 1; @@ -28,7 +26,7 @@ fn main() { .with(CellularAutomata::new()) .with(AreaStartingPosition::new(XStart::CENTER, YStart::CENTER)) .with(CullUnreachable::new()) - .with(Box::new(IncrementData)) + .with(Box::new(IncrementData::)) .build(); println!("{:}", &map); diff --git a/src/dijkstra.rs b/src/dijkstra.rs index c7dfc20..a1c9d7b 100644 --- a/src/dijkstra.rs +++ b/src/dijkstra.rs @@ -25,7 +25,7 @@ //! --- //! -use super::map::{BuilderData, Map}; +use super::map::Map; use std::f32::MAX; use std::{collections::VecDeque, marker::PhantomData}; @@ -41,13 +41,13 @@ pub struct DijkstraMap { phantom: PhantomData, } -impl DijkstraMap { +impl DijkstraMap { //! Construct a new Dijkstra map, ready to run. pub fn new(map: &Map) -> DijkstraMap { let len = map.width * map.height; let tiles = vec![MAX; len]; let mut d = DijkstraMap { - tiles: tiles, + tiles, size_x: map.width, size_y: map.height, max_depth: len as f32, diff --git a/src/filter/bsp_interior.rs b/src/filter/bsp_interior.rs index e08e0c3..85eeb63 100644 --- a/src/filter/bsp_interior.rs +++ b/src/filter/bsp_interior.rs @@ -25,21 +25,21 @@ use std::marker::PhantomData; use crate::geometry::{Point, Rect}; use crate::random::Rng; use crate::Map; -use crate::{BuilderData, MapFilter}; +use crate::MapFilter; use rand::prelude::*; -pub struct BspInterior { +pub struct BspInterior { min_room_size: usize, phantom: PhantomData, } -impl MapFilter for BspInterior { +impl MapFilter for BspInterior { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map { self.build(rng, map) } } -impl BspInterior { +impl BspInterior { pub fn new() -> Box> { Box::new(BspInterior { min_room_size: 8, diff --git a/src/filter/bsp_rooms.rs b/src/filter/bsp_rooms.rs index ece1f5e..cd73551 100644 --- a/src/filter/bsp_rooms.rs +++ b/src/filter/bsp_rooms.rs @@ -22,23 +22,22 @@ use std::marker::PhantomData; use crate::geometry::Rect; use crate::random::Rng; -use crate::BuilderData; use crate::Map; use crate::MapFilter; use rand::prelude::*; -pub struct BspRooms { +pub struct BspRooms { max_split: usize, phantom: PhantomData, } -impl MapFilter for BspRooms { +impl MapFilter for BspRooms { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map { self.build_rooms(map, rng) } } -impl BspRooms { +impl BspRooms { pub fn new() -> Box> { Box::new(BspRooms { max_split: 240, @@ -152,10 +151,8 @@ impl BspRooms { if y < 1 { can_build = false; } - if can_build { - if map.at(x as usize, y as usize).is_walkable() { - can_build = false; - } + if can_build && map.at(x as usize, y as usize).is_walkable() { + can_build = false; } } } diff --git a/src/filter/cellular_automata.rs b/src/filter/cellular_automata.rs index dc849b2..8fd430d 100644 --- a/src/filter/cellular_automata.rs +++ b/src/filter/cellular_automata.rs @@ -23,24 +23,23 @@ use std::marker::PhantomData; -use crate::BuilderData; use crate::MapFilter; use crate::{Map, Tile}; use rand::prelude::*; /// Map filter -pub struct CellularAutomata { +pub struct CellularAutomata { num_iteraction: u32, phantom: PhantomData, } -impl MapFilter for CellularAutomata { +impl MapFilter for CellularAutomata { fn modify_map(&self, _rng: &mut StdRng, map: &Map) -> Map { self.build(map) } } -impl CellularAutomata { +impl CellularAutomata { /// Create generator which will create map with the given dimension. pub fn new() -> Box> { Box::new(CellularAutomata { @@ -60,7 +59,7 @@ impl CellularAutomata { } } -fn apply_iteration(map: &Map) -> Map { +fn apply_iteration(map: &Map) -> Map { let mut new_map = map.clone(); for y in 1..map.height - 1 { diff --git a/src/filter/cull_unreachable.rs b/src/filter/cull_unreachable.rs index 9adc84c..6c8f134 100644 --- a/src/filter/cull_unreachable.rs +++ b/src/filter/cull_unreachable.rs @@ -8,21 +8,21 @@ use std::marker::PhantomData; use crate::dijkstra::DijkstraMap; use crate::MapFilter; -use crate::{BuilderData, Map, Tile}; +use crate::{Map, Tile}; use rand::prelude::StdRng; /// Remove unreachable areas from the map. -pub struct CullUnreachable { +pub struct CullUnreachable { phantom: PhantomData, } -impl MapFilter for CullUnreachable { +impl MapFilter for CullUnreachable { fn modify_map(&self, _: &mut StdRng, map: &Map) -> Map { self.build(map) } } -impl CullUnreachable { +impl CullUnreachable { #[allow(dead_code)] pub fn new() -> Box> { Box::new(CullUnreachable { diff --git a/src/filter/distant_exit.rs b/src/filter/distant_exit.rs index 075ca57..ee306f5 100644 --- a/src/filter/distant_exit.rs +++ b/src/filter/distant_exit.rs @@ -6,7 +6,6 @@ use crate::dijkstra::DijkstraMap; use crate::geometry::Point; -use crate::BuilderData; use crate::Map; use crate::MapFilter; use rand::prelude::StdRng; @@ -14,17 +13,17 @@ use std::f32; use std::marker::PhantomData; /// Add exist position to the map based on the distance from the start point. -pub struct DistantExit { +pub struct DistantExit { phantom: PhantomData, } -impl MapFilter for DistantExit { +impl MapFilter for DistantExit { fn modify_map(&self, _: &mut StdRng, map: &Map) -> Map { self.build(map) } } -impl DistantExit { +impl DistantExit { #[allow(dead_code)] pub fn new() -> Box> { Box::new(DistantExit { diff --git a/src/filter/drunkard.rs b/src/filter/drunkard.rs index 6413303..6714375 100644 --- a/src/filter/drunkard.rs +++ b/src/filter/drunkard.rs @@ -20,7 +20,6 @@ use crate::{ geometry::Point, map::{Map, Symmetry, Tile}, random::Rng, - BuilderData, }; use rand::prelude::*; @@ -30,7 +29,7 @@ pub enum DrunkSpawnMode { Random, } -pub struct DrunkardsWalk { +pub struct DrunkardsWalk { spawn_mode: DrunkSpawnMode, drunken_lifetime: i32, floor_percent: f32, @@ -39,13 +38,13 @@ pub struct DrunkardsWalk { phantom: PhantomData, } -impl MapFilter for DrunkardsWalk { +impl MapFilter for DrunkardsWalk { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map { self.build(rng, map) } } -impl DrunkardsWalk { +impl DrunkardsWalk { pub fn new( spawn_mode: DrunkSpawnMode, drunken_lifetime: i32, diff --git a/src/filter/maze.rs b/src/filter/maze.rs index 3e12572..29a55fd 100644 --- a/src/filter/maze.rs +++ b/src/filter/maze.rs @@ -17,22 +17,22 @@ use std::marker::PhantomData; use crate::MapFilter; use crate::{ - map::{BuilderData, Map, Tile}, + map::{Map, Tile}, random::Rng, }; use rand::prelude::*; -pub struct MazeBuilder { +pub struct MazeBuilder { phantom: PhantomData, } -impl MapFilter for MazeBuilder { +impl MapFilter for MazeBuilder { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map { self.build(rng, map) } } -impl MazeBuilder { +impl MazeBuilder { pub fn new() -> Box> { Box::new(MazeBuilder { phantom: PhantomData, @@ -93,7 +93,7 @@ impl Cell { } } -struct Grid<'a, D: BuilderData> { +struct Grid<'a, D> { width: i32, height: i32, cells: Vec, @@ -103,7 +103,7 @@ struct Grid<'a, D: BuilderData> { phantom: PhantomData, } -impl<'a, D: BuilderData> Grid<'a, D> { +impl<'a, D: Clone + Default> Grid<'a, D> { fn new(width: i32, height: i32, rng: &mut StdRng) -> Grid { let mut grid = Grid { width, diff --git a/src/filter/noise_generator.rs b/src/filter/noise_generator.rs index f05cff0..b6e753a 100644 --- a/src/filter/noise_generator.rs +++ b/src/filter/noise_generator.rs @@ -19,22 +19,22 @@ use std::marker::PhantomData; use crate::MapFilter; -use crate::{BuilderData, Map, Tile}; +use crate::{Map, Tile}; use rand::prelude::*; /// Map noise generator -pub struct NoiseGenerator { +pub struct NoiseGenerator { prob: f32, phantom: PhantomData, } -impl MapFilter for NoiseGenerator { +impl MapFilter for NoiseGenerator { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map { self.build(map, rng) } } -impl NoiseGenerator { +impl NoiseGenerator { /// Create noise with custom probability pub fn new(prob: f32) -> Box> { Box::new(NoiseGenerator { diff --git a/src/filter/rooms_corridors_nearest.rs b/src/filter/rooms_corridors_nearest.rs index 05a4857..2c108ff 100644 --- a/src/filter/rooms_corridors_nearest.rs +++ b/src/filter/rooms_corridors_nearest.rs @@ -1,23 +1,22 @@ //! Connect nearest rooms on the map with corridors //! -use crate::BuilderData; use crate::Map; use crate::MapFilter; use rand::prelude::StdRng; use std::collections::HashSet; use std::marker::PhantomData; -pub struct NearestCorridors { +pub struct NearestCorridors { phantom: PhantomData, } -impl MapFilter for NearestCorridors { +impl MapFilter for NearestCorridors { fn modify_map(&self, _: &mut StdRng, map: &Map) -> Map { self.corridors(map) } } -impl NearestCorridors { +impl NearestCorridors { pub fn new() -> Box> { Box::new(NearestCorridors { phantom: PhantomData, diff --git a/src/filter/simple_rooms.rs b/src/filter/simple_rooms.rs index ec68e7f..b8de238 100644 --- a/src/filter/simple_rooms.rs +++ b/src/filter/simple_rooms.rs @@ -22,25 +22,24 @@ use std::marker::PhantomData; use crate::geometry::Rect; use crate::random::Rng; -use crate::BuilderData; use crate::Map; use crate::MapFilter; use rand::prelude::*; -pub struct SimpleRooms { +pub struct SimpleRooms { max_rooms: usize, min_room_size: usize, max_room_size: usize, phantom: PhantomData, } -impl MapFilter for SimpleRooms { +impl MapFilter for SimpleRooms { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map { self.build_rooms(map, rng) } } -impl SimpleRooms { +impl SimpleRooms { pub fn new() -> Box> { Box::new(SimpleRooms { max_rooms: 30, diff --git a/src/filter/starting_point.rs b/src/filter/starting_point.rs index 9e55ffd..d04e643 100644 --- a/src/filter/starting_point.rs +++ b/src/filter/starting_point.rs @@ -23,7 +23,6 @@ use std::marker::PhantomData; use crate::geometry::Point; -use crate::BuilderData; use crate::Map; use crate::MapFilter; use rand::prelude::StdRng; @@ -43,19 +42,19 @@ pub enum YStart { } /// Add starting position to the map -pub struct AreaStartingPosition { +pub struct AreaStartingPosition { x: XStart, y: YStart, phantom: PhantomData, } -impl MapFilter for AreaStartingPosition { +impl MapFilter for AreaStartingPosition { fn modify_map(&self, _: &mut StdRng, map: &Map) -> Map { self.build(map) } } -impl AreaStartingPosition { +impl AreaStartingPosition { /// Create new modifier with given region pub fn new(x: XStart, y: YStart) -> Box> { Box::new(AreaStartingPosition { diff --git a/src/filter/voronoi.rs b/src/filter/voronoi.rs index f708e34..1aa05c9 100644 --- a/src/filter/voronoi.rs +++ b/src/filter/voronoi.rs @@ -18,23 +18,23 @@ use std::marker::PhantomData; use crate::MapFilter; use crate::{ geometry::Point, - map::{BuilderData, Map, Tile}, + map::{Map, Tile}, random::Rng, }; use rand::prelude::*; -pub struct VoronoiHive { +pub struct VoronoiHive { n_seeds: usize, phantom: PhantomData, } -impl MapFilter for VoronoiHive { +impl MapFilter for VoronoiHive { fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map { self.build(rng, map) } } -impl VoronoiHive { +impl VoronoiHive { pub fn new() -> Box> { Box::new(VoronoiHive { n_seeds: 64, diff --git a/src/lib.rs b/src/lib.rs index 7146621..c3c9bae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,7 @@ pub mod map; pub mod metric; pub use filter::*; -pub use map::{BuilderData, Map, NoData, Symmetry, Tile}; +pub use map::{Map, NoData, Symmetry, Tile}; pub(crate) mod dijkstra; pub(crate) mod random; @@ -44,18 +44,18 @@ use std::time::{SystemTime, UNIX_EPOCH}; /// Trait which should be implemented by map modifier. /// Modifier takes initiall map and apply changes to it. -pub trait MapFilter { +pub trait MapFilter { 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 { width: usize, height: usize, modifiers: Vec>>, } -impl MapBuilder { +impl MapBuilder { /// Create Map Builder with initial map generator pub fn new(width: usize, height: usize) -> MapBuilder { MapBuilder { diff --git a/src/map.rs b/src/map.rs index 31f950f..2f34fa5 100644 --- a/src/map.rs +++ b/src/map.rs @@ -24,18 +24,13 @@ pub enum Symmetry { Both, } -/// Arbitrary data associated with each map -pub trait BuilderData: Clone + Default {} - /// No build data #[derive(Clone, Debug, Default)] pub struct NoData; -impl BuilderData for NoData {} - /// Map data #[derive(Default, Clone)] -pub struct Map { +pub struct Map { pub tiles: Vec, pub width: usize, pub height: usize, @@ -72,7 +67,7 @@ impl Tile { } } -impl Map { +impl Map { /// Generates an empty map, consisting entirely of solid walls pub fn new(width: usize, height: usize) -> Map { let map_tile_count = width * height; @@ -274,7 +269,7 @@ impl Map { } } -impl fmt::Display for Map { +impl fmt::Display for Map { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for y in 0..self.height { let bytes: Vec = (0..self.width) diff --git a/src/metric.rs b/src/metric.rs index e0fcd6b..d91c15c 100644 --- a/src/metric.rs +++ b/src/metric.rs @@ -6,12 +6,11 @@ use super::dijkstra::DijkstraMap; use super::map::Map; -use super::BuilderData; /// This metric calculates the percentage of walkable cells (Floor). /// If this number is very low (like < 10%) then it means that the map /// is probably to degenerated and shouldn't be used -pub fn density(map: &Map) -> f32 { +pub fn density(map: &Map) -> f32 { let floor_count = map.tiles.iter().filter(|&t| t.is_walkable()).count(); floor_count as f32 / map.tiles.len() as f32 } @@ -19,7 +18,7 @@ pub fn density(map: &Map) -> f32 { /// Calculate the length of the shortes path from the starting point /// to the exit. /// If this path is very short, then the map is probably degenerated. -pub fn path_length(map: &Map) -> f32 { +pub fn path_length(map: &Map) -> f32 { if map.starting_point.is_none() { return 0.0; }