aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs37
-rw-r--r--crates/fparkan-runtime/src/clan_clock.rs74
-rw-r--r--crates/fparkan-runtime/src/lib.rs9
3 files changed, 92 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);
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)]);
+ }
+}
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<LoadedMissionState>,
behavior_rng_state: u32,
+ ai_rng_state: u32,
autodemo_combat: Option<unit_combat::AutoDemoCombat>,
autodemo_reinforcements: Option<autodemo::LiveReinforcements>,
production: Option<production::Production>,
@@ -1725,6 +1727,7 @@ pub fn create(services: EngineServices) -> Result<Engine, EngineError> {
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<MissionEventStop>,
/// 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(),
@@ -3092,6 +3098,9 @@ pub fn simulate_mission_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());
report.laboratory = loaded.laboratories.report();