From 87c082f87844f47dd6b057fc2458cee80002dc55 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 20:08:24 +0400 Subject: feat(control): replenish ammunition on repair pads --- crates/fparkan-runtime/src/weapon_resupply.rs | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 crates/fparkan-runtime/src/weapon_resupply.rs (limited to 'crates/fparkan-runtime/src/weapon_resupply.rs') diff --git a/crates/fparkan-runtime/src/weapon_resupply.rs b/crates/fparkan-runtime/src/weapon_resupply.rs new file mode 100644 index 00000000..31a23594 --- /dev/null +++ b/crates/fparkan-runtime/src/weapon_resupply.rs @@ -0,0 +1,59 @@ +use crate::MissionControlDeviceTableStatus; + +pub(super) fn capacity(bits: u32) -> Option { + let rounds = f32::from_bits(bits).round_ties_even(); + (0.0..2_147_483_648.0) + .contains(&rounds) + .then_some(rounds as u32) +} + +pub(super) fn refill(status: &mut MissionControlDeviceTableStatus, fraction: f32) { + let MissionControlDeviceTableStatus::Available(devices) = status else { + return; + }; + for device in devices + .iter_mut() + .filter(|device| device.descriptor.tag == 2) + { + if let (Some(stock), Some(capacity)) = ( + &mut device.weapon_rounds, + capacity(device.descriptor.fixed_words[11]).filter(|&capacity| capacity != 0), + ) { + // Behavior.dll+0x18276 rounds to nearest and grants at least one round. + let added = ((capacity as f32 * fraction).round_ties_even() as u32).max(1); + *stock = stock.saturating_add(added).min(capacity); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::synthetic_effective_control_device; + + #[test] + fn pad_rounds_each_refill_and_leaves_unlimited_weapons_alone() { + let device = |capacity: f32, stock| { + let mut device = + synthetic_effective_control_device(2, 2, capacity.to_bits(), None, None); + device.weapon_rounds = stock; + device + }; + let mut status = MissionControlDeviceTableStatus::Available(vec![ + device(100.0, Some(3)), + device(4.0, Some(3)), + device(0.0, Some(0)), + device(-1.0, None), + ]); + for (seconds, expected) in [(0.01, 4), (0.25, 6), (2.0, 26)] { + refill(&mut status, seconds * 0.1); + let MissionControlDeviceTableStatus::Available(devices) = &status else { + unreachable!(); + }; + assert_eq!(devices[0].weapon_rounds, Some(expected)); + assert_eq!(devices[1].weapon_rounds, Some(4)); + assert_eq!(devices[2].weapon_rounds, Some(0)); + assert_eq!(devices[3].weapon_rounds, None); + } + } +} -- cgit v1.2.3