//! 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::().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; 4], scale: &RatingScale, ) -> [Option>; 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>], ) -> Result<(), String> { paint_at(font, panel, lines, [71, 203, 206], 40) } pub(super) fn paint_at( font: &BitmapFont, panel: &mut RgbaImage, lines: &[Option>], [label, number, unit]: [u32; 3], top: u32, ) -> 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 = top + 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, label, false, green), (line.number.as_str(), number, true, value), (line.unit, unit, 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(), "") ); } }