ghast-physics 0.5.4

Vanilla-exact, pure and deterministic player movement for the ghast Minecraft Java Edition client library (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3), verified bit for bit against the real game
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
//! Living mounts the local player rides on the 1.16.5 client: the horse family (horse, donkey,
//! mule, skeleton and zombie horse), llamas and trader llamas, saddled pigs and striders.
//!
//! # Who moves the mount
//!
//! The client moves a mount itself when the main player is its primary passenger and the mount
//! lets its rider control it (`Entity.isLogicalSideForUpdatingMovement` with
//! `MobEntity.canBeControlledByRider`): every horse-family mount except llamas, a pig while the
//! rider holds a carrot on a stick in either hand, a strider while it holds a warped fungus on a
//! stick. The client then ignores the server's position packets for it, sends `vehicle_move` after
//! each tick and answers the server's `vehicle_move` correction. Any other mount (llamas, a pig or
//! strider without the stick) is moved by the server: the client interpolates it over 3 steps
//! towards each position packet, like any mob.
//!
//! # A tick while riding, in vanilla order
//!
//! 1. The mount's tick (`ClientWorld.tickEntity`): a strider first decides whether it is cold (no
//!    lava at its feet or under them); the entity base tick (fluid contact and pushes, lava halves
//!    the fall distance); `LivingEntity.tickMovement`: a controlled mount drops its interpolation, a
//!    server-moved one takes one interpolation step, otherwise the motion is scaled by 0.98 (the
//!    client never moves a mob "voluntarily"); motion below 0.003 is zeroed; then the travel:
//!    * horse family, saddled and controlled (`HorseBaseEntity.travel`): the mount takes the
//!      rider's yaw and half its pitch; input is the rider's sideways speed ×0.5 and forward speed
//!      (×0.25 backwards) of the rider's PREVIOUS tick; a charged jump launches it
//!      (jump strength attribute × bar strength × block jump factor; forward input adds a push);
//!      air acceleration is a tenth of the last movement speed; the movement speed is the speed
//!      attribute; then the living travel; a rearing mount on the ground stops;
//!    * pig / strider steered with the stick (`ItemSteerable.travel`): the rider's yaw, half its
//!      pitch, step height 1, always full forward input at the saddled speed (pig: speed ×0.225;
//!      strider: ×0.55, ×0.23 when cold), raised by a boost along a sine over its duration;
//!    * the living travel (`LivingEntity.travel`) as for the player: water, lava (a strider walks
//!      on lava: its feet's lava is no lava for this choice), ground friction, gravity, the step
//!      height of the mount (horse family 1; steered pig / strider 1, else 0.5);
//!    * the walk animation (`limbDistance`, `limbAngle`, twice for a controlled horse) the strider's
//!      seat height follows;
//!
//!    then the body turn (`BodyControl`), a horse's rearing counters and animation, and a
//!    strider's lava floating (`updateFloating`) and block effects.
//! 2. The passenger's tick (`tickRiding`): motion zeroed, the whole player tick as a passenger; on
//!    a saddled horse-family mount the jump key charges the jump bar and releasing it sends
//!    `entity_action` (start riding jump) and charges the mount's jump; then `look`,
//!    `steer_vehicle` and, for a mount the client controls, `vehicle_move`. With the shift key
//!    held in the previous tick the client gets off by itself instead (`PlayerEntity.tickRiding`).
//! 3. The mount places its passenger (`updatePassengerPosition`): mount position + mounted height
//!    offset − 0.35 (a rearing horse lifts and moves its rider; a llama seats it 0.3 ahead of its
//!    body; a strider's seat bobs with its legs); the fall distance is reset.
//!
//! [`tick_mount`](super::tick_mount) runs all three. Not modelled (not proven by the oracle):
//! baby mounts, status effects of the mount (jump boost, slow falling, levitation, dolphin's
//! grace), depth strider on a mount, other entities colliding with the mount, the server's own
//! entity data for the horse flags (rearing, eating), head-look packets.

// Vanilla's negated float comparisons are kept as written: they decide the NaN cases.
#![allow(clippy::neg_cmp_op_on_partial_ord)]

use ghast_data::FluidKind;
use ghast_data::PhysicsFlags;
use ghast_data::v1_16_5::{Block, BlockState};

use super::blocks::{
    ShapeCtx, flags, fluid, is_bed, jump_multiplier, slipperiness, state_at, velocity_multiplier,
};
use super::collide::{BorderShape, Collider, NoEntities, bx, offset};
use super::fluid::{V3, ceil, region_loaded, update_in_fluid_as};
use crate::math::{self, Aabb, floor};
use crate::world::{BlockView, WorldBorder};
use crate::{Packet, Packets, TickInput};

/// A living mount type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MountKind {
    /// Horse.
    Horse,
    /// Donkey.
    Donkey,
    /// Mule.
    Mule,
    /// Skeleton horse.
    SkeletonHorse,
    /// Zombie horse.
    ZombieHorse,
    /// Llama (never steered by its rider).
    Llama,
    /// Trader llama (never steered by its rider).
    TraderLlama,
    /// Pig (steered with a carrot on a stick).
    Pig,
    /// Strider (steered with a warped fungus on a stick).
    Strider,
}

impl MountKind {
    /// The type from its registry name (`horse`, `trader_llama`, ...).
    #[must_use]
    pub fn from_name(name: &str) -> Option<MountKind> {
        Some(match name.strip_prefix("minecraft:").unwrap_or(name) {
            "horse" => MountKind::Horse,
            "donkey" => MountKind::Donkey,
            "mule" => MountKind::Mule,
            "skeleton_horse" => MountKind::SkeletonHorse,
            "zombie_horse" => MountKind::ZombieHorse,
            "llama" => MountKind::Llama,
            "trader_llama" => MountKind::TraderLlama,
            "pig" => MountKind::Pig,
            "strider" => MountKind::Strider,
            _ => return None,
        })
    }

    /// Box width and height of the adult (`EntityType` dimensions).
    #[must_use]
    pub fn dimensions(self) -> (f32, f32) {
        match self {
            MountKind::Horse
            | MountKind::Mule
            | MountKind::SkeletonHorse
            | MountKind::ZombieHorse => (1.396_484_4, 1.6),
            MountKind::Donkey => (1.396_484_4, 1.5),
            MountKind::Llama | MountKind::TraderLlama => (0.9, 1.87),
            MountKind::Pig => (0.9, 0.9),
            MountKind::Strider => (0.9, 1.7),
        }
    }

    /// A `HorseBaseEntity` (llamas included): a jumping mount once saddled.
    #[must_use]
    pub fn is_horse_family(self) -> bool {
        !matches!(self, MountKind::Pig | MountKind::Strider)
    }

