fun with layers, and inventory display refactor

This commit is contained in:
Llywelwyn 2023-07-23 15:50:56 +01:00
parent 50e3cb50bc
commit b7b2061228
7 changed files with 122 additions and 129 deletions

View file

@ -1,4 +1,4 @@
use super::{BuilderMap, InitialMapBuilder, TileType};
use super::{BuilderMap, InitialMapBuilder, MetaMapBuilder, TileType};
use rltk::RandomNumberGenerator;
pub struct CellularAutomataBuilder {}
@ -10,6 +10,13 @@ impl InitialMapBuilder for CellularAutomataBuilder {
}
}
impl MetaMapBuilder for CellularAutomataBuilder {
#[allow(dead_code)]
fn build_map(&mut self, _rng: &mut rltk::RandomNumberGenerator, build_data: &mut BuilderMap) {
self.apply_iteration(build_data);
}
}
impl CellularAutomataBuilder {
#[allow(dead_code)]
pub fn new() -> Box<CellularAutomataBuilder> {
@ -34,47 +41,51 @@ impl CellularAutomataBuilder {
// Now we iteratively apply cellular automata rules
for _i in 0..15 {
let mut newtiles = build_data.map.tiles.clone();
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);
let mut neighbors = 0;
if build_data.map.tiles[idx - 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if neighbors > 4 || neighbors == 0 {
newtiles[idx] = TileType::Wall;
} else {
newtiles[idx] = TileType::Floor;
}
}
}
build_data.map.tiles = newtiles.clone();
build_data.take_snapshot();
self.apply_iteration(build_data);
}
}
fn apply_iteration(&mut self, build_data: &mut BuilderMap) {
let mut newtiles = build_data.map.tiles.clone();
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);
let mut neighbors = 0;
if build_data.map.tiles[idx - 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + 1] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + build_data.map.width as usize] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx - (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize - 1)] == TileType::Wall {
neighbors += 1;
}
if build_data.map.tiles[idx + (build_data.map.width as usize + 1)] == TileType::Wall {
neighbors += 1;
}
if neighbors > 4 || neighbors == 0 {
newtiles[idx] = TileType::Wall;
} else {
newtiles[idx] = TileType::Floor;
}
}
}
build_data.map.tiles = newtiles.clone();
build_data.take_snapshot();
}
}

View file

@ -1,4 +1,4 @@
use super::{paint, BuilderMap, InitialMapBuilder, Position, Symmetry, TileType};
use super::{paint, BuilderMap, InitialMapBuilder, MetaMapBuilder, Position, Symmetry, TileType};
use rltk::RandomNumberGenerator;
#[derive(PartialEq, Copy, Clone)]
@ -23,6 +23,13 @@ impl InitialMapBuilder for DLABuilder {
}
}
impl MetaMapBuilder for DLABuilder {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut rltk::RandomNumberGenerator, build_data: &mut BuilderMap) {
self.build(rng, build_data);
}
}
impl DLABuilder {
#[allow(dead_code)]
pub fn new() -> Box<DLABuilder> {
@ -74,6 +81,16 @@ impl DLABuilder {
})
}
#[allow(dead_code)]
pub fn heavy_erosion() -> Box<DLABuilder> {
Box::new(DLABuilder {
algorithm: DLAAlgorithm::WalkInwards,
brush_size: 2,
symmetry: Symmetry::None,
floor_percent: 0.35,
})
}
#[allow(clippy::map_entry)]
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
// Carve a starting seed

View file

@ -1,4 +1,4 @@
use super::{paint, BuilderMap, InitialMapBuilder, Position, Symmetry, TileType};
use super::{paint, BuilderMap, InitialMapBuilder, MetaMapBuilder, Position, Symmetry, TileType};
use rltk::RandomNumberGenerator;
#[derive(PartialEq, Copy, Clone)]
@ -27,6 +27,13 @@ impl InitialMapBuilder for DrunkardsWalkBuilder {
}
}
impl MetaMapBuilder for DrunkardsWalkBuilder {
#[allow(dead_code)]
fn build_map(&mut self, rng: &mut rltk::RandomNumberGenerator, build_data: &mut BuilderMap) {
self.build(rng, build_data);
}
}
impl DrunkardsWalkBuilder {
#[allow(dead_code)]
pub fn new(settings: DrunkardSettings) -> DrunkardsWalkBuilder {

View file

@ -146,30 +146,11 @@ fn random_initial_builder(rng: &mut rltk::RandomNumberGenerator) -> (Box<dyn Ini
pub fn random_builder(new_depth: i32, rng: &mut rltk::RandomNumberGenerator) -> BuilderChain {
let mut builder = BuilderChain::new(new_depth);
let (random_starter, has_rooms) = random_initial_builder(rng);
builder.start_with(random_starter);
if has_rooms {
builder.with(RoomBasedSpawner::new());
builder.with(RoomBasedStairs::new());
builder.with(RoomBasedStartingPosition::new());
} else {
builder.with(AreaStartingPosition::new(XStart::CENTRE, YStart::CENTRE));
builder.with(CullUnreachable::new());
builder.with(VoronoiSpawning::new());
builder.with(DistantExit::new());
}
if rng.roll_dice(1, 3) == 1 {
builder.with(WaveFunctionCollapseBuilder::new());
builder.with(CullUnreachable::new());
builder.with(VoronoiSpawning::new());
}
if rng.roll_dice(1, 20) == 1 {
builder.with(PrefabBuilder::sectional(prefab_builder::prefab_sections::UNDERGROUND_FORT));
}
builder.with(PrefabBuilder::vaults());
builder.start_with(simple_map::SimpleMapBuilder::new());
builder.with(DLABuilder::heavy_erosion());
builder.with(AreaStartingPosition::new(XStart::CENTRE, YStart::CENTRE));
builder.with(CullUnreachable::new());
builder.with(VoronoiSpawning::new());
builder.with(DistantExit::new());
builder
}