From 7dfbf534267011ad629c11c7140d0f9c0fe9cd3b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 2 Dec 2026 10:44:41 +0400 Subject: feat(ui): show the original strategic resource balances --- crates/fparkan-assets/src/lib.rs | 1 + crates/fparkan-assets/src/strategic_balance.rs | 130 ++++++++++++++++++++++++ crates/fparkan-runtime/src/lib.rs | 1 + crates/fparkan-runtime/src/strategic_balance.rs | 103 +++++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100644 crates/fparkan-assets/src/strategic_balance.rs create mode 100644 crates/fparkan-runtime/src/strategic_balance.rs (limited to 'crates') diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs index 5bdd7579..d64425ef 100644 --- a/crates/fparkan-assets/src/lib.rs +++ b/crates/fparkan-assets/src/lib.rs @@ -46,6 +46,7 @@ pub mod mission_loader; pub mod mission_pager; pub mod object_descriptions; pub mod satellite_map; +pub mod strategic_balance; pub mod strategy_toolbar; mod turret_elevation; mod ui_configuration; diff --git a/crates/fparkan-assets/src/strategic_balance.rs b/crates/fparkan-assets/src/strategic_balance.rs new file mode 100644 index 00000000..eeebbce0 --- /dev/null +++ b/crates/fparkan-assets/src/strategic_balance.rs @@ -0,0 +1,130 @@ +//! Resource balance rows assembled from the original compound controls. + +use crate::{game_font::load_layout_font, mission_pager::copy_sprite, ui_sprite::load_ui_sprites}; +use fparkan_font::BitmapFont; +use fparkan_texm::RgbaImage; +use fparkan_vfs::Vfs; + +const ROW_HEIGHT: u16 = 19; +const SEPARATION: u32 = 21; +const LABEL_WIDTH: u16 = 205; +const VALUE_WIDTH: u16 = 35; +const GREEN: [u8; 3] = [128, 255, 128]; +const RED: [u8; 3] = [255, 50, 50]; + +/// Panel size in the original 640×480 layout, including the overlapping edge pixel. +pub const EXTENT: [u32; 2] = [267, SEPARATION + ROW_HEIGHT as u32]; + +/// Atlas pieces and the original font for the two strategic balance rows. +pub struct BalancePanel { + background: RgbaImage, + labels: [RgbaImage; 2], + font: BitmapFont, +} + +impl BalancePanel { + /// Loads the reusable atlas art and centers the localized labels in their fields. + pub fn load(vfs: &dyn Vfs, labels: [&str; 2]) -> Result { + let [end, value, separator, emitter, body, cap] = load_ui_sprites( + vfs, + "ui/compaund.cfg", + [ + "ccres_ending_text", + "ccres_body_text", + "ccres_separator_left_text", + "ccres_ray_emitter_off", + "ccres_ray_body", + "ccres_ray_ending", + ], + )?; + let cells = [ + (&end, 5), + (&value, u32::from(VALUE_WIDTH)), + (&separator, 5), + (&emitter, 10), + (&body, u32::from(LABEL_WIDTH)), + (&cap, 6), + ]; + let [width, height] = EXTENT; + let mut background = RgbaImage { + width, + height, + rgba8: vec![0; (width * height * 4) as usize], + }; + for y in [0, SEPARATION] { + let mut right = width; + for (sprite, width) in cells { + copy_sprite( + &mut background, + sprite, + [right - width - 1, y, right, y + u32::from(ROW_HEIGHT)], + [false; 2], + [255; 3], + ); + right -= width; + } + } + let font = load_layout_font(vfs)?; + let [ore, power] = labels.map(|text| font.centered_line(text, LABEL_WIDTH, ROW_HEIGHT)); + Ok(Self { + background, + labels: [ore?, power?], + font, + }) + } + + /// Composes the filled bars and their text. A hidden zero keeps the row frame. + pub fn paint(&self, [ore, power]: [Option; 2]) -> Result { + const LABEL_LEFT: u32 = 7; + const LABEL_RIGHT: u32 = LABEL_LEFT + LABEL_WIDTH as u32; + const VALUE_LEFT: u32 = EXTENT[0] - 5 - VALUE_WIDTH as u32; + let mut image = self.background.clone(); + for (row, percent) in [ore, power].into_iter().enumerate() { + let Some(percent) = percent else { + continue; + }; + let y = row as u32 * SEPARATION; + let end = y + u32::from(ROW_HEIGHT); + let filled = + (f64::from(percent.min(100)) * f64::from(LABEL_WIDTH) * f64::from(0.01_f32)) + .round_ties_even() as u32; + if filled > 2 { + let color = match percent { + 0..=19 => [128, 0, 0, 128], + 20..=79 => [128, 128, 0, 128], + _ => [0, 128, 0, 128], + }; + copy_sprite( + &mut image, + &RgbaImage { + width: 1, + height: 1, + rgba8: color.to_vec(), + }, + [LABEL_RIGHT - filled, y + 3, LABEL_RIGHT - 2, end - 3], + [false; 2], + [255; 3], + ); + } + let tint = if percent == 0 { RED } else { GREEN }; + copy_sprite( + &mut image, + &self.labels[row], + [LABEL_LEFT, y, LABEL_RIGHT, end], + [false; 2], + tint, + ); + let number = + self.font + .centered_line(&format!("{percent}%"), VALUE_WIDTH, ROW_HEIGHT)?; + copy_sprite( + &mut image, + &number, + [VALUE_LEFT, y, VALUE_LEFT + u32::from(VALUE_WIDTH), end], + [false; 2], + tint, + ); + } + Ok(image) + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index efa4f272..cb8c53d5 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -71,6 +71,7 @@ mod mobile_budget; mod node_amount; mod node_stage; pub mod satellite_contacts; +pub mod strategic_balance; pub mod strategic_view; pub use clan_clock::note_mission_pause_elapsed; use control_profile::project_initial as project_control_owner_profile_fca0; diff --git a/crates/fparkan-runtime/src/strategic_balance.rs b/crates/fparkan-runtime/src/strategic_balance.rs new file mode 100644 index 00000000..f0c2a8a1 --- /dev/null +++ b/crates/fparkan-runtime/src/strategic_balance.rs @@ -0,0 +1,103 @@ +//! Clan resource indicators on the strategic screen. + +use crate::Engine; + +/// Target percentages for the strategic bars, in minerals and energy order. +#[must_use] +pub fn clan_balance(engine: &Engine) -> Option<[u8; 2]> { + let mission = engine.loaded.as_ref()?; + let local = mission + .mission + .clans + .iter() + .position(|clan| clan.mode == 1) + .unwrap_or(0); + let mut all_power = 0.0; + let mut local_totals = [0.0; 5]; + for bundle in &mission.script_bundles { + if let Some(state) = bundle.behavior_warm_start { + let totals = state.cleared_words.map(f32::from_bits); + all_power += totals[0]; + if bundle.clan_index == local { + local_totals = totals; + } + } + } + let [power, _, ore, demand, _] = local_totals; + Some(balance_percent(ore, power, demand, all_power)) +} + +fn balance_percent(ore: f32, power: f32, demand: f32, all_power: f32) -> [u8; 2] { + // iron3d+0x6dd80 uses a single-precision reciprocal for the ore buffer, + // but carries the product and power ratio in the x87 stack until rounding. + let ore = f64::from(ore) * f64::from(0.000_222_222_22_f32); + let surplus = if all_power != 0.0 { + (f64::from(power) - f64::from(demand)) / f64::from(all_power) + } else { + 0.0 + }; + [ore, surplus].map(|fraction| (100.0 * fraction.clamp(0.0, 1.0)).round_ties_even() as u8) +} + +/// The visible reading and the shared zero-value blink phase. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub struct BalanceReading { + /// Current displayed value of each bar. + pub percent: [u8; 2], + /// Whether the labels of zero-valued bars should be drawn this frame. + pub blink_on: bool, +} + +/// Original display state, retained through cockpit and strategic view changes. +#[derive(Default)] +pub struct BalanceDisplay { + reading: BalanceReading, + last_step_ms: u32, + last_blink_ms: u32, +} + +impl BalanceDisplay { + /// Supplies this frame's reading and then moves the display toward its target. + pub fn sample(&mut self, target: [u8; 2], now_ms: u32) -> BalanceReading { + if self.reading.percent.contains(&0) && now_ms.wrapping_sub(self.last_blink_ms) > 500 { + self.last_blink_ms = now_ms; + self.reading.blink_on = !self.reading.blink_on; + } + let shown = self.reading; + if target != shown.percent && now_ms.wrapping_sub(self.last_step_ms) > 50 { + self.last_step_ms = now_ms; + for (value, target) in self.reading.percent.iter_mut().zip(target) { + let step = (i16::from(target) - i16::from(*value)).signum(); + *value = (i16::from(*value) + step) as u8; + } + } + shown + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn balance_uses_local_surplus_relative_to_world_production() { + assert_eq!(balance_percent(500.0, 75.0, 15.0, 100.0), [11, 60]); + assert_eq!(balance_percent(10_000.0, 25.0, 0.0, 200.0), [100, 12]); + assert_eq!(balance_percent(-1.0, 5.0, 10.0, 100.0), [0, 0]); + assert_eq!(balance_percent(0.0, 0.0, 0.0, 0.0), [0, 0]); + } + + #[test] + fn display_moves_one_point_per_due_frame_and_blinks_at_zero() { + let mut display = BalanceDisplay::default(); + assert_eq!(display.sample([11, 60], 50).percent, [0, 0]); + assert_eq!(display.sample([11, 60], 51).percent, [0, 0]); + assert_eq!(display.sample([11, 60], 101).percent, [1, 1]); + assert_eq!(display.sample([11, 60], 650).percent, [1, 1]); + assert_eq!(display.sample([11, 0], 701).percent, [2, 2]); + assert_eq!(display.sample([11, 0], 752).percent, [3, 1]); + let zero = display.sample([11, 0], 803); + assert_eq!(zero.percent, [4, 0]); + assert!(zero.blink_on); + } +} -- cgit v1.2.3