diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-14 19:20:27 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-14 19:20:27 +0300 |
| commit | 71ae57fb95805e6262aa70fc4890dcad91ab3ea1 (patch) | |
| tree | 754c2e7d3d2a4fa3bb6518534f4a98a0c636d507 /crates/fparkan-runtime | |
| parent | f61cc5fd7dda6a76b843b36013795f52b03fae7e (diff) | |
| download | fparkan-71ae57fb95805e6262aa70fc4890dcad91ab3ea1.tar.xz fparkan-71ae57fb95805e6262aa70fc4890dcad91ab3ea1.zip | |
feat(ai): reserve placed units for scenario orders
Handler(51) changes availability in the owning clan, matching a direct ai.dll call. Reserved units stay out of the free capturer query. Campaign mission 7 now finishes Init and mission 13 reserves the addressed unit.
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_script_init.rs | 52 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 51 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/mission_events.rs | 49 |
3 files changed, 137 insertions, 15 deletions
diff --git a/crates/fparkan-runtime/src/clan_script_init.rs b/crates/fparkan-runtime/src/clan_script_init.rs index f9c23498..98d899c2 100644 --- a/crates/fparkan-runtime/src/clan_script_init.rs +++ b/crates/fparkan-runtime/src/clan_script_init.rs @@ -1,6 +1,29 @@ use crate::script_patrol::ScriptPatrol; use fparkan_script::{ScriptDispatchSelector, ScriptInstruction, VarSet, VarSetDefault}; +pub(super) enum InitOrder { + Patrol(ScriptPatrol), + Reserve { + clan: usize, + id: u32, + reserved: bool, + }, +} + +pub(super) fn reservation( + instruction: &ScriptInstruction, + values: &[VarSetDefault], +) -> Option<(u32, bool)> { + if instruction.dispatch_selector() != ScriptDispatchSelector::Handler(51) + || instruction.header_words[1] != u32::MAX + || instruction.header_words[3] != u32::MAX + { + return None; + } + let (id, flag) = instruction.dword_pair(values)?; + Some((id, flag != 0)) +} + pub(super) fn patrol_order( declarations: &VarSet, instruction: &ScriptInstruction, @@ -40,4 +63,33 @@ mod tests { instruction.header_words[1] = 0; assert_eq!(stubbed_handler(&instruction), None); } + + #[test] + fn reservation_reads_logical_id_and_treats_every_nonzero_flag_as_set() { + let instruction = ScriptInstruction { + header_words: [51, u32::MAX, u32::MAX, u32::MAX, 6, 2, u32::MAX], + references: vec![1, 0], + }; + assert_eq!( + reservation( + &instruction, + &[VarSetDefault::Dword(2), VarSetDefault::Dword(12)] + ), + Some((12, true)) + ); + assert_eq!( + reservation( + &instruction, + &[VarSetDefault::Dword(0), VarSetDefault::Dword(13)] + ), + Some((13, false)) + ); + assert_eq!( + reservation( + &instruction, + &[VarSetDefault::FloatBits(0), VarSetDefault::Dword(13)] + ), + None + ); + } } diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index cce50e1c..c86b6b01 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2356,7 +2356,7 @@ fn load_mission_with_options( MissionEvents::default() }; let mut next_behavior_rng_state = engine.behavior_rng_state; - let mut init_patrols = Vec::new(); + let mut init_orders = Vec::new(); if options.asset_scope == MissionAssetScope::Full { let queue_time_ms = startup_input .expect("full mission loading requires startup inputs") @@ -2380,7 +2380,7 @@ fn load_mission_with_options( mask, )) }, - &mut init_patrols, + &mut init_orders, ) { let _ = fparkan_world::shutdown(new_runtime_world); return Err(error); @@ -2442,7 +2442,9 @@ fn load_mission_with_options( &object_drafts[..reserve_start], &handles[..reserve_start], ), - MissionMode::SingleMission => (!init_patrols.is_empty() + MissionMode::SingleMission => (init_orders + .iter() + .any(|order| matches!(order, clan_script_init::InitOrder::Patrol(_))) || loaded_scripts .bundles .iter() @@ -2517,14 +2519,23 @@ fn load_mission_with_options( unit_patrol = Some(patrol); } } - if let Some(patrol) = &mut unit_patrol { - for order in init_patrols { - if let Some(handle) = order.apply(patrol, &mission, &object_drafts, &new_runtime_world) - { - eprintln!( - "script_init_patrol unit={} id={} target={:?}", - handle.slot, order.actor_id, order.center - ); + for order in init_orders { + match order { + clan_script_init::InitOrder::Patrol(order) => { + let Some(patrol) = &mut unit_patrol else { + continue; + }; + if let Some(handle) = + order.apply(patrol, &mission, &object_drafts, &new_runtime_world) + { + eprintln!( + "script_init_patrol unit={} id={} target={:?}", + handle.slot, order.actor_id, order.center + ); + } + } + clan_script_init::InitOrder::Reserve { clan, id, reserved } => { + mission_events.set_reservation(&mission, &new_runtime_world, clan, id, reserved); } } } @@ -8000,7 +8011,7 @@ fn start_mission_scripts( behavior_rng_state: &mut u32, init_policy: InitPolicy, clan_count: impl Fn(u32, u32) -> Option<u32>, - init_patrols: &mut Vec<script_patrol::ScriptPatrol>, + init_orders: &mut Vec<clan_script_init::InitOrder>, ) -> Result<(), EngineError> { // The recovered slot refreshes one Behavior instance before dispatching // that same instance's Init event. The Rust loader waits until its new @@ -8024,7 +8035,7 @@ fn start_mission_scripts( varset, init_policy, &clan_count, - init_patrols, + init_orders, )?); } } @@ -8084,7 +8095,7 @@ fn resolve_init_states( varset: &MissionScriptVarSet, policy: InitPolicy, clan_count: impl Fn(u32, u32) -> Option<u32>, - init_patrols: &mut Vec<script_patrol::ScriptPatrol>, + init_orders: &mut Vec<clan_script_init::InitOrder>, ) -> Result<Vec<MissionScriptInitState>, EngineError> { let mut states = Vec::new(); for bundle in bundles { @@ -8111,6 +8122,7 @@ fn resolve_init_states( message: "clan index does not fit the recovered DWORD ClanID field".to_string(), })?, }; + let clan = bundle.clan_index; let prefix = varset .declarations .resolve_init_prefix_with_host( @@ -8128,7 +8140,16 @@ fn resolve_init_states( if let Some(order) = clan_script_init::patrol_order(&varset.declarations, instruction, values) { - init_patrols.push(order); + init_orders.push(clan_script_init::InitOrder::Patrol(order)); + return Some(None); + } + if let Some((id, reserved)) = clan_script_init::reservation(instruction, values) + { + init_orders.push(clan_script_init::InitOrder::Reserve { + clan, + id, + reserved, + }); return Some(None); } if let Some(handler) = clan_script_init::stubbed_handler(instruction) { diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index 2226ebd5..d63ea43c 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -146,11 +146,39 @@ pub(super) struct MissionEvents { reported_stops: HashSet<(usize, usize)>, inside_hero_exit: bool, voiced_messages: HashSet<u32>, + reserved_units: HashSet<(usize, ObjectHandle)>, placed_clan_objects: Vec<(ObjectHandle, u32)>, objectives: MissionObjectives, } impl MissionEvents { + pub(super) fn set_reservation( + &mut self, + mission: &super::MissionDocument, + world: &World, + clan: usize, + id: u32, + reserved: bool, + ) { + let Some(handle) = placed_logic_objects(mission, world, id).find(|&handle| { + identity_metadata(world, handle) + .is_ok_and(|identity| identity.owner_id == Some(OwnerId(clan as u16))) + }) else { + return; + }; + let key = (clan, handle); + if if reserved { + self.reserved_units.insert(key) + } else { + self.reserved_units.remove(&key) + } { + eprintln!( + "script_unit_reservation clan={clan} unit={} id={id} reserved={reserved}", + handle.slot + ); + } + } + pub(super) fn count_clan_mask( &self, world: &World, @@ -173,6 +201,12 @@ impl MissionEvents { self.placed_clan_objects .retain(|(handle, _)| registration_sequence(world, *handle).is_ok()); self.placed_clan_objects.push((handle, class)); + self.reserved_units.retain(|(_, handle)| { + registration_sequence(world, *handle) + .ok() + .flatten() + .is_some() + }); } pub(super) fn with_placed_objects( @@ -612,6 +646,9 @@ impl LoadedMissionState { let owner = OwnerId(bundle.clan_index as u16); for &(handle, class) in &mission_events.placed_clan_objects { if class != super::boarding::WARBOT_CLASS + || mission_events + .reserved_units + .contains(&(bundle.clan_index, handle)) || registration_sequence(world, handle) .ok() .flatten() @@ -694,6 +731,18 @@ impl LoadedMissionState { .find_map(|handle| identity_metadata(world, handle).ok()?.owner_id); Ok(Some(clan.map_or(u32::MAX, |owner| u32::from(owner.0)))) } + ScriptDispatchSelector::Handler(51) => { + let (id, reserved) = crate::clan_script_init::reservation(instruction, values) + .ok_or("Handler(51) requires a logical ID and a DWORD flag")?; + mission_events.set_reservation( + &self.mission, + world, + bundle.clan_index, + id, + reserved, + ); + Ok(None) + } ScriptDispatchSelector::Handler(59 | 60) => { crate::script_time::result(instruction, values, clock_seconds) .map(Some) |
