diff options
Diffstat (limited to 'crates/fparkan-runtime/src/projectile.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/projectile.rs | 130 |
1 files changed, 122 insertions, 8 deletions
diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs index 5359fc42..b5da26b8 100644 --- a/crates/fparkan-runtime/src/projectile.rs +++ b/crates/fparkan-runtime/src/projectile.rs @@ -194,6 +194,8 @@ pub enum ProjectileNode { Building(u32), /// A node of the warbot's chassis model. Chassis(u32), + /// A provider index in the assembled warbot's global Control table. + Mounted(u32), /// An indestructible communicator field. CaptureBarrier, } @@ -215,6 +217,17 @@ pub fn projectile_unit_hit( shooter: ObjectHandle, segment: [[f32; 3]; 2], ) -> Option<ProjectileContact> { + projectile_unit_hit_with_poses(engine, shooter, segment, |index| { + engine.autodemo_combat.as_ref()?.poses_for_collision(index) + }) +} + +pub(super) fn projectile_unit_hit_with_poses<'a>( + engine: &Engine, + shooter: ObjectHandle, + segment: [[f32; 3]; 2], + poses: impl Fn(usize) -> Option<&'a super::weapon_pose::WeaponPoses>, +) -> Option<ProjectileContact> { let mission = engine.loaded.as_ref()?; let passenger = mission.boarding.map(|boarding| boarding.hero.handle); mission @@ -234,8 +247,9 @@ pub fn projectile_unit_hit( } let state = transform_state(&engine.world, handle).ok()?; let (center, radius) = unit_sphere(&mission.mission_assets, index, state)?; - let sphere_fraction = segment_sphere_fraction(segment, center, radius)?; + let sphere_fraction = segment_sphere_fraction(segment, center, radius); let (fraction, node) = if building { + sphere_fraction?; let (fraction, node) = crate::capture_barrier::projectile_contact( mission, index, handle, state, segment, )?; @@ -249,9 +263,10 @@ pub fn projectile_unit_hit( segment, (center, radius), sphere_fraction, + poses(index), )? } else { - (sphere_fraction, None) + (sphere_fraction?, None) }; Some(ProjectileContact { handle, @@ -319,7 +334,11 @@ pub fn apply_projectile_impact( let key = match contact.node { Some(ProjectileNode::CaptureBarrier) => return Ok(ProjectileImpact::Unresolved), None | Some(ProjectileNode::Chassis(0)) => None, - Some(ProjectileNode::Chassis(key) | ProjectileNode::Building(key)) => Some(key), + Some( + ProjectileNode::Chassis(key) + | ProjectileNode::Mounted(key) + | ProjectileNode::Building(key), + ) => Some(key), }; let unabsorbed = super::projectile_shield::absorb(engine, contact, volume); if volume > 0.0 && unabsorbed == 0.0 { @@ -365,10 +384,11 @@ fn apply_contact_damage( { return Ok(None); } - if volume >= remaining - && matches!(contact.node, Some(ProjectileNode::Chassis(_))) - && row.current_flags.is_some_and(|flags| flags & 1 != 0) - { + let warbot_part = matches!( + contact.node, + Some(ProjectileNode::Chassis(_) | ProjectileNode::Mounted(_)) + ); + if volume >= remaining && warbot_part && row.current_flags.is_some_and(|flags| flags & 1 != 0) { super::destruction::destroy(engine, contact.handle)?; return Ok(Some((0.0, true))); } @@ -380,7 +400,7 @@ fn apply_contact_damage( if updated.is_none() { return Ok(None); } - if volume >= remaining && matches!(contact.node, Some(ProjectileNode::Chassis(_))) { + if volume >= remaining && warbot_part { let table = engine .loaded .as_mut() @@ -467,6 +487,100 @@ mod tests { #[test] #[ignore = "requires licensed corpus"] + fn licensed_mounted_node_uses_the_stock_warbot_pose_and_provider() { + use crate::{ + collect_unit_destruction_effects, create, frame, load_gameplay_mission, + take_unit_destruction_effects, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::OriginalObjectId; + + 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(); + frame(&mut engine, 0).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 hull_before = crate::control_node_provider_row(table, 0) + .unwrap() + .current_amount_bits; + + let (center, radius) = unit_sphere(&mission.mission_assets, 0, pose).unwrap(); + let hit = (0..3) + .flat_map(|axis| (-9..=9).flat_map(move |a| (-9..=9).map(move |b| (axis, a, b)))) + .find_map(|(axis, a, b)| { + let mut center = center; + center[(axis + 1) % 3] += a as f32 * radius / 8.0; + center[(axis + 2) % 3] += b as f32 * radius / 8.0; + let mut segment = [center; 2]; + segment[0][axis] -= radius * 2.0; + segment[1][axis] += radius * 2.0; + let hit = projectile_unit_hit(&engine, shooter, segment)?; + let Some(ProjectileNode::Mounted(key)) = hit.node else { + return None; + }; + let row = crate::control_node_provider_row(table, key).ok()?; + (hit.handle == target + && row.current_flags == Some(0) + && row.source_row.effect.name[0] != 0) + .then_some((segment, hit, row.unit_component_index, row.source_row_index)) + }) + .expect("a ray hits a mounted part on the stock AutoDemo warbot"); + let (segment, contact, component_index, local_node) = hit; + collect_unit_destruction_effects(&mut engine); + assert_eq!( + apply_projectile_contact(&mut engine, contact, f32::MAX).unwrap(), + Some((0.0, false)) + ); + let table = engine + .loaded + .as_ref() + .unwrap() + .control_device_tables + .iter() + .find(|table| table.handle == target) + .unwrap(); + assert_eq!( + crate::control_node_provider_row(table, 0) + .unwrap() + .current_amount_bits, + hull_before + ); + assert!(crate::depleted_warbot_nodes(&engine) + .any(|node| node == (0, component_index, local_node))); + assert_ne!( + projectile_unit_hit(&engine, shooter, segment).and_then(|hit| hit.node), + contact.node + ); + assert!( + matches!(take_unit_destruction_effects(&mut engine).as_slice(), + [crate::UnitDestructionEffect { mesh: Some(crate::DestroyedMesh::ChassisNode { component_index: component, node, .. }), .. }] + if *component == component_index && *node == local_node) + ); + } + + #[test] + #[ignore = "requires licensed corpus"] fn licensed_chassis_node_takes_damage_without_destroying_the_warbot() { use crate::{ collect_unit_destruction_effects, create, load_gameplay_mission, starting_cockpit, |
