Skip to content

Commit

Permalink
Fix debug layers and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed May 8, 2024
1 parent a32891a commit c31af38
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl Default for GenerationConfig {
waypoint_reached_dist: 250,
inner_size_probs: vec![(3, 0.25), (5, 0.75)],
outer_margin_probs: vec![(0, 0.5), (2, 0.5)],
skip_min_spacing_sqr: 18,
skip_length_bounds: (4, 15),
skip_min_spacing_sqr: 45,
skip_length_bounds: (3, 11),
}
}
}
36 changes: 29 additions & 7 deletions src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::borrow::BorrowMut;
use std::{f32::consts::SQRT_2, usize};

use clap::Parser;
use std::collections::BTreeMap;
use timing::Timer;

Expand Down Expand Up @@ -40,9 +42,9 @@ impl Generator {
let rnd = Random::new(seed, config);

let debug_layers = BTreeMap::from([
("edge_bugs", DebugLayer::new(true, colors::RED, &map)),
("corners", DebugLayer::new(true, colors::BLUE, &map)),
("corner_ends", DebugLayer::new(true, colors::GREEN, &map)),
("edge_bugs", DebugLayer::new(true, colors::BLUE, &map)),
("skips", DebugLayer::new(true, colors::GREEN, &map)),
("skips_invalid", DebugLayer::new(true, colors::RED, &map)),
]);

Generator {
Expand Down Expand Up @@ -126,7 +128,7 @@ impl Generator {

/// Using a distance transform this function will fill up all empty blocks that are too far
/// from the next solid/non-empty block
pub fn fill_area(&mut self, max_distance: &f32) -> Array2<f32> {
pub fn fill_open_areas(&mut self, max_distance: &f32) -> Array2<f32> {
let grid = self.map.grid.map(|val| *val != BlockType::Empty);

// euclidean distance transform
Expand Down Expand Up @@ -373,7 +375,7 @@ impl Generator {
}
}

pub fn get_all_valid_skips(&mut self, length_bounds: (usize, usize), min_spacing_sqr: usize) {
pub fn generate_all_skips(&mut self, length_bounds: (usize, usize), min_spacing_sqr: usize) {
// get corner candidates
let corner_candidates = self.find_corners().expect("corner detection failed");

Expand All @@ -397,6 +399,7 @@ impl Generator {
}

// skip is valid -> invalidate all following conflicting skips
// TODO: right now skips can still cross each other
let (start, end, _, _) = &skips[skip_index];
for other_index in (skip_index + 1)..skips.len() {
let (other_start, other_end, _, _) = &skips[other_index];
Expand All @@ -418,6 +421,24 @@ impl Generator {
self.generate_skip(start, end, shift);
}
}

// set debug layer for valid skips
let debug_skips = &mut self.debug_layers.get_mut("skips").unwrap().grid;
for ((start, end, _, _), valid) in skips.iter().zip(valid_skips.iter()) {
if *valid {
debug_skips[start.as_index()] = true;
debug_skips[end.as_index()] = true;
}
}

// set debug layer for invalid skips
let debug_skips_invalid = &mut self.debug_layers.get_mut("skips_invalid").unwrap().grid;
for ((start, end, _, _), valid) in skips.iter().zip(valid_skips.iter()) {
if !*valid {
debug_skips_invalid[start.as_index()] = true;
debug_skips_invalid[end.as_index()] = true;
}
}
}

pub fn print_time(timer: &Timer, message: &str) {
Expand All @@ -438,10 +459,11 @@ impl Generator {
.expect("start finish room generation");
Generator::print_time(&timer, "place rooms");

self.fill_area(&config.max_distance);
self.fill_open_areas(&config.max_distance);
Generator::print_time(&timer, "place obstacles");

self.get_all_valid_skips(config.skip_length_bounds, config.skip_min_spacing_sqr);
self.generate_all_skips(config.skip_length_bounds, config.skip_min_spacing_sqr);
Generator::print_time(&timer, "generate skips");

// debug layers
// let corners_grid = &mut self.debug_layers.get_mut("corners").unwrap().grid;
Expand Down
2 changes: 1 addition & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ pub fn sidebar(ctx: &Context, editor: &mut Editor) {
ui,
&mut editor.config.skip_min_spacing_sqr,
edit_usize,
"skip length bounds",
"skip min spacing sqr",
true,
);
}
Expand Down

0 comments on commit c31af38

Please sign in to comment.