From e8c03b953671c2f467f19992b3b8cac28cdf6cff Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 3 Dec 2021 14:15:17 -0500 Subject: [PATCH] Add corridors to the bsp interior map builder, completing section 4.4 --- src/map_builders/bsp_interior.rs | 46 ++++++++++++++++++++++++++++++-- src/map_builders/mod.rs | 3 ++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/map_builders/bsp_interior.rs b/src/map_builders/bsp_interior.rs index 29b142f..2105d55 100644 --- a/src/map_builders/bsp_interior.rs +++ b/src/map_builders/bsp_interior.rs @@ -1,5 +1,4 @@ use super::MapBuilder; -use crate::map_builders::common::apply_room_to_map; use crate::spawner; use crate::{components::Position, Map, Rect, TileType, SHOW_MAPGEN_VISUALIZER}; use rltk::RandomNumberGenerator; @@ -22,7 +21,7 @@ impl MapBuilder for BspInteriorBuilder { } fn get_starting_position(&self) -> Position { - self.starting_position.clone() + self.starting_position } fn get_snapshot_history(&self) -> Vec { @@ -91,6 +90,29 @@ impl BspInteriorBuilder { self.take_snapshot(); } + // Now we sort the rooms + self.rooms.sort_by(|a, b| a.x1.cmp(&b.x1)); + + // Now we want corridors + for i in 0..self.rooms.len() - 1 { + let room = self.rooms[i]; + let next_room = self.rooms[i + 1]; + let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2)) - 1); + let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2) - 1)); + let end_x = + next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2)) - 1); + let end_y = + next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2)) - 1); + + self.draw_corridor(start_x, start_y, end_x, end_y); + self.take_snapshot(); + } + + // Don't forget the stairs + let stairs = self.rooms[self.rooms.len() - 1].center(); + let stairs_idx = self.map.xy_idx(stairs.0, stairs.1); + self.map.tiles[stairs_idx] = TileType::DownStairs; + let start = self.rooms[0].center(); self.starting_position = start.into(); } @@ -137,4 +159,24 @@ impl BspInteriorBuilder { } } } + + fn draw_corridor(&mut self, x1: i32, y1: i32, x2: i32, y2: i32) { + let mut x = x1; + let mut y = y1; + + while x != x2 || y != y2 { + if x < x2 { + x += 1; + } else if x > x2 { + x -= 1; + } else if y < y2 { + y += 1; + } else if y > y2 { + y -= 1; + } + + let idx = self.map.xy_idx(x, y); + self.map.tiles[idx] = TileType::Floor; + } + } } diff --git a/src/map_builders/mod.rs b/src/map_builders/mod.rs index 31a6138..f0b3a98 100644 --- a/src/map_builders/mod.rs +++ b/src/map_builders/mod.rs @@ -21,8 +21,9 @@ pub trait MapBuilder { pub fn random_builder(new_depth: i32) -> Box { let mut rng = rltk::RandomNumberGenerator::new(); - match rng.roll_dice(1, 2) { + match rng.roll_dice(1, 3) { 1 => Box::new(BspDungeonBuilder::new(new_depth)), + 2 => Box::new(BspInteriorBuilder::new(new_depth)), _ => Box::new(SimpleMapBuilder::new(new_depth)), } }