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>
This commit is contained in:
@@ -469,7 +469,8 @@ impl Game<'_> {
|
||||
"flying": p.flying,
|
||||
"fall_flying": p.fall_flying,
|
||||
"sneaking": self.keys.contains(&KeyCode::ShiftLeft),
|
||||
"sprinting": self.keys.contains(&KeyCode::ControlLeft),
|
||||
// The sprint flag jumpFromGround reads, not whether Ctrl is down.
|
||||
"sprinting": p.sprinting,
|
||||
"health": p.health.health(),
|
||||
"absorption": p.health.absorption(),
|
||||
// The hurt animation's ticks left (the camera's hurt tilt).
|
||||
|
||||
@@ -1982,9 +1982,15 @@ impl Hub {
|
||||
g.center = None;
|
||||
}
|
||||
S::PlayerInput(p) => {
|
||||
// ServerGamePacketListenerImpl.handlePlayerInput records the
|
||||
// keys and updates crouching. Sprinting is not a held key:
|
||||
// PlayerCommand start/stop owns the flag, so releasing Ctrl
|
||||
// (or double-tapping forward, which never holds it) must not
|
||||
// clear it. The flag is synced back to the client, and
|
||||
// LivingEntity.jumpFromGround adds its 0.2 look-direction
|
||||
// impulse only while that flag is set.
|
||||
self.guests[i].input = p.flags;
|
||||
self.guests[i].sneaking = p.flags & 32 != 0;
|
||||
self.guests[i].sprinting = p.flags & 64 != 0 && p.flags & 1 != 0;
|
||||
}
|
||||
// OPEN_INVENTORY (the ridden mount's inventory) and
|
||||
// START_FALL_FLYING (checked against the guest's chest slot
|
||||
|
||||
@@ -134,3 +134,76 @@ impl SprintState {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,6 +278,67 @@ fn run<T: Send + 'static>(
|
||||
handle.join().unwrap().unwrap()
|
||||
}
|
||||
|
||||
/// PlayerCommand owns sprint. PlayerInput (the jump bit included) must not
|
||||
/// clear it, or the client's jumpFromGround skips the 0.2 forward impulse.
|
||||
#[test]
|
||||
fn sprint_commands_survive_keyboard_input_changes() {
|
||||
let Some(resources) = resources() else {
|
||||
return;
|
||||
};
|
||||
let mut s = server(&resources);
|
||||
let port = s.hub.lan.port;
|
||||
let _bot = run(&mut s, move || connect(port, "Sprinter", true));
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
while s.hub.guests.is_empty() {
|
||||
s.tick(&[]);
|
||||
assert!(Instant::now() < deadline, "guest did not join");
|
||||
thread::yield_now();
|
||||
}
|
||||
let id = s.hub.guests[0].entity_id;
|
||||
s.hub.guests[0].loaded = true;
|
||||
s.hub.guests[0]
|
||||
.inbox
|
||||
.push_back(S::PlayerCommand(play::PlayerCommand {
|
||||
id,
|
||||
action: 1,
|
||||
data: 0,
|
||||
}));
|
||||
s.tick(&[]);
|
||||
assert!(s.hub.guests[0].sprinting);
|
||||
// Forward with the sprint key (65), forward alone after Ctrl is released
|
||||
// (1), no keys, and forward plus jump (17): the sprint a jump needs.
|
||||
for flags in [65u8, 1, 0, 17, 33] {
|
||||
s.hub.guests[0]
|
||||
.inbox
|
||||
.push_back(S::PlayerInput(play::PlayerInput { flags }));
|
||||
s.tick(&[]);
|
||||
let g = &s.hub.guests[0];
|
||||
assert!(
|
||||
g.sprinting,
|
||||
"keyboard input {flags} cleared the sprint flag"
|
||||
);
|
||||
assert_eq!(g.input, flags);
|
||||
assert_eq!(g.sneaking, flags & 32 != 0);
|
||||
}
|
||||
s.hub.guests[0]
|
||||
.inbox
|
||||
.push_back(S::PlayerCommand(play::PlayerCommand {
|
||||
id,
|
||||
action: 2,
|
||||
data: 0,
|
||||
}));
|
||||
s.tick(&[]);
|
||||
assert!(!s.hub.guests[0].sprinting);
|
||||
s.hub.guests[0]
|
||||
.inbox
|
||||
.push_back(S::PlayerInput(play::PlayerInput { flags: 65 }));
|
||||
s.tick(&[]);
|
||||
assert!(
|
||||
!s.hub.guests[0].sprinting,
|
||||
"keyboard input started a sprint"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_and_join() {
|
||||
let Some(resources) = resources() else {
|
||||
|
||||
@@ -122,3 +122,119 @@ pub fn air_drag<W: LivingMotionWorld>(world: &mut W) -> Result<f32, W::Error> {
|
||||
};
|
||||
Ok(modified_friction(drag, world.air_drag_modifier()? as f32))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use horizon_core::geometry::{Vec3, trig};
|
||||
|
||||
struct Jumper {
|
||||
velocity: Vec3,
|
||||
sprinting: bool,
|
||||
yaw: f32,
|
||||
synced: bool,
|
||||
}
|
||||
impl LivingMotionWorld for Jumper {
|
||||
type Error = ();
|
||||
fn velocity(&self) -> Vec3 {
|
||||
self.velocity
|
||||
}
|
||||
fn set_velocity(&mut self, velocity: Vec3) -> Result<(), ()> {
|
||||
self.velocity = velocity;
|
||||
Ok(())
|
||||
}
|
||||
fn jump_strength(&mut self) -> Result<f64, ()> {
|
||||
Ok(0.42)
|
||||
}
|
||||
fn block_jump_factor(&mut self) -> Result<f32, ()> {
|
||||
Ok(1.0)
|
||||
}
|
||||
fn has_jump_boost(&mut self) -> Result<bool, ()> {
|
||||
Ok(false)
|
||||
}
|
||||
fn jump_boost_amplifier(&mut self) -> Result<i32, ()> {
|
||||
Ok(0)
|
||||
}
|
||||
fn sprinting(&mut self) -> Result<bool, ()> {
|
||||
Ok(self.sprinting)
|
||||
}
|
||||
fn yaw(&mut self) -> Result<f32, ()> {
|
||||
Ok(self.yaw)
|
||||
}
|
||||
fn mark_needs_sync(&mut self) {
|
||||
self.synced = true;
|
||||
}
|
||||
fn no_gravity(&mut self) -> Result<bool, ()> {
|
||||
Ok(false)
|
||||
}
|
||||
fn gravity_attribute(&mut self) -> Result<f64, ()> {
|
||||
Ok(0.08)
|
||||
}
|
||||
fn has_slow_falling(&mut self) -> Result<bool, ()> {
|
||||
Ok(false)
|
||||
}
|
||||
fn controlling_passenger_is_player(&mut self) -> Result<bool, ()> {
|
||||
Ok(false)
|
||||
}
|
||||
fn speed(&mut self) -> Result<f32, ()> {
|
||||
Ok(0.1)
|
||||
}
|
||||
fn omnidirectional_air_mover(&mut self) -> Result<bool, ()> {
|
||||
Ok(false)
|
||||
}
|
||||
fn air_drag_modifier(&mut self) -> Result<f64, ()> {
|
||||
Ok(0.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn impulse(yaw: f32) -> (f64, f64) {
|
||||
let angle = f64::from(yaw * trig::DEG_TO_RAD);
|
||||
(
|
||||
f64::from(-trig::sin(angle)) * 0.2,
|
||||
f64::from(trig::cos(angle)) * 0.2,
|
||||
)
|
||||
}
|
||||
|
||||
/// LivingEntity.jumpFromGround: a sprint adds ~0.2 along look; a walk does not.
|
||||
#[test]
|
||||
fn sprint_jump_adds_look_direction_impulse() {
|
||||
for yaw in [0.0, 45.0, 90.0, 180.0, 270.0, 300.0] {
|
||||
let before = Vec3::new(0.13, -0.08, -0.05);
|
||||
let mut sprinting = Jumper {
|
||||
velocity: before,
|
||||
sprinting: true,
|
||||
yaw,
|
||||
synced: false,
|
||||
};
|
||||
jump_from_ground(&mut sprinting).unwrap();
|
||||
let (dx, dz) = impulse(yaw);
|
||||
let horizontal = ((dx * dx) + (dz * dz)).sqrt();
|
||||
assert!(
|
||||
(horizontal - 0.2).abs() < 1.0e-4,
|
||||
"yaw {yaw}: impulse length {horizontal}"
|
||||
);
|
||||
assert!(
|
||||
(sprinting.velocity.x - (before.x + dx)).abs() < 1.0e-9,
|
||||
"yaw {yaw}: x {:?} expected {}",
|
||||
sprinting.velocity,
|
||||
before.x + dx
|
||||
);
|
||||
assert!((sprinting.velocity.z - (before.z + dz)).abs() < 1.0e-9);
|
||||
// jump_power narrows the 0.42 strength through float, as vanilla does.
|
||||
let jump_y = f64::from(0.42_f32);
|
||||
assert!((sprinting.velocity.y - jump_y).abs() < 1.0e-9);
|
||||
assert!(sprinting.synced);
|
||||
|
||||
let mut walking = Jumper {
|
||||
velocity: before,
|
||||
sprinting: false,
|
||||
yaw,
|
||||
synced: false,
|
||||
};
|
||||
jump_from_ground(&mut walking).unwrap();
|
||||
assert_eq!(walking.velocity.x, before.x);
|
||||
assert_eq!(walking.velocity.z, before.z);
|
||||
assert!((walking.velocity.y - jump_y).abs() < 1.0e-9);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user