roguelike-game/src/map_builders/waveform_collapse.rs

92 lines
2.6 KiB
Rust
Raw Normal View History

mod common;
mod constraints;
mod solver;
2021-12-07 15:13:02 -05:00
2021-12-10 20:16:48 -05:00
use std::collections::HashMap;
use common::*;
use constraints::*;
2021-12-07 15:13:02 -05:00
use rltk::RandomNumberGenerator;
use solver::*;
2021-12-10 20:16:48 -05:00
use super::common::{
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant,
};
use super::MapBuilder;
use crate::map_builders::{BuilderMap, MetaMapBuilder};
2021-12-10 20:16:48 -05:00
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
2021-12-07 15:13:02 -05:00
pub struct WaveformCollapseBuilder {}
impl MetaMapBuilder for WaveformCollapseBuilder {
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.build(rng, build_data);
}
2021-12-07 15:13:02 -05:00
}
impl WaveformCollapseBuilder {
/// Constructor for waveform collapse.
#[allow(dead_code)]
pub fn new() -> Box<WaveformCollapseBuilder> {
Box::new(WaveformCollapseBuilder {})
2021-12-07 15:13:02 -05:00
}
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
2021-12-09 11:34:46 -05:00
const CHUNK_SIZE: i32 = 8;
build_data.take_snapshot();
let patterns = build_patterns(&build_data.map, CHUNK_SIZE, true, true);
let constraints = patterns_to_constraints(patterns, CHUNK_SIZE);
self.render_tile_gallery(&constraints, CHUNK_SIZE, build_data);
build_data.map = Map::new(build_data.map.depth);
loop {
let mut solver = Solver::new(constraints.clone(), CHUNK_SIZE, &build_data.map);
while !solver.iteration(&mut build_data.map, rng) {
build_data.take_snapshot();
}
build_data.take_snapshot();
if solver.possible {
break;
} // If it has hit an impossible condition, try again
}
build_data.spawn_list.clear();
2021-12-07 15:13:02 -05:00
}
fn render_tile_gallery(
&mut self,
constraints: &[MapChunk],
chunk_size: i32,
build_data: &mut BuilderMap,
) {
build_data.map = Map::new(0);
let mut counter = 0;
let mut x = 1;
let mut y = 1;
while counter < constraints.len() {
render_pattern_to_map(&mut build_data.map, &constraints[counter], chunk_size, x, y);
x += chunk_size + 1;
if x + chunk_size > build_data.map.width {
// Move to the next row
x = 1;
y += chunk_size + 1;
if y + chunk_size > build_data.map.height {
// Move to the next page
build_data.take_snapshot();
build_data.map = Map::new(0);
x = 1;
y = 1;
}
}
counter += 1;
}
build_data.take_snapshot();
}
2021-12-07 15:13:02 -05:00
}