Track corridor tile locations, and update corridor builders to update the map builder state

This commit is contained in:
Timothy Warren 2021-12-17 11:14:41 -05:00
parent 5cbd914a98
commit 132d1dac62
6 changed files with 67 additions and 17 deletions

View File

@ -60,6 +60,7 @@ pub struct BuilderMap {
pub map: Map,
pub starting_position: Option<Position>,
pub rooms: Option<Vec<Rect>>,
pub corridors: Option<Vec<Vec<usize>>>,
pub history: Vec<Map>,
}
@ -70,6 +71,7 @@ impl BuilderMap {
map: Map::new(new_depth),
starting_position: None,
rooms: None,
corridors: None,
history: Vec::new(),
}
}

View File

@ -11,27 +11,44 @@ pub enum Symmetry {
Both,
}
pub fn apply_horizontal_tunnel(map: &mut Map, x1: i32, x2: i32, y: i32) {
pub fn apply_horizontal_tunnel(map: &mut Map, x1: i32, x2: i32, y: i32) -> Vec<usize> {
let mut corridor = Vec::new();
for x in min(x1, x2)..=max(x1, x2) {
let idx = map.xy_idx(x, y);
if idx > 0 && idx < map.width as usize * map.height as usize {
if idx > 0
&& idx < map.width as usize * map.height as usize
&& map.tiles[idx as usize] != TileType::Floor
{
map.tiles[idx as usize] = TileType::Floor;
corridor.push(idx as usize);
}
}
corridor
}
pub fn apply_vertical_tunnel(map: &mut Map, y1: i32, y2: i32, x: i32) {
pub fn apply_vertical_tunnel(map: &mut Map, y1: i32, y2: i32, x: i32) -> Vec<usize> {
let mut corridor = Vec::new();
for y in min(y1, y2)..=max(y1, y2) {
let idx = map.xy_idx(x, y);
if idx > 0 && idx < map.width as usize * map.height as usize {
if idx > 0
&& idx < map.width as usize * map.height as usize
&& map.tiles[idx as usize] != TileType::Floor
{
map.tiles[idx as usize] = TileType::Floor;
corridor.push(idx as usize);
}
}
corridor
}
pub fn draw_corridor(map: &mut Map, x1: i32, y1: i32, x2: i32, y2: i32) {
pub fn draw_corridor(map: &mut Map, x1: i32, y1: i32, x2: i32, y2: i32) -> Vec<usize> {
let mut corridor = Vec::new();
let mut x = x1;
let mut y = y1;
@ -47,8 +64,13 @@ pub fn draw_corridor(map: &mut Map, x1: i32, y1: i32, x2: i32, y2: i32) {
}
let idx = map.xy_idx(x, y);
if map.tiles[idx] != TileType::Floor {
map.tiles[idx] = TileType::Floor;
corridor.push(idx);
}
}
corridor
}
pub fn paint(map: &mut Map, mode: Symmetry, brush_size: i32, x: i32, y: i32) {

View File

@ -27,6 +27,7 @@ impl BspCorridors {
panic!("BSP Corridors require a builder with room structures");
}
let mut corridors: Vec<Vec<usize>> = Vec::new();
for i in 0..rooms.len() - 1 {
let room = rooms[i];
let next_room = rooms[i + 1];
@ -37,8 +38,11 @@ impl BspCorridors {
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);
let corridor = draw_corridor(&mut build_data.map, start_x, start_y, end_x, end_y);
corridors.push(corridor);
build_data.take_snapshot();
}
build_data.corridors = Some(corridors);
}
}

View File

@ -27,20 +27,30 @@ impl DoglegCorridors {
panic!("Dogleg Corridors require a builder with room structures");
}
let mut corridors: Vec<Vec<usize>> = Vec::new();
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 {
let mut c1 =
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);
let mut c2 = apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, new_x);
c1.append(&mut c2);
corridors.push(c1);
} 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);
let mut c1 = apply_vertical_tunnel(&mut build_data.map, prev_y, new_y, prev_x);
let mut c2 = apply_horizontal_tunnel(&mut build_data.map, prev_x, new_x, new_y);
c1.append(&mut c2);
corridors.push(c1);
}
build_data.take_snapshot();
}
}
build_data.corridors = Some(corridors);
}
}

View File

@ -1,7 +1,9 @@
use std::collections::HashSet;
use rltk::RandomNumberGenerator;
use super::{BuilderMap, MetaMapBuilder};
use crate::{Rect, TileType};
use rltk::RandomNumberGenerator;
use std::collections::HashSet;
pub struct StraightLineCorridors {}
@ -27,6 +29,7 @@ impl StraightLineCorridors {
}
let mut connected: HashSet<usize> = HashSet::new();
let mut corridors: Vec<Vec<usize>> = Vec::new();
for (i, room) in rooms.iter().enumerate() {
let mut room_distance: Vec<(usize, f32)> = Vec::new();
let room_center = room.center();
@ -51,15 +54,21 @@ impl StraightLineCorridors {
room_center_pt,
rltk::Point::new(dest_center.0, dest_center.1),
);
let mut corridor = Vec::new();
for cell in line.iter() {
let idx = build_data.map.xy_idx(cell.x, cell.y);
if build_data.map.tiles[idx] != TileType::Floor {
build_data.map.tiles[idx] = TileType::Floor;
corridor.push(idx);
}
}
corridors.push(corridor);
connected.insert(i);
build_data.take_snapshot();
}
}
build_data.corridors = Some(corridors);
}
}

View File

@ -30,6 +30,7 @@ impl NearestCorridors {
}
let mut connected: HashSet<usize> = HashSet::new();
let mut corridors: Vec<Vec<usize>> = Vec::new();
for (i, room) in rooms.iter().enumerate() {
let mut room_distance: Vec<(usize, f32)> = Vec::new();
let room_center = room.center();
@ -49,8 +50,7 @@ impl NearestCorridors {
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(
let corridor = draw_corridor(
&mut build_data.map,
room_center.0,
room_center.1,
@ -59,8 +59,11 @@ impl NearestCorridors {
);
connected.insert(i);
corridors.push(corridor);
build_data.take_snapshot();
}
}
build_data.corridors = Some(corridors);
}
}