voronoi, and starting on wfc
This commit is contained in:
parent
09bafa4d1f
commit
7f775b80dd
10 changed files with 301 additions and 4 deletions
BIN
resources/wfc-demo1.xp
Normal file
BIN
resources/wfc-demo1.xp
Normal file
Binary file not shown.
BIN
resources/wfc-demo2.xp
Normal file
BIN
resources/wfc-demo2.xp
Normal file
Binary file not shown.
BIN
resources/wfc-populated.xp
Normal file
BIN
resources/wfc-populated.xp
Normal file
Binary file not shown.
|
|
@ -206,7 +206,7 @@ pub fn draw_map(map: &Map, ctx: &mut Rltk) {
|
|||
|
||||
fn is_revealed_and_wall(map: &Map, x: i32, y: i32) -> bool {
|
||||
let idx = map.xy_idx(x, y);
|
||||
map.tiles[idx] == TileType::Wall //&& map.revealed_tiles[idx]
|
||||
map.tiles[idx] == TileType::Wall && map.revealed_tiles[idx]
|
||||
}
|
||||
|
||||
fn wall_glyph(map: &Map, x: i32, y: i32) -> rltk::FontCharType {
|
||||
|
|
@ -277,8 +277,11 @@ fn wall_glyph(map: &Map, x: i32, y: i32) -> rltk::FontCharType {
|
|||
55 => 185,
|
||||
59 => 204,
|
||||
63 => 203,
|
||||
87 => 185,
|
||||
126 => 203,
|
||||
143 => 206,
|
||||
77 => 202,
|
||||
171 => 204,
|
||||
187 => 204,
|
||||
215 => 185,
|
||||
190 => 203,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ mod dla;
|
|||
mod drunkard;
|
||||
mod maze;
|
||||
mod simple_map;
|
||||
mod voronoi;
|
||||
mod wfc;
|
||||
use common::*;
|
||||
use rltk::RandomNumberGenerator;
|
||||
use specs::prelude::*;
|
||||
|
|
@ -22,7 +24,7 @@ pub trait MapBuilder {
|
|||
|
||||
pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
||||
/*let mut rng = rltk::RandomNumberGenerator::new();
|
||||
let builder = rng.roll_dice(1, 14);
|
||||
let builder = rng.roll_dice(1, 17);
|
||||
match builder {
|
||||
1 => Box::new(bsp_dungeon::BspDungeonBuilder::new(new_depth)),
|
||||
2 => Box::new(bsp_interior::BspInteriorBuilder::new(new_depth)),
|
||||
|
|
@ -37,7 +39,10 @@ pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
|||
9 => Box::new(dla::DLABuilder::walk_outwards(new_depth)),
|
||||
10 => Box::new(dla::DLABuilder::central_attractor(new_depth)),
|
||||
11 => Box::new(dla::DLABuilder::insectoid(new_depth)),
|
||||
12 => Box::new(voronoi::VoronoiBuilder::pythagoras(new_depth)),
|
||||
12 => Box::new(voronoi::VoronoiBuilder::manhattan(new_depth)),
|
||||
12 => Box::new(voronoi::VoronoiBuilder::chebyshev(new_depth)),
|
||||
_ => Box::new(simple_map::SimpleMapBuilder::new(new_depth)),
|
||||
}*/
|
||||
Box::new(drunkard::DrunkardsWalkBuilder::fearful_symmetry(new_depth))
|
||||
Box::new(wfc::WaveFunctionCollapseBuilder::new(new_depth))
|
||||
}
|
||||
|
|
|
|||
177
src/map_builders/voronoi.rs
Normal file
177
src/map_builders/voronoi.rs
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
use super::{
|
||||
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant, spawner, Map, MapBuilder,
|
||||
Position, TileType, SHOW_MAPGEN,
|
||||
};
|
||||
use rltk::RandomNumberGenerator;
|
||||
use specs::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
pub enum DistanceAlgorithm {
|
||||
Pythagoras,
|
||||
Manhattan,
|
||||
Chebyshev,
|
||||
}
|
||||
|
||||
pub struct VoronoiBuilder {
|
||||
map: Map,
|
||||
starting_position: Position,
|
||||
depth: i32,
|
||||
history: Vec<Map>,
|
||||
noise_areas: HashMap<i32, Vec<usize>>,
|
||||
n_seeds: usize,
|
||||
distance_algorithm: DistanceAlgorithm,
|
||||
}
|
||||
|
||||
impl MapBuilder for VoronoiBuilder {
|
||||
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 VoronoiBuilder {
|
||||
pub fn pythagoras(new_depth: i32) -> VoronoiBuilder {
|
||||
VoronoiBuilder {
|
||||
map: Map::new(new_depth),
|
||||
starting_position: Position { x: 0, y: 0 },
|
||||
depth: new_depth,
|
||||
history: Vec::new(),
|
||||
noise_areas: HashMap::new(),
|
||||
n_seeds: 64,
|
||||
distance_algorithm: DistanceAlgorithm::Pythagoras,
|
||||
}
|
||||
}
|
||||
pub fn manhattan(new_depth: i32) -> VoronoiBuilder {
|
||||
VoronoiBuilder {
|
||||
map: Map::new(new_depth),
|
||||
starting_position: Position { x: 0, y: 0 },
|
||||
depth: new_depth,
|
||||
history: Vec::new(),
|
||||
noise_areas: HashMap::new(),
|
||||
n_seeds: 64,
|
||||
distance_algorithm: DistanceAlgorithm::Manhattan,
|
||||
}
|
||||
}
|
||||
pub fn chebyshev(new_depth: i32) -> VoronoiBuilder {
|
||||
VoronoiBuilder {
|
||||
map: Map::new(new_depth),
|
||||
starting_position: Position { x: 0, y: 0 },
|
||||
depth: new_depth,
|
||||
history: Vec::new(),
|
||||
noise_areas: HashMap::new(),
|
||||
n_seeds: 64,
|
||||
distance_algorithm: DistanceAlgorithm::Chebyshev,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::map_entry)]
|
||||
fn build(&mut self, rng: &mut RandomNumberGenerator) {
|
||||
// Make a Voronoi diagram. We'll do this the hard way to learn about the technique!
|
||||
let mut voronoi_seeds: Vec<(usize, rltk::Point)> = Vec::new();
|
||||
|
||||
while voronoi_seeds.len() < self.n_seeds {
|
||||
let vx = rng.roll_dice(1, self.map.width - 1);
|
||||
let vy = rng.roll_dice(1, self.map.height - 1);
|
||||
let vidx = self.map.xy_idx(vx, vy);
|
||||
let candidate = (vidx, rltk::Point::new(vx, vy));
|
||||
if !voronoi_seeds.contains(&candidate) {
|
||||
voronoi_seeds.push(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
let mut voronoi_distance = vec![(0, 0.0f32); self.n_seeds];
|
||||
let mut voronoi_membership: Vec<i32> = vec![0; self.map.width as usize * self.map.height as usize];
|
||||
for (i, vid) in voronoi_membership.iter_mut().enumerate() {
|
||||
let x = i as i32 % self.map.width;
|
||||
let y = i as i32 / self.map.width;
|
||||
|
||||
for (seed, pos) in voronoi_seeds.iter().enumerate() {
|
||||
let distance;
|
||||
match self.distance_algorithm {
|
||||
DistanceAlgorithm::Pythagoras => {
|
||||
distance = rltk::DistanceAlg::PythagorasSquared.distance2d(rltk::Point::new(x, y), pos.1);
|
||||
}
|
||||
DistanceAlgorithm::Manhattan => {
|
||||
distance = rltk::DistanceAlg::Manhattan.distance2d(rltk::Point::new(x, y), pos.1);
|
||||
}
|
||||
DistanceAlgorithm::Chebyshev => {
|
||||
distance = rltk::DistanceAlg::Chebyshev.distance2d(rltk::Point::new(x, y), pos.1);
|
||||
}
|
||||
}
|
||||
voronoi_distance[seed] = (seed, distance);
|
||||
}
|
||||
|
||||
voronoi_distance.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
|
||||
|
||||
*vid = voronoi_distance[0].0 as i32;
|
||||
}
|
||||
|
||||
for y in 1..self.map.height - 1 {
|
||||
for x in 1..self.map.width - 1 {
|
||||
let mut neighbors = 0;
|
||||
let my_idx = self.map.xy_idx(x, y);
|
||||
let my_seed = voronoi_membership[my_idx];
|
||||
if voronoi_membership[self.map.xy_idx(x - 1, y)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
if voronoi_membership[self.map.xy_idx(x + 1, y)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
if voronoi_membership[self.map.xy_idx(x, y - 1)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
if voronoi_membership[self.map.xy_idx(x, y + 1)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
|
||||
if neighbors < 2 {
|
||||
self.map.tiles[my_idx] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
self.take_snapshot();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
24
src/map_builders/wfc/image_loader.rs
Normal file
24
src/map_builders/wfc/image_loader.rs
Normal 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
|
||||
}
|
||||
84
src/map_builders/wfc/mod.rs
Normal file
84
src/map_builders/wfc/mod.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
use rltk::rex::XpFile;
|
||||
|
||||
rltk::embedded_resource!(CAVE_TUNNEL, "../resources/cave_tunnel80x60.xp");
|
||||
rltk::embedded_resource!(WFC_DEMO_IMAGE1, "../resources/wfc-demo1.xp");
|
||||
rltk::embedded_resource!(WFC_DEMO_IMAGE2, "../resources/wfc-demo2.xp");
|
||||
|
||||
pub struct RexAssets {
|
||||
pub menu: XpFile,
|
||||
|
|
@ -10,6 +12,8 @@ impl RexAssets {
|
|||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> RexAssets {
|
||||
rltk::link_resource!(CAVE_TUNNEL, "../resources/cave_tunnel80x60.xp");
|
||||
rltk::link_resource!(WFC_DEMO_IMAGE1, "../resources/wfc-demo1.xp");
|
||||
rltk::link_resource!(WFC_DEMO_IMAGE2, "../resources/wfc-demo2.xp");
|
||||
|
||||
RexAssets { menu: XpFile::from_resource("../resources/cave_tunnel80x60.xp").unwrap() }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ pub fn roll_hit_dice(ecs: &mut World, n: i32, d: i32) -> i32 {
|
|||
}
|
||||
|
||||
// Consts
|
||||
const MAX_ENTITIES: i32 = 4;
|
||||
const MAX_ENTITIES: i32 = 2;
|
||||
|
||||
#[allow(clippy::map_entry)]
|
||||
pub fn spawn_room(ecs: &mut World, room: &Rect, map_depth: i32) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue