diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 20:05:43 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 20:05:43 +0300 |
| commit | 6e18e9f05c1ae92a54dd671b6e47f3e21e299d4f (patch) | |
| tree | f1b7f39fcaf08a3d5beeab8be73aa0a3e3f65fd1 /crates/fparkan-runtime/src/clan_ai_events.rs | |
| parent | 58910ebe71100617abf6523827e2b04351709de9 (diff) | |
| download | fparkan-6e18e9f05c1ae92a54dd671b6e47f3e21e299d4f.tar.xz fparkan-6e18e9f05c1ae92a54dd671b6e47f3e21e299d4f.zip | |
feat(ai): expose clan roster counts to problem scripts
Handler(49) counts free native-format startup slots and Handler(34) reads the live clan roster. The Two Fortresses reaches the research-dependent Handler(65) from every scheduled Problems0 invocation.
Diffstat (limited to 'crates/fparkan-runtime/src/clan_ai_events.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_ai_events.rs | 40 |
1 files changed, 38 insertions, 2 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); + } } |
