ghast-entity 0.5.3

Entity tracking for the ghast Minecraft Java Edition client library: zero-allocation parsing of 1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3 entity packets, vanilla-exact positions, O(1) lookup
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Modern entity movement: the `VecDeltaCodec`, rotation maths, interpolation (linear and
//! 26.3's stepped paths) and the per-entity client tick.

use super::*;

#[inline]
pub(super) fn chunk_key(x: i32, z: i32) -> u64 {
    u64::from(x as u32) | (u64::from(z as u32) << 32)
}

/// `Mth.floor(double)`: `(int) v`, minus one when that is above `v` (no libm `floor` call).
#[inline]
pub(super) fn mth_floor(v: f64) -> i32 {
    let i = v as i32;
    if v < f64::from(i) { i - 1 } else { i }
}

/// `Mth.floor` of a coordinate, then the chunk.
#[inline]
pub(super) fn chunk_of(p: [f64; 3]) -> u64 {
    chunk_key(mth_floor(p[0]) >> 4, mth_floor(p[2]) >> 4)
}

/// Java `Math.round(double)` (half up, saturating, NaN = 0), without a libm `floor` call.
#[inline]
pub(super) fn jround(x: f64) -> i64 {
    if x.is_nan() || x.abs() >= 4.5e15 {
        // Integral already (or NaN / out of range): the saturating cast is Java's result.
        return x as i64;
    }
    let t = x as i64;
    let f = if x < t as f64 { t - 1 } else { t };
    if x - f as f64 >= 0.5 { f + 1 } else { f }
}

/// The `VecDeltaCodec` base (the last absolute packet position), kept inline in the entity's
/// version-specific `traced` slot (as `f64` bits) so a move needs no second cache line for it.
#[inline]
pub(super) fn base(e: &Entity) -> [f64; 3] {
    e.traced.map(|b| f64::from_bits(b as u64))
}

#[inline]
pub(super) fn set_base(e: &mut Entity, p: [f64; 3]) {
    e.traced = p.map(|c| c.to_bits() as i64);
}

/// `VecDeltaCodec.decode`.
#[inline]
pub(super) fn decode_delta(base: [f64; 3], d: [i16; 3]) -> [f64; 3] {
    if d == [0; 3] {
        return base;
    }
    let ax = |b: f64, d: i16| {
        if d == 0 {
            b
        } else {
            jround(b * 4096.0).wrapping_add(i64::from(d)) as f64 / 4096.0
        }
    };
    [ax(base[0], d[0]), ax(base[1], d[1]), ax(base[2], d[2])]
}

/// `Float.equals` / `Double.compare == 0` (bits, NaN equal to NaN).
#[inline]
fn same_f32(a: f32, b: f32) -> bool {
    a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan())
}

#[inline]
fn same_f64(a: f64, b: f64) -> bool {
    a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan())
}

/// `Mth.wrapDegrees(double)`.
#[inline]
fn wrap_degrees(a: f64) -> f64 {
    let mut r = a % 360.0;
    if r >= 180.0 {
        r -= 360.0;
    }
    if r < -180.0 {
        r += 360.0;
    }
    r
}

/// `Entity.setYRot` (non-finite values are discarded).
#[inline]
pub(super) fn set_yrot(e: &mut Entity, y: f32) {
    if y.is_finite() {
        e.yaw = y;
    }
}

/// `Entity.setXRot`: `clamp(x % 360, -90, 90)`.
#[inline]
pub(super) fn set_xrot(e: &mut Entity, x: f32) {
    if x.is_finite() {
        e.pitch = (x % 360.0).clamp(-90.0, 90.0);
    }
}

#[inline]
pub(super) fn living(e: &Entity) -> bool {
    e.m.td.is_some_and(EntityTypeData::is_living)
}

/// `snapTo(pos, yRot, xRot)`.
pub(super) fn snap_to(e: &mut Entity, p: [f64; 3], y: f32, x: f32) {
    e.pos = p;
    set_yrot(e, y);
    set_xrot(e, x);
}

/// One step of a 26.3 position path: an absolute position and the ticks to reach it.
pub(super) type PathStep = ([f64; 3], i32);

/// The 26.3 stepped interpolations, by entity id (see [`crate::modern_state::Stepped`]).
type SteppedTable = Fast<i32, crate::modern_state::Stepped>;

