From 85474bcd60f66b610985c90a697351837208528b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 25 Nov 2026 19:41:17 +0400 Subject: fix(ai): advance clan cycles when units register Advance the signed AI deadline by 1400 ms when an active clan receives a new unit. Connect production, construction, mines, reinforcements and ownership transfers through the shared spawn path. Preserve negative deadlines and count registration notifications per clan. Move the common spawn code and result type out of AutoDemo without changing its public API. Native ai.dll calls cover duplicate and missing IDs, the disabled guard, both registration kinds, negative deadlines and unchanged RNG. A live original warbot registration confirms the same advance. Workspace checks, the licensed stock factory timing test and all 29 900-second reports pass; a stock mission-28 window runs through successive factory outputs. Initial clock phase, complete RNG ordering and full G7 chronology remain open. --- crates/fparkan-runtime/src/clan_clock.rs | 108 +++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 7 deletions(-) (limited to 'crates/fparkan-runtime/src/clan_clock.rs') diff --git a/crates/fparkan-runtime/src/clan_clock.rs b/crates/fparkan-runtime/src/clan_clock.rs index 3a9a1a4f..f3ae89a8 100644 --- a/crates/fparkan-runtime/src/clan_clock.rs +++ b/crates/fparkan-runtime/src/clan_clock.rs @@ -1,18 +1,20 @@ use std::collections::BTreeMap; struct Clock { - deadline_ms: u32, + deadline_ms: i64, seconds: u32, ticks: u64, + registrations: u64, } impl Default for Clock { fn default() -> Self { Self { - // shortcut: the initial phase and registration's 1400 ms advance remain unwired. + // shortcut: keep the initial phase until a stock-start native comparison establishes it. deadline_ms: 7_000, seconds: 0, ticks: 0, + registrations: 0, } } } @@ -24,11 +26,11 @@ 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 { + if i64::from(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.deadline_ms = i64::from(elapsed_ms.wrapping_add(7_000 + jitter)); clock.seconds = clock.seconds.wrapping_add(7); clock.ticks += 1; if clock.ticks == 1 { @@ -37,8 +39,47 @@ impl ClanClocks { Some(clock.seconds) } - pub(super) fn ticks(&self) -> impl Iterator + '_ { - self.0.iter().map(|(&clan, clock)| (clan, clock.ticks)) + pub(super) fn advance_for_registration(&mut self, clan: usize) -> u64 { + let clock = self.0.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 + .iter() + .map(|(&clan, clock)| (clan, clock.ticks, clock.registrations)) + } +} + +impl crate::LoadedMissionState { + pub(super) fn note_clan_registration( + &mut self, + owner: fparkan_world::OwnerId, + unit: fparkan_world::ObjectHandle, + ) { + let clan = usize::from(owner.0); + if !self + .mission + .clans + .get(clan) + .is_some_and(|entry| entry.mode == 2) + || !self.script_bundles.iter().any(|bundle| { + bundle.clan_index == clan + && bundle.behavior_warm_start.is_some() + && bundle.package.events.iter().any(|event| { + event.name_raw == b"Problems0\0" && !event.instructions.is_empty() + }) + }) + { + return; + } + let count = self.clan_ai_events.advance_for_registration(clan); + eprintln!( + "script_clan_unit_registered clan={clan} unit={} generation={} count={count}", + unit.slot, unit.generation + ); } } @@ -69,6 +110,59 @@ mod tests { 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)]); + assert_eq!(clocks.ticks().collect::>(), [(1, 1, 0)]); + } + + #[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; + assert_eq!(clocks.advance_for_registration(1), 1); + assert_eq!(clocks.0[&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; + clocks.advance_for_registration(1); + assert_eq!(clocks.0[&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!(rng, 2_745_024); + assert_eq!(clocks.ticks().collect::>(), [(1, 1, 3)]); + } + + #[test] + #[ignore = "requires licensed mission resources"] + fn licensed_stock_factory_registration_brings_the_next_ai_cycle_forward() { + use crate::{ + create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").unwrap(); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/Single.01/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + for now_ms in (0..=28_000).step_by(100) { + frame(&mut engine, now_ms).unwrap(); + } + let loaded = engine.loaded.as_ref().unwrap(); + assert_eq!( + loaded.clan_ai_events.clock_ticks().collect::>(), + [(1, 4, 1)] + ); + assert!(loaded.clan_ai_events.stops().next().is_none()); } } -- cgit v1.2.3