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/ore_transport.rs | 143 +++++++++++++++++++++++----- 1 file changed, 118 insertions(+), 25 deletions(-) (limited to 'crates/fparkan-runtime/src/ore_transport.rs') diff --git a/crates/fparkan-runtime/src/ore_transport.rs b/crates/fparkan-runtime/src/ore_transport.rs index 0d88cace..92d2df55 100644 --- a/crates/fparkan-runtime/src/ore_transport.rs +++ b/crates/fparkan-runtime/src/ore_transport.rs @@ -1,13 +1,48 @@ -use crate::unit_patrol::UnitPatrol; -use crate::BehaviorOreState; +use crate::{ + route_motion::WAYPOINT_TOLERANCE, + route_obstacles::{model_footprint, Footprint, CORNER_GAP}, + route_traffic::body_radius, + unit_patrol::UnitPatrol, + BehaviorOreState, MissionAssets, MissionObjectDraft, TerrainWorld, TransformState, +}; +use fparkan_terrain::SurfaceQuery; use fparkan_world::{ - identity_metadata, registration_sequence, transform_state, ObjectHandle, OwnerId, World, + identity_metadata, registration_sequence, transform_state, ObjectHandle, OriginalObjectId, + OwnerId, World, }; +#[derive(Clone, Copy)] +pub(super) struct TransportGeometry<'a> { + 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 = -- cgit v1.2.3