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
|
//! 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);
}
}
|