/// Rotation equality of the interpolation handler's "same target" test: `Objects.equals` on
/// boxed floats before 26.3, `PositionAndRotation.is` (`==`) since.
#[inline]
fn same_rot(a: f32, b: f32) -> bool {
    if super::PATHS { a == b } else { same_f32(a, b) }
}

/// `Mth.rotLerp(float, float, float)`.
#[inline]
fn rot_lerp_f32(a: f32, from: f32, to: f32) -> f32 {
    from + a * wrap_f32(to - from)
}

/// `Math.max(float, float)` (NaN wins).
#[inline]
fn jmax_f32(a: f32, b: f32) -> f32 {
    if a.is_nan() || a > b { a } else { b }
}

/// `true` when entity type `td` interpolates with 26.3's `SteppedInterpolationHandler`
/// (living entities with an interpolation handler).
#[inline]
pub(super) fn stepped_type(td: Option<&EntityTypeData>) -> bool {
    super::PATHS && td.is_some_and(|t| t.is_living() && t.interpolation.is_some_and(|n| n > 0))
}

/// `Entity.moveOrInterpolateTo(position?, yRot?, xRot?)`. `path`: the steps of a 26.3 stepped
/// position path ending at `p` (empty: a linear one).
#[inline]
pub(super) fn move_or_interpolate(
    e: &mut Entity,
    table: &mut SteppedTable,
    p: Option<[f64; 3]>,
    path: &[PathStep],
    y: Option<f32>,
    x: Option<f32>,
) {
    if stepped_type(e.m.td) {
        return stepped_interpolate_to(e, table, p, path, y, x);
    }
    let _ = (table, path);
    // One borrow of the boxed state for the whole update.
    let m = &mut *e.m;
    match m.td.and_then(|t| t.interpolation) {
        Some(n) => {
            let active = m.steps > 0;
            let p = p.unwrap_or(if active { m.target } else { e.pos });
            let y = y.unwrap_or(if active { m.target_rot[0] } else { e.yaw });
            let x = x.unwrap_or(if active { m.target_rot[1] } else { e.pitch });
            if n == 0 {
                m.steps = 0;
                m.set_flag(st::HAS_PREV, false);
                snap_to(e, p, y, x);
            } else if !active
                || !same_rot(m.target_rot[0], y)
                || !same_rot(m.target_rot[1], x)
                || !(0..3).all(|i| same_f64(m.target[i], p[i]))
            {
                m.steps = n;
                m.target = p;
                m.target_rot = [y, x];
                m.prev = e.pos;
                m.prev_rot = [e.pitch, e.yaw];
                m.set_flag(st::HAS_PREV, true);
            }
        }
        None => {
            if let Some(p) = p {
                e.pos = p;
            }
            if let Some(y) = y {
                set_yrot(e, y % 360.0);
            }
            if let Some(x) = x {
                set_xrot(e, x % 360.0);
            }
        }
    }
}

/// 26.3 `AbstractInterpolationHandler.interpolateTo` with `SteppedInterpolationHandler`'s
/// `startInterpolating`: the path's steps join the entity's queue, each with the ticks it takes
/// (a linear path or an unchanged end: one step of the type's update interval); a turn is spread
/// over a stepped path by its tick counts. Out of line: only 26.3 living entities get here.
#[inline(never)]
fn stepped_interpolate_to(
    e: &mut Entity,
    table: &mut SteppedTable,
    p: Option<[f64; 3]>,
    path: &[PathStep],
    y: Option<f32>,
    x: Option<f32>,
) {
    let n = e.m.td.and_then(|t| t.interpolation).unwrap_or(0);
    let m = &mut *e.m;
    let active = m.steps > 0;
    let end = p.unwrap_or(if active { m.target } else { e.pos });
    let y = y.unwrap_or(if active { m.target_rot[0] } else { e.yaw });
    let x = x.unwrap_or(if active { m.target_rot[1] } else { e.pitch });
    if active
        && same_rot(m.target_rot[0], y)
        && same_rot(m.target_rot[1], x)
        && (0..3).all(|i| same_f64(m.target[i], end[i]))
    {
        return;
    }
    let q = table
        .entry(e.id)
        .or_insert_with(|| crate::modern_state::Stepped {
            speed: 1.0,
            ..Default::default()
        });
    if !active {
        // setStartingPoint.
        q.last = e.pos;
        q.last_rot = [e.yaw, e.pitch];
        q.current = 1.0;
    }
    let mut add = |pos: [f64; 3], rot: [f32; 2], ticks: i32| {
        q.queue
            .push_back(crate::modern_state::Step { pos, rot, ticks });
        q.remaining += ticks as f32;
    };
    let same_end = (0..3).all(|i| same_f64(m.target[i], end[i]));
    if same_end || path.is_empty() {
        add(end, [y, x], i32::from(n));
    } else if m.target_rot[0] == y && m.target_rot[1] == x {
        for &(pos, ticks) in path {
            add(pos, [y, x], ticks);
        }
    } else {
        let total = path.iter().fold(0i32, |t, s| t.wrapping_add(s.1));
        let mut offset = 0i32;
        let from = m.target_rot;
        for &(pos, ticks) in path {
            offset = offset.wrapping_add(ticks);
            let a = offset as f32 / total as f32;
            let ry = rot_lerp_f32(a, from[0], y);
            let rx = from[1] + a * (x - from[1]);
            add(pos, [ry, rx], ticks);
        }
    }
    m.target = end;
    m.target_rot = [y, x];
    m.steps = 1;
    // setLastPositionAndRotation.
    m.prev = e.pos;
    m.prev_rot = [e.pitch, e.yaw];
    m.set_flag(st::HAS_PREV, true);
}

