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 10, 2024
1 parent a32891a commit db16159
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 47 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),
}
}
}
12 changes: 2 additions & 10 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
collections::{BTreeMap, HashMap},
path::PathBuf,
str::FromStr,
};
use std::{collections::HashMap, path::PathBuf, str::FromStr};

const STEPS_PER_FRAME: usize = 50;

Expand All @@ -14,20 +10,16 @@ use crate::{
random::Seed,
};
use egui::{epaint::Shadow, Color32, Frame, Margin};
use itertools::Itertools;
use std::env;

use macroquad::camera::{set_camera, Camera2D};
use macroquad::input::{
is_key_pressed, is_mouse_button_down, is_mouse_button_released, mouse_position, mouse_wheel,
KeyCode, MouseButton,
};
use macroquad::math::{Rect, Vec2};
use macroquad::time::get_fps;
use macroquad::window::{screen_height, screen_width};
use macroquad::{
camera::{set_camera, Camera2D},
input::is_key_down,
};
use rand_distr::num_traits::Zero;

const ZOOM_FACTOR: f32 = 0.9;
Expand Down
59 changes: 28 additions & 31 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,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 +126,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 +373,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 +397,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 +419,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 @@ -428,6 +447,7 @@ impl Generator {
let timer = Timer::start();

let edge_bugs = self.fix_edge_bugs().expect("fix edge bugs failed");
self.debug_layers.get_mut("edge_bugs").unwrap().grid = edge_bugs;
Generator::print_time(&timer, "fix edge bugs");

self.map
Expand All @@ -438,34 +458,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);

// debug layers
// let corners_grid = &mut self.debug_layers.get_mut("corners").unwrap().grid;
// for (pos, _) in &corner_candidates {
// corners_grid[pos.as_index()] = true;
// }

// for (pos, shift) in corner_candidates {
// if let Some(end_pos) = self.check_corner_skip(&pos, &shift, (4, 15)) {
// *self
// .debug_layers
// .get_mut("corner_ends")
// .unwrap()
// .grid
// .get_mut(end_pos.as_index())
// .unwrap() = true;
//
// self.generate_skip(&pos, &end_pos, &shift);
// }
// }

// set debug layers
self.debug_layers.get_mut("edge_bugs").unwrap().grid = edge_bugs;
Generator::print_time(&timer, "set debug layers");
self.generate_all_skips(config.skip_length_bounds, config.skip_min_spacing_sqr);
Generator::print_time(&timer, "generate skips");
}

/// Generates an entire map with a single function call. This function is used by the CLI.
Expand Down
5 changes: 2 additions & 3 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ pub fn vec_edit_widget<T, F>(
.default_open(!collapsed)
.show(ui, |ui| {
ui.vertical(|ui| {
for (_i, value) in vec.iter_mut().enumerate() {
for value in vec.iter_mut() {
ui.horizontal(|ui| {
// ui.label(i.to_string());
edit_element(ui, value);
});
}
Expand Down Expand Up @@ -431,7 +430,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
2 changes: 1 addition & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl Map {
.slice_mut(s![top_left.x..=bot_right.x, top_left.y..=bot_right.y]);

for ((x, y), current_value) in view.indexed_iter_mut() {
if overide.will_override(&current_value) {
if overide.will_override(current_value) {
*current_value = value.clone();

let chunk_pos =
Expand Down

0 comments on commit db16159

Please sign in to comment.