From fd0c4eb23337953895ec3b7ac23178b495128930 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 03:10:34 +0400 Subject: fix(behavior): exclude depleted objects from clan resources Apply the original Behavior resource distributor's 0xfffe state guard before resource callbacks in both passes. Keep the object, distributor row, shares and previous scratch words intact. Move the two-pass driver out of lib.rs into resource_distribution.rs without changing its allocation arithmetic. The GOG Behavior.dll RVA 0x19e80 oracle preserves seeded scratch fields and performs only lookup/state callbacks for a depleted object. The licensed stock mission-19 raid reduces clan power output from 10.07 to 10 per second after bunker depletion while retaining its live world object and distributor entry. The small 0.07 contribution is below the window title's display precision. Verified the stock raid in the game window with the hero at the starting position: bunker root reaches zero at 329760 ms, Attack finishes and queued retreat begins. Destruction visuals and other Control lifecycle paths remain incomplete; this does not establish whole-mission parity. Validation: cargo fmt --all -- --check; cargo check --workspace; cargo test --workspace; release build; focused Rust and licensed-stock tests; native DLL oracle; all 29 stock missions at --sim-report 900 (existing motion_factory_5 stub allowed). No new unsupported commands or damage errors, and world summaries match the preceding commit. --- crates/fparkan-runtime/src/lib.rs | 230 +------------------------------------- 1 file changed, 4 insertions(+), 226 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 58140bee..81fad53a 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -91,6 +91,8 @@ mod player_weapon; mod production; mod projectile; mod research; +mod resource_distribution; +use resource_distribution::advance_behavior_distributor_tick; mod script_host; mod script_patrol; mod script_time; @@ -6153,230 +6155,6 @@ fn behavior_query_control( .map_err(|error| format!("{error:?}")) } -fn advance_behavior_distributor_tick( - distributor: &mut BehaviorDistributorState, - delta_seconds: f32, - world: &World, - ore_states: &mut [BehaviorOreState], - profiles: &[BehaviorProfileState], - control_tables: &mut [MissionControlDeviceTableState], -) -> Result<[u32; 5], String> { - let mut totals = BehaviorTickTotals::default(); - - // Native pass one measures supply and demand, then records per-row demand. - for object in &mut distributor.registered_objects { - let Some(live_handle) = handle_by_original_id(world, object.original_id) else { - object.native_tail[1] = 0; - object.native_tail[3] = 0; - continue; - }; - if live_handle != object.handle { - return Err(format!( - "live original object {} maps to {live_handle:?}, expected {:?}", - object.original_id.0, object.handle - )); - } - let share = checked_control_float(object.class_flags, "Behavior share", object.share) - .map_err(|error| format!("{error:?}"))?; - - let power_source = matches!( - object.class_flags, - 0x8000_0002 | 0x8001_0000 | 0x8002_0000 | 0x8004_0000 - ); - let power_demand = if power_source { - let rate = behavior_tick_resource_rate( - object.class_flags, - live_handle, - 0x1005, - profiles, - control_tables, - )?; - let production = behavior_product(delta_seconds, rate, "power production")?; - behavior_add_total(&mut totals.power_available, production, "power available")?; - 0.0 - } else { - let total_power = behavior_query_control(control_tables, live_handle, 2)?; - let power_ratio = behavior_query_control(control_tables, live_handle, 1)?; - if total_power > 0.0 { - let used_power = behavior_product(power_ratio, total_power, "used power")?; - behavior_difference(total_power, used_power, "power demand")? - } else { - 0.0 - } - }; - object.native_tail[1] = power_demand.to_bits(); - behavior_add_total(&mut totals.power_demand, power_demand, "power demand")?; - behavior_add_total( - &mut totals.power_weighted_demand, - behavior_product(power_demand, share, "weighted power demand")?, - "weighted power demand", - )?; - - let ore_source = matches!(object.class_flags, 0x8000_0004 | 0x8000_0008); - let ore_index = behavior_ore_index_for_handle(ore_states, live_handle)?; - let current = behavior_checked_property(&ore_states[ore_index], false)?; - let ore_demand = if ore_source { - let rate = behavior_tick_resource_rate( - object.class_flags, - live_handle, - 0x1002, - profiles, - control_tables, - )?; - let production = behavior_product(delta_seconds, rate, "ore production")?; - let available = if current < production { - current - } else { - production - }; - behavior_add_total(&mut totals.ore_available, available, "ore available")?; - behavior_add_total(&mut totals.ore_in_buffer, current, "ore in buffer")?; - 0.0 - } else { - let maximum = behavior_checked_property(&ore_states[ore_index], true)?; - let deficit = behavior_difference(maximum, current, "ore deficit")?; - if deficit >= f32::from_bits(0x3c23_d70a) { - deficit - } else { - ore_states[ore_index].current_ore_bits = maximum.to_bits(); - 0.0 - } - }; - object.native_tail[3] = ore_demand.to_bits(); - behavior_add_total(&mut totals.ore_demand, ore_demand, "ore demand")?; - behavior_add_total( - &mut totals.ore_weighted_demand, - behavior_product(ore_demand, share, "weighted ore demand")?, - "weighted ore demand", - )?; - } - - let power_allocation = behavior_allocation( - totals.power_available, - totals.power_demand, - totals.power_weighted_demand, - )?; - let ore_allocation = behavior_allocation( - totals.ore_available, - totals.ore_demand, - totals.ore_weighted_demand, - )?; - - // Native pass two re-reads callbacks before applying each row in order. - for object in &mut distributor.registered_objects { - let Some(live_handle) = handle_by_original_id(world, object.original_id) else { - object.native_tail[2] = 0; - object.native_tail[4] = 0; - continue; - }; - if live_handle != object.handle { - return Err(format!( - "live original object {} maps to {live_handle:?}, expected {:?}", - object.original_id.0, object.handle - )); - } - object.native_tail[2] = 0; - object.native_tail[4] = 0; - - let power_source = matches!( - object.class_flags, - 0x8000_0002 | 0x8001_0000 | 0x8002_0000 | 0x8004_0000 - ); - if power_source { - let table_index = behavior_control_index_for_handle(control_tables, live_handle)?; - set_control_device_table_multiplier(&mut control_tables[table_index], 1.0) - .map_err(|error| format!("{error:?}"))?; - } else { - let table_index = behavior_control_index_for_handle(control_tables, live_handle)?; - let total_power = behavior_query_control(control_tables, live_handle, 2)?; - if total_power > 0.0 { - let power_ratio = behavior_query_control(control_tables, live_handle, 1)?; - let used_power = behavior_product(power_ratio, total_power, "used power")?; - let demand = behavior_difference(total_power, used_power, "power demand")?; - let share = object.share; - let priority_share = - behavior_product(share, power_allocation.priority, "priority power share")?; - let residual_share = behavior_product( - behavior_difference(1.0, share, "remaining share")?, - power_allocation.residual, - "residual power share", - )?; - let fraction = checked_control_sum( - 0, - "power allocation fraction", - residual_share, - priority_share, - ) - .map_err(|error| format!("{error:?}"))?; - let allocated = behavior_product(demand, fraction, "power allocation")?; - let updated = - checked_control_sum(0, "used plus allocated power", used_power, allocated) - .map_err(|error| format!("{error:?}"))?; - let new_ratio = - checked_control_ratio("new Control selector 1", updated, total_power) - .map_err(|error| format!("{error:?}"))?; - set_control_device_table_multiplier(&mut control_tables[table_index], new_ratio) - .map_err(|error| format!("{error:?}"))?; - object.native_tail[2] = allocated.to_bits(); - } - } - - let ore_source = matches!(object.class_flags, 0x8000_0004 | 0x8000_0008); - let ore_index = behavior_ore_index_for_handle(ore_states, live_handle)?; - let current = behavior_checked_property(&ore_states[ore_index], false)?; - if ore_source { - let rate = behavior_tick_resource_rate( - object.class_flags, - live_handle, - 0x1002, - profiles, - control_tables, - )?; - let production = behavior_product(delta_seconds, rate, "ore withdrawal rate")?; - let bounded_production = if current < production { - current - } else { - production - }; - let taken = behavior_product( - bounded_production, - ore_allocation.consumption, - "ore withdrawal", - )?; - ore_states[ore_index].current_ore_bits = - behavior_difference(current, taken, "remaining ore")?.to_bits(); - } else { - let maximum = behavior_checked_property(&ore_states[ore_index], true)?; - let capacity = behavior_difference(maximum, current, "ore capacity")?; - let share = object.share; - let priority_share = - behavior_product(share, ore_allocation.priority, "priority ore share")?; - let residual_share = behavior_product( - behavior_difference(1.0, share, "remaining share")?, - ore_allocation.residual, - "residual ore share", - )?; - let fraction = - checked_control_sum(0, "ore allocation fraction", residual_share, priority_share) - .map_err(|error| format!("{error:?}"))?; - let allocated = behavior_product(capacity, fraction, "ore allocation")?; - ore_states[ore_index].current_ore_bits = - checked_control_sum(0, "current plus allocated ore", current, allocated) - .map_err(|error| format!("{error:?}"))? - .to_bits(); - object.native_tail[4] = allocated.to_bits(); - } - } - - Ok([ - totals.power_available.to_bits(), - totals.ore_available.to_bits(), - totals.ore_in_buffer.to_bits(), - totals.power_demand.to_bits(), - totals.ore_demand.to_bits(), - ]) -} - fn set_control_device_table_multiplier( table: &mut MissionControlDeviceTableState, value: f32, @@ -17915,7 +17693,7 @@ mod tests { assert_eq!(exact.consumption.to_bits(), 1.0_f32.to_bits()); } - fn behavior_tick_test_object( + pub(super) fn behavior_tick_test_object( world: &mut World, original_id: u32, class_flags: u32, @@ -17940,7 +17718,7 @@ mod tests { } } - fn behavior_tick_test_control_table( + pub(super) fn behavior_tick_test_control_table( original_id: u32, handle: ObjectHandle, ) -> MissionControlDeviceTableState { -- cgit v1.2.3