diff options
| author | Valentin Popov <valentin@popov.link> | 2026-12-01 17:38:40 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-12-01 17:38:40 +0300 |
| commit | 5f4850555ac2fd62009cffd5e98ea04c081bbaf7 (patch) | |
| tree | 3345de4620581dc074e59b2727e174102671f289 /crates/fparkan-runtime/src/player_weapon.rs | |
| parent | 18be5a8bf2249e2cff89038e8180e9db9581d71b (diff) | |
| download | fparkan-5f4850555ac2fd62009cffd5e98ea04c081bbaf7.tar.xz fparkan-5f4850555ac2fd62009cffd5e98ea04c081bbaf7.zip | |
feat(audio): announce exhausted weapon ammunition
Diffstat (limited to 'crates/fparkan-runtime/src/player_weapon.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/player_weapon.rs | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index 914201c3..e9d748e6 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -21,6 +21,15 @@ enum WeaponSelection { Slot(usize), } +/// Cockpit announcements triggered by a weapon's current supply. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum PlayerWeaponWarning { + /// A weapon with finite ammunition has run out of rounds. + AmmoEmpty, + /// The charge for the weapon has dropped below the cockpit threshold. + LowCharge, +} + /// A manual-fire device prepared from its CTLD record and effect rows. #[derive(Clone)] pub struct PlayerWeapon { @@ -28,6 +37,7 @@ pub struct PlayerWeapon { pub component_index: usize, pub(super) control_record_index: usize, pub(super) charge: Option<crate::mobile_budget::Charge>, + ammo_empty_reported: bool, low_charge_reported: bool, pub(super) destroyed: bool, pub(super) energy_waits: u64, @@ -92,19 +102,26 @@ 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> + '_ { + /// Weapon slots and warnings that have just become active in the cockpit. + pub fn take_warnings(&mut self) -> impl Iterator<Item = (usize, PlayerWeaponWarning)> + '_ { self.weapons .iter_mut() .enumerate() - .filter_map(|(slot, weapon)| { + .flat_map(|(slot, weapon)| { + let empty = weapon.rounds_remaining == Some(0); + let was_empty = std::mem::replace(&mut weapon.ammo_empty_reported, empty); // 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) + let was_low = std::mem::replace(&mut weapon.low_charge_reported, low); + [ + (empty && !was_empty).then_some((slot, PlayerWeaponWarning::AmmoEmpty)), + (low && !was_low).then_some((slot, PlayerWeaponWarning::LowCharge)), + ] + .into_iter() + .flatten() }) } @@ -559,6 +576,7 @@ fn prepare_weapons( component_index, control_record_index, charge: None, + ammo_empty_reported: false, low_charge_reported: false, destroyed: false, energy_waits: 0, @@ -850,6 +868,7 @@ mod tests { component_index: 0, control_record_index: 0, charge: None, + ammo_empty_reported: false, low_charge_reported: false, destroyed: false, energy_waits: 0, @@ -942,14 +961,21 @@ mod tests { assert_eq!(weapons.firing_effect_phases(7).next(), Some((effect, 0.75))); assert_eq!(weapons.weapons()[0].rounds_remaining(), Some(1)); assert_eq!(weapons.weapons[0].charge.as_ref().unwrap().amount, 8.0); + assert_eq!(weapons.take_warnings().next(), None); assert!(weapons.fire(false, 22).next().is_none()); weapons.apply_firing_pose(0, &mut node_times); assert_eq!(node_times, [Some(4.0)]); assert_eq!(weapons.firing_effect_phases(7).next(), Some((effect, 0.0))); assert_eq!(weapons.weapons()[0].rounds_remaining(), Some(0)); assert_eq!(weapons.weapons[0].charge.as_ref().unwrap().amount, 4.0); + assert_eq!( + weapons.take_warnings().next(), + Some((0, PlayerWeaponWarning::AmmoEmpty)) + ); + assert_eq!(weapons.take_warnings().next(), None); assert!(weapons.fire(true, 100).next().is_none()); weapons.weapons[0].rounds_remaining = None; + assert_eq!(weapons.take_warnings().next(), None); weapons.weapons[0].charge.as_mut().unwrap().amount = 0.0; assert!(weapons.fire(true, 150).next().is_none()); assert!(weapons.weapons[0].energy_waits > 0); @@ -961,13 +987,23 @@ mod tests { cost: 0.1, amount: 0.01, }); - assert_eq!(weapons.take_low_charge_warnings().next(), None); + assert_eq!(weapons.take_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); + assert_eq!( + weapons.take_warnings().next(), + Some((0, PlayerWeaponWarning::LowCharge)) + ); + assert_eq!(weapons.take_warnings().next(), None); weapons.weapons[0].charge.as_mut().unwrap().amount = 0.01; - assert_eq!(weapons.take_low_charge_warnings().next(), None); + assert_eq!(weapons.take_warnings().next(), None); weapons.weapons[0].charge.as_mut().unwrap().amount = 0.0; - assert_eq!(weapons.take_low_charge_warnings().next(), Some(0)); + weapons.weapons[0].rounds_remaining = Some(0); + assert_eq!( + weapons.take_warnings().collect::<Vec<_>>(), + [ + (0, PlayerWeaponWarning::AmmoEmpty), + (0, PlayerWeaponWarning::LowCharge), + ] + ); } } |
