Refactor some voronoi-based builders to use Rust builder pattern

This commit is contained in:
Timothy Warren 2021-12-15 10:45:58 -05:00
parent a6d37bf0dc
commit db1c4c2d44
3 changed files with 145 additions and 360 deletions

View File

@ -8,6 +8,7 @@ use super::common::{
}; };
use super::MapBuilder; use super::MapBuilder;
use crate::components::Position; use crate::components::Position;
use crate::map_builders::{BuilderMap, InitialMapBuilder};
use crate::{spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER}; use crate::{spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER};
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
@ -18,121 +19,81 @@ pub enum DLAAlgorithm {
} }
pub struct DLABuilder { pub struct DLABuilder {
map: Map,
starting_position: Position,
depth: i32,
history: Vec<Map>,
noise_areas: HashMap<i32, Vec<usize>>,
algorithm: DLAAlgorithm, algorithm: DLAAlgorithm,
brush_size: i32, brush_size: i32,
symmetry: Symmetry, symmetry: Symmetry,
floor_percent: f32, floor_percent: f32,
spawn_list: Vec<(usize, String)>,
} }
impl MapBuilder for DLABuilder { impl InitialMapBuilder for DLABuilder {
fn get_map(&self) -> Map { fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.map.clone() self.build(rng, build_data);
}
fn get_starting_position(&self) -> Position {
self.starting_position
}
fn get_snapshot_history(&self) -> Vec<Map> {
self.history.clone()
}
fn build_map(&mut self) {
self.build();
}
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);
}
}
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
&self.spawn_list
} }
} }
impl DLABuilder { impl DLABuilder {
pub fn new(new_depth: i32) -> DLABuilder { pub fn new() -> DLABuilder {
DLABuilder { DLABuilder {
map: Map::new(new_depth),
starting_position: Position::default(),
depth: new_depth,
history: Vec::new(),
noise_areas: HashMap::new(),
algorithm: DLAAlgorithm::WalkOutwards, algorithm: DLAAlgorithm::WalkOutwards,
brush_size: 1, brush_size: 1,
symmetry: Symmetry::None, symmetry: Symmetry::None,
floor_percent: 0.25, floor_percent: 0.25,
spawn_list: Vec::new(),
} }
} }
pub fn walk_inwards(new_depth: i32) -> DLABuilder { pub fn walk_inwards() -> Box<DLABuilder> {
DLABuilder { Box::new(DLABuilder {
algorithm: DLAAlgorithm::WalkInwards, algorithm: DLAAlgorithm::WalkInwards,
..DLABuilder::new(new_depth) ..DLABuilder::new()
} })
} }
pub fn walk_outwards(new_depth: i32) -> DLABuilder { pub fn walk_outwards() -> Box<DLABuilder> {
DLABuilder { Box::new(DLABuilder {
brush_size: 2, brush_size: 2,
..DLABuilder::new(new_depth) ..DLABuilder::new()
} })
} }
pub fn central_attractor(new_depth: i32) -> DLABuilder { pub fn central_attractor() -> Box<DLABuilder> {
DLABuilder { Box::new(DLABuilder {
algorithm: DLAAlgorithm::CentralAttractor, algorithm: DLAAlgorithm::CentralAttractor,
brush_size: 2, brush_size: 2,
..DLABuilder::new(new_depth) ..DLABuilder::new()
} })
} }
pub fn insectoid(new_depth: i32) -> DLABuilder { pub fn insectoid() -> Box<DLABuilder> {
DLABuilder { Box::new(DLABuilder {
algorithm: DLAAlgorithm::CentralAttractor, algorithm: DLAAlgorithm::CentralAttractor,
brush_size: 2, brush_size: 2,
symmetry: Symmetry::Horizontal, symmetry: Symmetry::Horizontal,
..DLABuilder::new(new_depth) ..DLABuilder::new()
} })
} }
#[allow(clippy::map_entry)] #[allow(clippy::map_entry)]
fn build(&mut self) { fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
let mut rng = RandomNumberGenerator::new(); let starting_position = Position {
x: build_data.map.width / 2,
self.starting_position = Position { y: build_data.map.height / 2,
x: self.map.width / 2,
y: self.map.height / 2,
}; };
let start_idx = self let start_idx = build_data
.map .map
.xy_idx(self.starting_position.x, self.starting_position.y); .xy_idx(starting_position.x, starting_position.y);
self.take_snapshot(); build_data.take_snapshot();
// Carve a starting seed // Carve a starting seed
self.map.tiles[start_idx] = TileType::Floor; build_data.map.tiles[start_idx] = TileType::Floor;
self.map.tiles[start_idx - 1] = TileType::Floor; build_data.map.tiles[start_idx - 1] = TileType::Floor;
self.map.tiles[start_idx + 1] = TileType::Floor; build_data.map.tiles[start_idx + 1] = TileType::Floor;
self.map.tiles[start_idx - self.map.width as usize] = TileType::Floor; build_data.map.tiles[start_idx - build_data.map.width as usize] = TileType::Floor;
self.map.tiles[start_idx + self.map.width as usize] = TileType::Floor; build_data.map.tiles[start_idx + build_data.map.width as usize] = TileType::Floor;
// Random walker // Random walker
let total_tiles = self.map.width * self.map.height; let total_tiles = build_data.map.width * build_data.map.height;
let desired_floor_tiles = (self.floor_percent * total_tiles as f32) as usize; let desired_floor_tiles = (self.floor_percent * total_tiles as f32) as usize;
let mut floor_tile_count = self let mut floor_tile_count = build_data
.map .map
.tiles .tiles
.iter() .iter()
@ -142,13 +103,13 @@ impl DLABuilder {
while floor_tile_count < desired_floor_tiles { while floor_tile_count < desired_floor_tiles {
match self.algorithm { match self.algorithm {
DLAAlgorithm::WalkInwards => { DLAAlgorithm::WalkInwards => {
let mut digger_x = rng.roll_dice(1, self.map.width - 3) + 1; let mut digger_x = rng.roll_dice(1, build_data.map.width - 3) + 1;
let mut digger_y = rng.roll_dice(1, self.map.height - 3) + 1; let mut digger_y = rng.roll_dice(1, build_data.map.height - 3) + 1;
let mut prev_x = digger_x; let mut prev_x = digger_x;
let mut prev_y = digger_y; let mut prev_y = digger_y;
let mut digger_idx = self.map.xy_idx(digger_x, digger_y); let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y);
while self.map.tiles[digger_idx] == TileType::Wall { while build_data.map.tiles[digger_idx] == TileType::Wall {
prev_x = digger_x; prev_x = digger_x;
prev_y = digger_y; prev_y = digger_y;
@ -160,7 +121,7 @@ impl DLABuilder {
} }
} }
2 => { 2 => {
if digger_x < self.map.width - 2 { if digger_x < build_data.map.width - 2 {
digger_x += 1 digger_x += 1
} }
} }
@ -170,17 +131,17 @@ impl DLABuilder {
} }
} }
_ => { _ => {
if digger_y < self.map.height - 2 { if digger_y < build_data.map.height - 2 {
digger_y += 1 digger_y += 1
} }
} }
} }
digger_idx = self.map.xy_idx(digger_x, digger_y); digger_idx = build_data.map.xy_idx(digger_x, digger_y);
} }
paint( paint(
&mut self.map, &mut build_data.map,
self.symmetry, self.symmetry,
self.brush_size, self.brush_size,
prev_x, prev_x,
@ -189,11 +150,11 @@ impl DLABuilder {
} }
DLAAlgorithm::WalkOutwards => { DLAAlgorithm::WalkOutwards => {
let mut digger_x = self.starting_position.x; let mut digger_x = starting_position.x;
let mut digger_y = self.starting_position.y; let mut digger_y = starting_position.y;
let mut digger_idx = self.map.xy_idx(digger_x, digger_y); let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y);
while self.map.tiles[digger_idx] == TileType::Floor { while build_data.map.tiles[digger_idx] == TileType::Floor {
let stagger_direction = rng.roll_dice(1, 4); let stagger_direction = rng.roll_dice(1, 4);
match stagger_direction { match stagger_direction {
1 => { 1 => {
@ -202,7 +163,7 @@ impl DLABuilder {
} }
} }
2 => { 2 => {
if digger_x < self.map.width - 2 { if digger_x < build_data.map.width - 2 {
digger_x += 1 digger_x += 1
} }
} }
@ -212,17 +173,17 @@ impl DLABuilder {
} }
} }
_ => { _ => {
if digger_y < self.map.height - 2 { if digger_y < build_data.map.height - 2 {
digger_y += 1 digger_y += 1
} }
} }
} }
digger_idx = self.map.xy_idx(digger_x, digger_y); digger_idx = build_data.map.xy_idx(digger_x, digger_y);
} }
paint( paint(
&mut self.map, &mut build_data.map,
self.symmetry, self.symmetry,
self.brush_size, self.brush_size,
digger_x, digger_x,
@ -231,29 +192,29 @@ impl DLABuilder {
} }
DLAAlgorithm::CentralAttractor => { DLAAlgorithm::CentralAttractor => {
let mut digger_x = rng.roll_dice(1, self.map.width - 3) + 1; let mut digger_x = rng.roll_dice(1, build_data.map.width - 3) + 1;
let mut digger_y = rng.roll_dice(1, self.map.height - 3) + 1; let mut digger_y = rng.roll_dice(1, build_data.map.height - 3) + 1;
let mut prev_x = digger_x; let mut prev_x = digger_x;
let mut prev_y = digger_y; let mut prev_y = digger_y;
let mut digger_idx = self.map.xy_idx(digger_x, digger_y); let mut digger_idx = build_data.map.xy_idx(digger_x, digger_y);
let mut path = rltk::line2d( let mut path = rltk::line2d(
rltk::LineAlg::Bresenham, rltk::LineAlg::Bresenham,
rltk::Point::new(digger_x, digger_y), rltk::Point::new(digger_x, digger_y),
rltk::Point::new(self.starting_position.x, self.starting_position.y), rltk::Point::new(starting_position.x, starting_position.y),
); );
while self.map.tiles[digger_idx] == TileType::Wall && !path.is_empty() { while build_data.map.tiles[digger_idx] == TileType::Wall && !path.is_empty() {
prev_x = digger_x; prev_x = digger_x;
prev_y = digger_y; prev_y = digger_y;
digger_x = path[0].x; digger_x = path[0].x;
digger_y = path[0].y; digger_y = path[0].y;
path.remove(0); path.remove(0);
digger_idx = self.map.xy_idx(digger_x, digger_y); digger_idx = build_data.map.xy_idx(digger_x, digger_y);
} }
paint( paint(
&mut self.map, &mut build_data.map,
self.symmetry, self.symmetry,
self.brush_size, self.brush_size,
prev_x, prev_x,
@ -262,36 +223,14 @@ impl DLABuilder {
} }
} }
self.take_snapshot(); build_data.take_snapshot();
floor_tile_count = self floor_tile_count = build_data
.map .map
.tiles .tiles
.iter() .iter()
.filter(|a| **a == TileType::Floor) .filter(|a| **a == TileType::Floor)
.count(); .count();
} }
// 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::DownStairs;
self.take_snapshot();
// Now we build a noise map for use in spawning entities later
self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
// Spawn the entities
for area in self.noise_areas.iter() {
spawner::spawn_region(
&self.map,
&mut rng,
area.1,
self.depth,
&mut self.spawn_list,
);
}
} }
} }

