From 590c8f39b935f34fc6a4f9c033520c2c0359253d Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 09:27:02 +0400 Subject: fix(ai): prepend base defence attacks to unit orders The native task dispatcher inserts mode 2 at index zero without replacing the old list. Keep the interrupted order and queued tail, then run the base-defence wait after assigning the group in the stock mission. --- crates/fparkan-runtime/src/clan_ai_events.rs | 9 +- crates/fparkan-runtime/src/clan_combat_groups.rs | 13 ++- .../fparkan-runtime/src/clan_problem_conditions.rs | 5 +- crates/fparkan-runtime/src/unit_patrol.rs | 11 -- crates/fparkan-runtime/src/unit_patrol/orders.rs | 112 +++++++++++++++++++-- 5 files changed, 118 insertions(+), 32 deletions(-) (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 9f636280..f5dad22f 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -969,14 +969,7 @@ impl LoadedMissionState { } ScriptDispatchSelector::Handler(8) if problem.is_some() => { match single_dword(&instruction.references, values) { - Some(1) - if problem - .as_ref() - .is_some_and(|record| matches!(record.key.word_0, OPTIMAL_TRANSPORT | MINE_NEEDED | MAKE_RESEARCH | 6 | BUILDING_PROTECT | PLACE_PROTECT)) - || !conditions.is_empty() => - { - solving = true - } + Some(1) => solving = true, Some(2) => solved = true, _ => { return Err( diff --git a/crates/fparkan-runtime/src/clan_combat_groups.rs b/crates/fparkan-runtime/src/clan_combat_groups.rs index 71d8a2a5..50c86e16 100644 --- a/crates/fparkan-runtime/src/clan_combat_groups.rs +++ b/crates/fparkan-runtime/src/clan_combat_groups.rs @@ -147,11 +147,12 @@ impl CombatGroup { drafts: &[MissionObjectDraft], order: Handler15Invocation, ) -> Result<(), String> { - let (0, kind @ (3 | 4), 3) = (order.word_0, order.word_1, order.word_2) else { + let kind = order.word_1; + if order.word_0 != 0 || !matches!((kind, order.word_2), (3, 2 | 3) | (4, 3)) { return Err( - "Handler(28) requires a replacing patrol or attack order on group 0".into(), + "Handler(28) requires a supported patrol or attack order on group 0".into(), ); - }; + } let target = GroupTarget::resolve(world, mission, order.target) .ok_or("Handler(28) target is unavailable")?; for &unit in &self.members { @@ -186,7 +187,11 @@ impl CombatGroup { }; for &unit in &self.members { if let Some((handle, index)) = attack { - patrol.set_attack_target(world, unit, handle, index, point); + if order.word_2 == 2 { + patrol.prepend_attack(world, unit, handle, index); + } else { + patrol.set_attack_target(world, unit, handle, index, point); + } } else { let radius = match order.word_3 { 0 | u32::MAX => 60.0, diff --git a/crates/fparkan-runtime/src/clan_problem_conditions.rs b/crates/fparkan-runtime/src/clan_problem_conditions.rs index a7c2c298..a3de9635 100644 --- a/crates/fparkan-runtime/src/clan_problem_conditions.rs +++ b/crates/fparkan-runtime/src/clan_problem_conditions.rs @@ -36,7 +36,10 @@ impl Condition { Self::TaskInactive(unit) => !live(unit) || !task_active(unit), Self::GroupIdle => group.is_none_or(|group| { group.members.iter().all(|&unit| { - !live(unit) || patrol.is_none_or(|patrol| !patrol.is_patrolling(unit)) + !live(unit) + || patrol.is_none_or(|patrol| { + !patrol.is_patrolling(unit) && !patrol.is_leaving_factory(unit) + }) }) }), } diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index cfe49901..b4b81c7f 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -310,17 +310,6 @@ impl UnitPatrol { self.units.iter().any(|unit| unit.handle == handle) } - pub(super) fn has_object_order(&self, handle: ObjectHandle, target: ObjectHandle) -> bool { - self.units - .iter() - .any(|unit| unit.handle == handle && unit.has_object_order(target)) - } - - pub(super) fn clear_object_order(&mut self, handle: ObjectHandle, target: ObjectHandle) { - self.units - .retain(|unit| unit.handle != handle || !unit.has_object_order(target)); - } - pub(super) fn has_point_order(&self, handle: ObjectHandle, point: [f32; 2]) -> bool { self.units .iter() diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index f3ab609b..cd21e4ff 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -1,24 +1,34 @@ -use super::UnitPatrol; +use super::{PatrollingUnit, UnitPatrol}; use crate::{MissionControlDeviceTableState, MissionDocument, MissionObjectDraft}; use fparkan_script::{Handler15Invocation, Handler15TargetPayload}; use fparkan_world::{identity_metadata, transform_state, ObjectHandle, World}; use std::collections::VecDeque; -#[derive(Clone, Copy)] +#[derive(Clone)] pub(super) enum UnitOrder { Move([f32; 2]), Attack(ObjectHandle, usize), Patrol([f32; 2], f32), Shutdown, + Resume(Box), } impl UnitOrder { - fn opcode(self) -> u32 { + fn opcode(&self) -> u32 { match self { Self::Move(_) => 2, Self::Attack(..) => 3, Self::Patrol(..) => 4, Self::Shutdown => 0x13, + Self::Resume(_) => unreachable!("a suspended order has already started"), + } + } + + fn targets_object(&self, target: ObjectHandle) -> bool { + match self { + Self::Attack(handle, _) => *handle == target, + Self::Resume(unit) => unit.has_object_order(target), + _ => false, } } } @@ -40,6 +50,57 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool { } impl UnitPatrol { + pub(crate) fn has_object_order(&self, handle: ObjectHandle, target: ObjectHandle) -> bool { + self.units + .iter() + .any(|unit| unit.handle == handle && unit.has_object_order(target)) + || self.script_orders.iter().any(|(unit, queue)| { + *unit == handle && queue.iter().any(|order| order.targets_object(target)) + }) + } + + pub(crate) fn clear_object_order(&mut self, handle: ObjectHandle, target: ObjectHandle) { + self.units + .retain(|unit| unit.handle != handle || !unit.has_object_order(target)); + if let Some((_, queue)) = self + .script_orders + .iter_mut() + .find(|(unit, _)| *unit == handle) + { + queue.retain(|order| !order.targets_object(target)); + } + self.script_orders.retain(|(_, queue)| !queue.is_empty()); + } + + pub(crate) fn prepend_attack( + &mut self, + world: &World, + actor: ObjectHandle, + target: ObjectHandle, + target_index: usize, + ) { + let mut queue = self + .script_orders + .iter() + .position(|(unit, _)| *unit == actor) + .map(|index| self.script_orders.remove(index).1) + .unwrap_or_default(); + let order = UnitOrder::Attack(target, target_index); + if self.is_leaving_factory(actor) { + queue.push_front(order); + } else { + if let Some(index) = self.units.iter().position(|unit| unit.handle == actor) { + queue.push_front(UnitOrder::Resume(Box::new(self.units.remove(index)))); + } else if self.is_shut_down(actor) { + queue.push_front(UnitOrder::Shutdown); + } + self.start_script_order(world, actor, order); + } + if !queue.is_empty() { + self.script_orders.push((actor, queue)); + } + } + pub(crate) fn script_order( &mut self, world: &World, @@ -129,20 +190,29 @@ impl UnitPatrol { } fn start_script_order(&mut self, world: &World, actor: ObjectHandle, order: UnitOrder) -> bool { - match order { + if let UnitOrder::Resume(unit) = order { + let mut unit = *unit; + unit.points.clear(); + unit.retry_ms = 0; + unit.attack = None; + self.units.retain(|current| current.handle != actor); + self.units.push(unit); + return true; + } + match &order { UnitOrder::Move(point) => { - self.move_to(world, actor, point, false); + self.move_to(world, actor, *point, false); eprintln!( "sim_coverage script_move_approx=1 unit={} destination={point:?}", actor.slot ); } UnitOrder::Attack(target, index) => { - let Ok(pose) = transform_state(world, target) else { + let Ok(pose) = transform_state(world, *target) else { return false; }; let [x, y, _] = pose.position.map(f32::from_bits); - self.set_attack_target(world, actor, target, index, [x, y]); + self.set_attack_target(world, actor, *target, *index, [x, y]); eprintln!( "sim_coverage script_attack_approx=1 unit={} target_unit={}", actor.slot, target.slot @@ -153,9 +223,10 @@ impl UnitPatrol { eprintln!("sim_coverage unit_shutdown_approx=1 unit={}", actor.slot); } UnitOrder::Patrol(point, radius) => { - self.order(world, actor, point, radius); + self.order(world, actor, *point, *radius); eprintln!("sim_coverage script_point_patrol_approx=1 unit={} center={point:?} radius={radius}", actor.slot); } + UnitOrder::Resume(_) => unreachable!("handled above"), } eprintln!( "script_unit_order_started unit={} order={}", @@ -280,6 +351,31 @@ mod tests { assert!(!patrol.is_patrolling(actor)); } + #[test] + fn prepended_attack_interrupts_the_current_order_and_preserves_its_tail() { + let mut world = fparkan_world::new(Default::default()); + let actor = object(&mut world); + let target = object(&mut world); + place(&mut world, target, 50.0); + let mut patrol = UnitPatrol::scripted(); + patrol.enqueue_script_order(&world, actor, UnitOrder::Move([10.0, 0.0]), false); + patrol.enqueue_script_order(&world, actor, UnitOrder::Shutdown, true); + patrol.prepend_attack(&world, actor, target, 1); + assert_eq!(patrol.attack_target(actor), Some((target, 1))); + assert!(!patrol.is_moving_to_destination(actor)); + request_delete(&mut world, target).unwrap(); + fparkan_world::step(&mut world, &Default::default()).unwrap(); + patrol.advance_script_orders(&world, &[]); + assert_eq!( + patrol.units[0].destinations.as_ref().unwrap()[0], + [10.0, 0.0] + ); + assert!(!patrol.is_shut_down(actor)); + place(&mut world, actor, 10.0); + patrol.advance_script_orders(&world, &[]); + assert!(patrol.is_shut_down(actor)); + } + #[test] fn replacing_an_order_discards_the_queued_tail() { let mut world = fparkan_world::new(Default::default()); -- cgit v1.2.3