Refactor waveform_collapse map builder to use Rust builder pattern
This commit is contained in:
parent
8a596bf93c
commit
71a07b301c
@ -168,7 +168,8 @@ pub trait MapBuilder {
|
|||||||
pub fn random_builder(new_depth: i32, rng: &mut RandomNumberGenerator) -> BuilderChain {
|
pub fn random_builder(new_depth: i32, rng: &mut RandomNumberGenerator) -> BuilderChain {
|
||||||
let mut builder = BuilderChain::new(new_depth);
|
let mut builder = BuilderChain::new(new_depth);
|
||||||
builder
|
builder
|
||||||
.start_with(CellularAutomataBuilder::new())
|
.start_with(VoronoiCellBuilder::pythagoras())
|
||||||
|
.with(WaveformCollapseBuilder::new())
|
||||||
.with(AreaStartingPosition::new(XStart::Center, YStart::Center))
|
.with(AreaStartingPosition::new(XStart::Center, YStart::Center))
|
||||||
.with(CullUnreachable::new())
|
.with(CullUnreachable::new())
|
||||||
.with(VoronoiSpawning::new())
|
.with(VoronoiSpawning::new())
|
||||||
|
@ -13,167 +13,71 @@ use super::common::{
|
|||||||
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant,
|
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant,
|
||||||
};
|
};
|
||||||
use super::MapBuilder;
|
use super::MapBuilder;
|
||||||
|
use crate::map_builders::{BuilderMap, MetaMapBuilder};
|
||||||
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
|
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
|
||||||
|
|
||||||
pub struct WaveformCollapseBuilder {
|
pub struct WaveformCollapseBuilder {}
|
||||||
map: Map,
|
|
||||||
starting_position: Position,
|
|
||||||
depth: i32,
|
|
||||||
history: Vec<Map>,
|
|
||||||
noise_areas: HashMap<i32, Vec<usize>>,
|
|
||||||
derive_from: Option<Box<dyn MapBuilder>>,
|
|
||||||
spawn_list: Vec<(usize, String)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MapBuilder for WaveformCollapseBuilder {
|
impl MetaMapBuilder for WaveformCollapseBuilder {
|
||||||
fn get_map(&self) -> Map {
|
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
||||||
self.map.clone()
|
self.build(rng, build_data);
|
||||||
}
|
|
||||||
|
|
||||||
fn get_starting_position(&self) -> Position {
|
|
||||||
self.starting_position
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_snapshot_history(&self) -> Vec<Map> {
|
|
||||||
self.history.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_map(&mut self) {
|
|
||||||
self.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn take_snapshot(&mut self) {
|
|
||||||
if SHOW_MAPGEN_VISUALIZER {
|
|
||||||
let mut snapshot = self.map.clone();
|
|
||||||
for v in snapshot.revealed_tiles.iter_mut() {
|
|
||||||
*v = true;
|
|
||||||
}
|
|
||||||
self.history.push(snapshot);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
|
|
||||||
&self.spawn_list
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WaveformCollapseBuilder {
|
impl WaveformCollapseBuilder {
|
||||||
/// Generic constructor for waveform collapse.
|
/// Constructor for waveform collapse.
|
||||||
/// # Arguments
|
#[allow(dead_code)]
|
||||||
/// * new_depth - the new map depth
|
pub fn new() -> Box<WaveformCollapseBuilder> {
|
||||||
/// * derive_from - either None, or a boxed MapBuilder, as output by `random_builder`
|
Box::new(WaveformCollapseBuilder {})
|
||||||
pub fn new(
|
|
||||||
new_depth: i32,
|
|
||||||
derive_from: Option<Box<dyn MapBuilder>>,
|
|
||||||
) -> WaveformCollapseBuilder {
|
|
||||||
WaveformCollapseBuilder {
|
|
||||||
map: Map::new(new_depth),
|
|
||||||
starting_position: Position::default(),
|
|
||||||
depth: new_depth,
|
|
||||||
history: Vec::new(),
|
|
||||||
noise_areas: HashMap::new(),
|
|
||||||
derive_from,
|
|
||||||
spawn_list: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Derives a map from a pre-existing map builder.
|
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
||||||
/// # Arguments
|
|
||||||
/// * new_depth - the new map depth
|
|
||||||
/// * 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 {
|
|
||||||
WaveformCollapseBuilder::new(new_depth, Some(builder))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build(&mut self) {
|
|
||||||
let mut rng = RandomNumberGenerator::new();
|
|
||||||
|
|
||||||
const CHUNK_SIZE: i32 = 8;
|
const CHUNK_SIZE: i32 = 8;
|
||||||
|
build_data.take_snapshot();
|
||||||
|
|
||||||
let prebuilder = &mut self.derive_from.as_mut().unwrap();
|
let patterns = build_patterns(&build_data.map, CHUNK_SIZE, true, true);
|
||||||
prebuilder.build_map();
|
|
||||||
self.map = prebuilder.get_map();
|
|
||||||
for t in self.map.tiles.iter_mut() {
|
|
||||||
if *t == TileType::DownStairs {
|
|
||||||
*t = TileType::Floor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.take_snapshot();
|
|
||||||
|
|
||||||
let patterns = build_patterns(&self.map, CHUNK_SIZE, true, true);
|
|
||||||
let constraints = patterns_to_constraints(patterns, CHUNK_SIZE);
|
let constraints = patterns_to_constraints(patterns, CHUNK_SIZE);
|
||||||
self.render_tile_gallery(&constraints, CHUNK_SIZE);
|
self.render_tile_gallery(&constraints, CHUNK_SIZE, build_data);
|
||||||
|
|
||||||
self.map = Map::new(self.depth);
|
build_data.map = Map::new(build_data.map.depth);
|
||||||
loop {
|
loop {
|
||||||
let mut solver = Solver::new(constraints.clone(), CHUNK_SIZE, &self.map);
|
let mut solver = Solver::new(constraints.clone(), CHUNK_SIZE, &build_data.map);
|
||||||
while !solver.iteration(&mut self.map, &mut rng) {
|
while !solver.iteration(&mut build_data.map, rng) {
|
||||||
self.take_snapshot();
|
build_data.take_snapshot();
|
||||||
}
|
}
|
||||||
self.take_snapshot();
|
build_data.take_snapshot();
|
||||||
if solver.possible {
|
if solver.possible {
|
||||||
break;
|
break;
|
||||||
} // If it has hit an impossible condition, try again
|
} // If it has hit an impossible condition, try again
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find a starting point; start at the middle and walk left until we find an open tile
|
build_data.spawn_list.clear();
|
||||||
self.starting_position = Position {
|
|
||||||
x: self.map.width / 2,
|
|
||||||
y: self.map.height / 2,
|
|
||||||
};
|
|
||||||
let mut start_idx = self
|
|
||||||
.map
|
|
||||||
.xy_idx(self.starting_position.x, self.starting_position.y);
|
|
||||||
while self.map.tiles[start_idx] != TileType::Floor {
|
|
||||||
self.starting_position.x -= 1;
|
|
||||||
start_idx = self
|
|
||||||
.map
|
|
||||||
.xy_idx(self.starting_position.x, self.starting_position.y);
|
|
||||||
}
|
|
||||||
self.take_snapshot();
|
|
||||||
|
|
||||||
// Find all tiles we can reach from the starting point
|
|
||||||
let exit_tile = remove_unreachable_areas_returning_most_distant(&mut self.map, start_idx);
|
|
||||||
self.take_snapshot();
|
|
||||||
|
|
||||||
// Place the stairs
|
|
||||||
self.map.tiles[exit_tile] = TileType::DownStairs;
|
|
||||||
self.take_snapshot();
|
|
||||||
|
|
||||||
// Now we build a noise map for use in spawning entities later
|
|
||||||
self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
|
|
||||||
|
|
||||||
// Spawn the entities
|
|
||||||
for area in self.noise_areas.iter() {
|
|
||||||
spawner::spawn_region(
|
|
||||||
&self.map,
|
|
||||||
&mut rng,
|
|
||||||
area.1,
|
|
||||||
self.depth,
|
|
||||||
&mut self.spawn_list,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_tile_gallery(&mut self, constraints: &[MapChunk], chunk_size: i32) {
|
fn render_tile_gallery(
|
||||||
self.map = Map::new(0);
|
&mut self,
|
||||||
|
constraints: &[MapChunk],
|
||||||
|
chunk_size: i32,
|
||||||
|
build_data: &mut BuilderMap,
|
||||||
|
) {
|
||||||
|
build_data.map = Map::new(0);
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
let mut x = 1;
|
let mut x = 1;
|
||||||
let mut y = 1;
|
let mut y = 1;
|
||||||
|
|
||||||
while counter < constraints.len() {
|
while counter < constraints.len() {
|
||||||
render_pattern_to_map(&mut self.map, &constraints[counter], chunk_size, x, y);
|
render_pattern_to_map(&mut build_data.map, &constraints[counter], chunk_size, x, y);
|
||||||
|
|
||||||
x += chunk_size + 1;
|
x += chunk_size + 1;
|
||||||
if x + chunk_size > self.map.width {
|
if x + chunk_size > build_data.map.width {
|
||||||
// Move to the next row
|
// Move to the next row
|
||||||
x = 1;
|
x = 1;
|
||||||
y += chunk_size + 1;
|
y += chunk_size + 1;
|
||||||
|
|
||||||
if y + chunk_size > self.map.height {
|
if y + chunk_size > build_data.map.height {
|
||||||
// Move to the next page
|
// Move to the next page
|
||||||
self.take_snapshot();
|
build_data.take_snapshot();
|
||||||
self.map = Map::new(0);
|
build_data.map = Map::new(0);
|
||||||
|
|
||||||
x = 1;
|
x = 1;
|
||||||
y = 1;
|
y = 1;
|
||||||
@ -182,6 +86,6 @@ impl WaveformCollapseBuilder {
|
|||||||
|
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
self.take_snapshot();
|
build_data.take_snapshot();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user