diff options
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/clan_script_init.rs | 21 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 72 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/mission_events.rs | 38 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/script_patrol.rs | 101 |
4 files changed, 179 insertions, 53 deletions
diff --git a/crates/fparkan-runtime/src/clan_script_init.rs b/crates/fparkan-runtime/src/clan_script_init.rs index ce0d3e2f..f9c23498 100644 --- a/crates/fparkan-runtime/src/clan_script_init.rs +++ b/crates/fparkan-runtime/src/clan_script_init.rs @@ -1,4 +1,23 @@ -use fparkan_script::ScriptInstruction; +use crate::script_patrol::ScriptPatrol; +use fparkan_script::{ScriptDispatchSelector, ScriptInstruction, VarSet, VarSetDefault}; + +pub(super) fn patrol_order( + declarations: &VarSet, + instruction: &ScriptInstruction, + values: &[VarSetDefault], +) -> Option<ScriptPatrol> { + if instruction.dispatch_selector() != ScriptDispatchSelector::Handler(15) + || instruction.header_words[1] != u32::MAX + || instruction.header_words[3] != u32::MAX + { + return None; + } + ScriptPatrol::from_order( + declarations + .resolve_handler15_with_values(instruction, values) + .ok()?, + ) +} pub(super) fn stubbed_handler(instruction: &ScriptInstruction) -> Option<u8> { // shortcut: resume Init after the catalog refresh until the live research context is wired. diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index b9c655b2..6da34cbc 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -55,6 +55,7 @@ mod player_weapon; mod production; mod projectile; mod research; +mod script_patrol; mod unit_patrol; pub use autodemo::{ @@ -2354,6 +2355,7 @@ fn load_mission_with_options( MissionEvents::default() }; let mut next_behavior_rng_state = engine.behavior_rng_state; + let mut init_patrols = Vec::new(); if options.asset_scope == MissionAssetScope::Full { let queue_time_ms = startup_input .expect("full mission loading requires startup inputs") @@ -2376,6 +2378,7 @@ fn load_mission_with_options( mask, )) }, + &mut init_patrols, ) { let _ = fparkan_world::shutdown(new_runtime_world); return Err(error); @@ -2437,20 +2440,21 @@ fn load_mission_with_options( &object_drafts[..reserve_start], &handles[..reserve_start], ), - MissionMode::SingleMission => loaded_scripts - .bundles - .iter() - .find(|bundle| bundle.clan_index == 0) - .is_some_and(|bundle| { - bundle.package.events.iter().any(|event| { - event.name_raw.as_slice() == b"Mission\0" - && event.instructions.iter().any(|instruction| { - instruction.dispatch_selector() - == fparkan_script::ScriptDispatchSelector::Handler(15) - }) - }) - }) - .then(unit_patrol::UnitPatrol::scripted), + MissionMode::SingleMission => (!init_patrols.is_empty() + || loaded_scripts + .bundles + .iter() + .find(|bundle| bundle.clan_index == 0) + .is_some_and(|bundle| { + bundle.package.events.iter().any(|event| { + event.name_raw.as_slice() == b"Mission\0" + && event.instructions.iter().any(|instruction| { + instruction.dispatch_selector() + == fparkan_script::ScriptDispatchSelector::Handler(15) + }) + }) + })) + .then(unit_patrol::UnitPatrol::scripted), } } else { None @@ -2511,6 +2515,17 @@ 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 + ); + } + } + } let autodemo_combat = (unit_patrol.is_some() || guard_clans.contains(&true)).then(|| { autodemo_combat::AutoDemoCombat::new( &vfs, @@ -7960,6 +7975,7 @@ fn load_mission_scripts( varset, InitPolicy::Preview, |_, _| None, + &mut Vec::new(), )?; let varset_states = materialize_script_varset_states(&bundles, varset, &init_states)?; (init_states, varset_states) @@ -7982,6 +7998,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>, ) -> Result<(), EngineError> { // The recovered slot refreshes one Behavior instance before dispatching // that same instance's Init event. The Rust loader waits until its new @@ -8005,6 +8022,7 @@ fn start_mission_scripts( varset, init_policy, &clan_count, + init_patrols, )?); } } @@ -8064,6 +8082,7 @@ fn resolve_init_states( varset: &MissionScriptVarSet, policy: InitPolicy, clan_count: impl Fn(u32, u32) -> Option<u32>, + init_patrols: &mut Vec<script_patrol::ScriptPatrol>, ) -> Result<Vec<MissionScriptInitState>, EngineError> { let mut states = Vec::new(); for bundle in bundles { @@ -8097,12 +8116,19 @@ fn resolve_init_states( input, bundle.formula_table.as_ref(), &clan_count, - |index, instruction| { - if policy == InitPolicy::KeepPrefix { - if let Some(handler) = clan_script_init::stubbed_handler(instruction) { - bundle.init_stubs.push((index, handler)); - return true; - } + |index, instruction, values| { + if policy != InitPolicy::KeepPrefix { + return false; + } + if let Some(order) = + clan_script_init::patrol_order(&varset.declarations, instruction, values) + { + init_patrols.push(order); + return true; + } + if let Some(handler) = clan_script_init::stubbed_handler(instruction) { + bundle.init_stubs.push((index, handler)); + return true; } false }, @@ -12559,6 +12585,7 @@ mod tests { &mut rng_state, InitPolicy::Strict, |_, _| None, + &mut Vec::new(), ) .expect_err("unsupported first Init stops startup before the second bundle"); @@ -15893,6 +15920,7 @@ mod tests { &varset, InitPolicy::Strict, |_, _| None, + &mut Vec::new(), ) .expect("exact Init execution"); assert_eq!( @@ -16003,6 +16031,7 @@ mod tests { &varset, InitPolicy::Preview, |_, _| None, + &mut Vec::new(), ) .expect("static preview retains unsupported script"); assert!(preview_states.is_empty(), "no partial writes are retained"); @@ -16060,6 +16089,7 @@ mod tests { &varset, InitPolicy::KeepPrefix, |_, _| None, + &mut Vec::new(), ) .expect("partial Init keeps the prefix and continues with the next clan"); assert_eq!(partial_bundles[0].init_stubs, vec![(1, 43)]); @@ -16101,6 +16131,7 @@ mod tests { &varset, InitPolicy::Strict, |_, _| None, + &mut Vec::new(), ), Err(EngineError::ScriptInitUnsupported { clan_index: 0, @@ -16129,6 +16160,7 @@ mod tests { &varset, InitPolicy::Preview, |_, _| None, + &mut Vec::new(), ), Err(EngineError::ScriptInit { message, .. }) if message.contains("Handler(19) is missing reference 0") diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs index 7a86de19..c3b2af44 100644 --- a/crates/fparkan-runtime/src/mission_events.rs +++ b/crates/fparkan-runtime/src/mission_events.rs @@ -7,8 +7,7 @@ use super::{ use fparkan_msh::{draw_batches, node38_sampled_hierarchy, selected_slot, Group, Lod, NodeId}; use fparkan_render::LegacyIron3dEulerTransform; use fparkan_script::{ - Handler15TargetPayload, ScriptDispatchSelector, ScriptInstruction, VarSetDefault, - VmHostCallbackCommand, + ScriptDispatchSelector, ScriptInstruction, VarSetDefault, VmHostCallbackCommand, }; use fparkan_world::{ handle_by_original_id, identity_metadata, registration_sequence, set_owner, transform_state, @@ -697,44 +696,19 @@ impl LoadedMissionState { let order = declarations .resolve_handler15_with_values(instruction, values) .map_err(|error| error.to_string())?; - let (4, 3, Handler15TargetPayload::ByPlace { first, second }) = - (order.word_1, order.word_2, order.target) - else { + let Some(order) = crate::script_patrol::ScriptPatrol::from_order(order) else { return Err(format!( "Handler(15) order={} insertion={} target={:?}", order.word_1, order.word_2, order.target )); }; - let Some(handle) = - self.mission - .objects - .iter() - .enumerate() - .find_map(|(index, object)| { - let class = self.object_drafts[index].unit_dat_header_flags?; - (class == super::boarding::WARBOT_CLASS - && object.identity_or_clan_raw == order.word_0) - .then(|| { - handle_by_original_id(world, OriginalObjectId(index as u32)) - }) - .flatten() - }) - else { - // The native handler sets its missing-target status and advances. - return Ok(None); - }; let Some(patrol) = &mut self.unit_patrol else { return Err("Handler(15) patrol runtime is unavailable".into()); }; - // shortcut: default place radius and a routed random point; - // native patrol timing and profile overrides remain open. - let radius = if matches!(order.word_3, 0 | u32::MAX) { - 60.0 - } else { - order.word_3 as f32 - }; - if patrol.order(world, handle, [first as f32, second as f32], radius) { - patrol_orders.push((handle, [first, second])); + if let Some(handle) = + order.apply(patrol, &self.mission, &self.object_drafts, world) + { + patrol_orders.push((handle, order.center)); } Ok(None) } diff --git a/crates/fparkan-runtime/src/script_patrol.rs b/crates/fparkan-runtime/src/script_patrol.rs new file mode 100644 index 00000000..6d349c9b --- /dev/null +++ b/crates/fparkan-runtime/src/script_patrol.rs @@ -0,0 +1,101 @@ +use crate::unit_patrol::UnitPatrol; +use crate::{MissionDocument, MissionObjectDraft}; +use fparkan_script::{Handler15Invocation, Handler15TargetPayload}; +use fparkan_world::{handle_by_original_id, ObjectHandle, OriginalObjectId, World}; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(super) struct ScriptPatrol { + pub(super) actor_id: u32, + pub(super) center: [u32; 2], + pub(super) radius: u32, +} + +impl ScriptPatrol { + pub(super) fn from_order(order: Handler15Invocation) -> Option<Self> { + let (4, 3, Handler15TargetPayload::ByPlace { first, second }) = + (order.word_1, order.word_2, order.target) + else { + return None; + }; + Some(Self { + actor_id: order.word_0, + center: [first, second], + radius: order.word_3, + }) + } + + pub(super) fn apply( + self, + patrol: &mut UnitPatrol, + mission: &MissionDocument, + drafts: &[MissionObjectDraft], + world: &World, + ) -> Option<ObjectHandle> { + let (index, _) = + mission + .objects + .iter() + .zip(drafts) + .enumerate() + .find(|(_, (object, draft))| { + self.actor_id != u32::MAX + && object.identity_or_clan_raw == self.actor_id + && draft.unit_dat_header_flags == Some(crate::boarding::WARBOT_CLASS) + })?; + let handle = handle_by_original_id(world, OriginalObjectId(index as u32))?; + // shortcut: use the verified default radius until per-unit patrol profiles are wired. + let radius = match self.radius { + 0 | u32::MAX => 60.0, + radius => radius as f32, + }; + patrol + .order(world, handle, self.center.map(|axis| axis as f32), radius) + .then_some(handle) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ + create, load_gameplay_mission, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_vfs::DirectoryVfs; + use std::{path::PathBuf, sync::Arc}; + + #[test] + #[ignore = "requires licensed corpus"] + fn stock_campaign_assigns_patrols_during_init() { + let root = + PathBuf::from(std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed root")); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(&root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/CAMPAIGN/CAMPAIGN.01/Mission.02/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + + let mission = engine.loaded.as_ref().unwrap(); + let patrol = mission.unit_patrol.as_ref().unwrap(); + let live_patrols: Vec<_> = mission + .mission + .objects + .iter() + .enumerate() + .filter_map(|(index, _)| { + handle_by_original_id(&engine.world, OriginalObjectId(index as u32)) + }) + .filter(|&handle| patrol.is_patrolling(handle)) + .collect(); + assert!(!live_patrols.is_empty()); + } +} |
