2021-12-10 20:16:48 -05:00
|
|
|
use rltk::RandomNumberGenerator;
|
|
|
|
|
2021-12-15 10:57:30 -05:00
|
|
|
use crate::map_builders::{BuilderMap, InitialMapBuilder};
|
2021-12-15 12:08:23 -05:00
|
|
|
use crate::TileType;
|
2021-12-07 14:55:39 -05:00
|
|
|
|
|
|
|
#[derive(PartialEq, Copy, Clone)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum DistanceAlgorithm {
|
|
|
|
Pythagoras,
|
|
|
|
Manhattan,
|
|
|
|
Chebyshev,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct VoronoiCellBuilder {
|
|
|
|
n_seeds: usize,
|
|
|
|
distance_algorithm: DistanceAlgorithm,
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:57:30 -05:00
|
|
|
impl InitialMapBuilder for VoronoiCellBuilder {
|
|
|
|
fn build_map(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
|
|
|
self.build(rng, build_data);
|
2021-12-10 16:34:11 -05:00
|
|
|
}
|
2021-12-07 14:55:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VoronoiCellBuilder {
|
2021-12-15 10:57:30 -05:00
|
|
|
pub fn new() -> VoronoiCellBuilder {
|
2021-12-07 14:55:39 -05:00
|
|
|
VoronoiCellBuilder {
|
|
|
|
n_seeds: 64,
|
|
|
|
distance_algorithm: DistanceAlgorithm::Pythagoras,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:57:30 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn pythagoras() -> Box<VoronoiCellBuilder> {
|
|
|
|
Box::new(VoronoiCellBuilder::new())
|
2021-12-07 14:55:39 -05:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:57:30 -05:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn manhattan() -> Box<VoronoiCellBuilder> {
|
|
|
|
Box::new(VoronoiCellBuilder {
|
2021-12-07 14:55:39 -05:00
|
|
|
distance_algorithm: DistanceAlgorithm::Manhattan,
|
2021-12-15 10:57:30 -05:00
|
|
|
..VoronoiCellBuilder::new()
|
|
|
|
})
|
2021-12-07 14:55:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::map_entry)]
|
2021-12-15 10:57:30 -05:00
|
|
|
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) {
|
2021-12-07 14:55:39 -05:00
|
|
|
// 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 {
|
2021-12-15 10:57:30 -05:00
|
|
|
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);
|
2021-12-07 14:55:39 -05:00
|
|
|
let candidate = (vidx, rltk::Point::new(vx, vy));
|
|
|
|
if !voronoi_seeds.contains(&candidate) {
|
|
|
|
voronoi_seeds.push(candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut voronoi_distance = vec![(0, 0.0f32); self.n_seeds];
|
|
|
|
let mut voronoi_membership: Vec<i32> =
|
2021-12-15 10:57:30 -05:00
|
|
|
vec![0; build_data.map.width as usize * build_data.map.height as usize];
|
|
|
|
|
2021-12-07 14:55:39 -05:00
|
|
|
for (i, vid) in voronoi_membership.iter_mut().enumerate() {
|
2021-12-15 10:57:30 -05:00
|
|
|
let x = i as i32 % build_data.map.width;
|
|
|
|
let y = i as i32 / build_data.map.width;
|
2021-12-07 14:55:39 -05:00
|
|
|
|
|
|
|
for (seed, pos) in voronoi_seeds.iter().enumerate() {
|
2021-12-15 10:57:30 -05:00
|
|
|
let distance = match self.distance_algorithm {
|
|
|
|
DistanceAlgorithm::Pythagoras => rltk::DistanceAlg::PythagorasSquared
|
|
|
|
.distance2d(rltk::Point::new(x, y), pos.1),
|
|
|
|
|
2021-12-07 14:55:39 -05:00
|
|
|
DistanceAlgorithm::Manhattan => {
|
2021-12-15 10:57:30 -05:00
|
|
|
rltk::DistanceAlg::Manhattan.distance2d(rltk::Point::new(x, y), pos.1)
|
2021-12-07 14:55:39 -05:00
|
|
|
}
|
2021-12-15 10:57:30 -05:00
|
|
|
|
2021-12-07 14:55:39 -05:00
|
|
|
DistanceAlgorithm::Chebyshev => {
|
2021-12-15 10:57:30 -05:00
|
|
|
rltk::DistanceAlg::Chebyshev.distance2d(rltk::Point::new(x, y), pos.1)
|
2021-12-07 14:55:39 -05:00
|
|
|
}
|
2021-12-15 10:57:30 -05:00
|
|
|
};
|
2021-12-07 14:55:39 -05:00
|
|
|
voronoi_distance[seed] = (seed, distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
voronoi_distance.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
|
|
|
|
|
|
|
|
*vid = voronoi_distance[0].0 as i32;
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:57:30 -05:00
|
|
|
for y in 1..build_data.map.height - 1 {
|
|
|
|
for x in 1..build_data.map.width - 1 {
|
2021-12-07 14:55:39 -05:00
|
|
|
let mut neighbors = 0;
|
2021-12-15 10:57:30 -05:00
|
|
|
let my_idx = build_data.map.xy_idx(x, y);
|
2021-12-07 14:55:39 -05:00
|
|
|
let my_seed = voronoi_membership[my_idx];
|
|
|
|
|
2021-12-15 10:57:30 -05:00
|
|
|
if voronoi_membership[build_data.map.xy_idx(x - 1, y)] != my_seed {
|
2021-12-07 14:55:39 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:57:30 -05:00
|
|
|
if voronoi_membership[build_data.map.xy_idx(x + 1, y)] != my_seed {
|
2021-12-07 14:55:39 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:57:30 -05:00
|
|
|
if voronoi_membership[build_data.map.xy_idx(x, y - 1)] != my_seed {
|
2021-12-07 14:55:39 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
2021-12-15 10:57:30 -05:00
|
|
|
if voronoi_membership[build_data.map.xy_idx(x, y + 1)] != my_seed {
|
2021-12-07 14:55:39 -05:00
|
|
|
neighbors += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if neighbors < 2 {
|
2021-12-15 10:57:30 -05:00
|
|
|
build_data.map.tiles[my_idx] = TileType::Floor;
|
2021-12-07 14:55:39 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 10:57:30 -05:00
|
|
|
build_data.take_snapshot();
|
2021-12-10 16:34:11 -05:00
|
|
|
}
|
2021-12-07 14:55:39 -05:00
|
|
|
}
|
|
|
|
}
|