diff --git a/src/map_builders.rs b/src/map_builders.rs index 649fda9..29464cb 100644 --- a/src/map_builders.rs +++ b/src/map_builders.rs @@ -18,6 +18,7 @@ mod room_exploder; mod room_sorter; mod rooms_corridors_bsp; mod rooms_corridors_dogleg; +mod rooms_corridors_nearest; mod simple_map; mod voronoi; mod voronoi_spawning; @@ -43,6 +44,7 @@ use room_exploder::RoomExploder; use room_sorter::{RoomSort, RoomSorter}; use rooms_corridors_bsp::BspCorridors; use rooms_corridors_dogleg::DoglegCorridors; +use rooms_corridors_nearest::NearestCorridors; use simple_map::SimpleMapBuilder; use specs::prelude::*; use voronoi::VoronoiCellBuilder; diff --git a/src/map_builders/rooms_corridors_nearest.rs b/src/map_builders/rooms_corridors_nearest.rs new file mode 100644 index 0000000..33832d9 --- /dev/null +++ b/src/map_builders/rooms_corridors_nearest.rs @@ -0,0 +1,66 @@ +use std::collections::HashSet; + +use rltk::RandomNumberGenerator; + +use super::common::draw_corridor; +use super::{BuilderMap, MetaMapBuilder}; +use crate::Rect; + +pub struct NearestCorridors {} + +impl MetaMapBuilder for NearestCorridors { + #[allow(dead_code)] + fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + self.corridors(rng, build_data); + } +} + +impl NearestCorridors { + #[allow(dead_code)] + pub fn new() -> Box { + Box::new(NearestCorridors {}) + } + + fn corridors(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + let rooms: Vec; + if let Some(rooms_builder) = &build_data.rooms { + rooms = rooms_builder.clone(); + } else { + panic!("Nearest Corridors requires a builder with room structures"); + } + + let mut connected: HashSet = HashSet::new(); + for (i, room) in rooms.iter().enumerate() { + let mut room_distance: Vec<(usize, f32)> = Vec::new(); + let room_center = room.center(); + let room_center_pt = rltk::Point::new(room_center.0, room_center.1); + + for (j, other_room) in rooms.iter().enumerate() { + if i != j && !connected.contains(&j) { + let other_center = other_room.center(); + let other_center_pt = rltk::Point::new(other_center.0, other_center.1); + let distance = + rltk::DistanceAlg::Pythagoras.distance2d(room_center_pt, other_center_pt); + + room_distance.push((j, distance)); + } + } + + if !room_distance.is_empty() { + room_distance.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap()); + let dest_center = rooms[room_distance[0].0].center(); + + draw_corridor( + &mut build_data.map, + room_center.0, + room_center.1, + dest_center.0, + dest_center.1, + ); + + connected.insert(i); + build_data.take_snapshot(); + } + } + } +}