    fn is_llama(self) -> bool {
        matches!(self, MountKind::Llama | MountKind::TraderLlama)
    }

    /// The default movement speed attribute (before the server's attributes packet).
    #[must_use]
    pub fn default_speed(self) -> f64 {
        match self {
            MountKind::Horse => f64::from(0.225f32),
            MountKind::Donkey | MountKind::Mule | MountKind::Llama | MountKind::TraderLlama => {
                f64::from(0.175f32)
            }
            MountKind::SkeletonHorse | MountKind::ZombieHorse => f64::from(0.2f32),
            MountKind::Pig => 0.25,
            MountKind::Strider => f64::from(0.175f32),
        }
    }

    /// The default horse jump strength attribute.
    #[must_use]
    pub fn default_jump(self) -> f64 {
        match self {
            MountKind::Donkey | MountKind::Mule | MountKind::Llama | MountKind::TraderLlama => 0.5,
            _ => 0.7,
        }
    }

    /// `getBaseMovementSpeedMultiplier`: the water drag.
    fn water_drag(self) -> f32 {
        if self == MountKind::SkeletonHorse {
            0.96
        } else {
            0.8
        }
    }

    /// The step height an entity of the type is created with.
    fn initial_step(self) -> f32 {
        if self.is_horse_family() { 1.0 } else { 0.6 }
    }
}

/// A living mount the player rides. Create it from the mob spawn packet ([`Mount::new`]).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Mount {
    /// The type.
    pub kind: MountKind,
    /// Position.
    pub pos: [f64; 3],
    /// Box.
    pub bb: Aabb,
    /// Velocity.
    pub vel: [f64; 3],
    /// Yaw.
    pub yaw: f32,
    /// Pitch.
    pub pitch: f32,
    /// Body yaw.
    pub body_yaw: f32,
    /// Head yaw.
    pub head_yaw: f32,
    /// On ground.
    pub on_ground: bool,
    /// Horizontal collision of the last move.
    pub horizontal_collision: bool,
    /// Vertical collision of the last move.
    pub vertical_collision: bool,
    /// Fall distance.
    pub fall_distance: f32,
    /// Touching water.
    pub touching_water: bool,
    /// Step height.
    pub step_height: f32,
    /// Air acceleration (`flyingSpeed`).
    pub flying_speed: f32,
    /// Movement speed field (`getMovementSpeed()`).
    pub movement_speed: f32,
    /// Walk animation speed (`limbDistance`).
    pub limb_distance: f32,
    /// Walk animation phase (`limbAngle`).
    pub limb_angle: f32,
    /// Movement speed attribute.
    pub speed_attribute: f64,
    /// Horse jump strength attribute.
    pub jump_attribute: f64,
    /// Saddled (horse flag 4, a pig's or strider's saddle).
    pub saddled: bool,
    /// The rider holds the mount's stick (pig: carrot on a stick, strider: warped fungus on a
    /// stick) in either hand.
    pub rider_holds_stick: bool,
    /// The server's position of it (`trackedPosition`, what relative moves add to).
    pub tracked: [f64; 3],
    /// The local player is its primary passenger ([`Mount::new`]); `false` for a mount the client
    /// only watches ([`Mount::watched`]) until [`Mount::board`].
    passenger: bool,
    /// Head yaw interpolation (`serverHeadYaw`, `headTrackingIncrements`).
    head_target: f64,
    head_steps: i32,
    /// A strider's last tick halved its motion (`updateFloating`, after the mob's pushes).
    floated: bool,
    water_height: f64,
    lava_height: f64,
    first_update: bool,
    movement_multiplier: [f64; 3],
    prev: [f64; 3],
    target: [f64; 3],
    target_yaw: f64,
    target_pitch: f64,
    steps: i32,
    last_head_yaw: f32,
    body_ticks: i32,
    horse: HorseState,
    boost: Boost,
    cold: bool,
}

/// The horse part (`HorseBaseEntity`).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct HorseState {
    jump_strength: f32,
    in_air: bool,
    jumping: bool,
    angry: bool,
    angry_ticks: i32,
    angry_progress: f32,
    last_angry_progress: f32,
}

/// `SaddledComponent`'s boost.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct Boost {
    boosted: bool,
    ticks: i32,
    time: i32,
}

/// The horse state of a mount, as the oracle records it.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct HorseView {
    /// Charged jump strength (`jumpStrength`).
    pub jump_strength: f32,
    /// In a jump (`isInAir`).
    pub in_air: bool,
    /// `HorseBaseEntity.jumping`.
    pub jumping: bool,
    /// Rearing (horse flag 32).
    pub angry: bool,
    /// Rearing counter.
    pub angry_ticks: i32,
    /// Rearing animation.
    pub angry_progress: f32,
    /// Rearing animation of the previous tick.
    pub last_angry_progress: f32,
}

/// What the mount reads from its rider in its tick.
#[derive(Clone, Copy, Debug)]
struct Rider {
    yaw: f32,
    pitch: f32,
    sideways: f32,
    forward: f32,
}

fn mount_box(kind: MountKind, x: f64, y: f64, z: f64) -> Aabb {
    let (w, h) = kind.dimensions();
    let r = f64::from(w / 2.0);
    bx(x - r, y, z - r, x + r, y + f64::from(h), z + r)
}

/// 1.16 `MathHelper.wrapDegrees(double)`.
fn wrap_degrees(d: f64) -> f64 {
    let mut e = d % 360.0;
    if e >= 180.0 {
        e -= 360.0;
    }
    if e < -180.0 {
        e += 360.0;
    }
    e
}

/// `MathHelper.stepAngleTowards`.
fn step_angle(from: f32, to: f32, step: f32) -> f32 {
    let f = crate::rotation::wrap_degrees(to - from);
    let g = if f < -step {
        -step
    } else if f > step {
        step
    } else {
        f
    };
    to - g
}

/// The 1/4096 fixed point of relative moves (`EntityS2CPacket.encodePacketCoordinate`).
fn encode(v: f64) -> i64 {
    (v * 4096.0).floor() as i64
}

