Decompose bsp_dungeon builder into rooms, corridors, and sorting

This commit is contained in:
Timothy Warren 2021-12-16 13:26:48 -05:00
parent 23dfbeeeb9
commit b0b154c8d7
4 changed files with 77 additions and 19 deletions

View File

@ -14,6 +14,8 @@ mod room_based_stairs;
mod room_based_starting_position;
mod room_corner_rounding;
mod room_exploder;
mod room_sorter;
mod rooms_corridors_bsp;
mod rooms_corridors_dogleg;
mod simple_map;
mod voronoi;
@ -36,6 +38,8 @@ use room_based_stairs::RoomBasedStairs;
use room_based_starting_position::RoomBasedStartingPosition;
use room_corner_rounding::RoomCornerRounder;
use room_exploder::RoomExploder;
use room_sorter::RoomSorter;
use rooms_corridors_bsp::BspCorridors;
use rooms_corridors_dogleg::DoglegCorridors;
use simple_map::SimpleMapBuilder;
use specs::prelude::*;
@ -212,7 +216,8 @@ pub fn random_builder(new_depth: i32, rng: &mut RandomNumberGenerator) -> Builde
builder
.start_with(BspDungeonBuilder::new())
.with(DoglegCorridors::new())
.with(RoomSorter::new())
.with(BspCorridors::new())
.with(AreaStartingPosition::new(XStart::Center, YStart::Center))
.with(CullUnreachable::new())
.with(VoronoiSpawning::new())

View File

@ -51,24 +51,6 @@ impl BspDungeonBuilder {
n_rooms += 1;
}
// Now we sort the rooms
rooms.sort_by(|a, b| a.x1.cmp(&b.x1));
// Now we want corridors
for i in 0..rooms.len() - 1 {
let room = rooms[i];
let next_room = 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);
draw_corridor(&mut build_data.map, start_x, start_y, end_x, end_y);
build_data.take_snapshot();
}
build_data.rooms = Some(rooms);
}

View File

@ -0,0 +1,27 @@
use rltk::RandomNumberGenerator;
use super::{BuilderMap, MetaMapBuilder};
pub struct RoomSorter {}
impl MetaMapBuilder for RoomSorter {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.sorter(rng, build_data);
}
}
impl RoomSorter {
#[allow(dead_code)]
pub fn new() -> Box<RoomSorter> {
Box::new(RoomSorter {})
}
fn sorter(&mut self, _rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
build_data
.rooms
.as_mut()
.unwrap()
.sort_by(|a, b| a.x1.cmp(&b.x1));
}
}

View File

@ -0,0 +1,44 @@
use rltk::RandomNumberGenerator;
use super::common::draw_corridor;
use super::{BuilderMap, MetaMapBuilder};
use crate::Rect;
pub struct BspCorridors {}
impl MetaMapBuilder for BspCorridors {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.corridors(rng, build_data);
}
}
impl BspCorridors {
#[allow(dead_code)]
pub fn new() -> Box<BspCorridors> {
Box::new(BspCorridors {})
}
fn corridors(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
let rooms: Vec<Rect>;
if let Some(rooms_builder) = &build_data.rooms {
rooms = rooms_builder.clone();
} else {
panic!("BSP Corridors require a builder with room structures");
}
for i in 0..rooms.len() - 1 {
let room = rooms[i];
let next_room = 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);
draw_corridor(&mut build_data.map, start_x, start_y, end_x, end_y);
build_data.take_snapshot();
}
}
}