From 21fa86908190819bda8c3a6674e1502d2b5eff3b Mon Sep 17 00:00:00 2001 From: klangner Date: Mon, 19 Oct 2020 20:08:40 +0200 Subject: [PATCH] Refactoring --- src/dijkstra.rs | 3 +- src/filter/starting_point.rs | 53 +++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/dijkstra.rs b/src/dijkstra.rs index b089d38..8d2062b 100644 --- a/src/dijkstra.rs +++ b/src/dijkstra.rs @@ -62,8 +62,7 @@ impl DijkstraMap { /// depth is further than the current depth. /// WARNING: Will give incorrect results when used with non-uniform exit costs. Much slower /// algorithm required to support that. - /// Automatically branches to a parallel version if you provide more than 4 starting points - fn build(self: &mut DijkstraMap, map: &Map) { + fn build(&mut self, map: &Map) { let mapsize: usize = (self.size_x * self.size_y) as usize; let mut open_list: VecDeque<((usize, usize), f32)> = VecDeque::with_capacity(mapsize); diff --git a/src/filter/starting_point.rs b/src/filter/starting_point.rs index e087846..a3f9fe7 100644 --- a/src/filter/starting_point.rs +++ b/src/filter/starting_point.rs @@ -53,20 +53,17 @@ impl AreaStartingPosition { } fn build(&self, map : &Map) -> Map { - let seed_x; - let seed_y; + let seed_x = match self.x { + XStart::LEFT => 1, + XStart::CENTER => map.width / 2, + XStart::RIGHT => map.width - 2 + }; - match self.x { - XStart::LEFT => seed_x = 1, - XStart::CENTER => seed_x = map.width / 2, - XStart::RIGHT => seed_x = map.width - 2 - } - - match self.y { - YStart::TOP => seed_y = 1, - YStart::CENTER => seed_y = map.height / 2, - YStart::BOTTOM => seed_y = map.height - 2 - } + let seed_y = match self.y { + YStart::TOP => 1, + YStart::CENTER => map.height / 2, + YStart::BOTTOM => map.height - 2 + }; let mut available_floors : Vec<(usize, f32)> = Vec::new(); for (idx, tiletype) in map.tiles.iter().enumerate() { @@ -93,4 +90,34 @@ impl AreaStartingPosition { new_map.starting_point = Some(Point::new(start_x, start_y)); new_map } +} + +/// ------------------------------------------------------------------------------------------------ +/// Module unit tests +/// ------------------------------------------------------------------------------------------------ +#[cfg(test)] +mod tests { + use rand::prelude::*; + use super::*; + use super::MapFilter; + use crate::geometry::Point; + use crate::map::Map; + + #[test] + fn test_exit() { + let map_str = " + ########## + # ## # + # # # # + ########## + "; + let mut map = Map::from_string(map_str); + map.starting_point = Some(Point::new(9, 2)); + + let modifier = AreaStartingPosition::new(XStart::CENTER, YStart::TOP); + let mut rng = StdRng::seed_from_u64(0); + let new_map = modifier.modify_map(&mut rng, &map); + + assert_eq!(new_map.starting_point, Some(Point::new(6, 1))); + } } \ No newline at end of file