Mobs: a punch shows smooth and slow, as on vanilla's client
The server sends a knocked-back, pushed or jumping mob's move at once (Entity.needsSync), and the remote client walks every living entity along the server's positions with vanilla's SteppedInterpolationHandler: each position takes the type's update interval to reach, sped up while steps pile up. Before, the client waited for the next due update and then lerped there in fewer ticks, so a hit pig jumped away fast. tools/test_stepped_interpolation_parity.py drives vanilla's unmodified InterpolationData and the Rust port with the same packets and ticks (random cases and a punched pig's knockback); 135236 positions and rotations match bit for bit. Fixes #1374 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2700,6 +2700,7 @@ impl Game<'_> {
|
||||
equipment: Vec::new(),
|
||||
attributes: Vec::new(),
|
||||
precise: false,
|
||||
needs_sync: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -760,6 +760,7 @@ impl Game<'_> {
|
||||
(e.yaw, e.pitch, e.head_yaw) = (m.yaw, m.head_pitch, m.head_yaw);
|
||||
e.on_ground = m.on_ground;
|
||||
e.velocity = [m.velocity.x, m.velocity.y, m.velocity.z];
|
||||
e.needs_sync = m.needs_sync;
|
||||
mob_meta(m, hub, &mut e.meta);
|
||||
mob_equipment(m, hub, &mut e.equipment);
|
||||
self.mob_attributes(m, &mut e.attributes);
|
||||
@@ -828,6 +829,10 @@ impl Game<'_> {
|
||||
} else {
|
||||
states.clear();
|
||||
}
|
||||
// ServerEntity.sendChanges clears needsSync once it has looked.
|
||||
for m in &mut self.mobs {
|
||||
m.needs_sync = false;
|
||||
}
|
||||
let entities = states.as_slice();
|
||||
let spawn = self.spawn.map_or([0.5, 80.0, 0.5], |s| [s.x, s.y, s.z]);
|
||||
let events = {
|
||||
|
||||
@@ -39,6 +39,7 @@ fn state(id: u32, kind: &str, position: [f64; 3], yaw: f32, on_ground: bool) ->
|
||||
equipment: Vec::new(),
|
||||
attributes: Vec::new(),
|
||||
precise: false,
|
||||
needs_sync: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,11 @@ use horizon_protocol::{
|
||||
registry::{Builtin, DimensionType, Dynamic, ceil_log2},
|
||||
text::{Language, format_java, plain},
|
||||
};
|
||||
use horizon_world::{nbt::Tag, registry::BlockStateId};
|
||||
use horizon_world::{
|
||||
nbt::Tag,
|
||||
registry::BlockStateId,
|
||||
stepped_interpolation::{Path, PosRot, Start, SteppedInterpolation},
|
||||
};
|
||||
use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
sync::Arc,
|
||||
@@ -85,6 +89,15 @@ struct Lerp {
|
||||
steps: u32,
|
||||
}
|
||||
|
||||
/// `SteppedInterpolationHandler.doInterpolate`'s `setPos` and `setRot` on a
|
||||
/// mob: its yaw kept continuous (the same turn as vanilla's, without the
|
||||
/// wrap that would spin it in the frame between ticks), its pitch clamped.
|
||||
fn place_interpolated(m: &mut Mob, at: PosRot) {
|
||||
m.position = Vec3::new(at.position[0], at.position[1], at.position[2]);
|
||||
m.yaw += wrap_degrees(at.yaw - m.yaw);
|
||||
m.head_pitch = (at.pitch % 360.0).clamp(-90.0, 90.0);
|
||||
}
|
||||
|
||||
pub(super) struct Remote {
|
||||
pub session: Session,
|
||||
pub brand: String,
|
||||
@@ -123,6 +136,8 @@ pub(super) struct Remote {
|
||||
use_target: Option<[i32; 3]>,
|
||||
entities: HashMap<i32, Tracked>,
|
||||
lerps: HashMap<i32, Lerp>,
|
||||
/// Living entities' `SteppedInterpolationHandler`s.
|
||||
stepped: HashMap<i32, SteppedInterpolation>,
|
||||
/// `LivingEntity.lerpHeadTo`: the head yaw and the ticks left to reach it.
|
||||
head_lerps: HashMap<i32, (f32, u32)>,
|
||||
/// Item entities waiting for their item data.
|
||||
@@ -380,6 +395,7 @@ impl Game<'_> {
|
||||
use_target: None,
|
||||
entities: HashMap::new(),
|
||||
lerps: HashMap::new(),
|
||||
stepped: HashMap::new(),
|
||||
head_lerps: HashMap::new(),
|
||||
pending_items: HashMap::new(),
|
||||
health: (20.0, 20, 5.0),
|
||||
@@ -931,6 +947,7 @@ impl Game<'_> {
|
||||
if let Some(r) = self.remote.as_mut() {
|
||||
r.entities.clear();
|
||||
r.lerps.clear();
|
||||
r.stepped.clear();
|
||||
r.head_lerps.clear();
|
||||
r.pending_items.clear();
|
||||
// A new ClientLevel: a new chunk storage, centred on 0, 0.
|
||||
@@ -1322,6 +1339,9 @@ impl Game<'_> {
|
||||
mob.persistent = true;
|
||||
self.mobs.retain(|m| m.id != id as u32);
|
||||
self.mobs.push(mob);
|
||||
if let Some(r) = self.remote.as_mut() {
|
||||
r.stepped.remove(&id);
|
||||
}
|
||||
}
|
||||
if let Some(r) = self.remote.as_mut() {
|
||||
if kind == "minecraft:item" {
|
||||
@@ -1336,23 +1356,77 @@ impl Game<'_> {
|
||||
);
|
||||
}
|
||||
}
|
||||
/// A move packet for entity `id`: to the end of `path` (None: a
|
||||
/// rotation alone), turned to `rotation` if it has one. `far_snaps`:
|
||||
/// an `EntityPositionSync`, which puts an entity more than 64 blocks
|
||||
/// away there at once.
|
||||
fn move_entity(
|
||||
&mut self,
|
||||
id: i32,
|
||||
position: [f64; 3],
|
||||
path: Option<Path>,
|
||||
rotation: Option<(f32, f32)>,
|
||||
on_ground: bool,
|
||||
far_snaps: bool,
|
||||
) {
|
||||
let mob = self.mobs.iter_mut().find(|m| m.id == id as u32);
|
||||
let tracked = self
|
||||
.remote
|
||||
.as_ref()
|
||||
.and_then(|r| r.entities.get(&id))
|
||||
.map(|t| t.position);
|
||||
let position = path
|
||||
.as_ref()
|
||||
.map(Path::end)
|
||||
.or(tracked)
|
||||
.or_else(|| {
|
||||
mob.as_ref()
|
||||
.map(|m| [m.position.x, m.position.y, m.position.z])
|
||||
})
|
||||
.unwrap_or([0.0; 3]);
|
||||
if let Some(r) = self.remote.as_mut() {
|
||||
if let Some(t) = r.entities.get_mut(&id) {
|
||||
t.position = position;
|
||||
}
|
||||
if let Some(m) = mob {
|
||||
// A living entity: LivingEntity.createInterpolationHandler's
|
||||
// SteppedInterpolationHandler, over its type's update interval.
|
||||
let entity = PosRot {
|
||||
position: [m.position.x, m.position.y, m.position.z],
|
||||
yaw: m.yaw,
|
||||
pitch: m.head_pitch,
|
||||
};
|
||||
let far = far_snaps && {
|
||||
let d = [0, 1, 2].map(|i| position[i] - entity.position[i]);
|
||||
d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > 4096.0
|
||||
};
|
||||
let handler = r.stepped.entry(id).or_insert_with(|| {
|
||||
let interval = horizon_client::lan::tracking::settings(&m.kind.id).1;
|
||||
SteppedInterpolation::new(interval as i32)
|
||||
});
|
||||
let snap = if far {
|
||||
handler.cancel();
|
||||
let (yaw, pitch) = rotation.unwrap_or((m.yaw, m.head_pitch));
|
||||
Some(PosRot {
|
||||
position,
|
||||
yaw,
|
||||
pitch,
|
||||
})
|
||||
} else {
|
||||
match handler.interpolate_to(entity, path, rotation) {
|
||||
Start::Snap(at) => Some(at),
|
||||
Start::Interpolating | Start::Unchanged => None,
|
||||
}
|
||||
};
|
||||
if let Some(at) = snap {
|
||||
place_interpolated(m, at);
|
||||
m.previous = m.position;
|
||||
}
|
||||
m.on_ground = on_ground;
|
||||
return;
|
||||
}
|
||||
// AbstractInterpolationHandler.interpolateTo: without a rotation,
|
||||
// the one it was heading for (or has).
|
||||
let rotation = rotation
|
||||
.or_else(|| r.lerps.get(&id).and_then(|l| l.rotation))
|
||||
.or_else(|| mob.as_ref().map(|m| (m.yaw, m.head_pitch)));
|
||||
// the one it was heading for.
|
||||
let rotation = rotation.or_else(|| r.lerps.get(&id).and_then(|l| l.rotation));
|
||||
r.lerps.insert(
|
||||
id,
|
||||
Lerp {
|
||||
@@ -1365,9 +1439,6 @@ impl Game<'_> {
|
||||
*p = position;
|
||||
}
|
||||
}
|
||||
if let Some(m) = mob {
|
||||
m.on_ground = on_ground;
|
||||
}
|
||||
self.turn_remote_object(id, rotation.map(|(yaw, _)| yaw), on_ground);
|
||||
self.turn_remote_projectile(id, rotation);
|
||||
}
|
||||
@@ -1384,6 +1455,7 @@ impl Game<'_> {
|
||||
if let Some(r) = self.remote.as_mut() {
|
||||
r.entities.remove(&id);
|
||||
r.lerps.remove(&id);
|
||||
r.stepped.remove(&id);
|
||||
r.head_lerps.remove(&id);
|
||||
r.pending_items.remove(&id);
|
||||
}
|
||||
@@ -1743,17 +1815,36 @@ impl Game<'_> {
|
||||
.and_then(|r| r.entities.get(&m.id))
|
||||
.map(|t| t.position);
|
||||
let Some(base) = base else { return Ok(()) };
|
||||
let position = m.delta.as_ref().map_or(base, |d| d.end(base));
|
||||
// VecDeltaCodec.decode: each step's position from the last.
|
||||
let path = m.delta.as_ref().map(|d| match d {
|
||||
play::VecDelta::Linear(_) => Path::Linear(d.end(base)),
|
||||
play::VecDelta::Stepped(steps) => {
|
||||
let mut at = base;
|
||||
Path::Stepped(
|
||||
steps
|
||||
.iter()
|
||||
.map(|(delta, ticks)| {
|
||||
at = play::VecDelta::apply(at, *delta);
|
||||
(at, *ticks)
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
});
|
||||
let rotation = m.rotation.map(|(y, x)| {
|
||||
(
|
||||
horizon_protocol::buf::unpack_degrees(y),
|
||||
horizon_protocol::buf::unpack_degrees(x),
|
||||
)
|
||||
});
|
||||
self.move_entity(m.id, position, rotation, m.on_ground);
|
||||
self.move_entity(m.id, path, rotation, m.on_ground, false);
|
||||
}
|
||||
C::EntityPositionSync(p) => {
|
||||
self.move_entity(p.id, p.path.end(), Some((p.yaw, p.pitch)), p.on_ground)
|
||||
let path = match p.path {
|
||||
play::PositionPath::Linear(at) => Path::Linear(at),
|
||||
play::PositionPath::Stepped(steps) => Path::Stepped(steps),
|
||||
};
|
||||
self.move_entity(p.id, Some(path), Some((p.yaw, p.pitch)), p.on_ground, true)
|
||||
}
|
||||
C::TeleportEntity(p) => {
|
||||
let base = self
|
||||
@@ -1773,7 +1864,13 @@ impl Game<'_> {
|
||||
rel(play::relative::Y, 1),
|
||||
rel(play::relative::Z, 2),
|
||||
];
|
||||
self.move_entity(p.id, position, Some((p.yaw, p.pitch)), p.on_ground);
|
||||
self.move_entity(
|
||||
p.id,
|
||||
Some(Path::Linear(position)),
|
||||
Some((p.yaw, p.pitch)),
|
||||
p.on_ground,
|
||||
false,
|
||||
);
|
||||
}
|
||||
C::SetPassengers(p) => self.remote_set_passengers(p.vehicle, &p.passengers),
|
||||
C::MoveVehicle(p) => self.remote_move_vehicle(p.position, p.yaw),
|
||||
@@ -3149,6 +3246,7 @@ impl Game<'_> {
|
||||
return;
|
||||
};
|
||||
let mut lerps = std::mem::take(&mut r.lerps);
|
||||
let stepped = &mut r.stepped;
|
||||
let head_lerps = &mut r.head_lerps;
|
||||
let players = &r.level_players;
|
||||
let mut using = Vec::new();
|
||||
@@ -3179,22 +3277,12 @@ impl Game<'_> {
|
||||
// LivingEntity.updateSwingTime.
|
||||
m.anim.attack_anim_o = m.anim.attack_anim;
|
||||
horizon_client::mobs::ai::tick_swing(m);
|
||||
// Entity.commonTick: LinearInterpolationHandler.interpolate.
|
||||
if let Some(l) = lerps.get_mut(&(m.id as i32))
|
||||
&& l.steps > 0
|
||||
// Entity.tick: the SteppedInterpolationHandler's step.
|
||||
if let Some(at) = stepped
|
||||
.get_mut(&(m.id as i32))
|
||||
.and_then(|h| h.interpolate())
|
||||
{
|
||||
let n = f64::from(l.steps);
|
||||
m.position = Vec3::new(
|
||||
m.position.x + (l.target[0] - m.position.x) / n,
|
||||
m.position.y + (l.target[1] - m.position.y) / n,
|
||||
m.position.z + (l.target[2] - m.position.z) / n,
|
||||
);
|
||||
if let Some((yaw, pitch)) = l.rotation {
|
||||
let alpha = 1.0 / l.steps as f32;
|
||||
m.yaw += wrap_degrees(yaw - m.yaw) * alpha;
|
||||
m.head_pitch += (pitch - m.head_pitch) * alpha;
|
||||
}
|
||||
l.steps -= 1;
|
||||
place_interpolated(m, at);
|
||||
}
|
||||
// LivingEntity.aiStep: the head's own interpolation.
|
||||
if let Some((yaw, steps)) = head_lerps.get_mut(&(m.id as i32))
|
||||
|
||||
@@ -59,6 +59,8 @@ pub struct EntityState {
|
||||
/// `Entity.getRequiresPrecisePosition` (primed TNT and falling blocks
|
||||
/// touching a block): moves are sent as full position syncs.
|
||||
pub precise: bool,
|
||||
/// `Entity.needsSync`: send its changes this tick, due or not.
|
||||
pub needs_sync: bool,
|
||||
}
|
||||
|
||||
impl EntityState {
|
||||
@@ -79,6 +81,7 @@ impl EntityState {
|
||||
self.equipment.clear();
|
||||
self.attributes.clear();
|
||||
self.precise = false;
|
||||
self.needs_sync = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,7 +325,7 @@ impl ServerEntity {
|
||||
// SynchedEntityData.isDirty: once for every client.
|
||||
let dirty = e.meta != self.meta;
|
||||
let due = self.interval != 0 && self.tick.is_multiple_of(self.interval);
|
||||
if due || dirty {
|
||||
if due || dirty || e.needs_sync {
|
||||
let rotation = (pack_degrees(e.pitch), pack_degrees(e.yaw));
|
||||
let turned = |a: i8, b: i8| (i32::from(a) - i32::from(b)).abs() >= 1;
|
||||
let rotate = self.arrow
|
||||
@@ -398,7 +401,7 @@ impl ServerEntity {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if self.track_deltas {
|
||||
if self.track_deltas || e.needs_sync {
|
||||
let diff: f64 = (0..3)
|
||||
.map(|k| (e.velocity[k] - self.velocity[k]).powi(2))
|
||||
.sum();
|
||||
@@ -855,6 +858,7 @@ mod tests {
|
||||
equipment: Vec::new(),
|
||||
attributes: Vec::new(),
|
||||
precise: false,
|
||||
needs_sync: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -918,6 +922,37 @@ mod tests {
|
||||
assert!(matches!(jump[0], play::Clientbound::EntityPositionSync(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_knocked_back_mob_is_sent_at_once() {
|
||||
let mut s = added(&state(0.0));
|
||||
changes(&mut s, &state(0.0));
|
||||
// Not due (tick 1 of 3), but LivingEntity.knockback set needsSync:
|
||||
// its move and its new motion go out now.
|
||||
let mut knocked = state(0.3);
|
||||
knocked.velocity = [0.2, 0.3, 0.0];
|
||||
knocked.needs_sync = true;
|
||||
let out = changes(&mut s, &knocked);
|
||||
assert!(
|
||||
matches!(
|
||||
out[..],
|
||||
[
|
||||
play::Clientbound::SetEntityMotion(_),
|
||||
play::Clientbound::MoveEntityPos(_)
|
||||
]
|
||||
),
|
||||
"{out:?}"
|
||||
);
|
||||
// Then every third tick again.
|
||||
assert!(changes(&mut s, &state(0.5)).is_empty());
|
||||
assert!(matches!(
|
||||
changes(&mut s, &state(0.6))[..],
|
||||
[
|
||||
play::Clientbound::SetEntityMotion(_),
|
||||
play::Clientbound::MoveEntityPos(_)
|
||||
]
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fireballs_bundle_their_motion_with_their_power() {
|
||||
let mut e = state(0.0);
|
||||
|
||||
@@ -1547,6 +1547,7 @@ impl Hub {
|
||||
equipment,
|
||||
attributes,
|
||||
precise: false,
|
||||
needs_sync: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -792,6 +792,9 @@ pub struct Mob {
|
||||
pub on_ground: bool,
|
||||
pub horizontal_collision: bool,
|
||||
pub vertical_collision_below: bool,
|
||||
/// `Entity.needsSync`: knocked back, pushed or jumped since its server
|
||||
/// last sent it, so the next update goes out at once.
|
||||
pub needs_sync: bool,
|
||||
pub health: f32,
|
||||
pub hurt_time: i32,
|
||||
pub invulnerable: i32,
|
||||
@@ -1361,8 +1364,10 @@ impl PendingPushes {
|
||||
}
|
||||
/// Adds the pending pushes aimed at `m` to its velocity, in push order.
|
||||
pub fn apply_mob_pushes(m: &mut Mob, pending: &mut PendingPushes) {
|
||||
// Entity.push(x, y, z) sets needsSync.
|
||||
let mut add = |v: Vec3| {
|
||||
m.velocity = Vec3::new(m.velocity.x + v.x, m.velocity.y + v.y, m.velocity.z + v.z);
|
||||
m.needs_sync = true;
|
||||
};
|
||||
if !pending.list.is_empty() {
|
||||
pending.list.retain(|(id, v)| {
|
||||
@@ -1529,6 +1534,7 @@ impl Mob {
|
||||
on_ground: false,
|
||||
horizontal_collision: false,
|
||||
vertical_collision_below: false,
|
||||
needs_sync: false,
|
||||
hurt_time: 0,
|
||||
invulnerable: 0,
|
||||
death_time: 0,
|
||||
@@ -1769,6 +1775,7 @@ impl Mob {
|
||||
if power <= 0.0 {
|
||||
return;
|
||||
}
|
||||
self.needs_sync = true;
|
||||
while xd * xd + zd * zd < f64::from(1.0e-5_f32) {
|
||||
xd = (self.random.next_double() - self.random.next_double()) * 0.01;
|
||||
zd = (self.random.next_double() - self.random.next_double()) * 0.01;
|
||||
|
||||
@@ -832,6 +832,9 @@ impl Mob {
|
||||
};
|
||||
}
|
||||
pub fn jump_from_ground(&mut self, w: &MobWorld) {
|
||||
// LivingEntity.jumpFromGround (and the rabbit's and cubes' own,
|
||||
// which call it or do the same): needsSync.
|
||||
self.needs_sync = true;
|
||||
if self.species() == super::Species::Rabbit {
|
||||
super::rabbit::jump(self, w);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user