impl Mount {
    /// A mount as the mob spawn packet creates it: at `pos` with the packet's yaw, pitch and head
    /// yaw (decoded from the angle bytes: `byte * 360 / 256`), not moving. Pigs and striders get
    /// the boost the spawn's full entity data starts (its boost time 0, see [`Mount::boost`]).
    #[must_use]
    pub fn new(kind: MountKind, pos: [f64; 3], yaw: f32, pitch: f32, head_yaw: f32) -> Mount {
        let mut m = Mount {
            kind,
            pos,
            bb: mount_box(kind, pos[0], pos[1], pos[2]),
            vel: [0.0; 3],
            yaw: yaw % 360.0,
            pitch: pitch.clamp(-90.0, 90.0) % 360.0,
            body_yaw: head_yaw,
            head_yaw,
            on_ground: false,
            horizontal_collision: false,
            vertical_collision: false,
            fall_distance: 0.0,
            touching_water: false,
            step_height: kind.initial_step(),
            flying_speed: 0.02,
            movement_speed: 0.0,
            limb_distance: 0.0,
            limb_angle: 0.0,
            speed_attribute: kind.default_speed(),
            jump_attribute: kind.default_jump(),
            saddled: false,
            rider_holds_stick: false,
            tracked: pos,
            passenger: true,
            head_target: 0.0,
            head_steps: 0,
            floated: false,
            water_height: 0.0,
            lava_height: 0.0,
            first_update: true,
            movement_multiplier: [0.0; 3],
            prev: pos,
            target: pos,
            target_yaw: 0.0,
            target_pitch: 0.0,
            steps: 0,
            last_head_yaw: 0.0,
            body_ticks: 0,
            horse: HorseState::default(),
            boost: Boost::default(),
            cold: false,
        };
        if !kind.is_horse_family() {
            m.boost(0);
        }
        m
    }

    /// A mount the client only watches (no rider): created by the mob spawn packet like
    /// [`Mount::new`], then fed the server's packets for it and ticked with
    /// [`tick_watched`] every client tick, so that it holds what the real client holds when the
    /// player boards it ([`Mount::board`]): the position and rotation of its interpolation, the
    /// steps left, the velocity of the server's velocity packets (decayed ×0.98 on ticks without
    /// an interpolation step, cut off below 0.003), the mob's own pushes on the player
    /// (`tickCramming`), the ground flag of the last packet, body and head yaw, the walk
    /// animation, the horse's rearing.
    #[must_use]
    pub fn watched(kind: MountKind, pos: [f64; 3], yaw: f32, pitch: f32, head_yaw: f32) -> Mount {
        let mut m = Mount::new(kind, pos, yaw, pitch, head_yaw);
        m.passenger = false;
        m
    }

    /// The local player became its primary passenger (`set_passengers`).
    pub fn board(&mut self) {
        self.passenger = true;
    }

    /// The local player is no longer its passenger: from now on it is only watched.
    pub fn leave(&mut self) {
        self.passenger = false;
    }

    /// The local player is its primary passenger.
    #[must_use]
    pub fn ridden(&self) -> bool {
        self.passenger
    }

    /// Head-yaw steps left (`headTrackingIncrements`).
    #[must_use]
    pub fn head_steps(&self) -> i32 {
        self.head_steps
    }

    /// The server's head-yaw packet (`updateTrackedHeadRotation(yaw, 3)`), the angle byte.
    pub fn server_head_yaw(&mut self, yaw: i8) {
        self.head_target = f64::from(f32::from(yaw) * 360.0 / 256.0);
        self.head_steps = 3;
    }

    /// The server's horse flags entity data (index 16; horse family): saddled (4) and rearing
    /// (32).
    pub fn server_horse_flags(&mut self, flags: u8) {
        if self.kind.is_horse_family() {
            self.saddled = flags & 4 != 0;
            self.horse.angry = flags & 32 != 0;
        }
    }

    /// The client moves this mount (`isLogicalSideForUpdatingMovement` with the main player as
    /// its primary passenger).
    #[must_use]
    pub fn controlled(&self) -> bool {
        if !self.passenger {
            return false;
        }
        match self.kind {
            MountKind::Llama | MountKind::TraderLlama => false,
            MountKind::Pig | MountKind::Strider => self.rider_holds_stick,
            _ => true,
        }
    }

    /// The rider's jump bar works (`hasJumpingMount`: a saddled horse-family mount).
    #[must_use]
    pub fn can_jump(&self) -> bool {
        self.kind.is_horse_family() && self.saddled
    }

    /// Steps of interpolation left (`bodyTrackingIncrements`).
    #[must_use]
    pub fn interpolation_steps(&self) -> i32 {
        self.steps
    }

    /// A strider's cold flag of the last tick (`false` for other types).
    #[must_use]
    pub fn cold(&self) -> bool {
        self.cold
    }

    /// The boost of a pig or strider: boosted, ticks into it, its length.
    #[must_use]
    pub fn boost_state(&self) -> (bool, i32, i32) {
        (self.boost.boosted, self.boost.ticks, self.boost.time)
    }

    /// The horse state (horse family).
    #[must_use]
    pub fn horse(&self) -> HorseView {
        let h = self.horse;
        HorseView {
            jump_strength: h.jump_strength,
            in_air: h.in_air,
            jumping: h.jumping,
            angry: h.angry,
            angry_ticks: h.angry_ticks,
            angry_progress: h.angry_progress,
            last_angry_progress: h.last_angry_progress,
        }
    }

    /// The server's boost time entity data of a pig or strider (`onTrackedDataSet(BOOST_TIME)`
    /// on the client: `SaddledComponent.boost`): a boost of `time` ticks starts.
    pub fn boost(&mut self, time: i32) {
        self.boost = Boost {
            boosted: true,
            ticks: 0,
            time,
        };
    }

    /// The server's velocity packet for the mount (`setVelocityClient`, units of 1/8000).
    pub fn server_velocity(&mut self, x: i16, y: i16, z: i16) {
        self.vel = [
            f64::from(x) / 8000.0,
            f64::from(y) / 8000.0,
            f64::from(z) / 8000.0,
        ];
    }

    /// The velocity of the mob spawn packet (`onMobSpawn`: units of 1/8000, divided in `float`).
    pub fn spawn_velocity(&mut self, x: i16, y: i16, z: i16) {
        self.vel = [x, y, z].map(|v| f64::from(f32::from(v) / 8000.0));
    }

    /// Moves the mount to a position (vanilla `setPosition`).
    pub fn set_position(&mut self, x: f64, y: f64, z: f64) {
        self.pos = [x, y, z];
        self.bb = mount_box(self.kind, x, y, z);
    }

    /// The server's `vehicle_move` (`onVehicleMove`: `updatePositionAndAngles`) for a mount the
    /// client controls; returns the `vehicle_move` the client answers with at once. `None` (and
    /// nothing changes) for a mount the server moves.
    pub fn server_move(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) -> Option<Packet> {
        if !self.controlled() {
            return None;
        }
        self.yaw = yaw % 360.0;
        self.pitch = pitch.clamp(-90.0, 90.0) % 360.0;
        self.set_position(
            pos[0].clamp(-3.0e7, 3.0e7),
            pos[1],
            pos[2].clamp(-3.0e7, 3.0e7),
        );
        Some(self.vehicle_move())
    }

