diff options
Diffstat (limited to 'crates/fparkan-runtime/src/unit_patrol')
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol/orders.rs | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs index 8e9260a1..fec2731c 100644 --- a/crates/fparkan-runtime/src/unit_patrol/orders.rs +++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs @@ -9,6 +9,8 @@ pub(super) enum UnitOrder { Move([f32; 2]), Attack(ObjectHandle, usize), Patrol([f32; 2], f32), + Stand, + Route(Vec<[f32; 2]>), Shutdown, Resume(Box<PatrollingUnit>), } @@ -16,10 +18,11 @@ pub(super) enum UnitOrder { impl UnitOrder { fn opcode(&self) -> u32 { match self { - Self::Move(_) => 2, + Self::Move(_) | Self::Route(_) => 2, Self::Attack(..) => 3, Self::Patrol(..) => 4, Self::Shutdown => 0x13, + Self::Stand => 0x15, Self::Resume(_) => unreachable!("a suspended order has already started"), } } @@ -50,6 +53,21 @@ pub(crate) fn supports_order(order: &Handler15Invocation) -> bool { } impl UnitPatrol { + pub(crate) fn command_order( + &mut self, + world: &World, + actor: ObjectHandle, + route: Option<Vec<[f32; 2]>>, + ) { + let order = route.map_or(UnitOrder::Stand, UnitOrder::Route); + self.script_orders.retain(|(unit, _)| *unit != actor); + if self.is_leaving_factory(actor) { + self.script_orders.push((actor, VecDeque::from([order]))); + } else { + self.start_script_order(world, actor, order); + } + } + pub(crate) fn active_order_code(&self, actor: ObjectHandle) -> u32 { if self.is_leaving_factory(actor) { return 0x14; @@ -57,6 +75,9 @@ impl UnitPatrol { if self.is_shut_down(actor) { return UnitOrder::Shutdown.opcode(); } + if self.standing.contains(&actor) { + return UnitOrder::Stand.opcode(); + } let Some(unit) = self.units.iter().find(|unit| unit.handle == actor) else { return 0; }; @@ -96,7 +117,8 @@ impl UnitPatrol { match order { UnitOrder::Move(point) | UnitOrder::Patrol(point, _) => points.push(*point), UnitOrder::Resume(unit) => unit.append_point_orders(&mut points), - UnitOrder::Attack(..) | UnitOrder::Shutdown => {} + UnitOrder::Route(route) => points.extend(route), + UnitOrder::Attack(..) | UnitOrder::Shutdown | UnitOrder::Stand => {} } } } @@ -161,6 +183,8 @@ impl UnitPatrol { queue.push_front(UnitOrder::Resume(Box::new(self.units.remove(index)))); } else if self.is_shut_down(actor) { queue.push_front(UnitOrder::Shutdown); + } else if self.standing.contains(&actor) { + queue.push_front(UnitOrder::Stand); } self.start_script_order(world, actor, order); } @@ -268,6 +292,18 @@ impl UnitPatrol { return true; } match &order { + UnitOrder::Stand => { + self.units.retain(|unit| unit.handle != actor); + self.shut_down.remove(&actor); + self.standing.insert(actor); + return true; + } + UnitOrder::Route(route) => { + for (index, &point) in route.iter().enumerate() { + self.move_to(world, actor, point, index != 0); + } + return true; + } UnitOrder::Move(point) => { self.move_to(world, actor, *point, false); eprintln!( @@ -309,6 +345,8 @@ impl UnitPatrol { world: &World, controls: &[MissionControlDeviceTableState], ) { + self.standing + .retain(|&actor| transform_state(world, actor).is_ok()); self.units.retain(|unit| { let Some((target, _)) = unit.attack_target else { return true; @@ -415,6 +453,29 @@ mod tests { } #[test] + fn command_panel_stops_then_replaces_the_route_with_confirmed_points() { + let mut world = fparkan_world::new(Default::default()); + let actor = object(&mut world); + let other = object(&mut world); + let mut patrol = UnitPatrol::scripted(); + patrol.enqueue_script_order(&world, actor, UnitOrder::Move([10.0, 10.0]), false); + patrol.enqueue_script_order(&world, actor, UnitOrder::Shutdown, true); + patrol.move_to(&world, other, [40.0, 40.0], false); + + patrol.command_order(&world, actor, None); + patrol.advance_script_orders(&world, &[]); + assert_eq!(patrol.active_order_code(actor), 0x15); + assert!(!patrol.is_shut_down(actor)); + assert!(patrol.satellite_route(actor).is_empty()); + assert_eq!(patrol.satellite_route(other), [[40.0, 40.0]]); + + let points = vec![[20.0, 30.0], [30.0, 40.0]]; + patrol.command_order(&world, actor, Some(points.clone())); + assert_eq!(patrol.active_order_code(actor), 2); + assert_eq!(patrol.satellite_route(actor), points); + } + + #[test] fn move_attack_and_shutdown_wait_for_the_preceding_task() { let mut world = fparkan_world::new(Default::default()); let actor = object(&mut world); |
