From c63fadd7c2aa0e0d9f81f65aa88ef9fe905e76e3 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 25 Nov 2026 20:47:00 +0400 Subject: fix(ai): age script deadlines while the world is paused Record paused wall time separately from simulation time. After resuming, Mission and clan AI check their expired deadlines once, then schedule the next interval without replaying missed cycles. Move application pause handling into its own module and retain frozen world, animation and audio. Live ai.dll timer calls confirm that Mission runs before the paused world clock refreshes and the next AI callback uses an already expired deadline. Record the unresolved AutoDemo sphere and first-projectile boundaries. Validation: workspace fmt/check/tests, release build, licensed stock-clock pause test, all 29 missions at --sim-report 900 without script stops, and a stock mission-28 window with a 33-second pause and unchanged world time. Full AI chronology and minimized-window timing remain open. --- crates/fparkan-runtime/src/clan_ai_events.rs | 8 +++ crates/fparkan-runtime/src/clan_clock.rs | 97 +++++++++++++++++++++++----- crates/fparkan-runtime/src/lib.rs | 1 + crates/fparkan-runtime/src/mission_events.rs | 19 +++++- 4 files changed, 108 insertions(+), 17 deletions(-) (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 85062060..455ee91f 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -1607,6 +1607,14 @@ impl ClanAiEvents { self.clocks.seconds(clan) } + pub(super) fn wall_time_ms(&self, queue_ms: u32) -> u32 { + self.clocks.wall_time_ms(queue_ms) + } + + pub(super) fn add_paused_time(&mut self, paused_ms: u32) { + self.clocks.add_paused_time(paused_ms); + } + pub(super) fn clock_ticks(&self) -> impl Iterator + '_ { self.clocks.ticks() } diff --git a/crates/fparkan-runtime/src/clan_clock.rs b/crates/fparkan-runtime/src/clan_clock.rs index dd61a959..586157df 100644 --- a/crates/fparkan-runtime/src/clan_clock.rs +++ b/crates/fparkan-runtime/src/clan_clock.rs @@ -20,12 +20,16 @@ impl Default for Clock { } #[derive(Default)] -pub(super) struct ClanClocks(BTreeMap); +pub(super) struct ClanClocks { + clans: BTreeMap, + paused_ms: u32, +} impl ClanClocks { /// ai.dll RVA 0x17ae: equality is not due; a late callback runs one cycle. pub(super) fn tick(&mut self, clan: usize, elapsed_ms: u32, rng: &mut u32) -> Option { - let clock = self.0.entry(clan).or_default(); + let elapsed_ms = self.wall_time_ms(elapsed_ms); + let clock = self.clans.entry(clan).or_default(); if i64::from(elapsed_ms) <= clock.deadline_ms { return None; } @@ -40,20 +44,37 @@ impl ClanClocks { } pub(super) fn advance_for_registration(&mut self, clan: usize) -> u64 { - let clock = self.0.entry(clan).or_default(); + let clock = self.clans.entry(clan).or_default(); clock.deadline_ms = clock.deadline_ms.wrapping_sub(1_400); clock.registrations += 1; clock.registrations } pub(super) fn ticks(&self) -> impl Iterator + '_ { - self.0 + self.clans .iter() .map(|(&clan, clock)| (clan, clock.ticks, clock.registrations)) } pub(super) fn seconds(&self, clan: usize) -> u32 { - self.0.get(&clan).map_or(0, |clock| clock.seconds) + self.clans.get(&clan).map_or(0, |clock| clock.seconds) + } + + pub(super) fn wall_time_ms(&self, queue_ms: u32) -> u32 { + queue_ms.wrapping_add(self.paused_ms) + } + + pub(super) fn add_paused_time(&mut self, paused_ms: u32) { + self.paused_ms = self.paused_ms.wrapping_add(paused_ms); + } +} + +/// Records wall time excluded by the caller's paused world clock. +/// The next frame checks AI deadlines once, without advancing the world or replaying missed cycles. +pub fn note_mission_pause_elapsed(engine: &mut crate::Engine, elapsed_ms: u32) { + if let Some(loaded) = &mut engine.loaded { + loaded.clan_ai_events.add_paused_time(elapsed_ms); + eprintln!("script_ai_pause_elapsed duration_ms={elapsed_ms}"); } } @@ -83,19 +104,42 @@ impl crate::LoadedMissionState { mod tests { use super::*; + #[test] + fn pause_ages_deadlines_without_ticking_or_replaying_missed_cycles() { + let mut clocks = ClanClocks::default(); + let mut rng = 1; + assert_eq!(clocks.tick(0, 0, &mut rng), Some(7)); + assert_eq!(clocks.tick(1, 0, &mut rng), Some(7)); + let before_pause = rng; + clocks.add_paused_time(22_300); + assert_eq!( + (clocks.seconds(0), clocks.seconds(1), rng), + (7, 7, before_pause) + ); + assert_eq!(clocks.tick(0, 500, &mut rng), Some(14)); + assert_eq!(clocks.tick(1, 500, &mut rng), Some(14)); + let resumed_rng = rng; + assert_eq!(clocks.tick(0, 500, &mut rng), None); + assert_eq!(clocks.tick(1, 500, &mut rng), None); + assert_eq!(rng, resumed_rng); + assert_eq!(clocks.clans[&0].deadline_ms, 22_800 + 7_334); + assert_eq!(clocks.tick(0, 7_834, &mut rng), None); + assert_eq!(clocks.tick(0, 7_835, &mut rng), Some(21)); + } + #[test] fn native_rng_jitter_is_shared_between_clans_and_equality_does_not_consume_it() { let mut clocks = ClanClocks::default(); let mut rng = 1; assert_eq!(clocks.seconds(0), 0); assert_eq!(clocks.tick(0, 0, &mut rng), Some(7)); - assert_eq!((clocks.0[&0].deadline_ms, rng), (7_041, 2_745_024)); + assert_eq!((clocks.clans[&0].deadline_ms, rng), (7_041, 2_745_024)); assert_eq!(clocks.tick(1, 0, &mut rng), Some(7)); - assert_eq!((clocks.0[&1].deadline_ms, rng), (7_467, 3_357_800_067)); + assert_eq!((clocks.clans[&1].deadline_ms, rng), (7_467, 3_357_800_067)); assert_eq!(clocks.tick(0, 7_041, &mut rng), None); assert_eq!(rng, 3_357_800_067); assert_eq!(clocks.tick(0, 7_042, &mut rng), Some(14)); - assert_eq!((clocks.0[&0].deadline_ms, rng), (14_376, 415_139_642)); + assert_eq!((clocks.clans[&0].deadline_ms, rng), (14_376, 415_139_642)); assert_eq!((clocks.seconds(0), clocks.seconds(1)), (14, 7)); } @@ -104,7 +148,7 @@ mod tests { let mut clocks = ClanClocks::default(); let mut rng = 1; assert_eq!(clocks.tick(1, 100_000, &mut rng), Some(7)); - assert_eq!(clocks.0[&1].deadline_ms, 107_041); + assert_eq!(clocks.clans[&1].deadline_ms, 107_041); assert_eq!(clocks.tick(1, 100_000, &mut rng), None); assert_eq!(clocks.ticks().collect::>(), [(1, 1, 0)]); } @@ -112,17 +156,17 @@ mod tests { #[test] fn native_registration_shift_accumulates_and_a_negative_deadline_is_due() { let mut clocks = ClanClocks::default(); - clocks.0.entry(1).or_default().deadline_ms = 8_000; + clocks.clans.entry(1).or_default().deadline_ms = 8_000; assert_eq!(clocks.advance_for_registration(1), 1); - assert_eq!(clocks.0[&1].deadline_ms, 6_600); + assert_eq!(clocks.clans[&1].deadline_ms, 6_600); assert_eq!(clocks.advance_for_registration(1), 2); - assert_eq!(clocks.0[&1].deadline_ms, 5_200); - clocks.0.get_mut(&1).unwrap().deadline_ms = 500; + assert_eq!(clocks.clans[&1].deadline_ms, 5_200); + clocks.clans.get_mut(&1).unwrap().deadline_ms = 500; clocks.advance_for_registration(1); - assert_eq!(clocks.0[&1].deadline_ms, -900); + assert_eq!(clocks.clans[&1].deadline_ms, -900); let mut rng = 1; assert_eq!(clocks.tick(1, 0, &mut rng), Some(7)); - assert_eq!(clocks.0[&1].deadline_ms, 7_041); + assert_eq!(clocks.clans[&1].deadline_ms, 7_041); assert_eq!(rng, 2_745_024); assert_eq!(clocks.ticks().collect::>(), [(1, 1, 3)]); } @@ -171,5 +215,28 @@ mod tests { [(0, 4, 0), (1, 4, 1)] ); assert!(loaded.clan_ai_events.stops().next().is_none()); + note_mission_pause_elapsed(&mut engine, 22_300); + frame(&mut engine, 28_000).unwrap(); + assert_eq!( + engine + .loaded + .as_ref() + .unwrap() + .clan_ai_events + .clock_ticks() + .collect::>(), + [(0, 5, 0), (1, 5, 1)] + ); + frame(&mut engine, 28_000).unwrap(); + assert_eq!( + engine + .loaded + .as_ref() + .unwrap() + .clan_ai_events + .clock_ticks() + .collect::>(), + [(0, 5, 0), (1, 5, 1)] + ); } } diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 111633a9..accf0716 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -51,6 +51,7 @@ mod clan_area_strength; mod clan_build_limits; mod clan_building_queries; mod clan_clock; +pub use clan_clock::note_mission_pause_elapsed; mod clan_combat_groups; mod clan_combat_strength; mod clan_mining; diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index e8da353c..b200d0b9 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -779,11 +779,13 @@ impl LoadedMissionState { let inside_exit = exit_event.is_some() && self.hero_in_teleport_exit(world); let entered_exit = inside_exit && !self.mission_events.inside_hero_exit; self.mission_events.inside_hero_exit = inside_exit; - // shortcut: dispatch Mission on the shared 2 s queue timer until its native cadence is wired. let (event_index, event) = if entered_exit && self.script_outcome == super::MissionScriptOutcome::Active { exit_event? - } else if self.mission_events.due(now_ms) { + } else if self + .mission_events + .due(self.clan_ai_events.wall_time_ms(now_ms)) + { events.find(|(_, event)| { event.name_raw.split(|byte| *byte == 0).next() == Some(b"Mission") })? @@ -1364,4 +1366,17 @@ mod tests { assert!(!events.due(12_122)); assert!(events.due(12_123)); } + + #[test] + fn mission_timer_is_due_after_a_long_pause_without_advancing_the_world_clock() { + let mut events = MissionEvents::default(); + let mut clocks = crate::clan_ai_events::ClanAiEvents::default(); + assert!(events.due(clocks.wall_time_ms(0))); + assert!(!events.due(clocks.wall_time_ms(500))); + clocks.add_paused_time(22_300); + assert!(events.due(clocks.wall_time_ms(500))); + assert!(!events.due(clocks.wall_time_ms(500))); + assert!(!events.due(clocks.wall_time_ms(2_499))); + assert!(events.due(clocks.wall_time_ms(2_500))); + } } -- cgit v1.2.3