From f61cc5fd7dda6a76b843b36013795f52b03fae7e Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 20:16:15 +0400 Subject: feat(script): evaluate scenario deadlines and timeout tests Handler(59) stores a DWORD deadline in Init or Mission. Handler(60) compares it strictly against the current scenario clock. The original ai.dll confirms overflow and the equality boundary. Mission 16 now reaches its scheduled message. --- crates/fparkan-runtime/src/lib.rs | 15 ++++-- crates/fparkan-runtime/src/mission_events.rs | 12 +++++ crates/fparkan-runtime/src/script_time.rs | 74 ++++++++++++++++++++++++++++ crates/fparkan-script/src/lib.rs | 45 +++++++++-------- 4 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 crates/fparkan-runtime/src/script_time.rs (limited to 'crates') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 6da34cbc..cce50e1c 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -56,6 +56,7 @@ mod production; mod projectile; mod research; mod script_patrol; +mod script_time; mod unit_patrol; pub use autodemo::{ @@ -2343,7 +2344,7 @@ fn load_mission_with_options( Vec::new() }; - let mission_events = if options.asset_scope == MissionAssetScope::Full { + let mut mission_events = if options.asset_scope == MissionAssetScope::Full { MissionEvents::with_placed_objects( &mut new_runtime_world, &object_drafts, @@ -2360,6 +2361,7 @@ fn load_mission_with_options( let queue_time_ms = startup_input .expect("full mission loading requires startup inputs") .queue_time_ms; + mission_events.start_ms = queue_time_ms; if let Err(error) = start_mission_scripts( &mission, &mut loaded_scripts, @@ -8118,19 +8120,22 @@ fn resolve_init_states( &clan_count, |index, instruction, values| { if policy != InitPolicy::KeepPrefix { - return false; + return None; + } + if let Some(write) = script_time::deadline_write(instruction, values) { + return Some(Some(write)); } if let Some(order) = clan_script_init::patrol_order(&varset.declarations, instruction, values) { init_patrols.push(order); - return true; + return Some(None); } if let Some(handler) = clan_script_init::stubbed_handler(instruction) { bundle.init_stubs.push((index, handler)); - return true; + return Some(None); } - false + None }, ) .map_err(|source| EngineError::ScriptInit { diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index c3b2af44..2226ebd5 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -141,6 +141,7 @@ impl MissionObjectives { #[derive(Default)] pub(super) struct MissionEvents { + pub(super) start_ms: u32, next_ms: u32, reported_stops: HashSet<(usize, usize)>, inside_hero_exit: bool, @@ -588,6 +589,7 @@ impl LoadedMissionState { .iter_mut() .find(|state| state.clan_index == bundle.clan_index)?; let mission_events = &mut self.mission_events; + let clock_seconds = crate::script_time::elapsed_seconds(mission_events.start_ms, now_ms); let mut notices = Vec::new(); let mut patrol_orders = Vec::new(); let execution = declarations.execute_mission_prefix( @@ -692,6 +694,16 @@ impl LoadedMissionState { .find_map(|handle| identity_metadata(world, handle).ok()?.owner_id); Ok(Some(clan.map_or(u32::MAX, |owner| u32::from(owner.0)))) } + ScriptDispatchSelector::Handler(59 | 60) => { + crate::script_time::result(instruction, values, clock_seconds) + .map(Some) + .ok_or_else(|| { + format!( + "{:?} requires one DWORD operand", + instruction.dispatch_selector() + ) + }) + } ScriptDispatchSelector::Handler(15) => { let order = declarations .resolve_handler15_with_values(instruction, values) diff --git a/crates/fparkan-runtime/src/script_time.rs b/crates/fparkan-runtime/src/script_time.rs new file mode 100644 index 00000000..55e3f3d1 --- /dev/null +++ b/crates/fparkan-runtime/src/script_time.rs @@ -0,0 +1,74 @@ +use fparkan_script::{ScriptDispatchSelector, ScriptInitWrite, ScriptInstruction, VarSetDefault}; + +pub(super) fn deadline_write( + instruction: &ScriptInstruction, + values: &[VarSetDefault], +) -> Option { + let index = instruction.header_words[1]; + if instruction.dispatch_selector() != ScriptDispatchSelector::Handler(59) + || instruction.header_words[3] != u32::MAX + || !matches!(values.get(index as usize), Some(VarSetDefault::Dword(_))) + { + return None; + } + Some(ScriptInitWrite { + index, + value: VarSetDefault::Dword(result(instruction, values, 0)?), + }) +} + +pub(super) fn result( + instruction: &ScriptInstruction, + values: &[VarSetDefault], + clock: u32, +) -> Option { + let &[index] = instruction.references.as_slice() else { + return None; + }; + let &VarSetDefault::Dword(value) = values.get(index as usize)? else { + return None; + }; + match instruction.dispatch_selector() { + ScriptDispatchSelector::Handler(59) => Some(clock.wrapping_add(value)), + ScriptDispatchSelector::Handler(60) => Some(if value < clock { 1 } else { u32::MAX }), + _ => None, + } +} + +// shortcut: approximate the native AI scheduler's logical seconds by mission elapsed seconds. +pub(super) fn elapsed_seconds(start_ms: u32, now_ms: u32) -> u32 { + now_ms.wrapping_sub(start_ms) / 1_000 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn deadline_and_strict_timeout_match_native_handlers() { + let mut instruction = ScriptInstruction { + header_words: [59, 0, u32::MAX, u32::MAX, 6, 1, u32::MAX], + references: vec![0], + }; + assert_eq!( + deadline_write(&instruction, &[VarSetDefault::Dword(45)]), + Some(ScriptInitWrite { + index: 0, + value: VarSetDefault::Dword(45) + }) + ); + assert_eq!( + result(&instruction, &[VarSetDefault::Dword(45)], 7), + Some(52) + ); + assert_eq!( + result(&instruction, &[VarSetDefault::Dword(2)], u32::MAX), + Some(1) + ); + instruction.header_words[0] = 60; + let values = [VarSetDefault::Dword(45)]; + assert_eq!(result(&instruction, &values, 44), Some(u32::MAX)); + assert_eq!(result(&instruction, &values, 45), Some(u32::MAX)); + assert_eq!(result(&instruction, &values, 46), Some(1)); + } +} diff --git a/crates/fparkan-script/src/lib.rs b/crates/fparkan-script/src/lib.rs index 160fdc04..60ddc826 100644 --- a/crates/fparkan-script/src/lib.rs +++ b/crates/fparkan-script/src/lib.rs @@ -811,10 +811,10 @@ impl VarSet { formulas: Option<&FormulaTable>, clan_count: impl Fn(u32, u32) -> Option, ) -> Result, ScriptInitResolveError> { - self.resolve_init_prefix_with_host(package, input, formulas, clan_count, |_, _, _| false) + self.resolve_init_prefix_with_host(package, input, formulas, clan_count, |_, _, _| None) } - /// Allows the host to handle an unsupported instruction that has no variable writes. + /// Allows the host to handle an instruction and supply its optional variable write. /// /// # Errors /// @@ -825,7 +825,11 @@ impl VarSet { input: Handler19InitInput, formulas: Option<&FormulaTable>, clan_count: impl Fn(u32, u32) -> Option, - mut handle: impl FnMut(usize, &ScriptInstruction, &[VarSetDefault]) -> bool, + mut handle: impl FnMut( + usize, + &ScriptInstruction, + &[VarSetDefault], + ) -> Option>, ) -> Result, ScriptInitResolveError> { let Some((event_index, event)) = package.events.iter().enumerate().find(|(_, event)| { event.name_raw.split(|byte| *byte == 0).next() == Some(&b"Init"[..]) @@ -845,33 +849,32 @@ impl VarSet { stopped_at: None, }; for (instruction_index, instruction) in event.instructions.iter().enumerate() { - match self.resolve_init_instruction( + let writes = match self.resolve_init_instruction( instruction, input, formulas, &values, &clan_count, )? { - InitInstructionResolution::Executed(instruction_writes) => { - for write in &instruction_writes { - let Some(value) = values.get_mut(write.index as usize) else { - return Err(ScriptInitResolveError::VarSetIndexOutOfBounds { - index: write.index, - declarations: self.declarations.len(), - }); - }; - *value = write.value; - } - prefix.writes.extend(instruction_writes); - } + InitInstructionResolution::Executed(writes) => writes, InitInstructionResolution::Unsupported(error) => { - if handle(instruction_index, instruction, &values) { - continue; - } - prefix.stopped_at = Some((instruction_index, error)); - break; + let Some(write) = handle(instruction_index, instruction, &values) else { + prefix.stopped_at = Some((instruction_index, error)); + break; + }; + write.into_iter().collect() } + }; + for write in &writes { + let Some(value) = values.get_mut(write.index as usize) else { + return Err(ScriptInitResolveError::VarSetIndexOutOfBounds { + index: write.index, + declarations: self.declarations.len(), + }); + }; + *value = write.value; } + prefix.writes.extend(writes); } Ok(Some(prefix)) -- cgit v1.2.3