/// One 26.3 `SteppedInterpolationHandler` tick (`AbstractInterpolationHandler.interpolate`):
/// an entity with an empty queue drops its entry (`cancel`); otherwise the queue follows moves
/// made since the last step, the entity moves along the current step, and the pace adapts to
/// the ticks still queued.
#[inline(never)]
fn stepped_tick(
    e: &mut Entity,
    table: &mut SteppedTable,
    no_collision: &mut impl FnMut(&Entity, [f64; 6]) -> bool,
) {
    let Some(q) = table.get_mut(&e.id) else {
        return;
    };
    if q.queue.is_empty() {
        table.remove(&e.id);
        e.m.steps = 0;
        return;
    }
    let n = e.m.td.and_then(|t| t.interpolation).unwrap_or(1);
    // adjustInterpolationTargetFromDeltas.
    let d = [
        e.pos[0] - e.m.prev[0],
        e.pos[1] - e.m.prev[1],
        e.pos[2] - e.m.prev[2],
    ];
    if d[0] * d[0] + d[1] * d[1] + d[2] * d[2] > f64::from(1.0e-5_f32) {
        let t = e.m.target;
        let moved = [t[0] + d[0], t[1] + d[1], t[2] + d[2]];
        let bb = dimensions(e).map(|dm| {
            let b = dm.bounding_box(moved[0], moved[1], moved[2]);
            [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z]
        });
        if bb.is_none_or(|bb| no_collision(&*e, bb)) {
            let add = |p: &mut [f64; 3]| {
                for i in 0..3 {
                    p[i] += d[i];
                }
            };
            add(&mut e.m.target);
            for s in &mut q.queue {
                add(&mut s.pos);
            }
            add(&mut q.last);
        }
    }
    let dy = e.yaw - e.m.prev_rot[1];
    let dx = e.pitch - e.m.prev_rot[0];
    let turn = |r: &mut [f32; 2]| {
        r[0] += dy;
        r[1] += dx;
    };
    turn(&mut e.m.target_rot);
    for s in &mut q.queue {
        turn(&mut s.rot);
    }
    turn(&mut q.last_rot);
    // doInterpolate: getNewPositionAndRotation.
    let (pos, rot) = loop {
        let Some(s) = q.queue.front().copied() else {
            break (e.m.target, e.m.target_rot);
        };
        let offset = s.ticks as f32;
        if q.current < offset {
            let a = q.current / offset;
            let lerp = |from: f64, to: f64| from + f64::from(a) * (to - from);
            break (
                [
                    lerp(q.last[0], s.pos[0]),
                    lerp(q.last[1], s.pos[1]),
                    lerp(q.last[2], s.pos[2]),
                ],
                [
                    rot_lerp_f32(a, q.last_rot[0], s.rot[0]),
                    q.last_rot[1] + a * (s.rot[1] - q.last_rot[1]),
                ],
            );
        }
        q.current -= offset;
        q.last = s.pos;
        q.last_rot = s.rot;
        q.queue.pop_front();
    };
    e.pos = pos;
    set_yrot(e, rot[0] % 360.0);
    set_xrot(e, rot[1] % 360.0);
    // advance(getRelativeTickSpeed() = 1 at 20 ticks per second).
    let steps = f32::from(n);
    let target_speed = jmax_f32(q.remaining / steps, 1.0);
    q.speed += (1.0 / steps) * (target_speed - q.speed);
    let mut ticks = 1.0_f32;
    if ticks * q.speed < q.remaining {
        ticks *= q.speed;
    } else {
        ticks = q.remaining;
        q.speed = 1.0;
    }
    q.current += ticks;
    q.remaining -= ticks;
    e.m.steps = u8::from(!q.queue.is_empty());
    e.m.prev = e.pos;
    e.m.prev_rot = [e.pitch, e.yaw];
    e.m.set_flag(st::HAS_PREV, true);
}

