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); } if let (Some(coefficients), Some(ratio_bits)) = (device.coefficient_bits, device.multiplier_bits) { let capacity = f32::from_bits(coefficients[1]); let ratio = f32::from_bits(ratio_bits); if capacity.is_finite() && capacity > 0.0 && ratio.is_finite() { // Behavior.dll+0x182c5 sets the ratio. Control.dll+0x29b10 // publishes both that ratio and its absolute charge amount. let filled = (ratio + fraction).min(1.0); device.multiplier_bits = Some(filled.to_bits()); device.weapon_charge_bits = Some((filled * capacity).to_bits()); } } } } #[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); } } #[test] fn pad_charge_updates_the_ratio_and_amount_even_without_ammunition() { let mut laser = synthetic_effective_control_device( 2, 2, (-1.0_f32).to_bits(), Some(0.25_f32.to_bits()), None, ); laser.coefficient_bits.as_mut().unwrap()[1] = 70.0_f32.to_bits(); laser.weapon_charge_bits = Some(17.5_f32.to_bits()); let mut no_capacity = laser.clone(); no_capacity.coefficient_bits.as_mut().unwrap()[1] = 0; let mut status = MissionControlDeviceTableStatus::Available(vec![laser, no_capacity]); refill(&mut status, 0.025); let MissionControlDeviceTableStatus::Available(devices) = &status else { unreachable!(); }; assert_eq!(devices[0].weapon_rounds, None); assert_eq!(devices[0].multiplier_bits, Some(0.275_f32.to_bits())); assert_eq!(devices[0].weapon_charge_bits, Some(19.25_f32.to_bits())); assert_eq!(devices[1].multiplier_bits, Some(0.25_f32.to_bits())); assert_eq!(devices[1].weapon_charge_bits, Some(17.5_f32.to_bits())); refill(&mut status, 1.0); let MissionControlDeviceTableStatus::Available(devices) = status else { unreachable!(); }; assert_eq!(devices[0].multiplier_bits, Some(1.0_f32.to_bits())); assert_eq!(devices[0].weapon_charge_bits, Some(70.0_f32.to_bits())); } }