aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/clan_clock.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-25 18:20:42 +0300
committerValentin Popov <valentin@popov.link>2026-11-25 18:20:42 +0300
commitd4e008fe33337494c0a28d9e35da9986a7dd42c7 (patch)
tree46a5f96065f4be457a5f84d90ce4feeeb88b2cda /crates/fparkan-runtime/src/clan_clock.rs
parentf0e3a84cf5c0d7a0edd890c303299306c513ba8b (diff)
downloadfparkan-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_clock.rs')
-rw-r--r--crates/fparkan-runtime/src/clan_clock.rs74
1 files changed, 74 insertions, 0 deletions
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<usize, Clock>);
+
+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<u32> {
+ 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<Item = (usize, u64)> + '_ {
+ 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::<Vec<_>>(), [(1, 1)]);
+ }
+}