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 ++++---------- crates/fparkan-runtime/src/clan_clock.rs | 74 ++++++++++++++++++++++++++++ crates/fparkan-runtime/src/lib.rs | 9 ++++ 3 files changed, 92 insertions(+), 28 deletions(-) create mode 100644 crates/fparkan-runtime/src/clan_clock.rs (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 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); diff --git a/crates/fparkan-runtime/src/clan_clock.rs b/crates/fparkan-runtime/src/clan_clock.rs new file mode 100644 index 00000000..3a9a1a4f --- /dev/null +++ b/crates/fparkan-runtime/src/clan_clock.rs @@ -0,0 +1,74 @@ +use std::collections::BTreeMap; + +struct Clock { + deadline_ms: u32, + seconds: u32, + ticks: u64, +} + +impl Default for Clock { + fn default() -> Self { + Self { + // shortcut: the initial phase and registration's 1400 ms advance remain unwired. + deadline_ms: 7_000, + seconds: 0, + ticks: 0, + } + } +} + +#[derive(Default)] +pub(super) struct ClanClocks(BTreeMap); + +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(); + if elapsed_ms <= clock.deadline_ms { + return None; + } + let jitter = crate::behavior_clock::next_crt_rand(rng) % 1_000; + clock.deadline_ms = elapsed_ms.wrapping_add(7_000 + jitter); + clock.seconds = clock.seconds.wrapping_add(7); + clock.ticks += 1; + if clock.ticks == 1 { + eprintln!("sim_coverage script_clan_clock_approx=1 clan={clan}"); + } + Some(clock.seconds) + } + + pub(super) fn ticks(&self) -> impl Iterator + '_ { + self.0.iter().map(|(&clan, clock)| (clan, clock.ticks)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[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.tick(1, 7_000, &mut rng), None); + assert_eq!(rng, 1); + assert_eq!(clocks.tick(1, 7_001, &mut rng), Some(7)); + assert_eq!((clocks.0[&1].deadline_ms, rng), (14_042, 2_745_024)); + assert_eq!(clocks.tick(2, 7_001, &mut rng), Some(7)); + assert_eq!((clocks.0[&2].deadline_ms, rng), (14_468, 3_357_800_067)); + assert_eq!(clocks.tick(1, 14_042, &mut rng), None); + assert_eq!(rng, 3_357_800_067); + assert_eq!(clocks.tick(1, 14_043, &mut rng), Some(14)); + assert_eq!((clocks.0[&1].deadline_ms, rng), (21_377, 415_139_642)); + } + + #[test] + fn a_late_callback_advances_logical_time_once_and_schedules_from_actual_time() { + 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.tick(1, 100_000, &mut rng), None); + assert_eq!(clocks.ticks().collect::>(), [(1, 1)]); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 20d05295..b810eda8 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -50,6 +50,7 @@ mod clan_ai_events; mod clan_area_strength; mod clan_build_limits; mod clan_building_queries; +mod clan_clock; mod clan_combat_groups; mod clan_combat_strength; mod clan_mining; @@ -1341,6 +1342,7 @@ pub struct Engine { world: World, loaded: Option, behavior_rng_state: u32, + ai_rng_state: u32, autodemo_combat: Option, autodemo_reinforcements: Option, production: Option, @@ -1725,6 +1727,7 @@ pub fn create(services: EngineServices) -> Result { world: new_world(WorldConfig), loaded: None, behavior_rng_state: INITIAL_BEHAVIOR_RNG_STATE, + ai_rng_state: 1, autodemo_combat: None, autodemo_reinforcements: None, production: None, @@ -2914,6 +2917,8 @@ pub struct SimulationReport { pub mission_event_stops: Vec, /// Uncovered clan AI instructions and the number of invocations they stopped. pub clan_ai_event_stops: Vec<(MissionEventStop, u64)>, + /// Executed AI clock cycles by clan; initial phase and full RNG ordering remain approximate. + pub clan_ai_clock_ticks: Vec<(usize, u64)>, /// Calls to stubbed research policy or status, grouped by clan and handler. pub clan_research_status_stubs: Vec<(usize, u8, u64)>, pub clan_research_catalogs: Vec<(usize, usize, u32, u64)>, @@ -2988,6 +2993,7 @@ pub fn simulate_mission_report( mission_events: 0, mission_event_stops: Vec::new(), clan_ai_event_stops: Vec::new(), + clan_ai_clock_ticks: Vec::new(), clan_research_status_stubs: Vec::new(), clan_research_catalogs: Vec::new(), laboratory: Default::default(), @@ -3091,6 +3097,9 @@ pub fn simulate_mission_report( report .clan_research_status_stubs .extend(loaded.clan_ai_events.research_status_stubs()); + report + .clan_ai_clock_ticks + .extend(loaded.clan_ai_events.clock_ticks()); report .clan_research_catalogs .extend(loaded.clan_ai_events.research_catalogs()); -- cgit v1.2.3