aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/mission_events.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 05:06:04 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 05:06:04 +0300
commitdb3f404b3d6d24144c49ae5248262ab1d8d55e21 (patch)
tree1c23b1d4bb101460c1c1a601fa232c00d2ec0c60 /crates/fparkan-runtime/src/mission_events.rs
parentbadbf2e74fceecadba1487b0863825e180e61c85 (diff)
downloadfparkan-db3f404b3d6d24144c49ae5248262ab1d8d55e21.tar.xz
fparkan-db3f404b3d6d24144c49ae5248262ab1d8d55e21.zip
feat(runtime): count live clan objects in mission scripts
Diffstat (limited to 'crates/fparkan-runtime/src/mission_events.rs')
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs100
1 files changed, 85 insertions, 15 deletions
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index 6151d704..02706a33 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -1,6 +1,8 @@
-use super::LoadedMissionState;
-use fparkan_script::{ScriptDispatchSelector, VarSetDefault};
-use fparkan_world::{handle_by_original_id, transform_state, OriginalObjectId, World};
+use super::{LoadedMissionState, MissionObjectDraft, LAST_SUPER_AI_TABLE_INDEX};
+use fparkan_script::{ScriptDispatchSelector, ScriptInstruction, VarSetDefault};
+use fparkan_world::{
+ handle_by_original_id, transform_state, ObjectHandle, OriginalObjectId, World,
+};
use std::collections::HashSet;
/// First encounter with an uncovered periodic script instruction.
@@ -32,9 +34,30 @@ pub(super) struct MissionEvents {
next_ms: u32,
reported_stops: HashSet<usize>,
voiced_messages: HashSet<u32>,
+ placed_clan_objects: Vec<Vec<(ObjectHandle, u32)>>,
}
impl MissionEvents {
+ pub(super) fn with_placed_objects(
+ drafts: &[MissionObjectDraft],
+ handles: &[ObjectHandle],
+ clan_count: usize,
+ ) -> Self {
+ let mut rosters = vec![Vec::new(); clan_count.min(LAST_SUPER_AI_TABLE_INDEX as usize + 1)];
+ for (draft, &handle) in drafts.iter().zip(handles) {
+ if let (Some(roster), Some(class)) = (
+ rosters.get_mut(draft.raw_after_resource as usize),
+ draft.unit_dat_header_flags,
+ ) {
+ roster.push((handle, class));
+ }
+ }
+ Self {
+ placed_clan_objects: rosters,
+ ..Self::default()
+ }
+ }
+
fn due(&mut self, now_ms: u32) -> bool {
if now_ms < self.next_ms {
return false;
@@ -74,6 +97,7 @@ impl LoadedMissionState {
.script_varset_states
.iter_mut()
.find(|state| state.clan_index == bundle.clan_index)?;
+ let mission_events = &mut self.mission_events;
let mut messages = Vec::new();
let stop = declarations
.execute_mission_prefix(
@@ -81,16 +105,21 @@ impl LoadedMissionState {
bundle.formula_table.as_ref(),
&mut state.values,
|instruction, values| match instruction.dispatch_selector() {
+ ScriptDispatchSelector::Handler(31) => {
+ let (clan, mask) = dword_pair(instruction, values)
+ .ok_or("Handler(31) requires two DWORD operands")?;
+ let count = self
+ .behavior_distributors
+ .get(clan as usize)
+ .filter(|distributor| distributor.super_ai_visible)
+ .and_then(|_| mission_events.placed_clan_objects.get(clan as usize))
+ .map_or(0, |objects| count_clan_objects(objects, world, mask));
+ Ok(Some(count))
+ }
ScriptDispatchSelector::Handler(32) => {
- let [area, id] = *instruction.references.as_slice() else {
- return Err("Handler(32) requires two DWORD operands".into());
- };
- let (Some(VarSetDefault::Dword(area)), Some(VarSetDefault::Dword(id))) =
- (values.get(area as usize), values.get(id as usize))
- else {
- return Err("Handler(32) requires two DWORD operands".into());
- };
- let inside = tactical_area_contains(&self.mission, world, *area, *id);
+ let (area, id) = dword_pair(instruction, values)
+ .ok_or("Handler(32) requires two DWORD operands")?;
+ let inside = tactical_area_contains(&self.mission, world, area, id);
Ok(Some(u32::from(inside)))
}
ScriptDispatchSelector::Handler(30) => {
@@ -103,7 +132,7 @@ impl LoadedMissionState {
if command.first != 1 {
return Err(format!("Handler(30) command {}", command.first));
}
- if self.mission_events.voiced_messages.insert(command.second) {
+ if mission_events.voiced_messages.insert(command.second) {
messages.push(command.second);
}
Ok(None)
@@ -112,7 +141,7 @@ impl LoadedMissionState {
},
)
.err()
- .filter(|(index, _)| self.mission_events.reported_stops.insert(*index))
+ .filter(|(index, _)| mission_events.reported_stops.insert(*index))
.map(|(instruction_index, reason)| MissionEventStop {
clan_index: bundle.clan_index,
script_path: bundle.script_path.clone(),
@@ -124,6 +153,29 @@ impl LoadedMissionState {
}
}
+fn dword_pair(instruction: &ScriptInstruction, values: &[VarSetDefault]) -> Option<(u32, u32)> {
+ let [first, second] = *instruction.references.as_slice() else {
+ return None;
+ };
+ match (values.get(first as usize)?, values.get(second as usize)?) {
+ (&VarSetDefault::Dword(first), &VarSetDefault::Dword(second)) => Some((first, second)),
+ _ => None,
+ }
+}
+
+fn count_clan_objects(objects: &[(ObjectHandle, u32)], world: &World, mask: u32) -> u32 {
+ // shortcut: placed units retain their starting clan until capture and
+ // production update the roster.
+ objects
+ .iter()
+ .filter(|&&(handle, class)| {
+ class & mask != 0
+ && fparkan_world::registration_sequence(world, handle)
+ .is_ok_and(|seq| seq.is_some())
+ })
+ .count() as u32
+}
+
fn tactical_area_contains(
mission: &super::MissionDocument,
world: &World,
@@ -173,7 +225,25 @@ fn in_tactical_polygon(vertices: &[[f32; 3]], x: f32, y: f32) -> bool {
#[cfg(test)]
mod tests {
- use super::{in_tactical_polygon, MissionEvents};
+ use super::{count_clan_objects, in_tactical_polygon, MissionEvents};
+ use fparkan_world::{
+ construct_object, new, register_object, request_delete, ObjectDraft, WorldConfig,
+ };
+
+ #[test]
+ fn clan_count_uses_overlapping_bits_and_skips_removed_objects() {
+ let mut world = new(WorldConfig);
+ let objects = [0x0102_0000, 0x0100_8000, 0x2000_0000, 0x8000_0010].map(|class| {
+ let handle = construct_object(&mut world, ObjectDraft { original_id: None }).unwrap();
+ register_object(&mut world, handle).unwrap();
+ (handle, class)
+ });
+ assert_eq!(count_clan_objects(&objects, &world, 0x0100_8000), 2);
+ assert_eq!(count_clan_objects(&objects, &world, 0x2100_0000), 3);
+ assert_eq!(count_clan_objects(&objects, &world, 0x8000_0000), 1);
+ request_delete(&mut world, objects[0].0).unwrap();
+ assert_eq!(count_clan_objects(&objects, &world, 0x0100_0000), 1);
+ }
#[test]
fn tactical_polygon_preserves_original_edge_inclusion() {