From 23dfbeeeb90cd7cf18a0734bef296b49c1b184fc Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 16 Dec 2021 11:38:53 -0500 Subject: [PATCH] Split corridor creation from room creation for simple_map builder --- src/map_builders.rs | 4 +- src/map_builders/rooms_corridors_dogleg.rs | 46 ++++++++++++++++++++++ src/map_builders/simple_map.rs | 21 +--------- 3 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/map_builders/rooms_corridors_dogleg.rs diff --git a/src/map_builders.rs b/src/map_builders.rs index cd704d6..b96d267 100644 --- a/src/map_builders.rs +++ b/src/map_builders.rs @@ -14,6 +14,7 @@ mod room_based_stairs; mod room_based_starting_position; mod room_corner_rounding; mod room_exploder; +mod rooms_corridors_dogleg; mod simple_map; mod voronoi; mod voronoi_spawning; @@ -35,6 +36,7 @@ use room_based_stairs::RoomBasedStairs; use room_based_starting_position::RoomBasedStartingPosition; use room_corner_rounding::RoomCornerRounder; use room_exploder::RoomExploder; +use rooms_corridors_dogleg::DoglegCorridors; use simple_map::SimpleMapBuilder; use specs::prelude::*; use voronoi::VoronoiCellBuilder; @@ -210,7 +212,7 @@ pub fn random_builder(new_depth: i32, rng: &mut RandomNumberGenerator) -> Builde builder .start_with(BspDungeonBuilder::new()) - .with(RoomCornerRounder::new()) + .with(DoglegCorridors::new()) .with(AreaStartingPosition::new(XStart::Center, YStart::Center)) .with(CullUnreachable::new()) .with(VoronoiSpawning::new()) diff --git a/src/map_builders/rooms_corridors_dogleg.rs b/src/map_builders/rooms_corridors_dogleg.rs new file mode 100644 index 0000000..0c55fce --- /dev/null +++ b/src/map_builders/rooms_corridors_dogleg.rs @@ -0,0 +1,46 @@ +use rltk::RandomNumberGenerator; + +use super::common::{apply_horizontal_tunnel, apply_vertical_tunnel}; +use crate::map_builders::{BuilderMap, MetaMapBuilder}; +use crate::Rect; + +pub struct DoglegCorridors {} + +impl MetaMapBuilder for DoglegCorridors { + #[allow(dead_code)] + fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { + self.corridors(rng, build_data); + } +} + +impl DoglegCorridors { + #[allow(dead_code)] + pub fn new() -> Box { + Box::new(DoglegCorridors {}) + } + + 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!("Dogleg Corridors require a builder with room structures"); + } + + for (i, room) in rooms.iter().enumerate() { + if i > 0 { + let (new_x, new_y) = room.center(); + let (prev_x, prev_y) = rooms[i as usize - 1].center(); + + if rng.range(0, 2) == 1 { + apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, prev_y); + apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, new_x); + } else { + apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, prev_x); + apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, new_y); + } + build_data.take_snapshot(); + } + } + } +} diff --git a/src/map_builders/simple_map.rs b/src/map_builders/simple_map.rs index ea09711..ee9e174 100644 --- a/src/map_builders/simple_map.rs +++ b/src/map_builders/simple_map.rs @@ -11,7 +11,7 @@ pub struct SimpleMapBuilder {} impl InitialMapBuilder for SimpleMapBuilder { #[allow(dead_code)] fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - self.rooms_and_corridors(rng, build_data); + self.build_rooms(rng, build_data); } } @@ -21,11 +21,7 @@ impl SimpleMapBuilder { Box::new(SimpleMapBuilder {}) } - fn rooms_and_corridors( - &mut self, - rng: &mut RandomNumberGenerator, - build_data: &mut BuilderMap, - ) { + fn build_rooms(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { const MAX_ROOMS: i32 = 30; const MIN_SIZE: i32 = 6; const MAX_SIZE: i32 = 10; @@ -50,19 +46,6 @@ impl SimpleMapBuilder { apply_room_to_map(&mut build_data.map, &new_room); build_data.take_snapshot(); - if !rooms.is_empty() { - let (new_x, new_y) = new_room.center(); - let (prev_x, prev_y) = rooms[rooms.len() - 1].center(); - - if rng.range(0, 2) == 1 { - apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, prev_y); - apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, new_x); - } else { - apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, prev_x); - apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, new_y); - } - } - rooms.push(new_room); build_data.take_snapshot(); }