Extract symmetry and brush size functions from DLA map builder
This commit is contained in:
parent
9b4953be7d
commit
d0c4f9b8c2
@ -2,6 +2,15 @@ use crate::{Map, Rect, TileType};
|
||||
use std::cmp::{max, min};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub enum Symmetry {
|
||||
None,
|
||||
Horizontal,
|
||||
Vertical,
|
||||
Both,
|
||||
}
|
||||
|
||||
pub fn apply_room_to_map(map: &mut Map, room: &Rect) {
|
||||
for y in room.y1 + 1..=room.y2 {
|
||||
for x in room.x1 + 1..=room.x2 {
|
||||
@ -92,3 +101,68 @@ pub fn generate_voronoi_spawn_regions(
|
||||
|
||||
noise_areas
|
||||
}
|
||||
|
||||
pub fn paint(map: &mut Map, mode: Symmetry, brush_size: i32, x: i32, y: i32) {
|
||||
match mode {
|
||||
Symmetry::None => apply_paint(map, brush_size, x, y),
|
||||
Symmetry::Horizontal => {
|
||||
let center_x = map.width / 2;
|
||||
if x == center_x {
|
||||
apply_paint(map, brush_size, x, y);
|
||||
} else {
|
||||
let dist_x = i32::abs(center_x - x);
|
||||
apply_paint(map, brush_size, center_x + dist_x, y);
|
||||
apply_paint(map, brush_size, center_x - dist_x, y);
|
||||
}
|
||||
}
|
||||
Symmetry::Vertical => {
|
||||
let center_y = map.height / 2;
|
||||
if y == center_y {
|
||||
apply_paint(map, brush_size, x, y);
|
||||
} else {
|
||||
let dist_y = i32::abs(center_y - y);
|
||||
apply_paint(map, brush_size, x, center_y + dist_y);
|
||||
apply_paint(map, brush_size, x, center_y - dist_y);
|
||||
}
|
||||
}
|
||||
Symmetry::Both => {
|
||||
let center_x = map.width / 2;
|
||||
let center_y = map.height / 2;
|
||||
if x == center_x && y == center_y {
|
||||
apply_paint(map, brush_size, x, y);
|
||||
} else {
|
||||
let dist_x = i32::abs(center_x - x);
|
||||
apply_paint(map, brush_size, center_x + dist_x, y);
|
||||
apply_paint(map, brush_size, center_x - dist_x, y);
|
||||
let dist_y = i32::abs(center_y - y);
|
||||
apply_paint(map, brush_size, x, center_y + dist_y);
|
||||
apply_paint(map, brush_size, x, center_y - dist_y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_paint(map: &mut Map, brush_size: i32, x: i32, y: i32) {
|
||||
match brush_size {
|
||||
1 => {
|
||||
let digger_idx = map.xy_idx(x, y);
|
||||
map.tiles[digger_idx] = TileType::Floor;
|
||||
}
|
||||
|
||||
_ => {
|
||||
let half_brush_size = brush_size / 2;
|
||||
for brush_y in y - half_brush_size..y + half_brush_size {
|
||||
for brush_x in x - half_brush_size..x + half_brush_size {
|
||||
if brush_x > 1
|
||||
&& brush_x < map.width - 1
|
||||
&& brush_y > 1
|
||||
&& brush_y < map.height - 1
|
||||
{
|
||||
let idx = map.xy_idx(brush_x, brush_y);
|
||||
map.tiles[idx] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
use super::common::{
|
||||
generate_voronoi_spawn_regions, remove_unreachable_areas_returning_most_distant,
|
||||
generate_voronoi_spawn_regions, paint, remove_unreachable_areas_returning_most_distant,
|
||||
Symmetry,
|
||||
};
|
||||
use super::MapBuilder;
|
||||
use crate::{components::Position, spawner, Map, TileType, SHOW_MAPGEN_VISUALIZER};
|
||||
@ -14,15 +15,6 @@ pub enum DLAAlgorithm {
|
||||
CentralAttractor,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub enum DLASymmetry {
|
||||
None,
|
||||
Horizontal,
|
||||
Vertical,
|
||||
Both,
|
||||
}
|
||||
|
||||
pub struct DLABuilder {
|
||||
map: Map,
|
||||
starting_position: Position,
|
||||
@ -31,7 +23,7 @@ pub struct DLABuilder {
|
||||
noise_areas: HashMap<i32, Vec<usize>>,
|
||||
algorithm: DLAAlgorithm,
|
||||
brush_size: i32,
|
||||
symmetry: DLASymmetry,
|
||||
symmetry: Symmetry,
|
||||
floor_percent: f32,
|
||||
}
|
||||
|
||||
@ -79,7 +71,7 @@ impl DLABuilder {
|
||||
noise_areas: HashMap::new(),
|
||||
algorithm: DLAAlgorithm::WalkOutwards,
|
||||
brush_size: 1,
|
||||
symmetry: DLASymmetry::None,
|
||||
symmetry: Symmetry::None,
|
||||
floor_percent: 0.25,
|
||||
}
|
||||
}
|
||||
@ -110,7 +102,7 @@ impl DLABuilder {
|
||||
DLABuilder {
|
||||
algorithm: DLAAlgorithm::CentralAttractor,
|
||||
brush_size: 2,
|
||||
symmetry: DLASymmetry::Horizontal,
|
||||
symmetry: Symmetry::Horizontal,
|
||||
..DLABuilder::new(new_depth)
|
||||
}
|
||||
}
|
||||
@ -185,7 +177,13 @@ impl DLABuilder {
|
||||
digger_idx = self.map.xy_idx(digger_x, digger_y);
|
||||
}
|
||||
|
||||
self.paint(prev_x, prev_y)
|
||||
paint(
|
||||
&mut self.map,
|
||||
self.symmetry,
|
||||
self.brush_size,
|
||||
prev_x,
|
||||
prev_y,
|
||||
);
|
||||
}
|
||||
|
||||
DLAAlgorithm::WalkOutwards => {
|
||||
@ -221,7 +219,13 @@ impl DLABuilder {
|
||||
digger_idx = self.map.xy_idx(digger_x, digger_y);
|
||||
}
|
||||
|
||||
self.paint(digger_x, digger_y);
|
||||
paint(
|
||||
&mut self.map,
|
||||
self.symmetry,
|
||||
self.brush_size,
|
||||
digger_x,
|
||||
digger_y,
|
||||
);
|
||||
}
|
||||
|
||||
DLAAlgorithm::CentralAttractor => {
|
||||
@ -246,7 +250,13 @@ impl DLABuilder {
|
||||
digger_idx = self.map.xy_idx(digger_x, digger_y);
|
||||
}
|
||||
|
||||
self.paint(prev_x, prev_y);
|
||||
paint(
|
||||
&mut self.map,
|
||||
self.symmetry,
|
||||
self.brush_size,
|
||||
prev_x,
|
||||
prev_y,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -271,70 +281,4 @@ impl DLABuilder {
|
||||
// Now we build a noise map for use in spawning entities later
|
||||
self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
|
||||
}
|
||||
|
||||
fn paint(&mut self, x: i32, y: i32) {
|
||||
match self.symmetry {
|
||||
DLASymmetry::None => self.apply_paint(x, y),
|
||||
DLASymmetry::Horizontal => {
|
||||
let center_x = self.map.width / 2;
|
||||
if x == center_x {
|
||||
self.apply_paint(x, y);
|
||||
} else {
|
||||
let dist_x = i32::abs(center_x - x);
|
||||
self.apply_paint(center_x + dist_x, y);
|
||||
self.apply_paint(center_x - dist_x, y);
|
||||
}
|
||||
}
|
||||
DLASymmetry::Vertical => {
|
||||
let center_y = self.map.height / 2;
|
||||
if y == center_y {
|
||||
self.apply_paint(x, y);
|
||||
} else {
|
||||
let dist_y = i32::abs(center_y - y);
|
||||
self.apply_paint(x, center_y + dist_y);
|
||||
self.apply_paint(x, center_y - dist_y);
|
||||
}
|
||||
}
|
||||
DLASymmetry::Both => {
|
||||
let center_x = self.map.width / 2;
|
||||
let center_y = self.map.height / 2;
|
||||
if x == center_x && y == center_y {
|
||||
self.apply_paint(x, y);
|
||||
} else {
|
||||
let dist_x = i32::abs(center_x - x);
|
||||
self.apply_paint(center_x + dist_x, y);
|
||||
self.apply_paint(center_x - dist_x, y);
|
||||
let dist_y = i32::abs(center_y - y);
|
||||
self.apply_paint(x, center_y + dist_y);
|
||||
self.apply_paint(x, center_y - dist_y);
|
||||
}
|
||||
}
|
||||
}
|
||||
let digger_idx = self.map.xy_idx(x, y);
|
||||
self.map.tiles[digger_idx] = TileType::Floor;
|
||||
}
|
||||
|
||||
fn apply_paint(&mut self, x: i32, y: i32) {
|
||||
match self.brush_size {
|
||||
1 => {
|
||||
let digger_idx = self.map.xy_idx(x, y);
|
||||
self.map.tiles[digger_idx] = TileType::Floor;
|
||||
}
|
||||
_ => {
|
||||
let half_brush_size = self.brush_size / 2;
|
||||
for brush_y in y - half_brush_size..y + half_brush_size {
|
||||
for brush_x in x - half_brush_size..x + half_brush_size {
|
||||
if brush_x > 1
|
||||
&& brush_x < self.map.width - 1
|
||||
&& brush_y > 1
|
||||
&& brush_y < self.map.height - 1
|
||||
{
|
||||
let idx = self.map.xy_idx(brush_x, brush_y);
|
||||
self.map.tiles[idx] = TileType::Floor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user