diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/player_weapon.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index f0b73401..914201c3 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -28,6 +28,7 @@ pub struct PlayerWeapon { pub component_index: usize, pub(super) control_record_index: usize, pub(super) charge: Option<crate::mobile_budget::Charge>, + low_charge_reported: bool, pub(super) destroyed: bool, pub(super) energy_waits: u64, pub(super) autonomous_fire_distance: f32, @@ -91,6 +92,22 @@ impl PlayerWeapons { &self.weapons } + /// Weapon slots that have just crossed the cockpit's low-charge threshold. + pub fn take_low_charge_warnings(&mut self) -> impl Iterator<Item = usize> + '_ { + self.weapons + .iter_mut() + .enumerate() + .filter_map(|(slot, weapon)| { + // iron3d+0x9d28a treats a ratio outside [0.01, 1] as depleted. + let low = weapon.charge.as_ref().is_some_and(|charge| { + charge.capacity > 0.0 + && !(0.01..=1.0).contains(&(charge.amount / charge.capacity)) + }); + let previous = std::mem::replace(&mut weapon.low_charge_reported, low); + (low && !previous).then_some(slot) + }) + } + /// Control sound instances whose wind-up began on this sample. pub fn started_windup_sounds( &self, @@ -542,6 +559,7 @@ fn prepare_weapons( component_index, control_record_index, charge: None, + low_charge_reported: false, destroyed: false, energy_waits: 0, autonomous_fire_distance: f32::from_bits(record.fixed_words[19]), @@ -832,6 +850,7 @@ mod tests { component_index: 0, control_record_index: 0, charge: None, + low_charge_reported: false, destroyed: false, energy_waits: 0, autonomous_fire_distance: 0.0, @@ -937,5 +956,18 @@ mod tests { weapons.weapons[0].charge.as_mut().unwrap().amount = 4.0; assert!(weapons.fire(true, 10_000).next().is_none()); assert!(weapons.fire(true, 10_011).next().is_some()); + weapons.weapons[0].charge = Some(crate::mobile_budget::Charge { + capacity: 1.0, + cost: 0.1, + amount: 0.01, + }); + assert_eq!(weapons.take_low_charge_warnings().next(), None); + weapons.weapons[0].charge.as_mut().unwrap().amount = f32::from_bits(0.01_f32.to_bits() - 1); + assert_eq!(weapons.take_low_charge_warnings().next(), Some(0)); + assert_eq!(weapons.take_low_charge_warnings().next(), None); + weapons.weapons[0].charge.as_mut().unwrap().amount = 0.01; + assert_eq!(weapons.take_low_charge_warnings().next(), None); + weapons.weapons[0].charge.as_mut().unwrap().amount = 0.0; + assert_eq!(weapons.take_low_charge_warnings().next(), Some(0)); } } |
