drunkards walk

This commit is contained in:
Llywelwyn 2023-07-15 23:19:46 +01:00
parent d96d4881d5
commit 363ff4c0a3
5 changed files with 280 additions and 44 deletions

View file

@ -1,4 +1,7 @@
use super::{spawner, Map, MapBuilder, Position, TileType, SHOW_MAPGEN};
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;
@ -122,50 +125,15 @@ impl CellularAutomataBuilder {
start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
}
// Find all tiles we can reach from the starting point
let map_starts: Vec<usize> = vec![start_idx];
let dijkstra_map = rltk::DijkstraMap::new(self.map.width, self.map.height, &map_starts, &self.map, 200.0);
let mut exit_tile = (0, 0.0f32);
for (i, tile) in self.map.tiles.iter_mut().enumerate() {
if *tile == TileType::Floor {
let distance_to_start = dijkstra_map.map[i];
// We can't get to this tile - so we'll make it a wall
if distance_to_start == std::f32::MAX {
*tile = TileType::Wall;
} else {
// If it is further away than our current exit candidate, move the exit
if distance_to_start > exit_tile.1 {
exit_tile.0 = i;
exit_tile.1 = distance_to_start;
}
}
}
}
// Find all tiles reachable from starting pos
let exit_tile = remove_unreachable_areas_returning_most_distant(&mut self.map, start_idx);
self.take_snapshot();
self.map.tiles[exit_tile.0] = TileType::DownStair;
// Place stairs
self.map.tiles[exit_tile] = TileType::DownStair;
self.take_snapshot();
// Build noise map for spawning entities
let mut noise = rltk::FastNoise::seeded(rng.roll_dice(1, 65536) as u64);
noise.set_noise_type(rltk::NoiseType::Cellular);
noise.set_frequency(0.08);
noise.set_cellular_distance_function(rltk::CellularDistanceFunction::Manhattan);
for y in 1..self.map.height - 1 {
for x in 1..self.map.width - 1 {
let idx = self.map.xy_idx(x, y);
if self.map.tiles[idx] == TileType::Floor {
let cell_value_f = noise.get_noise(x as f32, y as f32) * 10240.0;
let cell_value = cell_value_f as i32;
if self.noise_areas.contains_key(&cell_value) {
self.noise_areas.get_mut(&cell_value).unwrap().push(idx);
} else {
self.noise_areas.insert(cell_value, vec![idx]);
}
}
}
}
// Noise map for spawning entities
self.noise_areas = generate_voronoi_spawn_regions(&self.map, rng);
}
}