diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 19:16:15 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 19:16:15 +0300 |
| commit | f61cc5fd7dda6a76b843b36013795f52b03fae7e (patch) | |
| tree | 89836bc276b7f81430feddc3da949cc1a28f3538 /crates/fparkan-runtime/src/script_time.rs | |
| parent | 38cd46123518136222473662df409f78cc63584b (diff) | |
| download | fparkan-f61cc5fd7dda6a76b843b36013795f52b03fae7e.tar.xz fparkan-f61cc5fd7dda6a76b843b36013795f52b03fae7e.zip | |
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.
Diffstat (limited to 'crates/fparkan-runtime/src/script_time.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/script_time.rs | 74 |
1 files changed, 74 insertions, 0 deletions
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<ScriptInitWrite> { + 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<u32> { + 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)); + } +} |
