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.
/// 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);

View File

@ -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)));
}
}