diff options
Diffstat (limited to 'crates/fparkan-runtime/src/unit_patrol')
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol/orders.rs | 112 |
1 files changed, 104 insertions, 8 deletions
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<PatrollingUnit>), } 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={}", @@ -281,6 +352,31 @@ mod tests { } #[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()); let actor = object(&mut world); |