    /// The client's `vehicle_move` packet for this mount.
    #[must_use]
    pub fn vehicle_move(&self) -> Packet {
        Packet::MoveVehicle {
            pos: self.pos,
            yaw: self.yaw,
            pitch: self.pitch,
            on_ground: self.on_ground,
        }
    }

    /// The server's entity teleport (`onEntityPosition`): position, angle bytes, on ground. A
    /// controlled mount only records the server's position.
    pub fn server_teleport(&mut self, pos: [f64; 3], yaw: i8, pitch: i8, on_ground: bool) {
        self.tracked = pos;
        if !self.controlled() {
            let yaw = f32::from(yaw) * 360.0 / 256.0;
            let pitch = f32::from(pitch) * 360.0 / 256.0;
            self.retarget(pos, yaw, pitch);
            self.on_ground = on_ground;
        }
    }

    /// The server's relative move, look or move-look (`onEntityUpdate`): `delta` in 1/4096 blocks
    /// (`None`: a look only), `rotation` the angle bytes when the packet has them. Ignored for a
    /// controlled mount.
    pub fn server_update(
        &mut self,
        delta: Option<[i16; 3]>,
        rotation: Option<(i8, i8)>,
        on_ground: bool,
    ) {
        if self.controlled() {
            return;
        }
        let angles =
            |(y, p): (i8, i8)| (f32::from(y) * 360.0 / 256.0, f32::from(p) * 360.0 / 256.0);
        match delta {
            Some(d) => {
                if d != [0; 3] {
                    for k in 0..3 {
                        if d[k] != 0 {
                            self.tracked[k] =
                                (encode(self.tracked[k]) + i64::from(d[k])) as f64 / 4096.0;
                        }
                    }
                }
                let (yaw, pitch) = rotation.map_or((self.yaw, self.pitch), angles);
                self.retarget(self.tracked, yaw, pitch);
            }
            None => {
                if let Some(r) = rotation {
                    let (yaw, pitch) = angles(r);
                    self.retarget(self.pos, yaw, pitch);
                }
            }
        }
        self.on_ground = on_ground;
    }

    /// `LivingEntity.updateTrackedPositionAndAngles` with 3 steps.
    fn retarget(&mut self, target: [f64; 3], yaw: f32, pitch: f32) {
        self.target = target;
        self.target_yaw = f64::from(yaw);
        self.target_pitch = f64::from(pitch);
        self.steps = 3;
    }

    /// `HorseBaseEntity.setJumpStrength`: the rider released the jump bar at `strength` (×100).
    pub fn set_jump_strength(&mut self, strength: i32) {
        if !self.kind.is_horse_family() || !self.saddled {
            return;
        }
        if strength >= 0 {
            self.horse.jumping = true;
            // updateAnger: the client moves the mount, so it rears.
            if self.controlled() {
                self.horse.angry_ticks = 1;
                self.horse.angry = true;
            }
        }
        let strength = strength.max(0);
        self.horse.jump_strength = if strength >= 90 {
            1.0
        } else {
            0.4 + 0.4 * strength as f32 / 90.0
        };
    }

    /// The passenger's position (`updatePassengerPosition` of the player: mounted height offset
    /// plus the player's −0.35).
    #[must_use]
    pub fn passenger_pos(&self) -> [f64; 3] {
        let [x, y, z] = self.pos;
        let (_, h) = self.kind.dimensions();
        let base = f64::from(h) * 0.75;
        match self.kind {
            MountKind::Llama | MountKind::TraderLlama => {
                let r = self.body_yaw * (std::f64::consts::PI / 180.0) as f32;
                let (f, g) = (math::cos(r), math::sin(r));
                let off = f64::from(h) * 0.67;
                [
                    x + f64::from(0.3f32 * g),
                    y + off + -0.35,
                    z - f64::from(0.3f32 * f),
                ]
            }
            MountKind::Pig => [x, y + base + -0.35, z],
            MountKind::Strider => {
                let f = 0.25f32.min(self.limb_distance);
                let g = self.limb_angle;
                let off = f64::from(h) - 0.19 + f64::from(0.12f32 * math::cos(g * 1.5) * 2.0 * f);
                [x, y + off + -0.35, z]
            }
            _ => {
                let off = match self.kind {
                    MountKind::Donkey | MountKind::Mule => base - 0.25,
                    MountKind::SkeletonHorse => base - 0.1875,
                    _ => base,
                };
                let last = self.horse.last_angry_progress;
                if last > 0.0 {
                    let r = self.body_yaw * (std::f64::consts::PI / 180.0) as f32;
                    let (f, g) = (math::sin(r), math::cos(r));
                    let hh = 0.7f32 * last;
                    let i = 0.15f32 * last;
                    [
                        x + f64::from(hh * f),
                        y + off + -0.35 + f64::from(i),
                        z - f64::from(hh * g),
                    ]
                } else {
                    [x, y + off + -0.35, z]
                }
            }
        }
    }
}

/// One client tick while riding a living mount (the module docs list the steps): the mount's
/// tick, the player's passenger tick (with `around` as in [`tick_ext`](super::tick_ext)), the
/// rider's jump bar, the `vehicle_move` of a controlled mount, the seat. Packets in vanilla order:
/// `entity_action` (start riding jump, on a release), `look`, `steer_vehicle`, `vehicle_move`.
///
/// Returns `false` when the player got off (the shift key held in the previous tick): the mount
/// has ticked, the player has not (only its age counted); it then ticks normally in the same tick
/// when it comes after the mount in the client world's entity order, else from the next tick on.
pub fn tick_mount<W: BlockView<State = BlockState>>(
    p: &mut super::PlayerState,
    x: &mut super::PlayerExt,
    mount: &mut Mount,
    around: &super::Surroundings<'_>,
    input: &TickInput,
    world: &W,
    out: &mut Packets,
) -> bool {
    let rider = Rider {
        yaw: input.yaw,
        pitch: input.pitch,
        sideways: p.sideways_speed,
        forward: p.forward_speed,
    };
    mount_tick(mount, world, x.ultrawarm, rider);
    if p.input.sneaking {
        p.age += 1;
        p.yaw = input.yaw;
        p.pitch = input.pitch;
        return false;
    }
    let ride = super::player::RideCtx {
        bb: mount.bb,
        submerged: false,
        boat: false,
        jumping_mount: mount.can_jump(),
    };
    super::player::tick_passenger(p, x, around, ride, input, world, out);
    if let Some(s) = x.take_mount_jump() {
        mount.set_jump_strength(s);
    }
    if mount.controlled() {
        out.push(mount.vehicle_move());
    }
    let [sx, sy, sz] = mount.passenger_pos();
    p.set_position(sx, sy, sz);
    p.fall_distance = 0.0;
    true
}

