Skip to content

Commit

Permalink
Add distance transform obstacles
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed Apr 21, 2024
1 parent 46ce3b2 commit 3854758
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ serde = "1.0.197"
serde_json = "1.0.115"
rust-embed = "8.3.0"
tinyfiledialogs = "3.9.1"
# regex = { version = "1.10.4", optional = true }
# telnet = { version = "0.2.1", optional = true }


# dt = "1.0.6"
dt = {git = "https://github.com/iMilchshake/dt"}


[dev-dependencies]
telnet = { version = "0.2.1"}
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub struct GenerationConfig {
/// probability for doing the last shift direction again
pub momentum_prob: f32,

/// TODO:
pub max_distance: f32,

// ------- TODO: these should go somewhere else -----
pub waypoints: Vec<Position>,
}
Expand Down Expand Up @@ -103,6 +106,7 @@ impl Default for GenerationConfig {
step_weights: vec![20, 11, 10, 9],
platform_distance_bounds: (500, 750),
momentum_prob: 0.01,
max_distance: 3.0,
}
}
}
33 changes: 27 additions & 6 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ pub fn edit_pos_i32(ui: &mut Ui, value: &mut i32) {
ui.add(egui::DragValue::new(value).clamp_range(0..=isize::max_value()));
}

pub fn edit_f32(ui: &mut Ui, value: &mut f32) {
// TODO: IMAGINE having a dynamic range argument.. imagine, that would be nice
pub fn edit_f32_wtf(ui: &mut Ui, value: &mut f32) {
ui.add(egui::Slider::new(value, 0.0..=15.0));
}

pub fn edit_f32_prob(ui: &mut Ui, value: &mut f32) {
ui.add(egui::Slider::new(value, 0.0..=1.0));
}

Expand Down Expand Up @@ -339,29 +344,29 @@ impl Editor {
field_edit_widget(
ui,
&mut self.config.inner_rad_mut_prob,
edit_f32,
edit_f32_prob,
"inner rad mut prob",
true,
);
field_edit_widget(
ui,
&mut self.config.inner_size_mut_prob,
edit_f32,
edit_f32_prob,
"inner size mut prob",
true,
);

field_edit_widget(
ui,
&mut self.config.outer_rad_mut_prob,
edit_f32,
edit_f32_prob,
"outer rad mut prob",
true,
);
field_edit_widget(
ui,
&mut self.config.outer_size_mut_prob,
edit_f32,
edit_f32_prob,
"outer size mut prob",
true,
);
Expand All @@ -377,11 +382,27 @@ impl Editor {
field_edit_widget(
ui,
&mut self.config.momentum_prob,
edit_f32,
edit_f32_prob,
"momentum prob",
true,
);

field_edit_widget(
ui,
&mut self.config.momentum_prob,
edit_f32_prob,
"momentum prob",
true,
);

field_edit_widget(
ui,
&mut self.config.max_distance,
edit_f32_wtf,
"max distance",
true,
);

// only show these in setup mode
ui.add_visible_ui(self.is_setup(), |ui| {
vec_edit_widget(
Expand Down
30 changes: 26 additions & 4 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::{
walker::CuteWalker,
};

use ndarray::{s, Array2};
use dt::{dt, dt_bool, dt_int};
use ndarray::{arr2, s, Array, Array2, Ix2, IxDyn};

pub struct Generator {
pub walker: CuteWalker,
pub map: Map,
pub rnd: Random,
// TODO: should this really be part of the generator?
}

impl Generator {
Expand Down Expand Up @@ -97,14 +97,36 @@ impl Generator {
Ok(edge_bug)
}

pub fn post_processing(&mut self) {
pub fn fill_area(&mut self, max_distance: &f32) -> Array2<f32> {
let grid = self.map.grid.map(|val| *val != BlockType::Empty);

let distance = dt_bool::<f32>(&grid.into_dyn())
.into_dimensionality::<Ix2>()
.unwrap();

// let max = distance.fold(0.0, |v1, v2| f32::max(v1, *v2));

self.map
.grid
.zip_mut_with(&distance, |block_type, distance| {
if *block_type == BlockType::Empty && *distance > *max_distance {
*block_type = BlockType::Freeze;
}
});

distance
}

pub fn post_processing(&mut self, config: &GenerationConfig) {
self.fix_edge_bugs().expect("fix edge bugs failed");
self.map
.generate_room(&self.map.spawn.clone(), 4, Some(&BlockType::Start))
.expect("start room generation failed");
self.map
.generate_room(&self.walker.pos.clone(), 4, Some(&BlockType::Finish))
.expect("start finish room generation");

self.fill_area(&config.max_distance);
}

/// Generates an entire map with a single function call. This function is used by the CLI.
Expand All @@ -124,7 +146,7 @@ impl Generator {
gen.step(&config)?;
}

gen.post_processing();
gen.post_processing(config);

Ok(gen.map)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn main() {

// this is called ONCE after map was generated
if editor.gen.walker.finished && !editor.is_setup() {
editor.gen.post_processing();
editor.gen.post_processing(&editor.config);

// switch into setup mode for next map
editor.set_setup();
Expand Down

0 comments on commit 3854758

Please sign in to comment.