aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/turret_aim.rs
blob: d8e145fd6b2944ebf41e754e43ccadef63a8aef6 (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
115
116
117
118
//! Aim a mount through the azimuth and elevation tracks supplied by its asset.

use crate::{Engine, ObjectHandle, TransformState};
use fparkan_assets::{
    apply_manual_turret_elevation, PreparedComponentControl, PreparedControlTimelineRow,
};
use fparkan_render::LegacyIron3dEulerTransform;

/// An autonomous turret's selected direction in its mount's coordinates.
#[derive(Clone, Copy, Debug)]
pub struct TurretAim {
    pub(super) azimuth: f32,
    pub(super) elevation: f32,
}

impl TurretAim {
    pub(super) fn toward([x, y, z]: [f32; 3]) -> Option<Self> {
        let horizontal = x.hypot(y);
        if !horizontal.is_finite() || !z.is_finite() || horizontal.hypot(z) <= f32::EPSILON {
            return None;
        }
        Some(Self {
            azimuth: (-x).atan2(y),
            elevation: z.atan2(horizontal),
        })
    }
}

/// Last aim used by the weapon sampler for a live autonomous component.
pub fn autonomous_turret_aim(
    engine: &Engine,
    handle: ObjectHandle,
    component: usize,
) -> Option<TurretAim> {
    engine
        .autodemo_combat
        .as_ref()?
        .turret_aim(handle, component)
}

fn azimuth_row(control: &PreparedComponentControl) -> Option<&PreparedControlTimelineRow> {
    let row = control
        .typed_records
        .iter()
        .find(|record| record.tag == 1)?
        .values
        .first()?;
    let row = control.timeline_rows.get(*row as usize)?;
    let span = f32::from_bits(row.raw_parameters[3]);
    (row.flags & 4 == 0 && span.is_finite() && span > 0.0).then_some(row)
}

/// Whether a component supplies a usable turret azimuth track.
pub fn has_turret_aim(control: &PreparedComponentControl) -> bool {
    azimuth_row(control).is_some()
}

/// Sample the same tracks for the rendered turret and the CPU muzzle pose.
pub fn apply_autonomous_turret_pose(
    control: &PreparedComponentControl,
    aim: TurretAim,
    node_times: &mut [Option<f32>],
) {
    let Some(row) = azimuth_row(control) else {
        return;
    };
    let blend = row.initial_blend + aim.azimuth / f32::from_bits(row.raw_parameters[3]);
    if let Some(time) = node_times.get_mut(row.node_index) {
        let mut blend = if row.flags & 1 != 0 {
            blend.rem_euclid(1.0)
        } else {
            blend.clamp(0.0, 1.0)
        };
        if row.flags & 2 != 0 {
            blend = 1.0 - blend;
        }
        let sample = (1.0 - blend) * row.frame_a + blend * row.frame_b;
        if sample.is_finite() && sample >= 0.0 {
            *time = Some(sample);
        }
    }
    apply_manual_turret_elevation(control, aim.elevation, node_times);
}

pub(super) fn hull_local_point(state: TransformState, target: [f32; 3]) -> Option<[f32; 3]> {
    let translation = state.position.map(f32::from_bits);
    let matrix = LegacyIron3dEulerTransform {
        translation,
        orientation_radians: state.orientation.map(f32::from_bits),
    }
    .try_row_major()?;
    let [dx, dy, dz] = std::array::from_fn(|i| target[i] - translation[i]);
    let scale = state.scale.map(f32::from_bits);
    let point = std::array::from_fn(|i| {
        (matrix[i] * dx + matrix[4 + i] * dy + matrix[8 + i] * dz) / scale[i]
    });
    point.iter().all(|value| value.is_finite()).then_some(point)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32::consts::{FRAC_PI_2, FRAC_PI_4};

    #[test]
    fn target_uses_hull_rotation_and_scale_before_computing_aim() {
        let hull = TransformState {
            position: [20.0, 30.0, 40.0].map(f32::to_bits),
            orientation: [0.0, 0.0, FRAC_PI_2].map(f32::to_bits),
            scale: [2.0, 3.0, 4.0].map(f32::to_bits),
        };
        let local_target = hull_local_point(hull, [20.0, 32.0, 44.0]).unwrap();
        let aim = TurretAim::toward(local_target).unwrap();
        assert!((aim.azimuth + FRAC_PI_2).abs() < 1e-5);
        assert!((aim.elevation - FRAC_PI_4).abs() < 1e-5);
        assert!(TurretAim::toward([0.0; 3]).is_none());
    }
}