From b3d2b95b8dd5e4578e2889845809fe7c21c5886b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 00:31:29 +0400 Subject: feat(ui): show cockpit altitude above mission sea level --- crates/fparkan-runtime/src/cockpit_altimeter.rs | 58 +++++++++++++++++++++++++ crates/fparkan-runtime/src/lib.rs | 1 + 2 files changed, 59 insertions(+) create mode 100644 crates/fparkan-runtime/src/cockpit_altimeter.rs (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/cockpit_altimeter.rs b/crates/fparkan-runtime/src/cockpit_altimeter.rs new file mode 100644 index 00000000..e511872a --- /dev/null +++ b/crates/fparkan-runtime/src/cockpit_altimeter.rs @@ -0,0 +1,58 @@ +//! The cockpit altimeter samples the controlled object's origin in world space. + +use crate::{loaded_terrain, Engine, StartingCockpit}; +use fparkan_world::transform_state; + +/// The signed reading and the first visible scanline of the altitude scale. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct Altimeter { + pub altitude: i32, + pub scale_top: u32, +} + +impl Altimeter { + pub const SCALE_HEIGHT: u32 = 105; + + fn from_elevations(z: f32, sea_level: f32) -> Self { + // iron3d+0x3fa62 subtracts the two source floats in x87 and rounds to integer. + let altitude = (f64::from(z) - f64::from(sea_level)).round_ties_even() as i32; + let percent = (altitude.clamp(-100, 100) + 100) as u32 * 100 / 200; + Self { + altitude, + scale_top: (100 - percent) * Self::SCALE_HEIGHT / 100, + } + } +} + +/// Reads the altimeter for the active hero or boarded vehicle. +#[must_use] +pub fn cockpit_altimeter(engine: &Engine, cockpit: StartingCockpit) -> Option { + let sea_level = loaded_terrain(engine)?.sea_level(); + let pose = transform_state(&engine.world, cockpit.handle).ok()?; + let z = f32::from_bits(pose.position[2]); + Some(Altimeter::from_elevations(z, sea_level)) +} + +#[cfg(test)] +mod tests { + use super::Altimeter; + + #[test] + fn original_altimeter_rounds_the_signed_distance_then_clips_only_the_scale() { + for (z, sea, altitude, scale_top) in [ + (14.09392, -1.72549, 16, 44), + (2.5, 0.0, 2, 51), + (-2.5, 0.0, -2, 53), + (-150.0, 0.0, -150, 105), + (170.0, 0.0, 170, 0), + ] { + assert_eq!( + Altimeter::from_elevations(z, sea), + Altimeter { + altitude, + scale_top + } + ); + } + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index a53e32f0..2a5497f1 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -101,6 +101,7 @@ pub use destruction::{ collect_unit_destruction_effects, take_unit_destruction_effects, unit_destruction_effect_sources, DestroyedMesh, UnitDestructionEffect, }; +pub mod cockpit_altimeter; mod flight_altitude; mod ground_support; mod laboratory; -- cgit v1.2.3