voronoi, and starting on wfc

This commit is contained in:
Llywelwyn 2023-07-20 15:26:51 +01:00
parent 09bafa4d1f
commit 7f775b80dd
10 changed files with 301 additions and 4 deletions

View file

@ -0,0 +1,24 @@
use super::{Map, TileType};
use rltk::rex::XpFile;
// Load RexPaint file, convert to map format
pub fn load_rex_map(new_depth: i32, xp_file: &XpFile) -> Map {
let mut map: 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

@ -0,0 +1,84 @@
use super::{
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant, spawner, Map, MapBuilder,
Position, TileType, SHOW_MAPGEN,
};
mod image_loader;
use image_loader::load_rex_map;
use rltk::RandomNumberGenerator;
use specs::prelude::*;
use std::collections::HashMap;
pub struct WaveFunctionCollapseBuilder {
map: Map,
starting_position: Position,
depth: i32,
history: Vec<Map>,
noise_areas: HashMap<i32, Vec<usize>>,
}
impl MapBuilder for WaveFunctionCollapseBuilder {
fn build_map(&mut self, rng: &mut RandomNumberGenerator) {
return self.build(rng);
}
fn spawn_entities(&mut self, ecs: &mut World) {
for area in self.noise_areas.iter() {
spawner::spawn_region(ecs, area.1, self.depth);
}
}
// Getters
fn get_map(&mut self) -> Map {
return self.map.clone();
}
fn get_starting_pos(&mut self) -> Position {
return self.starting_position.clone();
}
// Mapgen visualisation stuff
fn get_snapshot_history(&self) -> Vec<Map> {
return self.history.clone();
}
fn take_snapshot(&mut self) {
if SHOW_MAPGEN {
let mut snapshot = self.map.clone();
for v in snapshot.revealed_tiles.iter_mut() {
*v = true;
}
self.history.push(snapshot);
}
}
}
impl WaveFunctionCollapseBuilder {
pub fn new(new_depth: i32) -> WaveFunctionCollapseBuilder {
WaveFunctionCollapseBuilder {
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, rng: &mut RandomNumberGenerator) {
self.map = load_rex_map(self.depth, &rltk::rex::XpFile::from_resource("../resources/wfc-demo1.xp").unwrap());
// 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);
//}
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::DownStair;
self.take_snapshot();
// Now we build a noise map for use in spawning entities later
self.noise_areas = generate_voronoi_spawn_regions(&self.map, rng);
}
}