diff options
Diffstat (limited to 'crates/fparkan-runtime/src/unit_combat.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/unit_combat.rs | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index 01f1e164..e616a605 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -1,9 +1,13 @@ use super::{ + aim_convergence::sight_target, autodemo::MOBILE_UNIT, combat_report::{CombatObject, CombatReport}, default_weapon_target::{self, DefaultWeaponTarget}, player_weapon::prepare_unit_weapons, - projectile::{load_projectile, segment_sphere_fraction, unit_sphere, ANIMAL_UNIT_CLASS}, + projectile::{ + load_projectile, projectile_unit_hit_with_poses, segment_sphere_fraction, unit_sphere, + ANIMAL_UNIT_CLASS, + }, projectile_flight::ProjectileFlightHit, projectile_terrain_hit, turret_aim::{hull_local_point, TurretAim}, @@ -31,6 +35,7 @@ pub struct AutoDemoProjectileType { speed: f32, lifetime_ms: u32, volume: Option<f32>, + sight_convergence: bool, autonomous_fire_distance: f32, } @@ -254,6 +259,7 @@ impl AutoDemoCombat { speed: data.speed, lifetime_ms: data.lifetime_ms, volume: data.volume, + sight_convergence: data.sight_convergence, autonomous_fire_distance: data.autonomous_fire_distance, }); Some(battle.projectiles.len() - 1) @@ -585,7 +591,9 @@ impl AutoDemoCombat { &mission.control_device_tables, Some(now), ); - for unit in &mut self.units { + for unit_index in 0..self.units.len() { + let (before, tail) = self.units.split_at_mut(unit_index); + let (unit, after) = tail.split_first_mut().expect("unit index in bounds"); unit.weapons.read_control_energy(engine, unit.handle); let Ok(state) = transform_state(&engine.world, unit.handle) else { continue; @@ -720,7 +728,36 @@ impl AutoDemoCombat { continue; } let data = &self.projectiles[projectile]; - let direction = std::array::from_fn(|axis| to[axis] - from[axis]); + let direction = poses + .turret_sight_component(weapon.component_index) + .and_then(|component| { + let axis = poses.cpt_axis(state, weapon.component_index, barrel)?; + let hit = if data.sight_convergence { + poses + .sight_ray(&mission.mission_assets, unit.index, state, component) + .and_then(|[origin, axis]| { + sight_target(engine, origin, axis, |segment| { + projectile_unit_hit_with_poses( + engine, + unit.handle, + segment, + |index| { + collision_poses(before, index) + .or_else(|| collision_poses(after, index)) + }, + ) + }) + }) + } else { + None + }; + Some(hit.map_or(axis, |hit| { + std::array::from_fn(|axis| hit[axis] - from[axis]) + })) + }) + // shortcut: weapons without a sampled turret still use the + // target direction until their aiming controller is restored. + .unwrap_or_else(|| std::array::from_fn(|axis| to[axis] - from[axis])); if let Some(pose) = PlayerProjectile::new(from, direction, data.speed, data.lifetime_ms, now) { |
