Files
horizon-airdef/crates/horizon-client/src/sprint.rs
T
Cursor Agent 260dd423c9 Keep sprint as a state so a sprint-jump keeps its forward impulse
PlayerInput was rewriting the server sprint flag from the Ctrl and forward
keys. That flag is synced back onto the client, and jumpFromGround adds its
0.2 look-direction impulse only while the flag is set, so releasing Ctrl
before the jump (or double-tapping forward) produced a plain hop. Sprint
still starts and stops through PlayerCommand.

Co-Authored-By: Grok <noreply@x.ai>

Co-authored-by: 一水合氨NH3-H2O <mc070309@users.noreply.github.com>
2026-10-25 14:30:42 +00:00

210 lines
6.2 KiB
Rust

//! LocalPlayer sprint decisions around KeyboardInput.tick. Conditions are the
//! current player queries; setting sprint dispatches to the live attribute state.
use crate::input;
#[derive(Clone, Copy, Debug, Default)]
pub struct Keys {
pub forward: bool,
pub backward: bool,
pub left: bool,
pub right: bool,
pub jump: bool,
pub shift: bool,
pub sprint: bool,
/// A controller stick's move vector (`[left, forward]`, Horizon's own),
/// used instead of the keys' direction while it is pushed.
pub analog: Option<[f32; 2]>,
}
impl Keys {
pub fn direction(self) -> [f32; 2] {
match self.analog {
Some(v) => v,
None => input::keyboard_direction(self.forward, self.backward, self.left, self.right),
}
}
/// Add a stick's move vector: the direction flags code reading keys
/// (sprint, vehicles, the server's input packet) see are set past a
/// third of the travel.
pub fn with_stick(mut self, stick: Option<[f32; 2]>) -> Self {
if let Some([left, forward]) = stick {
self.forward |= forward > 0.3;
self.backward |= forward < -0.3;
self.left |= left > 0.3;
self.right |= left < -0.3;
self.analog = Some([left, forward]);
}
self
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SprintState {
pub trigger_time: i32,
pub previous_forward: bool,
pub previous_shift: bool,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Conditions {
pub sprinting: bool,
pub mobility_restricted: bool,
pub passenger: bool,
pub vehicle_can_sprint: bool,
pub vehicle_local: bool,
pub enough_food: bool,
pub may_fly: bool,
pub flying: bool,
pub shallow_water: bool,
pub slow_item: bool,
pub fall_flying: bool,
pub under_water: bool,
pub moving_slowly: bool,
pub swimming: bool,
pub in_water: bool,
pub on_ground: bool,
pub horizontal_collision: bool,
pub minor_horizontal_collision: bool,
}
impl Conditions {
fn possible(self, allow_shallow_water: bool) -> bool {
!self.mobility_restricted
&& if self.passenger {
self.vehicle_can_sprint && self.vehicle_local
} else {
self.enough_food || self.may_fly
}
&& (allow_shallow_water || !self.shallow_water)
}
fn can_start(self, forward: bool) -> bool {
!self.sprinting
&& forward
&& self.possible(self.flying)
&& !self.slow_item
&& (!self.fall_flying || self.under_water)
&& (!self.moving_slowly || self.under_water)
}
fn should_stop(self, forward: bool, shift: bool) -> bool {
if self.swimming {
!self.possible(true) || !self.in_water || (!forward && !self.on_ground && !shift)
} else {
!self.possible(self.flying)
|| !forward
|| (self.horizontal_collision && !self.minor_horizontal_collision)
}
}
}
impl SprintState {
/// Runs the sprint phase. The default vanilla sprint window is seven ticks;
/// it is supplied so the options binding can also select 0..10. The setter
/// is called for every native request, including repeated true requests.
pub fn tick<E>(
&mut self,
keys: Keys,
conditions: Conditions,
window: i32,
mut set_sprinting: impl FnMut(bool) -> Result<(), E>,
) -> Result<(), E> {
if self.trigger_time > 0 {
self.trigger_time -= 1;
}
let old_forward = self.previous_forward;
let old_shift = self.previous_shift;
let forward = keys.direction()[1] > 1.0e-5;
self.previous_forward = forward;
self.previous_shift = keys.shift;
if old_shift || (conditions.slow_item && !conditions.passenger) || keys.backward {
self.trigger_time = 0;
}
let mut sprinting = conditions.sprinting;
if conditions.can_start(forward) {
if !old_forward {
if self.trigger_time > 0 {
set_sprinting(true)?;
sprinting = true;
} else {
self.trigger_time = window;
}
}
if keys.sprint {
set_sprinting(true)?;
sprinting = true;
}
}
if sprinting && conditions.should_stop(forward, keys.shift) {
set_sprinting(false)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn standing() -> Conditions {
Conditions {
enough_food: true,
on_ground: true,
..Conditions::default()
}
}
fn step(state: &mut SprintState, keys: Keys, sprinting: &mut bool) {
let current = *sprinting;
let mut next = current;
state
.tick(
keys,
Conditions {
sprinting: current,
..standing()
},
7,
|value| {
next = value;
Ok::<(), ()>(())
},
)
.unwrap();
*sprinting = next;
}
/// Vanilla sprint is a state. Releasing the sprint key, including on the
/// jump tick, keeps it while forward is held; releasing forward stops it.
#[test]
fn sprint_survives_releasing_the_key_through_a_jump() {
let mut state = SprintState::default();
let mut sprinting = false;
step(
&mut state,
Keys {
forward: true,
sprint: true,
..Keys::default()
},
&mut sprinting,
);
assert!(sprinting);
step(
&mut state,
Keys {
forward: true,
jump: true,
..Keys::default()
},
&mut sprinting,
);
assert!(
sprinting,
"a jump after the sprint key comes up must stay a sprint"
);
step(
&mut state,
Keys {
jump: true,
..Keys::default()
},
&mut sprinting,
);
assert!(!sprinting);
}
}