/// One client tick of a mount the client only watches ([`Mount::watched`]): the same mob tick
/// as under a rider that does not control it (interpolation step or velocity decay, the cut-off,
/// fluid pushes, the walk animation, body turn, rearing), without a rider. `ultrawarm`: the
/// dimension (a strider's lava push). Then call [`Mount::pushed_player`] if the mob touches the
/// player in this tick (`tickCramming`, at the end of the mob's movement tick).
pub fn tick_watched<W: BlockView<State = BlockState>>(m: &mut Mount, world: &W, ultrawarm: bool) {
    let rider = Rider {
        yaw: 0.0,
        pitch: 0.0,
        sideways: 0.0,
        forward: 0.0,
    };
    mount_tick(m, world, ultrawarm, rider);
}

impl Mount {
    /// The mob pushed the local player in its tick (`tickCramming` → `player.pushAwayFrom(mob)`):
    /// `player` is the velocity the player took ([`push_by_with`](super::push_by_with)); the mob
    /// takes the opposite (`addVelocity`; a mob with passengers takes nothing).
    ///
    /// The push comes at the end of the mob's movement tick: a strider's floating in lava
    /// (`updateFloating`, ×0.5 after `super.tick()`) halves it too, which this applies.
    pub fn pushed_player(&mut self, player: [f64; 2]) {
        let f = if self.floated { 0.5 } else { 1.0 };
        self.vel[0] += -player[0] * f;
        self.vel[1] += 0.0;
        self.vel[2] += -player[1] * f;
    }
}

/// The world as a strider's collision sees it: a lava source with no lava above is solid up to
/// half its height for an entity whose feet are above that half (`FluidBlock.getCollisionShape`
/// with `canWalkOnFluid`), here a bottom slab of the same shape.
struct LavaFloor<'a, W> {
    w: &'a W,
    /// The mount walks on lava (a strider).
    walk: bool,
    /// The mount's feet when the collision context was made.
    min_y: f64,
}

impl<W: BlockView<State = BlockState>> BlockView for LavaFloor<'_, W> {
    type State = BlockState;

    #[inline]
    fn block(&self, x: i32, y: i32, z: i32) -> BlockState {
        let s = self.w.block(x, y, z);
        if self.walk
            && s.block() == Block::LAVA
            && fluid(s).is_some_and(|f| f.amount == 8 && !f.falling)
            && self.min_y > f64::from(y) + 0.5 - f64::from(1.0e-5f32)
            && !fluid(state_at(self.w, x, y + 1, z)).is_some_and(|f| f.kind == FluidKind::Lava)
        {
            return Block::SMOOTH_STONE_SLAB.default_state();
        }
        s
    }

    #[inline]
    fn is_chunk_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
        self.w.is_chunk_loaded(chunk_x, chunk_z)
    }

    #[inline]
    fn world_border(&self) -> WorldBorder {
        self.w.world_border()
    }
}

/// The mount's client tick (`MobEntity.tick` and the type's additions).
fn mount_tick<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, ultrawarm: bool, r: Rider) {
    // ClientWorld.tickEntity: resetPosition.
    m.prev = m.pos;
    m.floated = false;
    if m.kind == MountKind::Strider {
        // Warm: lava at the block position or at the landing position, or lava contact.
        let at = state_at(w, floor(m.pos[0]), floor(m.pos[1]), floor(m.pos[2]));
        let landing = landing_state(m, w);
        let warm =
            at.block() == Block::LAVA || landing.block() == Block::LAVA || m.lava_height > 0.0;
        m.cold = !warm;
    }
    base_tick(m, w, ultrawarm);
    tick_movement(m, w, r);
    body_control(m);
    if m.kind.is_horse_family() {
        horse_tick(m);
    }
    if m.kind == MountKind::Strider {
        // updateFloating, then checkBlockCollision.
        if in_lava(m) {
            let bp = [floor(m.pos[0]), floor(m.pos[1]), floor(m.pos[2])];
            let above = m.pos[1] > f64::from(bp[1]) + 0.5 - f64::from(1.0e-5f32);
            let lava_up = fluid(state_at(w, bp[0], bp[1] + 1, bp[2]))
                .is_some_and(|f| f.kind == FluidKind::Lava);
            m.floated = !(above && !lava_up);
            if above && !lava_up {
                m.on_ground = true;
            } else {
                m.vel = [m.vel[0] * 0.5, m.vel[1] * 0.5 + 0.05, m.vel[2] * 0.5];
            }
        }
        block_contacts(m, w);
    }
}

fn in_lava(m: &Mount) -> bool {
    !m.first_update && m.lava_height > 0.0
}

/// `Entity.getLandingPos` state: 0.2 below the feet, or the fence / wall / gate under air.
fn landing_state<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> BlockState {
    let (lx, ly, lz) = (
        floor(m.pos[0]),
        floor(m.pos[1] - f64::from(0.2f32)),
        floor(m.pos[2]),
    );
    let s = state_at(w, lx, ly, lz);
    if s.is_air() {
        let below = state_at(w, lx, ly - 1, lz);
        let f = flags(below);
        if f.contains(PhysicsFlags::FENCE)
            || f.contains(PhysicsFlags::WALL)
            || f.contains(PhysicsFlags::FENCE_GATE)
        {
            return below;
        }
    }
    s
}

/// `Entity.checkWaterState` for a mob: contact with water and its push.
fn check_water<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W) {
    let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
    let c = update_in_fluid_as::<W, false>(w, &m.bb, FluidKind::Water, 0.014, true, &mut v);
    m.vel = [v.x, v.y, v.z];
    m.water_height = c.height;
    if c.touching {
        m.fall_distance = 0.0;
    }
    m.touching_water = c.touching;
}

/// `Entity.baseTick` (the movement parts).
fn base_tick<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, ultrawarm: bool) {
    m.water_height = 0.0;
    m.lava_height = 0.0;
    check_water(m, w);
    let speed = if ultrawarm {
        0.007
    } else {
        0.002_333_333_333_333_333_5
    };
    let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
    let c = update_in_fluid_as::<W, false>(w, &m.bb, FluidKind::Lava, speed, true, &mut v);
    m.vel = [v.x, v.y, v.z];
    m.lava_height = c.height;
    if in_lava(m) {
        m.fall_distance *= 0.5;
    }
    m.first_update = false;
}

