aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/projectile_flight.rs
blob: 2efb8bb784f8383596f452aadd11622fc3e376d7 (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
//! Order field crossings and physical contacts along the traveled segment.

use crate::{
    projectile::{projectile_unit_hit_with_poses, segment_point},
    projectile_shield::{absorb, shield_crossings},
    projectile_terrain_hit, Engine, ObjectHandle, PlayerProjectile, ProjectileContact,
};
use fparkan_terrain::TerrainError;

/// What ended this flight segment before its planned endpoint.
pub enum ProjectileFlightHit {
    /// The flight reached the ground or water surface.
    Terrain,
    /// A charged field absorbed all the remaining volume.
    Shield,
    /// The segment reached geometry or a physical collision fallback.
    Body(ProjectileContact),
}

/// Result of one queue tick of projectile flight.
pub struct ProjectileFlightStep {
    pub flying: bool,
    /// Traveled trace clipped to the contact, absent if this tick covered no distance.
    pub segment: Option<[[f32; 3]; 2]>,
    pub hit: Option<ProjectileFlightHit>,
    /// Volume remaining for any physical impact or the next tick.
    pub volume: Option<f32>,
}

impl PlayerProjectile {
    /// Advances a player shot through the mission terrain, fields and live bodies.
    pub fn advance_in_world(
        &mut self,
        engine: &mut Engine,
        shooter: ObjectHandle,
        clock_ms: u32,
    ) -> Result<ProjectileFlightStep, TerrainError> {
        self.advance_in_world_with(engine, shooter, clock_ms, crate::projectile_unit_hit)
    }

    pub(super) fn advance_in_world_with_poses<'a>(
        &mut self,
        engine: &mut Engine,
        shooter: ObjectHandle,
        clock_ms: u32,
        poses: impl Fn(usize) -> Option<&'a crate::weapon_pose::WeaponPoses>,
    ) -> Result<ProjectileFlightStep, TerrainError> {
        self.advance_in_world_with(engine, shooter, clock_ms, |engine, shooter, segment| {
            projectile_unit_hit_with_poses(engine, shooter, segment, &poses)
        })
    }

    fn advance_in_world_with(
        &mut self,
        engine: &mut Engine,
        shooter: ObjectHandle,
        clock_ms: u32,
        body_contact: impl Fn(&Engine, ObjectHandle, [[f32; 3]; 2]) -> Option<ProjectileContact>,
    ) -> Result<ProjectileFlightStep, TerrainError> {
        let mut traveled = None;
        let mut hit = None;
        let mut volume = self.volume;
        let flying = self.advance(clock_ms, |segment| {
            let Some(mission) = &engine.loaded else {
                return Ok(true);
            };
            let ground = projectile_terrain_hit(&mission.terrain, segment)?;
            let body = body_contact(engine, shooter, segment)
                .filter(|contact| ground.is_none_or(|fraction| contact.fraction < fraction));
            let mut stop = body.as_ref().map(|contact| contact.fraction).or(ground);
            hit = body
                .map(ProjectileFlightHit::Body)
                .or_else(|| ground.map(|_| ProjectileFlightHit::Terrain));
            if let Some(mut remaining) = volume {
                for crossing in shield_crossings(engine, shooter, segment, stop) {
                    remaining = absorb(engine, crossing, remaining);
                    if remaining == 0.0 {
                        stop = Some(crossing.fraction);
                        hit = Some(ProjectileFlightHit::Shield);
                        break;
                    }
                }
                volume = Some(remaining);
            }
            traveled = Some(match stop {
                Some(fraction) => [segment[0], segment_point(segment, fraction)],
                None => segment,
            });
            Ok(stop.is_some())
        })?;
        self.volume = volume;
        Ok(ProjectileFlightStep {
            flying,
            segment: traveled,
            hit,
            volume,
        })
    }
}