diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 19:52:30 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 19:52:30 +0300 |
| commit | 23ec106748d4641a9dfd809b56029391daddf6ba (patch) | |
| tree | 64a5c65367d889e8bac9eac352bdc5d916a295ca /crates/fparkan-runtime/src | |
| parent | 1f366ac0d39a3b1f0b19721ed78936bad1e504dd (diff) | |
| download | fparkan-23ec106748d4641a9dfd809b56029391daddf6ba.tar.xz fparkan-23ec106748d4641a9dfd809b56029391daddf6ba.zip | |
fix(input): match the original hero mouse turn scale
Use the stock relative X binding and control settings, filter once per input sample, and carry the filter across collision steps. World3D dispatch confirmed both directions, saturation and angle wrapping. Mission 2 was checked from a stock spawn in a Vulkan window with two view captures. The runtime still applies the requested rotation in one step.
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/boarding.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/hero_look.rs | 120 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 5 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_motion.rs | 41 |
4 files changed, 159 insertions, 9 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs index afb2e50d..e7ad46d2 100644 --- a/crates/fparkan-runtime/src/boarding.rs +++ b/crates/fparkan-runtime/src/boarding.rs @@ -143,6 +143,7 @@ pub fn board_friendly_warbot( }); mission.player_pitch_bits = 0; mission.hero_walk_velocity = [0.0; 2]; + mission.hero_look.filtered_x = 0.0; } Ok(Some(cockpit)) } @@ -224,6 +225,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, mission.boarding = None; mission.player_pitch_bits = 0; mission.hero_walk_velocity = [0.0; 2]; + mission.hero_look.filtered_x = 0.0; Ok(Some(hero)) } diff --git a/crates/fparkan-runtime/src/hero_look.rs b/crates/fparkan-runtime/src/hero_look.rs new file mode 100644 index 00000000..0c96a65d --- /dev/null +++ b/crates/fparkan-runtime/src/hero_look.rs @@ -0,0 +1,120 @@ +use fparkan_path::{normalize_relative, PathPolicy}; +use fparkan_vfs::Vfs; + +#[derive(Clone, Copy, Debug)] +pub(super) struct HeroLook { + turns: f32, + sensitivity: f32, + pub(super) filtered_x: f32, +} + +impl HeroLook { + pub(super) fn load(vfs: &dyn Vfs) -> Self { + let read = |name: &[u8]| { + let path = normalize_relative(name, PathPolicy::StrictLegacy).ok()?; + vfs.read(&path).ok() + }; + let table = read(b"hero.tbl"); + let settings = read(b"Iron_3D.ini"); + Self { + turns: table + .as_deref() + .and_then(|bytes| horizontal_binding(&String::from_utf8_lossy(bytes))) + .unwrap_or(0.15), + sensitivity: settings + .as_deref() + .and_then(|bytes| mouse_sensitivity(&String::from_utf8_lossy(bytes))) + .unwrap_or(1.0), + filtered_x: 0.0, + } + } + + /// Returns the filter state and the requested change in world yaw. + pub(super) fn sample(self, dx: f32, dy: f32) -> (f32, f32) { + let filtered = if dx == 0.0 && dy == 0.0 { + 0.0 + } else { + dx.clamp(i32::MIN as f32, i32::MAX as f32) * self.sensitivity * 0.95 + + self.filtered_x * 0.05 + }; + let scaled = filtered * 0.006; + let axis = if scaled.abs() > 0.999 { + scaled.signum() + } else { + scaled + }; + (filtered, -(axis * self.turns) * std::f32::consts::TAU) + } +} + +fn horizontal_binding(table: &str) -> Option<f32> { + table.lines().find_map(|line| { + let line = line.split_once("//").map_or(line, |(binding, _)| binding); + let mut words = line.split_ascii_whitespace(); + for name in [ + "MOUSE", + "SCAN_NULL", + "SCAN_MOUSE_X", + "1", + "CICLS_UNKNOWN", + "MCMD_ANGLE_Z", + ] { + if !words.next()?.eq_ignore_ascii_case(name) { + return None; + } + } + let turns = words.next()?.parse::<f32>().ok()?; + (turns.is_finite() && words.nth(1)?.eq_ignore_ascii_case("MAN_WRAP")).then_some(turns) + }) +} + +fn mouse_sensitivity(settings: &str) -> Option<f32> { + let mut controls = false; + for line in settings.lines().map(str::trim) { + if let Some(section) = line.strip_prefix('[').and_then(|line| line.split_once(']')) { + controls = section.0.eq_ignore_ascii_case("CS"); + } + if controls { + if let Some((name, value)) = line.split_once('=') { + if name.trim().eq_ignore_ascii_case("MOUSE_SENS") { + return Some(value.trim().parse::<i32>().ok()? as f32 * 0.01); + } + } + } + } + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn horizontal_profile_reads_only_unmodified_body_turn() { + let bindings = + "+MOUSE SCAN_SHIFT SCAN_MOUSE_X 1 CICLS_UNKNOWN MCMD_ANGLE_Z 0.10 0 MAN_WRAP 0.0 0 +MOUSE SCAN_NULL SCAN_MOUSE_Y 1 CICLS_UNKNOWN MCMD_ANGLE_Z 0.25 0 MAN_WRAP 0.0 0 +MOUSE SCAN_NULL SCAN_MOUSE_X 1 CICLS_UNKNOWN MCMD_ANGLE_Z 0.20 0 MAN_WRAP 0.0 0 // body"; + assert_eq!(horizontal_binding(bindings), Some(0.20)); + let settings = "[VIDEO]\nMOUSE_SENS=100\n[cs]\n mouse_sens = 50"; + assert_eq!(mouse_sensitivity(settings), Some(0.5)); + } + + #[test] + fn hero_mouse_matches_world3d_dispatch() { + let mut look = HeroLook { + turns: 0.15, + sensitivity: 1.0, + filtered_x: 0.0, + }; + for (dx, expected) in [(100.0, 0.0855), (-100.0, -0.0855), (250.0, 0.15)] { + let (_, yaw) = look.sample(dx, 0.0); + assert!((yaw / -std::f32::consts::TAU - expected).abs() < 1e-6); + } + look.filtered_x = look.sample(100.0, 0.0).0; + let (tail, yaw) = look.sample(0.0, 1.0); + assert_eq!(tail, 4.75); + assert!((yaw / -std::f32::consts::TAU - 0.004275).abs() < 1e-6); + assert_eq!(look.sample(0.0, 0.0), (0.0, 0.0)); + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index c21a1c89..527f2f85 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -97,6 +97,7 @@ use behavior_clock::next_crt_rand; mod mesh_collision; mod mining; pub use laboratory::LaboratoryReport; +mod hero_look; mod hero_walk; mod main_teleport; mod manual_chassis; @@ -1476,6 +1477,7 @@ struct LoadedMissionState { player_pitch_bits: u32, player_gait: player_gait::PlayerGait, hero_walk_velocity: [f32; 2], + hero_look: hero_look::HeroLook, boarding: Option<boarding::Boarding>, local_hero: Option<ObjectHandle>, script_host_mode: MissionMode, @@ -2809,6 +2811,7 @@ node_provider_distribution_suspended: false, player_pitch_bits: 0, player_gait: Default::default(), hero_walk_velocity: [0.0; 2], + hero_look: hero_look::HeroLook::load(vfs.as_ref()), boarding: None, local_hero, script_host_mode: request.mode, @@ -3036,7 +3039,7 @@ 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; - player_motion::commit_velocity( + player_motion::commit_motion_state( mission, cockpit, motion, diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index 8ac5eb5a..8571b899 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -66,6 +66,7 @@ pub struct PlayerMotionStep { pub(super) pitch_bits: u32, flight_velocity: Option<[f32; 3]>, hero_walk_velocity: Option<[f32; 2]>, + hero_filtered_x: Option<f32>, max_floor_clearance: f32, previous_terrain_height: Option<f32>, terrain_height: Option<f32>, @@ -221,6 +222,7 @@ mod tests { pitch_bits: 0, flight_velocity: None, hero_walk_velocity: None, + hero_filtered_x: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: Some(11.5), @@ -260,6 +262,7 @@ mod tests { pitch_bits: 0, flight_velocity: None, hero_walk_velocity: None, + hero_filtered_x: None, max_floor_clearance: 1.25, previous_terrain_height: Some(10.0), terrain_height: Some(10.0), @@ -284,6 +287,7 @@ mod tests { pitch_bits: 0, flight_velocity: None, hero_walk_velocity: None, + hero_filtered_x: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: None, @@ -449,17 +453,29 @@ fn plan_player_steps( .and_then(|profile| WalkingDrive::from_profile(profile, mission.hero_walk_velocity)); let walking_motion = input.movement != [0.0; 2] || (walking.is_some() && mission.hero_walk_velocity != [0.0; 2]); + let hero_control = boarded.is_none() && mission.local_hero == Some(cockpit.handle); if !walking_motion && flight.is_none_or(|drive| input.climb == 0.0 && drive.velocity == [0.0; 3]) && input.mouse_x == 0.0 && input.mouse_y == 0.0 + && (!hero_control || mission.hero_look.filtered_x == 0.0) { return Ok(None); } let pitch = f32::from_bits(mission.player_pitch_bits); - if !input.delta_seconds.is_finite() || input.delta_seconds < 0.0 { + if ![input.delta_seconds, input.mouse_x, input.mouse_y] + .into_iter() + .all(f32::is_finite) + || input.delta_seconds < 0.0 + { return Err(EngineError::Movement("invalid player step".into())); } + let hero_look = hero_control.then(|| mission.hero_look.sample(input.mouse_x, input.mouse_y)); + // shortcut: mounted chassis still use a constant mouse gain. + let mut yaw_delta = hero_look.map_or_else( + || -input.mouse_x.clamp(-1_000.0, 1_000.0) * 0.003, + |(_, yaw)| yaw, + ); let speeds = if boarded.is_some() && !flying { crate::manual_chassis::speed_limits(mission, cockpit.handle) .unwrap_or([WALK_SPEED_FALLBACK; 2]) @@ -503,12 +519,14 @@ fn plan_player_steps( &mission.terrain, previous, pitch, + yaw_delta, flight, ground_displacement, input, )?; step.max_floor_clearance = max_floor_clearance; step.hero_walk_velocity = walk.map(|(velocity, _)| velocity); + step.hero_filtered_x = hero_look.map(|(filtered_x, _)| filtered_x); match resolve.as_mut() { Some(resolve) => resolve(step), None => Ok((step, false)), @@ -536,6 +554,7 @@ fn plan_player_steps( if index + 1 == count || drowns(engine, cockpit, step) { return Ok(Some(step)); } + yaw_delta = 0.0; input.mouse_x = 0.0; input.mouse_y = 0.0; pitch = f32::from_bits(step.pitch_bits); @@ -548,16 +567,21 @@ fn plan_player_steps( unreachable!("a movement sample always has at least one step") } -pub(super) fn commit_velocity( +pub(super) fn commit_motion_state( mission: &mut crate::LoadedMissionState, cockpit: StartingCockpit, step: PlayerMotionStep, blocked: bool, ) { - if let Some(velocity) = step.hero_walk_velocity { - if mission.local_hero == Some(cockpit.handle) { + if mission.local_hero == Some(cockpit.handle) { + if let Some(velocity) = step.hero_walk_velocity { mission.hero_walk_velocity = if blocked { [0.0; 2] } else { velocity }; } + if mission.boarding.is_none() { + if let Some(filtered_x) = step.hero_filtered_x { + mission.hero_look.filtered_x = filtered_x; + } + } } if let (Some(boarding), Some(velocity)) = (&mut mission.boarding, step.flight_velocity) { if boarding.cockpit.handle == cockpit.handle { @@ -579,6 +603,7 @@ fn plan_motion( terrain: &TerrainWorld, mut pose: TransformState, pitch: f32, + yaw_delta: f32, flight: Option<FlightDrive>, ground_displacement: [f32; 2], input: PlayerMotion, @@ -594,6 +619,7 @@ fn plan_motion( input.climb, input.delta_seconds, yaw, + yaw_delta, pitch, ]) .chain(from) @@ -602,10 +628,8 @@ fn plan_motion( { return Err(EngineError::Movement("invalid player step".into())); } - // shortcut: use constant mouse gain until native sensitivity is measured. - let yaw = if input.mouse_x != 0.0 { - let yaw = (yaw - input.mouse_x.clamp(-1_000.0, 1_000.0) * 0.003) - .rem_euclid(std::f32::consts::TAU); + let yaw = if yaw_delta != 0.0 { + let yaw = (yaw + yaw_delta).rem_euclid(std::f32::consts::TAU); pose.orientation[2] = yaw.to_bits(); yaw } else { @@ -678,6 +702,7 @@ fn plan_motion( pitch_bits: pitch.to_bits(), flight_velocity, hero_walk_velocity: None, + hero_filtered_x: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height, terrain_height, |
