From 72d0ef912751f621d11d7d045e506904003c0b9a Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 08:25:17 +0400 Subject: fix(game): stop firing when weapon ammunition runs out --- crates/fparkan-runtime/src/lib.rs | 8 +++++++ crates/fparkan-runtime/src/player_weapon.rs | 33 +++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index ff0cdf69..78949222 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -17238,6 +17238,14 @@ mod tests { .map(|weapon| weapon.projectile.as_str()) .collect::>(); assert_eq!(projectiles, ["bb_h_01", "bp_h_01", "bl_h_01", "bm_h_01"]); + assert_eq!( + weapons + .weapons() + .iter() + .map(player_weapon::PlayerWeapon::rounds_remaining) + .collect::>(), + [Some(500), Some(150), None, Some(4)] + ); for (weapon, (speed, lifetime_ms)) in weapons.weapons().iter().zip([ (350.0, 1000), (150.0, 5000), diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index 045aad5b..6e055a43 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -20,6 +20,7 @@ pub struct PlayerWeapon { pub muzzle: PreparedControlPoint, /// Optional type-4 effect started by this weapon. pub effect: Option<(String, String)>, + rounds_remaining: Option, interval_ms: u32, last_fire_ms: Option, } @@ -51,7 +52,10 @@ impl PlayerWeapons { .iter_mut() .enumerate() .filter_map(move |(index, weapon)| { - if !trigger || selected.is_some_and(|selected| selected != index) { + if !trigger + || selected.is_some_and(|selected| selected != index) + || weapon.rounds_remaining == Some(0) + { return None; } if weapon @@ -61,12 +65,20 @@ impl PlayerWeapons { return None; } weapon.last_fire_ms = Some(clock_ms); + if let Some(rounds) = &mut weapon.rounds_remaining { + *rounds -= 1; + } Some((index, &*weapon)) }) } } impl PlayerWeapon { + /// Remaining shots, or `None` for an unlimited weapon. + pub fn rounds_remaining(&self) -> Option { + self.rounds_remaining + } + /// Smallest separation between consecutive shots on the current queue clock. pub fn interval_ms(&self) -> u32 { self.interval_ms @@ -124,6 +136,14 @@ fn prepare_weapons(control: &PreparedComponentControl) -> Vec { if !interval_ms.is_finite() || interval_ms < 0.0 { return None; } + let rounds = f32::from_bits(record.fixed_words[11]).round_ties_even(); + let rounds_remaining = if rounds == -1.0 { + None + } else if (0.0..2_147_483_648.0).contains(&rounds) { + Some(rounds as u32) + } else { + return None; + }; let effect = match &control.effect_section { PreparedControlEffectSection::Parsed(rows) => control .node_effect_links @@ -149,6 +169,7 @@ fn prepare_weapons(control: &PreparedComponentControl) -> Vec { projectile_archive, muzzle: muzzle.clone(), effect, + rounds_remaining, // shortcut: begin the next cycle after the two configured // waits and a 60 Hz step for each of the four native phases. interval_ms: (interval_ms + 4.0 * (1000.0 / 60.0)).ceil() as u32, @@ -166,8 +187,8 @@ mod tests { fn trigger_respects_selection_and_independent_weapon_waits() { let mut weapons = PlayerWeapons { selected: None, - weapons: [100, 250] - .map(|interval_ms| PlayerWeapon { + weapons: [(100, Some(2)), (250, None)] + .map(|(interval_ms, rounds_remaining)| PlayerWeapon { projectile: String::from("test"), projectile_archive: String::from("test.rlb"), muzzle: PreparedControlPoint { @@ -179,6 +200,7 @@ mod tests { name: [0; 32], }, effect: None, + rounds_remaining, interval_ms, last_fire_ms: None, }) @@ -194,10 +216,13 @@ mod tests { assert_eq!(fired(true, 0), [0, 1]); assert!(fired(true, 99).is_empty()); assert_eq!(fired(true, 100), [0]); + assert_eq!(weapons.weapons()[0].rounds_remaining(), Some(0)); weapons.select(2); assert!(weapons.fire(true, 249).next().is_none()); assert_eq!(weapons.fire(true, 250).next().unwrap().0, 1); weapons.select(0); - assert_eq!(weapons.fire(true, 251).next().unwrap().0, 0); + assert!(weapons.fire(true, 251).next().is_none()); + assert_eq!(weapons.fire(true, 500).next().unwrap().0, 1); + assert_eq!(weapons.weapons()[1].rounds_remaining(), None); } } -- cgit v1.2.3