1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
//! 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> {
paint_at(font, panel, lines, [71, 203, 206], 40)
}
pub(super) fn paint_at(
font: &BitmapFont,
panel: &mut RgbaImage,
lines: &[Option<CardLine<'_>>],
[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(), "")
);
}
}
|