Add drunkards walk
This commit is contained in:
parent
d4892e33bd
commit
34c4aa35fc
@ -13,15 +13,11 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="a9f5cad0-d253-42e8-a38a-89dfec417dbc" name="Changes" comment="">
|
||||
<change afterPath="$PROJECT_DIR$/src/map_builders/cellular_automata.rs" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/map_builders/drunkard.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/components.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/components.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/map_builders/bsp/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/map_builders/bsp/mod.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/map_builders/bsp_dungeon.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/map_builders/bsp_dungeon.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/map_builders/bsp_interior.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/map_builders/bsp_interior.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/savegame.json" beforeDir="false" afterPath="$PROJECT_DIR$/savegame.json" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/map_builders/common.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/map_builders/common.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/map_builders/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/map_builders/mod.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/map_builders/simple_map.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/map_builders/simple_map.rs" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/spawner.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/spawner.rs" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -82,7 +78,7 @@
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1643546643557</updated>
|
||||
<workItem from="1643546645546" duration="7299000" />
|
||||
<workItem from="1643546645546" duration="12996000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,4 +1,5 @@
|
||||
use std::cmp::{max, min};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::rect::Rect;
|
||||
use crate::{Map, TileType};
|
||||
@ -37,3 +38,53 @@ pub fn reveal_all(map: &Map) -> Map {
|
||||
}
|
||||
snapshot
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
pub fn generate_voronois_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
|
||||
}
|
180
src/map_builders/drunkard.rs
Normal file
180
src/map_builders/drunkard.rs
Normal file
@ -0,0 +1,180 @@
|
||||
use std::collections::HashMap;
|
||||
use rltk::RandomNumberGenerator;
|
||||
use crate::{Map, Position, SHOW_MAPGEN_VISUALIZER, TileType, World};
|
||||
use crate::map_builders::common::{generate_voronois_spawn_regions, remove_unreachable_areas_returning_most_distant, reveal_all};
|
||||
use crate::map_builders::MapBuilder;
|
||||
use crate::spawner::spawn_region;
|
||||
|
||||
pub struct DrunkardsWalkBuilder {
|
||||
map: Map,
|
||||
starting_position: Position,
|
||||
depth: i32,
|
||||
history: Vec<Map>,
|
||||
noise_areas: HashMap<i32, Vec<usize>>,
|
||||
settings: DrunkardSettings,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
pub enum DrunkSpawnMode { StartingPoint, Random }
|
||||
|
||||
pub struct DrunkardSettings {
|
||||
pub spawn_mode: DrunkSpawnMode,
|
||||
pub drunken_lifetime: i32,
|
||||
pub floor_percent: f32,
|
||||
}
|
||||
|
||||
impl DrunkardsWalkBuilder {
|
||||
pub fn new(new_depth: i32, settings: DrunkardSettings) -> Self {
|
||||
Self {
|
||||
depth: new_depth,
|
||||
map: Map::new(new_depth),
|
||||
history: Vec::new(),
|
||||
noise_areas: HashMap::new(),
|
||||
starting_position: Position::default(),
|
||||
settings,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_area(new_depth: i32) -> Self {
|
||||
DrunkardsWalkBuilder::new(
|
||||
new_depth,
|
||||
DrunkardSettings {
|
||||
spawn_mode: DrunkSpawnMode::StartingPoint,
|
||||
drunken_lifetime: 400,
|
||||
floor_percent: 0.5,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
pub fn open_halls(new_depth: i32) -> Self {
|
||||
DrunkardsWalkBuilder::new(
|
||||
new_depth,
|
||||
DrunkardSettings {
|
||||
spawn_mode: DrunkSpawnMode::Random,
|
||||
drunken_lifetime: 400,
|
||||
floor_percent: 0.5,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn winding_passages(new_depth: i32) -> Self {
|
||||
DrunkardsWalkBuilder::new(
|
||||
new_depth,
|
||||
DrunkardSettings {
|
||||
spawn_mode: DrunkSpawnMode::Random,
|
||||
drunken_lifetime: 100,
|
||||
floor_percent: 0.4,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fn build(&mut self) {
|
||||
let mut rng = RandomNumberGenerator::new();
|
||||
|
||||
self.starting_position = Position::new(self.map.width / 2, 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;
|
||||
|
||||
let total_tiles = self.map.width * self.map.height;
|
||||
let desired_floor_tiles = (self.settings.floor_percent * total_tiles as f32) as usize;
|
||||
let mut floor_tile_count = self.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
|
||||
let mut digger_count = 0;
|
||||
let mut active_digger_count = 0;
|
||||
|
||||
while floor_tile_count < desired_floor_tiles {
|
||||
let mut did_something = false;
|
||||
let mut drunk_x = self.starting_position.x;
|
||||
let mut drunk_y = self.starting_position.y;
|
||||
|
||||
match self.settings.spawn_mode {
|
||||
DrunkSpawnMode::StartingPoint => {
|
||||
drunk_x = self.starting_position.x;
|
||||
drunk_y = self.starting_position.y;
|
||||
}
|
||||
DrunkSpawnMode::Random => {
|
||||
if digger_count == 0 {
|
||||
drunk_x = self.starting_position.x;
|
||||
drunk_y = self.starting_position.y;
|
||||
} else {
|
||||
drunk_x = rng.roll_dice(1, self.map.width - 3) + 1;
|
||||
drunk_y = rng.roll_dice(1, self.map.height - 3) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut drunk_life = self.settings.drunken_lifetime;
|
||||
|
||||
while drunk_life > 0 {
|
||||
let drunk_idx = self.map.xy_idx(drunk_x, drunk_y);
|
||||
if self.map.tiles[drunk_idx] == TileType::Wall {
|
||||
did_something = true;
|
||||
}
|
||||
self.map.tiles[drunk_idx] = TileType::DownStairs;
|
||||
|
||||
let stagger_direction = rng.roll_dice(1, 4);
|
||||
match stagger_direction {
|
||||
1 if drunk_x > 2 => { drunk_x -= 1; }
|
||||
2 if drunk_x < self.map.width - 2 => { drunk_x += 1; }
|
||||
3 if drunk_y > 2 => { drunk_y -= 1; }
|
||||
_ if drunk_y < self.map.height - 2 => { drunk_y += 1; }
|
||||
_ => {}
|
||||
}
|
||||
|
||||
drunk_life -= 1;
|
||||
}
|
||||
if did_something {
|
||||
self.take_snapshot();
|
||||
active_digger_count += 1;
|
||||
}
|
||||
|
||||
digger_count += 1;
|
||||
for t in self.map.tiles.iter_mut() {
|
||||
if *t == TileType::DownStairs {
|
||||
*t = TileType::Floor;
|
||||
}
|
||||
}
|
||||
floor_tile_count = self.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
|
||||
}
|
||||
rltk::console::log(format!("{} dwarves gave up their sobriety, of whom {} actually found a wall.", digger_count, active_digger_count));
|
||||
|
||||
let exit_tile = remove_unreachable_areas_returning_most_distant(&mut self.map, start_idx);
|
||||
self.take_snapshot();
|
||||
|
||||
self.map.tiles[exit_tile] = TileType::DownStairs;
|
||||
self.take_snapshot();
|
||||
|
||||
self.noise_areas = generate_voronois_spawn_regions(&self.map, &mut rng);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
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 {
|
||||
self.history.push(reveal_all(&self.map))
|
||||
}
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ use crate::map_builders::bsp_interior::BspInteriorBuilder;
|
||||
use crate::map_builders::simple_map::SimpleMapBuilder;
|
||||
use crate::{Map, Position};
|
||||
use crate::map_builders::cellular_automata::CellularAutomataBuilder;
|
||||
use crate::map_builders::drunkard::{DrunkardSettings, DrunkardsWalkBuilder, DrunkSpawnMode};
|
||||
|
||||
mod bsp;
|
||||
mod bsp_dungeon;
|
||||
@ -12,6 +13,7 @@ mod bsp_interior;
|
||||
mod cellular_automata;
|
||||
mod common;
|
||||
mod simple_map;
|
||||
mod drunkard;
|
||||
|
||||
pub trait MapBuilder {
|
||||
fn build_map(&mut self);
|
||||
@ -24,9 +26,12 @@ pub trait MapBuilder {
|
||||
|
||||
pub fn new_random_builder(new_depth: i32) -> Box<dyn MapBuilder> {
|
||||
let mut rng = rltk::RandomNumberGenerator::new();
|
||||
//let builder_choice = rng.roll_dice(1, 4);
|
||||
let builder_choice = 3;
|
||||
//let builder_choice = rng.roll_dice(1, 7);
|
||||
let builder_choice = 4;
|
||||
match builder_choice {
|
||||
6 => Box::new(DrunkardsWalkBuilder::open_area(new_depth)),
|
||||
5 => Box::new(DrunkardsWalkBuilder::open_halls(new_depth)),
|
||||
4 => Box::new(DrunkardsWalkBuilder::winding_passages(new_depth)),
|
||||
3 => Box::new(CellularAutomataBuilder::new(new_depth)),
|
||||
2 => Box::new(BspInteriorBuilder::new(new_depth)),
|
||||
1 => Box::new(SimpleMapBuilder::new(new_depth)),
|
||||
|
Loading…
Reference in New Issue
Block a user