From 2d01cd35b069d0d1aa78a064593b54f51b23540d Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 1 Dec 2026 19:34:08 +0400 Subject: fix(ai): resume building capture after losing its assignee --- crates/fparkan-runtime/src/unit_patrol/orders.rs | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'crates/fparkan-runtime/src/unit_patrol/orders.rs') diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index cd21e4ff..cde2bd09 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -50,6 +50,21 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool { } impl UnitPatrol { + pub(crate) fn clear_capture_order(&mut self, unit: ObjectHandle, building: ObjectHandle) { + self.units + .retain(|order| order.handle != unit || order.capture_target != Some(building)); + if let Some((_, queue)) = self + .script_orders + .iter_mut() + .find(|(handle, _)| *handle == unit) + { + queue.retain(|order| { + !matches!(order, UnitOrder::Resume(state) if state.capture_target == Some(building)) + }); + } + self.script_orders.retain(|(_, queue)| !queue.is_empty()); + } + pub(crate) fn has_object_order(&self, handle: ObjectHandle, target: ObjectHandle) -> bool { self.units .iter() @@ -266,6 +281,9 @@ impl UnitPatrol { .iter() .find(|unit| unit.handle == actor) .is_none_or(|unit| { + if unit.capture_target.is_some() { + return false; + } if let Some(points) = &unit.destinations { let [x, y, _] = pose.position.map(f32::from_bits); return points.is_empty() @@ -394,6 +412,23 @@ mod tests { assert!(patrol.script_orders.is_empty()); } + #[test] + fn capture_holds_an_appended_order_until_released() { + let mut world = fparkan_world::new(Default::default()); + let actor = object(&mut world); + let building = object(&mut world); + let mut patrol = UnitPatrol::scripted(); + patrol.move_to_capture(&world, actor, building, [10.0, 0.0], None); + patrol.enqueue_script_order(&world, actor, UnitOrder::Shutdown, true); + place(&mut world, actor, 10.0); + patrol.advance_script_orders(&world, &[]); + assert!(patrol.has_capture_order(actor, building)); + assert!(!patrol.is_shut_down(actor)); + patrol.clear_capture_order(actor, building); + patrol.advance_script_orders(&world, &[]); + assert!(patrol.is_shut_down(actor)); + } + #[test] fn attack_holds_an_appended_patrol_until_the_target_is_removed() { let mut world = fparkan_world::new(Default::default()); -- cgit v1.2.3