From 0fade3cf1100f9122d104f69d9cce2efc6306279 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 10:01:44 +0400 Subject: feat(factory): release assembled warbots above the roof Find the factory roof with a vertical ray, apply the native height offsets, and keep the departure task active through descent and apron clearance. Verify both stock Two Fortresses factories and transport delivery, and check the descent in a fixed-camera Vulkan run. --- crates/fparkan-runtime/src/factory_release.rs | 122 +++++++++++++++++++++----- crates/fparkan-runtime/src/production.rs | 59 ++++++------- crates/fparkan-runtime/src/route_obstacles.rs | 9 ++ crates/fparkan-runtime/src/unit_patrol.rs | 8 +- 4 files changed, 145 insertions(+), 53 deletions(-) (limited to 'crates/fparkan-runtime') diff --git a/crates/fparkan-runtime/src/factory_release.rs b/crates/fparkan-runtime/src/factory_release.rs index 03975a0f..7a3b724f 100644 --- a/crates/fparkan-runtime/src/factory_release.rs +++ b/crates/fparkan-runtime/src/factory_release.rs @@ -1,19 +1,22 @@ -//! Select a free point on the ground for the current outdoor factory release. +//! Release above the factory roof and clear the apron before taking orders. use crate::{ flight_altitude::FlightClearance, ground_support::GroundSupport, + projectile_parts::root_triangle_hit, route_motion::{follow_route, RouteClearance, WAYPOINT_TOLERANCE}, route_obstacles::{BuildingFootprints, Footprint}, route_traffic::{body_radius, FrameTraffic}, EngineError, LoadedMissionState, ObjectHandle, TransformState, World, }; use fparkan_terrain::SurfaceQuery; +use fparkan_world::transform_state; use std::collections::VecDeque; pub(super) struct ReleaseSite { pub(super) position: [f32; 3], - pub(super) exit: Option<[f32; 2]>, + exit: Option<[f32; 2]>, + roof: Option, } pub(super) fn clear_release_position( @@ -21,6 +24,7 @@ pub(super) fn clear_release_position( mission: &LoadedMissionState, buildings: &mut BuildingFootprints, plant: ObjectHandle, + plant_index: usize, slot: usize, preferred: [f32; 3], ) -> Result, EngineError> { @@ -39,6 +43,7 @@ pub(super) fn clear_release_position( return Ok(Some(ReleaseSite { position: preferred, exit: None, + roof: None, })); }; let mut blocked = buildings.with_clearance(radius); @@ -48,7 +53,7 @@ pub(super) fn clear_release_position( return Ok(None); }; blocked.extend(neighbors); - release_position( + let site = release_position( [x, y], plant_bounds.expanded(radius), radius, @@ -59,7 +64,33 @@ pub(super) fn clear_release_position( .height_at(point) .map_err(EngineError::Terrain) }, - ) + )?; + let Some(mut site) = site else { + return Ok(None); + }; + let plant_pose = transform_state(world, plant)?; + let [x, y, z] = plant_pose.position.map(f32::from_bits); + if let Some(terrain) = mission + .terrain + .height_at([x, y]) + .map_err(EngineError::Terrain)? + { + let top = z + 1000.0; + if let Some((fraction, _)) = root_triangle_hit( + &mission.mission_assets, + plant_index, + plant_pose, + [[x, y, top], [x, y, terrain]], + |_| true, + ) { + let roof = top + fraction * (terrain - top); + site.exit = Some(site.exit.unwrap_or([site.position[0], site.position[1]])); + // Behavior's upper support ray adds 100 before the 15-unit release offset. + site.position = [x, y, roof + 100.0 + 15.0]; + site.roof = Some(roof); + } + } + Ok(Some(site)) } fn release_position( @@ -87,6 +118,7 @@ fn release_position( return Ok(Some(ReleaseSite { position, exit: None, + roof: None, })); } let mut exits = apron.exits(point); @@ -99,6 +131,7 @@ fn release_position( return Ok(Some(ReleaseSite { position, exit: Some(exit), + roof: None, })); } } @@ -109,18 +142,28 @@ fn release_position( #[derive(Clone)] pub(super) struct FactoryDeparture { pub(super) handle: ObjectHandle, + pub(super) plant: ObjectHandle, goal: [f32; 2], ground: Option, airborne: bool, + descent: Option, +} + +#[derive(Clone)] +struct Descent { + roof: f32, + fall_speed: f32, } impl FactoryDeparture { - pub(super) fn new( + pub(super) fn from_site( handle: ObjectHandle, + plant: ObjectHandle, index: usize, - goal: [f32; 2], + site: ReleaseSite, mission: &LoadedMissionState, - ) -> Self { + ) -> Option { + let goal = site.exit?; let airborne = mission.object_drafts[index] .original_id .and_then(|id| mission.unit_chassis_flags.get(&id.0)) @@ -129,24 +172,35 @@ impl FactoryDeparture { let ground = (!airborne) .then(|| GroundSupport::prepare(&mission.mission_assets, index)) .flatten(); - Self { + Some(Self { handle, + plant, goal, ground, airborne, - } + descent: site.roof.map(|roof| Descent { + roof, + fall_speed: 0.0, + }), + }) } pub(super) fn move_out( - &self, + &mut self, before: TransformState, mission: &LoadedMissionState, traffic: &mut FrameTraffic, buildings: &BuildingFootprints, seconds: f32, ) -> Result { - let [x, y, _] = before.position.map(f32::from_bits); + let [x, y, altitude] = before.position.map(f32::from_bits); let radius = traffic.radius(self.handle); + let plant_bounds = buildings + .for_handle(self.plant) + .map(|bounds| bounds.expanded(radius)); + let above_plant = + self.descent.is_some() && plant_bounds.is_some_and(|bounds| bounds.contains([x, y])); + let obstacles = buildings.with_clearance_except(radius, above_plant.then_some(self.plant)); let Some(neighbors) = traffic.blockers(self.handle, [x, y], radius) else { return Ok(before); }; @@ -161,31 +215,57 @@ impl FactoryDeparture { .map(|profile| f32::from_bits(profile.current_words[7])) .filter(|speed| speed.is_finite() && *speed >= 0.0) .unwrap_or(9.0); - let (after, swept) = follow_route( + let mut height_at = |point| { + mission + .terrain + .height_at(point) + .map_err(EngineError::Terrain) + }; + let (mut after, swept) = follow_route( before, &mut VecDeque::from([self.goal]), speed, seconds, RouteClearance { - buildings: &buildings.with_clearance(radius), + buildings: &obstacles, traffic: &neighbors, flight, - ground: self.ground, - }, - |point| { - mission - .terrain - .height_at(point) - .map_err(EngineError::Terrain) + ground: self.descent.is_none().then_some(self.ground).flatten(), }, + &mut height_at, )?; + if let Some(descent) = &mut self.descent { + let [nx, ny, _] = after.position.map(f32::from_bits); + let xy = [nx, ny]; + if let Some(terrain) = height_at(xy)? { + let floor = if let Some(ground) = self.ground { + ground.height(xy, after, &mut height_at)?.unwrap_or(terrain) + } else { + terrain + if self.airborne { 2.0 * radius } else { 2.0 } + }; + let over_roof = plant_bounds.is_some_and(|bounds| bounds.contains(xy)); + let support = if over_roof { + floor.max(descent.roof + radius) + } else { + floor + }; + // shortcut: constant gravity until the original release motion is decoded. + let end_speed = descent.fall_speed + 9.81 * seconds; + let z = (altitude - descent.fall_speed.midpoint(end_speed) * seconds).max(support); + descent.fall_speed = if z > support { end_speed } else { 0.0 }; + after.position[2] = z.to_bits(); + if !over_roof && z == floor { + self.descent = None; + } + } + } traffic.reserve(self.handle, swept); Ok(after) } pub(super) fn finished(&self, pose: TransformState) -> bool { let [x, y, _] = pose.position.map(f32::from_bits); - (x - self.goal[0]).hypot(y - self.goal[1]) <= WAYPOINT_TOLERANCE + self.descent.is_none() && (x - self.goal[0]).hypot(y - self.goal[1]) <= WAYPOINT_TOLERANCE } } diff --git a/crates/fparkan-runtime/src/production.rs b/crates/fparkan-runtime/src/production.rs index f1ba5035..b8e33e00 100644 --- a/crates/fparkan-runtime/src/production.rs +++ b/crates/fparkan-runtime/src/production.rs @@ -291,12 +291,16 @@ impl Production { } ready &= paid.ready(); } - if !ready { + if !ready + || mission + .unit_patrol + .as_ref() + .is_some_and(|patrol| patrol.has_factory_departure(order.plant)) + { return Ok(None); } let pose = transform_state(world, order.plant)?; - // shortcut: the outdoor direction selects an apron until the elevated - // assembly platform and its ramp are simulated. + // shortcut: use the outdoor direction to select the exit from the apron. let preferred = reinforcement_position( pose.position.map(f32::from_bits), pose.orientation.map(f32::from_bits), @@ -311,6 +315,7 @@ impl Production { mission, &mut self.buildings, order.plant, + order.plant_index, order.slot, preferred, )? @@ -356,8 +361,9 @@ impl Production { .expect("free order belongs to a prepared factory"); *count -= 1; } - if let Some(exit) = site.exit { - let departure = FactoryDeparture::new(handle, order.slot, exit, mission); + if let Some(departure) = + FactoryDeparture::from_site(handle, order.plant, order.slot, site, mission) + { mission .unit_patrol .get_or_insert_with(crate::unit_patrol::UnitPatrol::scripted) @@ -907,6 +913,8 @@ mod tests { None, ) .unwrap(); + let enemy_factory = handle_by_original_id(&engine.world, OriginalObjectId(9)).unwrap(); + let enemy_pose = transform_state(&engine.world, enemy_factory).unwrap(); let report = super::super::simulate_mission_report(&mut engine, 40_000).unwrap(); assert!( report.first_factory_completion_ms.contains_key(&1), @@ -949,33 +957,22 @@ mod tests { .produced_unit { let pose = transform_state(&engine.world, unit.handle).unwrap(); - let [x, y, _] = pose.position.map(f32::from_bits); + let [x, y, z] = pose.position.map(f32::from_bits); let mission = engine.loaded.as_ref().unwrap(); - let radius = crate::route_traffic::body_radius( - &mission.mission_assets, - unit.object_index, - pose, - ) - .unwrap(); - let mut buildings = BuildingFootprints::default(); - buildings.refresh(&engine.world, mission); - assert!(buildings - .with_clearance(radius) - .iter() - .all(|bounds| !bounds.contains([x, y]))); - let traffic = crate::route_traffic::FrameTraffic::read(&engine.world, mission); - assert!(traffic - .blockers(unit.handle, [x, y], radius) - .unwrap() - .iter() - .all(|bounds| !bounds.contains([x, y]))); - if mission + let (plant, expected_z) = if unit.clan == 1 { + (enemy_pose, 205.0637) + } else { + (transform_state(&engine.world, factory).unwrap(), 196.756) + }; + let [px, py, _] = plant.position.map(f32::from_bits); + assert_eq!([x, y], [px, py]); + assert!((z - expected_z).abs() < 0.001, "{z} vs {expected_z}"); + assert!(mission .unit_patrol .as_ref() - .is_some_and(|patrol| patrol.is_leaving_factory(unit.handle)) - { - departures.push((unit.handle, [x, y])); - } + .unwrap() + .is_leaving_factory(unit.handle)); + departures.push((unit.handle, [x, y, z])); completed.push((unit.clan, unit.object_index)); } } @@ -1029,8 +1026,8 @@ mod tests { return false; }; transform_state(&engine.world, actor).is_ok_and(|pose| { - let [x, y, _] = pose.position.map(f32::from_bits); - !patrol.is_leaving_factory(actor) && [x, y] != start + let [x, y, z] = pose.position.map(f32::from_bits); + !patrol.is_leaving_factory(actor) && [x, y] != start[..2] && z < start[2] }) })); return; diff --git a/crates/fparkan-runtime/src/route_obstacles.rs b/crates/fparkan-runtime/src/route_obstacles.rs index 1ee46c7c..eb29b680 100644 --- a/crates/fparkan-runtime/src/route_obstacles.rs +++ b/crates/fparkan-runtime/src/route_obstacles.rs @@ -64,8 +64,17 @@ impl BuildingFootprints { } pub(super) fn with_clearance(&self, radius: f32) -> Vec { + self.with_clearance_except(radius, None) + } + + pub(super) fn with_clearance_except( + &self, + radius: f32, + skip: Option, + ) -> Vec { self.entries .values() + .filter(|(handle, _, _)| Some(*handle) != skip) .filter_map(|(_, _, footprint)| footprint.map(|bounds| bounds.expanded(radius))) .collect() } diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 8050caac..e73c2404 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -319,6 +319,12 @@ impl UnitPatrol { .any(|departure| departure.handle == unit) } + pub(super) fn has_factory_departure(&self, plant: ObjectHandle) -> bool { + self.factory_departures + .iter() + .any(|departure| departure.plant == plant) + } + pub(super) fn is_moving_to_destination(&self, handle: ObjectHandle) -> bool { self.units .iter() @@ -461,7 +467,7 @@ impl UnitPatrol { }; let mut poses = Vec::new(); if seconds > 0.0 { - for departure in std::mem::take(&mut state.factory_departures) { + for mut departure in std::mem::take(&mut state.factory_departures) { let handle = departure.handle; let Ok(before) = transform_state(world, handle) else { continue; -- cgit v1.2.3