From 2de3fd6567d6eee36910af260f1e5767213ba89c Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 16:26:55 +0400 Subject: fix(combat): converge manual shots on the authored sight ray Read the ray origin and axis from the turret CTL rows. Match the Control sight query and its minimum convergence distance for compatible projectile profiles. Free look keeps using the same turret ray. Checked Control.dll RVA 0x2a610 with near, far, and clear rays. Used the stock firing range window for the changed launch vectors and replayed ordinary player inputs from the normal start through victory. --- crates/fparkan-runtime/src/aim_convergence.rs | 84 +++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 crates/fparkan-runtime/src/aim_convergence.rs (limited to 'crates/fparkan-runtime/src/aim_convergence.rs') diff --git a/crates/fparkan-runtime/src/aim_convergence.rs b/crates/fparkan-runtime/src/aim_convergence.rs new file mode 100644 index 00000000..c67a9ab0 --- /dev/null +++ b/crates/fparkan-runtime/src/aim_convergence.rs @@ -0,0 +1,84 @@ +//! Control's sight ray and convergence point for offset barrels. + +use crate::{ + projectile::segment_point, projectile_terrain_hit, projectile_unit_hit, Engine, ObjectHandle, +}; +use fparkan_assets::{PreparedComponentControl, PreparedControlPoint}; + +/// The origin CPT and direction CPT selected by Control's turret rows. +pub fn turret_sight_points( + control: &PreparedComponentControl, +) -> Option<[&PreparedControlPoint; 2]> { + let turret = control + .typed_records + .iter() + .find(|record| record.tag == 1)?; + let [azimuth, elevation, ..] = *turret.values.as_slice() else { + return None; + }; + let origin = control.timeline_rows.get(azimuth as usize)?.raw_parameters[0]; + let direction = control + .timeline_rows + .get(elevation as usize)? + .raw_parameters[1]; + let points = &control.point_list.as_ref()?.points; + Some([ + points.get(origin as usize)?, + points.get(direction as usize)?, + ]) +} + +/// Resolves the first physical hit along the sights and clamps near convergence. +/// A clear ray leaves the barrel direction unchanged. +pub fn projectile_sight_target( + engine: &Engine, + shooter: ObjectHandle, + origin: [f32; 3], + axis: [f32; 3], +) -> Option<[f32; 3]> { + let mission = engine.loaded.as_ref()?; + let length = axis[0].hypot(axis[1]).hypot(axis[2]); + if !length.is_finite() || length <= 0.0 { + return None; + } + let axis = axis.map(|value| value / length); + let at = |distance: f32| std::array::from_fn(|i| origin[i] + axis[i] * distance); + let segment = [at(5.0), at(1_000_000.0)]; + let terrain = projectile_terrain_hit(&mission.terrain, segment).ok()?; + let unit = projectile_unit_hit(engine, shooter, segment).map(|hit| hit.fraction); + let fraction = terrain.into_iter().chain(unit).min_by(f32::total_cmp)?; + Some(convergence_point( + origin, + axis, + segment_point(segment, fraction), + )) +} + +fn convergence_point(origin: [f32; 3], axis: [f32; 3], hit: [f32; 3]) -> [f32; 3] { + // Control+0x2a610 clamps the range from the sight origin, before + // constructing the direction from the muzzle. + if (0..3).map(|i| (hit[i] - origin[i]).powi(2)).sum::() < 100.0_f32.powi(2) { + std::array::from_fn(|i| origin[i] + axis[i] * 100.0) + } else { + hit + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn near_sight_contacts_use_the_native_minimum_from_the_sight_origin() { + let origin = [10.0, 20.0, 30.0]; + let axis = [0.0, 1.0, 0.0]; + assert_eq!( + convergence_point(origin, axis, [10.0, 40.0, 30.0]), + [10.0, 120.0, 30.0] + ); + assert_eq!( + convergence_point(origin, axis, [10.0, 220.0, 30.0]), + [10.0, 220.0, 30.0] + ); + } +} -- cgit v1.2.3