From 489244b8918b3ca933776d640f7fa2a0800dccad Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 17:57:11 +0400 Subject: fix(combat): aim at exposed unit hulls --- crates/fparkan-runtime/src/autodemo_combat.rs | 30 ++++++++++++++++++--------- crates/fparkan-runtime/src/lib.rs | 24 ++++++++++++++++----- crates/fparkan-runtime/src/projectile.rs | 2 +- 3 files changed, 40 insertions(+), 16 deletions(-) (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/autodemo_combat.rs b/crates/fparkan-runtime/src/autodemo_combat.rs index 5a864eb9..ecef3809 100644 --- a/crates/fparkan-runtime/src/autodemo_combat.rs +++ b/crates/fparkan-runtime/src/autodemo_combat.rs @@ -2,7 +2,7 @@ use super::{ apply_projectile_contact, autodemo::MOBILE_UNIT, player_weapon::prepare_unit_weapons, - projectile::{load_projectile, unit_sphere}, + projectile::{load_projectile, segment_sphere_fraction, unit_sphere}, projectile_terrain_hit, projectile_unit_hit, Engine, MissionAssets, MissionDocument, MissionObjectDraft, ObjectHandle, PlayerProjectile, PlayerWeapons, }; @@ -73,6 +73,7 @@ pub(super) struct AutoDemoCombat { pub(super) struct CombatTarget { pub(super) handle: ObjectHandle, pub(super) center: [f32; 3], + radius: f32, pub(super) in_range: bool, } @@ -230,27 +231,28 @@ impl AutoDemoCombat { .filter_map(|unit| { let owner = identity_metadata(world, unit.handle).ok()?.owner_id?; let state = transform_state(world, unit.handle).ok()?; - let (center, _) = unit_sphere(assets, unit.index, state)?; - Some((unit.handle, usize::from(owner.0), center)) + let (center, radius) = unit_sphere(assets, unit.index, state)?; + Some((unit.handle, usize::from(owner.0), center, radius)) }) .collect(); live.iter() - .filter_map(|&(handle, clan, from)| { - let (enemy, center, distance) = live + .filter_map(|&(handle, clan, from, _)| { + let (enemy, center, radius, distance) = live .iter() - .filter_map(|&(enemy, other, to)| { + .filter_map(|&(enemy, other, to, radius)| { let distance = (to[0] - from[0]).hypot(to[1] - from[1]); (enemy != handle && distance <= ATTACK_RANGE && self.hostile.get(clan)?.get(other) == Some(&true)) - .then_some((enemy, to, distance)) + .then_some((enemy, to, radius, distance)) }) - .min_by(|a, b| a.2.total_cmp(&b.2))?; + .min_by(|a, b| a.3.total_cmp(&b.3))?; Some(( handle, CombatTarget { handle: enemy, center, + radius, in_range: distance <= FIRE_RANGE, }, )) @@ -334,8 +336,16 @@ impl AutoDemoCombat { let state = transform_state(&engine.world, unit.handle).ok()?; let (from, _) = unit_sphere(&mission.mission_assets, unit.index, state)?; match projectile_terrain_hit(&mission.terrain, [from, target.center]) { - Ok(None) => Some((target.handle, from, target.center)), - Ok(Some(_)) => None, + Ok(ground) => { + // shortcut: use the same hull sphere as the projectile until per-node aiming is wired. + segment_sphere_fraction( + [from, target.center], + target.center, + target.radius, + ) + .filter(|&contact| ground.is_none_or(|ground| contact < ground)) + .map(|_| (target.handle, from, target.center)) + } Err(_) => { unavailable += 1; None diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index f7a81c7b..740de83a 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -18323,12 +18323,26 @@ mod tests { player_status(&engine, hero.handle).unwrap().health_percent, Some(75) ); - let enemy = mission_object_handle(&engine, OriginalObjectId(11)).unwrap(); - let enemy_pose = transform_state(&engine.world, enemy).unwrap(); - let [x, y, z] = enemy_pose.position.map(f32::from_bits); + // Poses from the stock-start walk into the battle. The hero's hull + // meets the ray before terrain, even though its center is underground. + let enemy = mission_object_handle(&engine, OriginalObjectId(14)).unwrap(); + let mut enemy_pose = transform_state(&engine.world, enemy).unwrap(); + enemy_pose.position = [1504.2733_f32, 915.2701, 67.87986].map(f32::to_bits); + set_transform(&mut engine.world, enemy, enemy_pose).unwrap(); let mut pose = transform_state(&engine.world, hero.handle).unwrap(); - pose.position = [x - 10.0, y, z + 18.0].map(f32::to_bits); + pose.position = [1504.273_f32, 917.2702, 64.927925].map(f32::to_bits); set_transform(&mut engine.world, hero.handle, pose).unwrap(); + let mission = engine.loaded.as_ref().unwrap(); + let (from, _) = projectile::unit_sphere(&mission.mission_assets, 14, enemy_pose).unwrap(); + let (to, _) = + projectile::unit_sphere(&mission.mission_assets, hero.object_index, pose).unwrap(); + let ray = [from, to]; + let ground = projectile_terrain_hit(&mission.terrain, ray) + .unwrap() + .unwrap(); + let contact = projectile_unit_hit(&engine, enemy, ray).unwrap(); + assert_eq!(contact.handle, hero.handle); + assert!(contact.fraction < ground); let mut shots = Vec::new(); let mut failures = 0; for now in (100..=6_000).step_by(50) { @@ -18343,7 +18357,7 @@ mod tests { }); } let assets = loaded_mission_assets(&engine).unwrap(); - let weapons = player_weapon::prepare_unit_weapons(&assets.object_component_controls[11]); + let weapons = player_weapon::prepare_unit_weapons(&assets.object_component_controls[14]); assert!( shots.iter().any(|event| matches!(event, AutoDemoCombatEvent::Shot { shooter, target, .. } diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs index 7e177a17..15ffce85 100644 --- a/crates/fparkan-runtime/src/projectile.rs +++ b/crates/fparkan-runtime/src/projectile.rs @@ -331,7 +331,7 @@ pub fn apply_projectile_contact( Ok(updated.map(|_| ((remaining - volume).max(0.0), false))) } -fn segment_sphere_fraction( +pub(super) fn segment_sphere_fraction( [start, end]: [[f32; 3]; 2], center: [f32; 3], radius: f32, -- cgit v1.2.3