aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/cockpit_instruments.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src/cockpit_instruments.rs')
-rw-r--r--crates/fparkan-runtime/src/cockpit_instruments.rs80
1 files changed, 68 insertions, 12 deletions
diff --git a/crates/fparkan-runtime/src/cockpit_instruments.rs b/crates/fparkan-runtime/src/cockpit_instruments.rs
index 655dfa80..6aa2e3f6 100644
--- a/crates/fparkan-runtime/src/cockpit_instruments.rs
+++ b/crates/fparkan-runtime/src/cockpit_instruments.rs
@@ -3,30 +3,61 @@
use crate::{
control_device_provider_value, control_device_selector_zero,
projectile::unit_sphere,
+ query_control_device_table_selector,
strategic_map_readout::{contact_readout, ContactReadout},
- Engine, MissionControlDeviceTableStatus, StartingCockpit,
+ Engine, MissionControlDeviceTableStatus, StartingCockpit, HERO_UNIT_CLASS,
};
-use fparkan_world::transform_state;
+use fparkan_world::{identity_metadata, transform_state, ObjectHandle};
-/// Main instrument for the controlled object.
+/// Data shared by the controlled object and the selected target instruments.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct OwnedInstrument {
- pub object: ContactReadout,
+pub struct InstrumentReadout {
+ pub object_index: usize,
+ pub object: Option<ContactReadout>,
+ pub status: Option<InstrumentStatus>,
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)?;
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum InstrumentStatus {
+ Order(u32),
+ Dangerous,
+}
+
+pub fn instrument_readout(
+ engine: &Engine,
+ cockpit: StartingCockpit,
+ subject: ObjectHandle,
+) -> Option<InstrumentReadout> {
+ let identity = identity_metadata(&engine.world, subject).ok()?;
+ let object = contact_readout(engine, cockpit, subject);
+ let operator = identity_metadata(&engine.world, cockpit.handle).ok()?;
let table = engine
.loaded
.as_ref()?
.control_device_tables
.iter()
- .find(|table| table.handle == cockpit.handle)?;
+ .find(|table| table.handle == subject)?;
let MissionControlDeviceTableStatus::Available(devices) = &table.status else {
return None;
};
+ let status = object
+ .filter(|object| object.class != HERO_UNIT_CLASS && object.class & 0x8000_0000 == 0)
+ .and_then(|object| {
+ if identity.owner_id == operator.owner_id {
+ return object.order_label.map(InstrumentStatus::Order);
+ }
+ // iron3d+0x76b40 checks the operational provider and selector 6.
+ let dangerous = devices.iter().enumerate().any(|(index, device)| {
+ device.descriptor.tag == 2
+ && control_device_provider_value(table, device)
+ .is_ok_and(|provider| provider > 0.0)
+ && query_control_device_table_selector(table, index, 6)
+ .is_ok_and(|value| value.is_some_and(|value| value > 10_000.0))
+ });
+ dangerous.then_some(InstrumentStatus::Dangerous)
+ });
let batteries = devices
.iter()
.filter(|device| device.descriptor.tag == 19)
@@ -47,18 +78,35 @@ pub fn owned_instrument(engine: &Engine, cockpit: StartingCockpit) -> Option<Own
.clamp(0.0, 255.0) as u8
}))
})();
- Some(OwnedInstrument {
+ Some(InstrumentReadout {
+ object_index: identity.original_id?.0 as usize,
object,
+ status,
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)> {
+pub fn instrument_sphere(
+ engine: &Engine,
+ subject: ObjectHandle,
+ object_index: usize,
+) -> 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)
+ let pose = transform_state(&engine.world, subject).ok()?;
+ unit_sphere(&mission.mission_assets, object_index, pose)
+}
+
+/// Targeter distance is the 3D separation of the two centers in world units.
+pub fn instrument_distance(origin: [f32; 3], target: [f32; 3]) -> Option<u32> {
+ let distance = origin
+ .into_iter()
+ .zip(target)
+ .map(|(a, b)| (f64::from(a) - f64::from(b)).powi(2))
+ .sum::<f64>()
+ .sqrt() as f32;
+ (distance.is_finite() && distance > 0.0).then(|| distance.round_ties_even() as u32)
}
fn battery_reading<E>(batteries: impl Iterator<Item = Result<f32, E>>) -> Result<f32, E> {
@@ -82,3 +130,11 @@ fn battery_reading_uses_the_native_sentinel_and_sum() {
assert_eq!(reading(&[0.25, 0.5]), Ok(0.75));
assert_eq!(reading(&[0.25, -1.0, 1.0]), Ok(-1.0));
}
+
+#[cfg(test)]
+#[test]
+fn targeter_distance_includes_height_and_rounds_to_even() {
+ assert_eq!(instrument_distance([0.0; 3], [3.0, 0.0, 4.0]), Some(5));
+ assert_eq!(instrument_distance([0.0; 3], [0.0, 0.0, 4.5]), Some(4));
+ assert_eq!(instrument_distance([0.0; 3], [0.0; 3]), None);
+}