2021-12-10 20:16:48 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use rltk::RandomNumberGenerator;
|
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
use crate::components::Position;
|
|
|
|
use crate::map_builders::common::{
|
2021-12-06 14:23:35 -05:00
|
|
|
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant,
|
|
|
|
};
|
2021-12-15 10:08:05 -05:00
|
|
|
use crate::map_builders::{BuilderMap, InitialMapBuilder};
|
2021-12-10 20:16:48 -05:00
|
|
|
use crate::{spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER};
|
2021-12-03 15:55:07 -05:00
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
pub struct CellularAutomataBuilder {}
|
2021-12-03 15:55:07 -05:00
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
impl InitialMapBuilder for CellularAutomataBuilder {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
|
|
|
self.build(rng, build_data);
|
2021-12-10 16:34:11 -05:00
|
|
|
}
|
2021-12-03 15:55:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CellularAutomataBuilder {
|
2021-12-15 10:08:05 -05:00
|
|
|
pub fn new() -> Box<CellularAutomataBuilder> {
|
|
|
|
Box::new(CellularAutomataBuilder {})
|
2021-12-03 15:55:07 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
2021-12-03 15:55:07 -05:00
|
|
|
// First we completely randomize the map, setting 55% of it to be floor.
|
2021-12-15 10:08:05 -05:00
|
|
|
for y in 1..build_data.map.height - 1 {
|
|
|
|
for x in 1..build_data.map.width - 1 {
|
2021-12-03 15:55:07 -05:00
|
|
|
let roll = rng.roll_dice(1, 100);
|
2021-12-15 10:08:05 -05:00
|
|
|
let idx = build_data.map.xy_idx(x, y);
|
2021-12-03 15:55:07 -05:00
|
|
|
|
|
|
|
if roll > 55 {
|
2021-12-15 10:08:05 -05:00
|
|
|
build_data.map.tiles[idx] = TileType::Floor
|
2021-12-03 15:55:07 -05:00
|
|
|
} else {
|
2021-12-15 10:08:05 -05:00
|
|
|
build_data.map.tiles[idx] = TileType::Wall
|
2021-12-03 15:55:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-03 15:55:07 -05:00
|
|
|
|
|
|
|
// Now we iteratively apply cellular automata rules
|
|
|
|
for _i in 0..15 {
|
2021-12-15 10:08:05 -05:00
|
|
|
let mut newtiles = build_data.map.tiles.clone();
|
2021-12-03 15:55:07 -05:00
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
for y in 1..build_data.map.height - 1 {
|
|
|
|
for x in 1..build_data.map.width - 1 {
|
|
|
|
let idx = build_data.map.xy_idx(x, y);
|
2021-12-03 15:55:07 -05:00
|
|
|
let mut neighbors = 0;
|
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx - 1] == TileType::Wall {
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx + 1] == TileType::Wall {
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx - build_data.map.width as usize] == TileType::Wall {
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx + build_data.map.width as usize] == TileType::Wall {
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx - (build_data.map.width as usize - 1)]
|
|
|
|
== TileType::Wall
|
|
|
|
{
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx - (build_data.map.width as usize + 1)]
|
|
|
|
== TileType::Wall
|
|
|
|
{
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx + (build_data.map.width as usize - 1)]
|
|
|
|
== TileType::Wall
|
|
|
|
{
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:08:05 -05:00
|
|
|
if build_data.map.tiles[idx + (build_data.map.width as usize + 1)]
|
|
|
|
== TileType::Wall
|
|
|
|
{
|
2021-12-03 15:55:07 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if neighbors > 4 || neighbors == 0 {
|
|
|
|
newtiles[idx] = TileType::Wall;
|
|
|
|
} else {
|
|
|
|
newtiles[idx] = TileType::Floor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:08:05 -05:00
|
|
|
build_data.map.tiles = newtiles.clone();
|
|
|
|
build_data.take_snapshot();
|
2021-12-10 16:34:11 -05:00
|
|
|
}
|
2021-12-03 15:55:07 -05:00
|
|
|
}
|
|
|
|
}
|