diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/aim_convergence.rs | 84 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/projectile.rs | 5 |
3 files changed, 91 insertions, 0 deletions
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::<f32>() < 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] + ); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index c2b4f7e5..6150fa44 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -119,6 +119,7 @@ mod player_motion; mod unit_speed; pub use patrol_points::PatrolPointReport; pub use unit_speed::{UnitSpeedKind, UnitSpeedReport}; +mod aim_convergence; mod factory_release; mod player_status; mod player_weapon; @@ -164,6 +165,7 @@ mod wheel_animation; pub use weapon_pose::{initial_component_node_times, WeaponPoseReport}; pub use wheel_animation::WheelAnimation; +pub use aim_convergence::{projectile_sight_target, turret_sight_points}; pub use autodemo::{autodemo_camera, AutoDemoCamera, AutoDemoReinforcement, AutoDemoView}; pub use boarding::{ board_friendly_warbot, disembark_warbot, player_vehicle, select_boarding_target, diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs index 720aad08..5681ad4f 100644 --- a/crates/fparkan-runtime/src/projectile.rs +++ b/crates/fparkan-runtime/src/projectile.rs @@ -25,6 +25,8 @@ pub struct ProjectileAssets { pub lifetime_ms: u32, /// Initial provider volume of the projectile. pub volume: Option<f32>, + /// Whether the BULL profile permits bringing the shot onto the sight ray. + pub sight_convergence: bool, pub(super) autonomous_fire_distance: f32, } @@ -75,6 +77,8 @@ pub(super) fn load_projectile( }; let speed = f32::from_bits(control.native_profile_words[7]); let lifetime_ms = control.native_profile_words[18]; + // Control+0x297b1 forces a nonzero launch mode for this profile flag. + let sight_convergence = control.native_profile_words[21] == 0; let autonomous_fire_distance = f32::from_bits(control.native_profile_words[22]); let volume = control.node_data.as_ref().and_then(|data| { let amount = data.rows.first()?.max_volume; @@ -88,6 +92,7 @@ pub(super) fn load_projectile( speed, lifetime_ms, volume, + sight_convergence, autonomous_fire_distance, }) } |
