Skip to content

Commit

Permalink
Update Seed to struct for proper ui implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed Apr 24, 2024
1 parent 7571245 commit ce79ff6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/bin/ddnet_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Econ {
let seed = vote
.vote_reason
.parse::<u64>()
.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();
Expand Down
87 changes: 28 additions & 59 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<String, GenerationConfig>,
Expand All @@ -192,7 +174,9 @@ pub struct Editor {
cam: Option<Camera2D>,
last_mouse: Option<Vec2>,
pub gen: Generator,
user_str_seed: String,

user_seed: Seed,

pub instant: bool,

/// whether to keep generating after a map is generated
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)));
});

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
46 changes: 20 additions & 26 deletions src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ use rand_distr::WeightedAliasIndex;
use seahash::hash;

pub struct Random {
pub seed_str: Option<String>,
pub seed_u64: u64,
pub seed: Seed,
gen: SmallRng,
weighted_dist: WeightedAliasIndex<i32>,
}

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 {
Expand All @@ -32,35 +42,19 @@ impl Seed {

impl Random {
pub fn new(seed: Seed, weights: Vec<i32>) -> Random {
let mut seed_str: Option<String> = 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<i32>) -> 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;
Expand Down

0 comments on commit ce79ff6

Please sign in to comment.