aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 19:26:18 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 19:26:18 +0300
commit29c908ed848f2a85853764d7212c85fc886cd471 (patch)
treebb6f035f73c8e2d939c6b60d97b194a30b6f6a02 /crates/fparkan-runtime/src
parent1fd51e5bbcfb4d5590fbc1ded8308fc40f6158f4 (diff)
downloadfparkan-29c908ed848f2a85853764d7212c85fc886cd471.tar.xz
fparkan-29c908ed848f2a85853764d7212c85fc886cd471.zip
feat(ui): show the controlled unit in the cockpit instrument
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/cockpit_instruments.rs84
-rw-r--r--crates/fparkan-runtime/src/lib.rs1
-rw-r--r--crates/fparkan-runtime/src/strategic_map_readout.rs8
3 files changed, 93 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));
+}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 2cf1f62e..a432ed17 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -112,6 +112,7 @@ pub use destruction::{
unit_destruction_effect_sources, DestroyedMesh, UnitDestructionEffect,
};
pub mod cockpit_altimeter;
+pub mod cockpit_instruments;
mod flight_altitude;
mod ground_support;
mod laboratory;
diff --git a/crates/fparkan-runtime/src/strategic_map_readout.rs b/crates/fparkan-runtime/src/strategic_map_readout.rs
index 027dd83f..00eae097 100644
--- a/crates/fparkan-runtime/src/strategic_map_readout.rs
+++ b/crates/fparkan-runtime/src/strategic_map_readout.rs
@@ -28,6 +28,14 @@ pub fn contact_at(
point: [f32; 2],
) -> Option<ContactReadout> {
let handle = satellite_contacts::pick_contact(engine, cockpit, point)?;
+ contact_readout(engine, cockpit, handle)
+}
+
+pub(super) fn contact_readout(
+ engine: &Engine,
+ cockpit: StartingCockpit,
+ handle: fparkan_world::ObjectHandle,
+) -> Option<ContactReadout> {
let mission = engine.loaded.as_ref()?;
let player = identity_metadata(&engine.world, cockpit.handle)
.ok()?