aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime/src')
-rw-r--r--crates/fparkan-runtime/src/autodemo_combat.rs31
-rw-r--r--crates/fparkan-runtime/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/unit_patrol.rs60
3 files changed, 86 insertions, 7 deletions
diff --git a/crates/fparkan-runtime/src/autodemo_combat.rs b/crates/fparkan-runtime/src/autodemo_combat.rs
index ecef3809..fea7a8ab 100644
--- a/crates/fparkan-runtime/src/autodemo_combat.rs
+++ b/crates/fparkan-runtime/src/autodemo_combat.rs
@@ -3,8 +3,10 @@ use super::{
autodemo::MOBILE_UNIT,
player_weapon::prepare_unit_weapons,
projectile::{load_projectile, segment_sphere_fraction, unit_sphere},
- projectile_terrain_hit, projectile_unit_hit, Engine, MissionAssets, MissionDocument,
- MissionObjectDraft, ObjectHandle, PlayerProjectile, PlayerWeapons,
+ projectile_terrain_hit, projectile_unit_hit,
+ unit_patrol::UnitPatrol,
+ Engine, MissionAssets, MissionDocument, MissionObjectDraft, ObjectHandle, PlayerProjectile,
+ PlayerWeapons,
};
use fparkan_vfs::Vfs;
use fparkan_world::{identity_metadata, transform_state};
@@ -224,6 +226,7 @@ impl AutoDemoCombat {
&self,
world: &fparkan_world::World,
assets: &MissionAssets,
+ patrol: Option<&UnitPatrol>,
) -> Vec<(ObjectHandle, CombatTarget)> {
let live: Vec<_> = self
.units
@@ -232,17 +235,29 @@ impl AutoDemoCombat {
let owner = identity_metadata(world, unit.handle).ok()?.owner_id?;
let state = transform_state(world, unit.handle).ok()?;
let (center, radius) = unit_sphere(assets, unit.index, state)?;
- Some((unit.handle, usize::from(owner.0), center, radius))
+ Some((
+ unit.handle,
+ usize::from(owner.0),
+ center,
+ radius,
+ state.position.map(f32::from_bits),
+ ))
})
.collect();
live.iter()
- .filter_map(|&(handle, clan, from, _)| {
+ .filter_map(|&(handle, clan, from, _, position)| {
+ let limit = patrol.and_then(|patrol| patrol.attack_limit(handle));
+ if limit.is_some_and(|limit| !limit.allows_attack(position)) {
+ return None;
+ }
let (enemy, center, radius, distance) = live
.iter()
- .filter_map(|&(enemy, other, to, radius)| {
+ .filter_map(|&(enemy, other, to, radius, target_position)| {
let distance = (to[0] - from[0]).hypot(to[1] - from[1]);
(enemy != handle
&& distance <= ATTACK_RANGE
+ && limit
+ .is_none_or(|limit| limit.allows_target(enemy, target_position))
&& self.hostile.get(clan)?.get(other) == Some(&true))
.then_some((enemy, to, radius, distance))
})
@@ -312,7 +327,11 @@ impl AutoDemoCombat {
let Some(mission) = &engine.loaded else {
return (events, unavailable);
};
- let targets = self.targets(&engine.world, &mission.mission_assets);
+ let targets = self.targets(
+ &engine.world,
+ &mission.mission_assets,
+ mission.unit_patrol.as_ref(),
+ );
for unit in &mut self.units {
// shortcut: armed AI units defend their position while their Init is incomplete.
let owner = identity_metadata(&engine.world, unit.handle)
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 740de83a..614d235b 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -2646,7 +2646,7 @@ pub fn step_headless(
.autodemo_combat
.as_ref()
.map_or_else(Vec::new, |combat| {
- combat.targets(&engine.world, &mission.mission_assets)
+ combat.targets(&engine.world, &mission.mission_assets, Some(patrol))
});
Some(patrol.prepare(&engine.world, &mission.terrain, queue_time_ms, &targets))
})
diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs
index cfbee245..205af4ea 100644
--- a/crates/fparkan-runtime/src/unit_patrol.rs
+++ b/crates/fparkan-runtime/src/unit_patrol.rs
@@ -35,6 +35,24 @@ pub(super) struct PreparedPatrol {
poses: Vec<(ObjectHandle, TransformState, TransformState)>,
}
+#[derive(Clone, Copy)]
+pub(super) struct PatrolAttackLimit {
+ center: [f32; 2],
+ radius: f32,
+ current_target: Option<ObjectHandle>,
+}
+
+impl PatrolAttackLimit {
+ pub(super) fn allows_attack(self, position: [f32; 3]) -> bool {
+ distance_to_patrol(self.center, position) <= self.radius + 60.0
+ }
+
+ pub(super) fn allows_target(self, target: ObjectHandle, position: [f32; 3]) -> bool {
+ self.current_target == Some(target)
+ || distance_to_patrol(self.center, position) < self.radius
+ }
+}
+
impl UnitPatrol {
pub(super) fn approximate_factory_raid(
&mut self,
@@ -163,6 +181,19 @@ impl UnitPatrol {
.raid_factory
}
+ pub(super) fn attack_limit(&self, 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,
+ radius: unit.radius,
+ current_target: unit.attack,
+ })
+ }
+
pub(super) fn activate(&mut self, world: &World, handle: ObjectHandle) {
if let Some(center) = self.center {
self.order(world, handle, center, 150.0);
@@ -293,6 +324,35 @@ impl UnitPatrol {
}
}
+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)
+}
+
+#[cfg(test)]
+#[test]
+fn patrol_limits_new_targets_and_ends_pursuit_at_the_outer_boundary() {
+ let tracked = ObjectHandle {
+ slot: 1,
+ generation: 1,
+ };
+ let candidate = ObjectHandle {
+ slot: 2,
+ generation: 1,
+ };
+ let patrol = PatrolAttackLimit {
+ center: [500.0, 752.0],
+ radius: 150.0,
+ current_target: Some(tracked),
+ };
+ assert!(patrol.allows_target(candidate, [649.0, 752.0, 0.0]));
+ assert!(!patrol.allows_target(candidate, [650.0, 752.0, 0.0]));
+ assert!(patrol.allows_target(tracked, [650.0, 752.0, 0.0]));
+ assert!(patrol.allows_attack([710.0, 752.0, 0.0]));
+ assert!(!patrol.allows_attack([711.0, 752.0, 0.0]));
+ assert!(!patrol.allows_attack([650.0, 902.0, 0.0]));
+}
+
fn turn_toward(pose: TransformState, [dx, dy]: [f32; 2], seconds: f32) -> (f32, f32) {
let yaw = f32::from_bits(pose.orientation[2]);
let goal_yaw = (-dx).atan2(dy);