/// `true` when the bot (`me`) steers `e` (a boat whose first passenger is the bot, or the mount
/// the client said it steers, `steered`).
#[inline]
pub(super) fn controlled(me: Option<i32>, steered: Option<i32>, e: &Entity) -> bool {
    // The mount the client says it steers (it may not know the passengers yet).
    if steered == Some(e.id) {
        return true;
    }
    // Entities without links (nearly all) leave at the first test.
    let Some(l) = &e.links else {
        return false;
    };
    me.is_some()
        && l.passengers.first().copied() == me
        && e.m
            .td
            .is_some_and(|t| t.name.ends_with("_boat") || t.name.ends_with("_raft"))
}

/// `eid` is tracked and of a [`Config::kept_types`] type (its equipment and effects are kept
/// with those groups off).
pub(super) fn kept(s: &Entities, eid: i32) -> bool {
    !s.config.kept_types.is_empty()
        && s.map.get(&eid).is_some_and(
            |e| matches!(e.ty, EntityType::Registry(t) if s.config.kept_types.contains(&t)),
        )
}

/// `Mth.wrapDegrees(float)`.
#[inline]
fn wrap_f32(a: f32) -> f32 {
    let mut r = a % 360.0;
    if r >= 180.0 {
        r -= 360.0;
    }
    if r < -180.0 {
        r += 360.0;
    }
    r
}

/// `Mth.clamp(float)`: `v < min ? min : Math.min(v, max)`.
#[inline]
fn jclamp(v: f32, min: f32, max: f32) -> f32 {
    if v < min {
        min
    } else if v.is_nan() {
        v
    } else if v == 0.0 && max == 0.0 {
        if v.is_sign_negative() || max.is_sign_negative() {
            -0.0
        } else {
            0.0
        }
    } else if v <= max {
        v
    } else {
        max
    }
}

/// `Mth.rotateIfNecessary`.
#[inline]
fn rotate_if_necessary(base: f32, target: f32, max: f32) -> f32 {
    target - jclamp(wrap_f32(target - base), -max, max)
}

/// One `ClientLevel.tickNonPassenger` of a 26.1 entity, as far as it changes tracked state:
/// dirty attributes (`baseTick`), the interpolation and head-rotation steps (`aiStep`), the mob
/// body / head turn (`tickHeadTurn`: `BodyRotationControl.clientTick`).
/// The store's side tables a tick reads: stepped paths, sleeping positions, client motions.
type TickSide<'a> = (
    &'a mut SteppedTable,
    &'a mut Fast<i32, [i32; 3]>,
    &'a mut Fast<i32, [f64; 3]>,
);

