aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/warbot_card.rs
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/src/warbot_card.rs
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/src/warbot_card.rs')
-rw-r--r--crates/fparkan-assets/src/warbot_card.rs188
1 files changed, 188 insertions, 0 deletions
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(), "")
+ );
+ }
+}