aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2026-11-26 10:58:00 +0300
committerValentin Popov <valentin@popov.link>2026-11-26 10:58:00 +0300
commita519d5efefb4d846a9f65c6b9376fa245215a0f6 (patch)
tree317ab71339fd63f2065c0720f9d64b05b281a59d /crates/fparkan-runtime
parentbb1cf10f550f52bc7bb84d9eb695d2f7acd6dfa3 (diff)
downloadfparkan-a519d5efefb4d846a9f65c6b9376fa245215a0f6.tar.xz
fparkan-a519d5efefb4d846a9f65c6b9376fa245215a0f6.zip
fix(ai): route capturers to authored building entrances
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/building_passage.rs114
-rw-r--r--crates/fparkan-runtime/src/clan_ai_events.rs2
-rw-r--r--crates/fparkan-runtime/src/clan_targeting.rs36
-rw-r--r--crates/fparkan-runtime/src/lib.rs2
-rw-r--r--crates/fparkan-runtime/src/mesh_collision.rs1
-rw-r--r--crates/fparkan-runtime/src/route_obstacles.rs26
6 files changed, 166 insertions, 15 deletions
diff --git a/crates/fparkan-runtime/src/building_passage.rs b/crates/fparkan-runtime/src/building_passage.rs
new file mode 100644
index 00000000..9ed02556
--- /dev/null
+++ b/crates/fparkan-runtime/src/building_passage.rs
@@ -0,0 +1,114 @@
+//! Exterior approach to a capture point in a building's authored hallway.
+
+use crate::{
+ projectile_parts::root_model, route_motion::route_points, route_obstacles::model_footprint,
+ MissionAssets, TerrainWorld,
+};
+use fparkan_msh::{node38_fallback_hierarchy, Hallway};
+use fparkan_render::LegacyIron3dEulerTransform;
+use fparkan_terrain::SurfaceQuery;
+use fparkan_world::TransformState;
+use std::collections::VecDeque;
+
+pub(super) struct CaptureEntrance {
+ pub(super) staging: [f32; 2],
+ pub(super) capture: [f32; 3],
+}
+
+pub(super) fn capture_entrance(
+ assets: &MissionAssets,
+ terrain: &TerrainWorld,
+ index: usize,
+ pose: TransformState,
+ from: [f32; 3],
+ clearance: f32,
+) -> Option<CaptureEntrance> {
+ let model = &root_model(assets, index)?.validated;
+ let hallway = &model.hallway;
+ // Behavior::GoInside passes mask 0x40 to HallWay's first-match query.
+ let capture = hallway
+ .points
+ .iter()
+ .position(|point| point.flags & 0x40 != 0)?;
+ let toward_capture = links_to(hallway, capture);
+ let hierarchy = node38_fallback_hierarchy(model)?;
+ let transform = LegacyIron3dEulerTransform {
+ translation: pose.position.map(f32::from_bits),
+ orientation_radians: pose.orientation.map(f32::from_bits),
+ };
+ let scale = pose.scale.map(f32::from_bits);
+ let project = |index: usize| {
+ let point = hallway.points.get(index)?;
+ let node = hierarchy.poses.get(point.node.0 as usize)?;
+ transform.try_transform_scaled_point(node.transform_point(point.position), scale)
+ };
+ let capture = project(capture)?;
+ let footprint = model_footprint(assets, index, pose)?.expanded(clearance);
+ hallway
+ .points
+ .iter()
+ .enumerate()
+ .filter(|(_, point)| point.flags & 0x1000_0001 == 1)
+ .filter_map(|(index, _)| {
+ let entrance = project(index)?;
+ let inside = project(toward_capture[index]?)?;
+ let staging = footprint.exterior_along(
+ [entrance[0], entrance[1]],
+ [entrance[0] - inside[0], entrance[1] - inside[1]],
+ )?;
+ let ground = terrain.height_at(staging).ok()??;
+ route_points(terrain, from, [staging[0], staging[1], ground]).ok()??;
+ let distance: f32 = (0..3).map(|i| (from[i] - entrance[i]).powi(2)).sum();
+ Some((distance, CaptureEntrance { staging, capture }))
+ })
+ .min_by(|a, b| a.0.total_cmp(&b.0))
+ .map(|(_, entrance)| entrance)
+}
+
+fn links_to(hallway: &Hallway, goal: usize) -> Vec<Option<usize>> {
+ let mut next = vec![None; hallway.points.len()];
+ next[goal] = Some(goal);
+ let mut queue = VecDeque::from([goal]);
+ while let Some(point) = queue.pop_front() {
+ for &[a, b] in &hallway.links {
+ let (a, b) = (a as usize, b as usize);
+ let neighbor = if a == point {
+ b
+ } else if b == point {
+ a
+ } else {
+ continue;
+ };
+ if let Some(slot @ None) = next.get_mut(neighbor) {
+ *slot = Some(point);
+ queue.push_back(neighbor);
+ }
+ }
+ }
+ next
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use fparkan_msh::{HallwayPoint, NodeId};
+
+ #[test]
+ fn only_connected_entrances_lead_to_the_capture_point() {
+ let hallway = Hallway {
+ points: vec![
+ HallwayPoint {
+ position: [0.0; 3],
+ flags: 0,
+ node: NodeId(0)
+ };
+ 5
+ ],
+ links: vec![[1, 0], [1, 2], [2, 3], [3, 0], [4, 9]],
+ };
+ assert_eq!(
+ links_to(&hallway, 3),
+ [Some(3), Some(2), Some(3), Some(3), None]
+ );
+ }
+}
diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs
index dc72048a..30ceb573 100644
--- a/crates/fparkan-runtime/src/clan_ai_events.rs
+++ b/crates/fparkan-runtime/src/clan_ai_events.rs
@@ -806,7 +806,7 @@ impl LoadedMissionState {
let problem = problem.as_ref()
.ok_or("Handler(15) capture requires a current problem")?;
let approach = crate::clan_targeting::send_capturer(
- &mut self.unit_patrol, world, &self.object_drafts, &self.mission_assets, unit, target_id,
+ &mut self.unit_patrol, world, &self.object_drafts, &self.mission_assets, &self.terrain, unit, target_id,
);
if let Some(approach) = approach {
self.clan_ai_events.capture_orders.push((OwnerId(clan as u16), problem.key, approach));
diff --git a/crates/fparkan-runtime/src/clan_targeting.rs b/crates/fparkan-runtime/src/clan_targeting.rs
index 490a42f8..14ecb4b7 100644
--- a/crates/fparkan-runtime/src/clan_targeting.rs
+++ b/crates/fparkan-runtime/src/clan_targeting.rs
@@ -1,6 +1,7 @@
use crate::{
- clan_combat_strength::unit_strength, MissionControlDeviceTableState, MissionDocument,
- MissionObjectDraft,
+ building_passage::capture_entrance, clan_combat_strength::unit_strength,
+ projectile::unit_sphere, MissionControlDeviceTableState, MissionDocument, MissionObjectDraft,
+ TerrainWorld,
};
use fparkan_world::{
handle_by_original_id, identity_metadata, registration_sequence, transform_state, ObjectHandle,
@@ -27,6 +28,7 @@ pub(super) fn send_capturer(
world: &World,
drafts: &[MissionObjectDraft],
assets: &crate::MissionAssets,
+ terrain: &TerrainWorld,
unit: Capturer,
target_id: u32,
) -> Option<CaptureApproach> {
@@ -41,21 +43,27 @@ pub(super) fn send_capturer(
{
return None;
}
- let from = transform_state(world, unit.handle)
- .ok()?
- .position
- .map(f32::from_bits);
- // shortcut: use the communicator mesh until the native door and interior route are wired.
- let point = nearest_platform_point(
- crate::mission_events::communicator_triangles(assets, index, class, pose)?,
- from,
- )?;
- let [x, y, _] = point;
+ let unit_pose = transform_state(world, unit.handle).ok()?;
+ let from = unit_pose.position.map(f32::from_bits);
+ let unit_id = identity_metadata(world, unit.handle).ok()?.original_id?;
+ let clearance = unit_sphere(assets, unit_id.0 as usize, unit_pose).map_or(0.0, |(_, r)| r);
+ let (destination, point) =
+ if let Some(entrance) = capture_entrance(assets, terrain, index, pose, from, clearance) {
+ // shortcut: stop outside the connected door until movement on the
+ // indoor links and their gate messages are supported.
+ (entrance.staging, entrance.capture)
+ } else {
+ let point = nearest_platform_point(
+ crate::mission_events::communicator_triangles(assets, index, class, pose)?,
+ from,
+ )?;
+ ([point[0], point[1]], point)
+ };
patrol
.get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted)
- .move_to(world, unit.handle, [x, y], false);
+ .move_to(world, unit.handle, destination, false);
eprintln!(
- "sim_coverage script_capture_approach_stub=1 unit={} target={} target_id={target_id}",
+ "sim_coverage script_capture_approach_stub=1 unit={} target={} target_id={target_id} destination={destination:?}",
unit.handle.slot, unit.target.slot
);
Some(CaptureApproach {
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index f11614fb..9f3557af 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -48,6 +48,7 @@ use behavior_profile::prepare_behavior_profile_states;
mod boarding;
mod builder_supply;
mod building_door;
+mod building_passage;
mod clan_ai_events;
mod clan_area_strength;
mod clan_build_limits;
@@ -9413,6 +9414,7 @@ mod tests {
triangle_descriptors: None,
node_names: None,
hero_exit_points: Vec::new(),
+ hallway: Default::default(),
animation: None,
}
}
diff --git a/crates/fparkan-runtime/src/mesh_collision.rs b/crates/fparkan-runtime/src/mesh_collision.rs
index 7783400e..9d1fa8b8 100644
--- a/crates/fparkan-runtime/src/mesh_collision.rs
+++ b/crates/fparkan-runtime/src/mesh_collision.rs
@@ -452,6 +452,7 @@ mod tests {
triangle_descriptors: Some(triangle_descriptors),
node_names: None,
hero_exit_points: Vec::new(),
+ hallway: Default::default(),
animation: None,
}
}
diff --git a/crates/fparkan-runtime/src/route_obstacles.rs b/crates/fparkan-runtime/src/route_obstacles.rs
index eb29b680..7ac73f19 100644
--- a/crates/fparkan-runtime/src/route_obstacles.rs
+++ b/crates/fparkan-runtime/src/route_obstacles.rs
@@ -154,6 +154,32 @@ impl Footprint {
(0..2).all(|axis| p[axis] >= self.min[axis] && p[axis] <= self.max[axis])
}
+ pub(super) fn exterior_along(self, point: [f32; 2], direction: [f32; 2]) -> Option<[f32; 2]> {
+ if !self.contains(point) {
+ return Some(point);
+ }
+ let length = direction[0].hypot(direction[1]);
+ if length == 0.0 || !length.is_finite() {
+ return None;
+ }
+ let direction = direction.map(|component| component / length);
+ let distance = (0..2)
+ .filter(|&axis| direction[axis] != 0.0)
+ .map(|axis| {
+ let edge = if direction[axis] < 0.0 {
+ self.min[axis]
+ } else {
+ self.max[axis]
+ };
+ (edge - point[axis]) / direction[axis]
+ })
+ .fold(f32::INFINITY, f32::min)
+ + CORNER_GAP;
+ Some(std::array::from_fn(|axis| {
+ point[axis] + direction[axis] * distance
+ }))
+ }
+
pub(super) fn intersects(self, from: [f32; 2], to: [f32; 2]) -> bool {
let (mut first, mut last) = (0.0_f32, 1.0_f32);
for axis in 0..2 {