use rand::prelude::StdRng;
use crate::MapFilter;
use crate::geometry::Point;
use crate::MapBuffer;
pub enum XStart { LEFT, CENTER, RIGHT }
pub enum YStart { TOP, CENTER, BOTTOM }
pub struct AreaStartingPosition {
x : XStart,
y : YStart
}
impl MapFilter for AreaStartingPosition {
fn modify_map(&self, _: &mut StdRng, map: &MapBuffer) -> MapBuffer {
self.build(map)
}
}
impl AreaStartingPosition {
pub fn new(x : XStart, y : YStart) -> Box<AreaStartingPosition> {
Box::new(AreaStartingPosition{
x, y
})
}
fn build(&self, map : &MapBuffer) -> MapBuffer {
let seed_x = match self.x {
XStart::LEFT => 1,
XStart::CENTER => map.width / 2,
XStart::RIGHT => map.width - 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, &w) in map.walkables.iter().enumerate() {
if w {
available_floors.push(
(
idx,
Point::new(idx % map.width, idx / map.width)
.distance_to(&Point::new(seed_x, seed_y))
)
);
}
}
if available_floors.is_empty() {
panic!("No valid floors to start on");
}
available_floors.sort_by(|a,b| a.1.partial_cmp(&b.1).unwrap());
let start_x = available_floors[0].0 % map.width;
let start_y = available_floors[0].0 / map.width;
let mut new_map = map.clone();
new_map.starting_point = Some(Point::new(start_x, start_y));
new_map
}
}
#[cfg(test)]
mod tests {
use rand::prelude::*;
use super::*;
use super::MapFilter;
use crate::geometry::Point;
use crate::map_buffer::MapBuffer;
#[test]
fn test_exit() {
let map_str = "
##########
# ## #
# # # #
##########
";
let mut map = MapBuffer::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)));
}
}