aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-12-02 11:28:48 +0300
committerValentin Popov <valentin@popov.link>2026-12-02 11:28:48 +0300
commit0b0764de47a0a1fef30464fb5e4be496e101b083 (patch)
tree91d129a508a0e2fdbedfdeadf7b9fa5bbd0f1a2e /crates/fparkan-runtime
parent733f9d92e0d4e40ed40db42fa6cd11294e49eb7b (diff)
downloadfparkan-0b0764de47a0a1fef30464fb5e4be496e101b083.tar.xz
fparkan-0b0764de47a0a1fef30464fb5e4be496e101b083.zip
feat(ui): assign patrols from the strategic warbot panel
Use the installed order string and GUARD cursor. A single valid click assigns a 300-unit patrol around a terrain point or a live object. Checked the terrain and satellite paths, rejected terrain, and cancellation in the stock Two Fortresses window.
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/satellite_contacts.rs23
-rw-r--r--crates/fparkan-runtime/src/strategic_orders.rs53
-rw-r--r--crates/fparkan-runtime/src/unit_patrol.rs3
-rw-r--r--crates/fparkan-runtime/src/unit_patrol/orders.rs79
4 files changed, 139 insertions, 19 deletions
diff --git a/crates/fparkan-runtime/src/satellite_contacts.rs b/crates/fparkan-runtime/src/satellite_contacts.rs
index 17030fc1..31181bc1 100644
--- a/crates/fparkan-runtime/src/satellite_contacts.rs
+++ b/crates/fparkan-runtime/src/satellite_contacts.rs
@@ -45,6 +45,29 @@ pub fn landscape_extent(engine: &Engine) -> Option<f32> {
(extent.is_finite() && extent > 0.0).then_some(extent)
}
+/// Picks a live unit within 40 world units or a building within 80, in registry order.
+pub fn pick_contact(
+ engine: &Engine,
+ cockpit: StartingCockpit,
+ [x, y]: [f32; 2],
+) -> Option<ObjectHandle> {
+ let extent = landscape_extent(engine)?;
+ visible_contacts(engine, cockpit, extent, |_, class| {
+ let unit = unit_class(class);
+ (unit || icon_x(class).is_some()).then_some(unit)
+ })
+ .filter_map(|(_, handle, pose, _, unit)| {
+ let [u, v, _] = pose.position.map(f32::from_bits);
+ let limit = if unit { 40.0 } else { 80.0 };
+ let distance = f64::from(u - x).hypot(f64::from(v - y));
+ (distance < limit).then_some(())?;
+ let sequence = registration_sequence(&engine.world, handle).ok()??;
+ Some((!unit, sequence, handle))
+ })
+ .min_by_key(|&(building, sequence, _)| (building, sequence))
+ .map(|(_, _, handle)| handle)
+}
+
/// Building marks for the player's own clan and its radar contacts.
pub fn visible_buildings(
engine: &Engine,
diff --git a/crates/fparkan-runtime/src/strategic_orders.rs b/crates/fparkan-runtime/src/strategic_orders.rs
index 2c40c31f..d49fa963 100644
--- a/crates/fparkan-runtime/src/strategic_orders.rs
+++ b/crates/fparkan-runtime/src/strategic_orders.rs
@@ -1,8 +1,21 @@
//! Orders issued by the player from the strategic warbot panel.
-use crate::{strategic_roster::warbot_rows, unit_patrol::UnitPatrol, Engine, StartingCockpit};
+use crate::{
+ strategic_roster::warbot_rows,
+ unit_patrol::{UnitOrder, UnitPatrol},
+ Engine, StartingCockpit,
+};
use fparkan_terrain::NavigationQuery;
-use fparkan_world::{identity_metadata, ObjectHandle};
+use fparkan_world::{identity_metadata, registration_sequence, ObjectHandle};
+
+/// Where the warbot is to patrol.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum PatrolTarget {
+ /// Fixed world coordinates.
+ Point([f32; 2]),
+ /// Live object whose position determines the patrol center.
+ Object(ObjectHandle),
+}
/// Tests a route waypoint using the selected warbot's chassis and the native areal flag.
pub fn accepts_waypoint(engine: &Engine, actor: ObjectHandle, [x, y]: [f32; 2]) -> bool {
@@ -34,7 +47,7 @@ pub fn accepts_waypoint(engine: &Engine, actor: ObjectHandle, [x, y]: [f32; 2])
/// Stops the selected warbot and clears its outstanding movement orders.
pub fn stand(engine: &mut Engine, cockpit: StartingCockpit, actor: ObjectHandle) -> bool {
- issue(engine, cockpit, actor, None)
+ issue(engine, cockpit, actor, UnitOrder::Stand)
}
/// Replaces the selected warbot's order with the confirmed sequence of world points.
@@ -51,14 +64,42 @@ pub fn follow_route(
{
return false;
}
- issue(engine, cockpit, actor, Some(points.to_vec()))
+ issue(engine, cockpit, actor, UnitOrder::Route(points.to_vec()))
+}
+
+/// Whether an object may be used as the center of a patrol.
+pub fn accepts_patrol_object(engine: &Engine, target: ObjectHandle) -> bool {
+ registration_sequence(&engine.world, target)
+ .ok()
+ .flatten()
+ .is_some()
+ && engine.loaded.as_ref().is_some_and(|mission| {
+ !mission
+ .control_device_tables
+ .iter()
+ .any(|table| table.handle == target && table.node_provider_depletion_marked)
+ })
+}
+
+/// Replaces the selected warbot's order with a patrol around a point or an object.
+pub fn patrol_at(
+ engine: &mut Engine,
+ cockpit: StartingCockpit,
+ actor: ObjectHandle,
+ target: PatrolTarget,
+) -> bool {
+ let valid = match target {
+ PatrolTarget::Point(point) => accepts_waypoint(engine, actor, point),
+ PatrolTarget::Object(target) => accepts_patrol_object(engine, target),
+ };
+ valid && issue(engine, cockpit, actor, UnitOrder::StrategicPatrol(target))
}
fn issue(
engine: &mut Engine,
cockpit: StartingCockpit,
actor: ObjectHandle,
- route: Option<Vec<[f32; 2]>>,
+ order: UnitOrder,
) -> bool {
if !warbot_rows(engine, cockpit)
.iter()
@@ -72,6 +113,6 @@ fn issue(
mission
.unit_patrol
.get_or_insert_with(UnitPatrol::scripted)
- .command_order(&engine.world, actor, route);
+ .command_order(&engine.world, actor, order);
true
}
diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs
index 4f708af3..5e083403 100644
--- a/crates/fparkan-runtime/src/unit_patrol.rs
+++ b/crates/fparkan-runtime/src/unit_patrol.rs
@@ -23,6 +23,7 @@ use std::collections::{BTreeMap, HashSet, VecDeque};
mod orders;
pub(super) use orders::supports_order;
+pub(super) use orders::UnitOrder;
#[derive(Clone)]
pub(super) struct UnitPatrol {
@@ -45,7 +46,7 @@ pub(super) struct UnitPatrol {
}
#[derive(Clone)]
-struct PatrollingUnit {
+pub(super) struct PatrollingUnit {
handle: ObjectHandle,
center: [f32; 2],
center_object: Option<ObjectHandle>,
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);