diff options
Diffstat (limited to 'crates/fparkan-runtime')
| -rw-r--r-- | crates/fparkan-runtime/src/ground_support.rs | 199 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 1 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/route_motion.rs | 6 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/unit_patrol.rs | 25 |
4 files changed, 230 insertions, 1 deletions
diff --git a/crates/fparkan-runtime/src/ground_support.rs b/crates/fparkan-runtime/src/ground_support.rs new file mode 100644 index 00000000..3e60554a --- /dev/null +++ b/crates/fparkan-runtime/src/ground_support.rs @@ -0,0 +1,199 @@ +//! Terrain support inferred from the chassis's initial rendered geometry. + +use crate::{initial_component_node_times, EngineError, MissionAssets, TransformState}; +use fparkan_msh::{ + draw_batches, node38_fallback_hierarchy, node38_sampled_hierarchy_at_times, selected_slot, + Group, Lod, NodeId, +}; +use fparkan_render::LegacyIron3dEulerTransform; + +#[derive(Clone, Copy, Debug)] +pub(super) struct GroundSupport { + // One lowest visible mesh point per quadrant of the chassis. A central + // ground sample alone can miss the ground under an outboard foot. + contacts: [Option<[f32; 3]>; 4], +} + +impl GroundSupport { + pub(super) fn prepare(assets: &MissionAssets, index: usize) -> Option<Self> { + let visual = assets.visual_by_id(assets.visual_for_object(index)?)?; + let model = &assets.model_by_id(visual.model_id?)?.validated; + let bindings = assets + .object_component_controls + .get(index) + .and_then(|controls| controls.first()) + .and_then(Option::as_ref) + .map_or(&[][..], |control| control.bindings.as_slice()); + let hierarchy = initial_component_node_times(model.node_count, bindings) + .ok() + .and_then(|times| node38_sampled_hierarchy_at_times(model, ×)) + .or_else(|| node38_fallback_hierarchy(model))?; + let mut contacts: [Option<[f32; 3]>; 4] = [None; 4]; + for (index, pose) in hierarchy.poses.into_iter().enumerate() { + let Some(slot) = selected_slot(model, NodeId(index as u32), Lod(0), Group(0)) else { + continue; + }; + for batch in draw_batches(model, slot).ok()? { + let start = batch.index_start as usize; + for &vertex in model + .indices + .get(start..start + usize::from(batch.index_count))? + { + let point = pose.transform_point( + *model + .positions + .get(batch.base_vertex as usize + usize::from(vertex))?, + ); + if !point.into_iter().all(f32::is_finite) { + continue; + } + let quadrant = usize::from(point[0] >= 0.0) + 2 * usize::from(point[1] >= 0.0); + if contacts[quadrant].is_none_or(|other| point[2] < other[2]) { + contacts[quadrant] = Some(point); + } + } + } + } + contacts + .iter() + .any(Option::is_some) + .then_some(Self { contacts }) + } + + /// Lowest world Z that keeps the sampled chassis contacts above terrain. + pub(super) fn height( + self, + xy: [f32; 2], + pose: TransformState, + height_at: &mut impl FnMut([f32; 2]) -> Result<Option<f32>, EngineError>, + ) -> Result<Option<f32>, EngineError> { + let world = LegacyIron3dEulerTransform { + translation: [xy[0], xy[1], 0.0], + orientation_radians: pose.orientation.map(f32::from_bits), + }; + let scale = pose.scale.map(f32::from_bits); + let mut root = None::<f32>; + for contact in self.contacts.into_iter().flatten() { + let Some([x, y, offset]) = world.try_transform_scaled_point(contact, scale) else { + continue; + }; + if let Some(ground) = height_at([x, y])? { + let z = ground - offset; + root = Some(root.map_or(z, |other| other.max(z))); + } + } + Ok(root) + } + + pub(super) fn reanchor( + self, + before: TransformState, + after: &mut TransformState, + mut height_at: impl FnMut([f32; 2]) -> Result<Option<f32>, EngineError>, + ) -> Result<(), EngineError> { + let [x, y, z] = before.position.map(f32::from_bits); + let [nx, ny, nz] = after.position.map(f32::from_bits); + let previous_support = self.height([x, y], before, &mut height_at)?; + let next_support = if [x, y] == [nx, ny] && before.orientation == after.orientation { + previous_support + } else { + self.height([nx, ny], *after, &mut height_at)? + }; + if let Some(next_support) = next_support { + let altitude = previous_support.map_or(nz.max(next_support), |height| { + next_support + (z - height).max(0.0) + }); + after.position[2] = altitude.to_bits(); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + 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] + fn support_clears_the_high_foot_and_does_not_keep_the_rise_after_leaving() { + let model = GroundSupport { + contacts: [Some([-1.0, 0.0, -2.0]), Some([1.0, 0.0, -2.0]), None, None], + }; + let ground = |[x, y]: [f32; 2]| Ok(Some(if x > 0.5 && y == 0.0 { 3.0 } else { 0.0 })); + let embedded = TransformState { + position: [0.0_f32, 0.0, 1.0].map(f32::to_bits), + scale: [1.0_f32.to_bits(); 3], + ..Default::default() + }; + let mut planted = embedded; + model.reanchor(embedded, &mut planted, ground).unwrap(); + assert_eq!(planted.position.map(f32::from_bits), [0.0, 0.0, 5.0]); + let mut downhill = planted; + downhill.position[1] = 2.0_f32.to_bits(); + model.reanchor(planted, &mut downhill, ground).unwrap(); + assert_eq!(downhill.position.map(f32::from_bits), [0.0, 2.0, 2.0]); + } + + #[test] + #[ignore = "requires licensed corpus"] + fn licensed_stock_heavy_autodemo_chassis_stays_above_its_ground_contacts() { + 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 index = engine + .loaded + .as_ref() + .unwrap() + .object_drafts + .iter() + .position(|draft| draft.resource_name_raw.ends_with(b"w_b_wlk1.dat")) + .unwrap(); + let id = OriginalObjectId(index as u32); + let support = + GroundSupport::prepare(&engine.loaded.as_ref().unwrap().mission_assets, index).unwrap(); + let mut previous = None; + for now in (0..=210_000).step_by(50) { + frame(&mut engine, now).unwrap(); + let Some(handle) = handle_by_original_id(&engine.world, id) else { + continue; + }; + if previous.replace(handle) != Some(handle) { + continue; + } + let pose = transform_state(&engine.world, handle).unwrap(); + let [x, y, z] = pose.position.map(f32::from_bits); + let terrain = &engine.loaded.as_ref().unwrap().terrain; + let min_z = support + .height([x, y], pose, &mut |xy| { + terrain.height_at(xy).map_err(EngineError::Terrain) + }) + .unwrap() + .unwrap(); + assert!( + z + 0.001 >= min_z, + "at {now} ms: heavy chassis Z {z} intersects support Z {min_z}" + ); + return; + } + panic!("stock heavy chassis did not reach an active frame"); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index d8eaf7f6..991e5efc 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -76,6 +76,7 @@ mod clan_targeting; mod construction; mod construction_site; mod flight_altitude; +mod ground_support; 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 59b7f766..5ebaae78 100644 --- a/crates/fparkan-runtime/src/route_motion.rs +++ b/crates/fparkan-runtime/src/route_motion.rs @@ -2,6 +2,7 @@ use crate::{ flight_altitude::FlightClearance, + ground_support::GroundSupport, route_obstacles::{open_leg, Footprint}, EngineError, TerrainWorld, TransformState, }; @@ -15,6 +16,7 @@ pub(super) struct RouteClearance<'a> { pub(super) buildings: &'a [Footprint], pub(super) traffic: &'a [Footprint], pub(super) flight: Option<FlightClearance>, + pub(super) ground: Option<GroundSupport>, } pub(super) fn route_points( @@ -55,6 +57,7 @@ pub(super) fn follow_route( clearance: RouteClearance<'_>, mut height_at: impl FnMut([f32; 2]) -> Result<Option<f32>, EngineError>, ) -> Result<(TransformState, Footprint), EngineError> { + let before = pose; 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 { @@ -123,6 +126,9 @@ pub(super) fn follow_route( points.pop_front(); seconds -= travel_time; } + if let Some(ground) = clearance.ground { + ground.reanchor(before, &mut pose, height_at)?; + } Ok((pose, swept)) } diff --git a/crates/fparkan-runtime/src/unit_patrol.rs b/crates/fparkan-runtime/src/unit_patrol.rs index e04b8f3a..57e01b78 100644 --- a/crates/fparkan-runtime/src/unit_patrol.rs +++ b/crates/fparkan-runtime/src/unit_patrol.rs @@ -2,6 +2,7 @@ use super::{ autodemo::{MOBILE_UNIT, PLANT_CLASS}, boarding::WARBOT_CLASS, flight_altitude::FlightClearance, + ground_support::GroundSupport, next_crt_rand, patrol_points::{PatrolPointReport, PatrolPoints}, route_motion::{follow_route, route_points, turn_toward, RouteClearance}, @@ -49,6 +50,7 @@ struct PatrollingUnit { attack: Option<ObjectHandle>, attack_target: Option<(ObjectHandle, usize)>, raid_factory: Option<(ObjectHandle, usize)>, + ground_support: Option<Option<GroundSupport>>, } pub(super) struct PreparedPatrol { @@ -208,6 +210,7 @@ impl UnitPatrol { attack: None, attack_target: None, raid_factory: None, + ground_support: None, }); true } @@ -392,6 +395,7 @@ impl UnitPatrol { attack: None, attack_target: None, raid_factory: None, + ground_support: None, }) .collect(); Some(Self { @@ -554,6 +558,14 @@ impl UnitPatrol { let flight = (can_fly == Some(true) && radius > 0.0).then_some(FlightClearance { body_radius: radius, }); + let ground = if can_fly == Some(false) { + *unit.ground_support.get_or_insert_with(|| { + GroundSupport::prepare(&mission.mission_assets, original_id as usize) + }) + } else { + None + }; + let height_at = |xy| terrain.height_at(xy).map_err(EngineError::Terrain); if let Some(enemy) = enemy.filter(|enemy| enemy.in_range) { let mut after = before; let (yaw, _) = turn_toward( @@ -570,6 +582,9 @@ impl UnitPatrol { after.position[2] = flight.raised(from[2], floor, 9.0, seconds).0.to_bits(); } } + if let Some(ground) = ground { + ground.reanchor(before, &mut after, height_at)?; + } if after != before { poses.push((unit.handle, before, after)); } @@ -608,6 +623,13 @@ impl UnitPatrol { unit.retry_ms = now_ms.saturating_add(1_000); } if unit.points.is_empty() && flight.is_none() { + if let Some(ground) = ground { + let mut after = before; + ground.reanchor(before, &mut after, height_at)?; + if after != before { + poses.push((unit.handle, before, after)); + } + } continue; } let speed = if (place_patrol && enemy.is_none()) || unit.destinations.is_some() { @@ -641,8 +663,9 @@ impl UnitPatrol { buildings: &buildings, traffic: &neighbors, flight, + ground, }, - |xy| terrain.height_at(xy).map_err(EngineError::Terrain), + height_at, )?; traffic.reserve(unit.handle, swept); if after != before { |
