From 364f8a0984e5cd3b65e81f24e95fcf8cd92aec2d Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 07:34:52 +0400 Subject: fix(combat): apply critical damage on the Control resource tick Use the alarm entry amount for sequential wear and disable the onboard repair device before recomputing motion limits. Match the Control.dll alarm and recovery vector. In the stock AutoDemo the affected warbot now loses its remaining movement instead of keeping its last limit. Alarm effects and the full native node cascade remain approximate. --- crates/fparkan-runtime/src/control_profile.rs | 325 +++++++++++++++++++------- 1 file changed, 243 insertions(+), 82 deletions(-) (limited to 'crates/fparkan-runtime/src/control_profile.rs') diff --git a/crates/fparkan-runtime/src/control_profile.rs b/crates/fparkan-runtime/src/control_profile.rs index 8fda8937..236d523b 100644 --- a/crates/fparkan-runtime/src/control_profile.rs +++ b/crates/fparkan-runtime/src/control_profile.rs @@ -2,10 +2,11 @@ use crate::{ control_fca0_clamp_to_profile_cap, control_fca0_float, control_fca0_product, - control_fca0_ratio, control_fca0_sum, query_control_device_selector, EffectiveControlDevice, - MissionControlClassProjection, MissionControlDeviceTableState, MissionControlDeviceTableStatus, - MissionControlNodeProviderTableStatus, MissionControlOwnerProfileFca0UnavailableReason, - MissionControlOwnerProfileLoadProjection, + control_fca0_ratio, control_fca0_sum, node_amount::distribute_control_node_amount_rows, + query_control_device_selector, ControlNodeDistributionMode, ControlResourceAccessError, + EffectiveControlDevice, MissionControlClassProjection, MissionControlDeviceTableState, + MissionControlDeviceTableStatus, MissionControlNodeProviderTableStatus, + MissionControlOwnerProfileFca0UnavailableReason, MissionControlOwnerProfileLoadProjection, MissionControlOwnerProfileLoadProjectionUnavailableReason, }; use std::{collections::BTreeMap, sync::Arc}; @@ -59,56 +60,87 @@ pub(super) fn project_initial( ) } -fn node_means( - mut scalars: ControlProfileScalars, - rows: impl IntoIterator, - low_total_amount: bool, -) -> Result { - let mut sums = [0.0_f32; 3]; - let mut counts = [0.0_f32; 3]; - for (flags, value) in rows { - if flags & 0x70 == 0 { - continue; - } - if !value.is_finite() || !(0.0..=1.0).contains(&value) { - return Err("node_ratio"); - } - for (group, mask) in [0x70, 0x20, 0x40].into_iter().enumerate() { - if flags & mask != 0 { - sums[group] += value; - counts[group] += 1.0; +#[derive(Default)] +pub(super) struct DamageAlarm { + threshold: Option, + active: bool, +} + +#[derive(Default)] +struct NodeHealth { + sums: [f32; 3], + counts: [f32; 3], +} + +impl NodeHealth { + fn read(rows: impl IntoIterator) -> Result { + let mut health = Self::default(); + for (flags, value) in rows { + if flags & 0x70 == 0 { + continue; + } + if !value.is_finite() || !(0.0..=1.0).contains(&value) { + return Err("node_ratio"); + } + for (group, mask) in [0x70, 0x20, 0x40].into_iter().enumerate() { + if flags & mask != 0 { + health.sums[group] += value; + health.counts[group] += 1.0; + } } } + Ok(health) } - // Control+0x12a40 enters its damage/alarm path at this boundary. - if counts[0] > 1.0 { - if sums[0] <= counts[0] * 0.5 - 1.0 { - return Err("critical_nodes_alarm"); - } - } else if low_total_amount { - return Err("provider_amount_alarm"); + + fn from_provider(table: &MissionControlDeviceTableState) -> Result { + let MissionControlNodeProviderTableStatus::Available(rows) = &table.node_provider_status + else { + return Err("provider_table"); + }; + Self::read(rows.iter().map(|row| { + ( + row.source_row.raw_flags, + f32::from_bits(row.current_type1_value_bits), + ) + })) } - if counts[1] > 0.0 { - scalars.mean_a_bits = (sums[1] / counts[1]).to_bits(); + + fn triggers_alarm(&self, low_total_amount: bool) -> bool { + if self.counts[0] > 1.0 { + self.sums[0] <= self.counts[0] * 0.5 - 1.0 + } else { + low_total_amount + } } - if counts[2] > 0.0 { - scalars.mean_b_bits = (sums[2] / counts[2]).to_bits(); + + fn update_means(&self, mut scalars: ControlProfileScalars) -> ControlProfileScalars { + if self.counts[1] > 0.0 { + scalars.mean_a_bits = (self.sums[1] / self.counts[1]).to_bits(); + } + if self.counts[2] > 0.0 { + scalars.mean_b_bits = (self.sums[2] / self.counts[2]).to_bits(); + } + scalars } - Ok(scalars) } -/// The non-alarm dirty branch of Control+0x12a40, before the budget pass. -pub(super) fn refresh(table: &mut MissionControlDeviceTableState) -> Result { +#[derive(Debug, PartialEq, Eq)] +pub(super) enum Refresh { + Clean, + Published, + Depleted, +} + +/// The dirty branch of Control+0x12a40, before the budget pass. +pub(super) fn refresh( + table: &mut MissionControlDeviceTableState, + alarm: &mut DamageAlarm, + seconds: f32, +) -> Result { if !table.node_provider_dirty { - return Ok(false); + return Ok(Refresh::Clean); } - let profile = table.owner_profile.as_ref().ok_or("owner_profile")?; - let rows = match &table.node_provider_status { - MissionControlNodeProviderTableStatus::Available(rows) => rows, - MissionControlNodeProviderTableStatus::Unavailable(_) => { - return Err("provider_table".into()) - } - }; + table.owner_profile.as_ref().ok_or("owner_profile")?; let amount = table .node_provider_amount_bits .map(f32::from_bits) @@ -118,20 +150,52 @@ pub(super) fn refresh(table: &mut MissionControlDeviceTableState) -> Result *table = updated, + Err(ControlResourceAccessError::UnsupportedNodeLifecycle { .. }) => { + return Ok(Refresh::Depleted); + } + Err(reason) => { + table.node_provider_dirty = true; + return Err(format!("alarm_damage_{reason:?}")); + } + } + } + if let MissionControlDeviceTableStatus::Available(devices) = &mut table.status { + for device in devices + .iter_mut() + .filter(|device| device.descriptor.tag == 15) + { + device.native_word_50_bits = Some(0); + } + } + health = NodeHealth::from_provider(table)?; + } + let profile = table.owner_profile.as_ref().expect("checked profile"); + let scalars = health.update_means(profile.scalars); let devices = match &table.status { MissionControlDeviceTableStatus::Available(devices) => devices, MissionControlDeviceTableStatus::Unavailable(_) => return Err("device_table".into()), @@ -149,8 +213,7 @@ pub(super) fn refresh(table: &mut MissionControlDeviceTableState) -> Result1 critical - // rows pass their own health test: 10 total / 100 max still updates. - assert!(node_means( - ControlProfileScalars::default(), - [(0x20, 1.0), (0x40, 1.0)], - true - ) - .is_ok()); + assert!(NodeHealth::read([(0x20, 0.0), (0x40, 0.0)]) + .unwrap() + .triggers_alarm(false)); + assert!(!NodeHealth::read([(0x20, 1.0), (0x40, 1.0)]) + .unwrap() + .triggers_alarm(true)); + assert!(NodeHealth::read([(0x20, 1.0)]) + .unwrap() + .triggers_alarm(true)); + } + + #[test] + fn alarm_keeps_its_entry_amount_and_disables_internal_repair() { + use crate::{ + tests::{ + synthetic_control_device_table_with_providers, synthetic_effective_control_device, + synthetic_node_provider_row, + }, + MissionControlClassSource, MissionControlNodeMeshState, MissionControlOwnerProfile, + }; + + // Control.dll+0x12a40, x87 CW 0x007f. Two depleted critical rows. + let rows: Vec<_> = [(0, 60.0_f32), (0x20, 0.0), (0x40, 0.0), (0, 20.0)] + .into_iter() + .enumerate() + .map(|(index, (flags, amount))| { + let ratio = (amount / 100.0).to_bits(); + let mut row = synthetic_node_provider_row(0, index, ratio); + row.source_row.raw_flags = flags; + row.initial_amount_bits = amount.to_bits(); + row.current_amount_bits = amount.to_bits(); + row.current_flags = Some(if amount == 0.0 { 0x10 } else { 0 }); + row.mesh_state = Some(MissionControlNodeMeshState { + type1_value_bits: ratio, + visible_stage: 0, + }); + row + }) + .collect(); + let source = rows[0].source.clone(); + let mut drive = synthetic_effective_control_device(5, 5, 1.25_f32.to_bits(), None, None); + drive.key = 0; + let mut repair = synthetic_effective_control_device(15, 15, 0, None, None); + repair.native_word_50_bits = Some(0x20); + let mut table = synthetic_control_device_table_with_providers( + MissionControlDeviceTableStatus::Available(vec![drive, repair]), + MissionControlNodeProviderTableStatus::Available(rows), + ); + table.native_control_class = Some(MissionControlClassProjection { + class_id: 4, + source: MissionControlClassSource::RootPrototypeFourCc { source_type: 0 }, + }); + let mut words = [0; 27]; + words[6..9].copy_from_slice(&[10.0_f32, 20.0, 30.0].map(f32::to_bits)); + words[26] = 4.0_f32.to_bits(); + table.owner_profile = Some(MissionControlOwnerProfile { + source, + unit_component_index: 0, + native_component_id: 0, + original_words: words, + current_words: words, + scalars: ControlProfileScalars::default(), + load_projection: Ok(MissionControlOwnerProfileLoadProjection { + aggregate_530_bits: 0, + aggregate_534_bits: 0, + aggregate_538_bits: 0, + row_20_bits: vec![0; 4], + }), + fca0_projection: Ok(()), + }); + table.node_provider_dirty = true; + let mut alarm = DamageAlarm::default(); + for (seconds, amount, tail) in [(0.25, 78.0_f32, 18.0_f32), (0.5, 74.0, 14.0)] { + assert_eq!( + refresh(&mut table, &mut alarm, seconds).unwrap(), + Refresh::Published + ); + assert_eq!(alarm.threshold, Some(80.0)); + assert!(alarm.active && table.node_provider_dirty); + assert_eq!(table.node_provider_amount_bits, Some(amount.to_bits())); + assert_eq!( + crate::control_node_provider_row(&table, 3) + .unwrap() + .current_amount_bits, + tail.to_bits() + ); + assert_eq!(table.owner_profile.as_ref().unwrap().current_words[7], 0); + assert_eq!( + crate::control_budget_device(&table, 1) + .unwrap() + .native_word_50_bits, + Some(0) + ); + } + let mut doomed = table.clone(); + assert_eq!( + refresh(&mut doomed, &mut alarm, 10.0).unwrap(), + Refresh::Depleted + ); + + for key in [1, 2] { + crate::node_amount::apply_control_node_delta(&mut table, key, 30.0).unwrap(); + } + assert_eq!( + refresh(&mut table, &mut alarm, 0.25).unwrap(), + Refresh::Published + ); + assert!(!alarm.active && !table.node_provider_dirty); + assert_eq!(table.node_provider_amount_bits, Some(134.0_f32.to_bits())); + assert_eq!( + table.owner_profile.as_ref().unwrap().current_words[7], + 4.5_f32.to_bits() + ); assert_eq!( - node_means(ControlProfileScalars::default(), [(0x20, 1.0)], true), - Err("provider_amount_alarm") + crate::control_budget_device(&table, 1) + .unwrap() + .native_word_50_bits, + Some(0) ); } -- cgit v1.2.3