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.rs54
-rw-r--r--crates/fparkan-runtime/src/clan_clock.rs57
-rw-r--r--crates/fparkan-runtime/src/clan_combat_groups.rs33
-rw-r--r--crates/fparkan-runtime/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs4
-rw-r--r--crates/fparkan-runtime/src/script_time.rs5
6 files changed, 109 insertions, 46 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index be2d46fe..85062060 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -293,9 +293,15 @@ impl LoadedMissionState {
return orders;
};
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
- }) {
+ for bundle in self
+ .script_bundles
+ .iter()
+ .filter(|bundle| bundle.behavior_warm_start.is_some())
+ {
+ let clan = bundle.clan_index;
+ let Some(clock) = self.clan_ai_events.clocks.tick(clan, elapsed_ms, rng) else {
+ continue;
+ };
let Some((event_index, event)) =
bundle.package.events.iter().enumerate().find(|(_, event)| {
event.name_raw == b"Problems0\0" && !event.instructions.is_empty()
@@ -303,10 +309,6 @@ impl LoadedMissionState {
else {
continue;
};
- let clan = bundle.clan_index;
- let Some(clock) = self.clan_ai_events.clocks.tick(clan, elapsed_ms, rng) else {
- continue;
- };
let Some(state) = self
.script_varset_states
.iter_mut()
@@ -563,8 +565,9 @@ impl LoadedMissionState {
let word = |index: u32| values.get(index as usize)?.as_dword();
let (&mode, rest) = instruction.references.split_first()
.ok_or("Handler(25) requires a recruitment mode")?;
- let mode = word(mode).filter(|mode| matches!(mode, 1 | 2))
- .ok_or("Handler(25) requires TAKE_ALL_FREE or TAKE_BY_HITS")?;
+ let mode = word(mode).filter(|mode| matches!(mode, 1..=3))
+ .ok_or("Handler(25) requires a supported recruitment mode")?;
+ let problem_kind = problem.as_ref().expect("recruitment requires a problem").key.word_0;
let count = self.clan_ai_events.group_recruitments.entry((clan, mode)).or_default();
*count += 1;
let first_recruitment = *count == 1;
@@ -572,17 +575,27 @@ impl LoadedMissionState {
// shortcut: infer roster occupancy from runtime jobs until native problem-slot ownership is wired.
let units = self.mission_events.clan_units(world, owner, mode != 1)
.filter(|&unit| !self.mission_events.is_reserved(owner, unit)
- && !self.clan_ai_events.unit_assigned(unit)
+ && (!self.clan_ai_events.unit_assigned(unit)
+ || mode == 3 && self.clan_ai_events.combat_assignment(unit)
+ .is_some_and(|kind| crate::clan_combat_groups::battle_assignment_available(Some(kind), problem_kind)))
&& !self.construction.active(unit)
&& !self.laboratories.active(unit)
&& !production.is_some_and(|queue| queue.is_building_at(unit, owner))
&& !orders.iter().any(|order: &ScriptFactoryOrder| order.plant == unit))
- .filter(|&unit| mode == 1 || self.unit_patrol.as_ref().is_none_or(|patrol| !patrol.is_shut_down(unit)));
- if mode == 1 {
+ .filter(|&unit| mode != 2 || self.unit_patrol.as_ref().is_none_or(|patrol| !patrol.is_shut_down(unit)));
+ if mode != 2 {
if !rest.is_empty() {
- return Err("Handler(25) TAKE_ALL_FREE takes one operand".into());
+ return Err("Handler(25) all-unit recruitment takes one operand".into());
}
group = crate::clan_combat_groups::recruit_all(units);
+ if mode == 3 {
+ if let Some(recruited) = &group {
+ for previous in self.clan_ai_events.groups.values_mut() {
+ previous.members.retain(|unit| !recruited.members.contains(unit));
+ }
+ self.clan_ai_events.capture_orders.retain(|(_, _, order)| !recruited.members.contains(&order.unit));
+ }
+ }
if first_recruitment {
eprintln!("sim_coverage script_group_recruitment_approx=1 clan={clan} mode={mode} units={:?}", group.as_ref().map(|group| group.members.iter().map(|unit| unit.slot).collect::<Vec<_>>()).unwrap_or_default());
}
@@ -1454,6 +1467,17 @@ impl ClanAiEvents {
.any(|group| group.members.contains(&unit))
}
+ fn combat_assignment(&self, unit: ObjectHandle) -> Option<u32> {
+ self.groups
+ .iter()
+ .find_map(|((_, key), group)| group.members.contains(&unit).then_some(key.word_0))
+ .or_else(|| {
+ self.capture_orders
+ .iter()
+ .find_map(|(_, key, order)| (order.unit == unit).then_some(key.word_0))
+ })
+ }
+
pub(super) fn group_orders(&self) -> impl Iterator<Item = (usize, u32, u64)> + '_ {
self.group_orders
.iter()
@@ -1579,6 +1603,10 @@ impl ClanAiEvents {
self.clocks.advance_for_registration(clan)
}
+ pub(super) fn clock_seconds(&self, clan: usize) -> u32 {
+ self.clocks.seconds(clan)
+ }
+
pub(super) fn clock_ticks(&self) -> impl Iterator<Item = (usize, u64, u64)> + '_ {
self.clocks.ticks()
}
diff --git a/crates/fparkan-runtime/src/clan_clock.rs b/crates/fparkan-runtime/src/clan_clock.rs
index f3ae89a8..dd61a959 100644
--- a/crates/fparkan-runtime/src/clan_clock.rs
+++ b/crates/fparkan-runtime/src/clan_clock.rs
@@ -10,8 +10,8 @@ struct Clock {
impl Default for Clock {
fn default() -> Self {
Self {
- // shortcut: keep the initial phase until a stock-start native comparison establishes it.
- deadline_ms: 7_000,
+ // A fresh native context is already due; do not reproduce its uninitialized heap bytes.
+ deadline_ms: -1,
seconds: 0,
ticks: 0,
registrations: 0,
@@ -51,6 +51,10 @@ impl ClanClocks {
.iter()
.map(|(&clan, clock)| (clan, clock.ticks, clock.registrations))
}
+
+ pub(super) fn seconds(&self, clan: usize) -> u32 {
+ self.0.get(&clan).map_or(0, |clock| clock.seconds)
+ }
}
impl crate::LoadedMissionState {
@@ -61,17 +65,9 @@ impl crate::LoadedMissionState {
) {
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()
- })
- })
+ .script_bundles
+ .iter()
+ .any(|bundle| bundle.clan_index == clan && bundle.behavior_warm_start.is_some())
{
return;
}
@@ -91,16 +87,16 @@ mod tests {
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!(clocks.seconds(0), 0);
+ assert_eq!(clocks.tick(0, 0, &mut rng), Some(7));
+ assert_eq!((clocks.0[&0].deadline_ms, rng), (7_041, 2_745_024));
+ assert_eq!(clocks.tick(1, 0, &mut rng), Some(7));
+ assert_eq!((clocks.0[&1].deadline_ms, rng), (7_467, 3_357_800_067));
+ assert_eq!(clocks.tick(0, 7_041, &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));
+ assert_eq!(clocks.tick(0, 7_042, &mut rng), Some(14));
+ assert_eq!((clocks.0[&0].deadline_ms, rng), (14_376, 415_139_642));
+ assert_eq!((clocks.seconds(0), clocks.seconds(1)), (14, 7));
}
#[test]
@@ -133,7 +129,7 @@ mod tests {
#[test]
#[ignore = "requires licensed mission resources"]
- fn licensed_stock_factory_registration_brings_the_next_ai_cycle_forward() {
+ fn licensed_stock_clocks_start_immediately_and_include_the_player_context() {
use crate::{
create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest,
MissionStartupInput,
@@ -155,13 +151,24 @@ mod tests {
None,
)
.unwrap();
- for now_ms in (0..=28_000).step_by(100) {
+ frame(&mut engine, 0).unwrap();
+ assert_eq!(
+ engine
+ .loaded
+ .as_ref()
+ .unwrap()
+ .clan_ai_events
+ .clock_ticks()
+ .collect::<Vec<_>>(),
+ [(0, 1, 0), (1, 1, 0)]
+ );
+ for now_ms in (100..=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::<Vec<_>>(),
- [(1, 4, 1)]
+ [(0, 4, 0), (1, 4, 1)]
);
assert!(loaded.clan_ai_events.stops().next().is_none());
}
diff --git a/crates/fparkan-runtime/src/clan_combat_groups.rs b/crates/fparkan-runtime/src/clan_combat_groups.rs
index b1476df0..71d8a2a5 100644
--- a/crates/fparkan-runtime/src/clan_combat_groups.rs
+++ b/crates/fparkan-runtime/src/clan_combat_groups.rs
@@ -85,6 +85,11 @@ pub(super) fn recruit_all(units: impl Iterator<Item = ObjectHandle>) -> Option<C
})
}
+pub(super) fn battle_assignment_available(assigned_kind: Option<u32>, current_kind: u32) -> bool {
+ // ai.dll+0xac36: TAKE_ALL_BATTLE_UNITS keeps units already assigned to this problem kind.
+ assigned_kind != Some(current_kind)
+}
+
pub(super) fn recruit(
world: &World,
units: impl Iterator<Item = ObjectHandle>,
@@ -208,6 +213,34 @@ mod tests {
use super::*;
#[test]
+ fn battle_group_takes_other_problem_kinds_but_keeps_matching_assignments() {
+ // ai.dll+0xab85 oracle: roster classes [warbot, transport, building, warbot].
+ // Reserved 0xfffe is excluded before recruitment, as in MissionEvents::clan_units.
+ for (jobs, expected) in [
+ ([None, None], vec![0, 3]),
+ ([Some(12), Some(12)], vec![]),
+ ([Some(8), Some(8)], vec![0, 3]),
+ ] {
+ let recruited = recruit_all([0, 3].into_iter().zip(jobs).filter_map(|(slot, job)| {
+ battle_assignment_available(job, 12).then_some(ObjectHandle {
+ slot,
+ generation: 1,
+ })
+ }));
+ assert_eq!(
+ recruited
+ .map(|group| group
+ .members
+ .into_iter()
+ .map(|unit| unit.slot)
+ .collect::<Vec<_>>())
+ .unwrap_or_default(),
+ expected
+ );
+ }
+ }
+
+ #[test]
fn recruits_by_distance_until_the_threshold_and_keeps_partial_groups() {
let [a, b, c] = [1, 2, 3].map(|slot| ObjectHandle {
slot,
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index c2c25634..111633a9 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -2917,7 +2917,7 @@ 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)>,
- /// Clan index, executed AI cycles and new registrations; initial phase and RNG order remain approximate.
+ /// Clan index, executed AI cycles and new registrations; full RNG ordering remains approximate.
pub clan_ai_clock_ticks: Vec<(usize, u64, u64)>,
/// Calls to stubbed research policy or status, grouped by clan and handler.
pub clan_research_status_stubs: Vec<(usize, u8, u64)>,
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index db37851f..e8da353c 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -779,7 +779,7 @@ impl LoadedMissionState {
let inside_exit = exit_event.is_some() && self.hero_in_teleport_exit(world);
let entered_exit = inside_exit && !self.mission_events.inside_hero_exit;
self.mission_events.inside_hero_exit = inside_exit;
- // shortcut: use the caller's queue clock until the AI wall clock is wired.
+ // shortcut: dispatch Mission on the shared 2 s queue timer until its native cadence is wired.
let (event_index, event) =
if entered_exit && self.script_outcome == super::MissionScriptOutcome::Active {
exit_event?
@@ -796,7 +796,7 @@ impl LoadedMissionState {
.iter_mut()
.find(|state| state.clan_index == bundle.clan_index)?;
let mission_events = &mut self.mission_events;
- let clock_seconds = crate::script_time::elapsed_seconds(mission_events.start_ms, now_ms);
+ let clock_seconds = self.clan_ai_events.clock_seconds(bundle.clan_index);
let mut notices = Vec::new();
let mut patrol_orders = Vec::new();
let execution = declarations.execute_mission_prefix(
diff --git a/crates/fparkan-runtime/src/script_time.rs b/crates/fparkan-runtime/src/script_time.rs
index 55e3f3d1..86ba44b9 100644
--- a/crates/fparkan-runtime/src/script_time.rs
+++ b/crates/fparkan-runtime/src/script_time.rs
@@ -35,11 +35,6 @@ pub(super) fn result(
}
}
-// shortcut: approximate the native AI scheduler's logical seconds by mission elapsed seconds.
-pub(super) fn elapsed_seconds(start_ms: u32, now_ms: u32) -> u32 {
- now_ms.wrapping_sub(start_ms) / 1_000
-}
-
#[cfg(test)]
mod tests {
use super::*;