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-assets/src/ui_sprite.rs | 36 ++++++++++----- crates/fparkan-runtime/src/cockpit_altimeter.rs | 58 +++++++++++++++++++++++++ crates/fparkan-runtime/src/lib.rs | 1 + crates/fparkan-terrain/src/lib.rs | 32 ++++++++++++++ 4 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 crates/fparkan-runtime/src/cockpit_altimeter.rs (limited to 'crates') diff --git a/crates/fparkan-assets/src/ui_sprite.rs b/crates/fparkan-assets/src/ui_sprite.rs index 157b851c..fe4c77e8 100644 --- a/crates/fparkan-assets/src/ui_sprite.rs +++ b/crates/fparkan-assets/src/ui_sprite.rs @@ -1,11 +1,21 @@ //! Original UI atlas rectangles selected through the game resource table. use crate::ui_configuration::{field, number, object, read}; -use fparkan_nres::{decode, EntryId, ReadProfile}; +use fparkan_nres::{decode, EntryId, NresDocument, ReadProfile}; use fparkan_texm::{decode_mip_rgba8, decode_texm, RgbaImage}; use fparkan_vfs::Vfs; use std::{collections::HashMap, sync::Arc}; +/// Load a named page from the original game UI texture catalog. +pub fn load_ui_page(vfs: &dyn Vfs, name: &str) -> Result { + let game = read(vfs, "ui/game_resources.cfg")?; + let game = String::from_utf8_lossy(&game); + let textures = object(&game, "textures").ok_or("missing UI texture catalog")?; + let page = number(&textures, &name.to_ascii_uppercase())?; + let archive = open_atlas(vfs, field(&textures, "LIBRARY")?)?; + decode_page(&archive, page) +} + pub fn load_ui_sprites( vfs: &dyn Vfs, config: &str, @@ -16,11 +26,7 @@ pub fn load_ui_sprites( let game = read(vfs, "ui/game_resources.cfg")?; let game = String::from_utf8_lossy(&game); let textures = object(&game, "textures").ok_or("missing UI texture catalog")?; - let archive = decode( - read(vfs, field(&textures, "LIBRARY")?)?, - ReadProfile::Strict, - ) - .map_err(|error| error.to_string())?; + let archive = open_atlas(vfs, field(&textures, "LIBRARY")?)?; let mut pages = HashMap::new(); let mut sprites = Vec::with_capacity(N); for name in names { @@ -30,11 +36,7 @@ pub fn load_ui_sprites( let image = match pages.entry(page) { std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(), std::collections::hash_map::Entry::Vacant(entry) => { - let payload = archive - .payload(EntryId(page)) - .map_err(|error| error.to_string())?; - let texture = decode_texm(Arc::from(payload)).map_err(|error| error.to_string())?; - entry.insert(decode_mip_rgba8(&texture, 0).map_err(|error| error.to_string())?) + entry.insert(decode_page(&archive, page)?) } }; let [x, y, width, height] = @@ -66,3 +68,15 @@ pub fn load_ui_sprites( .try_into() .expect("one rectangle per UI sprite name")) } + +fn open_atlas(vfs: &dyn Vfs, path: &str) -> Result { + decode(read(vfs, path)?, ReadProfile::Strict).map_err(|error| error.to_string()) +} + +fn decode_page(archive: &NresDocument, page: u32) -> Result { + let payload = archive + .payload(EntryId(page)) + .map_err(|error| error.to_string())?; + let texture = decode_texm(Arc::from(payload)).map_err(|error| error.to_string())?; + decode_mip_rgba8(&texture, 0).map_err(|error| error.to_string()) +} 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; diff --git a/crates/fparkan-terrain/src/lib.rs b/crates/fparkan-terrain/src/lib.rs index fd4a28a2..7e2ae588 100644 --- a/crates/fparkan-terrain/src/lib.rs +++ b/crates/fparkan-terrain/src/lib.rs @@ -33,6 +33,7 @@ pub struct TerrainWorld { surfaces: Vec, surface_bvh: SurfaceBvh, source_mesh: Option, + sea_level: Option, basement_foundation: Option, } @@ -299,6 +300,7 @@ impl TerrainWorld { surfaces: Vec::new(), surface_bvh: SurfaceBvh::default(), source_mesh: None, + sea_level: None, basement_foundation: None, }) } @@ -314,6 +316,7 @@ impl TerrainWorld { surfaces: build_surfaces(mesh)?, surface_bvh: SurfaceBvh::default(), source_mesh: Some(mesh.clone()), + sea_level: authored_sea_level(mesh), ..Self::default() } .with_surface_bvh()) @@ -333,6 +336,7 @@ impl TerrainWorld { world.surfaces = build_surfaces(mesh)?; world.surface_bvh = SurfaceBvh::from_surfaces(&world.surfaces); world.source_mesh = Some(mesh.clone()); + world.sea_level = authored_sea_level(mesh); Ok(world) } @@ -460,6 +464,12 @@ impl TerrainWorld { self.source_mesh.as_ref() } + /// The first vertex of the first authored water face, or −1 on a dry map. + #[must_use] + pub fn sea_level(&self) -> f32 { + self.sea_level.unwrap_or(-1.0) + } + /// Returns source terrain positions without exposing parser ownership to apps. #[must_use] pub fn source_positions(&self) -> Option<&[[f32; 3]]> { @@ -1090,6 +1100,15 @@ impl RuntimeGrid { } } +fn authored_sea_level(mesh: &LandMeshDocument) -> Option { + // Terrain+0xf2f7 scans the original faces before building its surface index. + let water = mesh + .faces + .iter() + .find(|face| face.flags.0 & 0x0002_0000 != 0)?; + Some(mesh.positions.get(usize::from(water.vertices[0]))?[2]) +} + fn build_surfaces(mesh: &LandMeshDocument) -> Result, TerrainError> { let assignments = mesh .face_lod_assignments() @@ -1879,6 +1898,19 @@ mod tests { } } + #[test] + fn sea_level_uses_the_first_water_face_in_source_order() { + let mut mesh = synthetic_land_mesh(); + assert_eq!( + TerrainWorld::from_land_msh(&mesh).unwrap().sea_level(), + -1.0 + ); + mesh.faces[1].flags.0 |= 0x0002_0000; + assert_eq!(TerrainWorld::from_land_msh(&mesh).unwrap().sea_level(), 1.0); + mesh.faces[0].flags.0 |= 0x0002_0000; + assert_eq!(TerrainWorld::from_land_msh(&mesh).unwrap().sea_level(), 0.0); + } + fn synthetic_land_mesh() -> LandMeshDocument { use fparkan_terrain_format::{TerrainSlotTable, TerrainStream}; -- cgit v1.2.3