diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 15 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_gait.rs | 24 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_motion.rs | 49 |
3 files changed, 71 insertions, 17 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 11bac68d..23e18cae 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -155,7 +155,7 @@ pub use mission_events::{ capture_building, capture_building_with_notice, capture_neutral_building, capture_neutral_warbot, MissionEventStop, MissionEventTick, MissionNotice, }; -pub use player_gait::player_walk_camera_lift; +pub use player_gait::{player_walk_camera_lift, PlayerFootstep}; pub use player_motion::{ plan_player_motion, plan_player_motion_with_collision, PlayerMotion, PlayerMotionStep, }; @@ -1394,6 +1394,8 @@ pub struct FrameResult { pub clan_notices: Vec<MissionNotice>, /// Vertical aim of the controlled hero as an IEEE-754 radian value. pub player_pitch_bits: u32, + /// Footstep from the accepted player motion, if this tick reached a footfall. + pub player_footstep: Option<PlayerFootstep>, /// Autonomous combat events during the step. pub autodemo_combat_events: Vec<AutoDemoCombatEvent>, /// Autonomous impact or flight operations that could not execute. @@ -2984,6 +2986,7 @@ pub fn step_headless( produced_unit, production_failure, researched_component, + player_footstep: None, player_pitch_bits: engine .loaded .as_ref() @@ -3029,6 +3032,16 @@ pub fn frame_with_player_motion( if let Some(mission) = &mut engine.loaded { mission.player_pitch_bits = motion.pitch_bits; frame.player_pitch_bits = motion.pitch_bits; + if !blocked + && !drowned + && mission.player_gait.footfall + && mission.local_hero == Some(cockpit.handle) + { + frame.player_footstep = Some(PlayerFootstep { + position_bits: pose.position, + building_floor: motion.on_building_floor(), + }); + } } Ok(frame) } diff --git a/crates/fparkan-runtime/src/player_gait.rs b/crates/fparkan-runtime/src/player_gait.rs index 3b293d9a..fc564f63 100644 --- a/crates/fparkan-runtime/src/player_gait.rs +++ b/crates/fparkan-runtime/src/player_gait.rs @@ -1,4 +1,4 @@ -//! First-person motion of the walking hero's camera. +//! Camera motion and footfalls of the walking hero. use crate::{Engine, LoadedMissionState, StartingCockpit}; use fparkan_world::{transform_state, World}; @@ -11,17 +11,19 @@ pub(super) struct PlayerGait { previous: Option<(u32, [u32; 2])>, phase: f32, lift: f32, + pub(super) footfall: bool, } impl PlayerGait { fn advance(&mut self, clock_ms: u32, xy: [u32; 2]) { + self.footfall = false; let Some((previous_ms, previous_xy)) = self.previous.replace((clock_ms, xy)) else { return; }; let elapsed_ms = clock_ms.wrapping_sub(previous_ms).min(500) as f32; if previous_xy == xy { - let decay = (1.0 - elapsed_ms / (WALK_CYCLE_MS / 4.0)).clamp(0.0, 1.0); - self.lift *= decay; + let decay = CAMERA_LIFT * elapsed_ms / (WALK_CYCLE_MS / 4.0); + self.lift = self.lift.signum() * (self.lift.abs() - decay).max(0.0); if self.lift == 0.0 { self.phase = 0.0; } @@ -29,11 +31,22 @@ impl PlayerGait { } // shortcut: a sine matches the observed main component of the walk; // the native camera track and its response to movement speed are open. - self.phase = (self.phase + elapsed_ms / WALK_CYCLE_MS).fract(); + let next = self.phase + elapsed_ms / WALK_CYCLE_MS; + self.footfall = (next - 0.75).floor() > (self.phase - 0.75).floor(); + self.phase = next.fract(); self.lift = CAMERA_LIFT * (self.phase * std::f32::consts::TAU).sin(); } } +/// A foot contacting the accepted surface during the latest walking step. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct PlayerFootstep { + /// Position of the walker at ground contact, as IEEE-754 words. + pub position_bits: [u32; 3], + /// Whether building geometry supports the walker. + pub building_floor: bool, +} + pub(super) fn advance(mission: &mut LoadedMissionState, world: &World, now: u32) { let pose = mission .local_hero @@ -68,10 +81,13 @@ mod tests { assert_eq!(gait.lift, 0.0); gait.advance(224, [1, 0]); assert_eq!(gait.lift, CAMERA_LIFT); + assert!(!gait.footfall); gait.advance(448, [3, 0]); assert_eq!(gait.lift, -CAMERA_LIFT); + assert!(gait.footfall); gait.advance(504, [3, 0]); assert_eq!(gait.lift, -CAMERA_LIFT * 0.5); + assert!(!gait.footfall); gait.advance(616, [3, 0]); assert_eq!(gait.lift, 0.0); } diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index 89f38412..b21efd5b 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -54,11 +54,28 @@ pub struct PlayerMotionStep { max_floor_clearance: f32, previous_terrain_height: Option<f32>, terrain_height: Option<f32>, - wet_floor: bool, + surface: WalkingSurface, /// Sweep segment when the step translates the hero. pub sweep: Option<[[f32; 3]; 2]>, } +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +enum WalkingSurface { + Ground, + Building, + Water, +} + +impl WalkingSurface { + fn terrain(mask: FullSurfaceMask) -> Self { + if mask.0 & WATER_SURFACE != 0 { + Self::Water + } else { + Self::Ground + } + } +} + impl PlayerMotionStep { /// Uses the scene's terrain after foundation cuts and fills. #[must_use] @@ -69,7 +86,9 @@ impl PlayerMotionStep { ) -> Self { self.previous_terrain_height = previous; self.terrain_height = next.map(|(height, _)| height); - self.wet_floor = next.is_some_and(|(_, mask)| mask.0 & WATER_SURFACE != 0); + self.surface = next.map_or(WalkingSurface::Ground, |(_, mask)| { + WalkingSurface::terrain(mask) + }); if let Some([from, to]) = self.sweep.as_mut() { to[2] = previous .zip(self.terrain_height) @@ -128,7 +147,7 @@ impl PlayerMotionStep { }); self.pose.position[2] = (height + clearance).to_bits(); sweep[1][2] = height + clearance + 0.5; - self.wet_floor = false; + self.surface = WalkingSurface::Building; self } @@ -136,18 +155,22 @@ impl PlayerMotionStep { #[must_use] pub fn with_terrain_floor(self, height: f32, previous_mesh: Option<f32>) -> Self { Self { - wet_floor: self.wet_floor, + surface: self.surface, ..self.with_building_floor(height, previous_mesh) } } pub(super) fn touches_water(self) -> bool { - self.wet_floor + self.surface == WalkingSurface::Water && self.terrain_height.is_some_and(|height| { f32::from_bits(self.pose.position[2]) <= height + self.max_floor_clearance + 0.05 }) } + pub(super) fn on_building_floor(self) -> bool { + self.surface == WalkingSurface::Building + } + /// Sweeps vertically before crossing a step up, and horizontally before a step down. #[must_use] pub fn floor_sweeps(self) -> Option<[[[f32; 3]; 2]; 2]> { @@ -177,7 +200,7 @@ mod tests { max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: Some(11.5), - wet_floor: false, + surface: super::WalkingSurface::Ground, sweep: Some([[3.0, 6.0, 12.5], [4.0, 6.0, 15.5]]), }; assert_eq!( @@ -212,7 +235,7 @@ mod tests { max_floor_clearance: 1.25, previous_terrain_height: Some(10.0), terrain_height: Some(10.0), - wet_floor: false, + surface: super::WalkingSurface::Ground, sweep: Some([[3.0, 6.0, 11.75], [4.0, 6.0, 11.75]]), }; assert_eq!( @@ -234,7 +257,7 @@ mod tests { max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: None, - wet_floor: false, + surface: super::WalkingSurface::Ground, sweep: Some([[3.0, 6.0, 10.52], [4.0, 6.0, 10.52]]), } .with_scene_terrain( @@ -403,6 +426,7 @@ fn plan_player_steps( input.delta_seconds /= count as f32; let mut step = plan_motion(&mission.terrain, pose, pitch, flying, input)?; let mut previous = pose; + let mut previous_surface = WalkingSurface::Ground; input.mouse_x = 0.0; input.mouse_y = 0.0; for index in 0..count { @@ -421,13 +445,14 @@ fn plan_player_steps( if blocked { step.pose.position = previous.position; step.sweep = None; - step.wet_floor = false; + step.surface = previous_surface; break; } if drowns(engine, cockpit, step) { break; } previous = step.pose; + previous_surface = step.surface; } Ok(Some(step)) } @@ -491,7 +516,7 @@ fn plan_motion( let mut sweep = None; let mut previous_terrain_height = None; let mut terrain_height = None; - let mut wet_floor = false; + let mut surface = WalkingSurface::Ground; if flying && (xy != [from[0], from[1]] || climb != 0.0) { let old_xy = [from[0], from[1]]; let floor = |xy| { @@ -530,7 +555,7 @@ fn plan_motion( let to = [xy[0], xy[1], new_ground + (from[2] - old_ground)]; previous_terrain_height = Some(old_ground); terrain_height = Some(new_ground); - wet_floor = mask.0 & WATER_SURFACE != 0; + surface = WalkingSurface::terrain(mask); pose.position = to.map(f32::to_bits); // shortcut: one sweep at body height until the physical collider // and floor transitions are recovered. @@ -550,7 +575,7 @@ fn plan_motion( max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height, terrain_height, - wet_floor, + surface, sweep, }) } |
