//! 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, /// Volume remaining for any physical impact or the next tick. pub volume: Option, } 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 { 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 { 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, ) -> Result { 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, }) } }