2021-12-09 10:31:43 -05:00
|
|
|
mod common;
|
|
|
|
mod constraints;
|
|
|
|
mod solver;
|
2021-12-07 15:13:02 -05:00
|
|
|
|
2021-12-24 10:38:44 -05:00
|
|
|
use ::rltk::RandomNumberGenerator;
|
2021-12-09 10:31:43 -05:00
|
|
|
use common::*;
|
|
|
|
use constraints::*;
|
|
|
|
use solver::*;
|
2021-12-10 20:16:48 -05:00
|
|
|
|
2022-01-28 14:01:24 -05:00
|
|
|
use super::{BuilderMap, MetaMapBuilder};
|
2021-12-15 12:08:23 -05:00
|
|
|
use crate::Map;
|
2021-12-07 15:13:02 -05:00
|
|
|
|
2021-12-15 11:07:19 -05:00
|
|
|
pub struct WaveformCollapseBuilder {}
|
2021-12-10 16:34:11 -05:00
|
|
|
|
2021-12-15 11:07:19 -05:00
|
|
|
impl MetaMapBuilder for WaveformCollapseBuilder {
|
|
|
|
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-07 15:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WaveformCollapseBuilder {
|
2021-12-15 11:07:19 -05:00
|
|
|
/// Constructor for waveform collapse.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn new() -> Box<WaveformCollapseBuilder> {
|
|
|
|
Box::new(WaveformCollapseBuilder {})
|
2021-12-07 15:13:02 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 11:07:19 -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;
|
2021-12-15 11:07:19 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-09 10:31:43 -05:00
|
|
|
|
2021-12-15 11:07:19 -05:00
|
|
|
let patterns = build_patterns(&build_data.map, CHUNK_SIZE, true, true);
|
2021-12-09 10:31:43 -05:00
|
|
|
let constraints = patterns_to_constraints(patterns, CHUNK_SIZE);
|
2021-12-15 11:07:19 -05:00
|
|
|
self.render_tile_gallery(&constraints, CHUNK_SIZE, build_data);
|
2021-12-09 10:31:43 -05:00
|
|
|
|
2022-01-28 14:01:24 -05:00
|
|
|
let old_map = build_data.map.clone();
|
|
|
|
|
2022-01-04 12:12:08 -05:00
|
|
|
build_data.map = Map::new(
|
|
|
|
build_data.map.depth,
|
|
|
|
build_data.width,
|
|
|
|
build_data.height,
|
|
|
|
&build_data.map.name,
|
|
|
|
);
|
2022-01-28 14:01:24 -05:00
|
|
|
build_data.spawn_list.clear();
|
|
|
|
build_data.rooms = None;
|
|
|
|
build_data.corridors = None;
|
|
|
|
let mut tries = 0;
|
2021-12-09 10:31:43 -05:00
|
|
|
loop {
|
2021-12-15 11:07:19 -05:00
|
|
|
let mut solver = Solver::new(constraints.clone(), CHUNK_SIZE, &build_data.map);
|
|
|
|
while !solver.iteration(&mut build_data.map, rng) {
|
|
|
|
build_data.take_snapshot();
|
2021-12-09 10:31:43 -05:00
|
|
|
}
|
2021-12-15 11:07:19 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-09 10:31:43 -05:00
|
|
|
if solver.possible {
|
|
|
|
break;
|
|
|
|
} // If it has hit an impossible condition, try again
|
2022-01-28 14:01:24 -05:00
|
|
|
tries += 1;
|
|
|
|
if tries > 10 {
|
|
|
|
break;
|
|
|
|
}
|
2021-12-09 10:31:43 -05:00
|
|
|
}
|
|
|
|
|
2022-01-28 14:01:24 -05:00
|
|
|
if tries > 10 {
|
|
|
|
// Restore the old one
|
|
|
|
build_data.map = old_map;
|
|
|
|
}
|
2021-12-07 15:13:02 -05:00
|
|
|
}
|
2021-12-09 10:31:43 -05:00
|
|
|
|
2021-12-15 11:07:19 -05:00
|
|
|
fn render_tile_gallery(
|
|
|
|
&mut self,
|
|
|
|
constraints: &[MapChunk],
|
|
|
|
chunk_size: i32,
|
|
|
|
build_data: &mut BuilderMap,
|
|
|
|
) {
|
2022-01-28 14:01:24 -05:00
|
|
|
build_data.map = Map::new(
|
|
|
|
build_data.map.depth,
|
|
|
|
build_data.width,
|
|
|
|
build_data.height,
|
|
|
|
&build_data.map.name,
|
|
|
|
);
|
2021-12-09 10:31:43 -05:00
|
|
|
let mut counter = 0;
|
|
|
|
let mut x = 1;
|
|
|
|
let mut y = 1;
|
|
|
|
|
|
|
|
while counter < constraints.len() {
|
2021-12-15 11:07:19 -05:00
|
|
|
render_pattern_to_map(&mut build_data.map, &constraints[counter], chunk_size, x, y);
|
2021-12-09 10:31:43 -05:00
|
|
|
|
|
|
|
x += chunk_size + 1;
|
2021-12-15 11:07:19 -05:00
|
|
|
if x + chunk_size > build_data.map.width {
|
2021-12-09 10:31:43 -05:00
|
|
|
// Move to the next row
|
|
|
|
x = 1;
|
|
|
|
y += chunk_size + 1;
|
|
|
|
|
2021-12-15 11:07:19 -05:00
|
|
|
if y + chunk_size > build_data.map.height {
|
2021-12-09 10:31:43 -05:00
|
|
|
// Move to the next page
|
2021-12-15 11:07:19 -05:00
|
|
|
build_data.take_snapshot();
|
2022-01-28 14:05:32 -05:00
|
|
|
build_data.map = Map::new(
|
|
|
|
build_data.map.depth,
|
|
|
|
build_data.width,
|
|
|
|
build_data.height,
|
|
|
|
&build_data.map.name,
|
|
|
|
);
|
2021-12-09 10:31:43 -05:00
|
|
|
|
|
|
|
x = 1;
|
|
|
|
y = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
counter += 1;
|
|
|
|
}
|
2021-12-15 11:07:19 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-09 10:31:43 -05:00
|
|
|
}
|
2021-12-07 15:13:02 -05:00
|
|
|
}
|