aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-14 19:41:29 +0300
committerValentin Popov <valentin@popov.link>2026-11-14 19:41:29 +0300
commit2420e84d82cc1afdafc70882e51d4482dcdaad5b (patch)
tree05cf5155c5cf0ec06c2429e413671433b1e8f7ce /crates/fparkan-runtime/src
parent7b04b7760373ee8d86f5652b1bd2914a8ee2db79 (diff)
downloadfparkan-2420e84d82cc1afdafc70882e51d4482dcdaad5b.tar.xz
fparkan-2420e84d82cc1afdafc70882e51d4482dcdaad5b.zip
feat(ai): patrol around script-selected objects
Uses the TMA logical ID to route a warbot around the current position of a building or unit. The original Behavior.dll confirms target class selection and radius. Mission 13 now assigns building patrols at stock startup.
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/lib.rs4
-rw-r--r--crates/fparkan-runtime/src/mission_events.rs6
-rw-r--r--crates/fparkan-runtime/src/script_patrol.rs47
-rw-r--r--crates/fparkan-runtime/src/unit_combat.rs2
-rw-r--r--crates/fparkan-runtime/src/unit_patrol.rs90
5 files changed, 128 insertions, 21 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index a17dd6ab..04aa8858 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -2528,12 +2528,12 @@ fn load_mission_with_options(
let Some(patrol) = &mut unit_patrol else {
continue;
};
- if let Some(handle) =
+ if let Some((handle, center)) =
order.apply(patrol, &mission, &object_drafts, &new_runtime_world)
{
eprintln!(
"script_init_patrol unit={} id={} target={:?}",
- handle.slot, order.actor_id, order.center
+ handle.slot, order.actor_id, center
);
}
}
diff --git a/crates/fparkan-runtime/src/mission_events.rs b/crates/fparkan-runtime/src/mission_events.rs
index 1d4c7856..1c900819 100644
--- a/crates/fparkan-runtime/src/mission_events.rs
+++ b/crates/fparkan-runtime/src/mission_events.rs
@@ -765,10 +765,10 @@ impl LoadedMissionState {
let Some(patrol) = &mut self.unit_patrol else {
return Err("Handler(15) patrol runtime is unavailable".into());
};
- if let Some(handle) =
+ if let Some(order) =
order.apply(patrol, &self.mission, &self.object_drafts, world)
{
- patrol_orders.push((handle, order.center));
+ patrol_orders.push(order);
}
Ok(None)
}
@@ -895,7 +895,7 @@ fn tactical_area_contains(
})
}
-fn placed_logic_objects<'a>(
+pub(super) fn placed_logic_objects<'a>(
mission: &'a super::MissionDocument,
world: &'a World,
id: u32,
diff --git a/crates/fparkan-runtime/src/script_patrol.rs b/crates/fparkan-runtime/src/script_patrol.rs
index d67874c6..0884bcc5 100644
--- a/crates/fparkan-runtime/src/script_patrol.rs
+++ b/crates/fparkan-runtime/src/script_patrol.rs
@@ -1,7 +1,9 @@
use crate::unit_patrol::UnitPatrol;
use crate::{MissionDocument, MissionObjectDraft};
use fparkan_script::{Handler15Invocation, Handler15TargetPayload};
-use fparkan_world::{handle_by_original_id, ObjectHandle, OriginalObjectId, World};
+use fparkan_world::{
+ handle_by_original_id, transform_state, ObjectHandle, OriginalObjectId, World,
+};
pub(super) fn warbot_by_logic_id(
mission: &MissionDocument,
@@ -26,20 +28,25 @@ pub(super) fn warbot_by_logic_id(
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct ScriptPatrol {
pub(super) actor_id: u32,
- pub(super) center: [u32; 2],
+ target: Handler15TargetPayload,
pub(super) radius: u32,
}
impl ScriptPatrol {
pub(super) fn from_order(order: Handler15Invocation) -> Option<Self> {
- let (4, 3, Handler15TargetPayload::ByPlace { first, second }) =
- (order.word_1, order.word_2, order.target)
- else {
+ if !matches!(
+ (order.word_1, order.word_2, order.target),
+ (
+ 4,
+ 3,
+ Handler15TargetPayload::ByPlace { .. } | Handler15TargetPayload::ByLogicId(_)
+ )
+ ) {
return None;
- };
+ }
Some(Self {
actor_id: order.word_0,
- center: [first, second],
+ target: order.target,
radius: order.word_3,
})
}
@@ -50,16 +57,34 @@ impl ScriptPatrol {
mission: &MissionDocument,
drafts: &[MissionObjectDraft],
world: &World,
- ) -> Option<ObjectHandle> {
+ ) -> Option<(ObjectHandle, [u32; 2])> {
let handle = warbot_by_logic_id(mission, drafts, world, self.actor_id)?;
// shortcut: use the verified default radius until per-unit patrol profiles are wired.
let radius = match self.radius {
0 | u32::MAX => 60.0,
radius => radius as f32,
};
- patrol
- .order(world, handle, self.center.map(|axis| axis as f32), radius)
- .then_some(handle)
+ match self.target {
+ Handler15TargetPayload::ByPlace { first, second } => {
+ let center = [first, second];
+ patrol
+ .order(world, handle, center.map(|axis| axis as f32), radius)
+ .then_some((handle, center))
+ }
+ Handler15TargetPayload::ByLogicId(id) => {
+ let target =
+ crate::mission_events::placed_logic_objects(mission, world, id).next()?;
+ let [x, y, _] = transform_state(world, target)
+ .ok()?
+ .position
+ .map(f32::from_bits);
+ patrol.order_around_object(world, handle, target, [x, y], radius).then(|| {
+ eprintln!("sim_coverage script_patrol_target_approx=1 unit={} target_id={id} target_unit={}", handle.slot, target.slot);
+ (handle, [x as u32, y as u32])
+ })
+ }
+ _ => None,
+ }
}
}
diff --git a/crates/fparkan-runtime/src/unit_combat.rs b/crates/fparkan-runtime/src/unit_combat.rs
index 56141775..b8410a5b 100644
--- a/crates/fparkan-runtime/src/unit_combat.rs
+++ b/crates/fparkan-runtime/src/unit_combat.rs
@@ -249,7 +249,7 @@ impl AutoDemoCombat {
if patrol.is_some_and(|patrol| patrol.is_shut_down(handle)) {
return None;
}
- let limit = patrol.and_then(|patrol| patrol.attack_limit(handle));
+ let limit = patrol.and_then(|patrol| patrol.attack_limit(world, handle));
if limit.is_some_and(|limit| !limit.allows_attack(position)) {
return None;
}
diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs
index 8be60d89..d5caa0cd 100644
--- a/crates/fparkan-runtime/src/unit_patrol.rs
+++ b/crates/fparkan-runtime/src/unit_patrol.rs
@@ -25,6 +25,7 @@ pub(super) struct UnitPatrol {
struct PatrollingUnit {
handle: ObjectHandle,
center: [f32; 2],
+ center_object: Option<ObjectHandle>,
radius: f32,
points: VecDeque<[f32; 2]>,
retry_ms: u32,
@@ -153,7 +154,8 @@ impl UnitPatrol {
.retain(|unit| transform_state(world, unit.handle).is_ok());
if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) {
unit.raid_factory = None;
- if unit.center != center || unit.radius != radius {
+ if unit.center_object.take().is_some() || unit.center != center || unit.radius != radius
+ {
unit.center = center;
unit.radius = radius;
unit.points.clear();
@@ -165,6 +167,7 @@ impl UnitPatrol {
self.units.push(PatrollingUnit {
handle,
center,
+ center_object: None,
radius,
points: VecDeque::new(),
retry_ms: 0,
@@ -174,6 +177,28 @@ impl UnitPatrol {
true
}
+ pub(super) fn order_around_object(
+ &mut self,
+ world: &World,
+ handle: ObjectHandle,
+ target: ObjectHandle,
+ center: [f32; 2],
+ radius: f32,
+ ) -> bool {
+ if self.units.iter().any(|unit| {
+ unit.handle == handle && unit.center_object == Some(target) && unit.radius == radius
+ }) {
+ return false;
+ }
+ self.order(world, handle, center, radius);
+ if let Some(unit) = self.units.iter_mut().find(|unit| unit.handle == handle) {
+ unit.center_object = Some(target);
+ unit.points.clear();
+ unit.retry_ms = 0;
+ }
+ true
+ }
+
pub(super) fn is_patrolling(&self, handle: ObjectHandle) -> bool {
self.units.iter().any(|unit| unit.handle == handle)
}
@@ -195,14 +220,18 @@ impl UnitPatrol {
.raid_factory
}
- pub(super) fn attack_limit(&self, handle: ObjectHandle) -> Option<PatrolAttackLimit> {
+ pub(super) fn attack_limit(
+ &self,
+ world: &World,
+ handle: ObjectHandle,
+ ) -> Option<PatrolAttackLimit> {
// A raid starts with an explicit attack order. It never receives PatrolPlace limits.
let unit = self
.units
.iter()
.find(|unit| unit.handle == handle && unit.raid_factory.is_none())?;
Some(PatrolAttackLimit {
- center: unit.center,
+ center: unit.center(world)?,
radius: unit.radius,
current_target: unit.attack,
})
@@ -234,6 +263,7 @@ impl UnitPatrol {
.map(|(_, &handle)| PatrollingUnit {
handle,
center,
+ center_object: None,
radius: 150.0,
points: VecDeque::new(),
retry_ms: 0,
@@ -259,6 +289,7 @@ impl UnitPatrol {
targets: &[(ObjectHandle, CombatTarget)],
) -> Result<PreparedPatrol, EngineError> {
let mut state = self.clone();
+ state.units.retain(|unit| unit.center(world).is_some());
let seconds = now_ms.saturating_sub(state.clock_ms) as f32 * 0.001;
state.clock_ms = state.clock_ms.max(now_ms);
let mut poses = Vec::new();
@@ -270,6 +301,12 @@ impl UnitPatrol {
continue;
};
let from = before.position.map(f32::from_bits);
+ let center = unit.center(world).expect("retained patrol center");
+ if (center[0] - unit.center[0]).hypot(center[1] - unit.center[1]) >= unit.radius {
+ unit.center = center;
+ unit.points.clear();
+ unit.retry_ms = 0;
+ }
let enemy = targets
.iter()
.find(|(handle, _)| *handle == unit.handle)
@@ -296,7 +333,7 @@ impl UnitPatrol {
unit.points = if let Some(enemy) = enemy {
route_points(terrain, from, enemy.center)?.unwrap_or_default()
} else {
- choose_route(terrain, from, unit.center, unit.radius, &mut state.rng)?
+ choose_route(terrain, from, center, unit.radius, &mut state.rng)?
};
unit.retry_ms = now_ms.saturating_add(1_000);
}
@@ -339,6 +376,22 @@ impl UnitPatrol {
}
}
+impl PatrollingUnit {
+ fn center(&self, world: &World) -> Option<[f32; 2]> {
+ // shortcut: use the target's transform as the patrol center until native building approaches are wired.
+ match self.center_object {
+ None => Some(self.center),
+ Some(handle) => {
+ let [x, y, _] = transform_state(world, handle)
+ .ok()?
+ .position
+ .map(f32::from_bits);
+ Some([x, y])
+ }
+ }
+ }
+}
+
#[cfg(test)]
#[test]
fn shutdown_clears_patrol_until_a_new_order_arrives() {
@@ -359,6 +412,35 @@ fn shutdown_clears_patrol_until_a_new_order_arrives() {
assert!(patrol.is_patrolling(handle));
}
+#[cfg(test)]
+#[test]
+fn object_patrol_tracks_its_center_and_ignores_repeated_orders() {
+ let mut world = fparkan_world::new(fparkan_world::WorldConfig::default());
+ let mut make_actor = || {
+ let actor = fparkan_world::construct_object(
+ &mut world,
+ fparkan_world::ObjectDraft { original_id: None },
+ )
+ .unwrap();
+ fparkan_world::register_object(&mut world, actor).unwrap();
+ actor
+ };
+ let actor = make_actor();
+ let target = make_actor();
+ let mut patrol = UnitPatrol::scripted();
+ assert!(patrol.order_around_object(&world, actor, target, [0.0; 2], 60.0));
+ assert!(!patrol.order_around_object(&world, actor, target, [0.0; 2], 60.0));
+ let mut position = transform_state(&world, target).unwrap();
+ position.position = [20.0_f32, 35.0, 0.0].map(f32::to_bits);
+ set_transform(&mut world, target, position).unwrap();
+ assert_eq!(
+ patrol.attack_limit(&world, actor).unwrap().center,
+ [20.0, 35.0]
+ );
+ patrol.order(&world, actor, [0.0; 2], 60.0);
+ assert_eq!(patrol.attack_limit(&world, actor).unwrap().center, [0.0; 2]);
+}
+
fn distance_to_patrol([x, y]: [f32; 2], [px, py, _]: [f32; 3]) -> f32 {
// shortcut: use the horizontal distance until patrol orders carry their native Z.
(px - x).hypot(py - y)