From f3d1b4d2aff8e47003626129641c36e44c7fb040 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Thu, 26 Nov 2026 20:20:38 +0400 Subject: fix(movement): limit hero ascent using the Control slope profile Use the normal of the same triangle that supplies terrain support, apply the walking profile's speed envelope, and resolve support again for the shortened step. The existing immediate-speed model still omits native braking inertia. The native Control call confirms the envelope and the downhill bypass. In the stock Firing Line window the hero stopped at the west slope and could walk back. The bridge and factory walkway corpus checks passed. --- crates/fparkan-runtime/src/lib.rs | 1 + crates/fparkan-runtime/src/player_motion.rs | 129 ++++++++++++++++++---------- crates/fparkan-runtime/src/walking_slope.rs | 45 ++++++++++ crates/fparkan-terrain/src/lib.rs | 47 +++++++++- 4 files changed, 173 insertions(+), 49 deletions(-) create mode 100644 crates/fparkan-runtime/src/walking_slope.rs (limited to 'crates') diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 02333bd0..3afb3717 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -106,6 +106,7 @@ mod patrol_points; mod player_gait; mod player_motion; mod unit_speed; +mod walking_slope; pub use patrol_points::PatrolPointReport; pub use unit_speed::{UnitSpeedKind, UnitSpeedReport}; mod factory_release; diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index 18bd2d68..18164ec0 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -1,8 +1,8 @@ use super::{transform_state, Engine, EngineError, StartingCockpit, SurfaceQuery, TransformState}; use crate::manual_flight::{FlightDrive, FlightProfile}; -use crate::TerrainWorld; +use crate::{walking_slope::WalkingSlope, TerrainWorld}; use fparkan_resource::CachedResourceRepository; -use fparkan_terrain::FullSurfaceMask; +use fparkan_terrain::{FullSurfaceMask, TerrainSupport}; pub(super) fn warbot_can_fly(engine: &crate::Engine, object_index: usize) -> bool { let profile = || { @@ -68,6 +68,7 @@ pub struct PlayerMotionStep { max_floor_clearance: f32, previous_terrain_height: Option, terrain_height: Option, + terrain_normal: Option<[f32; 3]>, surface: WalkingSurface, /// Sweep segment when the step translates the hero. pub sweep: Option<[[f32; 3]; 2]>, @@ -96,12 +97,13 @@ impl PlayerMotionStep { pub fn with_scene_terrain( mut self, previous: Option, - next: Option<(f32, FullSurfaceMask)>, + next: Option, ) -> Self { self.previous_terrain_height = previous; - self.terrain_height = next.map(|(height, _)| height); - self.surface = next.map_or(WalkingSurface::Ground, |(_, mask)| { - WalkingSurface::terrain(mask) + self.terrain_height = next.map(|plane| plane.height); + self.terrain_normal = next.map(|plane| plane.normal); + self.surface = next.map_or(WalkingSurface::Ground, |plane| { + WalkingSurface::terrain(plane.mask) }); if let Some([from, to]) = self.sweep.as_mut() { to[2] = previous @@ -174,6 +176,15 @@ impl PlayerMotionStep { } } + fn slope_speed_factor(self, limit: Option) -> f32 { + if let (WalkingSurface::Ground, Some(limit), Some(normal), Some([from, to])) = + (self.surface, limit, self.terrain_normal, self.sweep) + { + return limit.speed_factor(normal, [to[0] - from[0], to[1] - from[1]]); + } + 1.0 + } + pub(super) fn touches_water(self) -> bool { self.surface == WalkingSurface::Water && self.terrain_height.is_some_and(|height| { @@ -214,6 +225,7 @@ mod tests { max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: Some(11.5), + terrain_normal: Some([0.0, -0.8, 0.6]), surface: super::WalkingSurface::Ground, sweep: Some([[3.0, 6.0, 12.5], [4.0, 6.0, 15.5]]), }; @@ -223,6 +235,12 @@ mod tests { ); assert_eq!(step.terrain_exit_floor(), Some(11.5)); let lowered = step.with_building_floor(11.5, Some(12.0)); + let mut words = [0; 27]; + words[21] = 2; + words[23] = 0.6_f32.to_bits(); + let slope = super::WalkingSlope::from_profile(&words); + assert_eq!(step.slope_speed_factor(slope), 0.0); + assert_eq!(lowered.slope_speed_factor(slope), 1.0); assert_eq!(lowered.pose.position.map(f32::from_bits), [4.0, 6.0, 11.52]); assert_eq!(lowered.sweep.unwrap()[1], [4.0, 6.0, 12.02]); assert_eq!( @@ -249,6 +267,7 @@ mod tests { max_floor_clearance: 1.25, previous_terrain_height: Some(10.0), terrain_height: Some(10.0), + terrain_normal: None, surface: super::WalkingSurface::Ground, sweep: Some([[3.0, 6.0, 11.75], [4.0, 6.0, 11.75]]), }; @@ -271,12 +290,17 @@ mod tests { max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: None, + terrain_normal: None, surface: super::WalkingSurface::Ground, sweep: Some([[3.0, 6.0, 10.52], [4.0, 6.0, 10.52]]), } .with_scene_terrain( Some(10.0), - Some((10.0, super::FullSurfaceMask(super::WATER_SURFACE))), + Some(fparkan_terrain::TerrainSupport { + height: 10.0, + mask: super::FullSurfaceMask(super::WATER_SURFACE), + normal: [0.0, 0.0, 1.0], + }), ); assert!(step.touches_water()); assert!(!step.with_building_floor(10.0, None).touches_water()); @@ -388,7 +412,7 @@ fn plan_player_steps( engine: &Engine, cockpit: StartingCockpit, mut input: PlayerMotion, - resolve: Option>, + mut resolve: Option>, ) -> Result, EngineError> { let (Some(mission), Ok(pose)) = ( &engine.loaded, @@ -425,21 +449,26 @@ fn plan_player_steps( if !input.delta_seconds.is_finite() || input.delta_seconds < 0.0 { return Err(EngineError::Movement("invalid player step".into())); } - let speeds = if boarded.is_some() && !flying { - crate::manual_chassis::speed_limits(mission, cockpit.handle) - .unwrap_or([WALK_SPEED_FALLBACK; 2]) - } else if mission.local_hero == Some(cockpit.handle) { - let forward = mission + let hero_profile = if mission.local_hero == Some(cockpit.handle) { + mission .control_device_tables .iter() .find(|table| table.handle == cockpit.handle) .and_then(|table| table.owner_profile.as_ref()) - .map(|profile| f32::from_bits(profile.current_words[7])) + .map(|profile| &profile.current_words) + } else { + None + }; + let slope = hero_profile.and_then(WalkingSlope::from_profile); + let speeds = if boarded.is_some() && !flying { + crate::manual_chassis::speed_limits(mission, cockpit.handle) + .unwrap_or([WALK_SPEED_FALLBACK; 2]) + } else { + let forward = hero_profile + .map(|words| f32::from_bits(words[7])) .filter(|speed| speed.is_finite() && *speed >= 0.0) .unwrap_or(WALK_SPEED_FALLBACK); [WALK_SPEED_FALLBACK, forward] - } else { - [WALK_SPEED_FALLBACK; 2] }; // shortcut: use the outdoor spawn clearance until the native walking collider is wired. let mut max_floor_clearance = FLOOR_CLEARANCE; @@ -448,13 +477,7 @@ fn plan_player_steps( max_floor_clearance = spawn_floor_clearance(&mission.terrain, draft.position)?; } } - let Some(resolve) = resolve else { - let mut step = plan_motion(&mission.terrain, pose, pitch, flight, speeds, input)?; - step.max_floor_clearance = max_floor_clearance; - crate::manual_chassis::support_step(chassis_ground, &mission.terrain, pose, &mut step)?; - return Ok(Some(step)); - }; - let count = if !flying && input.movement != [0.0; 2] { + let count = if resolve.is_some() && !flying && input.movement != [0.0; 2] { // shortcut: bound movement after a long stall while the scene uses the render clock. input.delta_seconds = input.delta_seconds.min(0.5); let [across, along] = body_velocity(input.movement, speeds); @@ -464,40 +487,49 @@ fn plan_player_steps( } .max(1); input.delta_seconds /= count as f32; - let mut step = plan_motion(&mission.terrain, pose, pitch, flight, speeds, input)?; let mut previous = pose; let mut previous_surface = WalkingSurface::Ground; - input.mouse_x = 0.0; - input.mouse_y = 0.0; + let mut pitch = pitch; for index in 0..count { - if index != 0 { - step = plan_motion( - &mission.terrain, - previous, - f32::from_bits(step.pitch_bits), - flight, - speeds, - input, - )?; + let mut propose = |input| { + let mut step = plan_motion(&mission.terrain, previous, pitch, flight, speeds, input)?; + step.max_floor_clearance = max_floor_clearance; + match resolve.as_mut() { + Some(resolve) => resolve(step), + None => Ok((step, false)), + } + }; + let (mut step, mut blocked) = propose(input)?; + if !blocked { + let factor = step.slope_speed_factor(slope); + if factor < 1.0 { + // The existing hero step applies the speed immediately. Use the + // native speed envelope and resample the shortened step's support. + (step, blocked) = propose(PlayerMotion { + delta_seconds: input.delta_seconds * factor, + ..input + })?; + blocked |= factor == 0.0; + } } - step.max_floor_clearance = max_floor_clearance; - let (resolved, blocked) = resolve(step)?; - step = resolved; if blocked { step.pose.position = previous.position; step.sweep = None; step.surface = previous_surface; step.flight_velocity = step.flight_velocity.map(|_| [0.0; 3]); - break; + return Ok(Some(step)); } crate::manual_chassis::support_step(chassis_ground, &mission.terrain, previous, &mut step)?; - if drowns(engine, cockpit, step) { - break; + if index + 1 == count || drowns(engine, cockpit, step) { + return Ok(Some(step)); } + input.mouse_x = 0.0; + input.mouse_y = 0.0; + pitch = f32::from_bits(step.pitch_bits); previous = step.pose; previous_surface = step.surface; } - Ok(Some(step)) + unreachable!("a movement sample always has at least one step") } pub(super) fn commit_flight( @@ -562,6 +594,7 @@ fn plan_motion( let mut flight_velocity = None; let mut previous_terrain_height = None; let mut terrain_height = None; + let mut terrain_normal = None; let mut surface = WalkingSurface::Ground; if let Some(flight) = flight { let [across, along] = input.movement; @@ -595,18 +628,19 @@ fn plan_motion( )); } if xy != [from[0], from[1]] { - if let (Some(old_ground), Some((new_ground, mask))) = ( + if let (Some(old_ground), Some(plane)) = ( terrain .height_at([from[0], from[1]]) .map_err(|err| EngineError::Movement(err.to_string()))?, terrain - .support_at(xy) + .support_plane_at(xy) .map_err(|err| EngineError::Movement(err.to_string()))?, ) { - let to = [xy[0], xy[1], new_ground + (from[2] - old_ground)]; + let to = [xy[0], xy[1], plane.height + (from[2] - old_ground)]; previous_terrain_height = Some(old_ground); - terrain_height = Some(new_ground); - surface = WalkingSurface::terrain(mask); + terrain_height = Some(plane.height); + terrain_normal = Some(plane.normal); + surface = WalkingSurface::terrain(plane.mask); pose.position = to.map(f32::to_bits); // shortcut: one sweep at body height until the physical collider // and floor transitions are recovered. @@ -627,6 +661,7 @@ fn plan_motion( max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height, terrain_height, + terrain_normal, surface, sweep, }) diff --git a/crates/fparkan-runtime/src/walking_slope.rs b/crates/fparkan-runtime/src/walking_slope.rs new file mode 100644 index 00000000..a6604c51 --- /dev/null +++ b/crates/fparkan-runtime/src/walking_slope.rs @@ -0,0 +1,45 @@ +//! Ground speed envelope for the walking mode of Control. + +#[derive(Clone, Copy)] +pub(super) struct WalkingSlope { + minimum_up: f32, +} + +impl WalkingSlope { + pub(super) fn from_profile(words: &[u32; 27]) -> Option { + let angle = f32::from_bits(words[23]); + let minimum_up = angle.cos(); + (words[21] == 2 && angle > 0.0 && angle <= std::f32::consts::FRAC_PI_2 && minimum_up < 1.0) + .then_some(Self { minimum_up }) + } + + pub(super) fn speed_factor(self, normal: [f32; 3], direction: [f32; 2]) -> f32 { + // Control+0x153c0 applies the limit on an ascent and along the contour. + let along_normal = direction[0] * normal[0] + direction[1] * normal[1]; + if along_normal > 0.0 { + return 1.0; + } + (2.0 * (normal[2] - self.minimum_up) / (1.0 - self.minimum_up)).clamp(0.0, 1.0) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hero_profile_limits_steep_ascent_and_contour() { + let mut words = [0; 27]; + words[21] = 2; + words[23] = 0.6_f32.to_bits(); + let slope = WalkingSlope::from_profile(&words).unwrap(); + let up_normal = |angle: f32| [0.0, -angle.sin(), angle.cos()]; + + assert_eq!(slope.speed_factor(up_normal(0.3), [0.0, 1.0]), 1.0); + let uphill = slope.speed_factor(up_normal(0.5), [0.0, 1.0]); + assert!((14.0 * uphill - 8.375_571).abs() < 0.000_01); + assert_eq!(slope.speed_factor(up_normal(0.7), [0.0, 1.0]), 0.0); + assert_eq!(slope.speed_factor(up_normal(0.7), [1.0, 0.0]), 0.0); + assert_eq!(slope.speed_factor(up_normal(0.7), [0.0, -1.0]), 1.0); + } +} diff --git a/crates/fparkan-terrain/src/lib.rs b/crates/fparkan-terrain/src/lib.rs index 022b937d..fd4a28a2 100644 --- a/crates/fparkan-terrain/src/lib.rs +++ b/crates/fparkan-terrain/src/lib.rs @@ -49,6 +49,17 @@ pub struct SurfaceHit { pub face: usize, } +/// The top terrain triangle supporting an object at an XY position. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct TerrainSupport { + /// Elevation of the supporting triangle at that position. + pub height: f32, + /// Flags of that triangle. + pub mask: FullSurfaceMask, + /// Upward unit normal of the triangle. + pub normal: [f32; 3], +} + /// Areal id. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct ArealId(pub u32); @@ -558,6 +569,29 @@ impl TerrainWorld { &self, position: [f32; 2], ) -> Result, TerrainError> { + Ok(self + .support_triangle_at(position)? + .map(|(height, triangle)| (height, triangle.mask))) + } + + /// Returns the top surface with the geometric normal of the same triangle. + pub fn support_plane_at( + &self, + position: [f32; 2], + ) -> Result, TerrainError> { + Ok(self + .support_triangle_at(position)? + .map(|(height, triangle)| TerrainSupport { + height, + mask: triangle.mask, + normal: triangle.upward_normal(), + })) + } + + fn support_triangle_at( + &self, + position: [f32; 2], + ) -> Result, TerrainError> { if self.surfaces.is_empty() { return Err(TerrainError::Unsupported); } @@ -566,8 +600,8 @@ impl TerrainWorld { let triangle = &self.surfaces[index]; if let Some(height) = triangle.height_at(position) { best = Some(match best { - Some((current, mask)) if current >= height => (height.max(current), mask), - _ => (height, triangle.mask), + Some((current, face)) if current >= height => (height.max(current), face), + _ => (height, triangle), }); } } @@ -870,6 +904,15 @@ fn ray_intersects_surface_bounds( } impl RuntimeTriangle { + fn upward_normal(&self) -> [f32; 3] { + let a = sub3_f64(self.vertices[1], self.vertices[0]); + let b = sub3_f64(self.vertices[2], self.vertices[0]); + let normal = cross3_f64(a, b); + // Height queries already exclude triangles with a degenerate XY projection. + let length = dot3_f64(normal, normal).sqrt().copysign(normal[2]); + normal.map(|axis| (axis / length) as f32) + } + fn height_at(&self, position: [f32; 2]) -> Option { let a = [self.vertices[0][0], self.vertices[0][1]]; let b = [self.vertices[1][0], self.vertices[1][1]]; -- cgit v1.2.3