diff --git a/src/bin/ddnet_bridge.rs b/src/bin/ddnet_bridge.rs index ece6109..b8c7857 100644 --- a/src/bin/ddnet_bridge.rs +++ b/src/bin/ddnet_bridge.rs @@ -98,7 +98,7 @@ impl Econ { let seed = vote .vote_reason .parse::() - .unwrap_or_else(|_| Random::get_random_seed()); + .unwrap_or_else(|_| Random::get_random_u64()); // split selected preset let mut vote_parts = vote.vote_name.split_whitespace(); diff --git a/src/editor.rs b/src/editor.rs index b25d19c..b45ab0b 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,6 +1,4 @@ use std::collections::HashMap; -use std::path::PathBuf; -use std::str::FromStr; use std::{env, isize}; use egui::{ComboBox, InnerResponse, RichText}; @@ -163,22 +161,6 @@ pub fn edit_range_usize(ui: &mut Ui, values: &mut (usize, usize)) { }); } - -pub fn enum_input_ui(ui: &mut Ui, seed: &mut Seed) { - - - match seed { - // U64 - Seed::U64(value) => { - ui.add(egui::DragValue::new(value)); - }, - // String - Seed::Str(value) => { - ui.add(egui::TextEdit::singleline(value).desired_width(100.0)); - } - } -} - pub struct Editor { state: EditorState, pub configs: HashMap, @@ -192,7 +174,9 @@ pub struct Editor { cam: Option, last_mouse: Option, pub gen: Generator, - user_str_seed: String, + + user_seed: Seed, + pub instant: bool, /// whether to keep generating after a map is generated @@ -222,7 +206,7 @@ impl Editor { config, steps_per_frame: STEPS_PER_FRAME, gen, - user_str_seed: "iMilchshake".to_string(), + user_seed: Seed::from_string(&"iMilchshake".to_string()), instant: false, auto_generate: false, fixed_seed: false, @@ -293,21 +277,26 @@ impl Editor { }); }); - - Seed - egui::ComboBox::from_label("seed type") - .selected_text(format!("{:}", self.config.name.clone())) - .show_ui(ui, |ui| { - for (name, cfg) in self.configs.iter() { - ui.selectable_value(&mut self.config, cfg.clone(), name); + if self.is_setup() { + ui.horizontal(|ui| { + ui.label("str"); + if ui + .text_edit_singleline(&mut self.user_seed.seed_str) + .changed() + { + self.user_seed.seed_u64 = Seed::str_to_u64(&self.user_seed.seed_str); } }); - if self.is_setup() { - field_edit_widget(ui, &mut self.user_str_seed, edit_string, "str seed", true); - - let text_edit = egui::TextEdit::singleline(value).desired_width(100.0); - ui.add(text_edit); + ui.horizontal(|ui| { + ui.label("u64"); + if ui + .add(egui::DragValue::new(&mut self.user_seed.seed_u64)) + .changed() + { + self.user_seed.seed_str = String::new(); + } + }); ui.checkbox(&mut self.fixed_seed, "fixed seed"); } @@ -333,8 +322,6 @@ impl Editor { .to_string_lossy() .to_string(); - dbg!(&initial_path); - if let Some(path_out) = tinyfiledialogs::save_file_dialog("save config", &initial_path) { @@ -468,10 +455,9 @@ impl Editor { self.average_fps.round() as usize ))); ui.add(Label::new(format!("playback: {:?}", self.state))); - ui.add(Label::new(format!( - "seed: {:?}", - (&self.gen.rnd.seed_u64, &self.gen.rnd.seed_str) - ))); + + ui.add(Label::new(format!("seed: {:?}", self.user_seed))); + ui.add(Label::new(format!("config: {:?}", &self.config))); }); @@ -527,20 +513,11 @@ impl Editor { } fn on_start(&mut self) { - let seed = if !self.user_str_seed.is_empty() { - // generate new seed based on user string - let seed_u64 = Seed::from_string(&self.user_str_seed); - if !self.fixed_seed { - self.user_str_seed = String::new(); - } - seed_u64 - } else if self.fixed_seed { - Seed::from_u64(self.gen.rnd.seed_u64) // re-use last seed - } else { - Seed::from_u64(self.gen.rnd.random_u64()) // generate new seed from previous generator - }; + if !self.fixed_seed { + self.user_seed = Seed::from_random(&mut self.gen.rnd); + } - self.gen = Generator::new(&self.config, seed); + self.gen = Generator::new(&self.config, self.user_seed.clone()); } fn mouse_in_viewport(cam: &Camera2D) -> bool { @@ -587,14 +564,6 @@ impl Editor { self.offset = Vec2::ZERO; } - // if is_key_pressed(KeyCode::E) { - // let t0 = Instant::now(); - // let name: String = self.gen.rnd.seed_hex.clone(); - // self.gen.map.export(name); - // let time = Instant::now().duration_since(t0); - // dbg!(time); - // } - // handle mouse inputs let mouse_wheel_y = mouse_wheel().1; if !mouse_wheel_y.is_zero() { diff --git a/src/random.rs b/src/random.rs index 623821c..d8a0e81 100644 --- a/src/random.rs +++ b/src/random.rs @@ -5,24 +5,34 @@ use rand_distr::WeightedAliasIndex; use seahash::hash; pub struct Random { - pub seed_str: Option, - pub seed_u64: u64, + pub seed: Seed, gen: SmallRng, weighted_dist: WeightedAliasIndex, } -pub enum Seed { - U64(u64), - Str(String), +#[derive(Debug, Clone)] +pub struct Seed { + pub seed_u64: u64, + pub seed_str: String, } impl Seed { pub fn from_u64(seed_u64: u64) -> Seed { - Seed::U64(seed_u64) + Seed { + seed_u64, + seed_str: String::new(), + } } pub fn from_string(seed_str: &String) -> Seed { - Seed::Str(seed_str.to_owned()) + Seed { + seed_u64: Seed::str_to_u64(&seed_str), + seed_str: seed_str.to_owned(), + } + } + + pub fn from_random(rnd: &mut Random) -> Seed { + Seed::from_u64(rnd.random_u64()) } pub fn str_to_u64(seed_str: &String) -> u64 { @@ -32,35 +42,19 @@ impl Seed { impl Random { pub fn new(seed: Seed, weights: Vec) -> Random { - let mut seed_str: Option = None; - let seed_u64 = match seed { - Seed::U64(seed_u64) => seed_u64, - Seed::Str(seed) => { - let seed_u64 = Seed::str_to_u64(&seed); - seed_str = Some(seed); - seed_u64 - } - }; - Random { - seed_str, - seed_u64, - gen: SmallRng::seed_from_u64(seed_u64), + gen: SmallRng::seed_from_u64(seed.seed_u64), + seed, weighted_dist: Random::get_weighted_dist(weights), } } /// derive a u64 seed from entropy - pub fn get_random_seed() -> u64 { + pub fn get_random_u64() -> u64 { let mut tmp_rng = SmallRng::from_entropy(); tmp_rng.next_u64() } - /// uses another rnd struct to derive initial seed for a new rnd struct - pub fn from_previous_rnd(rnd: &mut Random, weights: Vec) -> Random { - Random::new(Seed::from_u64(rnd.gen.next_u64()), weights) - } - pub fn in_range_inclusive(&mut self, low: usize, high: usize) -> usize { assert!(high >= low, "no valid range"); let n = (high - low) + 1;