From fbc1b236b0fa173d0cc9b87e80b6a9be2a2af341 Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 30 Nov 2024 00:13:59 +0100 Subject: [PATCH] implement `displayed_mass_kg` command, add masses for planets/moons --- src/cmd.rs | 11 +++++++++++ src/data/defs.txt | 15 +++++++++++++++ src/hud.rs | 10 +++++++--- src/nature.rs | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/cmd.rs b/src/cmd.rs index 3925c7f..9065b90 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -86,6 +86,7 @@ struct ParserState { velocity: DVec3, angular_momentum: DVec3, pronoun: Option, + displayed_mass: Option, message_on_entry: Option, chat_counts_towards_achievements: bool, is_sphere: bool, @@ -153,6 +154,7 @@ impl Default for ParserState { velocity: DVec3::splat(0.0), angular_momentum: DVec3::new(0.03, 0.3, 0.09), pronoun: None, + displayed_mass: None, message_on_entry: None, chat_counts_towards_achievements: true, is_sphere: false, @@ -515,6 +517,14 @@ pub fn load_defs(mut ew_spawn: EventWriter) { ["pronoun", pronoun] => { state.pronoun = Some(pronoun.to_string()); } + ["displayed_mass_kg", mass] => { + if let Ok(mass_float) = mass.parse::() { + state.displayed_mass = Some(mass_float); + } else { + error!("Can't parse float: {line}"); + continue; + } + } ["chatid", chat] => { state.chat = chat.to_string(); } @@ -1507,6 +1517,7 @@ fn spawn_entities( actor.insert(hud::IsClickable { name: state.name.clone(), pronoun: state.pronoun.clone(), + displayed_mass: state.displayed_mass.clone(), ..default() }); } diff --git a/src/data/defs.txt b/src/data/defs.txt index 1924ecb..9620006 100644 --- a/src/data/defs.txt +++ b/src/data/defs.txt @@ -4,6 +4,7 @@ actor 0 0 0 scale 695700e3 sphere yes sun yes + displayed_mass_kg 1.9885e30 physics off actor 0 0 0 mercury name Mercury @@ -11,6 +12,7 @@ actor 0 0 0 sphere yes physics off scale 2439.7e3 + displayed_mass_kg 3.3011e23 axialtilt 0.034 orbitaround sol 57.91e9 actor 0 0 0 orbitring @@ -24,6 +26,7 @@ actor 0 0 0 sphere yes physics off scale 6115e3 + displayed_mass_kg 4.8675e24 axialtilt 177.36 orbitaround sol 108.21e9 actor 0 0 0 orbitring @@ -38,6 +41,7 @@ actor 0 0 0 sphere yes physics off scale 6371e3 + displayed_mass_kg 5.972168e24 axialtilt 23.4392811 orbitaround sol 149.598023e9 actor 0 0 0 orbitring @@ -53,6 +57,7 @@ actor 0 0 0 tidally locked angularmomentum 0 0 0 scale 1737.4e3 + displayed_mass_kg 7.346e22 rotationz 90 sphere yes moon yes @@ -68,6 +73,7 @@ actor 0 0 0 sphere yes physics off scale 3389.5e3 + displayed_mass_kg 6.4171e23 axialtilt 25.19 orbitaround sol 227.939366e9 actor 0 0 0 orbitring @@ -81,6 +87,7 @@ actor 0 0 0 id jupiter name Jupiter scale 69911e3 + displayed_mass_kg 1.8982e27 planet yes sphere yes ring yes @@ -186,6 +193,7 @@ actor 0 0 0 orbitaround jupiter 421700e3 tidally locked scale 1822e3 + displayed_mass_kg 8.931938e22 angularmomentum 0 0 0 sphere yes moon yes @@ -202,6 +210,7 @@ actor 0 0 0 orbitaround jupiter 670900e3 tidally locked scale 1561e3 + displayed_mass_kg 4.79984e22 angularmomentum 0 0 0 sphere yes moon yes @@ -218,6 +227,7 @@ actor 0 0 0 orbitaround jupiter 1070400e3 tidally locked scale 2634e3 + displayed_mass_kg 1.4819e23 angularmomentum 0 0 0 sphere yes moon yes @@ -234,6 +244,7 @@ actor 0 0 0 orbitaround jupiter 1882700e3 tidally locked scale 2410e3 + displayed_mass_kg 1.075938e23 angularmomentum 0 0 0 sphere yes moon yes @@ -251,6 +262,7 @@ actor 0 0 0 aurora yes physics off scale 58232e3 + displayed_mass_kg 5.6834e26 axialtilt 26.73 orbitaround sol 1433.53e9 actor 0 0 0 orbitring @@ -265,6 +277,7 @@ actor 0 0 0 ring yes physics off scale 25362e3 + displayed_mass_kg 8.6810e25 axialtilt 82.23 orbitaround sol 2870.972e9 actor 0 0 0 orbitring @@ -279,6 +292,7 @@ actor 0 0 0 ring yes physics off scale 24622e3 + displayed_mass_kg 1.02409e26 axialtilt 28.32 orbitaround sol 4500e9 actor 0 0 0 orbitring @@ -288,6 +302,7 @@ actor 0 0 0 orbitring yes actor 0 0 0 name Pluto + displayed_mass_kg 1.3025e22 orbitaround sol 5906.38e9 actor 0 0 0 orbitring scale 5906.38e9 diff --git a/src/hud.rs b/src/hud.rs index 78e3630..618c558 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -264,6 +264,7 @@ impl Message { pub struct IsClickable { pub name: Option, pub pronoun: Option, + pub displayed_mass: Option, pub distance: Option, pub disable_in_map: bool, } @@ -272,6 +273,7 @@ impl Default for IsClickable { Self { name: None, pronoun: None, + displayed_mass: None, distance: None, disable_in_map: false, } @@ -1066,13 +1068,15 @@ fn update_hud( "".to_string() }; let size = nature::readable_si(trans.scale.x as f64 * 2.0); - let mass = if let Some(mass) = mass { - nature::readable_si(mass.0 * 0.001) + let mass = if let Some(mass) = clickable.displayed_mass { + nature::readable_mass(mass) + } else if let Some(mass) = mass { + nature::readable_mass(mass.0) } else { String::from("?") }; text.sections[0].value = - format!("Target: {target_name}\n{pronoun}Distance: {distance}\nSize: {size}m\nMass: {mass}g\n\n"); + format!("Target: {target_name}\n{pronoun}Distance: {distance}\nSize: {size}m\nMass: {mass}\n\n"); } else { text.sections[0].value = "".to_string(); } diff --git a/src/nature.rs b/src/nature.rs index 652f3d6..7c0408e 100644 --- a/src/nature.rs +++ b/src/nature.rs @@ -157,6 +157,21 @@ pub fn readable_speed(speed: f64) -> String { } } +/// Unit: kg +pub fn readable_mass(value: f64) -> String { + if value > SOL_MASS * 0.0001 { + return format!("{:.5} Suns", value / SOL_MASS); + } else if value > EARTH_MASS * 0.0001 { + return format!("{:.5} Earths", value / EARTH_MASS); + } else if value > 1e6 { + return format!("{value:.2e} kg").replace("e", " x 10^"); + } else if value >= 10.0 { + return format!("{value:.0} kg"); + } else { + return readable_si(value * 1000.0) + "g"; + } +} + pub fn readable_si(value: f64) -> String { let abs = value.abs(); if abs > 1e13 {