roguelike-game/src/map_builders/waveform_collapse/mod.rs

155 lines
4.5 KiB
Rust
Raw Normal View History

mod common;
mod constraints;
2021-12-07 15:13:02 -05:00
mod image_loader;
mod solver;
2021-12-07 15:13:02 -05:00
use super::common::{
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant,
};
use super::MapBuilder;
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
use common::*;
use constraints::*;
use image_loader::*;
2021-12-07 15:13:02 -05:00
use rltk::RandomNumberGenerator;
use solver::*;
2021-12-07 15:13:02 -05:00
use specs::prelude::*;
use std::collections::HashMap;
pub struct WaveformCollapseBuilder {
map: Map,
starting_position: Position,
depth: i32,
history: Vec<Map>,
noise_areas: HashMap<i32, Vec<usize>>,
}
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 {
self.map.clone()
}
fn get_starting_position(&self) -> Position {
self.starting_position.clone()
}
fn get_snapshot_history(&self) -> Vec<Map> {
self.history.clone()
}
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);
}
}
}
impl WaveformCollapseBuilder {
pub fn new(new_depth: i32) -> WaveformCollapseBuilder {
WaveformCollapseBuilder {
map: Map::new(new_depth),
starting_position: Position { x: 0, y: 0 },
depth: new_depth,
history: Vec::new(),
noise_areas: HashMap::new(),
}
}
fn build(&mut self) {
let mut rng = RandomNumberGenerator::new();
const CHUNK_SIZE: i32 = 7;
self.map = load_rex_map(
self.depth,
&rltk::XpFile::from_resource("../resources/wfc-demo2.xp").unwrap(),
);
2021-12-07 15:13:02 -05:00
self.take_snapshot();
let patterns = build_patterns(&self.map, CHUNK_SIZE, true, true);
let constraints = patterns_to_constraints(patterns, CHUNK_SIZE);
self.render_tile_gallery(&constraints, CHUNK_SIZE);
self.map = Map::new(self.depth);
loop {
let mut solver = Solver::new(constraints.clone(), CHUNK_SIZE, &self.map);
while !solver.iteration(&mut self.map, &mut rng) {
self.take_snapshot();
}
self.take_snapshot();
if solver.possible {
break;
} // If it has hit an impossible condition, try again
}
2021-12-07 15:13:02 -05:00
// Find a starting point; start at the middle and walk left until we find an open tile
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);
// }
2021-12-07 15:13:02 -05:00
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);
}
fn render_tile_gallery(&mut self, constraints: &Vec<MapChunk>, chunk_size: i32) {
self.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 self.map, &constraints[counter], chunk_size, x, y);
x += chunk_size + 1;
if x + chunk_size > self.map.width {
// Move to the next row
x = 1;
y += chunk_size + 1;
if y + chunk_size > self.map.height {
// Move to the next page
self.take_snapshot();
self.map = Map::new(0);
x = 1;
y = 1;
}
}
counter += 1;
}
self.take_snapshot();
}
2021-12-07 15:13:02 -05:00
}