/// `LivingEntity.tickMovement` of the mount on the client.
fn tick_movement<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, r: Rider) {
    if m.controlled() {
        m.steps = 0;
        m.tracked = m.pos;
    }
    if m.steps > 0 {
        let n = f64::from(m.steps);
        let x = m.pos[0] + (m.target[0] - m.pos[0]) / n;
        let y = m.pos[1] + (m.target[1] - m.pos[1]) / n;
        let z = m.pos[2] + (m.target[2] - m.pos[2]) / n;
        let g = wrap_degrees(m.target_yaw - f64::from(m.yaw));
        m.yaw = (f64::from(m.yaw) + g / n) as f32;
        m.pitch = (f64::from(m.pitch) + (m.target_pitch - f64::from(m.pitch)) / n) as f32;
        m.steps -= 1;
        m.set_position(x, y, z);
        m.yaw %= 360.0;
        m.pitch %= 360.0;
    } else {
        // The client never moves a mob voluntarily.
        m.vel = [m.vel[0] * 0.98, m.vel[1] * 0.98, m.vel[2] * 0.98];
    }
    if m.head_steps > 0 {
        let d = wrap_degrees(m.head_target - f64::from(m.head_yaw));
        m.head_yaw = (f64::from(m.head_yaw) + d / f64::from(m.head_steps)) as f32;
        m.head_steps -= 1;
    }
    for v in &mut m.vel {
        if v.abs() < 0.003 {
            *v = 0.0;
        }
    }
    travel(m, w, r);
}

/// The type's `travel` with the mount's own (zero) input.
fn travel<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, r: Rider) {
    match m.kind {
        MountKind::Pig | MountKind::Strider => {
            if m.kind == MountKind::Strider {
                m.movement_speed = strider_speed(m);
            }
            if m.controlled() {
                m.yaw = r.yaw;
                m.pitch = r.pitch * 0.5;
                m.yaw %= 360.0;
                m.pitch %= 360.0;
                m.body_yaw = m.yaw;
                m.head_yaw = m.yaw;
                m.step_height = 1.0;
                m.flying_speed = m.movement_speed * 0.1;
                if m.boost.boosted {
                    let t = m.boost.ticks;
                    m.boost.ticks += 1;
                    if t > m.boost.time {
                        m.boost.boosted = false;
                    }
                }
                let mut f = saddled_speed(m);
                if m.boost.boosted {
                    let k = m.boost.ticks as f32 / m.boost.time as f32 * std::f32::consts::PI;
                    f += f * 1.15 * math::sin(k);
                }
                m.movement_speed = f;
                living_travel(m, w, V3::new(0.0, 0.0, 1.0));
                m.steps = 0;
            } else {
                m.step_height = 0.5;
                m.flying_speed = 0.02;
                living_travel(m, w, V3::ZERO);
            }
        }
        _ if m.passenger && m.saddled && !m.kind.is_llama() => {
            m.yaw = r.yaw;
            m.pitch = r.pitch * 0.5;
            m.yaw %= 360.0;
            m.pitch %= 360.0;
            m.body_yaw = m.yaw;
            m.head_yaw = m.body_yaw;
            let mut f = r.sideways * 0.5;
            let mut g = r.forward;
            if g <= 0.0 {
                g *= 0.25;
            }
            let h = m.horse;
            if m.on_ground && h.jump_strength == 0.0 && h.angry && !h.jumping {
                f = 0.0;
                g = 0.0;
            }
            if h.jump_strength > 0.0 && !h.in_air && m.on_ground {
                let d = m.jump_attribute
                    * f64::from(h.jump_strength)
                    * f64::from(jump_velocity_multiplier(m, w));
                m.vel[1] = d;
                m.horse.in_air = true;
                if g > 0.0 {
                    let rad = m.yaw * (std::f64::consts::PI / 180.0) as f32;
                    let (i, j) = (math::sin(rad), math::cos(rad));
                    m.vel[0] += f64::from(-0.4 * i * h.jump_strength);
                    m.vel[2] += f64::from(0.4 * j * h.jump_strength);
                }
                m.horse.jump_strength = 0.0;
            }
            m.flying_speed = m.movement_speed * 0.1;
            m.movement_speed = m.speed_attribute as f32;
            living_travel(m, w, V3::new(f64::from(f), 0.0, f64::from(g)));
            if m.on_ground {
                m.horse.jump_strength = 0.0;
                m.horse.in_air = false;
            }
            limbs(m);
        }
        _ => {
            m.flying_speed = 0.02;
            living_travel(m, w, V3::ZERO);
        }
    }
}

fn strider_speed(m: &Mount) -> f32 {
    m.speed_attribute as f32 * if m.cold { 0.66 } else { 1.0 }
}

fn saddled_speed(m: &Mount) -> f32 {
    if m.kind == MountKind::Pig {
        m.speed_attribute as f32 * 0.225
    } else {
        m.speed_attribute as f32 * if m.cold { 0.23 } else { 0.55 }
    }
}

/// `Entity.getJumpVelocityMultiplier`.
fn jump_velocity_multiplier<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> f32 {
    let at = jump_multiplier(state_at(
        w,
        floor(m.pos[0]),
        floor(m.pos[1]),
        floor(m.pos[2]),
    ));
    if at == 1.0 {
        let [x, y, z] = velocity_pos(m);
        jump_multiplier(state_at(w, x, y, z))
    } else {
        at
    }
}

/// `getVelocityAffectingPos`.
fn velocity_pos(m: &Mount) -> [i32; 3] {
    [
        floor(m.pos[0]),
        floor(m.bb.min_y - 0.500_000_1),
        floor(m.pos[2]),
    ]
}

/// `LivingEntity.method_29242`: the walk animation from the move since the tick started.
fn limbs(m: &mut Mount) {
    let dx = m.pos[0] - m.prev[0];
    let dz = m.pos[2] - m.prev[2];
    let mut g = ((dx * dx + 0.0 * 0.0 + dz * dz).sqrt() as f32) * 4.0;
    if g > 1.0 {
        g = 1.0;
    }
    m.limb_distance += (g - m.limb_distance) * 0.4;
    m.limb_angle += m.limb_distance;
}

fn climbing<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> bool {
    // HorseBaseEntity.isClimbing is false (llamas included).
    !m.kind.is_horse_family() && super::entity::climbing_at(w, m.pos)
}

/// `LivingEntity.travel` on the client: the physics run only for a controlled mount; the walk
/// animation always.
fn living_travel<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, input: V3) {
    if m.controlled() {
        travel_physics(m, w, input);
    }
    limbs(m);
}

fn accelerate(m: &mut Mount, speed: f32, input: V3) {
    let d = input.length_sq();
    if d < 1.0e-7 {
        return;
    }
    let v = if d > 1.0 { input.normalize() } else { input }.scale(f64::from(speed));
    let r = m.yaw * (std::f64::consts::PI / 180.0) as f32;
    let (f, g) = (f64::from(math::sin(r)), f64::from(math::cos(r)));
    m.vel = [
        m.vel[0] + (v.x * g - v.z * f),
        m.vel[1] + v.y,
        m.vel[2] + (v.z * g + v.x * f),
    ];
}

