aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-01 23:31:29 +0300
committerValentin Popov <valentin@popov.link>2026-12-01 23:31:29 +0300
commitb3d2b95b8dd5e4578e2889845809fe7c21c5886b (patch)
tree97c106a587c350cb82f05fc20c477c3e89f6aba9 /crates/fparkan-runtime/src
parentc3e2add9a189879b5e800344e07645af591a2cbd (diff)
downloadfparkan-b3d2b95b8dd5e4578e2889845809fe7c21c5886b.tar.xz
fparkan-b3d2b95b8dd5e4578e2889845809fe7c21c5886b.zip
feat(ui): show cockpit altitude above mission sea level
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/cockpit_altimeter.rs58
-rw-r--r--crates/fparkan-runtime/src/lib.rs1
2 files changed, 59 insertions, 0 deletions
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<Altimeter> {
+ 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;