diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-03 16:57:40 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-03 16:57:40 +0300 |
| commit | ca66e64b3995229ca7c8771b75ca26a60c3e344f (patch) | |
| tree | afd011b8db83dc5a7e3d0585631eae8135aefc94 /crates/fparkan-runtime/src/cockpit_speedometer.rs | |
| parent | e8978cf0a7f66fd36a3f47c5a616fbfd3a9d20b0 (diff) | |
| download | fparkan-ca66e64b3995229ca7c8771b75ca26a60c3e344f.tar.xz fparkan-ca66e64b3995229ca7c8771b75ca26a60c3e344f.zip | |
feat(ui): show cockpit speed and Control limit
Diffstat (limited to 'crates/fparkan-runtime/src/cockpit_speedometer.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/cockpit_speedometer.rs | 108 |
1 files changed, 108 insertions, 0 deletions
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<Speedometer> { + 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::<f64>() + .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 + } + ); + } + } +} |
