aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 10:40:01 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 10:40:01 +0300
commit20161fbf4fab9080a2d0d6ceedbf1344435cfda0 (patch)
tree174be0827089abc4407af7289142d66d9f995ae7 /crates/fparkan-assets
parent5f0b8c076b40f8eca67e2745367c731f803914f4 (diff)
downloadfparkan-20161fbf4fab9080a2d0d6ceedbf1344435cfda0.tar.xz
fparkan-20161fbf4fab9080a2d0d6ceedbf1344435cfda0.zip
feat(ui): show live Control stats in the warbot card
Diffstat (limited to 'crates/fparkan-assets')
-rw-r--r--crates/fparkan-assets/src/explosion_profile.rs20
-rw-r--r--crates/fparkan-assets/src/lib.rs3
-rw-r--r--crates/fparkan-assets/src/strategic_roster.rs45
-rw-r--r--crates/fparkan-assets/src/warbot_card.rs188
4 files changed, 253 insertions, 3 deletions
diff --git a/crates/fparkan-assets/src/explosion_profile.rs b/crates/fparkan-assets/src/explosion_profile.rs
index af9c5a3e..d9f15229 100644
--- a/crates/fparkan-assets/src/explosion_profile.rs
+++ b/crates/fparkan-assets/src/explosion_profile.rs
@@ -1,5 +1,13 @@
//! Effect references carried by the version-1 explosion descriptor.
+/// The contribution of an EXPL descriptor to Control's projectile capacity.
+pub fn decode_explosion_capacity(bytes: &[u8]) -> Option<f32> {
+ if bytes.len() != 792 || !matches!(u32::from_le_bytes(bytes[..4].try_into().ok()?), 1..=3) {
+ return None;
+ }
+ Some(f32::from_le_bytes(bytes[4..8].try_into().ok()?))
+}
+
/// Reads the initial effect archive and name from a version-1 EXPL payload.
/// Other versions and malformed references require separate interpretation.
pub fn decode_explosion_effect(bytes: &[u8]) -> Option<(&str, &str)> {
@@ -34,4 +42,16 @@ mod tests {
bytes[0] = 2;
assert_eq!(decode_explosion_effect(&bytes), None);
}
+
+ #[test]
+ fn reads_capacity_in_each_observed_explosion_layout() {
+ let mut bytes = [0; 792];
+ bytes[4..8].copy_from_slice(&300.0_f32.to_le_bytes());
+ for version in 1..=3_u32 {
+ bytes[..4].copy_from_slice(&version.to_le_bytes());
+ assert_eq!(decode_explosion_capacity(&bytes), Some(300.0));
+ }
+ bytes[0] = 4;
+ assert_eq!(decode_explosion_capacity(&bytes), None);
+ }
}
diff --git a/crates/fparkan-assets/src/lib.rs b/crates/fparkan-assets/src/lib.rs
index 090c6c78..253920fa 100644
--- a/crates/fparkan-assets/src/lib.rs
+++ b/crates/fparkan-assets/src/lib.rs
@@ -52,12 +52,13 @@ pub mod strategy_toolbar;
mod turret_elevation;
mod ui_configuration;
pub mod ui_sprite;
+pub mod warbot_card;
pub use behavior_profile_values::{
decode_behavior_go_speed_percent, decode_behavior_work_rates, BehaviorWorkRates,
};
pub use chassis_profile::{decode_chassis_can_fly, decode_chassis_flags, ChassisFlags};
pub use control_drive::{ControlDriveStates, PreparedControlDrive};
-pub use explosion_profile::decode_explosion_effect;
+pub use explosion_profile::{decode_explosion_capacity, decode_explosion_effect};
pub use turret_elevation::{apply_manual_turret_elevation, turret_elevation_row};
mod research;
pub use research::{
diff --git a/crates/fparkan-assets/src/strategic_roster.rs b/crates/fparkan-assets/src/strategic_roster.rs
index 3a28ac2a..0faeb7e4 100644
--- a/crates/fparkan-assets/src/strategic_roster.rs
+++ b/crates/fparkan-assets/src/strategic_roster.rs
@@ -4,6 +4,7 @@ use crate::{
game_font::{load_layout_font, BitmapFont},
mission_pager::copy_sprite,
ui_sprite::{atlas_region, load_ui_page, load_ui_sprites},
+ warbot_card::{self, CardLine},
RgbaImage,
};
use fparkan_vfs::Vfs;
@@ -47,6 +48,7 @@ pub struct Row<'a> {
pub size: u32,
pub chassis: u32,
pub selected: bool,
+ pub durability_percent: Option<i32>,
}
/// Resident art for the header, selected-unit frame, and rows.
@@ -150,14 +152,19 @@ impl RosterImages {
})
}
- /// Renders the visible rows and the identity of the selected unit.
- pub fn paint(&self, rows: &[Row<'_>]) -> Result<RgbaImage, String> {
+ /// Renders the visible rows and card of the selected unit.
+ pub fn paint(
+ &self,
+ rows: &[Row<'_>],
+ card: &[Option<CardLine<'_>>],
+ ) -> Result<RgbaImage, String> {
let mut panel = self.background.clone();
if let Some(row) = rows.iter().find(|row| row.selected) {
self.icons(&mut panel, row, [9, 27]);
let width = u32::from(self.font.line_metrics(row.label)?[0]).min(263);
let name = self.font.centered_line(row.label, width as u16, 16)?;
stamp(&mut panel, &name, [49, 25, 49 + width, 41], [255, 255, 0]);
+ warbot_card::paint(&self.font, &mut panel, card)?;
}
for (index, row) in rows.iter().take(19).enumerate() {
let y = ROW_TOP + index as u32 * ROW_PITCH;
@@ -179,6 +186,19 @@ impl RosterImages {
);
}
self.icons(&mut panel, row, [12, y + 2]);
+ if let Some((width, rgba)) = row.durability_percent.and_then(durability_fill) {
+ let fill = RgbaImage {
+ width: 1,
+ height: 1,
+ rgba8: rgba.to_vec(),
+ };
+ stamp(
+ &mut panel,
+ &fill,
+ [TEXT_LEFT + 2, y + 3, TEXT_LEFT + width, bottom - 3],
+ [255; 3],
+ );
+ }
let name = self
.font
.centered_line(row.label, TEXT_WIDTH as u16, ROW_HEIGHT as u16)?;
@@ -221,6 +241,17 @@ fn stamp(panel: &mut RgbaImage, sprite: &RgbaImage, bounds: [u32; 4], tint: [u8;
copy_sprite(panel, sprite, bounds, [false; 2], tint);
}
+fn durability_fill(percent: i32) -> Option<(u32, [u8; 4])> {
+ let width = (f64::from(percent.min(100)) * f64::from(TEXT_WIDTH) * f64::from(0.01_f32))
+ .round_ties_even() as u32;
+ let rgba = match percent {
+ ..=19 => [128, 0, 0, 128],
+ 20..=79 => [128, 128, 0, 128],
+ _ => [0, 128, 0, 128],
+ };
+ (width > 2).then_some((width, rgba))
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -234,4 +265,14 @@ mod tests {
assert_eq!(hit_test([200.0, 158.0], 2), None);
assert_eq!(hit_test([200.0, 480.0], 20), None);
}
+
+ #[test]
+ fn original_durability_colours_follow_the_rounded_health_percentage() {
+ assert_eq!(durability_fill(0), None);
+ assert_eq!(durability_fill(19), Some((47, [128, 0, 0, 128])));
+ assert_eq!(durability_fill(20), Some((50, [128, 128, 0, 128])));
+ assert_eq!(durability_fill(79), Some((196, [128, 128, 0, 128])));
+ assert_eq!(durability_fill(80), Some((198, [0, 128, 0, 128])));
+ assert_eq!(durability_fill(100), Some((248, [0, 128, 0, 128])));
+ }
}
diff --git a/crates/fparkan-assets/src/warbot_card.rs b/crates/fparkan-assets/src/warbot_card.rs
new file mode 100644
index 00000000..015bddce
--- /dev/null
+++ b/crates/fparkan-assets/src/warbot_card.rs
@@ -0,0 +1,188 @@
+//! Formatting and layout for the five original strategic card readings.
+
+use crate::{game_font::BitmapFont, mission_pager::copy_sprite, RgbaImage};
+
+/// The installed game's labels and unit strings, in card row order.
+pub struct CardLabels {
+ pub labels: [String; 5],
+ pub tonnes: String,
+ pub speed: String,
+ pub metres: String,
+}
+
+/// The `[TEMP]` ratings scale read by iron3d+0x6fb60.
+#[derive(Default)]
+pub struct RatingScale {
+ /// Defence and offence intervals when normalization is enabled.
+ limits: Option<[[f32; 2]; 2]>,
+}
+
+impl RatingScale {
+ /// Reads the five scale settings from the installed game's profile.
+ pub fn from_ini(ini: &str) -> Self {
+ let mut in_temp = false;
+ let mut normalize = false;
+ let mut defence = [0.0; 2];
+ let mut offence = [0.0; 2];
+ for line in ini
+ .lines()
+ .map(|line| line.trim_start_matches('\u{feff}').trim())
+ {
+ if line.starts_with('[') {
+ in_temp = line.eq_ignore_ascii_case("[TEMP]");
+ } else if in_temp {
+ let Some((key, value)) = line.split_once('=') else {
+ continue;
+ };
+ let value = value.split(';').next().unwrap_or_default().trim();
+ match key.trim().to_ascii_uppercase().as_str() {
+ "NORMALIZE" => normalize = value.parse::<i32>().unwrap_or(0) != 0,
+ "DEFENCE_MIN" => defence[0] = value.parse().unwrap_or(0.0),
+ "DEFENCE_MAX" => defence[1] = value.parse().unwrap_or(0.0),
+ "OFFENCE_MIN" => offence[0] = value.parse().unwrap_or(0.0),
+ "OFFENCE_MAX" => offence[1] = value.parse().unwrap_or(0.0),
+ _ => {}
+ }
+ }
+ }
+ Self {
+ limits: normalize.then_some([defence, offence]),
+ }
+ }
+
+ fn format(&self, index: usize, value: f32) -> (String, &'static str) {
+ if let Some(limits) = self.limits {
+ let [min, max] = limits[index];
+ let percent = if value <= min {
+ 0.0
+ } else if value >= max {
+ 100.0
+ } else {
+ (f64::from(value) - f64::from(min)) * 100.0 / (f64::from(max) - f64::from(min))
+ };
+ (format!("{percent:.0}"), "%")
+ } else {
+ (format!("{value:.6}"), "")
+ }
+ }
+}
+
+/// One card reading. The number has a separate tint and aligns with the unit column.
+pub struct CardLine<'a> {
+ label: &'a str,
+ number: String,
+ unit: &'a str,
+ warn: bool,
+}
+
+impl CardLabels {
+ /// Formats the values in the order used by iron3d+0x70090.
+ pub fn lines(
+ &self,
+ weight: Option<[f32; 2]>,
+ stats: [Option<f32>; 4],
+ scale: &RatingScale,
+ ) -> [Option<CardLine<'_>>; 5] {
+ let [speed, defence, offence, radar] = stats;
+ let line = |index: usize, number, unit, warn| CardLine {
+ label: &self.labels[index],
+ number,
+ unit,
+ warn,
+ };
+ [
+ weight.map(|[weight, capacity]| {
+ let [weight, capacity] = [weight, capacity].map(|value| value * 0.001_f32);
+ line(
+ 0,
+ format!("{weight:.0} / {capacity:.0}"),
+ &self.tonnes,
+ capacity == 0.0,
+ )
+ }),
+ speed.map(|speed| {
+ line(
+ 1,
+ format!("{:.0}", f64::from(speed) * f64::from(3.6_f32)),
+ &self.speed,
+ false,
+ )
+ }),
+ defence.map(|value| {
+ let (number, unit) = scale.format(0, value);
+ line(2, number, unit, false)
+ }),
+ offence.map(|value| {
+ let (number, unit) = scale.format(1, value);
+ line(3, number, unit, false)
+ }),
+ radar.map(|range| line(4, format!("{range:.0}"), &self.metres, false)),
+ ]
+ }
+}
+
+pub(super) fn paint(
+ font: &BitmapFont,
+ panel: &mut RgbaImage,
+ lines: &[Option<CardLine<'_>>],
+) -> Result<(), String> {
+ let height = u32::from(font.line_metrics("A")?[1]);
+ for (index, line) in lines.iter().enumerate() {
+ let Some(line) = line else { continue };
+ let y = 40 + index as u32 * (height + 2);
+ let green = [0, 255, 0];
+ let value = if line.warn {
+ [255, 0, 0]
+ } else {
+ [180, 180, 255]
+ };
+ for (text, x, align_right, tint) in [
+ (line.label, 71_u32, false, green),
+ (line.number.as_str(), 203, true, value),
+ (line.unit, 206, false, green),
+ ] {
+ if text.is_empty() {
+ continue;
+ }
+ let width = font.line_metrics(text)?[0];
+ let x = if align_right {
+ x.saturating_sub(u32::from(width))
+ } else {
+ x
+ };
+ let text = font.centered_line(text, width, height as u16)?;
+ copy_sprite(
+ panel,
+ &text,
+ [x, y, x + u32::from(width), y + height],
+ [false; 2],
+ tint,
+ );
+ }
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn uses_original_ini_ranges_and_honours_the_raw_mode() {
+ let scale = RatingScale::from_ini("[TEMP]\nNormalIze=1\nOFFENCE_MIN=0\nOFFENCE_MAX=6550\nDEFENCE_MIN=0\nDEFENCE_MAX=24400\n[Other]\nOFFENCE_MAX=9");
+ assert_eq!(
+ scale.format(1, f32::from_bits(0x4556_9556)),
+ ("52".into(), "%")
+ );
+ assert_eq!(
+ scale.format(0, f32::from_bits(0x465f_e6ea)),
+ ("59".into(), "%")
+ );
+ assert_eq!(scale.format(0, -1.0), ("0".into(), "%"));
+ assert_eq!(scale.format(0, 25_000.0), ("100".into(), "%"));
+ assert_eq!(
+ RatingScale::default().format(0, 59.0),
+ ("59.000000".into(), "")
+ );
+ }
+}