aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/cockpit_speedometer.rs
blob: 5069013bcf5f5112e109e849234e842f26872769 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! 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>,
) {
    let driving = mission
        .boarding
        .as_mut()
        .filter(|board| board.control_mode == RemoteWarbotControl::Autopilot)
        .zip(patrol);
    mission.cockpit_speed = driving.map_or(0.0, |(board, patrol)| {
        if board.can_fly {
            if let Some(velocity) = patrol.flight_velocity_of(board.cockpit.handle) {
                board.flight_velocity = velocity;
            }
        }
        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
                }
            );
        }
    }
}