aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs24
-rw-r--r--crates/fparkan-runtime/src/clan_research.rs97
-rw-r--r--crates/fparkan-runtime/src/lib.rs6
3 files changed, 125 insertions, 2 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index d191029c..31dca6d4 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -238,6 +238,7 @@ pub(super) struct ClanAiEvents {
clocks: crate::clan_clock::ClanClocks,
stops: BTreeMap<(usize, usize, usize), (MissionEventStop, u64)>,
research_stubs: BTreeMap<(usize, u8), u64>,
+ research_scan_queries: BTreeMap<(usize, u32), u64>,
pub(super) research_catalogs: BTreeMap<usize, crate::clan_research::ClanResearch>,
problems: BTreeMap<usize, Handler2RecordScheduler>,
started: BTreeMap<(usize, u32), u64>,
@@ -1261,10 +1262,23 @@ impl LoadedMissionState {
}
Ok(Some(VarSetDefault::Dword(id)))
}
- ScriptDispatchSelector::Handler(handler @ (56 | 65))
+ ScriptDispatchSelector::Handler(56)
if instruction.references.is_empty() =>
{
- // shortcut: keep research disabled until the native policy flag and catalog are wired.
+ let result = crate::clan_research::scan_status(
+ self.clan_ai_events.research_catalogs.get(&clan).map(|catalog| &catalog.candidates),
+ );
+ let count = self.clan_ai_events.research_scan_queries.entry((clan, result)).or_default();
+ *count += 1;
+ if *count == 1 {
+ eprintln!("sim_coverage script_research_scan_status=1 clan={clan} result={result}");
+ }
+ Ok(Some(VarSetDefault::Dword(result)))
+ }
+ ScriptDispatchSelector::Handler(handler @ 65)
+ if instruction.references.is_empty() =>
+ {
+ // shortcut: large-warbot eligibility needs the native candidate power property.
self.clan_ai_events.log_research_stub(
clan,
handler,
@@ -1408,6 +1422,12 @@ fn storage_space(ores: &[BehaviorOreState], world: &World, clan: OwnerId) -> f32
}
impl ClanAiEvents {
+ pub(super) fn research_scan_queries(&self) -> impl Iterator<Item = (usize, u32, u64)> + '_ {
+ self.research_scan_queries
+ .iter()
+ .map(|(&(clan, result), &count)| (clan, result, count))
+ }
+
pub(super) fn free_mind_queries(
&self,
) -> impl Iterator<Item = (usize, u32, u32, u32, u64)> + '_ {
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,
@@ -113,6 +117,99 @@ mod tests {
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::<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 [
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 730c52ab..317fc539 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -2923,6 +2923,8 @@ pub struct SimulationReport {
pub clan_ai_clock_ticks: Vec<(usize, u64, u64)>,
/// Calls to stubbed research policy or status, grouped by clan and handler.
pub clan_research_status_stubs: Vec<(usize, u8, u64)>,
+ /// Cached catalog scan queries, grouped by clan and normalized result.
+ pub clan_research_scan_queries: Vec<(usize, u32, u64)>,
pub clan_research_catalogs: Vec<(usize, usize, u32, u64)>,
pub laboratory: LaboratoryReport,
/// Counts of pending script problems by clan and problem kind.
@@ -2999,6 +3001,7 @@ pub fn simulate_mission_report(
clan_ai_event_stops: Vec::new(),
clan_ai_clock_ticks: Vec::new(),
clan_research_status_stubs: Vec::new(),
+ clan_research_scan_queries: Vec::new(),
clan_research_catalogs: Vec::new(),
laboratory: Default::default(),
clan_problem_dispatch_stubs: Default::default(),
@@ -3103,6 +3106,9 @@ pub fn simulate_mission_report(
.clan_research_status_stubs
.extend(loaded.clan_ai_events.research_status_stubs());
report
+ .clan_research_scan_queries
+ .extend(loaded.clan_ai_events.research_scan_queries());
+ report
.clan_ai_clock_ticks
.extend(loaded.clan_ai_events.clock_ticks());
report