diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 19:08:24 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 19:08:24 +0300 |
| commit | 87c082f87844f47dd6b057fc2458cee80002dc55 (patch) | |
| tree | 8d8785580c9d804dadadf1053b49f40bfae4a91e /crates/fparkan-runtime/src/weapon_resupply.rs | |
| parent | 3b10d83864d84715b875d596fcd5b4147fcd8ed3 (diff) | |
| download | fparkan-87c082f87844f47dd6b057fc2458cee80002dc55.tar.xz fparkan-87c082f87844f47dd6b057fc2458cee80002dc55.zip | |
feat(control): replenish ammunition on repair pads
Diffstat (limited to 'crates/fparkan-runtime/src/weapon_resupply.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/weapon_resupply.rs | 59 |
1 files changed, 59 insertions, 0 deletions
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<u32> { + 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); + } + } +} |
