From db3f404b3d6d24144c49ae5248262ab1d8d55e21 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 06:06:04 +0400 Subject: feat(runtime): count live clan objects in mission scripts --- crates/fparkan-runtime/src/lib.rs | 24 ++++--- crates/fparkan-runtime/src/mission_events.rs | 100 +++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 25 deletions(-) (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 55a1f324..f3dae1fe 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2186,6 +2186,11 @@ fn load_mission_with_options( script_varset_state_count: loaded_scripts.varset_states.len(), }; + let mission_events = if options.asset_scope == MissionAssetScope::Full { + MissionEvents::with_placed_objects(&object_drafts, &handles, summary.clan_count) + } else { + MissionEvents::default() + }; engine.world = new_runtime_world; engine.behavior_rng_state = next_behavior_rng_state; engine.loaded = Some(LoadedMissionState { @@ -2209,7 +2214,7 @@ fn load_mission_with_options( script_varset: loaded_scripts.varset, script_init_states: loaded_scripts.init_states, script_varset_states: loaded_scripts.varset_states, - mission_events: MissionEvents::default(), + mission_events, script_host_mode: request.mode, // Fresh IGame construction seeds this field with 5; this is not the // later status-4 transition performed after native game initialization. @@ -2275,7 +2280,7 @@ pub fn frame(engine: &mut Engine, queue_time_ms: u32) -> Result = Arc::new(DirectoryVfs::new(licensed_root("IS"))); let mut engine = create(EngineServices::new(vfs)).unwrap(); load_mission_with_init_stubs( @@ -17078,16 +17083,15 @@ mod tests { .unwrap(); let first = frame(&mut engine, 5).unwrap().mission_event.unwrap(); assert_eq!(first.messages, [11, 14]); - let stop = first.stop.unwrap(); - assert_eq!( - (stop.clan_index, stop.event_index, stop.instruction_index), - (0, 2, 49) - ); - assert_eq!(stop.reason, "Handler(31)"); + assert!(first.stop.is_none()); assert!(frame(&mut engine, 2_004).unwrap().mission_event.is_none()); + let first_target = mission_object_handle(&engine, OriginalObjectId(1)).unwrap(); + fparkan_world::request_delete(&mut engine.world, first_target).unwrap(); let next = frame(&mut engine, 2_005).unwrap().mission_event.unwrap(); assert!(next.stop.is_none()); - assert!(next.messages.is_empty()); + assert_eq!(next.messages, [17]); + let later = frame(&mut engine, 4_005).unwrap().mission_event.unwrap(); + assert!(later.messages.is_empty()); } #[test] 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, voiced_messages: HashSet, + placed_clan_objects: Vec>, } 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() { -- cgit v1.2.3