refactors mapgen into chained builders
This commit is contained in:
parent
8a5600267c
commit
dd367dc39b
22 changed files with 1381 additions and 1480 deletions
|
|
@ -1,12 +1,8 @@
|
|||
use super::{
|
||||
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant, spawner, Map, MapBuilder,
|
||||
Position, TileType, SHOW_MAPGEN,
|
||||
};
|
||||
use super::{BuilderMap, InitialMapBuilder, TileType};
|
||||
use rltk::RandomNumberGenerator;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub enum DistanceAlgorithm {
|
||||
Pythagoras,
|
||||
Manhattan,
|
||||
|
|
@ -14,94 +10,42 @@ pub enum DistanceAlgorithm {
|
|||
}
|
||||
|
||||
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,
|
||||
spawn_list: Vec<(usize, String)>,
|
||||
}
|
||||
|
||||
impl MapBuilder for VoronoiBuilder {
|
||||
fn build_map(&mut self, rng: &mut RandomNumberGenerator) {
|
||||
return self.build(rng);
|
||||
}
|
||||
// Getters
|
||||
fn get_map(&mut self) -> Map {
|
||||
return self.map.clone();
|
||||
}
|
||||
fn get_starting_pos(&mut self) -> Position {
|
||||
return self.starting_position.clone();
|
||||
}
|
||||
fn get_spawn_list(&self) -> &Vec<(usize, String)> {
|
||||
return &self.spawn_list;
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
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,
|
||||
spawn_list: Vec::new(),
|
||||
}
|
||||
}
|
||||
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,
|
||||
spawn_list: Vec::new(),
|
||||
}
|
||||
}
|
||||
impl InitialMapBuilder for VoronoiBuilder {
|
||||
#[allow(dead_code)]
|
||||
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,
|
||||
spawn_list: Vec::new(),
|
||||
}
|
||||
fn build_map(&mut self, rng: &mut rltk::RandomNumberGenerator, build_data: &mut BuilderMap) {
|
||||
self.build(rng, build_data);
|
||||
}
|
||||
}
|
||||
|
||||
impl VoronoiBuilder {
|
||||
#[allow(dead_code)]
|
||||
pub fn new() -> Box<VoronoiBuilder> {
|
||||
Box::new(VoronoiBuilder { n_seeds: 64, distance_algorithm: DistanceAlgorithm::Pythagoras })
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn pythagoras() -> Box<VoronoiBuilder> {
|
||||
Box::new(VoronoiBuilder { n_seeds: 64, distance_algorithm: DistanceAlgorithm::Pythagoras })
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn manhattan() -> Box<VoronoiBuilder> {
|
||||
Box::new(VoronoiBuilder { n_seeds: 64, distance_algorithm: DistanceAlgorithm::Manhattan })
|
||||
}
|
||||
|
||||
#[allow(clippy::map_entry)]
|
||||
fn build(&mut self, rng: &mut RandomNumberGenerator) {
|
||||
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
||||
// 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 vx = rng.roll_dice(1, build_data.map.width - 1);
|
||||
let vy = rng.roll_dice(1, build_data.map.height - 1);
|
||||
let vidx = build_data.map.xy_idx(vx, vy);
|
||||
let candidate = (vidx, rltk::Point::new(vx, vy));
|
||||
if !voronoi_seeds.contains(&candidate) {
|
||||
voronoi_seeds.push(candidate);
|
||||
|
|
@ -109,10 +53,10 @@ impl VoronoiBuilder {
|
|||
}
|
||||
|
||||
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];
|
||||
let mut voronoi_membership: Vec<i32> = vec![0; build_data.map.width as usize * build_data.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;
|
||||
let x = i as i32 % build_data.map.width;
|
||||
let y = i as i32 / build_data.map.width;
|
||||
|
||||
for (seed, pos) in voronoi_seeds.iter().enumerate() {
|
||||
let distance;
|
||||
|
|
@ -135,52 +79,29 @@ impl VoronoiBuilder {
|
|||
*vid = voronoi_distance[0].0 as i32;
|
||||
}
|
||||
|
||||
for y in 1..self.map.height - 1 {
|
||||
for x in 1..self.map.width - 1 {
|
||||
for y in 1..build_data.map.height - 1 {
|
||||
for x in 1..build_data.map.width - 1 {
|
||||
let mut neighbors = 0;
|
||||
let my_idx = self.map.xy_idx(x, y);
|
||||
let my_idx = build_data.map.xy_idx(x, y);
|
||||
let my_seed = voronoi_membership[my_idx];
|
||||
if voronoi_membership[self.map.xy_idx(x - 1, y)] != my_seed {
|
||||
if voronoi_membership[build_data.map.xy_idx(x - 1, y)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
if voronoi_membership[self.map.xy_idx(x + 1, y)] != my_seed {
|
||||
if voronoi_membership[build_data.map.xy_idx(x + 1, y)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
if voronoi_membership[self.map.xy_idx(x, y - 1)] != my_seed {
|
||||
if voronoi_membership[build_data.map.xy_idx(x, y - 1)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
if voronoi_membership[self.map.xy_idx(x, y + 1)] != my_seed {
|
||||
if voronoi_membership[build_data.map.xy_idx(x, y + 1)] != my_seed {
|
||||
neighbors += 1;
|
||||
}
|
||||
|
||||
if neighbors < 2 {
|
||||
self.map.tiles[my_idx] = TileType::Floor;
|
||||
build_data.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);
|
||||
|
||||
// Spawn the entities
|
||||
for area in self.noise_areas.iter() {
|
||||
spawner::spawn_region(&self.map, rng, area.1, self.depth, &mut self.spawn_list);
|
||||
build_data.take_snapshot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue