From 09c2d0778d5ecffb1c575883232f57aeeb5ac3f0 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 20:59:56 +0400 Subject: feat(ui): show the selected target in the left cockpit instrument --- crates/fparkan-assets/src/cockpit_instrument.rs | 125 ++++++++++++++++----- crates/fparkan-runtime/src/cockpit_instruments.rs | 80 +++++++++++-- .../fparkan-runtime/src/strategic_map_readout.rs | 6 +- 3 files changed, 169 insertions(+), 42 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-assets/src/cockpit_instrument.rs b/crates/fparkan-assets/src/cockpit_instrument.rs index 5114d0b1..c5f21a32 100644 --- a/crates/fparkan-assets/src/cockpit_instrument.rs +++ b/crates/fparkan-assets/src/cockpit_instrument.rs @@ -1,4 +1,4 @@ -//! Composition of the right cockpit instrument from the original UI sheets. +//! Composition of the cockpit instruments from the original UI sheets. use crate::{ game_font::{load_layout_font, BitmapFont}, @@ -9,14 +9,44 @@ use crate::{ }; use fparkan_vfs::Vfs; -pub const FRAME: [f32; 4] = [490.0, 306.0, 640.0, 480.0]; -pub const MODEL: [f32; 4] = [503.0, 315.0, 631.0, 443.0]; const METER_HEIGHT: u32 = 94; +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Side { + Left, + Right, +} + +impl Side { + pub fn frame(self) -> [f32; 4] { + match self { + Self::Left => [0.0, 306.0, 150.0, 480.0], + Self::Right => [490.0, 306.0, 640.0, 480.0], + } + } + + pub fn model_viewport(self) -> [f32; 4] { + match self { + Self::Left => [9.0, 315.0, 137.0, 443.0], + Self::Right => [503.0, 315.0, 631.0, 443.0], + } + } +} + +pub struct Readout<'a> { + pub name: &'a str, + pub status: Option<(&'a str, [u8; 3])>, + pub distance: Option<&'a str>, + pub durability: Option, + pub energy: u8, + pub shield: Option<[u8; 6]>, +} + pub struct Images { bezel: RgbaImage, life: RgbaImage, energy: RgbaImage, + range: RgbaImage, shields: [RgbaImage; 5], page: RgbaImage, font: BitmapFont, @@ -24,13 +54,14 @@ pub struct Images { impl Images { pub fn load(vfs: &dyn Vfs) -> Result { - let [bezel, life, energy, side, front, back, top, bottom] = load_ui_sprites( + let [bezel, life, energy, range, side, front, back, top, bottom] = load_ui_sprites( vfs, "ui/hq.cfg", [ "targeter_back", "targeter_life", "targeter_energy", + "targeter_range", "left_shld", "frwd_shld", "back_shld", @@ -44,56 +75,73 @@ impl Images { bezel, life, energy, + range, shields: [side, front, back, top, bottom], page, font, }) } - pub fn paint( - &self, - name: &str, - durability: Option, - energy: u8, - shield: Option<[u8; 6]>, - ) -> Result { + pub fn paint(&self, side: Side, readout: Option>) -> Result { let (width, height) = (self.bezel.width, self.bezel.height); let mut image = RgbaImage { width, height, rgba8: vec![0; (width * height * 4) as usize], }; + let right = side == Side::Right; + let meter_rect = |[x0, y0, x1, y1]: [u32; 4]| { + if right { + [x0, y0, x1, y1] + } else { + [150 - x1, y0, 150 - x0, y1] + } + }; copy_sprite( &mut image, &self.bezel, [0, 0, width, height], - [true, false], + [right, false], [255; 3], ); - stamp(&mut image, &self.life, [6, 105, 21, 120], [255; 3]); - stamp(&mut image, &self.energy, [130, 105, 145, 120], [255; 3]); - if let Some([front, back, left, right, top, bottom]) = shield { + stamp( + &mut image, + &self.life, + meter_rect([6, 105, 21, 120]), + [255; 3], + ); + stamp( + &mut image, + &self.energy, + meter_rect([130, 105, 145, 120]), + [255; 3], + ); + let Some(readout) = readout else { + return Ok(image); + }; + if let Some([front, back, left, right_shield, top, bottom]) = readout.shield { let [side_image, front_image, back_image, top_image, bottom_image] = &self.shields; - for (sprite, rect, flip, value) in [ - (side_image, [17, 28, 43, 107], false, left), - (side_image, [108, 28, 134, 107], true, right), - (front_image, [45, 15, 106, 34], false, front), - (back_image, [31, 103, 120, 132], false, back), - (top_image, [41, 2, 110, 21], false, top), - (bottom_image, [23, 114, 128, 145], false, bottom), + for (sprite, [x0, y0, x1, y1], flip, value) in [ + (side_image, [16, 28, 42, 107], false, left), + (side_image, [107, 28, 133, 107], true, right_shield), + (front_image, [44, 15, 105, 34], false, front), + (back_image, [30, 103, 119, 132], false, back), + (top_image, [40, 2, 109, 21], false, top), + (bottom_image, [22, 114, 127, 145], false, bottom), ] { + let shift = u32::from(right); copy_sprite( &mut image, sprite, - rect, + [x0 + shift, y0, x1 + shift, y1], [flip, false], [255 - value, value, 0], ); } } for (percent, x, flip, tint) in [ - (durability.unwrap_or(0), 1, false, [25, 255, 175]), - (i32::from(energy), 110, true, [255, 180, 80]), + (readout.durability.unwrap_or(0), 1, false, [25, 255, 175]), + (i32::from(readout.energy), 110, true, [255, 180, 80]), ] { let cut = meter_cut(percent); if cut == METER_HEIGHT { @@ -104,12 +152,33 @@ impl Images { copy_sprite( &mut image, &strip, - [x, 8 + cut, x + 40, 102], - [flip, false], + meter_rect([x, 8 + cut, x + 40, 102]), + [flip == right, false], tint, ); } - text(&self.font, &mut image, name, [4, 153, 142, 164], [255; 3])?; + text( + &self.font, + &mut image, + readout.name, + [4, 153, 142, 164], + [255; 3], + )?; + if let Some((status, tint)) = readout.status { + text(&self.font, &mut image, status, [4, 163, 142, 174], tint)?; + } + if side == Side::Left { + stamp(&mut image, &self.range, [108, 134, 147, 150], [255; 3]); + } + if let Some(distance) = readout.distance { + text( + &self.font, + &mut image, + distance, + [112, 138, 143, 149], + [0, 255, 0], + )?; + } Ok(image) } } 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, + pub status: Option, pub energy_percent: u8, pub shield: Option<[u8; 6]>, } -pub fn owned_instrument(engine: &Engine, cockpit: StartingCockpit) -> Option { - 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 { + 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 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 { + let distance = origin + .into_iter() + .zip(target) + .map(|(a, b)| (f64::from(a) - f64::from(b)).powi(2)) + .sum::() + .sqrt() as f32; + (distance.is_finite() && distance > 0.0).then(|| distance.round_ties_even() as u32) } fn battery_reading(batteries: impl Iterator>) -> Result { @@ -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); +} diff --git a/crates/fparkan-runtime/src/strategic_map_readout.rs b/crates/fparkan-runtime/src/strategic_map_readout.rs index 00eae097..6b633433 100644 --- a/crates/fparkan-runtime/src/strategic_map_readout.rs +++ b/crates/fparkan-runtime/src/strategic_map_readout.rs @@ -29,6 +29,7 @@ pub fn contact_at( ) -> Option { let handle = satellite_contacts::pick_contact(engine, cockpit, point)?; contact_readout(engine, cockpit, handle) + .filter(|contact| contact.class != crate::projectile::ANIMAL_UNIT_CLASS) } pub(super) fn contact_readout( @@ -53,7 +54,9 @@ pub(super) fn contact_readout( .get(&original) .and_then(|flags| flags.chassis_type) .unwrap_or(0); - let (name_label, callsign, order_label) = if building { + let (name_label, callsign, order_label) = if class == crate::projectile::ANIMAL_UNIT_CLASS { + (0x186d, None, None) + } else if building { (building_label(class, size), None, None) } else { let (role, label) = match class { @@ -62,7 +65,6 @@ pub(super) fn contact_readout( 0x0100_8000 => ('W', 0x183a), 0x0101_0000 => ('C', 0x183b), HERO_UNIT_CLASS => ('?', 0x1856), - // Animals have map marks but no footer. _ => return None, }; let callsign = (class != HERO_UNIT_CLASS) -- cgit v1.2.3