View File

@ -8,6 +8,7 @@ use super::common::{
}; };
use super::MapBuilder; use super::MapBuilder;
use crate::components::Position; use crate::components::Position;
use crate::map_builders::{BuilderMap, InitialMapBuilder};
use crate::{spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER}; use crate::{spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER};
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
@ -25,149 +26,91 @@ pub struct DrunkardSettings {
} }
pub struct DrunkardsWalkBuilder { pub struct DrunkardsWalkBuilder {
map: Map,
starting_position: Position,
depth: i32,
history: Vec<Map>,
noise_areas: HashMap<i32, Vec<usize>>,
settings: DrunkardSettings, settings: DrunkardSettings,
spawn_list: Vec<(usize, String)>,
} }
impl MapBuilder for DrunkardsWalkBuilder { impl InitialMapBuilder for DrunkardsWalkBuilder {
fn get_map(&self) -> Map { fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.map.clone() self.build(rng, build_data);
}
fn get_starting_position(&self) -> Position {
self.starting_position
}
fn get_snapshot_history(&self) -> Vec<Map> {
self.history.clone()
}
fn build_map(&mut self) {
self.build();
}
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);
}
}
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
&self.spawn_list
} }
} }
impl DrunkardsWalkBuilder { impl DrunkardsWalkBuilder {
pub fn new(new_depth: i32, settings: DrunkardSettings) -> DrunkardsWalkBuilder { pub fn new(settings: DrunkardSettings) -> DrunkardsWalkBuilder {
DrunkardsWalkBuilder { DrunkardsWalkBuilder { settings }
map: Map::new(new_depth),
starting_position: Position::default(),
depth: new_depth,
history: Vec::new(),
noise_areas: HashMap::new(),
settings,
spawn_list: Vec::new(),
}
} }
pub fn open_area(new_depth: i32) -> DrunkardsWalkBuilder { pub fn open_area() -> Box<DrunkardsWalkBuilder> {
DrunkardsWalkBuilder::new( Box::new(DrunkardsWalkBuilder::new(DrunkardSettings {
new_depth,
DrunkardSettings {
spawn_mode: DrunkSpawnMode::StaringPoint, spawn_mode: DrunkSpawnMode::StaringPoint,
drunken_lifetime: 400, drunken_lifetime: 400,
floor_percent: 0.5, floor_percent: 0.5,
brush_size: 1, brush_size: 1,
symmetry: Symmetry::None, symmetry: Symmetry::None,
}, }))
)
} }
pub fn open_halls(new_depth: i32) -> DrunkardsWalkBuilder { pub fn open_halls() -> Box<DrunkardsWalkBuilder> {
DrunkardsWalkBuilder::new( Box::new(DrunkardsWalkBuilder::new(DrunkardSettings {
new_depth,
DrunkardSettings {
spawn_mode: DrunkSpawnMode::Random, spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 400, drunken_lifetime: 400,
floor_percent: 0.5, floor_percent: 0.5,
brush_size: 1, brush_size: 1,
symmetry: Symmetry::None, symmetry: Symmetry::None,
}, }))
)
} }
pub fn winding_passages(new_depth: i32) -> DrunkardsWalkBuilder { pub fn winding_passages() -> Box<DrunkardsWalkBuilder> {
DrunkardsWalkBuilder::new( Box::new(DrunkardsWalkBuilder::new(DrunkardSettings {
new_depth,
DrunkardSettings {
spawn_mode: DrunkSpawnMode::Random, spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 100, drunken_lifetime: 100,
floor_percent: 0.4, floor_percent: 0.4,
brush_size: 1, brush_size: 1,
symmetry: Symmetry::None, symmetry: Symmetry::None,
}, }))
)
} }
pub fn fat_passages(new_depth: i32) -> DrunkardsWalkBuilder { pub fn fat_passages() -> Box<DrunkardsWalkBuilder> {
DrunkardsWalkBuilder::new( Box::new(DrunkardsWalkBuilder::new(DrunkardSettings {
new_depth,
DrunkardSettings {
spawn_mode: DrunkSpawnMode::Random, spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 100, drunken_lifetime: 100,
floor_percent: 0.4, floor_percent: 0.4,
brush_size: 2, brush_size: 2,
symmetry: Symmetry::None, symmetry: Symmetry::None,
}, }))
)
} }
pub fn fearful_symmetry(new_depth: i32) -> DrunkardsWalkBuilder { pub fn fearful_symmetry() -> Box<DrunkardsWalkBuilder> {
DrunkardsWalkBuilder::new( Box::new(DrunkardsWalkBuilder::new(DrunkardSettings {
new_depth,
DrunkardSettings {
spawn_mode: DrunkSpawnMode::Random, spawn_mode: DrunkSpawnMode::Random,
drunken_lifetime: 100, drunken_lifetime: 100,
floor_percent: 0.4, floor_percent: 0.4,
brush_size: 1, brush_size: 1,
symmetry: Symmetry::Both, symmetry: Symmetry::Both,
}, }))
)
} }
#[allow(clippy::map_entry)] #[allow(clippy::map_entry)]
fn build(&mut self) { fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
let mut rng = RandomNumberGenerator::new();
// Set a central starting point // Set a central starting point
self.starting_position = Position { let starting_position = Position {
x: self.map.width / 2, x: build_data.map.width / 2,
y: self.map.height / 2, y: build_data.map.height / 2,
}; };
let start_idx = self let start_idx = build_data
.map .map
.xy_idx(self.starting_position.x, self.starting_position.y); .xy_idx(starting_position.x, starting_position.y);
self.map.tiles[start_idx] = TileType::Floor; build_data.map.tiles[start_idx] = TileType::Floor;
let total_tiles = self.map.width * self.map.height; let total_tiles = build_data.map.width * build_data.map.height;
let desired_floor_tiles = (self.settings.floor_percent * total_tiles as f32) as usize; let desired_floor_tiles = (self.settings.floor_percent * total_tiles as f32) as usize;
let mut floor_tile_count = self let mut floor_tile_count = build_data
.map .map
.tiles .tiles
.iter() .iter()
.filter(|a| **a == TileType::Floor) .filter(|a| **a == TileType::Floor)
.count(); .count();
let mut digger_count = 0; let mut digger_count = 0;
let mut active_digger_count = 0;
while floor_tile_count < desired_floor_tiles { while floor_tile_count < desired_floor_tiles {
let mut did_something = false; let mut did_something = false;
@ -175,16 +118,16 @@ impl DrunkardsWalkBuilder {
let mut drunk_y; let mut drunk_y;
match self.settings.spawn_mode { match self.settings.spawn_mode {
DrunkSpawnMode::StaringPoint => { DrunkSpawnMode::StaringPoint => {
drunk_x = self.starting_position.x; drunk_x = starting_position.x;
drunk_y = self.starting_position.y; drunk_y = starting_position.y;
} }
DrunkSpawnMode::Random => { DrunkSpawnMode::Random => {
if digger_count == 0 { if digger_count == 0 {
drunk_x = self.starting_position.x; drunk_x = starting_position.x;
drunk_y = self.starting_position.y; drunk_y = starting_position.y;
} else { } else {
drunk_x = rng.roll_dice(1, self.map.width - 3) + 1; drunk_x = rng.roll_dice(1, build_data.map.width - 3) + 1;
drunk_y = rng.roll_dice(1, self.map.height - 3) + 1; drunk_y = rng.roll_dice(1, build_data.map.height - 3) + 1;
} }
} }
} }
@ -192,19 +135,19 @@ impl DrunkardsWalkBuilder {
let mut drunk_life = self.settings.drunken_lifetime; let mut drunk_life = self.settings.drunken_lifetime;
while drunk_life > 0 { while drunk_life > 0 {
let drunk_idx = self.map.xy_idx(drunk_x, drunk_y); let drunk_idx = build_data.map.xy_idx(drunk_x, drunk_y);
if self.map.tiles[drunk_idx] == TileType::Wall { if build_data.map.tiles[drunk_idx] == TileType::Wall {
did_something = true; did_something = true;
} }
paint( paint(
&mut self.map, &mut build_data.map,
self.settings.symmetry, self.settings.symmetry,
self.settings.brush_size, self.settings.brush_size,
drunk_x, drunk_x,
drunk_y, drunk_y,
); );
self.map.tiles[drunk_idx] = TileType::DownStairs; build_data.map.tiles[drunk_idx] = TileType::DownStairs;
let stagger_direction = rng.roll_dice(1, 4); let stagger_direction = rng.roll_dice(1, 4);
match stagger_direction { match stagger_direction {
@ -214,7 +157,7 @@ impl DrunkardsWalkBuilder {
} }
} }
2 => { 2 => {
if drunk_x < self.map.width - 2 { if drunk_x < build_data.map.width - 2 {
drunk_x += 1 drunk_x += 1
} }
} }
@ -224,7 +167,7 @@ impl DrunkardsWalkBuilder {
} }
} }
_ => { _ => {
if drunk_y < self.map.height - 2 { if drunk_y < build_data.map.height - 2 {
drunk_y += 1 drunk_y += 1
} }
} }
@ -234,49 +177,21 @@ impl DrunkardsWalkBuilder {
} }
if did_something { if did_something {
self.take_snapshot(); build_data.take_snapshot();
active_digger_count += 1;
} }
digger_count += 1; digger_count += 1;
for t in self.map.tiles.iter_mut() { for t in build_data.map.tiles.iter_mut() {
if *t == TileType::DownStairs { if *t == TileType::DownStairs {
*t = TileType::Floor; *t = TileType::Floor;
} }
} }
floor_tile_count = self floor_tile_count = build_data
.map .map
.tiles .tiles
.iter() .iter()
.filter(|a| **a == TileType::Floor) .filter(|a| **a == TileType::Floor)
.count(); .count();
} }
rltk::console::log(format!(
"{} dwarves gave up their sobriety, of whom {} actually found a wall.",
digger_count, active_digger_count
));
// 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::DownStairs;
self.take_snapshot();
// Now we build a noise map for use in spawning entities later
self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
// Spawn the entities
for area in self.noise_areas.iter() {
spawner::spawn_region(
&self.map,
&mut rng,
area.1,
self.depth,
&mut self.spawn_list,
);
}
} }
} }

