From 352026c1ce039443efc779876b27654d992bfff1 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 08:12:30 +0400 Subject: fix(combat): propagate the loss of a warbot chassis section Reuse the Control provider traversal after a lethal chassis hit so attached links disappear with the failed section. The GOG Control oracle and a controlled AutoDemo hit confirm the subtree behavior. All 29 missions reached 900 simulated seconds. --- crates/fparkan-runtime/src/lib.rs | 236 +------------------------------------- 1 file changed, 4 insertions(+), 232 deletions(-) (limited to 'crates/fparkan-runtime/src/lib.rs') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index e6fe77af..5f3f436e 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -67,6 +67,7 @@ mod control_profile; mod control_resources; mod mobile_budget; mod node_amount; +mod node_stage; 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}; @@ -74,6 +75,9 @@ pub use mobile_budget::MobileBudgetReport; use node_amount::{ apply_control_node_delta, distribute_control_node_amount, distribute_control_node_amount_inner, }; +use node_stage::{ + propagate_control_node_stage, propagate_control_node_stage_inner, update_control_node_stage, +}; mod clan_combat_groups; mod clan_combat_strength; mod clan_factory_schemes; @@ -4429,238 +4433,6 @@ pub fn propagate_loaded_mission_control_node_stage( propagate_control_node_stage(table, key, update_effects) } -fn update_control_node_stage( - table: &mut MissionControlDeviceTableState, - key: u32, - update_effects: bool, -) -> Result { - 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 index = usize::try_from(key) - .ok() - .filter(|index| *index < row_count) - .ok_or(ControlResourceAccessError::NodeProviderKeyOutOfRange { key, row_count })?; - let row = &rows[index]; - let flags = row - .current_flags - .ok_or(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider flags", - })?; - if flags & 0x1c0 != 0 { - if flags & 0x10 == 0 { - return Err(ControlResourceAccessError::UnsupportedNodeLifecycle { key }); - } - if flags & 0xc0 != 0 { - return Ok(false); - } - } - let metadata = row - .initial_mesh_state - .ok_or(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider mesh metadata", - })?; - let count = metadata.stage_count; - if count == 0 { - return Err(ControlResourceAccessError::InvalidNodeStageState { key }); - } - let parent = if flags & 0x0c != 0 { - Some( - usize::try_from(metadata.parent_index) - .ok() - .and_then(|parent| rows.get(parent)) - .ok_or(ControlResourceAccessError::NodeProviderParentOutOfRange { - key, - parent_index: metadata.parent_index, - row_count, - })?, - ) - } else { - None - }; - let ratio_bits = if flags & 4 != 0 { - parent - .expect("parent validated for inheritance") - .current_type1_value_bits - } else { - row.current_type1_value_bits - }; - let ratio = checked_control_float(0, "provider stage ratio", f32::from_bits(ratio_bits))?; - if !(0.0..=1.0).contains(&ratio) { - return Err(ControlResourceAccessError::InvalidNodeStageState { key }); - } - let stage = if flags & 8 != 0 { - parent - .expect("parent validated for inheritance") - .current_stage - .min(count) - } else { - let product = control_stage_product(count, ratio); - count.wrapping_sub(product.ceil() as u64 as u32).min(count) - }; - let changed = stage != row.current_stage; - if changed { - if update_effects { - return Err(ControlResourceAccessError::UnsupportedNodeEffects { key }); - } - if flags & 2 == 0 && (stage == count || row.current_stage == count) { - return Err(ControlResourceAccessError::UnsupportedNodeVisibility { key }); - } - if row.mesh_state.is_none() { - return Err(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "model node state", - }); - } - } - let row = &mut rows[index]; - row.current_type1_value_bits = ratio_bits; - row.processed_amount_bits = row.current_amount_bits; - if changed { - row.current_flags = Some(flags | 0x200); - row.current_stage = stage; - row.mesh_state - .as_mut() - .expect("model state validated") - .visible_stage = stage.min(count - 1); - } - Ok(changed) -} - -fn propagate_control_node_stage( - table: &mut MissionControlDeviceTableState, - key: u32, - update_effects: bool, -) -> Result<(), ControlResourceAccessError> { - let mut updated = table.clone(); - propagate_control_node_stage_inner(&mut updated, key, update_effects)?; - *table = updated; - Ok(()) -} - -fn propagate_control_node_stage_inner( - table: &mut MissionControlDeviceTableState, - key: u32, - update_effects: bool, -) -> Result<(), ControlResourceAccessError> { - let (index, row_count, previous_stage) = match &table.node_provider_status { - MissionControlNodeProviderTableStatus::Available(rows) => { - let row_count = rows.len(); - let index = usize::try_from(key) - .ok() - .filter(|index| *index < row_count) - .ok_or(ControlResourceAccessError::NodeProviderKeyOutOfRange { key, row_count })?; - (index, row_count, rows[index].current_stage) - } - MissionControlNodeProviderTableStatus::Unavailable(reason) => { - return Err(ControlResourceAccessError::NodeProviderTableUnavailable( - reason.clone(), - )) - } - }; - - update_control_node_stage(table, key, update_effects)?; - - let (stage, flags) = match &table.node_provider_status { - MissionControlNodeProviderTableStatus::Available(rows) => { - let row = &rows[index]; - let flags = row - .current_flags - .ok_or(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider flags", - })?; - (row.current_stage, flags) - } - MissionControlNodeProviderTableStatus::Unavailable(reason) => { - return Err(ControlResourceAccessError::NodeProviderTableUnavailable( - reason.clone(), - )) - } - }; - let depleted_or_worsened = previous_stage < stage || flags & 0x10 != 0; - - for child_index in index + 1..row_count { - let (metadata, child_flags) = match &table.node_provider_status { - MissionControlNodeProviderTableStatus::Available(rows) => { - let row = &rows[child_index]; - let metadata = - row.initial_mesh_state - .ok_or(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider mesh metadata", - })?; - let child_flags = row.current_flags; - (metadata, child_flags) - } - MissionControlNodeProviderTableStatus::Unavailable(reason) => { - return Err(ControlResourceAccessError::NodeProviderTableUnavailable( - reason.clone(), - )) - } - }; - if i64::from(metadata.parent_index) != i64::from(key) { - continue; - } - - let child_key = u32::try_from(child_index) - .map_err(|_| ControlResourceAccessError::InvalidNodeStageState { key })?; - let child_flags = child_flags.ok_or(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider flags", - })?; - if child_flags & 0x140 == 0 { - let child_flags = if flags & 0xc0 == 0 { - (child_flags | if depleted_or_worsened { 0x100 } else { 0 }) & !0x80 - } else { - child_flags | 0x80 - }; - if let MissionControlNodeProviderTableStatus::Available(rows) = - &mut table.node_provider_status - { - rows[child_index].current_flags = Some(child_flags); - } - } - - if depleted_or_worsened { - let child_amount = match &table.node_provider_status { - MissionControlNodeProviderTableStatus::Available(rows) => { - f32::from_bits(rows[child_index].current_amount_bits) - } - MissionControlNodeProviderTableStatus::Unavailable(reason) => { - return Err(ControlResourceAccessError::NodeProviderTableUnavailable( - reason.clone(), - )) - } - }; - apply_control_node_delta(table, child_key, -child_amount)?; - } - - propagate_control_node_stage_inner(table, child_key, update_effects)?; - } - Ok(()) -} - -fn control_stage_product(count: u32, ratio: f32) -> f32 { - if ratio == 0.0 { - return 0.0; - } - // FILD keeps the integer exact; round the mantissa product once, then rescale exactly. - let bits = ratio.to_bits(); - let significand = (bits & 0x007f_ffff) | 0x0080_0000; - let exponent = ((bits >> 23) & 0xff) as i32 - 150; - let product = u64::from(count) * u64::from(significand); - (f64::from(product as f32) * 2.0_f64.powi(exponent)) as f32 -} - fn initial_node_provider_amount(status: &MissionControlNodeProviderTableStatus) -> Option { let MissionControlNodeProviderTableStatus::Available(rows) = status else { return None; -- cgit v1.2.3