pub(super) fn tick_entity(
    e: &mut Entity,
    (table, sleeping, motions): TickSide<'_>,
    sleep_height: &mut dyn FnMut([i32; 3]) -> Option<f64>,
    carrying_mob: bool,
    no_collision: &mut impl FnMut(&Entity, [f64; 6]) -> bool,
) {
    let old = e.pos;
    let was_interpolating = e.m.steps > 0;
    // 26.3 living entities: an entry outlives its queue by one tick (vanilla's `cancel`).
    let queued = super::PATHS && !table.is_empty() && table.contains_key(&e.id);
    if queued {
        stepped_tick(e, table, no_collision);
    } else if e.m.steps > 0 {
        step(e, no_collision);
    }
    if super::PATHS && !e.m.flag(st::TICKED) {
        e.m.set_flag(st::TICKED, true);
        if !sleeping.is_empty()
            && let Some(bed) = sleeping.remove(&e.id)
            && e.m.flag(st::SLEEPING)
            && sleep_height(bed).is_none()
        {
            wake_up(e);
        }
    }
    // LivingEntity.aiStep: the client copy's own motion slows down on a tick without an
    // interpolation (26.1 / 26.2: one active before the step; 26.3 steps it in commonTick and
    // asks after) and is cut under 0.003 per axis.
    // An unridden boat's client tick (AbstractBoat.tick, not authoritative): no motion left.
    if !motions.is_empty()
        && e.m
            .td
            .is_some_and(|t| t.name.ends_with("_boat") || t.name.ends_with("_raft"))
    {
        motions.remove(&e.id);
    }
    let moving = !motions.is_empty()
        && motion_step(
            e.id,
            motions,
            if super::PATHS {
                e.m.steps > 0
            } else {
                was_interpolating
            },
        );
    if e.m.head_steps > 0 {
        let a = 1.0 / f64::from(e.m.head_steps);
        let from = f64::from(e.head_yaw);
        e.head_yaw = (from + a * wrap_degrees(f64::from(e.m.head_target) - from)) as f32;
        e.m.head_steps -= 1;
    }
    let Some(t) = e.m.td else {
        e.m.set_flag(st::SETTLED, true);
        return;
    };
    let mut settled = e.m.steps == 0
        && !moving
        && e.m.head_steps == 0
        && !(t.id == data::ITEM && e.m.item_count <= 0)
        && !(queued && table.contains_key(&e.id));
    if t.flags & (type_flags::MOB | type_flags::DEFAULT_BODY)
        == (type_flags::MOB | type_flags::DEFAULT_BODY)
    {
        let max = f32::from(t.max_head_y_rot);
        let (xd, zd) = (e.pos[0] - old[0], e.pos[2] - old[2]);
        let m = &mut e.m;
        if xd * xd + zd * zd > f64::from(2.500_000_3e-7_f32) {
            m.body_yaw = e.yaw;
            e.head_yaw = rotate_if_necessary(e.head_yaw, m.body_yaw, max);
            m.last_stable_head = e.head_yaw;
            m.head_stable_time = 0;
            settled = false;
        } else if carrying_mob {
            // Idle turning resumes once the mob passenger is gone: keep ticking.
            settled = false;
        } else {
            if (e.head_yaw - m.last_stable_head).abs() > 15.0 {
                m.head_stable_time = 0;
                m.last_stable_head = e.head_yaw;
                m.body_yaw = rotate_if_necessary(m.body_yaw, e.head_yaw, max);
                settled = false;
            } else {
                m.head_stable_time = m.head_stable_time.saturating_add(1).min(30);
                if m.head_stable_time > 10 {
                    let since = m.head_stable_time - 10;
                    let frac = (since as f32 / 10.0).clamp(0.0, 1.0);
                    let angle = max * (1.0 - frac);
                    m.body_yaw = rotate_if_necessary(m.body_yaw, e.head_yaw, angle);
                }
                settled &= m.head_stable_time >= 20 && m.body_yaw.to_bits() == e.head_yaw.to_bits();
            }
        }
    } else if t.flags_meta.is_some() {
        // ArmorStand.tickHeadTurn: the body follows yRot.
        e.m.body_yaw = e.yaw;
    }
    let sleeping = if t.name == "fox" {
        e.m.fox_flags & 0x20 != 0
    } else {
        e.m.flag(st::SLEEPING)
    };
    if sleeping {
        set_xrot(e, 0.0);
    }
    // refreshDirtyAttributes, at the end of LivingEntity.tick.
    if e.m.flag(st::SCALE_DIRTY) {
        e.m.set_flag(st::SCALE_DIRTY, false);
        {
            let d = compute_dims(e);
            e.m.set_dims(d);
        }
    }
    e.m.set_flag(st::SETTLED, settled);
}

/// One tick of a motion-type entity's client motion (`LivingEntity.aiStep` of a copy the
/// client does not simulate): x 0.98 unless `interpolating`, then each axis under 0.003 becomes
/// 0. Returns `true` while some motion is left (the entry goes when it is all zero).
#[inline(never)]
fn motion_step(id: i32, motions: &mut Fast<i32, [f64; 3]>, interpolating: bool) -> bool {
    let Some(v) = motions.get_mut(&id) else {
        return false;
    };
    if !interpolating {
        *v = [v[0] * 0.98, v[1] * 0.98, v[2] * 0.98];
    }
    for c in v.iter_mut() {
        if c.abs() < 0.003 {
            *c = 0.0;
        }
    }
    if v.iter().all(|c| c.to_bits() == 0) {
        motions.remove(&id);
        return false;
    }
    true
}

