diff --git a/all-is-cubes-wasm/src/init.rs b/all-is-cubes-wasm/src/init.rs index cfa017732..8e7300167 100644 --- a/all-is-cubes-wasm/src/init.rs +++ b/all-is-cubes-wasm/src/init.rs @@ -102,6 +102,7 @@ async fn start_game_with_dom( .map_or_else(String::new, |q| q.search().unwrap_or_default()); let OptionsInUrl { template, + seed, graphics_options, renderer: renderer_option, } = options_from_query_string(query_string.trim_start_matches('?').as_bytes()); @@ -162,7 +163,11 @@ async fn start_game_with_dom( .build::( universe_progress, all_is_cubes_content::TemplateParameters { - seed: thread_rng().gen(), + seed: Some(seed.unwrap_or_else(|| { + let seed: u64 = thread_rng().gen(); + log::info!("Randomly chosen universe seed: {seed}"); + seed + })), size: None, }, ) diff --git a/all-is-cubes-wasm/src/url_params.rs b/all-is-cubes-wasm/src/url_params.rs index 6840a2b1b..15adc5e2c 100644 --- a/all-is-cubes-wasm/src/url_params.rs +++ b/all-is-cubes-wasm/src/url_params.rs @@ -14,6 +14,7 @@ use all_is_cubes_render::camera::GraphicsOptions; #[expect(clippy::derive_partial_eq_without_eq)] pub struct OptionsInUrl { pub template: UniverseTemplate, + pub seed: Option, pub graphics_options: GraphicsOptions, pub renderer: RendererOption, } @@ -43,11 +44,19 @@ where let s = s.borrow(); let t = s.parse::(); if t.is_err() { - log::warn!("Unrecognized value for template=: {:?}", s); + log::warn!("Unrecognized value for template=: {s:?}"); } t.ok() }) .unwrap_or_default(), + seed: params.get("seed").and_then(|s| { + let s = s.borrow(); + let t = s.parse::(); + if t.is_err() { + log::warn!("Unparseable number for seed=: {s:?}"); + } + t.ok() + }), graphics_options: GraphicsOptions::default(), // TODO: offer graphics options renderer: params .get("renderer") @@ -56,7 +65,7 @@ where match s { "wgpu" => Some(RendererOption::Wgpu), _ => { - log::warn!("Unrecognized value for renderer=: {:?}", s); + log::warn!("Unrecognized value for renderer=: {s:?}"); None } } @@ -75,6 +84,7 @@ mod tests { options_from_query_string(b""), OptionsInUrl { template: UniverseTemplate::default(), + seed: None, graphics_options: GraphicsOptions::default(), renderer: RendererOption::Wgpu, }, @@ -89,6 +99,11 @@ mod tests { ) } + #[test] + fn parse_specified_seed() { + assert_eq!(options_from_query_string(b"seed=123").seed, Some(123)); + } + #[test] fn parse_specified_renderer() { assert_eq!(