Start section 4.6

This commit is contained in:
Timothy Warren 2021-12-03 19:33:56 -05:00
parent 545deb24d8
commit b24bf3562c
4 changed files with 147 additions and 47 deletions

View File

@ -3,6 +3,8 @@ use crate::{components::Position, spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER
use rltk::RandomNumberGenerator;
use specs::prelude::*;
use std::collections::HashMap;
use crate::map_builders::common::remove_unreachable_areas_returning_most_distant;
use super::common::generate_voronoi_spawn_regions;
pub struct CellularAutomataBuilder {
map: Map,
@ -139,58 +141,14 @@ impl CellularAutomataBuilder {
}
// 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 == 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;
}
}
}
}
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.0] = TileType::DownStairs;
self.map.tiles[exit_tile] = TileType::DownStairs;
self.take_snapshot();
// Now we build a noise map for use in spawning entities later
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;
#[allow(clippy::map_entry)]
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]);
}
}
}
}
self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
}
}

View File

@ -1,5 +1,6 @@
use crate::{Map, Rect, TileType};
use std::cmp::{max, min};
use std::collections::HashMap;
pub fn apply_room_to_map(map: &mut Map, room: &Rect) {
for y in room.y1 + 1..=room.y2 {
@ -29,3 +30,65 @@ pub fn apply_vertical_tunnel(map: &mut Map, y1: i32, y2: i32, x: i32) {
}
}
}
/// Searches a map, removes unreachable areas and returns the most distant tile.
pub fn remove_unreachable_areas_returning_most_distant(map: &mut Map, start_idx: usize) -> usize {
map.populate_blocked();
let map_starts: Vec<usize> = vec![start_idx];
let dijkstra_map = rltk::DijkstraMap::new(
map.width as usize,
map.height as usize,
&map_starts,
map,
200.0,
);
let mut exit_tile = (0, 0.0f32);
for (i, tile) in 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;
}
}
}
}
exit_tile.0
}
/// Generates a Voronoi/cellular noise map of a region, and divides it into spawn regions.
#[allow(clippy::map_entry)]
pub fn generate_voronoi_spawn_regions(
map: &Map,
rng: &mut rltk::RandomNumberGenerator,
) -> HashMap<i32, Vec<usize>> {
let mut noise_areas: HashMap<i32, Vec<usize>> = HashMap::new();
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..map.height - 1 {
for x in 1..map.width - 1 {
let idx = map.xy_idx(x, y);
if 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 noise_areas.contains_key(&cell_value) {
noise_areas.get_mut(&cell_value).unwrap().push(idx);
} else {
noise_areas.insert(cell_value, vec![idx]);
}
}
}
}
noise_areas
}

View File

@ -0,0 +1,76 @@
use super::MapBuilder;
use crate::{components::Position, spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER};
use rltk::RandomNumberGenerator;
use specs::prelude::*;
use std::collections::HashMap;
pub struct DrunkardsWalkBuilder {
map: Map,
starting_position: Position,
depth: i32,
history: Vec<Map>,
noise_areas: HashMap<i32, Vec<usize>>,
}
impl MapBuilder for DrunkardsWalkBuilder {
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
}
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 DrunkardsWalkBuilder {
pub fn new(new_depth: i32) -> DrunkardsWalkBuilder {
DrunkardsWalkBuilder {
map: Map::new(new_depth),
starting_position: Position::default(),
depth: new_depth,
history: Vec::new(),
noise_areas: HashMap::new(),
}
}
#[allow(clippy::map_entry)]
fn build(&mut self) {
// let mut rng = RandomNumberGenerator::new();
// Set a central starting point
self.starting_position = Position {
x: self.map.width / 2,
y: self.map.height / 2,
};
let start_idx = self
.map
.xy_idx(self.starting_position.x, self.starting_position.y);
self.map.tiles[start_idx] = TileType::Floor;
// @TODO implement the rest
}
}

View File

@ -2,6 +2,7 @@ mod bsp_dungeon;
mod bsp_interior;
mod cellular_automata;
mod common;
mod drunkard;
mod simple_map;
use crate::{Map, Position};
@ -9,6 +10,7 @@ use bsp_dungeon::BspDungeonBuilder;
use bsp_interior::BspInteriorBuilder;
use cellular_automata::CellularAutomataBuilder;
use common::*;
use drunkard::DrunkardsWalkBuilder;
use simple_map::SimpleMapBuilder;
use specs::prelude::*;
@ -29,4 +31,5 @@ pub fn random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
3 => Box::new(CellularAutomataBuilder::new(new_depth)),
_ => Box::new(SimpleMapBuilder::new(new_depth)),
}
// Box::new(DrunkardsWalkBuilder::new(new_depth))
}