/// `method_26317`: the sink rule in a liquid (a mount never sprints, it has gravity).
fn fluid_fall(gravity: f64, falling: bool, v: V3) -> V3 {
    let y = if falling && (v.y - 0.005).abs() >= 0.003 && (v.y - gravity / 16.0).abs() < 0.003 {
        -0.003
    } else {
        v.y - gravity / 16.0
    };
    V3::new(v.x, y, v.z)
}

fn walks_on<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> bool {
    // canWalkOnFluid of the fluid at the block position: lava for a strider.
    m.kind == MountKind::Strider
        && fluid(state_at(
            w,
            floor(m.pos[0]),
            floor(m.pos[1]),
            floor(m.pos[2]),
        ))
        .is_some_and(|f| f.kind == FluidKind::Lava)
}

fn travel_physics<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, input: V3) {
    let gravity = 0.08;
    let falling = m.vel[1] <= 0.0;
    let walks = walks_on(m, w);
    if m.touching_water && !walks {
        let start_y = m.pos[1];
        let f = m.kind.water_drag();
        accelerate(m, 0.02, input);
        let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
        mount_move(m, w, v);
        let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
        if m.horizontal_collision && climbing(m, w) {
            v.y = 0.2;
        }
        let v = V3::new(
            v.x * f64::from(f),
            v.y * f64::from(0.8f32),
            v.z * f64::from(f),
        );
        let v = fluid_fall(gravity, falling, v);
        m.vel = [v.x, v.y, v.z];
        jump_out(m, w, v, start_y);
    } else if in_lava(m) && !walks {
        let start_y = m.pos[1];
        accelerate(m, 0.02, input);
        let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
        mount_move(m, w, v);
        let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
        // getSwimHeight: every mount's eyes are above 0.4.
        let v = if m.lava_height <= 0.4 {
            fluid_fall(
                gravity,
                falling,
                V3::new(v.x * 0.5, v.y * f64::from(0.8f32), v.z * 0.5),
            )
        } else {
            v.scale(0.5)
        };
        let v = V3::new(v.x, v.y + -gravity / 4.0, v.z);
        m.vel = [v.x, v.y, v.z];
        jump_out(m, w, v, start_y);
    } else {
        let [px, py, pz] = velocity_pos(m);
        let t = slipperiness(state_at(w, px, py, pz));
        let u = if m.on_ground { t * 0.91 } else { 0.91 };
        let speed = if m.on_ground {
            m.movement_speed * (0.216_000_02 / (t * t * t))
        } else {
            m.flying_speed
        };
        accelerate(m, speed, input);
        if climbing(m, w) {
            m.fall_distance = 0.0;
            let lim = f64::from(0.15f32);
            m.vel[0] = m.vel[0].clamp(-lim, lim);
            m.vel[2] = m.vel[2].clamp(-lim, lim);
            m.vel[1] = math::java_max(m.vel[1], -lim);
        }
        let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
        mount_move(m, w, v);
        let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
        if m.horizontal_collision && climbing(m, w) {
            v.y = 0.2;
        }
        let u = f64::from(u);
        m.vel = [v.x * u, (v.y - gravity) * f64::from(0.98f32), v.z * u];
    }
}

fn collider<'a, W: BlockView<State = BlockState>>(
    w: &'a LavaFloor<'a, W>,
    border: &'a BorderShape,
) -> Collider<'a, LavaFloor<'a, W>, NoEntities> {
    Collider {
        w,
        border,
        ctx: ShapeCtx {
            min_y: w.min_y,
            descending: false,
        },
        entities: NoEntities,
    }
}

/// Lava a strider can walk on is solid for it; for other mounts the floor is never taken.
fn floor_view<'a, W: BlockView<State = BlockState>>(m: &Mount, w: &'a W) -> LavaFloor<'a, W> {
    LavaFloor {
        w,
        walk: m.kind == MountKind::Strider,
        min_y: m.pos[1],
    }
}

fn jump_out<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, v: V3, start_y: f64) {
    if !m.horizontal_collision {
        return;
    }
    let dy = v.y + f64::from(0.6f32) - m.pos[1] + start_y;
    let probe = offset(&m.bb, v.x, dy, v.z);
    let fw = floor_view(m, w);
    let border = BorderShape::new(&w.world_border());
    if collider(&fw, &border).is_space_empty(&m.bb, &probe) && !contains_fluid(w, &probe) {
        m.vel[1] = f64::from(0.3f32);
    }
}

fn contains_fluid<W: BlockView<State = BlockState>>(w: &W, b: &Aabb) -> bool {
    let (x0, x1) = (floor(b.min_x), ceil(b.max_x));
    let (y0, y1) = (floor(b.min_y), ceil(b.max_y));
    let (z0, z1) = (floor(b.min_z), ceil(b.max_z));
    (x0..x1).any(|x| (y0..y1).any(|y| (z0..z1).any(|z| fluid(state_at(w, x, y, z)).is_some())))
}

/// `Entity.move(SELF, movement)` for the mount.
fn mount_move<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, movement: V3) {
    let mut mv = movement;
    let mm = m.movement_multiplier;
    if mm[0] * mm[0] + mm[1] * mm[1] + mm[2] * mm[2] > 1.0e-7 {
        mv = V3::new(mv.x * mm[0], mv.y * mm[1], mv.z * mm[2]);
        m.movement_multiplier = [0.0; 3];
        m.vel = [0.0; 3];
    }
    let fw = floor_view(m, w);
    let border = BorderShape::new(&w.world_border());
    let v = collider(&fw, &border).adjust(mv, &m.bb, m.on_ground, m.step_height);
    if v.length_sq() > 1.0e-7 {
        m.bb = offset(&m.bb, v.x, v.y, v.z);
        m.pos = [
            (m.bb.min_x + m.bb.max_x) / 2.0,
            m.bb.min_y,
            (m.bb.min_z + m.bb.max_z) / 2.0,
        ];
    }
    let approx = |a: f64, b: f64| (b - a).abs() < f64::from(1.0e-5f32);
    m.horizontal_collision = !approx(mv.x, v.x) || !approx(mv.z, v.z);
    m.vertical_collision = mv.y != v.y;
    m.on_ground = m.vertical_collision && mv.y < 0.0;
    let landed = landing_state(m, w);
    // fall(): a strider checks its blocks first and forgets falls in lava; others re-check water.
    let skip = if m.kind == MountKind::Strider {
        block_contacts(m, w);
        if in_lava(m) {
            m.fall_distance = 0.0;
            true
        } else {
            false
        }
    } else {
        false
    };
    if !skip {
        if !m.touching_water {
            check_water(m, w);
        }
        if m.on_ground {
            m.fall_distance = 0.0;
        } else if v.y < 0.0 {
            m.fall_distance = (f64::from(m.fall_distance) - v.y) as f32;
        }
    }
    let old = m.vel;
    if mv.x != v.x {
        m.vel = [0.0, old[1], old[2]];
    }
    if mv.z != v.z {
        m.vel = [old[0], old[1], 0.0];
    }
    let block = landed.block();
    if mv.y != v.y {
        if block == Block::SLIME_BLOCK || is_bed(landed) {
            if m.vel[1] < 0.0 {
                let k = if block == Block::SLIME_BLOCK {
                    1.0
                } else {
                    f64::from(0.66f32)
                };
                m.vel[1] = -m.vel[1] * k;
            }
        } else {
            m.vel[1] *= 0.0;
        }
    }
    if m.on_ground && block == Block::SLIME_BLOCK {
        let d = m.vel[1].abs();
        if d < 0.1 {
            let e = 0.4 + d * 0.2;
            m.vel[0] *= e;
            m.vel[2] *= e;
        }
    }
    block_contacts(m, w);
    let k = f64::from(mount_velocity_multiplier(m, w));
    m.vel[0] *= k;
    m.vel[2] *= k;
}

