aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/node_amount.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src/node_amount.rs')
-rw-r--r--crates/fparkan-runtime/src/node_amount.rs271
1 files changed, 268 insertions, 3 deletions
diff --git a/crates/fparkan-runtime/src/node_amount.rs b/crates/fparkan-runtime/src/node_amount.rs
index 1a3e80a0..faafcc13 100644
--- a/crates/fparkan-runtime/src/node_amount.rs
+++ b/crates/fparkan-runtime/src/node_amount.rs
@@ -1,7 +1,10 @@
+//! Amount updates and distribution for Control node providers.
+
use super::{
- checked_control_float, checked_control_ratio, checked_control_sum,
+ checked_control_float, checked_control_product, checked_control_ratio, checked_control_sum,
+ control_node_provider_row, propagate_control_node_stage_inner, ControlNodeDistributionMode,
ControlNodeProviderPublication, ControlResourceAccessError, MissionControlDeviceTableState,
- MissionControlNodeProviderTableStatus,
+ MissionControlNodeProviderRow, MissionControlNodeProviderTableStatus,
};
pub(super) fn apply_control_node_delta(
@@ -12,7 +15,7 @@ pub(super) fn apply_control_node_delta(
apply_control_node_delta_inner(table, key, delta, false)
}
-pub(super) fn apply_control_node_distribution_delta(
+fn apply_control_node_distribution_delta(
table: &mut MissionControlDeviceTableState,
key: u32,
delta: f32,
@@ -130,6 +133,268 @@ fn apply_control_node_delta_inner(
}))
}
+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))
+}
+
+pub(super) 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(())
+}
+
+pub(super) fn distribute_control_node_amount_inner(
+ table: &mut MissionControlDeviceTableState,
+ delta: f32,
+ mode: ControlNodeDistributionMode,
+ revive_exhausted: bool,
+) -> Result<(), ControlResourceAccessError> {
+ if distribute_control_node_amount_rows(table, delta, mode, revive_exhausted)? {
+ // Native 0x10ba0 finishes with 0x11130(root, update_effects=true).
+ propagate_control_node_stage_inner(table, 0, true)?;
+ }
+ Ok(())
+}
+
+pub(super) fn distribute_control_node_amount_rows(
+ table: &mut MissionControlDeviceTableState,
+ delta: f32,
+ mode: ControlNodeDistributionMode,
+ revive_exhausted: bool,
+) -> Result<bool, 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(false);
+ }
+
+ 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,
+ )?;
+ }
+ }
+ }
+ }
+ }
+
+ Ok(true)
+}
+
#[cfg(test)]
mod tests {
use super::*;