Skip to content

Commit

Permalink
Improve Editor initialization and add retry on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed Nov 30, 2024
1 parent e6ede77 commit 37dd8fd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 67 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Procedual random map generator for the _gores_ gamemode in teeworlds/ddnet (see
Assuming that you have [rust installed](https://rustup.rs/) just `git clone` and then run `cargo run` inside the project directory. For documentation on all the possible settings check out the docstrings for the `GenerationConfig` struct in `config.rs`.

### Keybinds
`e`: Export map

`space`: Generate map

`r`: Refocus camera
- `space`: Generate map
- `shift+space`: Generate map (retry on failure)
- `r`: Refocus camera
- `d`: View debug layer hover

114 changes: 56 additions & 58 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use log::warn;
use std::env;

use macroquad::input::{
is_key_pressed, is_mouse_button_down, mouse_delta_position, mouse_position, mouse_wheel,
KeyCode, MouseButton,
is_key_down, is_key_pressed, is_mouse_button_down, mouse_delta_position, mouse_position,
mouse_wheel, KeyCode, MouseButton,
};
use macroquad::time::get_fps;
use macroquad::{camera::Camera2D, input::is_mouse_button_pressed};
Expand Down Expand Up @@ -58,21 +58,34 @@ enum PausedState {
}
pub struct Editor {
state: EditorState,
pub gen_config: GenerationConfig,
pub map_config: MapConfig,
pub init_gen_configs: Vec<GenerationConfig>,
pub init_map_configs: Vec<MapConfig>,
pub canvas: Option<egui::Rect>,
pub egui_wants_mouse: Option<bool>,
pub debug_layers: Option<DebugLayers>,
pub average_fps: f32,
pub gen_config: GenerationConfig,
pub map_config: MapConfig,
pub steps_per_frame: usize,
// pub cam: Option<Camera2D>,
pub gen: Generator,
pub debug_layers: Option<DebugLayers>,
pub user_seed: Seed,

/// keeps track of camera for map visualization
pub map_cam: MapCamera,
pub canvas: Option<egui::Rect>,
pub egui_wants_mouse: Option<bool>,

/// whether to show the GenerationConfig settings
pub edit_gen_config: bool,

/// whether to show the GenerationConfig settings
pub edit_map_config: bool,

/// whether to skip initialization of debug layers
pub disable_debug_layers: bool,

pub user_seed: Seed,
/// how many generation steps are performed in one frame render
pub steps_per_frame: usize,

/// whether to perform entire map generation in one frame render
/// if yes, steps_per_frame is ignored
pub instant: bool,

/// whether to keep generating after a map is generated
Expand All @@ -81,31 +94,20 @@ pub struct Editor {
/// whether to keep using the same seed for next generations
pub fixed_seed: bool,

/// whether to show the GenerationConfig settings
pub edit_gen_config: bool,

/// whether to show the GenerationConfig settings
pub edit_map_config: bool,

/// keeps track of camera for map visualization
pub map_cam: MapCamera,
/// whether to keep using the same seed for next generations
pub retry_on_failure: bool,
}

impl Editor {
pub fn new(
gen_config: GenerationConfig,
map_config: MapConfig,
disable_debug: bool,
enable_layers: &Option<Vec<String>>,
) -> Editor {
pub fn new(gen_config: GenerationConfig, map_config: MapConfig, args: &Args) -> Editor {
let init_gen_configs: Vec<GenerationConfig> = GenerationConfig::get_all_configs();
let init_map_configs: Vec<MapConfig> = MapConfig::get_all_configs();
let gen = Generator::new(&gen_config, &map_config, Seed::from_u64(0));

let mut editor = Editor {
state: EditorState::Paused(PausedState::Setup),
debug_layers: None,
disable_debug_layers: disable_debug,
disable_debug_layers: args.disable_debug,
init_gen_configs,
init_map_configs,
canvas: None,
Expand All @@ -117,57 +119,53 @@ impl Editor {
steps_per_frame: STEPS_PER_FRAME,
gen,
user_seed: Seed::from_string(&"iMilchshake".to_string()),
instant: false,
auto_generate: false,
fixed_seed: false,
instant: args.instant,
auto_generate: args.auto_generation,
fixed_seed: args.fixed_seed,
edit_gen_config: false,
edit_map_config: false,
retry_on_failure: false,
};

editor.initialize_debug_layers();

if let Some(ref enable_layers) = enable_layers {
for layer_name in enable_layers {
let layer = editor
.debug_layers
.as_mut()
.unwrap()
.active_layers
.get_mut(layer_name.as_str());

*layer.unwrap_or_else(|| panic!("layer name '{}' doesnt exist", layer_name)) = true;
// initialize debug layers
if !editor.disable_debug_layers {
editor.initialize_debug_layers();
if let Some(ref enable_layers) = args.enable_layers {
for layer_name in enable_layers {
let layer = editor
.debug_layers
.as_mut()
.unwrap()
.active_layers
.get_mut(layer_name.as_str());

*layer.unwrap_or_else(|| panic!("layer name '{}' doesnt exist", layer_name)) =
true;
}
}
}

editor
}

pub fn handle_cli_args(&mut self, args: &Args) {
self.instant = args.instant;
self.auto_generate = args.auto_generation;
self.fixed_seed = args.fixed_seed;

// load initial gen/map configs
if let Some(config_name) = &args.gen_config {
if self.load_gen_config(config_name).is_err() {
if editor.load_gen_config(config_name).is_err() {
warn!("Coulnt load gen config {}", config_name);
}
}

if let Some(config_name) = &args.map_config {
if self.load_map_config(config_name).is_err() {
if editor.load_map_config(config_name).is_err() {
warn!("Coulnt load map config {}", config_name);
}
}

if args.generate {
self.set_playing()
editor.set_playing()
}

editor
}

pub fn initialize_debug_layers(&mut self) {
if self.disable_debug_layers {
return;
}
assert!(!self.disable_debug_layers);

// if possible, get currently active layers for re-using
let previously_active_layers = self.debug_layers.take().map(|d| d.active_layers);
Expand All @@ -194,6 +192,7 @@ impl Editor {

debug_window(egui_ctx, self);

// TODO: move to key input function!
if macroquad::input::is_key_down(KeyCode::D) {
debug_layers_window(egui_ctx, self);
}
Expand Down Expand Up @@ -283,11 +282,10 @@ impl Editor {
}

pub fn handle_user_inputs(&mut self) {
// if is_key_pressed(KeyCode::E) {
// self.save_map_dialog();
// }
if is_key_pressed(KeyCode::LeftShift) {}

if is_key_pressed(KeyCode::Space) {
self.retry_on_failure = is_key_down(KeyCode::LeftShift);
self.set_playing();
}

Expand Down
1 change: 1 addition & 0 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub fn sidebar(ctx: &Context, editor: &mut Editor) {
ui.vertical(|ui| {
ui.checkbox(&mut editor.instant, "instant");
ui.checkbox(&mut editor.auto_generate, "auto generate");
ui.checkbox(&mut editor.retry_on_failure, "retry fail");
});
});

Expand Down
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ async fn main() {
let mut editor = Editor::new(
GenerationConfig::get_initial_gen_config(),
MapConfig::get_initial_config(),
args.disable_debug,
&args.enable_layers,
&args,
);
let mut fps_ctrl = FPSControl::new().with_max_fps(60);

// handle cli args
editor.handle_cli_args(&args);

// main loop for gui (and step-wise map generation)
loop {
fps_ctrl.on_frame_start();
Expand Down Expand Up @@ -73,6 +69,10 @@ async fn main() {
.unwrap_or_else(|err| {
println!("Walker Step Failed: {:}", err);
editor.set_setup();

if editor.retry_on_failure {
editor.set_playing();
}
});

// walker did a step using SingleStep -> now pause
Expand All @@ -82,6 +82,8 @@ async fn main() {
}

// this is called ONCE after map was generated
// TODO: handling successfull generation via 'setup' state is kinda stupid, i should
// just add a new state variable for this, in the generator?
if editor.gen.walker.finished && !editor.is_setup() {
// kinda crappy, but ensure that even a panic doesnt crash the program
let _ = panic::catch_unwind(AssertUnwindSafe(|| {
Expand Down Expand Up @@ -137,6 +139,7 @@ async fn main() {
draw_walker(&editor.gen.walker);
draw_waypoints(&editor.gen.walker, colors::BLUE, colors::RED);

// TODO: move to key input function!
if macroquad::input::is_key_down(miniquad::KeyCode::D) {
draw_mouse_map_cell_pos(&editor.map_cam);
}
Expand Down

0 comments on commit 37dd8fd

Please sign in to comment.