/// 26.3 `LivingEntity.stopSleeping` without a bed (the first tick of an entity whose sleeping
/// position has no bed block): standing pose, no sleeping position.
#[inline(never)]
fn wake_up(e: &mut Entity) {
    e.m.set_flag(st::SLEEPING, false);
    e.m.pose = 0;
    if !e.meta.is_empty() {
        e.meta.set(meta::POSE, MetaValue::Pose(0));
        e.meta.set(meta::SLEEPING_POS, MetaValue::OptBlockPos(None));
    }
    let d = compute_dims(e);
    e.m.set_dims(d);
}

/// The tab-list part of `Player.tick` for a remote player: the entity caches its tab-list
/// object on first use; spectators and passengers are never on the ground. Out of line: the
/// tick loop over all entities only pays the player check.
#[inline(never)]
pub(super) fn player_tick(e: &mut Entity, tabs: crate::store::Tabs<'_>) {
    if e.m.info_gen == 0
        && let Some(p) = tabs.get(&e.uuid)
    {
        e.m.info_gen = p.generation;
    }
    if e.vehicle().is_some() || game_mode(tabs, e) == Some(3) {
        e.on_ground = false;
    }
}

/// The game mode a player entity sees (`AbstractClientPlayer.getPlayerInfo` caches the tab list
/// object on first use; it stays frozen once the server removes the entry).
pub(super) fn game_mode(tabs: crate::store::Tabs<'_>, e: &Entity) -> Option<i32> {
    let cur = tabs.get(&e.uuid);
    if e.m.info_gen == 0 {
        return cur.map(|p| p.game_mode);
    }
    match cur {
        Some(p) if p.generation == e.m.info_gen => Some(p.game_mode),
        _ => Some(i32::from(e.m.frozen_game_mode)),
    }
}

/// One `InterpolationHandler.interpolate()` step.
fn step(e: &mut Entity, no_collision: &mut impl FnMut(&Entity, [f64; 6]) -> bool) {
    let alpha = 1.0 / f64::from(e.m.steps);
    if e.m.flag(st::HAS_PREV) {
        let d = [
            e.pos[0] - e.m.prev[0],
            e.pos[1] - e.m.prev[1],
            e.pos[2] - e.m.prev[2],
        ];
        let t = e.m.target;
        let moved = [t[0] + d[0], t[1] + d[1], t[2] + d[2]];
        // Before 26.3 any delta moves the target; adding +0.0 changes nothing (but -0.0): skip
        // the collision test then. 26.3 ignores deltas with `lengthSqr() <= 1.0E-5F`.
        let skip = if super::PATHS {
            d[0] * d[0] + d[1] * d[1] + d[2] * d[2] <= f64::from(1.0e-5_f32)
        } else {
            (0..3).all(|i| moved[i].to_bits() == t[i].to_bits())
        };
        if !skip {
            let bb = dimensions(e).map(|dm| {
                let b = dm.bounding_box(moved[0], moved[1], moved[2]);
                [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z]
            });
            if bb.is_none_or(|bb| no_collision(&*e, bb)) {
                e.m.target = moved;
            }
        }
        // previousTickRot = (xRot, yRot).
        e.m.target_rot[0] += e.yaw - e.m.prev_rot[1];
        e.m.target_rot[1] += e.pitch - e.m.prev_rot[0];
    }
    let t = e.m.target;
    let lerp = |a: f64, b: f64| a + alpha * (b - a);
    let p = [
        lerp(e.pos[0], t[0]),
        lerp(e.pos[1], t[1]),
        lerp(e.pos[2], t[2]),
    ];
    let y0 = f64::from(e.yaw);
    let ny = (y0 + alpha * wrap_degrees(f64::from(e.m.target_rot[0]) - y0)) as f32;
    let nx = lerp(f64::from(e.pitch), f64::from(e.m.target_rot[1])) as f32;
    e.pos = p;
    // setRot: setYRot(y % 360), setXRot(x % 360).
    set_yrot(e, ny % 360.0);
    set_xrot(e, nx % 360.0);
    e.m.steps -= 1;
    e.m.prev = p;
    e.m.prev_rot = [e.pitch, e.yaw];
    e.m.set_flag(st::HAS_PREV, true);
}