aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs40
-rw-r--r--crates/fparkan-runtime/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs14
3 files changed, 48 insertions, 8 deletions
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index 7801eebd..caca586c 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -1,4 +1,6 @@
use crate::{LoadedMissionState, MissionEventStop};
+use fparkan_script::{ScriptDispatchSelector, VarSetDefault};
+use fparkan_world::{OwnerId, World};
use std::collections::BTreeMap;
#[derive(Default)]
@@ -8,7 +10,7 @@ pub(super) struct ClanAiEvents {
}
impl LoadedMissionState {
- pub(super) fn tick_clan_ai_events(&mut self, now_ms: u32) {
+ pub(super) fn tick_clan_ai_events(&mut self, world: &World, now_ms: u32) {
let Some(varset) = &self.script_varset else {
return;
};
@@ -41,7 +43,31 @@ impl LoadedMissionState {
event,
bundle.formula_table.as_ref(),
&mut state.values,
- |instruction, _| Err(format!("{:?}", instruction.dispatch_selector())),
+ |instruction, values| match instruction.dispatch_selector() {
+ ScriptDispatchSelector::Handler(49) if instruction.references.is_empty() => {
+ let slots = self
+ .ai_startup_states
+ .iter()
+ .find(|state| state.clan_index == clan);
+ Ok(Some(
+ slots.map_or(0, |state| count_free_minds(&state.record_slots)),
+ ))
+ }
+ ScriptDispatchSelector::Handler(34) => {
+ let &[index] = instruction.references.as_slice() else {
+ return Err("Handler(34) requires one DWORD operand".to_owned());
+ };
+ let Some(&VarSetDefault::Dword(class)) = values.get(index as usize) else {
+ return Err("Handler(34) requires one DWORD operand".to_owned());
+ };
+ Ok(Some(self.mission_events.count_clan_class(
+ world,
+ OwnerId(clan as u16),
+ class,
+ )))
+ }
+ selector => Err(format!("{selector:?}")),
+ },
);
if let Err((instruction_index, reason)) = result {
let (stop, count) = self
@@ -69,6 +95,10 @@ impl LoadedMissionState {
}
}
+fn count_free_minds(slots: &[u32]) -> u32 {
+ slots.iter().filter(|&&word| word == u32::MAX).count() as u32
+}
+
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);
@@ -99,4 +129,10 @@ mod tests {
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/lib.rs b/crates/fparkan-runtime/src/lib.rs
index bfeb6ed1..3b857586 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -2810,7 +2810,7 @@ pub fn step_headless(
.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);
+ loaded.tick_clan_ai_events(&engine.world, queue_time_ms);
}
let researched_component = engine
.loaded
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index 1c900819..b97b154f 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -152,6 +152,12 @@ pub(super) struct MissionEvents {
}
impl MissionEvents {
+ pub(super) fn count_clan_class(&self, world: &World, owner: OwnerId, class: u32) -> u32 {
+ clan_object_classes(&self.placed_clan_objects, world, owner)
+ .filter(|&candidate| candidate == class)
+ .count() as u32
+ }
+
pub(super) fn set_reservation(
&mut self,
mission: &super::MissionDocument,
@@ -714,13 +720,11 @@ impl LoadedMissionState {
ScriptDispatchSelector::Handler(34) => {
let class = single_dword(instruction, values)
.ok_or("Handler(34) requires one DWORD operand")?;
- let count = clan_object_classes(
- &mission_events.placed_clan_objects,
+ let count = mission_events.count_clan_class(
world,
OwnerId(bundle.clan_index as u16),
- )
- .filter(|&candidate| candidate == class)
- .count() as u32;
+ class,
+ );
Ok(Some(count))
}
ScriptDispatchSelector::Handler(52) => {