diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 04:37:24 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 04:37:24 +0300 |
| commit | 6ed1829936f23c92b9aec24e0eef6884876bb99b (patch) | |
| tree | f138985c80f47df1d5440dd501590f7e083476da /crates/fparkan-runtime | |
| parent | 52a7977710508d31440d94a2d5ae345116e11448 (diff) | |
| download | fparkan-6ed1829936f23c92b9aec24e0eef6884876bb99b.tar.xz fparkan-6ed1829936f23c92b9aec24e0eef6884876bb99b.zip | |
feat(movement): let autonomous flyers climb above terrain
Feed verified CanFly profiles into the shared route follower. A flyer spends movement time climbing to clear its hull before advancing, checks the terrain along the proposed leg and retains world altitude over lower ground. Ground units keep their terrain-following step.
Covered takeoff, an intermediate ridge and a valley in route tests. Confirmed a stock AutoDemo flyer takes off and travels, observed the airborne scene with clean Vulkan validation, and ran the 29-mission simulation corpus. Descent and altitude-aware obstacle avoidance remain open.
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/flight_altitude.rs | 107 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/route_motion.rs | 89 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/route_traffic.rs | 63 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 47 |
5 files changed, 263 insertions, 44 deletions
diff --git a/crates/fparkan-runtime/src/flight_altitude.rs b/crates/fparkan-runtime/src/flight_altitude.rs new file mode 100644 index 00000000..d5276b46 --- /dev/null +++ b/crates/fparkan-runtime/src/flight_altitude.rs @@ -0,0 +1,107 @@ +//! A terrain clearance policy for the shared route follower. + +use crate::EngineError; + +#[derive(Clone, Copy)] +pub(super) struct FlightClearance { + pub(super) body_radius: f32, +} + +impl FlightClearance { + pub(super) fn raised(self, altitude: f32, ground: f32, speed: f32, seconds: f32) -> (f32, f32) { + // Keep approximately one radius of air under the hull, even when a + // parked actor starts on the surface. No descent is requested here. + let target = ground + 2.0 * self.body_radius; + if target <= altitude || speed <= 0.0 { + return (altitude, seconds); + } + let rise_time = (target - altitude) / speed; + if rise_time >= seconds { + ((altitude + speed * seconds).min(target), 0.0) + } else { + (target, seconds - rise_time) + } + } + + pub(super) fn corridor_floor( + self, + from: [f32; 2], + to: [f32; 2], + endpoint_floor: f32, + height_at: &mut impl FnMut([f32; 2]) -> Result<Option<f32>, EngineError>, + ) -> Result<f32, EngineError> { + let [dx, dy] = [to[0] - from[0], to[1] - from[1]]; + let samples = (dx.hypot(dy) / self.body_radius).ceil() as usize; + let samples = samples.clamp(2, 256); + let mut highest = endpoint_floor; + for index in 1..samples { + let t = index as f32 / samples as f32; + if let Some(ground) = height_at([from[0] + dx * t, from[1] + dy * t])? { + highest = highest.max(ground); + } + } + Ok(highest) + } +} + +#[cfg(test)] +mod tests { + use crate::{ + create, frame, load_gameplay_mission, EngineServices, MissionMode, MissionRequest, + MissionStartupInput, + }; + use fparkan_terrain::SurfaceQuery; + use fparkan_vfs::DirectoryVfs; + use fparkan_world::{handle_by_original_id, transform_state, OriginalObjectId}; + use std::sync::Arc; + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_stock_autodemo_flyer_takes_off_and_moves_above_the_surface() { + 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/Autodemo.00/data.tma".into(), + mode: MissionMode::AutoDemo, + }, + MissionStartupInput { + queue_time_ms: 0, + research_creation_gate: false, + }, + None, + ) + .unwrap(); + let id = OriginalObjectId(2); + assert_eq!( + engine.loaded.as_ref().unwrap().unit_can_fly.get(&id.0), + Some(&Some(true)) + ); + let handle = handle_by_original_id(&engine.world, id).unwrap(); + let [a, b, initial_z] = transform_state(&engine.world, handle) + .unwrap() + .position + .map(f32::from_bits); + for now in (0..=5_000).step_by(50) { + frame(&mut engine, now).unwrap(); + let [x, y, z] = transform_state(&engine.world, handle) + .unwrap() + .position + .map(f32::from_bits); + if (x - a).hypot(y - b) > 1.0 { + let floor = engine + .loaded + .as_ref() + .unwrap() + .terrain + .height_at([x, y]) + .unwrap() + .unwrap(); + assert!(z > initial_z + 1.0 && z > floor + 1.0); + return; + } + } + panic!("stock flyer did not move after takeoff"); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index e27d9c18..d8eaf7f6 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -75,6 +75,7 @@ mod clan_script_init; mod clan_targeting; mod construction; mod construction_site; +mod flight_altitude; mod laboratory; use behavior_clock::next_crt_rand; mod mesh_collision; diff --git a/crates/fparkan-runtime/src/route_motion.rs b/crates/fparkan-runtime/src/route_motion.rs index b02a4f2c..59b7f766 100644 --- a/crates/fparkan-runtime/src/route_motion.rs +++ b/crates/fparkan-runtime/src/route_motion.rs @@ -1,6 +1,7 @@ //! Follow a terrain route with a shared time budget for turning and travel. use crate::{ + flight_altitude::FlightClearance, route_obstacles::{open_leg, Footprint}, EngineError, TerrainWorld, TransformState, }; @@ -9,6 +10,13 @@ use std::collections::VecDeque; pub(super) const WAYPOINT_TOLERANCE: f32 = 0.05; +#[derive(Clone, Copy, Default)] +pub(super) struct RouteClearance<'a> { + pub(super) buildings: &'a [Footprint], + pub(super) traffic: &'a [Footprint], + pub(super) flight: Option<FlightClearance>, +} + pub(super) fn route_points( terrain: &TerrainWorld, from: [f32; 3], @@ -44,17 +52,26 @@ pub(super) fn follow_route( points: &mut VecDeque<[f32; 2]>, speed: f32, mut seconds: f32, - buildings: &[Footprint], - traffic: &[Footprint], + clearance: RouteClearance<'_>, mut height_at: impl FnMut([f32; 2]) -> Result<Option<f32>, EngineError>, ) -> Result<(TransformState, Footprint), EngineError> { let [mut x, mut y, mut z] = pose.position.map(f32::from_bits); let mut swept = Footprint::point([x, y]); + if let Some(flight) = clearance.flight { + if let Some(floor) = height_at([x, y])? { + (z, seconds) = flight.raised(z, floor, speed, seconds); + pose.position[2] = z.to_bits(); + } + } while seconds > 0.0 && !points.is_empty() { - if !open_leg([x, y], points, buildings, traffic, |point| { - height_at(point).map(|z| z.is_some()) - })? { - if traffic.is_empty() { + if !open_leg( + [x, y], + points, + clearance.buildings, + clearance.traffic, + |point| height_at(point).map(|z| z.is_some()), + )? { + if clearance.traffic.is_empty() { points.clear(); } break; @@ -84,10 +101,21 @@ pub(super) fn follow_route( points.clear(); break; }; - let offset = height_at([x, y])?.map_or(0.0, |height| z - height); + if let Some(flight) = clearance.flight { + let floor = flight.corridor_floor([x, y], xy, ground, &mut height_at)?; + let (new_z, remaining) = flight.raised(z, floor, speed, seconds); + if new_z != z || remaining != seconds { + z = new_z; + pose.position[2] = z.to_bits(); + seconds = remaining; + continue; + } + } else { + let offset = height_at([x, y])?.map_or(0.0, |height| z - height); + z = ground + offset; + } [x, y] = xy; swept.include(xy); - z = ground + offset; pose.position = [x, y, z].map(f32::to_bits); if !reached { break; @@ -109,9 +137,14 @@ 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)) - }) + let (end, _) = follow_route( + start, + &mut route, + 2.0, + 2.0, + RouteClearance::default(), + |[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; @@ -120,4 +153,38 @@ mod tests { assert!((z - (x + y + 2.0)).abs() < 0.000_01); assert_eq!(route, [[4.0, 1.0]]); } + + #[test] + fn airborne_route_spends_travel_time_to_take_off() { + let start = TransformState::default(); + let mut route = VecDeque::from([[0.0, 8.0]]); + let clearance = RouteClearance { + flight: Some(FlightClearance { body_radius: 1.0 }), + ..Default::default() + }; + let flat = |_| Ok(Some(0.0)); + let (rising, _) = follow_route(start, &mut route, 2.0, 0.5, clearance, flat).unwrap(); + assert_eq!(rising.position.map(f32::from_bits), [0.0, 0.0, 1.0]); + let (moving, _) = follow_route(rising, &mut route, 2.0, 2.5, clearance, flat).unwrap(); + assert_eq!(moving.position.map(f32::from_bits), [0.0, 4.0, 2.0]); + } + + #[test] + fn airborne_route_climbs_for_an_intermediate_ridge_and_stays_above_the_valley() { + let start = TransformState { + position: [0.0_f32, 0.0, 2.0].map(f32::to_bits), + ..Default::default() + }; + let mut route = VecDeque::from([[0.0, 10.0]]); + let clearance = RouteClearance { + flight: Some(FlightClearance { body_radius: 1.0 }), + ..Default::default() + }; + let ground = |[_, y]: [f32; 2]| Ok(Some(if (3.0..5.0).contains(&y) { 4.0 } else { 0.0 })); + let (crossed, _) = follow_route(start, &mut route, 4.0, 3.0, clearance, ground).unwrap(); + assert_eq!(crossed.position.map(f32::from_bits), [0.0, 8.0, 6.0]); + let (arrived, _) = follow_route(crossed, &mut route, 4.0, 1.0, clearance, ground).unwrap(); + assert_eq!(arrived.position.map(f32::from_bits), [0.0, 10.0, 6.0]); + assert!(route.is_empty()); + } } diff --git a/crates/fparkan-runtime/src/route_traffic.rs b/crates/fparkan-runtime/src/route_traffic.rs index 26116ceb..193ea995 100644 --- a/crates/fparkan-runtime/src/route_traffic.rs +++ b/crates/fparkan-runtime/src/route_traffic.rs @@ -85,7 +85,7 @@ impl FrameTraffic { #[cfg(test)] mod tests { use super::*; - use crate::route_motion::follow_route; + use crate::route_motion::{follow_route, RouteClearance}; use std::{collections::VecDeque, f32::consts::FRAC_PI_2}; fn pose(xy: [f32; 2], yaw: f32) -> TransformState { @@ -132,11 +132,18 @@ mod tests { let Some(blockers) = traffic.blockers(handles[i], xy(poses[i]), 0.5) else { continue; }; - let (after, sweep) = - follow_route(poses[i], &mut routes[i], 3.0, 0.1, &[], &blockers, |_| { - Ok(Some(0.0)) - }) - .unwrap(); + let (after, sweep) = follow_route( + poses[i], + &mut routes[i], + 3.0, + 0.1, + RouteClearance { + traffic: &blockers, + ..Default::default() + }, + |_| Ok(Some(0.0)), + ) + .unwrap(); poses[i] = after; traffic.reserve(handles[i], sweep); } @@ -178,8 +185,10 @@ mod tests { &mut VecDeque::from([[4.0, 0.0]]), 10.0, 1.0, - &[], - &first_blockers, + RouteClearance { + traffic: &first_blockers, + ..Default::default() + }, flat, ) .unwrap(); @@ -187,8 +196,18 @@ mod tests { traffic.reserve(first, swept); let second_blockers = traffic.blockers(second, xy(start[1]), 0.5).unwrap(); let mut route = VecDeque::from([[0.0, 4.0]]); - let (after, _) = - follow_route(start[1], &mut route, 10.0, 1.0, &[], &second_blockers, flat).unwrap(); + let (after, _) = follow_route( + start[1], + &mut route, + 10.0, + 1.0, + RouteClearance { + traffic: &second_blockers, + ..Default::default() + }, + flat, + ) + .unwrap(); assert!(xy(after)[1] < -1.0); assert_eq!(route.back(), Some(&[0.0, 4.0])); } @@ -201,12 +220,30 @@ mod tests { let start = pose(position, 0.0); let bounds = Footprint::point(target).expanded(1.0); let flat = |_| Ok(Some(0.0)); - let (waiting, _) = - follow_route(start, &mut route, 10.0, 2.0, &[], &[bounds], flat).unwrap(); + let (waiting, _) = follow_route( + start, + &mut route, + 10.0, + 2.0, + RouteClearance { + traffic: &[bounds], + ..Default::default() + }, + flat, + ) + .unwrap(); assert!(xy(waiting)[1] > 3.0); assert!(!bounds.contains(xy(waiting))); assert_eq!(route, [target]); - let (arrived, _) = follow_route(waiting, &mut route, 10.0, 2.0, &[], &[], flat).unwrap(); + let (arrived, _) = follow_route( + waiting, + &mut route, + 10.0, + 2.0, + RouteClearance::default(), + flat, + ) + .unwrap(); assert_eq!(xy(arrived), target); assert!(route.is_empty()); } diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index 5479ef95..e04b8f3a 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -1,9 +1,10 @@ use super::{ autodemo::{MOBILE_UNIT, PLANT_CLASS}, boarding::WARBOT_CLASS, + flight_altitude::FlightClearance, next_crt_rand, patrol_points::{PatrolPointReport, PatrolPoints}, - route_motion::{follow_route, route_points, turn_toward}, + route_motion::{follow_route, route_points, turn_toward, RouteClearance}, route_obstacles::BuildingFootprints, route_traffic::FrameTraffic, unit_combat::CombatTarget, @@ -13,7 +14,7 @@ use super::{ }; use fparkan_script::{Handler15TargetPayload, ScriptDispatchSelector, VarSet}; use fparkan_terrain::{NavigationQuery, SurfaceQuery}; -use fparkan_world::{identity_metadata, set_transform, transform_state}; +use fparkan_world::{set_transform, transform_state}; use std::collections::{BTreeMap, HashSet, VecDeque}; mod orders; @@ -442,6 +443,9 @@ impl UnitPatrol { continue; }; let from = before.position.map(f32::from_bits); + let motion_object = crate::CombatObject::read(world, unit.handle); + let original_id = motion_object.original_id.unwrap_or(unit.handle.slot); + let can_fly = chassis.get(&original_id).copied().flatten(); if let Some(destinations) = &mut unit.destinations { if destinations .front() @@ -475,11 +479,6 @@ impl UnitPatrol { && unit.attack_target.is_none() && unit.raid_factory.is_none(); if place_patrol && (enemy.is_none() || unit.place_points.current().is_none()) { - let original_id = identity_metadata(world, unit.handle) - .ok() - .and_then(|identity| identity.original_id) - .map_or(unit.handle.slot, |id| id.0); - let can_fly = chassis.get(&original_id).copied().flatten(); if let Some(bounds) = terrain.surface_bounds() { let mut checks = 0; let refreshed = unit.place_points.refresh( @@ -550,8 +549,11 @@ impl UnitPatrol { if seconds == 0.0 { continue; } - let motion_object = crate::CombatObject::read(world, unit.handle); state.motion_report.update(motion_object, now_ms); + let radius = traffic.radius(unit.handle); + let flight = (can_fly == Some(true) && radius > 0.0).then_some(FlightClearance { + body_radius: radius, + }); if let Some(enemy) = enemy.filter(|enemy| enemy.in_range) { let mut after = before; let (yaw, _) = turn_toward( @@ -560,6 +562,14 @@ impl UnitPatrol { seconds, ); after.orientation[2] = yaw.to_bits(); + if let Some(flight) = flight { + if let Some(floor) = terrain + .height_at([from[0], from[1]]) + .map_err(EngineError::Terrain)? + { + after.position[2] = flight.raised(from[2], floor, 9.0, seconds).0.to_bits(); + } + } if after != before { poses.push((unit.handle, before, after)); } @@ -597,7 +607,7 @@ impl UnitPatrol { }; unit.retry_ms = now_ms.saturating_add(1_000); } - if unit.points.is_empty() { + if unit.points.is_empty() && flight.is_none() { continue; } let speed = if (place_patrol && enemy.is_none()) || unit.destinations.is_some() { @@ -606,10 +616,6 @@ impl UnitPatrol { } else { UnitSpeedKind::Go }; - let object = identity_metadata(world, unit.handle) - .ok() - .and_then(|identity| identity.original_id) - .map_or(unit.handle.slot, |id| id.0); let table = mission .control_device_tables .iter() @@ -618,23 +624,24 @@ impl UnitPatrol { .behavior_profiles .iter() .find(|profile| profile.handle == unit.handle); - order_speed(table, behavior, object, kind, &mut state.speed_report) + order_speed(table, behavior, original_id, kind, &mut state.speed_report) } else { 9.0 }; - let clearance = traffic.radius(unit.handle); - let Some(neighbors) = traffic.blockers(unit.handle, [from[0], from[1]], clearance) - else { + let Some(neighbors) = traffic.blockers(unit.handle, [from[0], from[1]], radius) else { continue; }; - let buildings = state.buildings.with_clearance(clearance); + let buildings = state.buildings.with_clearance(radius); let (after, swept) = follow_route( before, &mut unit.points, speed, seconds, - &buildings, - &neighbors, + RouteClearance { + buildings: &buildings, + traffic: &neighbors, + flight, + }, |xy| terrain.height_at(xy).map_err(EngineError::Terrain), )?; traffic.reserve(unit.handle, swept); |
