2022-02-04 14:09:48 -05:00
|
|
|
use ::bracket_lib::prelude::*;
|
|
|
|
|
2021-12-15 12:08:23 -05:00
|
|
|
use super::common::{paint, Symmetry};
|
2021-12-10 20:16:48 -05:00
|
|
|
use crate::components::Position;
|
2021-12-15 13:58:47 -05:00
|
|
|
use crate::map_builders::{BuilderMap, InitialMapBuilder, MetaMapBuilder};
|
2022-02-03 14:59:35 -05:00
|
|
|
use crate::rng::roll_dice;
|
2021-12-15 12:08:23 -05:00
|
|
|
use crate::TileType;
|
2021-12-07 10:17:13 -05:00
|
|
|
|
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
|
|
|
pub enum DLAAlgorithm {
|
|
|
|
WalkInwards,
|
|
|
|
WalkOutwards,
|
|
|
|
CentralAttractor,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DLABuilder {
|
|
|
|
algorithm: DLAAlgorithm,
|
|
|
|
brush_size: i32,
|
2021-12-07 10:23:52 -05:00
|
|
|
symmetry: Symmetry,
|
2021-12-07 10:17:13 -05:00
|
|
|
floor_percent: f32,
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
impl InitialMapBuilder for DLABuilder {
|
2022-02-03 14:59:35 -05:00
|
|
|
fn build_map(&mut self, build_data: &mut BuilderMap) {
|
|
|
|
self.build(build_data);
|
2021-12-10 16:34:11 -05:00
|
|
|
}
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 13:58:47 -05:00
|
|
|
impl MetaMapBuilder for DLABuilder {
|
2022-02-03 14:59:35 -05:00
|
|
|
fn build_map(&mut self, build_data: &mut BuilderMap) {
|
|
|
|
self.build(build_data);
|
2021-12-15 13:58:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 10:17:13 -05:00
|
|
|
impl DLABuilder {
|
2021-12-15 10:45:58 -05:00
|
|
|
pub fn new() -> DLABuilder {
|
2021-12-07 10:17:13 -05:00
|
|
|
DLABuilder {
|
|
|
|
algorithm: DLAAlgorithm::WalkOutwards,
|
|
|
|
brush_size: 1,
|
2021-12-07 10:23:52 -05:00
|
|
|
symmetry: Symmetry::None,
|
2021-12-07 10:17:13 -05:00
|
|
|
floor_percent: 0.25,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
pub fn walk_inwards() -> Box<DLABuilder> {
|
|
|
|
Box::new(DLABuilder {
|
2021-12-07 10:17:13 -05:00
|
|
|
algorithm: DLAAlgorithm::WalkInwards,
|
2021-12-15 10:45:58 -05:00
|
|
|
..DLABuilder::new()
|
|
|
|
})
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
pub fn walk_outwards() -> Box<DLABuilder> {
|
|
|
|
Box::new(DLABuilder {
|
2021-12-07 10:17:13 -05:00
|
|
|
brush_size: 2,
|
2021-12-15 10:45:58 -05:00
|
|
|
..DLABuilder::new()
|
|
|
|
})
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
pub fn central_attractor() -> Box<DLABuilder> {
|
|
|
|
Box::new(DLABuilder {
|
2021-12-07 10:17:13 -05:00
|
|
|
algorithm: DLAAlgorithm::CentralAttractor,
|
|
|
|
brush_size: 2,
|
2021-12-15 10:45:58 -05:00
|
|
|
..DLABuilder::new()
|
|
|
|
})
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
pub fn insectoid() -> Box<DLABuilder> {
|
|
|
|
Box::new(DLABuilder {
|
2021-12-07 10:17:13 -05:00
|
|
|
algorithm: DLAAlgorithm::CentralAttractor,
|
|
|
|
brush_size: 2,
|
2021-12-07 10:23:52 -05:00
|
|
|
symmetry: Symmetry::Horizontal,
|
2021-12-15 10:45:58 -05:00
|
|
|
..DLABuilder::new()
|
|
|
|
})
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-16 14:26:50 -05:00
|
|
|
#[allow(dead_code)]
|
2021-12-15 13:58:47 -05:00
|
|
|
pub fn heavy_erosion() -> Box<DLABuilder> {
|
|
|
|
Box::new(DLABuilder {
|
|
|
|
algorithm: DLAAlgorithm::WalkInwards,
|
|
|
|
brush_size: 2,
|
|
|
|
floor_percent: 0.25,
|
|
|
|
..DLABuilder::new()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-07 10:17:13 -05:00
|
|
|
#[allow(clippy::map_entry)]
|
2022-02-03 14:59:35 -05:00
|
|
|
fn build(&mut self, build_data: &mut BuilderMap) {
|
2021-12-15 10:45:58 -05:00
|
|
|
let starting_position = Position {
|
|
|
|
x: build_data.map.width / 2,
|
|
|
|
y: build_data.map.height / 2,
|
2021-12-07 10:17:13 -05:00
|
|
|
};
|
2021-12-15 10:45:58 -05:00
|
|
|
let start_idx = build_data
|
2021-12-07 10:17:13 -05:00
|
|
|
.map
|
2021-12-15 10:45:58 -05:00
|
|
|
.xy_idx(starting_position.x, starting_position.y);
|
|
|
|
build_data.take_snapshot();
|
2021-12-07 10:17:13 -05:00
|
|
|
|
|
|
|
// Carve a starting seed
|
2021-12-15 10:45:58 -05:00
|
|
|
build_data.map.tiles[start_idx] = TileType::Floor;
|
|
|
|
build_data.map.tiles[start_idx - 1] = TileType::Floor;
|
|
|
|
build_data.map.tiles[start_idx + 1] = TileType::Floor;
|
|
|
|
build_data.map.tiles[start_idx - build_data.map.width as usize] = TileType::Floor;
|
|
|
|
build_data.map.tiles[start_idx + build_data.map.width as usize] = TileType::Floor;
|
2021-12-07 10:17:13 -05:00
|
|
|
|
|
|
|
// Random walker
|
2021-12-15 10:45:58 -05:00
|
|
|
let total_tiles = build_data.map.width * build_data.map.height;
|
2021-12-07 10:17:13 -05:00
|
|
|
let desired_floor_tiles = (self.floor_percent * total_tiles as f32) as usize;
|
2021-12-15 10:45:58 -05:00
|
|
|
let mut floor_tile_count = build_data
|
2021-12-07 10:17:13 -05:00
|
|
|
.map
|
|
|
|
.tiles
|
|
|
|
.iter()
|
|
|
|
.filter(|a| **a == TileType::Floor)
|
|
|
|
.count();
|
|
|
|
|
|
|
|
while floor_tile_count < desired_floor_tiles {
|
|
|
|
match self.algorithm {
|
|
|
|
DLAAlgorithm::WalkInwards => {
|
2022-02-03 14:59:35 -05:00
|
|
|
let mut digger_x = roll_dice(1, build_data.map.width - 3) + 1;
|
|
|
|
let mut digger_y = roll_dice(1, build_data.map.height - 3) + 1;
|
2021-12-07 10:17:13 -05:00
|
|
|
let mut prev_x = digger_x;
|
|
|
|
let mut prev_y = digger_y;
|
2021-12-15 10:45:58 -05:00
|
|
|
let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y);
|
2021-12-07 10:17:13 -05:00
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
while build_data.map.tiles[digger_idx] == TileType::Wall {
|
2021-12-07 10:17:13 -05:00
|
|
|
prev_x = digger_x;
|
|
|
|
prev_y = digger_y;
|
|
|
|
|
2022-02-03 14:59:35 -05:00
|
|
|
let stagger_direction = roll_dice(1, 4);
|
2021-12-07 10:17:13 -05:00
|
|
|
match stagger_direction {
|
|
|
|
1 => {
|
|
|
|
if digger_x > 2 {
|
|
|
|
digger_x -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
2 => {
|
2021-12-15 10:45:58 -05:00
|
|
|
if digger_x < build_data.map.width - 2 {
|
2021-12-07 10:17:13 -05:00
|
|
|
digger_x += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
3 => {
|
|
|
|
if digger_y > 2 {
|
|
|
|
digger_y -= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2021-12-15 10:45:58 -05:00
|
|
|
if digger_y < build_data.map.height - 2 {
|
2021-12-07 10:17:13 -05:00
|
|
|
digger_y += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
digger_idx = build_data.map.xy_idx(digger_x, digger_y);
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-07 10:23:52 -05:00
|
|
|
paint(
|
2021-12-15 10:45:58 -05:00
|
|
|
&mut build_data.map,
|
2021-12-07 10:23:52 -05:00
|
|
|
self.symmetry,
|
|
|
|
self.brush_size,
|
|
|
|
prev_x,
|
|
|
|
prev_y,
|
|
|
|
);
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DLAAlgorithm::WalkOutwards => {
|
2021-12-15 10:45:58 -05:00
|
|
|
let mut digger_x = starting_position.x;
|
|
|
|
let mut digger_y = starting_position.y;
|
|
|
|
let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y);
|
2021-12-07 10:17:13 -05:00
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
while build_data.map.tiles[digger_idx] == TileType::Floor {
|
2022-02-03 14:59:35 -05:00
|
|
|
let stagger_direction = roll_dice(1, 4);
|
2021-12-07 10:17:13 -05:00
|
|
|
match stagger_direction {
|
|
|
|
1 => {
|
|
|
|
if digger_x > 2 {
|
|
|
|
digger_x -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
2 => {
|
2021-12-15 10:45:58 -05:00
|
|
|
if digger_x < build_data.map.width - 2 {
|
2021-12-07 10:17:13 -05:00
|
|
|
digger_x += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
3 => {
|
|
|
|
if digger_y > 2 {
|
|
|
|
digger_y -= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2021-12-15 10:45:58 -05:00
|
|
|
if digger_y < build_data.map.height - 2 {
|
2021-12-07 10:17:13 -05:00
|
|
|
digger_y += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
digger_idx = build_data.map.xy_idx(digger_x, digger_y);
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-07 10:23:52 -05:00
|
|
|
paint(
|
2021-12-15 10:45:58 -05:00
|
|
|
&mut build_data.map,
|
2021-12-07 10:23:52 -05:00
|
|
|
self.symmetry,
|
|
|
|
self.brush_size,
|
|
|
|
digger_x,
|
|
|
|
digger_y,
|
|
|
|
);
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DLAAlgorithm::CentralAttractor => {
|
2022-02-03 14:59:35 -05:00
|
|
|
let mut digger_x = roll_dice(1, build_data.map.width - 3) + 1;
|
|
|
|
let mut digger_y = roll_dice(1, build_data.map.height - 3) + 1;
|
2021-12-07 10:17:13 -05:00
|
|
|
let mut prev_x = digger_x;
|
|
|
|
let mut prev_y = digger_y;
|
2021-12-15 10:45:58 -05:00
|
|
|
let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y);
|
2021-12-07 10:17:13 -05:00
|
|
|
|
2022-02-04 14:09:48 -05:00
|
|
|
let mut path = line2d(
|
|
|
|
LineAlg::Bresenham,
|
|
|
|
Point::new(digger_x, digger_y),
|
|
|
|
Point::new(starting_position.x, starting_position.y),
|
2021-12-07 10:17:13 -05:00
|
|
|
);
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
while build_data.map.tiles[digger_idx] == TileType::Wall && !path.is_empty() {
|
2021-12-07 10:17:13 -05:00
|
|
|
prev_x = digger_x;
|
|
|
|
prev_y = digger_y;
|
|
|
|
digger_x = path[0].x;
|
|
|
|
digger_y = path[0].y;
|
|
|
|
path.remove(0);
|
2021-12-15 10:45:58 -05:00
|
|
|
digger_idx = build_data.map.xy_idx(digger_x, digger_y);
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-07 10:23:52 -05:00
|
|
|
paint(
|
2021-12-15 10:45:58 -05:00
|
|
|
&mut build_data.map,
|
2021-12-07 10:23:52 -05:00
|
|
|
self.symmetry,
|
|
|
|
self.brush_size,
|
|
|
|
prev_x,
|
|
|
|
prev_y,
|
|
|
|
);
|
2021-12-07 10:17:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-07 10:17:13 -05:00
|
|
|
|
2021-12-15 10:45:58 -05:00
|
|
|
floor_tile_count = build_data
|
2021-12-07 10:17:13 -05:00
|
|
|
.map
|
|
|
|
.tiles
|
|
|
|
.iter()
|
|
|
|
.filter(|a| **a == TileType::Floor)
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|