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.rs102
-rw-r--r--crates/fparkan-runtime/src/lib.rs17
2 files changed, 119 insertions, 0 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
new file mode 100644
index 00000000..7801eebd
--- /dev/null
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -0,0 +1,102 @@
+use crate::{LoadedMissionState, MissionEventStop};
+use std::collections::BTreeMap;
+
+#[derive(Default)]
+pub(super) struct ClanAiEvents {
+ next_ms: BTreeMap<usize, u32>,
+ stops: BTreeMap<(usize, usize, usize), (MissionEventStop, u64)>,
+}
+
+impl LoadedMissionState {
+ pub(super) fn tick_clan_ai_events(&mut self, now_ms: u32) {
+ let Some(varset) = &self.script_varset else {
+ return;
+ };
+ for bundle in self.script_bundles.iter().filter(|bundle| {
+ bundle.behavior_warm_start.is_some() && self.mission.clans[bundle.clan_index].mode == 2
+ }) {
+ let Some((event_index, event)) =
+ bundle.package.events.iter().enumerate().find(|(_, event)| {
+ event.name_raw == b"Problems0\0" && !event.instructions.is_empty()
+ })
+ else {
+ 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)
+ {
+ continue;
+ }
+ let Some(state) = self
+ .script_varset_states
+ .iter_mut()
+ .find(|state| state.clan_index == clan)
+ else {
+ continue;
+ };
+ let result = varset.declarations.execute_mission_prefix(
+ event,
+ bundle.formula_table.as_ref(),
+ &mut state.values,
+ |instruction, _| Err(format!("{:?}", instruction.dispatch_selector())),
+ );
+ if let Err((instruction_index, reason)) = result {
+ let (stop, count) = self
+ .clan_ai_events
+ .stops
+ .entry((clan, event_index, instruction_index))
+ .or_insert_with(|| {
+ (
+ MissionEventStop {
+ clan_index: clan,
+ script_path: bundle.script_path.clone(),
+ event_index,
+ instruction_index,
+ reason,
+ },
+ 0,
+ )
+ });
+ *count += 1;
+ if *count == 1 {
+ eprintln!("sim_coverage script_clan_ai_unsupported=1 clan={clan} script={} event={event_index} instruction={instruction_index} reason={}", stop.script_path, stop.reason);
+ }
+ }
+ }
+ }
+}
+
+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 stops(&self) -> impl Iterator<Item = (&MissionEventStop, u64)> {
+ self.stops.values().map(|(stop, count)| (stop, *count))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[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));
+ }
+}
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 9fa36f84..bfeb6ed1 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -44,6 +44,7 @@ use std::sync::Arc;
mod autodemo;
mod boarding;
mod building_door;
+mod clan_ai_events;
mod clan_script_init;
mod mesh_collision;
mod mining;
@@ -1355,6 +1356,7 @@ struct LoadedMissionState {
script_init_states: Vec<MissionScriptInitState>,
script_varset_states: Vec<MissionScriptVarSetState>,
mission_events: MissionEvents,
+ clan_ai_events: clan_ai_events::ClanAiEvents,
mine_sites: Vec<mining::MineSite>,
unit_patrol: Option<unit_patrol::UnitPatrol>,
player_pitch_bits: u32,
@@ -2684,6 +2686,7 @@ fn load_mission_with_options(
script_init_states: loaded_scripts.init_states,
script_varset_states: loaded_scripts.varset_states,
mission_events,
+ clan_ai_events: clan_ai_events::ClanAiEvents::default(),
mine_sites,
unit_patrol,
player_pitch_bits: 0,
@@ -2806,6 +2809,9 @@ pub fn step_headless(
.loaded
.as_mut()
.and_then(|state| state.tick_mission_event(&engine.world, queue_time_ms));
+ if let Some(loaded) = &mut engine.loaded {
+ loaded.tick_clan_ai_events(queue_time_ms);
+ }
let researched_component = engine
.loaded
.as_mut()
@@ -2891,6 +2897,8 @@ pub struct SimulationReport {
pub mission_events: u64,
/// Distinct uncovered periodic event instructions.
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)>,
/// Total requests for mission information messages.
pub mission_messages: u64,
/// Clock of the first autonomous shot, in milliseconds.
@@ -2924,6 +2932,7 @@ pub fn simulate_mission_report(
world_events: 0,
mission_events: 0,
mission_event_stops: Vec::new(),
+ clan_ai_event_stops: Vec::new(),
mission_messages: 0,
autodemo_first_shot_ms: None,
autodemo_first_destroyed_ms: None,
@@ -2968,6 +2977,14 @@ pub fn simulate_mission_report(
.count() as u64;
}
}
+ if let Some(loaded) = &engine.loaded {
+ report.clan_ai_event_stops.extend(
+ loaded
+ .clan_ai_events
+ .stops()
+ .map(|(stop, count)| (stop.clone(), count)),
+ );
+ }
Ok(report)
}