From d4e008fe33337494c0a28d9e35da9986a7dd42c7 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 25 Nov 2026 19:20:42 +0400 Subject: fix(ai): schedule clan cycles with native jitter and logical time Use a separate AI CRT stream for the 7000 + rand() % 1000 ms cycle. Require a strictly later deadline, reschedule late callbacks from actual arrival time, and advance script time by seven seconds per cycle. Count executed cycles by clan; retain explicit initial-phase and RNG-order gaps. Validated the native RNG through ai.dll and the timer in the running original. Workspace fmt/check/tests and all 29 stock 900-second reports pass. A stock-start mission-28 window ran through warbot and transport production and successive laboratory projects. This is not full G7 chronology acceptance; candidate properties for 65/69 remain stubbed. --- crates/fparkan-runtime/src/clan_ai_events.rs | 37 +++++++--------------------- 1 file changed, 9 insertions(+), 28 deletions(-) (limited to 'crates/fparkan-runtime/src/clan_ai_events.rs') diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index f4ba65bf..fcb35ca7 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -180,6 +180,7 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) -> Vec Vec, + clocks: crate::clan_clock::ClanClocks, stops: BTreeMap<(usize, usize, usize), (MissionEventStop, u64)>, research_stubs: BTreeMap<(usize, u8), u64>, pub(super) research_catalogs: BTreeMap, @@ -283,6 +284,7 @@ impl LoadedMissionState { world: &World, production: Option<&crate::production::Production>, vfs: Option<&dyn fparkan_vfs::Vfs>, + rng: &mut u32, now_ms: u32, notices: &mut Vec, ) -> Vec { @@ -290,7 +292,7 @@ impl LoadedMissionState { let Some(varset) = &self.script_varset else { return orders; }; - let clock = crate::script_time::elapsed_seconds(self.mission_events.start_ms, now_ms); + let elapsed_ms = now_ms.wrapping_sub(self.mission_events.start_ms); for bundle in self.script_bundles.iter().filter(|bundle| { bundle.behavior_warm_start.is_some() && self.mission.clans[bundle.clan_index].mode == 2 }) { @@ -302,13 +304,9 @@ impl LoadedMissionState { continue; }; let clan = bundle.clan_index; - // shortcut: use the base interval until the separate native AI clock and RNG are wired. - if !self - .clan_ai_events - .due(clan, self.mission_events.start_ms, now_ms) - { + let Some(clock) = self.clan_ai_events.clocks.tick(clan, elapsed_ms, rng) else { continue; - } + }; let Some(state) = self .script_varset_states .iter_mut() @@ -317,7 +315,7 @@ impl LoadedMissionState { continue; }; let mut problems_event = Some((event_index, event, None::)); - // shortcut: protection resumes on the shared clan interval without native RNG. + // shortcut: task continuations still share the clan cycle rather than per-task scheduling. let mut pending = self .clan_ai_events .problems @@ -1577,14 +1575,8 @@ impl ClanAiEvents { } } - fn due(&mut self, clan: usize, start_ms: u32, now_ms: u32) -> bool { - let next = self.next_ms.entry(clan).or_insert(7_000); - let elapsed = now_ms.wrapping_sub(start_ms); - if elapsed < *next { - return false; - } - *next = elapsed.saturating_add(7_000); - true + pub(super) fn clock_ticks(&self) -> impl Iterator + '_ { + self.clocks.ticks() } pub(super) fn stops(&self) -> impl Iterator { @@ -1937,17 +1929,6 @@ mod tests { assert!(loaded.clan_ai_events.waits.contains_key(key)); } - #[test] - fn clans_have_independent_due_times_and_no_replay_after_a_late_tick() { - let mut ai = ClanAiEvents::default(); - assert!(!ai.due(1, 100_000, 106_999)); - assert!(ai.due(1, 100_000, 107_000)); - assert!(ai.due(2, 100_000, 107_000)); - assert!(!ai.due(1, 100_000, 113_999)); - assert!(ai.due(1, 100_000, 120_000)); - assert!(!ai.due(1, 100_000, 120_000)); - } - #[test] fn free_mind_count_uses_only_the_native_sentinel() { assert_eq!(count_free_minds(&[]), 0); -- cgit v1.2.3