diff options
Diffstat (limited to 'crates/fparkan-runtime/src/factory_release.rs')
| -rw-r--r-- | crates/fparkan-runtime/src/factory_release.rs | 122 |
1 files changed, 101 insertions, 21 deletions
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<f32>, } 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<Option<ReleaseSite>, 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<GroundSupport>, airborne: bool, + descent: Option<Descent>, +} + +#[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<Self> { + 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<TransformState, EngineError> { - 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 } } |
