diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 14:37:59 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 14:37:59 +0300 |
| commit | f03b144907be8ad8cec2e1c7487b5d9e0a43f13e (patch) | |
| tree | 219582ffa8178775ead715b45f9cdee6d578a4a1 /crates/fparkan-runtime/src/projectile_shield.rs | |
| parent | 06df070ba0d7bd2435b8b1313019278090889d09 (diff) | |
| download | fparkan-f03b144907be8ad8cec2e1c7487b5d9e0a43f13e.tar.xz fparkan-f03b144907be8ad8cec2e1c7487b5d9e0a43f13e.zip | |
fix(combat): continue projectile flight after piercing shields
Resolve shield sphere entrances before the first physical hit. Carry leftover volume into later ticks for both player and autonomous fire and let a geometric miss pass through a warbot bounding sphere.
Checked Control.dll RVAs 0x1e9f0 and 0xd0c0 in-process with a sphere-only contact. The AutoDemo corpus covers a charged stop, a pierced field, and an unchanged hull on a miss. Ran the workspace suite and AutoDemo in the game window with Vulkan validation.
Diffstat (limited to 'crates/fparkan-runtime/src/projectile_shield.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/projectile_shield.rs | 227 |
1 files changed, 207 insertions, 20 deletions
diff --git a/crates/fparkan-runtime/src/projectile_shield.rs b/crates/fparkan-runtime/src/projectile_shield.rs index 541a6cb0..91715cc1 100644 --- a/crates/fparkan-runtime/src/projectile_shield.rs +++ b/crates/fparkan-runtime/src/projectile_shield.rs @@ -2,8 +2,11 @@ use crate::{ control_device_budget_coefficient, control_device_selector_zero, fixed_words_ascii_c_string, - projectile::unit_sphere, turret_aim::hull_local_point, weapon_pose::WeaponPoses, Engine, - MissionAssets, MissionControlDeviceTableStatus, MissionObjectDraft, ProjectileContact, + projectile::{segment_point, segment_sphere_entrance, unit_sphere}, + turret_aim::hull_local_point, + weapon_pose::WeaponPoses, + Engine, LoadedMissionState, MissionAssets, MissionControlDeviceTableStatus, MissionObjectDraft, + ProjectileContact, }; use fparkan_render::LegacyIron3dEulerTransform; use fparkan_world::{transform_state, TransformState}; @@ -18,6 +21,61 @@ pub struct ShieldImpact { pub world_from_effect: [[f32; 4]; 4], } +/// Sphere entrances before the first physical collision, in travel order. +pub(super) fn shield_crossings( + engine: &Engine, + shooter: crate::ObjectHandle, + segment: [[f32; 3]; 2], + stop: Option<f32>, +) -> Vec<ProjectileContact> { + let Some(mission) = &engine.loaded else { + return Vec::new(); + }; + let passenger = mission.boarding.map(|boarding| boarding.hero.handle); + let mut crossings: Vec<_> = mission + .control_device_tables + .iter() + .filter_map(|table| { + if table.handle == shooter || passenger == Some(table.handle) { + return None; + } + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + return None; + }; + if !devices.iter().any(|device| device.factory_tag == 9) { + return None; + } + let pose = transform_state(&engine.world, table.handle).ok()?; + let index = table.original_id?.0 as usize; + let (center, radius) = contact_sphere(mission, index, pose)?; + let fraction = segment_sphere_entrance(segment, center, radius)?; + stop.is_none_or(|limit| fraction < limit) + .then(|| ProjectileContact { + handle: table.handle, + fraction, + position: segment_point(segment, fraction), + node: None, + }) + }) + .collect(); + crossings.sort_by(|a, b| a.fraction.total_cmp(&b.fraction)); + crossings +} + +fn contact_sphere( + mission: &LoadedMissionState, + index: usize, + pose: TransformState, +) -> Option<([f32; 3], f32)> { + mission + .shield_spheres + .get(index) + .copied() + .flatten() + .and_then(|sphere| world_sphere(sphere, pose)) + .or_else(|| unit_sphere(&mission.mission_assets, index, pose)) +} + pub(super) fn prepare_shield_spheres( assets: &MissionAssets, drafts: &[MissionObjectDraft], @@ -92,33 +150,23 @@ pub(super) fn absorb(engine: &mut Engine, hit: ProjectileContact, volume: f32) - let Some(mission) = engine.loaded.as_mut() else { return volume; }; - let Some(table) = mission + let Some(table_index) = mission .control_device_tables - .iter_mut() - .find(|table| table.handle == hit.handle) + .iter() + .position(|table| table.handle == hit.handle) else { return volume; }; - let MissionControlDeviceTableStatus::Available(devices) = &mut table.status else { - return volume; - }; - let Some(strength) = devices.iter().rfind(|device| device.factory_tag == 21) else { - return volume; - }; - let Some(index) = table.original_id.map(|id| id.0 as usize) else { + let Some(index) = mission.control_device_tables[table_index] + .original_id + .map(|id| id.0 as usize) + else { return volume; }; let Ok(pose) = transform_state(&engine.world, hit.handle) else { return volume; }; - let Some((center, radius)) = mission - .shield_spheres - .get(index) - .copied() - .flatten() - .and_then(|sphere| world_sphere(sphere, pose)) - .or_else(|| unit_sphere(&mission.mission_assets, index, pose)) - else { + let Some((center, radius)) = contact_sphere(mission, index, pose) else { return volume; }; let shield_pose = TransformState { @@ -129,6 +177,14 @@ pub(super) fn absorb(engine: &mut Engine, hit: ProjectileContact, volume: f32) - let Some(sector) = hull_local_point(shield_pose, hit.position).and_then(contact_sector) else { return volume; }; + let MissionControlDeviceTableStatus::Available(devices) = + &mut mission.control_device_tables[table_index].status + else { + return volume; + }; + let Some(strength) = devices.iter().rfind(|device| device.factory_tag == 21) else { + return volume; + }; let Ok(multiplier) = control_device_budget_coefficient(strength, sector) else { return volume; }; @@ -271,6 +327,137 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] + fn licensed_autodemo_grazing_shot_can_pierce_a_field_and_miss_the_body() { + use crate::{ + create, load_gameplay_mission, projectile::segment_sphere_fraction, + projectile_terrain_hit, projectile_unit_hit, EngineServices, MissionMode, + MissionRequest, MissionStartupInput, PlayerProjectile, ProjectileFlightHit, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{handle_by_original_id, OriginalObjectId}; + use std::sync::Arc; + + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Autodemo.00/data.tma".into(), + mode: MissionMode::AutoDemo, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let target = handle_by_original_id(&engine.world, OriginalObjectId(0)).unwrap(); + let shooter = handle_by_original_id(&engine.world, OriginalObjectId(1)).unwrap(); + let pose = transform_state(&engine.world, target).unwrap(); + let mission = engine.loaded.as_ref().unwrap(); + let table = mission + .control_device_tables + .iter() + .find(|table| table.handle == target) + .unwrap(); + let health_before = table.node_provider_amount_bits; + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + panic!("stock shield and resistance devices"); + }; + let shield = devices + .iter() + .rfind(|device| device.factory_tag == 9) + .unwrap(); + let resistance = devices + .iter() + .rfind(|device| device.factory_tag == 21) + .unwrap(); + let (center, radius) = contact_sphere(mission, 0, pose).unwrap(); + let (hull_center, hull_radius) = unit_sphere(&mission.mission_assets, 0, pose).unwrap(); + let (segment, crossing) = (0..2) + .flat_map(|axis| (-10..=10).flat_map(move |a| (-10..=10).map(move |b| (axis, a, b)))) + .find_map(|(axis, a, b)| { + let mut line = center; + line[1 - axis] += a as f32 * radius / 10.0; + line[2] += b as f32 * radius / 10.0; + let mut segment = [line; 2]; + segment[0][axis] -= radius * 2.0; + segment[1][axis] += radius * 2.0; + segment_sphere_fraction(segment, hull_center, hull_radius)?; + let crossings = shield_crossings(&engine, shooter, segment, None); + let [crossing] = crossings.as_slice() else { + return None; + }; + (crossing.handle == target + && crossing.fraction < 0.5 + && projectile_unit_hit(&engine, shooter, segment).is_none() + && projectile_terrain_hit(&mission.terrain, segment) + .ok()? + .is_none()) + .then_some((segment, *crossing)) + }) + .expect( + "a stock shot crosses the field and chassis sphere while missing all triangles", + ); + let shield_pose = TransformState { + position: center.map(f32::to_bits), + scale: [1.0_f32.to_bits(); 3], + ..pose + }; + let sector = + contact_sector(hull_local_point(shield_pose, crossing.position).unwrap()).unwrap(); + let capacity = control_device_selector_zero(shield).unwrap() + * control_device_budget_coefficient(resistance, sector).unwrap(); + assert!(capacity > 0.0); + let direction = std::array::from_fn(|i| segment[1][i] - segment[0][i]); + let speed = direction + .into_iter() + .map(|axis| axis * axis) + .sum::<f32>() + .sqrt(); + let launch = |volume| { + PlayerProjectile::new(segment[0], direction, speed, 2_000, 0) + .unwrap() + .with_volume(Some(volume)) + }; + collect_shield_impacts(&mut engine); + + let stopped = launch(capacity * 0.5) + .advance_in_world(&mut engine, shooter, 500) + .unwrap(); + assert!(matches!(stopped.hit, Some(ProjectileFlightHit::Shield))); + assert!(!stopped.flying); + assert!(stopped.segment.unwrap()[1] + .into_iter() + .zip(crossing.position) + .all(|(actual, expected)| (actual - expected).abs() < 0.001)); + assert_eq!(take_shield_impacts(&mut engine).len(), 1); + + let mut flight = launch(capacity * 0.5 + 20.0); + let pierced = flight.advance_in_world(&mut engine, shooter, 500).unwrap(); + assert!(pierced.flying && pierced.hit.is_none()); + assert!((pierced.volume.unwrap() - 20.0).abs() < 0.001); + assert_eq!(take_shield_impacts(&mut engine).len(), 1); + let tail = flight + .advance_in_world(&mut engine, shooter, 1_000) + .unwrap(); + assert!(tail.flying && tail.hit.is_none()); + assert_eq!(tail.volume, pierced.volume); + assert!(take_shield_impacts(&mut engine).is_empty()); + let table = engine + .loaded + .as_ref() + .unwrap() + .control_device_tables + .iter() + .find(|table| table.handle == target) + .unwrap(); + assert_eq!(table.node_provider_amount_bits, health_before); + } + + #[test] + #[ignore = "requires licensed corpus"] fn licensed_autodemo_shield_blocks_and_then_exposes_one_sector() { use crate::{ apply_projectile_impact, create, load_gameplay_mission, EngineServices, MissionMode, |
