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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
use crate::MissionDocument;
use fparkan_assets::{ResearchCandidate, ResearchCandidateSet, ResearchTrfDocument};
use fparkan_path::NormalizedPath;
use fparkan_prototype::decode_unit_dat;
use fparkan_resource::{ArchiveId, ResourceRepository};
use fparkan_vfs::Vfs;
use fparkan_world::OwnerId;
pub(super) struct ClanResearch {
pub(super) candidates: ResearchCandidateSet,
pub(super) table: ResearchTrfDocument,
pub(super) refreshes: u64,
assault_excluded: Vec<bool>,
}
pub(super) fn scan_status(candidates: Option<&ResearchCandidateSet>) -> u32 {
u32::from(candidates.is_some_and(|candidates| candidates.status != 0))
}
impl ClanResearch {
pub(super) fn load(
vfs: &dyn Vfs,
repository: &impl ResourceRepository,
mission: &MissionDocument,
clan: usize,
) -> Result<Self, String> {
let owner = OwnerId(u16::try_from(clan).map_err(|_| "research clan is out of range")?);
let table = crate::research::load_technology_table(vfs, mission, owner)
.map_err(|error| error.to_string())?;
let candidates = ResearchCandidateSet {
entries: paths(vfs)?
.into_iter()
.map(|path| ResearchCandidate { path, status: 0 })
.collect(),
loaded: true,
..Default::default()
};
let registry_path = crate::normalize_engine_path("AI research", "objects.rlb")
.expect("static registry path");
let registry = repository.open_archive_unchanged(®istry_path).ok();
let assault_excluded = candidates
.entries
.iter()
.map(|entry| {
excluded_from_assault(vfs, repository, registry, &entry.path).unwrap_or(false)
})
.collect();
let mut catalog = Self {
candidates,
table,
refreshes: 0,
assault_excluded,
};
catalog.refresh(vfs)?;
Ok(catalog)
}
pub(super) fn refresh(&mut self, vfs: &dyn Vfs) -> Result<(), String> {
self.refreshes += 1;
self.candidates
.refresh_selection(vfs, Some(&self.table))
.map_err(|error| error.to_string())
}
pub(super) fn assault_needs_weapon_metadata(&self) -> bool {
self.candidates.loaded
&& self
.candidates
.entries
.iter()
.zip(&self.assault_excluded)
.any(|(candidate, &excluded)| candidate.status != 0 && !excluded)
}
}
fn excluded_from_assault(
vfs: &dyn Vfs,
repository: &impl ResourceRepository,
registry: Option<ArchiveId>,
path: &NormalizedPath,
) -> Option<bool> {
let bytes = vfs.read(path).ok()?;
let unit = decode_unit_dat(&bytes).ok()?;
let flags = u32::from_le_bytes(unit.header_opaque[4..].try_into().ok()?);
if flags & crate::UNIT_DAT_CONTROL_CLASS_HIGH_BIT != 0 {
return Some(true);
}
if !matches!(
flags,
crate::boarding::WARBOT_CLASS
| crate::construction::BUILDER_CLASS
| crate::clan_factory_schemes::TRANSPORT_CLASS
) {
return None;
}
let root = unit.records.first()?;
let archive = root.archive_raw.split(|&byte| byte == 0).next()?;
if !archive.eq_ignore_ascii_case(b"objects.rlb") {
return None;
}
let name = root.resource_raw.split(|&byte| byte == 0).next()?;
let entry = repository
.find(registry?, &crate::resource_name(name))
.ok()??;
let kind = repository.entry_info(entry).ok()?.key.type_id;
(kind == Some(crate::CONTROL_CLASS_SOURCE_BTLU)).then(|| chassis_size(name) < 3)
}
fn chassis_size(name: &[u8]) -> u32 {
match name.get(2).map(u8::to_ascii_lowercase) {
Some(b't') => 1,
Some(b'h' | b'l') => 2,
Some(b'm') => 3,
Some(b'b') => 4,
_ => 0,
}
}
pub(super) fn initialize(
vfs: &dyn Vfs,
repository: &impl ResourceRepository,
mission: &MissionDocument,
ai: &mut crate::clan_ai_events::ClanAiEvents,
bundles: &mut [crate::MissionScriptBundle],
clan: usize,
instruction_index: usize,
) {
match ClanResearch::load(vfs, repository, mission, clan) {
Ok(catalog) => {
eprintln!(
"sim_coverage script_research_catalog_approx=1 clan={clan} entries={} selected={}",
catalog.candidates.entries.len(),
catalog.candidates.selected_index
);
ai.research_catalogs.insert(clan, catalog);
}
Err(reason) => {
eprintln!("script_research_catalog_stub clan={clan} reason={reason}");
if let Some(bundle) = bundles.iter_mut().find(|bundle| bundle.clan_index == clan) {
bundle.init_stubs.push((instruction_index, 43));
}
}
}
}
impl crate::clan_ai_events::ClanAiEvents {
pub(super) fn research_catalogs(&self) -> impl Iterator<Item = (usize, usize, u32, u64)> + '_ {
self.research_catalogs.iter().map(|(&clan, catalog)| {
(
clan,
catalog.candidates.entries.len(),
catalog.candidates.selected_index,
catalog.refreshes,
)
})
}
}
pub(super) fn paths(vfs: &dyn Vfs) -> Result<Vec<NormalizedPath>, String> {
let directory = crate::normalize_engine_path("AI research", "UNITS/UNITS/AI")
.expect("static AI catalog path");
let mut paths: Vec<_> = vfs
.list(&directory)
.map_err(|error| error.to_string())?
.into_iter()
.filter_map(|entry| {
let (parent, name) = entry.path.as_str().rsplit_once('/')?;
(parent.eq_ignore_ascii_case(directory.as_str())
&& name.to_ascii_lowercase().ends_with(".dat"))
.then_some(entry.path)
})
.collect();
// shortcut: reproduce stock NTFS enumeration; other filesystem ordering remains open.
paths.sort_by_cached_key(|path| path.as_bytes().to_ascii_uppercase());
Ok(paths)
}
#[cfg(test)]
mod tests {
use super::paths;
use crate::{
create, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
MissionStartupInput,
};
use fparkan_vfs::{DirectoryVfs, MemoryVfs};
use std::sync::Arc;
#[test]
fn assault_size_comes_from_the_zero_based_third_byte_of_a_mobile_root() {
for (root, size) in [
(b"R_T_01".as_slice(), 1),
(b"r_h_02", 2),
(b"R_L_03", 2),
(b"R_M_04", 3),
(b"r_b_03", 4),
(b"R_E_42", 0),
(b"R_", 0),
] {
assert_eq!(super::chassis_size(root), size);
}
}
#[test]
fn status_query_reads_the_cached_scan_without_reexamining_candidates() {
use fparkan_assets::{ResearchCandidate, ResearchCandidateSet};
let mut candidates = ResearchCandidateSet {
entries: vec![ResearchCandidate {
path: crate::normalize_engine_path("test", "UNITS/finished.dat").unwrap(),
status: 1,
}],
loaded: true,
..Default::default()
};
// Finishing the last candidate does not change the preceding scan's flag.
assert_eq!(super::scan_status(Some(&candidates)), 0);
candidates.status = 255;
assert_eq!(super::scan_status(Some(&candidates)), 1);
// The native query neither rescans entries nor checks the loaded flag.
candidates.entries[0].status = 0;
candidates.loaded = false;
assert_eq!(super::scan_status(Some(&candidates)), 1);
assert_eq!(super::scan_status(None), 0);
}
#[test]
#[ignore = "requires licensed scenario resources"]
fn licensed_scream_scan_flag_gates_the_laboratory_request() {
use fparkan_assets::ResearchCandidateSet;
use fparkan_script::{parse_varset, ScriptDispatchSelector, VarSetDefault};
let root = std::path::PathBuf::from(std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap());
let scripts = root.join("MISSIONS/SCRIPTS");
let varset = parse_varset(&std::fs::read(scripts.join("varset.var")).unwrap()).unwrap();
let package =
fparkan_script::decode(&std::fs::read(scripts.join("scream.scr")).unwrap()).unwrap();
let mut branch = package
.events
.into_iter()
.find(|event| event.name_raw == b"Problems0\0")
.unwrap();
let query = branch
.instructions
.iter()
.position(|instruction| {
instruction.dispatch_selector() == ScriptDispatchSelector::Handler(56)
})
.unwrap();
let request = branch.instructions[query + 1..]
.iter()
.find(|instruction| {
instruction.dispatch_selector() == ScriptDispatchSelector::Handler(2)
})
.unwrap()
.clone();
assert_eq!(
varset.declarations[request.references[4] as usize].name,
"BUILDING_INSTITUTE"
);
// Isolate the shipped scan condition and request from unrelated formula assignments.
branch.instructions = vec![
branch.instructions[query].clone(),
branch.instructions[query + 1].clone(),
request,
];
for (status, expected_requests) in [(0, 1), (1, 0), (255, 0)] {
let candidates = ResearchCandidateSet {
status,
..Default::default()
};
let mut values = varset
.declarations
.iter()
.map(|cell| cell.default_value)
.collect::<Vec<_>>();
let mut requests = 0;
varset
.execute_event_prefix(
&branch,
None,
&mut values,
|instruction, _| match instruction.dispatch_selector() {
ScriptDispatchSelector::Handler(56) => Ok(Some(VarSetDefault::Dword(
super::scan_status(Some(&candidates)),
))),
ScriptDispatchSelector::Handler(2) => {
requests += 1;
Ok(None)
}
other => Err(format!("unexpected callback {other:?}")),
},
)
.unwrap();
assert_eq!(requests, expected_requests, "cached scan status {status}");
}
}
#[test]
fn catalog_uses_direct_dat_files_and_the_observed_ntfs_name_order() {
let mut vfs = MemoryVfs::default();
for path in [
"UNITS/UNITS/AI/23_cpt1.dat",
"UNITS/UNITS/AI/23sfly1e.dat",
"UNITS/UNITS/AI/sub/skip.dat",
"UNITS/UNITS/AI/notes.txt",
"UNITS/UNITS/AI/12hel1.DAT",
] {
vfs.insert(
crate::normalize_engine_path("test", path).unwrap(),
Arc::from([]),
);
}
let actual: Vec<_> = paths(&vfs)
.unwrap()
.into_iter()
.map(|path| path.as_str().to_owned())
.collect();
assert_eq!(
actual,
[
"UNITS/UNITS/AI/12hel1.DAT",
"UNITS/UNITS/AI/23sfly1e.dat",
"UNITS/UNITS/AI/23_cpt1.dat"
]
);
}
#[test]
#[ignore = "requires licensed corpus and a live native catalog snapshot"]
fn licensed_catalog_matches_the_running_original() {
let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap();
let snapshot =
std::path::PathBuf::from(std::env::var_os("FPARKAN_NATIVE_AI_CATALOG").unwrap());
let native = std::fs::read(&snapshot).unwrap();
assert_eq!(native.len() % 0x124, 0);
let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap();
load_gameplay_mission(
&mut engine,
MissionRequest {
key: "MISSIONS/Single.02/data.tma".into(),
mode: MissionMode::SingleMission,
},
MissionStartupInput {
queue_time_ms: 0,
research_creation_gate: false,
},
None,
)
.unwrap();
let catalog = &engine
.loaded
.as_ref()
.unwrap()
.clan_ai_events
.research_catalogs[&1];
assert_eq!(catalog.candidates.entries.len(), native.len() / 0x124);
for (index, (entry, row)) in catalog
.candidates
.entries
.iter()
.zip(native.chunks_exact(0x124))
.enumerate()
{
let path = std::str::from_utf8(row[..260].split(|&b| b == 0).next().unwrap())
.unwrap()
.replace('\\', "/");
assert_eq!(entry.path.as_str(), path);
assert_eq!(entry.status, row[260], "{}", entry.path.as_str());
let flags = u32::from_le_bytes(row[0x108..0x10c].try_into().unwrap());
let size = u32::from_le_bytes(row[0x10c..0x110].try_into().unwrap());
assert_eq!(
catalog.assault_excluded[index],
flags & crate::UNIT_DAT_CONTROL_CLASS_HIGH_BIT != 0 || size < 3,
"{path} size {size}"
);
}
assert!(!catalog.assault_needs_weapon_metadata());
let index: u32 = std::fs::read_to_string(snapshot.with_extension("selection"))
.unwrap()
.trim()
.parse()
.unwrap();
assert_eq!(catalog.candidates.selected_index, index);
assert_eq!(catalog.table.row_count(), 368);
println!(
"native catalog entries={} selected={} path={}",
native.len() / 0x124,
index,
catalog.candidates.entries[index as usize].path.as_str()
);
}
}
|