aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src/unit_patrol
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src/unit_patrol')
-rw-r--r--crates/fparkan-runtime/src/unit_patrol/orders.rs79
1 files changed, 67 insertions, 12 deletions
diff --git a/crates/fparkan-runtime/src/unit_patrol/orders.rs b/crates/fparkan-runtime/src/unit_patrol/orders.rs
index fec2731c..48a849f3 100644
--- a/crates/fparkan-runtime/src/unit_patrol/orders.rs
+++ b/crates/fparkan-runtime/src/unit_patrol/orders.rs
@@ -1,16 +1,20 @@
use super::{PatrollingUnit, UnitPatrol};
-use crate::{MissionControlDeviceTableState, MissionDocument, MissionObjectDraft};
+use crate::{
+ strategic_orders::PatrolTarget, MissionControlDeviceTableState, MissionDocument,
+ MissionObjectDraft,
+};
use fparkan_script::{Handler15Invocation, Handler15TargetPayload};
use fparkan_world::{identity_metadata, transform_state, ObjectHandle, World};
use std::collections::VecDeque;
#[derive(Clone)]
-pub(super) enum UnitOrder {
+pub(crate) enum UnitOrder {
Move([f32; 2]),
Attack(ObjectHandle, usize),
Patrol([f32; 2], f32),
Stand,
Route(Vec<[f32; 2]>),
+ StrategicPatrol(PatrolTarget),
Shutdown,
Resume(Box<PatrollingUnit>),
}
@@ -20,7 +24,7 @@ impl UnitOrder {
match self {
Self::Move(_) | Self::Route(_) => 2,
Self::Attack(..) => 3,
- Self::Patrol(..) => 4,
+ Self::Patrol(..) | Self::StrategicPatrol(_) => 4,
Self::Shutdown => 0x13,
Self::Stand => 0x15,
Self::Resume(_) => unreachable!("a suspended order has already started"),
@@ -30,6 +34,7 @@ impl UnitOrder {
fn targets_object(&self, target: ObjectHandle) -> bool {
match self {
Self::Attack(handle, _) => *handle == target,
+ Self::StrategicPatrol(PatrolTarget::Object(handle)) => *handle == target,
Self::Resume(unit) => unit.has_object_order(target),
_ => false,
}
@@ -53,13 +58,7 @@ 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);
+ pub(crate) fn command_order(&mut self, world: &World, actor: ObjectHandle, order: UnitOrder) {
self.script_orders.retain(|(unit, _)| *unit != actor);
if self.is_leaving_factory(actor) {
self.script_orders.push((actor, VecDeque::from([order])));
@@ -118,6 +117,8 @@ impl UnitPatrol {
UnitOrder::Move(point) | UnitOrder::Patrol(point, _) => points.push(*point),
UnitOrder::Resume(unit) => unit.append_point_orders(&mut points),
UnitOrder::Route(route) => points.extend(route),
+ UnitOrder::StrategicPatrol(PatrolTarget::Point(point)) => points.push(*point),
+ UnitOrder::StrategicPatrol(PatrolTarget::Object(_)) => {}
UnitOrder::Attack(..) | UnitOrder::Shutdown | UnitOrder::Stand => {}
}
}
@@ -304,6 +305,23 @@ impl UnitPatrol {
}
return true;
}
+ UnitOrder::StrategicPatrol(target) => {
+ // iron3d+0x7a390 supplies this radius for both target forms.
+ let radius = 300.0;
+ match *target {
+ PatrolTarget::Point(point) => {
+ self.order(world, actor, point, radius);
+ }
+ PatrolTarget::Object(target) => {
+ let Ok(pose) = transform_state(world, target) else {
+ return false;
+ };
+ let [x, y, _] = pose.position.map(f32::from_bits);
+ self.order_around_object(world, actor, target, [x, y], radius);
+ }
+ }
+ return true;
+ }
UnitOrder::Move(point) => {
self.move_to(world, actor, *point, false);
eprintln!(
@@ -462,7 +480,7 @@ mod tests {
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.command_order(&world, actor, UnitOrder::Stand);
patrol.advance_script_orders(&world, &[]);
assert_eq!(patrol.active_order_code(actor), 0x15);
assert!(!patrol.is_shut_down(actor));
@@ -470,12 +488,49 @@ mod tests {
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()));
+ patrol.command_order(&world, actor, UnitOrder::Route(points.clone()));
assert_eq!(patrol.active_order_code(actor), 2);
assert_eq!(patrol.satellite_route(actor), points);
}
#[test]
+ fn command_panel_patrol_replaces_the_route_and_tracks_an_object_center() {
+ let mut world = fparkan_world::new(Default::default());
+ let actor = object(&mut world);
+ let target = object(&mut world);
+ let mut patrol = UnitPatrol::scripted();
+ patrol.command_order(&world, actor, UnitOrder::Route(vec![[45.0, 60.0]]));
+
+ let point = [100.0, 200.0];
+ patrol.command_order(
+ &world,
+ actor,
+ UnitOrder::StrategicPatrol(PatrolTarget::Point(point)),
+ );
+ assert_eq!(patrol.active_order_code(actor), 4);
+ assert_eq!(patrol.satellite_route(actor), [point]);
+ assert_eq!(patrol.attack_limit(&world, actor).unwrap().radius, 300.0);
+
+ place(&mut world, target, 250.0);
+ patrol.command_order(
+ &world,
+ actor,
+ UnitOrder::StrategicPatrol(PatrolTarget::Object(target)),
+ );
+ assert!(patrol.has_object_order(actor, target));
+ assert_eq!(
+ patrol.attack_limit(&world, actor).unwrap().center,
+ [250.0, 0.0]
+ );
+ place(&mut world, target, 360.0);
+ assert_eq!(
+ patrol.attack_limit(&world, actor).unwrap().center,
+ [360.0, 0.0]
+ );
+ assert_eq!(patrol.active_order_code(actor), 4);
+ }
+
+ #[test]
fn move_attack_and_shutdown_wait_for_the_preceding_task() {
let mut world = fparkan_world::new(Default::default());
let actor = object(&mut world);