diff options
Diffstat (limited to 'crates/fparkan-runtime/src/cockpit_instruments.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/cockpit_instruments.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/cockpit_instruments.rs b/crates/fparkan-runtime/src/cockpit_instruments.rs new file mode 100644 index 00000000..655dfa80 --- /dev/null +++ b/crates/fparkan-runtime/src/cockpit_instruments.rs @@ -0,0 +1,84 @@ +//! Live Control values used by the cockpit's unit instruments. + +use crate::{ + control_device_provider_value, control_device_selector_zero, + projectile::unit_sphere, + strategic_map_readout::{contact_readout, ContactReadout}, + Engine, MissionControlDeviceTableStatus, StartingCockpit, +}; +use fparkan_world::transform_state; + +/// Main instrument for the controlled object. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct OwnedInstrument { + pub object: ContactReadout, + pub energy_percent: u8, + pub shield: Option<[u8; 6]>, +} + +pub fn owned_instrument(engine: &Engine, cockpit: StartingCockpit) -> Option<OwnedInstrument> { + let object = contact_readout(engine, cockpit, cockpit.handle)?; + let table = engine + .loaded + .as_ref()? + .control_device_tables + .iter() + .find(|table| table.handle == cockpit.handle)?; + let MissionControlDeviceTableStatus::Available(devices) = &table.status else { + return None; + }; + let batteries = devices + .iter() + .filter(|device| device.descriptor.tag == 19) + .map(control_device_selector_zero); + let ratio = battery_reading(batteries).ok()?; + let energy_percent = (f64::from(ratio) * 100.0) + .round_ties_even() + .clamp(0.0, 100.0) as u8; + let shield = (|| { + let field = devices.iter().rfind(|device| device.factory_tag == 9)?; + let provider = devices.iter().rfind(|device| device.factory_tag == 21)?; + let multiplier = f32::from_bits(provider.multiplier_bits?); + let integrity = control_device_provider_value(table, provider).ok()?; + Some(field.class9_values_bits?.map(|bits| { + let ratio = f32::from_bits(bits) * multiplier * integrity; + (f64::from(ratio) * 255.0) + .round_ties_even() + .clamp(0.0, 255.0) as u8 + })) + })(); + Some(OwnedInstrument { + object, + energy_percent, + shield, + }) +} + +/// The same root sphere used for the live object's spatial queries. +pub fn instrument_sphere(engine: &Engine, cockpit: StartingCockpit) -> Option<([f32; 3], f32)> { + let mission = engine.loaded.as_ref()?; + let pose = transform_state(&engine.world, cockpit.handle).ok()?; + unit_sphere(&mission.mission_assets, cockpit.object_index, pose) +} + +fn battery_reading<E>(batteries: impl Iterator<Item = Result<f32, E>>) -> Result<f32, E> { + let mut total = 0.0; + // Control+0x2b410 (selector 1) returns the first negative descriptor as-is. + for battery in batteries { + let charge = battery?; + if charge < 0.0 { + return Ok(charge); + } + total += charge; + } + Ok(total) +} + +#[cfg(test)] +#[test] +fn battery_reading_uses_the_native_sentinel_and_sum() { + let reading = |values: &[f32]| battery_reading(values.iter().copied().map(Ok::<_, ()>)); + assert_eq!(reading(&[]), Ok(0.0)); + assert_eq!(reading(&[0.25, 0.5]), Ok(0.75)); + assert_eq!(reading(&[0.25, -1.0, 1.0]), Ok(-1.0)); +} |
