Refactoring

This commit is contained in:
klangner 2020-10-19 20:08:40 +02:00
parent 45c76e18c7
commit 21fa869081
2 changed files with 41 additions and 15 deletions

View File

@ -62,8 +62,7 @@ impl DijkstraMap {
/// depth is further than the current depth. /// depth is further than the current depth.
/// WARNING: Will give incorrect results when used with non-uniform exit costs. Much slower /// WARNING: Will give incorrect results when used with non-uniform exit costs. Much slower
/// algorithm required to support that. /// algorithm required to support that.
/// Automatically branches to a parallel version if you provide more than 4 starting points fn build(&mut self, map: &Map) {
fn build(self: &mut DijkstraMap, map: &Map) {
let mapsize: usize = (self.size_x * self.size_y) as usize; let mapsize: usize = (self.size_x * self.size_y) as usize;
let mut open_list: VecDeque<((usize, usize), f32)> = VecDeque::with_capacity(mapsize); let mut open_list: VecDeque<((usize, usize), f32)> = VecDeque::with_capacity(mapsize);

View File

@ -53,20 +53,17 @@ impl AreaStartingPosition {
} }
fn build(&self, map : &Map) -> Map { fn build(&self, map : &Map) -> Map {
let seed_x; let seed_x = match self.x {
let seed_y; XStart::LEFT => 1,
XStart::CENTER => map.width / 2,
XStart::RIGHT => map.width - 2
};
match self.x { let seed_y = match self.y {
XStart::LEFT => seed_x = 1, YStart::TOP => 1,
XStart::CENTER => seed_x = map.width / 2, YStart::CENTER => map.height / 2,
XStart::RIGHT => seed_x = map.width - 2 YStart::BOTTOM => map.height - 2
} };
match self.y {
YStart::TOP => seed_y = 1,
YStart::CENTER => seed_y = map.height / 2,
YStart::BOTTOM => seed_y = map.height - 2
}
let mut available_floors : Vec<(usize, f32)> = Vec::new(); let mut available_floors : Vec<(usize, f32)> = Vec::new();
for (idx, tiletype) in map.tiles.iter().enumerate() { for (idx, tiletype) in map.tiles.iter().enumerate() {
@ -94,3 +91,33 @@ impl AreaStartingPosition {
new_map 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)));
}
}