From 0cd6359c4abc90b458a2b90daadccdb6aee8e2c4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 25 Nov 2026 21:24:27 +0400 Subject: fix(ai): expose the native research catalog scan status Handler(56) returned a constant zero and was described as an automatic research permission flag. It actually normalizes the cached candidate scan byte. Read ResearchCandidateSet.status without refreshing the catalog and report queries by clan and result instead of counting them as stubs. GOG ai.dll SHA-256 c19cd6cd31cd2896f8c4ba0f0e700dd3408aec8182daaf4863d98a7d233a2abc: Handler(56), RVA 0xe430, reads AI+0x431. Native calls to this getter and the chooser at 0x10f90 confirm that finishing the last candidate preserves the old scan flag until the next refresh. Empty/processed sets, missing pending candidates, stale flags, and noncanonical nonzero bytes were exercised. A live stock Single.01 query also returned zero from the same cached byte. The shipped scream.scr condition suppresses the laboratory request for a nonzero result. An ignored resource test executes that original condition and request for cached bytes 0, 1 and 255; an ordinary Rust test covers the cached-state semantics and missing catalog. This isolated branch check does not establish that an entire mission has exhausted all research naturally. Validated fmt, workspace check/tests, the licensed scenario-branch test and the release build. All 29 stock missions pass --sim-report 900 without new script stops; stock mission 28 also runs in the window with ongoing research and the native zero result. None of these stock runs reached a nonzero scan flag. Handler(58) upgrades and Handler(65) candidate power remain explicit stubs, and full timed AI/mission parity remains open. --- crates/fparkan-runtime/src/clan_research.rs | 97 +++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'crates/fparkan-runtime/src/clan_research.rs') diff --git a/crates/fparkan-runtime/src/clan_research.rs b/crates/fparkan-runtime/src/clan_research.rs index 303d065e..a9407534 100644 --- a/crates/fparkan-runtime/src/clan_research.rs +++ b/crates/fparkan-runtime/src/clan_research.rs @@ -10,6 +10,10 @@ pub(super) struct ClanResearch { pub(super) refreshes: u64, } +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, @@ -112,6 +116,99 @@ mod tests { use fparkan_vfs::{DirectoryVfs, MemoryVfs}; use std::sync::Arc; + #[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::>(); + 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(); -- cgit v1.2.3