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/script_time.rs | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 crates/fparkan-runtime/src/script_time.rs (limited to 'crates/fparkan-runtime/src/script_time.rs') 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)); + } +} -- cgit v1.2.3