diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-25 18:20:42 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-25 18:20:42 +0300 |
| commit | d4e008fe33337494c0a28d9e35da9986a7dd42c7 (patch) | |
| tree | 46a5f96065f4be457a5f84d90ce4feeeb88b2cda /crates/fparkan-runtime/src/clan_ai_events.rs | |
| parent | f0e3a84cf5c0d7a0edd890c303299306c513ba8b (diff) | |
| download | fparkan-d4e008fe33337494c0a28d9e35da9986a7dd42c7.tar.xz fparkan-d4e008fe33337494c0a28d9e35da9986a7dd42c7.zip | |
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.
Diffstat (limited to 'crates/fparkan-runtime/src/clan_ai_events.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_ai_events.rs | 37 |
1 files changed, 9 insertions, 28 deletions
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<MissionNotice &engine.world, engine.production.as_ref(), engine.services.vfs.as_deref(), + &mut engine.ai_rng_state, now_ms, &mut notices, ) { @@ -234,7 +235,7 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) -> Vec<MissionNotice #[derive(Default)] pub(super) struct ClanAiEvents { - next_ms: BTreeMap<usize, u32>, + clocks: crate::clan_clock::ClanClocks, stops: BTreeMap<(usize, usize, usize), (MissionEventStop, u64)>, research_stubs: BTreeMap<(usize, u8), u64>, pub(super) research_catalogs: BTreeMap<usize, crate::clan_research::ClanResearch>, @@ -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<MissionNotice>, ) -> Vec<ScriptFactoryOrder> { @@ -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::<Handler2Record>)); - // 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<Item = (usize, u64)> + '_ { + self.clocks.ticks() } pub(super) fn stops(&self) -> impl Iterator<Item = (&MissionEventStop, u64)> { @@ -1938,17 +1930,6 @@ mod tests { } #[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); assert_eq!(count_free_minds(&[u32::MAX, 0, u32::MAX, 1, 0xfffe]), 2); |
