From ca66e64b3995229ca7c8771b75ca26a60c3e344f Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 3 Dec 2026 17:57:40 +0400 Subject: feat(ui): show cockpit speed and Control limit --- crates/fparkan-runtime/src/boarding.rs | 2 + crates/fparkan-runtime/src/cockpit_speedometer.rs | 108 ++++++++++++++++++++++ crates/fparkan-runtime/src/lib.rs | 10 +- crates/fparkan-runtime/src/manual_chassis.rs | 2 + crates/fparkan-runtime/src/manual_flight.rs | 9 ++ crates/fparkan-runtime/src/player_motion.rs | 25 +++++ crates/fparkan-runtime/src/unit_patrol.rs | 22 ++++- 7 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 crates/fparkan-runtime/src/cockpit_speedometer.rs (limited to 'crates') diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs index bbcc4615..c8f586b5 100644 --- a/crates/fparkan-runtime/src/boarding.rs +++ b/crates/fparkan-runtime/src/boarding.rs @@ -217,6 +217,7 @@ fn start_player_control( flight_velocity: [0.0; 3], }); mission.player_pitch_bits = 0; + mission.cockpit_speed = 0.0; mission.hero_walk_velocity = [0.0; 2]; mission.hero_look.reset(); } @@ -375,6 +376,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result, } mission.boarding = None; mission.player_pitch_bits = 0; + mission.cockpit_speed = 0.0; mission.hero_walk_velocity = [0.0; 2]; if turret.is_some() { if let Some(vfs) = &engine.services.vfs { diff --git a/crates/fparkan-runtime/src/cockpit_speedometer.rs b/crates/fparkan-runtime/src/cockpit_speedometer.rs new file mode 100644 index 00000000..021fc3ec --- /dev/null +++ b/crates/fparkan-runtime/src/cockpit_speedometer.rs @@ -0,0 +1,108 @@ +//! Speed and scale readings for the currently controlled cockpit. + +use crate::{ + unit_patrol::PreparedPatrol, Engine, LoadedMissionState, RemoteWarbotControl, StartingCockpit, +}; + +/// The reading in km/h and the first visible row of the fill. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct Speedometer { + pub speed: i32, + pub scale_top: u32, +} + +impl Speedometer { + fn from_speeds(speed: f32, limit: f32) -> Self { + // iron3d+0x3fcd8 multiplies by the stored float in x87, then rounds both values. + let kmh = |meters_per_second| { + (f64::from(meters_per_second) * f64::from(3.6_f32)).round_ties_even() as i32 + }; + let speed = kmh(speed); + let limit = kmh(limit); + let percent = if limit == 0 { + 0 + } else { + (i64::from(speed) * 100 / i64::from(limit)).clamp(0, 100) as u32 + }; + Self { + speed, + scale_top: (100 - percent) * 105 / 100, + } + } +} + +/// Reads Control's speed and forward scale for the active body. +#[must_use] +pub fn cockpit_speedometer(engine: &Engine, cockpit: StartingCockpit) -> Option { + let mission = engine.loaded.as_ref()?; + let active = mission + .boarding + .map_or_else(|| mission.local_hero, |board| Some(board.cockpit.handle)); + if active != Some(cockpit.handle) { + return None; + } + // Control selector 0x90 reads word seven from the owner's profile. + let limit = mission + .control_device_tables + .iter() + .find(|table| table.handle == cockpit.handle) + .and_then(|table| table.owner_profile.as_ref()) + .map_or(0.0, |profile| f32::from_bits(profile.current_words[7])); + Some(Speedometer::from_speeds(mission.cockpit_speed, limit)) +} + +pub(super) fn record_route_speed( + mission: &mut LoadedMissionState, + patrol: Option<&PreparedPatrol>, +) { + mission.cockpit_speed = mission + .boarding + .filter(|board| board.remote == Some(RemoteWarbotControl::Autopilot)) + .zip(patrol) + .map_or(0.0, |(board, patrol)| { + patrol.speed_of(board.cockpit.handle, board.can_fly) + }); +} + +pub(super) fn length(vector: [f32; 3]) -> f32 { + vector + .map(f64::from) + .into_iter() + .map(|v| v * v) + .sum::() + .sqrt() as f32 +} + +pub(super) fn translation_speed(before: [f32; 3], after: [f32; 3], seconds: f32) -> f32 { + if seconds > 0.0 { + length(std::array::from_fn(|axis| after[axis] - before[axis])) / seconds + } else { + 0.0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn original_speedometer_rounds_both_values_before_clipping_the_strip() { + for (velocity, limit, expected, scale_top) in [ + ([0.0, 0.0, 0.0], 14.0, 0, 105), + ([3.0, 4.0, 0.0], 14.0, 18, 67), + ([0.0, 0.0, 7.0], 14.0, 25, 52), + ([-14.0, 0.0, 0.0], 14.0, 50, 0), + ([9.0, 12.0, 0.0], 8.0, 54, 0), + ([3.0, 4.0, 0.0], 0.0, 18, 105), + ([1.25, 0.0, 0.0], 14.0, 4, 96), + ] { + assert_eq!( + Speedometer::from_speeds(length(velocity), limit), + Speedometer { + speed: expected, + scale_top + } + ); + } + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index d5c5b30f..6081fe98 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -113,6 +113,7 @@ pub use destruction::{ }; pub mod cockpit_altimeter; pub mod cockpit_instruments; +pub mod cockpit_speedometer; mod flight_altitude; mod ground_support; mod laboratory; @@ -1565,6 +1566,7 @@ struct LoadedMissionState { self_destructs: Vec, player_pitch_bits: u32, player_gait: player_gait::PlayerGait, + cockpit_speed: f32, hero_walk_velocity: [f32; 2], hero_look: hero_look::HeroLook, boarding: Option, @@ -2936,6 +2938,7 @@ node_provider_distribution_suspended: false, self_destructs: Vec::new(), player_pitch_bits: 0, player_gait: Default::default(), + cockpit_speed: 0.0, hero_walk_velocity: [0.0; 2], hero_look: hero_look::HeroLook::load(vfs.as_ref()), boarding: None, @@ -3022,8 +3025,11 @@ pub fn step_headless( return Err(error.into()); } }; - if let (Some(patrol), Some(mission)) = (patrol, &mut engine.loaded) { - mission.unit_patrol = Some(patrol.state); + if let Some(mission) = &mut engine.loaded { + cockpit_speedometer::record_route_speed(mission, patrol.as_ref()); + if let Some(patrol) = patrol { + mission.unit_patrol = Some(patrol.state); + } } if let Some(resources) = &mut resources { resources.rng_state = crt; diff --git a/crates/fparkan-runtime/src/manual_chassis.rs b/crates/fparkan-runtime/src/manual_chassis.rs index 3be28403..fcf3f43c 100644 --- a/crates/fparkan-runtime/src/manual_chassis.rs +++ b/crates/fparkan-runtime/src/manual_chassis.rs @@ -109,6 +109,8 @@ mod tests { let after = transform_state(&engine.world, robot).unwrap(); let [nx, ny, z] = after.position.map(f32::from_bits); assert!(((nx - x).hypot(ny - y) - 0.1 * cap).abs() < 0.001); + let reading = crate::cockpit_speedometer::cockpit_speedometer(&engine, cockpit).unwrap(); + assert_eq!(reading.scale_top, 0); let terrain = &engine.loaded.as_ref().unwrap().terrain; let support = ground .height([nx, ny], after, &mut |xy| { diff --git a/crates/fparkan-runtime/src/manual_flight.rs b/crates/fparkan-runtime/src/manual_flight.rs index cac50b41..f4f2a118 100644 --- a/crates/fparkan-runtime/src/manual_flight.rs +++ b/crates/fparkan-runtime/src/manual_flight.rs @@ -252,6 +252,12 @@ mod tests { .unwrap() .unwrap(); assert_eq!(vehicle.handle, helicopter); + let reading = |engine: &Engine| { + crate::cockpit_speedometer::cockpit_speedometer(engine, vehicle) + .unwrap() + .speed + }; + assert_eq!(reading(&engine), 0); let [x, y, z] = position(&engine, vehicle); let step = plan_player_motion(&engine, vehicle, up).unwrap().unwrap(); assert!(step.sweep.is_some()); @@ -260,6 +266,7 @@ mod tests { let [_, _, climb_z] = position(&engine, vehicle); assert!((8.0..16.0).contains(&(climb_z - z))); assert_eq!(position(&engine, hero), [x, y, climb_z]); + assert!(reading(&engine) > 0); let coast = PlayerMotion { delta_seconds: 1.0, @@ -274,6 +281,7 @@ mod tests { assert!(plan_player_motion(&engine, vehicle, coast) .unwrap() .is_none()); + assert_eq!(reading(&engine), 0); let forward = PlayerMotion { climb: 0.0, @@ -288,6 +296,7 @@ mod tests { let [fx, fy, fz] = position(&engine, vehicle); assert!(((fx - x).hypot(fy - y) - 1.25).abs() < 0.0001); assert_eq!(fz, hover_z); + assert_eq!(reading(&engine), 36); let down = PlayerMotion { climb: -1.0, diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index 6733f921..a32be631 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -71,6 +71,7 @@ pub struct PlayerMotionStep { pub(super) pitch_bits: u32, flight_velocity: Option<[f32; 3]>, hero_walk_velocity: Option<[f32; 2]>, + movement_seconds: f32, hero_look: Option, turret: Option, max_floor_clearance: f32, @@ -100,6 +101,20 @@ impl WalkingSurface { } impl PlayerMotionStep { + fn speed(self) -> f32 { + use crate::cockpit_speedometer::{length, translation_speed}; + + if let Some(velocity) = self.flight_velocity { + length(velocity) + } else if let Some([x, y]) = self.hero_walk_velocity { + length([x, y, 0.0]) + } else { + self.sweep.map_or(0.0, |[[x0, y0, _], [x1, y1, _]]| { + translation_speed([x0, y0, 0.0], [x1, y1, 0.0], self.movement_seconds) + }) + } + } + /// Uses the scene's terrain after foundation cuts and fills. #[must_use] pub fn with_scene_terrain( @@ -228,6 +243,7 @@ mod tests { pitch_bits: 0, flight_velocity: None, hero_walk_velocity: None, + movement_seconds: 0.0, hero_look: None, turret: None, max_floor_clearance: FLOOR_CLEARANCE, @@ -269,6 +285,7 @@ mod tests { pitch_bits: 0, flight_velocity: None, hero_walk_velocity: None, + movement_seconds: 0.0, hero_look: None, turret: None, max_floor_clearance: 1.25, @@ -295,6 +312,7 @@ mod tests { pitch_bits: 0, flight_velocity: None, hero_walk_velocity: None, + movement_seconds: 0.0, hero_look: None, turret: None, max_floor_clearance: FLOOR_CLEARANCE, @@ -657,6 +675,12 @@ pub(super) fn commit_motion_state( if let Some(look) = step.hero_look { mission.hero_look = look; } + if mission + .boarding + .is_none_or(|board| board.controls_chassis()) + { + mission.cockpit_speed = if blocked { 0.0 } else { step.speed() }; + } } if let Some(boarding) = &mut mission.boarding { if boarding.cockpit.handle != cockpit.handle { @@ -774,6 +798,7 @@ fn plan_motion( pitch_bits: pitch.to_bits(), flight_velocity, hero_walk_velocity: None, + movement_seconds: input.delta_seconds, hero_look: None, turret: None, max_floor_clearance: FLOOR_CLEARANCE, diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index c25b309f..a22543b6 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -71,6 +71,7 @@ pub(super) struct PatrollingUnit { pub(super) struct PreparedPatrol { pub(super) state: UnitPatrol, poses: Vec<(ObjectHandle, TransformState, TransformState)>, + seconds: f32, } #[derive(Clone, Copy)] @@ -880,7 +881,11 @@ impl UnitPatrol { poses.push((unit.handle, before, after)); } } - Ok(PreparedPatrol { state, poses }) + Ok(PreparedPatrol { + state, + poses, + seconds, + }) } } @@ -1126,6 +1131,21 @@ fn choose_route( } impl PreparedPatrol { + pub(super) fn speed_of(&self, handle: ObjectHandle, can_fly: bool) -> f32 { + self.poses + .iter() + .find(|(moved, _, _)| *moved == handle) + .map_or(0.0, |(_, before, after)| { + let from = before.position.map(f32::from_bits); + let mut to = after.position.map(f32::from_bits); + if !can_fly { + // Ground support moves the root vertically without driving the chassis. + to[2] = from[2]; + } + crate::cockpit_speedometer::translation_speed(from, to, self.seconds) + }) + } + pub(super) fn apply_poses(&self, world: &mut World) -> Result<(), EngineError> { // These handles were read during preparation and the world has yet to step. for &(handle, _, after) in &self.poses { -- cgit v1.2.3