/// `Entity.getVelocityMultiplier`.
fn mount_velocity_multiplier<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> f32 {
    let here = state_at(w, floor(m.pos[0]), floor(m.pos[1]), floor(m.pos[2]));
    let f = velocity_multiplier(here);
    if here.block() == Block::WATER || here.block() == Block::BUBBLE_COLUMN || f != 1.0 {
        f
    } else {
        let [x, y, z] = velocity_pos(m);
        velocity_multiplier(state_at(w, x, y, z))
    }
}

/// `Entity.checkBlockCollision`: the blocks inside the box act on the mount.
fn block_contacts<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W) {
    let b = m.bb;
    let (x0, y0, z0) = (
        floor(b.min_x + 0.001),
        floor(b.min_y + 0.001),
        floor(b.min_z + 0.001),
    );
    let (x1, y1, z1) = (
        floor(b.max_x - 0.001),
        floor(b.max_y - 0.001),
        floor(b.max_z - 0.001),
    );
    if !region_loaded(w, x0, y0, z0, x1, y1, z1) {
        return;
    }
    let (width, _) = m.kind.dimensions();
    for x in x0..=x1 {
        for y in y0..=y1 {
            for z in z0..=z1 {
                let s = state_at(w, x, y, z);
                let block = s.block();
                if block == Block::COBWEB {
                    m.fall_distance = 0.0;
                    m.movement_multiplier = [0.25, f64::from(0.05f32), 0.25];
                } else if block == Block::SWEET_BERRY_BUSH {
                    m.fall_distance = 0.0;
                    m.movement_multiplier = [f64::from(0.8f32), 0.75, f64::from(0.8f32)];
                } else if block == Block::HONEY_BLOCK {
                    let sliding = !m.on_ground
                        && !(m.pos[1] > f64::from(y) + 0.9375 - 1.0e-7)
                        && !(m.vel[1] >= -0.08)
                        && {
                            let d = (f64::from(x) + 0.5 - m.pos[0]).abs();
                            let e = (f64::from(z) + 0.5 - m.pos[2]).abs();
                            let f = 0.4375 + f64::from(width / 2.0);
                            d + 1.0e-7 > f || e + 1.0e-7 > f
                        };
                    if sliding {
                        if m.vel[1] < -0.13 {
                            let d = -0.05 / m.vel[1];
                            m.vel = [m.vel[0] * d, -0.05, m.vel[2] * d];
                        } else {
                            m.vel[1] = -0.05;
                        }
                        m.fall_distance = 0.0;
                    }
                } else if block == Block::BUBBLE_COLUMN {
                    let drag = s.get(ghast_data::v1_16_5::Property::Drag) == Some("true");
                    let v = m.vel[1];
                    if state_at(w, x, y + 1, z).is_air() {
                        m.vel[1] = if drag {
                            math::java_max(-0.9, v - 0.03)
                        } else {
                            math::java_min(1.8, v + 0.1)
                        };
                    } else {
                        m.vel[1] = if drag {
                            math::java_max(-0.3, v - 0.03)
                        } else {
                            math::java_min(0.7, v + 0.06)
                        };
                        m.fall_distance = 0.0;
                    }
                }
            }
        }
    }
}

/// `MobEntity.turnHead` → `BodyControl.tick` (body speed 75).
fn body_control(m: &mut Mount) {
    let dx = m.pos[0] - m.prev[0];
    let dz = m.pos[2] - m.prev[2];
    if dx * dx + dz * dz > f64::from(2.500_000_3e-7f32) {
        m.body_yaw = m.yaw;
        m.head_yaw = step_angle(m.head_yaw, m.body_yaw, 75.0);
        m.last_head_yaw = m.head_yaw;
        m.body_ticks = 0;
    } else if (m.head_yaw - m.last_head_yaw).abs() > 15.0 {
        // The rider is a player: the body turns on its own.
        m.body_ticks = 0;
        m.last_head_yaw = m.head_yaw;
        m.body_yaw = step_angle(m.body_yaw, m.head_yaw, 75.0);
    } else {
        m.body_ticks += 1;
        if m.body_ticks > 10 {
            let i = m.body_ticks - 10;
            let f = (i as f32 / 10.0).clamp(0.0, 1.0);
            let g = 75.0 * (1.0 - f);
            m.body_yaw = step_angle(m.body_yaw, m.head_yaw, g);
        }
    }
}

/// The rearing part of `HorseBaseEntity.tick`.
fn horse_tick(m: &mut Mount) {
    let controlled = m.controlled();
    let h = &mut m.horse;
    if controlled && h.angry_ticks > 0 {
        h.angry_ticks += 1;
        if h.angry_ticks > 20 {
            h.angry_ticks = 0;
            h.angry = false;
        }
    }
    h.last_angry_progress = h.angry_progress;
    if h.angry {
        h.angry_progress += (1.0 - h.angry_progress) * 0.4 + 0.05;
        if h.angry_progress > 1.0 {
            h.angry_progress = 1.0;
        }
    } else {
        h.jumping = false;
        let a = h.angry_progress;
        h.angry_progress += (0.8 * a * a * a - a) * 0.6 - 0.05;
        if h.angry_progress < 0.0 {
            h.angry_progress = 0.0;
        }
    }
}