Split corridor creation from room creation for simple_map builder

This commit is contained in:
Timothy Warren 2021-12-16 11:38:53 -05:00
parent 9ef557d938
commit 23dfbeeeb9
3 changed files with 51 additions and 20 deletions

View File

@ -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())

View File

@ -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<DoglegCorridors> {
Box::new(DoglegCorridors {})
}
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!("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();
}
}
}
}

View File

@ -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();
}