diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-assets/src/cockpit_instrument.rs | 128 | ||||
| -rw-r--r-- | crates/fparkan-assets/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/cockpit_instruments.rs | 84 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/strategic_map_readout.rs | 8 |
5 files changed, 222 insertions, 0 deletions
diff --git a/crates/fparkan-assets/src/cockpit_instrument.rs b/crates/fparkan-assets/src/cockpit_instrument.rs new file mode 100644 index 00000000..5114d0b1 --- /dev/null +++ b/crates/fparkan-assets/src/cockpit_instrument.rs @@ -0,0 +1,128 @@ +//! Composition of the right cockpit instrument from the original UI sheets. + +use crate::{ + game_font::{load_layout_font, BitmapFont}, + mission_pager::copy_sprite, + strategic_factory::{stamp, text}, + ui_sprite::{atlas_region, load_ui_page, load_ui_sprites}, + RgbaImage, +}; +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; + +pub struct Images { + bezel: RgbaImage, + life: RgbaImage, + energy: RgbaImage, + shields: [RgbaImage; 5], + page: RgbaImage, + font: BitmapFont, +} + +impl Images { + pub fn load(vfs: &dyn Vfs) -> Result<Self, String> { + let [bezel, life, energy, side, front, back, top, bottom] = load_ui_sprites( + vfs, + "ui/hq.cfg", + [ + "targeter_back", + "targeter_life", + "targeter_energy", + "left_shld", + "frwd_shld", + "back_shld", + "top_shld", + "bott_shld", + ], + )?; + let page = load_ui_page(vfs, "ui_menu3")?; + let font = load_layout_font(vfs)?; + Ok(Self { + bezel, + life, + energy, + shields: [side, front, back, top, bottom], + page, + font, + }) + } + + pub fn paint( + &self, + name: &str, + durability: Option<i32>, + energy: u8, + shield: Option<[u8; 6]>, + ) -> Result<RgbaImage, String> { + let (width, height) = (self.bezel.width, self.bezel.height); + let mut image = RgbaImage { + width, + height, + rgba8: vec![0; (width * height * 4) as usize], + }; + copy_sprite( + &mut image, + &self.bezel, + [0, 0, width, height], + [true, 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 { + 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), + ] { + copy_sprite( + &mut image, + sprite, + rect, + [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]), + ] { + let cut = meter_cut(percent); + if cut == METER_HEIGHT { + continue; + } + let strip = atlas_region(&self.page, [151, 82 + cut, 40, METER_HEIGHT - cut]) + .ok_or("cockpit meter outside ui_menu3")?; + copy_sprite( + &mut image, + &strip, + [x, 8 + cut, x + 40, 102], + [flip, false], + tint, + ); + } + text(&self.font, &mut image, name, [4, 153, 142, 164], [255; 3])?; + Ok(image) + } +} + +fn meter_cut(percent: i32) -> u32 { + (100 - percent.clamp(0, 100)) as u32 * METER_HEIGHT / 100 +} + +#[cfg(test)] +#[test] +fn the_native_bar_fills_upward_in_integer_pixels() { + assert_eq!(meter_cut(-1), 94); + assert_eq!(meter_cut(25), 70); + assert_eq!(meter_cut(99), 0); + assert_eq!(meter_cut(120), 0); +} diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 6d21e70b..c728f140 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -31,6 +31,7 @@ pub use fparkan_texm::{PageRect, RgbaImage}; use fparkan_vfs::DirectoryVfs; mod behavior_profile_values; mod chassis_profile; +pub mod cockpit_instrument; pub mod cockpit_reticle; mod control_drive; mod control_weapon_sound; 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()? |
