aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-26 01:18:33 +0300
committerValentin Popov <valentin@popov.link>2026-11-26 01:18:33 +0300
commit65ec4949e90fa140d68563df7a38d08d9a1d49b0 (patch)
tree563cc61a364573aa5324ca0fdb815082f84a19bb /crates/fparkan-runtime/src
parent8a0f8a449dc3b80c5348c60b54982a71a45b0da2 (diff)
downloadfparkan-65ec4949e90fa140d68563df7a38d08d9a1d49b0.tar.xz
fparkan-65ec4949e90fa140d68563df7a38d08d9a1d49b0.zip
fix(control): apply static root and critical-node depletion
Native Control+0x10f30 marks root/critical depletion with +0x550=0xfffe. Class 3 then returns without death notifications, deletion timers or a root cascade. Preserve that predicate through repair, and permit the native already-marked path while retaining explicit errors for unsupported deaths. Move the shared amount update out of the large runtime module. Validation: native x86 DLL oracle at x87 CW 0x007f for root/critical depletion, repair and an already-marked mobile owner; Rust rule tests; licensed stock mission-19 raid; workspace fmt/check/tests and release build. All 29 stock 900-second reports pass with unchanged world/outcome summaries and no combat damage_error entries. The windowed stock raid depleted bunker node 0 at 329083 ms, with the hero left at the stock start throughout. Scope is Control amount/marker publication. Static structures stay in the world. Stage propagation, ruins, effects and finishing an attack against an exhausted static target remain open; projectile damage is still approximate. The affected mission-19 target is a bunker, not a factory.
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/lib.rs137
-rw-r--r--crates/fparkan-runtime/src/node_amount.rs256
2 files changed, 271 insertions, 122 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 6311683b..7b88abd6 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -56,10 +56,12 @@ pub use combat_report::{CombatObject, CombatOccurrences, CombatReport};
mod control_profile;
mod control_resources;
mod mobile_budget;
+mod node_amount;
pub use clan_clock::note_mission_pause_elapsed;
use control_profile::project_initial as project_control_owner_profile_fca0;
pub use control_profile::{ControlProfileReport, ControlProfileScalars};
pub use mobile_budget::MobileBudgetReport;
+use node_amount::{apply_control_node_delta, apply_control_node_distribution_delta};
mod clan_combat_groups;
mod clan_combat_strength;
mod clan_factory_schemes;
@@ -824,6 +826,8 @@ pub struct MissionControlDeviceTableState {
pub node_provider_amount_bits: Option<u32>,
/// Native `+0x598`: amount changed since the last aggregate/contact update.
pub node_provider_dirty: bool,
+ /// Native `+0x550 == 0xfffe`: root or critical-node depletion has been marked.
+ pub node_provider_depletion_marked: bool,
/// Native `+0x618`: distribution returns without changing rows while set.
pub node_provider_distribution_suspended: bool,
/// Native `+0x5dc`, last output rate written by the Control budget pass.
@@ -2361,7 +2365,8 @@ fn load_mission_with_options(
),
node_provider_amount_bits: initial_node_provider_amount(&projection.node_provider_status),
node_provider_dirty: true,
- node_provider_distribution_suspended: false,
+ node_provider_depletion_marked: false,
+node_provider_distribution_suspended: false,
current_output_rate_5dc_bits: 0,
current_output_rate_5e0_bits: 0,
node_provider_status: projection.node_provider_status,
@@ -2375,7 +2380,8 @@ fn load_mission_with_options(
node_provider_maximum_amount_bits: None,
node_provider_amount_bits: None,
node_provider_dirty: false,
- node_provider_distribution_suspended: false,
+ node_provider_depletion_marked: false,
+node_provider_distribution_suspended: false,
current_output_rate_5dc_bits: 0,
current_output_rate_5e0_bits: 0,
node_provider_status:
@@ -4113,6 +4119,7 @@ pub fn set_loaded_mission_control_multiplier(
/// It does not run stage propagation,
/// visibility, or the separate Control clock; the processed amount stays unchanged.
/// The loaded Unit DAT path has unity maximum multipliers.
+/// Static class-3 root/critical depletion also retains the native exhaustion marker.
///
/// # Errors
/// Rejects invalid handles, unavailable state, unsupported numeric ranges, or
@@ -5758,124 +5765,6 @@ fn adjust_control_class9_values(
Ok(())
}
-fn apply_control_node_delta(
- table: &mut MissionControlDeviceTableState,
- key: u32,
- delta: f32,
-) -> Result<Option<ControlNodeProviderPublication>, ControlResourceAccessError> {
- apply_control_node_delta_inner(table, key, delta, false)
-}
-
-fn apply_control_node_distribution_delta(
- table: &mut MissionControlDeviceTableState,
- key: u32,
- delta: f32,
-) -> Result<Option<ControlNodeProviderPublication>, ControlResourceAccessError> {
- apply_control_node_delta_inner(table, key, delta, true)
-}
-
-fn apply_control_node_delta_inner(
- table: &mut MissionControlDeviceTableState,
- key: u32,
- delta: f32,
- accumulate_delta: bool,
-) -> Result<Option<ControlNodeProviderPublication>, ControlResourceAccessError> {
- let rows = match &mut table.node_provider_status {
- MissionControlNodeProviderTableStatus::Available(rows) => rows,
- MissionControlNodeProviderTableStatus::Unavailable(reason) => {
- return Err(ControlResourceAccessError::NodeProviderTableUnavailable(
- reason.clone(),
- ))
- }
- };
- let row_count = rows.len();
- let row = usize::try_from(key)
- .ok()
- .and_then(|index| rows.get_mut(index))
- .ok_or(ControlResourceAccessError::NodeProviderKeyOutOfRange { key, row_count })?;
- checked_control_float(0, "provider delta", delta)?;
- if delta == 0.0 {
- return Ok(None);
- }
- let previous = checked_control_float(
- 0,
- "provider amount",
- f32::from_bits(row.current_amount_bits),
- )?;
- let maximum = checked_control_float(0, "provider maximum", row.source_row.max_volume)?;
- if maximum < 0.0 || previous < 0.0 || previous > maximum {
- return Err(ControlResourceAccessError::InvalidNodeProviderAmount { key });
- }
- let amount =
- checked_control_sum(0, "provider amount plus delta", previous, delta)?.clamp(0.0, maximum);
- if amount == previous {
- row.current_amount_bits = amount.to_bits();
- return Ok(None);
- }
- let flags = row
- .current_flags
- .ok_or(ControlResourceAccessError::UnsupportedField {
- factory_tag: 0,
- field: "provider flags",
- })?;
- if amount == 0.0 && (key == 0 || flags & 1 != 0) {
- return Err(ControlResourceAccessError::UnsupportedNodeLifecycle { key });
- }
- let total_bits =
- table
- .node_provider_amount_bits
- .ok_or(ControlResourceAccessError::UnsupportedField {
- factory_tag: 0,
- field: "provider aggregate",
- })?;
- let total = checked_control_float(0, "provider aggregate", f32::from_bits(total_bits))?;
- if total < 0.0 {
- return Err(ControlResourceAccessError::InvalidNodeProviderAmount { key });
- }
- let change = checked_control_sum(0, "provider amount change", amount, -previous)?;
- let total = checked_control_sum(0, "provider aggregate", total, change)?.max(0.0);
- let ratio = checked_control_ratio("provider ratio", amount, maximum)?;
- let accumulated_delta = if accumulate_delta {
- let previous = checked_control_float(
- 0,
- "provider accumulated delta",
- f32::from_bits(row.current_accumulated_delta_bits),
- )?;
- Some(checked_control_sum(
- 0,
- "provider accumulated delta",
- change,
- previous,
- )?)
- } else {
- None
- };
- let mesh = row
- .mesh_state
- .as_mut()
- .ok_or(ControlResourceAccessError::UnsupportedField {
- factory_tag: 0,
- field: "model node state",
- })?;
- row.current_amount_bits = amount.to_bits();
- if let Some(accumulated_delta) = accumulated_delta {
- row.current_accumulated_delta_bits = accumulated_delta.to_bits();
- }
- row.current_type1_value_bits = ratio.to_bits();
- mesh.type1_value_bits = ratio.to_bits();
- row.current_flags = Some(if amount == 0.0 {
- flags | 0x10
- } else {
- flags & !0x10
- });
- table.node_provider_amount_bits = Some(total.to_bits());
- table.node_provider_dirty = true;
- Ok(Some(ControlNodeProviderPublication {
- key,
- type1_value_bits: ratio.to_bits(),
- }))
-}
-
fn query_control_device_table_resource(
table: &MissionControlDeviceTableState,
selector: u32,
@@ -9693,7 +9582,7 @@ mod tests {
control
}
- fn synthetic_node_provider_row(
+ pub(super) fn synthetic_node_provider_row(
component_index: usize,
source_row_index: usize,
initial_type1_value_bits: u32,
@@ -10026,7 +9915,7 @@ mod tests {
)
}
- fn synthetic_control_device_table_with_providers(
+ pub(super) fn synthetic_control_device_table_with_providers(
status: MissionControlDeviceTableStatus,
node_provider_status: MissionControlNodeProviderTableStatus,
) -> MissionControlDeviceTableState {
@@ -10044,6 +9933,7 @@ mod tests {
),
node_provider_amount_bits: initial_node_provider_amount(&node_provider_status),
node_provider_dirty: false,
+ node_provider_depletion_marked: false,
node_provider_distribution_suspended: false,
current_output_rate_5dc_bits: 0,
current_output_rate_5e0_bits: 0,
@@ -18065,6 +17955,7 @@ mod tests {
node_provider_maximum_amount_bits: Some(0),
node_provider_amount_bits: Some(0),
node_provider_dirty: false,
+ node_provider_depletion_marked: false,
node_provider_distribution_suspended: false,
current_output_rate_5dc_bits: 0,
current_output_rate_5e0_bits: 0,
@@ -18177,6 +18068,7 @@ mod tests {
node_provider_maximum_amount_bits: Some(100.0_f32.to_bits()),
node_provider_amount_bits: Some(100.0_f32.to_bits()),
node_provider_dirty: false,
+ node_provider_depletion_marked: false,
node_provider_distribution_suspended: false,
current_output_rate_5dc_bits: 0,
current_output_rate_5e0_bits: 0,
@@ -18231,6 +18123,7 @@ mod tests {
node_provider_maximum_amount_bits: Some(0),
node_provider_amount_bits: Some(0),
node_provider_dirty: false,
+ node_provider_depletion_marked: false,
node_provider_distribution_suspended: false,
current_output_rate_5dc_bits: 0,
current_output_rate_5e0_bits: 0,
diff --git a/crates/fparkan-runtime/src/node_amount.rs b/crates/fparkan-runtime/src/node_amount.rs
new file mode 100644
index 00000000..1a3e80a0
--- /dev/null
+++ b/crates/fparkan-runtime/src/node_amount.rs
@@ -0,0 +1,256 @@
+use super::{
+ checked_control_float, checked_control_ratio, checked_control_sum,
+ ControlNodeProviderPublication, ControlResourceAccessError, MissionControlDeviceTableState,
+ MissionControlNodeProviderTableStatus,
+};
+
+pub(super) fn apply_control_node_delta(
+ table: &mut MissionControlDeviceTableState,
+ key: u32,
+ delta: f32,
+) -> Result<Option<ControlNodeProviderPublication>, ControlResourceAccessError> {
+ apply_control_node_delta_inner(table, key, delta, false)
+}
+
+pub(super) fn apply_control_node_distribution_delta(
+ table: &mut MissionControlDeviceTableState,
+ key: u32,
+ delta: f32,
+) -> Result<Option<ControlNodeProviderPublication>, ControlResourceAccessError> {
+ apply_control_node_delta_inner(table, key, delta, true)
+}
+
+fn apply_control_node_delta_inner(
+ table: &mut MissionControlDeviceTableState,
+ key: u32,
+ delta: f32,
+ accumulate_delta: bool,
+) -> Result<Option<ControlNodeProviderPublication>, ControlResourceAccessError> {
+ let rows = match &mut table.node_provider_status {
+ MissionControlNodeProviderTableStatus::Available(rows) => rows,
+ MissionControlNodeProviderTableStatus::Unavailable(reason) => {
+ return Err(ControlResourceAccessError::NodeProviderTableUnavailable(
+ reason.clone(),
+ ))
+ }
+ };
+ let row_count = rows.len();
+ let row = usize::try_from(key)
+ .ok()
+ .and_then(|index| rows.get_mut(index))
+ .ok_or(ControlResourceAccessError::NodeProviderKeyOutOfRange { key, row_count })?;
+ checked_control_float(0, "provider delta", delta)?;
+ if delta == 0.0 {
+ return Ok(None);
+ }
+ let previous = checked_control_float(
+ 0,
+ "provider amount",
+ f32::from_bits(row.current_amount_bits),
+ )?;
+ let maximum = checked_control_float(0, "provider maximum", row.source_row.max_volume)?;
+ if maximum < 0.0 || previous < 0.0 || previous > maximum {
+ return Err(ControlResourceAccessError::InvalidNodeProviderAmount { key });
+ }
+ let amount =
+ checked_control_sum(0, "provider amount plus delta", previous, delta)?.clamp(0.0, maximum);
+ if amount == previous {
+ row.current_amount_bits = amount.to_bits();
+ return Ok(None);
+ }
+ let flags = row
+ .current_flags
+ .ok_or(ControlResourceAccessError::UnsupportedField {
+ factory_tag: 0,
+ field: "provider flags",
+ })?;
+ let depletion = amount == 0.0 && (key == 0 || flags & 1 != 0);
+ // Control+0x11085 marks class 3 without the mobile death callbacks or root cascade.
+ if depletion
+ && !table.node_provider_depletion_marked
+ && !table
+ .native_control_class
+ .is_some_and(|class| class.class_id == 3)
+ {
+ return Err(ControlResourceAccessError::UnsupportedNodeLifecycle { key });
+ }
+ let total_bits =
+ table
+ .node_provider_amount_bits
+ .ok_or(ControlResourceAccessError::UnsupportedField {
+ factory_tag: 0,
+ field: "provider aggregate",
+ })?;
+ let total = checked_control_float(0, "provider aggregate", f32::from_bits(total_bits))?;
+ if total < 0.0 {
+ return Err(ControlResourceAccessError::InvalidNodeProviderAmount { key });
+ }
+ let change = checked_control_sum(0, "provider amount change", amount, -previous)?;
+ let total = checked_control_sum(0, "provider aggregate", total, change)?.max(0.0);
+ let ratio = checked_control_ratio("provider ratio", amount, maximum)?;
+ let accumulated_delta = if accumulate_delta {
+ let previous = checked_control_float(
+ 0,
+ "provider accumulated delta",
+ f32::from_bits(row.current_accumulated_delta_bits),
+ )?;
+ Some(checked_control_sum(
+ 0,
+ "provider accumulated delta",
+ change,
+ previous,
+ )?)
+ } else {
+ None
+ };
+ let mesh = row
+ .mesh_state
+ .as_mut()
+ .ok_or(ControlResourceAccessError::UnsupportedField {
+ factory_tag: 0,
+ field: "model node state",
+ })?;
+ row.current_amount_bits = amount.to_bits();
+ if let Some(accumulated_delta) = accumulated_delta {
+ row.current_accumulated_delta_bits = accumulated_delta.to_bits();
+ }
+ row.current_type1_value_bits = ratio.to_bits();
+ mesh.type1_value_bits = ratio.to_bits();
+ row.current_flags = Some(if amount == 0.0 {
+ flags | 0x10
+ } else {
+ flags & !0x10
+ });
+ table.node_provider_amount_bits = Some(total.to_bits());
+ table.node_provider_dirty = true;
+ table.node_provider_depletion_marked |= depletion;
+ Ok(Some(ControlNodeProviderPublication {
+ key,
+ type1_value_bits: ratio.to_bits(),
+ }))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{
+ tests::{synthetic_control_device_table_with_providers, synthetic_node_provider_row},
+ MissionControlClassProjection, MissionControlClassSource, MissionControlDeviceTableStatus,
+ };
+
+ fn table() -> MissionControlDeviceTableState {
+ let mut root = synthetic_node_provider_row(0, 0, 1.0_f32.to_bits());
+ root.current_flags = Some(0);
+ let mut child = synthetic_node_provider_row(0, 1, 1.0_f32.to_bits());
+ child.source_row.max_volume = 40.0;
+ child.initial_amount_bits = 40.0_f32.to_bits();
+ child.current_amount_bits = 40.0_f32.to_bits();
+ child.processed_amount_bits = 40.0_f32.to_bits();
+ child.current_flags = Some(1);
+ for row in [&mut root, &mut child] {
+ row.mesh_state = Some(crate::MissionControlNodeMeshState {
+ type1_value_bits: 1.0_f32.to_bits(),
+ visible_stage: 0,
+ });
+ }
+ let mut table = synthetic_control_device_table_with_providers(
+ MissionControlDeviceTableStatus::Available(Vec::new()),
+ MissionControlNodeProviderTableStatus::Available(vec![root, child]),
+ );
+ table.native_control_class = Some(MissionControlClassProjection {
+ class_id: 3,
+ source: MissionControlClassSource::UnitDatHighBit {
+ header_flags: 0x8000_0010,
+ },
+ });
+ table
+ }
+
+ #[test]
+ fn static_depletion_matches_native_root_and_critical_child_without_cascading() {
+ for (key, delta, root, child, total) in [
+ (0, -150.0, 0, 0x4220_0000, 0x4220_0000),
+ (1, -60.0, 0x42c8_0000, 0, 0x42c8_0000),
+ ] {
+ let mut table = table();
+ let publication = apply_control_node_delta(&mut table, key, delta)
+ .unwrap()
+ .unwrap();
+ assert_eq!(publication.type1_value_bits, 0);
+ assert_eq!(table.node_provider_amount_bits, Some(total));
+ assert!(table.node_provider_depletion_marked);
+ let MissionControlNodeProviderTableStatus::Available(rows) =
+ &table.node_provider_status
+ else {
+ panic!()
+ };
+ assert_eq!(rows[0].current_amount_bits, root);
+ assert_eq!(rows[1].current_amount_bits, child);
+ assert_ne!(rows[key as usize].current_flags.unwrap() & 0x10, 0);
+ apply_control_node_delta(&mut table, key, 10.0).unwrap();
+ assert!(
+ table.node_provider_depletion_marked,
+ "native marker survives repair"
+ );
+ }
+ }
+
+ #[test]
+ fn mobile_death_callbacks_remain_explicit_until_the_marker_is_already_set() {
+ let mut table = table();
+ table.native_control_class = Some(MissionControlClassProjection {
+ class_id: 4,
+ source: MissionControlClassSource::RootPrototypeFourCc {
+ source_type: crate::CONTROL_CLASS_SOURCE_BTLU,
+ },
+ });
+ let before = table.clone();
+ assert_eq!(
+ apply_control_node_delta(&mut table, 0, -150.0),
+ Err(ControlResourceAccessError::UnsupportedNodeLifecycle { key: 0 })
+ );
+ assert_eq!(table, before);
+ table.node_provider_depletion_marked = true;
+ apply_control_node_delta(&mut table, 0, -150.0).unwrap();
+ assert_eq!(table.node_provider_amount_bits, Some(0x4220_0000));
+ }
+
+ #[test]
+ #[ignore = "requires licensed corpus"]
+ fn licensed_scripted_raid_depletes_the_stock_bunker_root() {
+ use crate::*;
+ use fparkan_vfs::DirectoryVfs;
+ 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/CAMPAIGN/CAMPAIGN.04/Mission.02/data.tma".into(),
+ mode: MissionMode::SingleMission,
+ },
+ MissionStartupInput {
+ queue_time_ms: 0,
+ research_creation_gate: false,
+ },
+ None,
+ )
+ .unwrap();
+ let report = simulate_mission_report(&mut engine, 900_000).unwrap();
+ let table = &loaded_mission_control_device_tables(&engine).unwrap()[0];
+ assert!(table.node_provider_depletion_marked);
+ let row = crate::control_node_provider_row(table, 0).unwrap();
+ assert_eq!(row.current_amount_bits, 0);
+ assert_eq!(row.mesh_state.unwrap().type1_value_bits, 0);
+ assert_ne!(row.current_flags.unwrap() & 0x10, 0);
+ assert!(
+ fparkan_world::transform_state(&engine.world, table.handle).is_ok(),
+ "static depletion does not immediately delete the building"
+ );
+ assert!(!report
+ .combat
+ .unavailable
+ .keys()
+ .any(|(_, reason)| *reason == "damage_error"));
+ }
+}