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
|
//! Live rows of the strategic building sections.
use crate::{strategic_map_readout::building_label, Engine, StartingCockpit};
use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle};
use std::cmp::Reverse;
pub(super) const OTHER_BUILDINGS: u32 = 0x8000_044e;
pub(super) const BUNKERS: u32 = 0x8007_0000;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BuildingSection {
Bunkers,
Other,
}
impl BuildingSection {
pub fn matches(self, class: u32) -> bool {
let mask = match self {
Self::Bunkers => BUNKERS,
Self::Other => OTHER_BUILDINGS,
};
class != 0 && class & mask == class
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BuildingRow {
pub handle: ObjectHandle,
pub class: u32,
pub size: u32,
pub label: u32,
pub durability_percent: Option<i32>,
pub manual_control: bool,
}
/// Buildings of the operator's clan, most recently registered first.
pub fn building_rows(
engine: &Engine,
cockpit: StartingCockpit,
section: BuildingSection,
) -> Vec<BuildingRow> {
let Some((mission, owner)) = engine.loaded.as_ref().zip(
identity_metadata(&engine.world, cockpit.handle)
.ok()
.and_then(|identity| identity.owner_id),
) else {
return Vec::new();
};
let mut rows: Vec<_> = mission
.control_device_tables
.iter()
.filter_map(|table| {
let handle = table.handle;
let draft = mission.object_drafts.get(table.original_id?.0 as usize)?;
let class = draft.unit_dat_header_flags?;
if !section.matches(class)
|| table.node_provider_depletion_marked
|| identity_metadata(&engine.world, handle).ok()?.owner_id != Some(owner)
{
return None;
}
let sequence = registration_sequence(&engine.world, handle).ok()??;
let size =
crate::unit_size_class::classify(3, &draft.unit_components.first()?.resource_raw);
let row = BuildingRow {
handle,
class,
size,
label: building_label(class, size),
durability_percent: crate::warbot_stats::durability_percent(table),
manual_control: section == BuildingSection::Bunkers
&& crate::bunker_control::cockpit(engine, cockpit, handle).is_some(),
};
Some((sequence, row))
})
.collect();
rows.sort_unstable_by_key(|(sequence, _)| Reverse(*sequence));
rows.into_iter().map(|(_, row)| row).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
create, frame, load_gameplay_mission, satellite_contacts, starting_cockpit, EngineServices,
MissionMode, MissionRequest, MissionStartupInput,
};
use fparkan_vfs::DirectoryVfs;
use std::sync::Arc;
#[test]
#[ignore = "requires licensed mission resources"]
fn two_fortresses_lists_owned_buildings_and_whitens_only_the_selected_map_icon() {
let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus");
let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
load_gameplay_mission(
&mut engine,
MissionRequest {
key: "MISSIONS/Single.01/data.tma".into(),
mode: MissionMode::SingleMission,
},
MissionStartupInput {
queue_time_ms: 0,
research_creation_gate: false,
},
None,
)
.unwrap();
frame(&mut engine, 1).unwrap();
let cockpit = starting_cockpit(&engine).unwrap();
let rows = building_rows(&engine, cockpit, BuildingSection::Other);
assert_eq!(
rows.iter().map(|row| row.label).collect::<Vec<_>>(),
[0x179e, 0x178f, 0x1794, 0x1799]
);
let extent = satellite_contacts::landscape_extent(&engine).unwrap();
let before: Vec<_> =
satellite_contacts::visible_buildings(&engine, cockpit, extent, None).collect();
let after: Vec<_> =
satellite_contacts::visible_buildings(&engine, cockpit, extent, Some(rows[1].handle))
.collect();
assert_eq!(before.len(), after.len());
let changed: Vec<_> = before.iter().zip(&after).filter(|(a, b)| a != b).collect();
assert_eq!(changed.len(), 1);
let (normal, selected) = changed[0];
assert_eq!(normal.tint, satellite_contacts::OWN_TINT);
assert_eq!(
*selected,
satellite_contacts::BuildingContact {
tint: [255; 3],
..*normal
}
);
}
}
|