diff options
Diffstat (limited to 'crates/fparkan-runtime/src/lib.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 258 |
1 files changed, 5 insertions, 253 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index cdd8d35a..3b097acd 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -71,7 +71,9 @@ 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}; +use node_amount::{ + apply_control_node_delta, distribute_control_node_amount, distribute_control_node_amount_inner, +}; mod clan_combat_groups; mod clan_combat_strength; mod clan_factory_schemes; @@ -2915,8 +2917,8 @@ pub fn step_headless( if let Some(mission) = &mut engine.loaded { capture_barrier::advance(mission, &engine.world, queue_time_ms); player_gait::advance(mission, &engine.world, queue_time_ms); - control_resources::tick(mission, &engine.world, queue_time_ms); } + control_resources::tick(engine, queue_time_ms); let (autodemo_combat_events, autodemo_combat_unavailable) = if let Some(mut combat) = engine.autodemo_combat.take() { let result = combat.tick(engine, queue_time_ms); @@ -4708,256 +4710,6 @@ fn control_node_provider_row_from_status( .ok_or(ControlResourceAccessError::NodeProviderKeyOutOfRange { key, row_count }) } -fn checked_control_node_provider_amounts( - row: &MissionControlNodeProviderRow, - key: u32, -) -> Result<(f32, f32), ControlResourceAccessError> { - let amount = 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 amount < 0.0 || maximum < amount { - return Err(ControlResourceAccessError::InvalidNodeProviderAmount { key }); - } - Ok((amount, maximum)) -} - -fn distribute_control_node_amount( - table: &mut MissionControlDeviceTableState, - delta: f32, - mode: ControlNodeDistributionMode, - revive_exhausted: bool, -) -> Result<(), ControlResourceAccessError> { - if table.node_provider_distribution_suspended || delta == 0.0 { - return Ok(()); - } - let delta = checked_control_float(0, "provider distribution delta", delta)?; - let mut updated = table.clone(); - distribute_control_node_amount_inner(&mut updated, delta, mode, revive_exhausted)?; - *table = updated; - Ok(()) -} - -fn distribute_control_node_amount_inner( - table: &mut MissionControlDeviceTableState, - delta: f32, - mode: ControlNodeDistributionMode, - revive_exhausted: bool, -) -> Result<(), ControlResourceAccessError> { - let row_count = match &table.node_provider_status { - MissionControlNodeProviderTableStatus::Available(rows) => rows.len(), - MissionControlNodeProviderTableStatus::Unavailable(reason) => { - return Err(ControlResourceAccessError::NodeProviderTableUnavailable( - reason.clone(), - )); - } - }; - let aggregate_bits = - table - .node_provider_amount_bits - .ok_or(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider aggregate", - })?; - let aggregate = checked_control_float(0, "provider aggregate", f32::from_bits(aggregate_bits))?; - if aggregate < 0.0 { - return Err(ControlResourceAccessError::InvalidNodeProviderAmount { key: 0 }); - } - - let capacity = if delta > 0.0 { - if revive_exhausted { - let maximum_bits = table.node_provider_maximum_amount_bits.ok_or( - ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider maximum aggregate", - }, - )?; - let maximum = checked_control_float( - 0, - "provider maximum aggregate", - f32::from_bits(maximum_bits), - )?; - checked_control_sum(0, "provider remaining total capacity", maximum, -aggregate)? - } else { - let rows = match &table.node_provider_status { - MissionControlNodeProviderTableStatus::Available(rows) => rows, - MissionControlNodeProviderTableStatus::Unavailable(reason) => { - return Err(ControlResourceAccessError::NodeProviderTableUnavailable( - reason.clone(), - )); - } - }; - let mut capacity = 0.0_f32; - for (index, row) in rows.iter().enumerate() { - let key = u32::try_from(index).map_err(|_| { - ControlResourceAccessError::NodeProviderKeyOutOfRange { - key: u32::MAX, - row_count, - } - })?; - let flags = - row.current_flags - .ok_or(ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider flags", - })?; - if flags & 0x10 != 0 { - continue; - } - let (amount, maximum) = checked_control_node_provider_amounts(row, key)?; - let free = checked_control_sum(0, "provider free capacity", maximum, -amount)?; - capacity = checked_control_sum(0, "provider total free capacity", capacity, free)?; - } - capacity - } - } else { - -aggregate - }; - if capacity == 0.0 { - return Ok(()); - } - - let bounded_delta = if delta > 0.0 { - if delta > capacity { - capacity - } else { - delta - } - } else if delta < capacity { - capacity - } else { - delta - }; - - match mode { - ControlNodeDistributionMode::Proportional => { - let adjusted_aggregate = checked_control_sum( - 0, - "provider aggregate after distribution", - aggregate, - bounded_delta, - )?; - let applied_delta = checked_control_sum( - 0, - "provider applied distribution delta", - adjusted_aggregate, - -aggregate, - )?; - let ratio = - checked_control_ratio("provider distribution ratio", applied_delta, capacity)?; - for index in 0..row_count { - let key = u32::try_from(index).map_err(|_| { - ControlResourceAccessError::NodeProviderKeyOutOfRange { - key: u32::MAX, - row_count, - } - })?; - if delta > 0.0 && !revive_exhausted { - let flags = control_node_provider_row(table, key)?.current_flags.ok_or( - ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider flags", - }, - )?; - if flags & 0x10 != 0 { - continue; - } - } - let (amount, maximum) = checked_control_node_provider_amounts( - control_node_provider_row(table, key)?, - key, - )?; - let row_delta = if delta > 0.0 { - let free = checked_control_sum(0, "provider free capacity", maximum, -amount)?; - checked_control_product(0, "provider proportional addition", free, ratio)? - } else { - -checked_control_product(0, "provider proportional withdrawal", ratio, amount)? - }; - apply_control_node_distribution_delta(table, key, row_delta)?; - } - } - ControlNodeDistributionMode::Sequential => { - let mut remaining = delta; - if delta > 0.0 { - for index in 0..row_count { - if remaining <= 0.0 { - break; - } - let key = u32::try_from(index).map_err(|_| { - ControlResourceAccessError::NodeProviderKeyOutOfRange { - key: u32::MAX, - row_count, - } - })?; - if !revive_exhausted { - let flags = control_node_provider_row(table, key)?.current_flags.ok_or( - ControlResourceAccessError::UnsupportedField { - factory_tag: 0, - field: "provider flags", - }, - )?; - if flags & 0x10 != 0 { - continue; - } - } - let (amount, maximum) = checked_control_node_provider_amounts( - control_node_provider_row(table, key)?, - key, - )?; - let free = checked_control_sum(0, "provider free capacity", maximum, -amount)?; - let row_delta = if remaining <= free { remaining } else { free }; - if row_delta != 0.0 { - apply_control_node_distribution_delta(table, key, row_delta)?; - remaining = checked_control_sum( - 0, - "provider remaining addition", - remaining, - -row_delta, - )?; - } - } - } else { - for index in (0..row_count).rev() { - if remaining >= 0.0 { - break; - } - let key = u32::try_from(index).map_err(|_| { - ControlResourceAccessError::NodeProviderKeyOutOfRange { - key: u32::MAX, - row_count, - } - })?; - let (amount, _) = checked_control_node_provider_amounts( - control_node_provider_row(table, key)?, - key, - )?; - let requested = -remaining; - let removed = if amount < requested { - amount - } else { - requested - }; - if removed != 0.0 { - let row_delta = -removed; - apply_control_node_distribution_delta(table, key, row_delta)?; - remaining = checked_control_sum( - 0, - "provider remaining withdrawal", - removed, - remaining, - )?; - } - } - } - } - } - - // Native 0x10ba0 always finishes with 0x11130(root, update_effects=true). - propagate_control_node_stage_inner(table, 0, true) -} - fn control_resource_budget_pass( table: &mut MissionControlDeviceTableState, delta_time_seconds: f32, @@ -9688,7 +9440,7 @@ mod tests { (status, node_provider_status) } - fn synthetic_effective_control_device( + pub(super) fn synthetic_effective_control_device( factory_tag: u32, descriptor_tag: u32, coefficient_bits: u32, |
