Cleanup some code for the start of Section 4.12

This commit is contained in:
Timothy Warren 2021-12-10 11:30:58 -05:00
parent 9879fa30b6
commit 54e9e684e7
10 changed files with 84 additions and 138 deletions

View File

@ -15,16 +15,6 @@ pub struct BspDungeonBuilder {
} }
impl MapBuilder for BspDungeonBuilder { impl MapBuilder for BspDungeonBuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for room in self.rooms.iter().skip(1) {
spawner::spawn_room(ecs, room, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -37,6 +27,16 @@ impl MapBuilder for BspDungeonBuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for room in self.rooms.iter().skip(1) {
spawner::spawn_room(ecs, room, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();

View File

@ -16,16 +16,6 @@ pub struct CellularAutomataBuilder {
} }
impl MapBuilder for CellularAutomataBuilder { impl MapBuilder for CellularAutomataBuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -38,6 +28,16 @@ impl MapBuilder for CellularAutomataBuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();

View File

@ -28,16 +28,6 @@ pub struct DLABuilder {
} }
impl MapBuilder for DLABuilder { impl MapBuilder for DLABuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -50,6 +40,16 @@ impl MapBuilder for DLABuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();

View File

@ -32,16 +32,6 @@ pub struct DrunkardsWalkBuilder {
} }
impl MapBuilder for DrunkardsWalkBuilder { impl MapBuilder for DrunkardsWalkBuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -54,6 +44,16 @@ impl MapBuilder for DrunkardsWalkBuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();

View File

@ -15,16 +15,6 @@ pub struct MazeBuilder {
} }
impl MapBuilder for MazeBuilder { impl MapBuilder for MazeBuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -37,6 +27,16 @@ impl MapBuilder for MazeBuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();

View File

@ -23,17 +23,17 @@ use voronoi::VoronoiCellBuilder;
use waveform_collapse::WaveformCollapseBuilder; use waveform_collapse::WaveformCollapseBuilder;
pub trait MapBuilder { pub trait MapBuilder {
fn build_map(&mut self);
fn spawn_entities(&mut self, ecs: &mut World);
fn get_map(&self) -> Map; fn get_map(&self) -> Map;
fn get_starting_position(&self) -> Position; fn get_starting_position(&self) -> Position;
fn get_snapshot_history(&self) -> Vec<Map>; fn get_snapshot_history(&self) -> Vec<Map>;
fn build_map(&mut self);
fn spawn_entities(&mut self, ecs: &mut World);
fn take_snapshot(&mut self); fn take_snapshot(&mut self);
} }
pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> { pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
let mut rng = rltk::RandomNumberGenerator::new(); let mut rng = rltk::RandomNumberGenerator::new();
let mut result: Box<dyn MapBuilder> = match rng.roll_dice(1, 17) { let mut result: Box<dyn MapBuilder> = match rng.roll_dice(1, 16) {
1 => Box::new(BspDungeonBuilder::new(new_depth)), 1 => Box::new(BspDungeonBuilder::new(new_depth)),
2 => Box::new(BspInteriorBuilder::new(new_depth)), 2 => Box::new(BspInteriorBuilder::new(new_depth)),
3 => Box::new(CellularAutomataBuilder::new(new_depth)), 3 => Box::new(CellularAutomataBuilder::new(new_depth)),
@ -49,7 +49,6 @@ pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
13 => Box::new(DLABuilder::insectoid(new_depth)), 13 => Box::new(DLABuilder::insectoid(new_depth)),
14 => Box::new(VoronoiCellBuilder::pythagoras(new_depth)), 14 => Box::new(VoronoiCellBuilder::pythagoras(new_depth)),
15 => Box::new(VoronoiCellBuilder::manhattan(new_depth)), 15 => Box::new(VoronoiCellBuilder::manhattan(new_depth)),
16 => Box::new(WaveformCollapseBuilder::test_map(new_depth)),
_ => Box::new(SimpleMapBuilder::new(new_depth)), _ => Box::new(SimpleMapBuilder::new(new_depth)),
}; };

View File

@ -13,16 +13,6 @@ pub struct SimpleMapBuilder {
} }
impl MapBuilder for SimpleMapBuilder { impl MapBuilder for SimpleMapBuilder {
fn build_map(&mut self) {
self.rooms_and_corridors();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for room in self.rooms.iter().skip(1) {
spawner::spawn_room(ecs, room, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -35,6 +25,16 @@ impl MapBuilder for SimpleMapBuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.rooms_and_corridors();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for room in self.rooms.iter().skip(1) {
spawner::spawn_room(ecs, room, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();

View File

@ -26,16 +26,6 @@ pub struct VoronoiCellBuilder {
} }
impl MapBuilder for VoronoiCellBuilder { impl MapBuilder for VoronoiCellBuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -48,6 +38,16 @@ impl MapBuilder for VoronoiCellBuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();

View File

@ -1,25 +0,0 @@
use crate::{Map, TileType};
use rltk::rex::XpFile;
/// Loads a RexPaint file, and converts it into our map format
pub fn load_rex_map(new_depth: i32, xp_file: &XpFile) -> Map {
let mut map = Map::new(new_depth);
for layer in &xp_file.layers {
for y in 0..layer.height {
for x in 0..layer.width {
let cell = layer.get(x, y).unwrap();
if x < map.width as usize && y < map.height as usize {
let idx = map.xy_idx(x as i32, y as i32);
match cell.ch {
32 => map.tiles[idx] = TileType::Floor, // #
35 => map.tiles[idx] = TileType::Wall, // #
_ => {}
}
}
}
}
}
map
}

View File

@ -1,6 +1,5 @@
mod common; mod common;
mod constraints; mod constraints;
mod image_loader;
mod solver; mod solver;
use super::common::{ use super::common::{
@ -10,39 +9,21 @@ use super::MapBuilder;
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER}; use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
use common::*; use common::*;
use constraints::*; use constraints::*;
use image_loader::*;
use rltk::RandomNumberGenerator; use rltk::RandomNumberGenerator;
use solver::*; use solver::*;
use specs::prelude::*; use specs::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(PartialEq, Copy, Clone)]
pub enum WaveformMode {
TestMap,
Derived,
}
pub struct WaveformCollapseBuilder { pub struct WaveformCollapseBuilder {
map: Map, map: Map,
starting_position: Position, starting_position: Position,
depth: i32, depth: i32,
history: Vec<Map>, history: Vec<Map>,
noise_areas: HashMap<i32, Vec<usize>>, noise_areas: HashMap<i32, Vec<usize>>,
mode: WaveformMode,
derive_from: Option<Box<dyn MapBuilder>>, derive_from: Option<Box<dyn MapBuilder>>,
} }
impl MapBuilder for WaveformCollapseBuilder { impl MapBuilder for WaveformCollapseBuilder {
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn get_map(&self) -> Map { fn get_map(&self) -> Map {
self.map.clone() self.map.clone()
} }
@ -55,6 +36,16 @@ impl MapBuilder for WaveformCollapseBuilder {
self.history.clone() self.history.clone()
} }
fn build_map(&mut self) {
self.build();
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
fn take_snapshot(&mut self) { fn take_snapshot(&mut self) {
if SHOW_MAPGEN_VISUALIZER { if SHOW_MAPGEN_VISUALIZER {
let mut snapshot = self.map.clone(); let mut snapshot = self.map.clone();
@ -73,7 +64,6 @@ impl WaveformCollapseBuilder {
/// * derive_from - either None, or a boxed MapBuilder, as output by `random_builder` /// * derive_from - either None, or a boxed MapBuilder, as output by `random_builder`
pub fn new( pub fn new(
new_depth: i32, new_depth: i32,
mode: WaveformMode,
derive_from: Option<Box<dyn MapBuilder>>, derive_from: Option<Box<dyn MapBuilder>>,
) -> WaveformCollapseBuilder { ) -> WaveformCollapseBuilder {
WaveformCollapseBuilder { WaveformCollapseBuilder {
@ -82,37 +72,19 @@ impl WaveformCollapseBuilder {
depth: new_depth, depth: new_depth,
history: Vec::new(), history: Vec::new(),
noise_areas: HashMap::new(), noise_areas: HashMap::new(),
mode,
derive_from, derive_from,
} }
} }
/// Creates a Wave Function Collapse builder using the baked-in WFC test map.
/// # Arguments
/// * new_depth - the new map depth
pub fn test_map(new_depth: i32) -> WaveformCollapseBuilder {
WaveformCollapseBuilder::new(new_depth, WaveformMode::TestMap, None)
}
/// Derives a map from a pre-existing map builder. /// Derives a map from a pre-existing map builder.
/// # Arguments /// # Arguments
/// * new_depth - the new map depth /// * new_depth - the new map depth
/// * derive_from - either None, or a boxed MapBuilder, as output by `random_builder` /// * derive_from - either None, or a boxed MapBuilder, as output by `random_builder`
pub fn derived_map(new_depth: i32, builder: Box<dyn MapBuilder>) -> WaveformCollapseBuilder { pub fn derived_map(new_depth: i32, builder: Box<dyn MapBuilder>) -> WaveformCollapseBuilder {
WaveformCollapseBuilder::new(new_depth, WaveformMode::Derived, Some(builder)) WaveformCollapseBuilder::new(new_depth, Some(builder))
} }
fn build(&mut self) { fn build(&mut self) {
if self.mode == WaveformMode::TestMap {
self.map = load_rex_map(
self.depth,
&rltk::XpFile::from_resource("../resources/wfc-demo1.xp").unwrap(),
);
self.take_snapshot();
return;
}
let mut rng = RandomNumberGenerator::new(); let mut rng = RandomNumberGenerator::new();
const CHUNK_SIZE: i32 = 8; const CHUNK_SIZE: i32 = 8;