From 475ce0f12eb0ad890dd423db2046b8082c2d1dbf Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 07:43:40 +0400 Subject: fix(transport): load and unload at the building perimeter Verify the AI transport fills at the mine and unloads at the storage facility from stock Single.01, in the licensed test and the game window. --- crates/fparkan-runtime/src/clan_ai_events.rs | 5 + crates/fparkan-runtime/src/ore_transport.rs | 143 +++++++++++++++++++++----- crates/fparkan-runtime/src/route_obstacles.rs | 20 ++-- 3 files changed, 135 insertions(+), 33 deletions(-) (limited to 'crates/fparkan-runtime/src') diff --git a/crates/fparkan-runtime/src/clan_ai_events.rs b/crates/fparkan-runtime/src/clan_ai_events.rs index 466baa51..dc72048a 100644 --- a/crates/fparkan-runtime/src/clan_ai_events.rs +++ b/crates/fparkan-runtime/src/clan_ai_events.rs @@ -127,6 +127,11 @@ pub(super) fn tick(engine: &mut crate::Engine, now_ms: u32) -> Vec { + pub(super) assets: &'a MissionAssets, + pub(super) drafts: &'a [MissionObjectDraft], + pub(super) terrain: &'a TerrainWorld, +} + +impl TransportGeometry<'_> { + fn index(self, id: OriginalObjectId) -> Option { + self.drafts + .iter() + .position(|draft| draft.original_id == Some(id)) + } + + fn clearance( + self, + unit: OriginalObjectId, + unit_pose: TransformState, + building: OriginalObjectId, + building_pose: TransformState, + ) -> Option { + let radius = body_radius(self.assets, self.index(unit)?, unit_pose)?; + model_footprint(self.assets, self.index(building)?, building_pose) + .map(|bounds| bounds.expanded(radius)) + } +} + pub(super) struct OreTransport { pub(super) unit: ObjectHandle, owner: OwnerId, destination: Option, + dock: Option<(ObjectHandle, TransformState, Option)>, unloading: bool, last_ms: u32, } @@ -18,6 +53,7 @@ impl OreTransport { unit, owner, destination: None, + dock: None, unloading: false, last_ms: now_ms, } @@ -28,6 +64,7 @@ impl OreTransport { world: &World, ores: &mut [BehaviorOreState], patrol: &mut Option, + geometry: TransportGeometry<'_>, now_ms: u32, ) -> bool { let owned = |unit| { @@ -76,47 +113,70 @@ impl OreTransport { { return None; } - let pose = transform_state(world, ore.handle).ok()?; - let [a, b, c] = pose.position.map(f32::from_bits); + let building = transform_state(world, ore.handle).ok()?; + let [a, b, c] = building.position.map(f32::from_bits); let distance = ((x - a).hypot(y - b)).hypot(z - c); distance .is_finite() - .then_some((index, ore.handle, [a, b], distance)) + .then_some((index, ore.handle, building, distance)) }); let target = endpoints.min_by(|a, b| { (self.destination != Some(a.1)) .cmp(&(self.destination != Some(b.1))) .then_with(|| a.3.total_cmp(&b.3)) }); - let Some((index, handle, center, _)) = target else { + let Some((index, handle, building, _)) = target else { if moving { patrol.as_mut().unwrap().clear_move(self.unit); } self.destination = None; + self.dock = None; if !self.unloading && current > 0.0 { self.unloading = true; } return true; }; - let distance = (x - center[0]).hypot(y - center[1]); - // shortcut: use exterior approach points until native dock and port movement are wired. - if distance > 48.0 { - if self.destination != Some(handle) || !moving { - let offset = 40.0 / distance; - let approach = [ - center[0] + (x - center[0]) * offset, - center[1] + (y - center[1]) * offset, - ]; - patrol - .get_or_insert_with(UnitPatrol::scripted) - .move_to(world, self.unit, approach, false); - if self.destination != Some(handle) { - eprintln!( - "script_ore_transport_route clan={} unit={} target={} unloading={} queue_time_ms={now_ms}", - self.owner.0, self.unit.slot, handle.slot, self.unloading, - ); + let dock_changed = self + .dock + .is_none_or(|(target, pose, _)| target != handle || pose != building); + if dock_changed { + let clearance = geometry.clearance( + ores[cargo].original_id, + pose, + ores[index].original_id, + building, + ); + self.dock = Some((handle, building, clearance)); + } + let [a, b, _] = building.position.map(f32::from_bits); + let center_distance = (x - a).hypot(y - b); + let clearance = self.dock.and_then(|(_, _, clearance)| clearance); + let arrived = clearance.map_or(center_distance <= 48.0, |bounds| { + beside_dock([x, y], bounds) + }); + if !arrived { + if self.destination != Some(handle) || dock_changed || !moving { + let approach = match clearance { + Some(bounds) => dock_approach([x, y], bounds, |point| { + geometry.terrain.height_at(point).ok().flatten().is_some() + }), + None => { + let offset = 40.0 / center_distance; + Some([a + (x - a) * offset, b + (y - b) * offset]) + } + }; + if let Some(approach) = approach { + patrol + .get_or_insert_with(UnitPatrol::scripted) + .move_to(world, self.unit, approach, false); + if self.destination != Some(handle) { + eprintln!( + "script_ore_transport_route clan={} unit={} target={} unloading={} queue_time_ms={now_ms}", + self.owner.0, self.unit.slot, handle.slot, self.unloading, + ); + } + self.destination = Some(handle); } - self.destination = Some(handle); } return true; } @@ -144,6 +204,27 @@ impl OreTransport { } } +fn beside_dock([x, y]: [f32; 2], bounds: Footprint) -> bool { + let [a, b] = bounds.closest_point([x, y]); + !bounds.contains([x, y]) && (a - x).hypot(b - y) <= CORNER_GAP + 2.0 * WAYPOINT_TOLERANCE +} + +fn dock_approach( + [x, y]: [f32; 2], + bounds: Footprint, + mut on_ground: impl FnMut([f32; 2]) -> bool, +) -> Option<[f32; 2]> { + bounds + .exits([x, y]) + .into_iter() + .chain(bounds.corners()) + .filter(|&point| on_ground(point)) + .min_by(|a, b| { + let distance = |[px, py]: [f32; 2]| (px - x).hypot(py - y); + distance(*a).total_cmp(&distance(*b)) + }) +} + fn transfer(ores: &mut [BehaviorOreState], source: usize, sink: usize, limit: f32) -> bool { let from = f32::from_bits(ores[source].current_ore_bits); let to = f32::from_bits(ores[sink].current_ore_bits); @@ -159,6 +240,18 @@ mod tests { use super::*; use fparkan_world::OriginalObjectId; + #[test] + fn transport_uses_a_clear_stop_for_wide_buildings() { + let clearance = Footprint::point([0.0; 2]).expanded(74.0); + let outside = dock_approach([130.0, 10.0], clearance, |_| true).unwrap(); + assert_eq!(outside, [74.1, 10.0]); + assert!(beside_dock(outside, clearance)); + assert!(!beside_dock([40.0, 10.0], clearance)); + assert!(!beside_dock([80.0, 10.0], clearance)); + let alternate = dock_approach([130.0, 10.0], clearance, |point| point != outside).unwrap(); + assert!(beside_dock(alternate, clearance)); + } + #[test] fn transfer_limits_the_batch_to_storage_room_and_preserves_ore() { let mut ores = diff --git a/crates/fparkan-runtime/src/route_obstacles.rs b/crates/fparkan-runtime/src/route_obstacles.rs index fb029bd2..1ee46c7c 100644 --- a/crates/fparkan-runtime/src/route_obstacles.rs +++ b/crates/fparkan-runtime/src/route_obstacles.rs @@ -1,6 +1,6 @@ //! Ground route clearance against the projected bounds of live buildings. -use crate::{EngineError, LoadedMissionState, ObjectHandle, TransformState, World}; +use crate::{EngineError, LoadedMissionState, MissionAssets, 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}; @@ -8,7 +8,7 @@ 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; +pub(super) const CORNER_GAP: f32 = 0.1; #[derive(Clone, Copy, Debug, PartialEq)] pub(super) struct Footprint { @@ -57,8 +57,8 @@ impl BuildingFootprints { .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))); + let footprint = model_footprint(&mission.mission_assets, index, pose); + self.entries.insert(id.0, (handle, pose, footprint)); } } } @@ -77,12 +77,11 @@ impl BuildingFootprints { } } -fn model_footprint( - mission: &LoadedMissionState, +pub(super) fn model_footprint( + assets: &MissionAssets, 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)?; @@ -176,16 +175,21 @@ impl Footprint { } pub(super) fn exits(self, p: [f32; 2]) -> [[f32; 2]; 4] { + let [x, y] = self.closest_point(p); let Self { min: [x0, y0], max: [x1, y1], } = self.expanded(CORNER_GAP); - [[x0, p[1]], [x1, p[1]], [p[0], y0], [p[0], y1]] + [[x0, y], [x1, y], [x, y0], [x, y1]] } pub(super) fn center(self) -> [f32; 2] { std::array::from_fn(|axis| self.min[axis].midpoint(self.max[axis])) } + + pub(super) fn closest_point(self, point: [f32; 2]) -> [f32; 2] { + std::array::from_fn(|axis| point[axis].clamp(self.min[axis], self.max[axis])) + } } fn distance(a: [f32; 2], b: [f32; 2]) -> f32 { -- cgit v1.2.3