From da4c8ed23fe5289dbdc7d81a6eb6ddf66ff94ef7 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 05:05:02 +0400 Subject: fix(movement): route autonomous units around buildings Project the bounds of live building meshes onto the ground and expand them by the vehicle radius. The shared route follower chooses exterior corners for blocked legs and can leave a footprint containing its starting position. Verified a detour on the stock factory geometry, stock builder resupply, a window run of Single.01, and 15 minutes of simulation on all 29 missions. Narrow openings, flying clearance and moving bodies remain open. --- crates/fparkan-runtime/src/lib.rs | 1 + crates/fparkan-runtime/src/route_motion.rs | 16 +- crates/fparkan-runtime/src/route_obstacles.rs | 390 ++++++++++++++++++++++++++ crates/fparkan-runtime/src/unit_patrol.rs | 19 +- 4 files changed, 422 insertions(+), 4 deletions(-) create mode 100644 crates/fparkan-runtime/src/route_obstacles.rs (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index a4444860..606b0987 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -94,6 +94,7 @@ mod projectile; mod research; mod resource_distribution; mod route_motion; +mod route_obstacles; use resource_distribution::advance_behavior_distributor_tick; mod script_host; mod script_patrol; diff --git a/crates/fparkan-runtime/src/route_motion.rs b/crates/fparkan-runtime/src/route_motion.rs index 3d831fa0..be57affb 100644 --- a/crates/fparkan-runtime/src/route_motion.rs +++ b/crates/fparkan-runtime/src/route_motion.rs @@ -1,6 +1,9 @@ //! Follow a terrain route with a shared time budget for turning and travel. -use crate::{EngineError, TerrainWorld, TransformState}; +use crate::{ + route_obstacles::{open_leg, Footprint}, + EngineError, TerrainWorld, TransformState, +}; use fparkan_terrain::{NavigationQuery, RouteRequest}; use std::collections::VecDeque; @@ -39,10 +42,17 @@ pub(super) fn follow_route( points: &mut VecDeque<[f32; 2]>, speed: f32, mut seconds: f32, + obstacles: &[Footprint], mut height_at: impl FnMut([f32; 2]) -> Result, EngineError>, ) -> Result { let [mut x, mut y, mut z] = pose.position.map(f32::from_bits); - while seconds > 0.0 { + while seconds > 0.0 && !points.is_empty() { + if !open_leg([x, y], points, obstacles, |point| { + height_at(point).map(|z| z.is_some()) + })? { + points.clear(); + break; + } let Some(&target) = points.front() else { break }; let [dx, dy] = [target[0] - x, target[1] - y]; let distance = dx.hypot(dy); @@ -92,7 +102,7 @@ mod tests { ..Default::default() }; let mut route = VecDeque::from([[0.0, 0.0], [0.0, 1.0], [4.0, 1.0]]); - let end = follow_route(start, &mut route, 2.0, 2.0, |[x, y]| Ok(Some(x + y))).unwrap(); + let end = follow_route(start, &mut route, 2.0, 2.0, &[], |[x, y]| Ok(Some(x + y))).unwrap(); let [x, y, z] = end.position.map(f32::from_bits); let expected_x = 3.0 - std::f32::consts::FRAC_PI_2; assert!((x - expected_x).abs() < 0.000_01); diff --git a/crates/fparkan-runtime/src/route_obstacles.rs b/crates/fparkan-runtime/src/route_obstacles.rs new file mode 100644 index 00000000..1eab5543 --- /dev/null +++ b/crates/fparkan-runtime/src/route_obstacles.rs @@ -0,0 +1,390 @@ +//! Ground route clearance against the projected bounds of live buildings. + +use crate::{EngineError, LoadedMissionState, ObjectHandle, TransformState, World}; +use fparkan_msh::{node38_fallback_hierarchy, selected_slot, Group, Lod, NodeId}; +use fparkan_render::LegacyIron3dEulerTransform; +use fparkan_world::{handle_by_original_id, registration_sequence, transform_state}; +use std::collections::{BTreeMap, VecDeque}; + +// Keep detour nodes far enough from the boundary that arrival tolerance can +// skip a node without putting the next leg back inside the obstacle. +const CORNER_GAP: f32 = 0.1; + +#[derive(Clone, Copy, Debug)] +pub(super) struct Footprint { + min: [f32; 2], + max: [f32; 2], +} + +#[derive(Clone, Default)] +pub(super) struct BuildingFootprints { + entries: BTreeMap)>, +} + +impl BuildingFootprints { + pub(super) fn refresh(&mut self, world: &World, mission: &LoadedMissionState) { + self.entries.retain(|_, (handle, _, _)| { + registration_sequence(world, *handle) + .ok() + .flatten() + .is_some() + }); + for (index, draft) in mission.object_drafts.iter().enumerate() { + if !draft + .unit_dat_header_flags + .is_some_and(|class| class >> 31 == 1) + { + continue; + } + let Some(id) = draft.original_id else { + continue; + }; + let Some(handle) = handle_by_original_id(world, id) else { + continue; + }; + if registration_sequence(world, handle) + .ok() + .flatten() + .is_none() + { + continue; + } + let Ok(pose) = transform_state(world, handle) else { + continue; + }; + if self + .entries + .get(&id.0) + .is_none_or(|&(old_handle, old_pose, _)| (old_handle, old_pose) != (handle, pose)) + { + self.entries + .insert(id.0, (handle, pose, model_footprint(mission, index, pose))); + } + } + } + + pub(super) fn with_clearance(&self, radius: f32) -> Vec { + self.entries + .values() + .filter_map(|(_, _, footprint)| footprint.map(|bounds| bounds.expanded(radius))) + .collect() + } +} + +fn model_footprint( + mission: &LoadedMissionState, + index: usize, + pose: TransformState, +) -> Option { + let assets = &mission.mission_assets; + let visual = assets.visual_by_id(assets.visual_for_object(index)?)?; + let model = &assets.model_by_id(visual.model_id?)?.validated; + 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 mut bounds = Footprint { + min: [f32::INFINITY; 2], + max: [f32::NEG_INFINITY; 2], + }; + for (index, node) in hierarchy.poses.iter().enumerate() { + let Some(slot) = selected_slot(model, NodeId(index as u32), Lod(0), Group(0)) else { + continue; + }; + let slot = &model.slots[slot.0 as usize]; + if slot.batch_count == 0 { + continue; + } + for corner in 0..8 { + let local = std::array::from_fn(|axis| { + if corner & (1 << axis) == 0 { + slot.aabb_min[axis] + } else { + slot.aabb_max[axis] + } + }); + let p = transform.try_transform_scaled_point(node.transform_point(local), scale)?; + for axis in 0..2 { + bounds.min[axis] = bounds.min[axis].min(p[axis]); + bounds.max[axis] = bounds.max[axis].max(p[axis]); + } + } + } + (0..2) + .all(|axis| bounds.min[axis] < bounds.max[axis]) + .then_some(bounds) +} + +impl Footprint { + fn expanded(self, margin: f32) -> Self { + Self { + min: self.min.map(|v| v - margin), + max: self.max.map(|v| v + margin), + } + } + + fn contains(self, p: [f32; 2]) -> bool { + (0..2).all(|axis| p[axis] >= self.min[axis] && p[axis] <= self.max[axis]) + } + + 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 { + let delta = to[axis] - from[axis]; + if delta == 0.0 { + if from[axis] < self.min[axis] || from[axis] > self.max[axis] { + return false; + } + continue; + } + let a = (self.min[axis] - from[axis]) / delta; + let b = (self.max[axis] - from[axis]) / delta; + first = first.max(a.min(b)); + last = last.min(a.max(b)); + if first > last { + return false; + } + } + true + } + + fn corners(self) -> [[f32; 2]; 4] { + let Self { + min: [x0, y0], + max: [x1, y1], + } = self.expanded(CORNER_GAP); + [[x0, y0], [x0, y1], [x1, y0], [x1, y1]] + } + + fn exits(self, p: [f32; 2]) -> [[f32; 2]; 4] { + let Self { + min: [x0, y0], + max: [x1, y1], + } = self.expanded(CORNER_GAP); + [[x0, p[1]], [x1, p[1]], [p[0], y0], [p[0], y1]] + } +} + +fn distance(a: [f32; 2], b: [f32; 2]) -> f32 { + (a[0] - b[0]).hypot(a[1] - b[1]) +} + +/// Replaces a blocked leg with exterior nodes. Returns false if the unit has +/// to wait for a new route. The walkable query keeps nodes on the surface. +pub(super) fn open_leg( + from: [f32; 2], + points: &mut VecDeque<[f32; 2]>, + obstacles: &[Footprint], + mut walkable: impl FnMut([f32; 2]) -> Result, +) -> Result { + while points.len() > 1 && obstacles.iter().any(|o| o.contains(points[0])) { + points.pop_front(); + } + let Some(&goal) = points.front() else { + return Ok(false); + }; + if !obstacles.iter().any(|o| o.intersects(from, goal)) { + return Ok(true); + } + let contains = |point| obstacles.iter().any(|o| o.contains(point)); + + if contains(from) { + let mut exits: Vec<_> = obstacles + .iter() + .filter(|o| o.contains(from)) + .flat_map(|o| o.exits(from)) + .filter(|&p| { + !contains(p) + && !obstacles + .iter() + .any(|o| !o.contains(from) && o.intersects(from, p)) + }) + .collect(); + exits.sort_by(|&a, &b| distance(from, a).total_cmp(&distance(from, b))); + for exit in exits { + if walkable(exit)? { + if exit != goal { + points.push_front(exit); + } + return Ok(true); + } + } + return Ok(false); + } + + let mut nodes = vec![from]; + let mut end = goal; + if contains(goal) { + let mut exits: Vec<_> = obstacles + .iter() + .filter(|o| o.contains(goal)) + .flat_map(|o| o.exits(goal)) + .filter(|&p| !contains(p)) + .collect(); + exits.sort_by(|&a, &b| distance(from, a).total_cmp(&distance(from, b))); + let mut selected = None; + for exit in exits { + if walkable(exit)? { + selected = Some(exit); + break; + } + } + let Some(exit) = selected else { + return Ok(false); + }; + end = exit; + } + nodes.push(end); + if obstacles.iter().any(|o| o.intersects(from, end)) { + for p in obstacles + .iter() + .flat_map(|o| o.corners()) + .filter(|&p| !contains(p)) + { + if walkable(p)? { + nodes.push(p); + } + } + } + let Some(path) = shortest_path(&nodes, obstacles) else { + return Ok(false); + }; + points.pop_front(); + for p in path { + points.push_front(p); + } + Ok(true) +} + +// Dijkstra over the visibility graph. Edges are built on demand and a node +// may connect to any other visible node, including corners on another box. +fn shortest_path(nodes: &[[f32; 2]], obstacles: &[Footprint]) -> Option> { + let mut distance_to = vec![f32::INFINITY; nodes.len()]; + let mut parent = vec![None; nodes.len()]; + let mut settled = vec![false; nodes.len()]; + distance_to[0] = 0.0; + loop { + let next = (0..nodes.len()) + .filter(|&i| !settled[i] && distance_to[i].is_finite()) + .min_by(|&a, &b| distance_to[a].total_cmp(&distance_to[b]))?; + if next == 1 { + break; + } + settled[next] = true; + for (i, &to) in nodes.iter().enumerate().filter(|&(i, _)| !settled[i]) { + let cost = distance_to[next] + distance(nodes[next], to); + if cost < distance_to[i] && !obstacles.iter().any(|o| o.intersects(nodes[next], to)) { + distance_to[i] = cost; + parent[i] = Some(next); + } + } + } + let mut path = Vec::new(); + let mut index = 1; + while index != 0 { + path.push(nodes[index]); + index = parent[index]?; + } + Some(path) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn detour_clears_both_buildings_and_keeps_the_destination() { + let a = Footprint { + min: [2.0, -2.0], + max: [4.0, 2.0], + } + .expanded(0.5); + let b = Footprint { + min: [5.0, -1.0], + max: [7.0, 3.0], + } + .expanded(0.5); + let start = [0.0, 0.0]; + let goal = [9.0, 0.0]; + let mut route = VecDeque::from([goal]); + assert!(open_leg(start, &mut route, &[a, b], |_| Ok(true)).unwrap()); + assert!(route.len() >= 3); + assert_eq!(route.back(), Some(&goal)); + let mut from = start; + for to in route { + assert!(!a.intersects(from, to)); + assert!(!b.intersects(from, to)); + from = to; + } + } + + #[test] + fn a_unit_already_inside_first_moves_towards_the_nearest_exit() { + let building = Footprint { + min: [0.0, -3.0], + max: [8.0, 3.0], + }; + let goal = [10.0, 0.0]; + let mut route = VecDeque::from([goal]); + assert!(open_leg([1.0, 0.0], &mut route, &[building], |_| Ok(true)).unwrap()); + assert_eq!(route, [[-CORNER_GAP, 0.0], goal]); + assert!(open_leg([0.5, 0.0], &mut route, &[building], |_| Ok(true)).unwrap()); + assert_eq!(route.len(), 2); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_factory_footprint_blocks_a_ground_route() { + use crate::{ + create, load_gameplay_mission, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_terrain::SurfaceQuery; + use fparkan_vfs::DirectoryVfs; + use std::sync::Arc; + + let root = std::env::var_os("FPARKAN_CORPUS_PART1_ROOT").expect("licensed corpus"); + let mut engine = create(EngineServices::new(Arc::new(DirectoryVfs::new(root)))).unwrap(); + load_gameplay_mission( + &mut engine, + MissionRequest { + key: "MISSIONS/CAMPAIGN/CAMPAIGN.04/Mission.02/data.tma".into(), + mode: MissionMode::SingleMission, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let mission = engine.loaded.as_ref().unwrap(); + let mut buildings = BuildingFootprints::default(); + buildings.refresh(&engine.world, mission); + let (_, _, Some(outline)) = buildings.entries[&1] else { + panic!("factory footprint unavailable") + }; + let y = outline.min[1].midpoint(outline.max[1]); + let start = [outline.min[0] - 8.0, y]; + let goal = [outline.max[0] + 8.0, y]; + assert!(outline.intersects(start, goal)); + let mut route = VecDeque::from([goal]); + assert!(open_leg(start, &mut route, &[outline], |p| { + mission + .terrain + .height_at(p) + .map(|z| z.is_some()) + .map_err(EngineError::Terrain) + }) + .unwrap()); + assert!(route.len() > 1); + assert_eq!(route.back(), Some(&goal)); + let mut from = start; + for to in route { + assert!(!outline.intersects(from, to)); + from = to; + } + } +} diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 0cbb4959..e8f94b3f 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -3,7 +3,9 @@ use super::{ boarding::WARBOT_CLASS, next_crt_rand, patrol_points::{PatrolPointReport, PatrolPoints}, + projectile::unit_sphere, route_motion::{follow_route, route_points, turn_toward}, + route_obstacles::BuildingFootprints, unit_combat::CombatTarget, unit_speed::{order_speed, UnitSpeedKind, UnitSpeedReport}, EngineError, LoadedMissionState, MissionDocument, MissionObjectDraft, MissionScriptBundle, @@ -30,6 +32,7 @@ pub(super) struct UnitPatrol { pub(super) speed_report: UnitSpeedReport, pub(super) motion_report: crate::UnitMotionReport, pub(super) factory_raid_clans: usize, + buildings: BuildingFootprints, } #[derive(Clone)] @@ -158,6 +161,7 @@ impl UnitPatrol { speed_report: UnitSpeedReport::default(), motion_report: crate::UnitMotionReport::default(), factory_raid_clans: 0, + buildings: BuildingFootprints::default(), } } @@ -401,6 +405,7 @@ impl UnitPatrol { speed_report: UnitSpeedReport::default(), motion_report: crate::UnitMotionReport::default(), factory_raid_clans: 0, + buildings: BuildingFootprints::default(), }) } @@ -425,6 +430,9 @@ impl UnitPatrol { }); let seconds = now_ms.saturating_sub(state.clock_ms) as f32 * 0.001; state.clock_ms = state.clock_ms.max(now_ms); + if seconds > 0.0 && !state.units.is_empty() { + state.buildings.refresh(world, mission); + } let mut poses = Vec::new(); for unit in &mut state.units { let Ok(before) = transform_state(world, unit.handle) else { @@ -611,7 +619,16 @@ impl UnitPatrol { } else { 9.0 }; - let after = follow_route(before, &mut unit.points, speed, seconds, |xy| { + let clearance = motion_object + .original_id + .and_then(|id| { + let (center, radius) = + unit_sphere(&mission.mission_assets, id as usize, before)?; + Some(radius + (center[0] - from[0]).hypot(center[1] - from[1])) + }) + .unwrap_or(0.0); + let obstacles = state.buildings.with_clearance(clearance); + let after = follow_route(before, &mut unit.points, speed, seconds, &obstacles, |xy| { terrain.height_at(xy).map_err(EngineError::Terrain) })?; if after != before { -- cgit v1.2.3