View File

@ -6,100 +6,31 @@ use rltk::RandomNumberGenerator;
use super::{ use super::{
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant, MapBuilder, generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant, MapBuilder,
}; };
use crate::map_builders::{BuilderMap, InitialMapBuilder};
use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER}; use crate::{spawner, Map, Position, TileType, SHOW_MAPGEN_VISUALIZER};
pub struct MazeBuilder { pub struct MazeBuilder {}
map: Map,
starting_position: Position,
depth: i32,
history: Vec<Map>,
noise_areas: HashMap<i32, Vec<usize>>,
spawn_list: Vec<(usize, String)>,
}
impl MapBuilder for MazeBuilder { impl InitialMapBuilder for MazeBuilder {
fn get_map(&self) -> Map { fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
self.map.clone() self.build(rng, build_data);
}
fn get_starting_position(&self) -> Position {
self.starting_position
}
fn get_snapshot_history(&self) -> Vec<Map> {
self.history.clone()
}
fn build_map(&mut self) {
self.build();
}
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);
}
}
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
&self.spawn_list
} }
} }
impl MazeBuilder { impl MazeBuilder {
pub fn new(new_depth: i32) -> MazeBuilder { pub fn new() -> Box<MazeBuilder> {
MazeBuilder { Box::new(MazeBuilder {})
map: Map::new(new_depth),
starting_position: Position::default(),
depth: new_depth,
history: Vec::new(),
noise_areas: HashMap::new(),
spawn_list: Vec::new(),
}
} }
#[allow(clippy::map_entry)] #[allow(clippy::map_entry)]
fn build(&mut self) { fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
let mut rng = RandomNumberGenerator::new();
// Maze gen // Maze gen
let mut maze = Grid::new( let mut maze = Grid::new(
(self.map.width / 2) - 2, (build_data.map.width / 2) - 2,
(self.map.height / 2) - 2, (build_data.map.height / 2) - 2,
&mut rng, rng,
); );
maze.generate_maze(self); maze.generate_maze(build_data);
self.starting_position = Position { x: 2, y: 2 };
let 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::DownStairs;
self.take_snapshot();
// Now we build a noise map for use in spawning entities later
self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
// Spawn the entities
for area in self.noise_areas.iter() {
spawner::spawn_region(
&self.map,
&mut rng,
area.1,
self.depth,
&mut self.spawn_list,
);
}
} }
} }
@ -218,7 +149,7 @@ impl<'a> Grid<'a> {
None None
} }
fn generate_maze(&mut self, generator: &mut MazeBuilder) { fn generate_maze(&mut self, build_data: &mut BuilderMap) {
let mut i = 0; let mut i = 0;
loop { loop {
self.cells[self.current].visited = true; self.cells[self.current].visited = true;
@ -250,8 +181,8 @@ impl<'a> Grid<'a> {
} }
if i % 50 == 0 { if i % 50 == 0 {
self.copy_to_map(&mut generator.map); self.copy_to_map(&mut build_data.map);
generator.take_snapshot(); build_data.take_snapshot();
} }
i += 1; i += 1;
} }