From c38fb66d2b2dfed3a7eb748914a3e2d9f9871768 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 14 Nov 2026 20:50:32 +0400 Subject: feat(ai): queue scenario move orders for mobile units Runs the stock order sequence in mission 16. Both mobile headquarters receive five destinations in script order. Movement uses existing terrain routing until the native Go task is wired. --- crates/fparkan-runtime/src/clan_script_init.rs | 14 ++++ crates/fparkan-runtime/src/lib.rs | 25 +++++++ crates/fparkan-runtime/src/script_patrol.rs | 14 +++- crates/fparkan-runtime/src/unit_combat.rs | 4 +- crates/fparkan-runtime/src/unit_patrol.rs | 96 +++++++++++++++++++++++++- 5 files changed, 149 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/fparkan-runtime/src/clan_script_init.rs b/crates/fparkan-runtime/src/clan_script_init.rs index 891cc5a3..ee205baa 100644 --- a/crates/fparkan-runtime/src/clan_script_init.rs +++ b/crates/fparkan-runtime/src/clan_script_init.rs @@ -5,6 +5,11 @@ use fparkan_script::{ pub(super) enum InitOrder { Patrol(ScriptPatrol), + Move { + id: u32, + destination: [u32; 2], + append: bool, + }, Shutdown(u32), Reserve { clan: usize, @@ -41,6 +46,15 @@ pub(super) fn unit_order( let order = declarations .resolve_handler15_with_values(instruction, values) .ok()?; + if let (2, insertion @ (1 | 3), Handler15TargetPayload::ByPlace { first, second }) = + (order.word_1, order.word_2, order.target) + { + return Some(InitOrder::Move { + id: order.word_0, + destination: [first, second], + append: insertion == 1, + }); + } if matches!( (order.word_1, order.word_2, order.target), (0x13, 3, Handler15TargetPayload::None) diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 04aa8858..3a7290f9 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -2446,6 +2446,7 @@ fn load_mission_with_options( matches!( order, clan_script_init::InitOrder::Patrol(_) + | clan_script_init::InitOrder::Move { .. } | clan_script_init::InitOrder::Shutdown(_) ) }) || loaded_scripts @@ -2540,6 +2541,30 @@ fn load_mission_with_options( clan_script_init::InitOrder::Reserve { clan, id, reserved } => { mission_events.set_reservation(&mission, &new_runtime_world, clan, id, reserved); } + clan_script_init::InitOrder::Move { + id, + destination, + append, + } => { + let Some(patrol) = &mut unit_patrol else { + continue; + }; + if let Some(handle) = script_patrol::unit_by_logic_id( + &mission, + &object_drafts, + &new_runtime_world, + id, + |class| class & autodemo::MOBILE_UNIT != 0 && class != HERO_UNIT_CLASS, + ) { + patrol.move_to( + &new_runtime_world, + handle, + destination.map(|axis| axis as f32), + append, + ); + eprintln!("sim_coverage script_move_approx=1 unit={} id={id} destination={destination:?} append={append}", handle.slot); + } + } clan_script_init::InitOrder::Shutdown(id) => { let Some(patrol) = &mut unit_patrol else { continue; diff --git a/crates/fparkan-runtime/src/script_patrol.rs b/crates/fparkan-runtime/src/script_patrol.rs index 0884bcc5..427dc397 100644 --- a/crates/fparkan-runtime/src/script_patrol.rs +++ b/crates/fparkan-runtime/src/script_patrol.rs @@ -10,6 +10,18 @@ pub(super) fn warbot_by_logic_id( drafts: &[MissionObjectDraft], world: &World, id: u32, +) -> Option { + unit_by_logic_id(mission, drafts, world, id, |class| { + class == crate::boarding::WARBOT_CLASS + }) +} + +pub(super) fn unit_by_logic_id( + mission: &MissionDocument, + drafts: &[MissionObjectDraft], + world: &World, + id: u32, + matches_class: impl Fn(u32) -> bool, ) -> Option { mission .objects @@ -19,7 +31,7 @@ pub(super) fn warbot_by_logic_id( .find_map(|(index, (object, draft))| { (id != u32::MAX && object.identity_or_clan_raw == id - && draft.unit_dat_header_flags == Some(crate::boarding::WARBOT_CLASS)) + && draft.unit_dat_header_flags.is_some_and(&matches_class)) .then(|| handle_by_original_id(world, OriginalObjectId(index as u32))) .flatten() }) diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs index b8410a5b..97544f6f 100644 --- a/crates/fparkan-runtime/src/unit_combat.rs +++ b/crates/fparkan-runtime/src/unit_combat.rs @@ -246,7 +246,9 @@ impl AutoDemoCombat { .collect(); live.iter() .filter_map(|&(handle, clan, from, _, position)| { - if patrol.is_some_and(|patrol| patrol.is_shut_down(handle)) { + if patrol.is_some_and(|patrol| { + patrol.is_shut_down(handle) || patrol.is_moving_to_destination(handle) + }) { return None; } let limit = patrol.and_then(|patrol| patrol.attack_limit(world, handle)); diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index d5caa0cd..ee5b359c 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -26,6 +26,7 @@ struct PatrollingUnit { handle: ObjectHandle, center: [f32; 2], center_object: Option, + destinations: Option>, radius: f32, points: VecDeque<[f32; 2]>, retry_ms: u32, @@ -154,7 +155,11 @@ impl UnitPatrol { .retain(|unit| transform_state(world, unit.handle).is_ok()); if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { unit.raid_factory = None; - if unit.center_object.take().is_some() || unit.center != center || unit.radius != radius + let was_moving = unit.destinations.take().is_some(); + if unit.center_object.take().is_some() + || was_moving + || unit.center != center + || unit.radius != radius { unit.center = center; unit.radius = radius; @@ -168,6 +173,7 @@ impl UnitPatrol { handle, center, center_object: None, + destinations: None, radius, points: VecDeque::new(), retry_ms: 0, @@ -177,6 +183,32 @@ impl UnitPatrol { true } + pub(super) fn move_to( + &mut self, + world: &World, + handle: ObjectHandle, + destination: [f32; 2], + append: bool, + ) { + if append { + if let Some(destinations) = self + .units + .iter_mut() + .find(|unit| unit.handle == handle) + .and_then(|unit| unit.destinations.as_mut()) + { + destinations.push_back(destination); + return; + } + } + self.order(world, handle, destination, 60.0); + if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) { + unit.destinations = Some(VecDeque::from([destination])); + unit.points.clear(); + unit.retry_ms = 0; + } + } + pub(super) fn order_around_object( &mut self, world: &World, @@ -213,6 +245,12 @@ impl UnitPatrol { self.shut_down.contains(&handle) } + pub(super) fn is_moving_to_destination(&self, handle: ObjectHandle) -> bool { + self.units + .iter() + .any(|unit| unit.handle == handle && unit.destinations.is_some()) + } + pub(super) fn raid_factory(&self, handle: ObjectHandle) -> Option<(ObjectHandle, usize)> { self.units .iter() @@ -264,6 +302,7 @@ impl UnitPatrol { handle, center, center_object: None, + destinations: None, radius: 150.0, points: VecDeque::new(), retry_ms: 0, @@ -289,7 +328,12 @@ impl UnitPatrol { targets: &[(ObjectHandle, CombatTarget)], ) -> Result { let mut state = self.clone(); - state.units.retain(|unit| unit.center(world).is_some()); + state.units.retain(|unit| { + unit.destinations + .as_ref() + .is_none_or(|items| !items.is_empty()) + && unit.center(world).is_some() + }); let seconds = now_ms.saturating_sub(state.clock_ms) as f32 * 0.001; state.clock_ms = state.clock_ms.max(now_ms); let mut poses = Vec::new(); @@ -301,6 +345,19 @@ impl UnitPatrol { continue; }; let from = before.position.map(f32::from_bits); + if let Some(destinations) = &mut unit.destinations { + if destinations + .front() + .is_some_and(|[x, y]| (x - from[0]).hypot(y - from[1]) <= 0.05) + { + destinations.pop_front(); + unit.points.clear(); + unit.retry_ms = 0; + } + if destinations.is_empty() { + continue; + } + } let center = unit.center(world).expect("retained patrol center"); if (center[0] - unit.center[0]).hypot(center[1] - unit.center[1]) >= unit.radius { unit.center = center; @@ -332,6 +389,17 @@ impl UnitPatrol { if (unit.points.is_empty() || enemy.is_some()) && now_ms >= unit.retry_ms { unit.points = if let Some(enemy) = enemy { route_points(terrain, from, enemy.center)?.unwrap_or_default() + } else if let Some(&[x, y]) = + unit.destinations.as_ref().and_then(|items| items.front()) + { + // shortcut: route to the same XY using the terrain height until native Go arrival is wired. + terrain + .height_at([x, y]) + .map_err(EngineError::Terrain)? + .map(|z| route_points(terrain, from, [x, y, z])) + .transpose()? + .flatten() + .unwrap_or_default() } else { choose_route(terrain, from, center, unit.radius, &mut state.rng)? }; @@ -441,6 +509,30 @@ fn object_patrol_tracks_its_center_and_ignores_repeated_orders() { assert_eq!(patrol.attack_limit(&world, actor).unwrap().center, [0.0; 2]); } +#[cfg(test)] +#[test] +fn move_orders_append_and_replace_destinations() { + let mut world = fparkan_world::new(fparkan_world::WorldConfig::default()); + let actor = fparkan_world::construct_object( + &mut world, + fparkan_world::ObjectDraft { original_id: None }, + ) + .unwrap(); + fparkan_world::register_object(&mut world, actor).unwrap(); + let mut patrol = UnitPatrol::scripted(); + patrol.move_to(&world, actor, [10.0, 20.0], false); + patrol.move_to(&world, actor, [30.0, 40.0], true); + assert_eq!( + patrol.units[0].destinations, + Some(VecDeque::from([[10.0, 20.0], [30.0, 40.0]])) + ); + patrol.move_to(&world, actor, [45.0, 50.0], false); + assert_eq!( + patrol.units[0].destinations, + Some(VecDeque::from([[45.0, 50.0]])) + ); +} + fn distance_to_patrol([x, y]: [f32; 2], [px, py, _]: [f32; 3]) -> f32 { // shortcut: use the horizontal distance until patrol orders carry their native Z. (px - x).hypot(py - y) -- cgit v1.2.3