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
|
use crate::{Engine, MissionControlNodeProviderTableStatus};
use fparkan_world::{handle_by_original_id, identity_metadata, ObjectHandle};
/// Reports whether this clan is controlled by the player.
#[must_use]
pub fn is_player_clan(engine: &Engine, clan: u16) -> bool {
engine
.loaded
.as_ref()
.and_then(|mission| mission.mission.clans.get(usize::from(clan)))
.is_some_and(|clan| clan.mode == 1)
}
/// Current values for the player's controlled unit and clan.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PlayerStatus {
/// Remaining root Control capacity as a percentage.
pub health_percent: Option<u32>,
/// Ore stored in the clan's buffer.
pub ore_in_buffer: Option<f32>,
/// Available power and total power demand.
pub power: Option<(f32, f32)>,
/// Combined current and initial health of the clan's live factories.
pub factory_health: Option<(f32, f32)>,
}
/// Reads the values used by combat and the clan's current distributor.
#[must_use]
pub fn player_status(engine: &Engine, controlled: ObjectHandle) -> Option<PlayerStatus> {
let mission = engine.loaded.as_ref()?;
let owner = identity_metadata(&engine.world, controlled)
.ok()?
.owner_id?;
let health_percent = mission
.control_device_tables
.iter()
.find(|table| table.handle == controlled)
.and_then(|table| {
let MissionControlNodeProviderTableStatus::Available(rows) =
&table.node_provider_status
else {
return None;
};
let root = rows.first()?;
health_percent(
f32::from_bits(root.current_amount_bits),
f32::from_bits(root.initial_amount_bits),
)
});
let resources = mission
.script_bundles
.iter()
.find(|bundle| bundle.clan_index == usize::from(owner.0))
.and_then(|bundle| bundle.behavior_warm_start);
let mut factory_health = (0.0, 0.0);
for (current, maximum) in mission.object_drafts.iter().filter_map(|draft| {
if draft.unit_dat_header_flags != Some(super::autodemo::PLANT_CLASS) {
return None;
}
let handle = handle_by_original_id(&engine.world, draft.original_id?)?;
if identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner) {
return None;
}
let table = mission
.control_device_tables
.iter()
.find(|table| table.handle == handle)?;
table
.node_provider_amount_bits
.zip(table.node_provider_maximum_amount_bits)
}) {
factory_health.0 += f32::from_bits(current);
factory_health.1 += f32::from_bits(maximum);
}
Some(PlayerStatus {
health_percent,
ore_in_buffer: resources.map(|state| f32::from_bits(state.cleared_words[2])),
power: resources.map(|state| {
let [available, _, _, demand, _] = state.cleared_words.map(f32::from_bits);
(available, demand)
}),
factory_health: (factory_health.1 > 0.0).then_some(factory_health),
})
}
fn health_percent(current: f32, initial: f32) -> Option<u32> {
(current.is_finite() && initial.is_finite() && initial > 0.0)
.then(|| (100.0 * (current / initial).clamp(0.0, 1.0)).round() as u32)
}
#[cfg(test)]
mod tests {
use super::health_percent;
#[test]
fn reports_current_capacity_within_display_range() {
assert_eq!(health_percent(23.0, 80.0), Some(29));
assert_eq!(health_percent(90.0, 80.0), Some(100));
assert_eq!(health_percent(-5.0, 80.0), Some(0));
assert_eq!(health_percent(0.0, 0.0), None);
}
}
|