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
|
//! 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
}
);
}
}
}
|