diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_weapon.rs | 94 |
2 files changed, 90 insertions, 5 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 2b148397..a53e32f0 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -193,6 +193,7 @@ pub use player_motion::{ pub use player_status::{is_player_clan, player_status, PlayerStatus}; pub use player_weapon::{ prepared_warbot_weapons, starting_player_weapons, PlayerWeaponWarning, PlayerWeapons, + WeaponIndicator, WeaponLamp, }; pub use production::{ clan_factory_schemes, factory_schemes, order_clan_factory_scheme, order_factory_robot, diff --git a/crates/fparkan-runtime/src/player_weapon.rs b/crates/fparkan-runtime/src/player_weapon.rs index 347e0450..84408c61 100644 --- a/crates/fparkan-runtime/src/player_weapon.rs +++ b/crates/fparkan-runtime/src/player_weapon.rs @@ -21,6 +21,33 @@ enum WeaponSelection { Slot(usize), } +impl WeaponSelection { + fn includes(self, index: usize) -> bool { + match self { + Self::All => true, + Self::StandardSuit => matches!(index, 0 | 2), + Self::Slot(slot) => slot == index, + } + } +} + +/// The lamp, ammunition and capacitor reading for one cockpit weapon slot. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct WeaponIndicator { + pub rounds: Option<u32>, + pub charge_percent: u8, + pub lamp: WeaponLamp, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum WeaponLamp { + Unavailable, + Reloading, + Cycling, + Ready, + Unselected, +} + /// Cockpit announcements triggered by a weapon's current supply. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum PlayerWeaponWarning { @@ -83,6 +110,47 @@ enum FirePhase { } impl PlayerWeapons { + /// Read the same slot, cycle and budget that drive manual firing. + pub fn indicators(&self) -> impl Iterator<Item = WeaponIndicator> + '_ { + self.weapons.iter().enumerate().map(|(slot, weapon)| { + let ratio = weapon + .charge + .as_ref() + .filter(|charge| charge.capacity > 0.0) + .map_or(1.0, |charge| charge.amount / charge.capacity); + let charge_percent = if (0.0..=1.0).contains(&ratio) { + (f64::from(ratio) * 100.0).round_ties_even() as u8 + } else { + 0 + }; + // iron3d+0x9ccd0 selects the compound-control lamp from Control's status. + let lamp = if !self.selection.includes(slot) { + WeaponLamp::Unselected + } else if weapon.destroyed || weapon.rounds_remaining == Some(0) { + WeaponLamp::Unavailable + } else { + match weapon.phase { + FirePhase::Idle + if weapon + .charge + .as_ref() + .is_some_and(|charge| !charge.can_start()) => + { + WeaponLamp::Reloading + } + FirePhase::Idle => WeaponLamp::Ready, + FirePhase::Reload => WeaponLamp::Reloading, + FirePhase::Shot | FirePhase::Recover | FirePhase::Finish => WeaponLamp::Cycling, + } + }; + WeaponIndicator { + rounds: weapon.rounds_remaining, + charge_percent, + lamp, + } + }) + } + /// Reads the authoritative Control charge and ammunition before firing. pub fn read_control_energy(&mut self, engine: &Engine, handle: crate::ObjectHandle) { if let Some(mission) = &engine.loaded { @@ -232,11 +300,7 @@ impl PlayerWeapons { weapon.windup_started = false; return None; } - let selected = match selection { - WeaponSelection::All => true, - WeaponSelection::StandardSuit => matches!(index, 0 | 2), - WeaponSelection::Slot(slot) => slot == index, - }; + let selected = selection.includes(index); let mut start = selected && trigger(index, weapon, &weapon.barrels[weapon.barrel_index].muzzle); if let Some(deployment) = &mut weapon.deployment { @@ -917,8 +981,20 @@ mod tests { cost: 4.0, amount: 8.0, }); + assert_eq!( + weapons.indicators().next(), + Some(WeaponIndicator { + rounds: Some(1), + charge_percent: 80, + lamp: WeaponLamp::Ready, + }) + ); let mut node_times = [None]; assert!(weapons.fire(true, 0).next().is_none()); + assert_eq!( + weapons.indicators().next().unwrap().lamp, + WeaponLamp::Cycling + ); let mut interrupted = weapons.clone(); interrupted.weapons[0].destroyed = true; assert!(interrupted.fire(true, 11).next().is_none()); @@ -965,6 +1041,14 @@ mod tests { 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.indicators().next(), + Some(WeaponIndicator { + rounds: Some(0), + charge_percent: 40, + lamp: WeaponLamp::Unavailable, + }) + ); assert_eq!(weapons.weapons[0].charge.as_ref().unwrap().amount, 4.0); assert_eq!( weapons.take_warnings().next(), |
