From 7f2f07669898e66b2391cfd36dcd7a9ea21bd90f Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 09:08:32 +0400 Subject: feat(combat): reduce damage with Control protection coefficients Keep the two damage coefficients in the existing tag-27 owner cache and apply the native quadratic clamp to projectile contacts. The calculation matches live GOG damage samples at single precision, and the AutoDemo window reaches combat and replacement after the change. --- crates/fparkan-runtime/src/lib.rs | 24 +++++-- crates/fparkan-runtime/src/projectile.rs | 20 +++--- crates/fparkan-runtime/src/projectile_damage.rs | 83 +++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 13 deletions(-) create mode 100644 crates/fparkan-runtime/src/projectile_damage.rs (limited to 'crates') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 4efbc365..e54c81bb 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -101,6 +101,7 @@ mod player_status; mod player_weapon; mod production; mod projectile; +mod projectile_damage; mod repair_zone; mod research; mod resource_distribution; @@ -851,6 +852,8 @@ pub struct MissionControlDeviceTableState { pub native_control_class: Option, /// Raw CTLD profile retained by native component ID zero and its per-owner copy. pub owner_profile: Option, + /// Linear and quadratic damage coefficients from the owner's tag 27 cache. + pub projectile_resistance_bits: Option<[u32; 2]>, /// Global NDP provider rows used by indexed Control selectors. pub node_provider_status: MissionControlNodeProviderTableStatus, /// Native `+0x58c`: total provider capacity in the supported unit-scale domain. @@ -989,6 +992,7 @@ pub enum MissionControlOwnerProfileFca0UnavailableReason { struct ControlDeviceProjection { devices: Vec, owner_profile: Option, + projectile_resistance_bits: Option<[u32; 2]>, node_provider_status: MissionControlNodeProviderTableStatus, } @@ -2397,6 +2401,7 @@ fn load_mission_with_options( status: MissionControlDeviceTableStatus::Available(projection.devices), native_control_class, owner_profile: projection.owner_profile, + projectile_resistance_bits: projection.projectile_resistance_bits, node_provider_maximum_amount_bits: initial_node_provider_maximum_amount( &projection.node_provider_status, ), @@ -2414,6 +2419,7 @@ node_provider_distribution_suspended: false, status: MissionControlDeviceTableStatus::Unavailable(reason.clone()), native_control_class, owner_profile: None, + projectile_resistance_bits: None, node_provider_maximum_amount_bits: None, node_provider_amount_bits: None, node_provider_dirty: false, @@ -6984,24 +6990,27 @@ fn native_control_factory_byte_94(factory_tag: u32) -> Option { (factory_tag == 15).then_some(0) } +type ControlProfileCache27 = + Result, MissionControlOwnerProfileLoadProjectionUnavailableReason>; + fn initialize_control_profile_cache27( - cache: &mut Result, MissionControlOwnerProfileLoadProjectionUnavailableReason>, + cache: &mut ControlProfileCache27, descriptor: &PreparedControlTypedRecord, ) { if descriptor.tag == 27 && cache.is_ok() { - *cache = Ok(Some(descriptor.fixed_words[11])); + *cache = Ok(Some([11, 12, 13].map(|word| descriptor.fixed_words[word]))); } } fn update_control_profile_cache27_on_into( - cache: &mut Result, MissionControlOwnerProfileLoadProjectionUnavailableReason>, + cache: &mut ControlProfileCache27, descriptor: &PreparedControlTypedRecord, ) { if descriptor.tag != 27 { return; } match cache { - Ok(Some(value)) => *value = descriptor.fixed_words[11], + Ok(Some(words)) => *words = [11, 12, 13].map(|word| descriptor.fixed_words[word]), Ok(None) => *cache = Err( MissionControlOwnerProfileLoadProjectionUnavailableReason::MissingProfileCacheOnInto, ), @@ -7471,7 +7480,7 @@ fn build_control_device_projection_with_class( .then_some(global_control_profile_slots.as_slice()), &global_mesh_component_ids, CONTROL_LOAD_FAC0_SCALE_BITS, - profile_cache27_bits, + profile_cache27_bits.clone().map(|cache| cache.map(|words| words[0])), ) } MissionControlNodeProviderTableStatus::Unavailable(reason) => Err( @@ -7509,6 +7518,7 @@ fn build_control_device_projection_with_class( Ok(ControlDeviceProjection { devices, owner_profile, + projectile_resistance_bits: profile_cache27_bits.ok().flatten().map(|[_, a, b]| [a, b]), node_provider_status, }) } @@ -9654,6 +9664,7 @@ mod tests { status, native_control_class: None, owner_profile: None, + projectile_resistance_bits: None, node_provider_maximum_amount_bits: initial_node_provider_maximum_amount( &node_provider_status, ), @@ -17684,6 +17695,7 @@ mod tests { status: MissionControlDeviceTableStatus::Available(vec![device]), native_control_class: None, owner_profile: None, + projectile_resistance_bits: None, node_provider_status: MissionControlNodeProviderTableStatus::Available(Vec::new()), node_provider_maximum_amount_bits: Some(0), node_provider_amount_bits: Some(0), @@ -17797,6 +17809,7 @@ mod tests { ]), native_control_class: None, owner_profile: None, + projectile_resistance_bits: None, node_provider_status: MissionControlNodeProviderTableStatus::Available(vec![ synthetic_node_provider_row(0, 0, 1.0_f32.to_bits()), ]), @@ -17854,6 +17867,7 @@ mod tests { status: MissionControlDeviceTableStatus::Available(Vec::new()), native_control_class: None, owner_profile: None, + projectile_resistance_bits: None, node_provider_status: MissionControlNodeProviderTableStatus::Available(Vec::new()), node_provider_maximum_amount_bits: Some(0), node_provider_amount_bits: Some(0), diff --git a/crates/fparkan-runtime/src/projectile.rs b/crates/fparkan-runtime/src/projectile.rs index 3e9b7088..0767ee4e 100644 --- a/crates/fparkan-runtime/src/projectile.rs +++ b/crates/fparkan-runtime/src/projectile.rs @@ -312,16 +312,20 @@ pub fn apply_projectile_contact( contact: ProjectileContact, volume: f32, ) -> Result, ControlResourceAccessError> { - let Some(key) = contact.building_node else { - return apply_projectile_hit(engine, contact.handle, volume); - }; - let row = engine.loaded.as_ref().and_then(|mission| { - let table = mission + let table = engine.loaded.as_ref().and_then(|mission| { + mission .control_device_tables .iter() - .find(|table| table.handle == contact.handle)?; - super::control_node_provider_row(table, key).ok() + .find(|table| table.handle == contact.handle) }); + let volume = super::projectile_damage::resisted_volume( + volume, + table.and_then(|table| table.projectile_resistance_bits), + ); + let Some(key) = contact.building_node else { + return apply_projectile_hit(engine, contact.handle, volume); + }; + let row = table.and_then(|table| super::control_node_provider_row(table, key).ok()); let Some(row) = row else { return Ok(None); }; @@ -334,7 +338,7 @@ pub fn apply_projectile_contact( { return Ok(None); } - // shortcut: keep per-node damage using the projectile NDP volume while armor and splash are open. + // shortcut: feed the projectile NDP volume into the native reduction while impact falloff is open. let effect = (volume >= remaining && engine.destruction_effects.is_some()) .then(|| super::destruction::building_part_effect(row, contact.position)) .flatten(); diff --git a/crates/fparkan-runtime/src/projectile_damage.rs b/crates/fparkan-runtime/src/projectile_damage.rs new file mode 100644 index 00000000..889123bb --- /dev/null +++ b/crates/fparkan-runtime/src/projectile_damage.rs @@ -0,0 +1,83 @@ +//! Damage reduction from the target's authored Control protection coefficients. + +pub(super) fn resisted_volume(incoming: f32, coefficients: Option<[u32; 2]>) -> f32 { + let Some([linear, quadratic]) = coefficients.map(|bits| bits.map(f32::from_bits)) else { + return incoming; + }; + if !incoming.is_finite() || incoming <= 0.0 || !linear.is_finite() || !quadratic.is_finite() { + return incoming; + } + // Control.dll+0x10218..0x1026c evaluates these operations at 24-bit precision. + ((incoming * quadratic + linear) * incoming).clamp(0.0, incoming) +} + +#[cfg(test)] +mod tests { + use super::resisted_volume; + + #[test] + fn protection_matches_live_control_damage() { + let coefficients = Some([0x3eab_af10, 0x3966_afcd]); + for (incoming, expected) in [(0x436b_0000, 0x42b5_e63f), (0x435a_aae4, 0x42a7_af9e)] { + assert_eq!( + resisted_volume(f32::from_bits(incoming), coefficients).to_bits(), + expected + ); + } + assert_eq!(resisted_volume(390.0, None), 390.0); + assert_eq!(resisted_volume(390.0, Some([0, 0])), 0.0); + assert_eq!(resisted_volume(390.0, Some([2.0_f32.to_bits(), 0])), 390.0); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_autodemo_contact_uses_the_loaded_protection() { + use crate::{ + apply_projectile_contact, control_node_provider_row, create, load_gameplay_mission, + loaded_mission_control_device_tables, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, ProjectileContact, + }; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::transform_state; + 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 table = loaded_mission_control_device_tables(&engine) + .unwrap() + .iter() + .find(|table| { + table.projectile_resistance_bits == Some([0x3eab_af10, 0x3966_afcd]) + && transform_state(&engine.world, table.handle).is_ok() + }) + .expect("the stock warbot with the protection measured in Control"); + let root = control_node_provider_row(table, 0).unwrap(); + let remaining = f32::from_bits(root.current_amount_bits) - f32::from_bits(0x42b5_e63f); + let contact = ProjectileContact { + handle: table.handle, + fraction: 0.5, + position: transform_state(&engine.world, table.handle) + .unwrap() + .position + .map(f32::from_bits), + building_node: None, + }; + assert_eq!( + apply_projectile_contact(&mut engine, contact, 235.0).unwrap(), + Some((remaining, false)) + ); + } +} -- cgit v1.2.3