ghast-physics 0.5.5

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
//! Boats the local player controls, as the 26.1 client ticks them (the boat is then
//! client-authoritative: the client moves it and tells the server where it is).
//!
//! # A tick while riding, in vanilla order
//!
//! 1. Mouse turning (before the tick): the player's rotation, clamped to 105 degrees either side of
//!    the boat's (`AbstractBoat.onPassengerTurned`).
//! 2. The boat's tick (`ClientLevel.tickNonPassenger`): its status (in water, under water, under
//!    flowing water, on land, in the air), the vanilla entity base tick (fluid pushing), floating
//!    and paddling with the keys the player held in the PREVIOUS tick, the `steer_boat` packet, its
//!    move (block and entity collisions, no step-up) and its block effects (twice, as vanilla).
//! 3. The passenger's tick (`tickPassenger` -> `rideTick`): its motion is zeroed, then the whole
//!    player tick runs as a passenger (no sprinting, no crouching, no flying toggle, its fluid box
//!    cut at the boat's top) and sends `player_input` (when the keys changed), a rotation-only
//!    movement packet with its own ground / collision flags, `vehicle_move` with the boat's
//!    position, rotation and ground flag, then a sprint packet if its sprint state changed.
//! 4. The boat places its passenger (`positionRider`): on the boat's attachment point, turned by
//!    the boat's turn of this tick and clamped again; the fall distance is reset.
//! 5. The player's keys of this tick become the boat's paddle input (`LocalPlayer.rideTick`).
//!
//! [`tick_boat`] runs all five. Server packets: `set_passengers` naming the player puts it on
//! the boat ([`Boat::board`]), one without it takes it off (nothing moves on the client; the
//! server then teleports the player); `move_vehicle` is a correction ([`Boat::server_move`],
//! which also gives the packet the client answers with). Sneaking does not dismount on the client:
//! the shift key goes to the server in `player_input`.

use super::collide::{Border, Ents, Vehicle, World};
use super::ext::PlayerExt;
use super::fluid::{amount, kind};
use super::math::{self as m, DEG_TO_RAD, V3, ceil, floor};
use super::player::{Ctx, Glide, PlayerState, Pose, Ride, tick_core};
use super::rules::Rules;
use crate::math::Aabb;
use crate::world::{BlockView, TickCache};
use crate::{Packet, Packets, TickInput};

/// The kind of a boat (its ride height and passenger seat).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum BoatKind {
    /// A boat (`*_boat`).
    Boat,
    /// A chest boat (`*_chest_boat`): one passenger, seated 0.15 forward.
    ChestBoat,
    /// A raft (`bamboo_raft`): a higher seat.
    Raft,
    /// A chest raft (`bamboo_chest_raft`).
    ChestRaft,
}

impl BoatKind {
    /// The kind of a 26.1 entity type name (`oak_boat`, `birch_chest_boat`, `bamboo_raft`, ...);
    /// `None` for anything that is not a boat.
    #[must_use]
    pub fn from_entity_name(name: &str) -> Option<BoatKind> {
        let name = name.strip_prefix("minecraft:").unwrap_or(name);
        if name == "bamboo_raft" {
            Some(BoatKind::Raft)
        } else if name == "bamboo_chest_raft" {
            Some(BoatKind::ChestRaft)
        } else if name.ends_with("_chest_boat") {
            Some(BoatKind::ChestBoat)
        } else if name.ends_with("_boat") {
            Some(BoatKind::Boat)
        } else {
            None
        }
    }

    /// `rideHeight(dimensions)`: a third of the height, or 8/9 of it for rafts.
    fn ride_height(self) -> f64 {
        match self {
            BoatKind::Boat | BoatKind::ChestBoat => f64::from(HEIGHT / 3.0),
            BoatKind::Raft | BoatKind::ChestRaft => f64::from(HEIGHT * 0.888_888_9),
        }
    }

    /// `getSinglePassengerXOffset`.
    fn seat_offset(self) -> f32 {
        match self {
            BoatKind::ChestBoat | BoatKind::ChestRaft => 0.15,
            BoatKind::Boat | BoatKind::Raft => 0.0,
        }
    }
}

/// The boat's status of a tick (`AbstractBoat.Status`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum BoatStatus {
    /// Floating on water.
    InWater,
    /// Under still water (the top of the box is in water).
    UnderWater,
    /// Under flowing water.
    UnderFlowingWater,
    /// On a block (land friction).
    OnLand,
    /// In the air.
    InAir,
}

const WIDTH: f32 = 1.375;
const HEIGHT: f32 = 0.5625;

/// A boat the player rides and controls. Create it from the add-entity packet ([`Boat::new`]).
#[derive(Clone, Debug)]
pub struct Boat {
    kind: BoatKind,
    /// The generic entity state (position, box, motion, ground and collision flags, fall
    /// distance, fluids).
    e: PlayerState,
    s: BoatState,
}

/// The boat's own fields (`AbstractBoat`).
#[derive(Clone, Copy, Debug, Default)]
struct BoatState {
    status: Option<BoatStatus>,
    old_status: Option<BoatStatus>,
    out_of_control: f32,
    delta_rotation: f32,
    water_level: f64,
    land_friction: f32,
    last_yd: f64,
    /// Paddle input: left, right, up (forward), down (back).
    input: [bool; 4],
    paddle: [bool; 2],
    /// `paddlePositions` (the paddle sound check re-reads the status, which resets the water level
    /// and the land friction).
    paddle_pos: [f32; 2],
    /// The player's index among the passengers and their number (`set_passengers`).
    seat: u8,
    passengers: u8,
    /// The first passenger is not a player (26.3: then nobody steers the boat; the paddles show
    /// as still, `getPaddleState`).
    front_not_player: bool,
    /// Nobody steers the boat (`getControllingPassenger() == null`).
    no_controller: bool,
    /// The entity tracker's interpolated position and rotation of a boat the player does not
    /// steer (applied after the base tick, where `AbstractBoat.tick` interpolates).
    follow: Option<([f64; 3], f32, f32)>,
    /// The interpolation target while a steered boat still interpolates (the `vehicle_move`).
    send_target: Option<([f64; 3], f32, f32)>,
}

impl Boat {
    /// A boat as the add-entity packet creates it: at `pos` with the packet's rotation (`yaw`,
    /// `pitch` as decoded from the packet's angle bytes).
    #[must_use]
    pub fn new(kind: BoatKind, pos: [f64; 3], yaw: f32, pitch: f32) -> Boat {
        let mut e = PlayerState::new(pos, yaw % 360.0, (pitch % 360.0).clamp(-90.0, 90.0));
        e.dims = super::player::Dimensions {
            width: WIDTH,
            height: HEIGHT,
            eye_height: HEIGHT,
        };
        e.set_pos(pos[0], pos[1], pos[2]);
        e.pose = Pose::Standing;
        e.on_ground = false;
        Boat {
            kind,
            e,
            s: BoatState::default(),
        }
    }

    /// The kind.
    #[must_use]
    pub fn kind(&self) -> BoatKind {
        self.kind
    }

    /// Position (feet).
    #[must_use]
    pub fn pos(&self) -> [f64; 3] {
        self.e.pos
    }

    /// Yaw.
    #[must_use]
    pub fn yaw(&self) -> f32 {
        self.e.yaw
    }

    /// Pitch.
    #[must_use]
    pub fn pitch(&self) -> f32 {
        self.e.pitch
    }

    /// 26.3's bubble column entity event for the boat the player controls (event 71: a
    /// downward column, 72: an upward one; `AbstractBoat.handleBubbleColumnEffect`, run where
    /// the client simulates the boat): 0.7 blocks per tick down added, or the vertical motion set
    /// to 0.6. Earlier versions have no such event (the server moved the boat). Covered by the
    /// cached `boats-bubble` oracle fuzz only (no golden file).
    pub fn bubble_column_event(&mut self, drag_down: bool) {
        let [x, y, z] = self.e.vel;
        self.e.vel = if drag_down {
            [x + 0.0, y + -0.7, z + 0.0]
        } else {
            [x, 0.6, z]
        };
    }

    /// Motion.
    #[must_use]
    pub fn vel(&self) -> [f64; 3] {
        self.e.vel
    }

    /// The box.
    #[must_use]
    pub fn bounding_box(&self) -> Aabb {
        self.e.bb
    }

    /// On the ground.
    #[must_use]
    pub fn on_ground(&self) -> bool {
        self.e.on_ground
    }

    /// Horizontal collision of the last move.
    #[must_use]
    pub fn horizontal_collision(&self) -> bool {
        self.e.horizontal_collision
    }

    /// Fall distance.
    #[must_use]
    pub fn fall_distance(&self) -> f64 {
        self.e.fall_distance
    }

    /// The status of the last tick (`None` before the first).
    #[must_use]
    pub fn status(&self) -> Option<BoatStatus> {
        self.s.status
    }

    /// Vertical collision of the last move.
    #[must_use]
    pub fn vertical_collision(&self) -> bool {
        self.e.vertical_collision
    }

    /// The status of the tick before the last.
    #[must_use]
    pub fn old_status(&self) -> Option<BoatStatus> {
        self.s.old_status
    }

    /// Ticks spent under water in a row (`outOfControlTicks`; the server ejects at 60).
    #[must_use]
    pub fn out_of_control_ticks(&self) -> f32 {
        self.s.out_of_control
    }

    /// The water level of the last status check (`waterLevel`).
    #[must_use]
    pub fn water_level(&self) -> f64 {
        self.s.water_level
    }

    /// The ground friction found on land (`landFriction`, halved each tick while paddled).
    #[must_use]
    pub fn land_friction(&self) -> f32 {
        self.s.land_friction
    }

    /// The vertical motion of the last move (`lastYd`).
    #[must_use]
    pub fn last_yd(&self) -> f64 {
        self.s.last_yd
    }

    /// The paddle input for the next tick: left, right, forward, back.
    #[must_use]
    pub fn input(&self) -> [bool; 4] {
        self.s.input
    }

    /// The turn speed (`deltaRotation`).
    #[must_use]
    pub fn delta_rotation(&self) -> f32 {
        self.s.delta_rotation
    }

    /// The paddle states of the last tick (left, right).
    #[must_use]
    pub fn paddles(&self) -> [bool; 2] {
        if self.s.no_controller {
            [false; 2]
        } else {
            self.s.paddle
        }
    }

    /// `isUnderWater()` (the status).
    #[must_use]
    pub fn is_under_water(&self) -> bool {
        matches!(
            self.s.status,
            Some(BoatStatus::UnderWater | BoatStatus::UnderFlowingWater)
        )
    }

    /// The server's `set_passengers` put the player on this boat (`startRiding`): standing pose,
    /// the player turned to the boat's yaw.
    pub fn board(&self, p: &mut PlayerState) {
        if p.pose != Pose::Standing {
            p.pose = Pose::Standing;
            p.dims = Pose::Standing.scaled(p.scale());
            let [x, y, z] = p.pos;
            p.set_pos(x, y, z);
        }
        p.yaw = self.e.yaw;
    }

    /// The player's index among the boat's passengers and their number (`set_passengers`
    /// order). The first passenger steers the boat (`getControllingPassenger`: the first living
    /// one); with two the front one sits 0.2 forward and the one behind 0.6 back.
    pub fn set_passengers(&mut self, index: usize, count: usize) {
        self.s.seat = index.min(usize::from(u8::MAX)) as u8;
        self.s.passengers = count.min(usize::from(u8::MAX)) as u8;
    }

    /// The player's index among the passengers.
    #[must_use]
    pub fn seat(&self) -> usize {
        usize::from(self.s.seat)
    }

    /// The number of passengers (0 before any `set_passengers`).
    #[must_use]
    pub fn passenger_count(&self) -> usize {
        usize::from(self.s.passengers)
    }

    /// The first passenger, when it is not the player, is no player either (a mob, an armor
    /// stand): since 26.3 only a player steers a boat (`getControllingPassenger`), so nobody does
    /// then; before, any living passenger in front did (on its own client). Only the paddle
    /// state differs here (the boat is moved by the server either way).
    pub fn set_front_not_player(&mut self, not_player: bool) {
        self.s.front_not_player = not_player;
    }

    /// The player steers the boat (it is the first passenger): the client moves it.
    #[must_use]
    pub fn controlled(&self) -> bool {
        self.s.seat == 0
    }

    /// Where the entity tracker has the boat after this tick's interpolation step (the boat's
    /// `InterpolationHandler`); call it before [`tick_boat`]: every tick for a boat someone else
    /// steers, and for one the player steers while an interpolation runs (the step comes before
    /// the paddling; keep the tracker on the client's own moves).
    pub fn follow_tracker(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) {
        self.s.follow = Some((pos, yaw, pitch));
    }

    /// The interpolation target of a boat the player steers while it still interpolates after
    /// this tick's step: this tick's `vehicle_move` carries it (`fromEntity`).
    pub fn set_interpolation_target(&mut self, target: ([f64; 3], f32, f32)) {
        self.s.send_target = Some(target);
    }

    /// A teleport-entity packet for the boat (`setValuesFromPositionPacket`): its motion becomes
    /// the packet's (absolute) delta and its ground flag the packet's (the position goes through
    /// the tracker's interpolation, [`Boat::follow_tracker`]).
    pub fn server_teleport(&mut self, delta: [f64; 3], on_ground: bool) {
        self.e.vel = delta;
        self.e.on_ground = on_ground;
    }

    /// The state of the client copy of a boat the client watched before the player boarded it:
    /// its motion (a `set_entity_motion` or teleport delta since its last tick: an unridden boat's
    /// client tick zeroes it; ghast-entity's `Entities::client_motion`) and the last position
    /// packet's ground flag.
    pub fn set_watched_state(&mut self, vel: [f64; 3], on_ground: bool) {
        self.e.vel = vel;
        self.e.on_ground = on_ground;
    }

    /// A `set_entity_motion` packet for the boat the player rides (`lerpMotion`: its motion,
    /// also while the player paddles it; an unsteered boat's tick zeroes it).
    pub fn server_motion(&mut self, vel: [f64; 3]) {
        self.e.vel = vel;
    }

    /// A relative move or position sync packet for a boat the player does not steer: its ground
    /// flag.
    pub fn server_on_ground(&mut self, on_ground: bool) {
        self.e.on_ground = on_ground;
    }

    /// A teleport-entity packet too far to interpolate (more than 64 blocks from where the boat
    /// is): the boat is put there at once, before its next tick.
    pub fn server_snap(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) {
        self.e.set_pos(pos[0], pos[1], pos[2]);
        if yaw.is_finite() {
            self.e.yaw = yaw;
        }
        if pitch.is_finite() {
            self.e.pitch = pitch.clamp(-90.0, 90.0);
        }
        self.s.follow = None;
    }

    /// `positionRider` of the player outside the tick (after [`Boat::server_snap`], the
    /// teleport handler places the passenger at once): on its seat, turned and clamped.
    pub fn position_passenger(&self, p: &mut PlayerState) {
        position_rider(self, p);
    }

    /// A `move_vehicle` packet from the server (`handleMoveVehicle`): the boat snaps to it unless
    /// it is within 1e-5; returns the `vehicle_move` packet the client answers with at once.
    pub fn server_move(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) -> Packet {
        let d = V3::of(pos).sub(V3::of(self.e.pos));
        if d.length() > f64::from(1.0e-5f32) {
            // absSnapTo: position, then the rotation (yaw wrapped into 360, pitch clamped).
            self.e.set_pos(pos[0], pos[1], pos[2]);
            if yaw.is_finite() {
                self.e.yaw = yaw % 360.0;
            }
            if pitch.is_finite() {
                self.e.pitch = (pitch % 360.0).clamp(-90.0, 90.0);
            }
        }
        Packet::MoveVehicle {
            pos: self.e.pos,
            yaw: self.e.yaw,
            pitch: self.e.pitch,
            on_ground: self.e.on_ground,
        }
    }
}

/// One client tick of a player riding (and controlling) `boat`: mouse turning, the boat's tick,
/// the passenger's tick and its placing on the boat, in vanilla order (see the module docs).
/// `boat_colliders` are the boxes of the entities the boat collides with (other boats, shulkers,
/// mobs and other pushable entities, not its passengers); `around` is what the player's own tick
/// collides with (its boat excluded).
#[allow(clippy::too_many_arguments)]
pub fn tick_boat<R: Rules, W: BlockView<State = R::State>>(
    boat: &mut Boat,
    p: &mut PlayerState,
    x: &mut PlayerExt,
    around: &super::Surroundings<'_>,
    boat_colliders: &[Aabb],
    input: &TickInput,
    world: &W,
    out: &mut Packets,
) {
    // Mouse turning (Entity.turn -> onPassengerTurned).
    let turned = input.yaw.is_finite() || input.pitch.is_finite();
    if input.yaw.is_finite() {
        p.yaw = input.yaw;
    }
    if input.pitch.is_finite() {
        p.pitch = (input.pitch % 360.0).clamp(-90.0, 90.0);
    }
    if turned {
        clamp_rotation(p, boat.e.yaw);
    }
    let controlled = boat.controlled();
    if controlled {
        boat_tick::<R, W>(boat, boat_colliders, world, out);
    } else {
        boat_tick_moved::<R, W>(boat, boat_colliders, world);
    }
    // tickPassenger -> rideTick: motion zeroed, the player's tick, positionRider.
    p.vel = [0.0; 3];
    let mut g = Glide {
        fall_flying: x.fall_flying,
        glider: x.glider,
        spin_attack: x.spin_attack,
        auto_spin_ticks: x.auto_spin_ticks,
        riding: Some(Ride {
            boat: true,
            controlled,
            top: boat.e.bb.max_y,
            under_water: boat.is_under_water(),
            pos: boat.e.pos,
            yaw: boat.e.yaw,
            pitch: boat.e.pitch,
            on_ground: boat.e.on_ground,
            ..Ride::vehicle(true, controlled, boat.e.pos)
        }),
        last_yd: 0.0,
        ..Glide::with_attributes(x)
    };
    if let Some((pos, yaw, pitch)) = boat.s.send_target.take()
        && let Some(r) = g.riding.as_mut()
    {
        // Still interpolating: the packet carries the interpolation's target.
        r.pos = pos;
        r.yaw = yaw;
        r.pitch = pitch;
    }
    let pending = x.pending_move.take();
    let keep = TickInput {
        yaw: f32::NAN,
        pitch: f32::NAN,
        ..*input
    };
    tick_core::<R, W, Ents<'_>>(p, &mut g, pending, Ents(around.entities), &keep, world, out);
    x.fall_flying = g.fall_flying;
    x.auto_spin_ticks = g.auto_spin_ticks;
    position_rider(boat, p);
    if R::SEND_AFTER_ENTITIES {
        // 26.3 sendChanges runs after the entities ticked: the rotation it sends is the one
        // positionRider left.
        retarget_look(out, (p.yaw, p.pitch));
    }
    // LivingEntity.rideTick
    p.fall_distance = 0.0;
    // LocalPlayer.rideTick: the boat the player steers paddles with these keys in its next tick.
    if controlled {
        let k = p.input.keys;
        boat.s.input = [k.left, k.right, k.forward, k.back];
    }
}

/// A vehicle the player rides but the server moves (a minecart): where it is after its own client
/// tick (the entity tracker's interpolated position) and the height of its seat.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Seat {
    /// The vehicle's position after its tick.
    pub pos: [f64; 3],
    /// The passenger attachment height above the vehicle's position (`EntityType` passenger
    /// attachment: [`Seat::MINECART`] for every minecart).
    pub height: f64,
}

impl Seat {
    /// The passenger attachment height of minecarts (`passengerAttachments(0.1875F)`).
    pub const MINECART: f64 = 0.1875;

    /// The server's `set_passengers` put the player on such a vehicle (`startRiding`): the
    /// standing pose (the rotation is kept).
    pub fn board(p: &mut PlayerState) {
        if p.pose != Pose::Standing {
            p.pose = Pose::Standing;
            p.dims = Pose::Standing.scaled(p.scale());
            let [x, y, z] = p.pos;
            p.set_pos(x, y, z);
        }
    }

    /// A seat at `height` above a vehicle at `pos`.
    #[must_use]
    pub fn new(pos: [f64; 3], height: f64) -> Seat {
        Seat { pos, height }
    }
}

/// One client tick of a player riding a vehicle the server moves (a minecart): mouse turning (no
/// clamp), the passenger's tick (motion zeroed first; packets: `player_input` when the keys changed
/// and a rotation-only movement packet, no `vehicle_move`), then the seat (`positionRider`: the
/// vehicle's position plus the seat height minus the player's 0.6 vehicle attachment) and the
/// fall distance reset. The vehicle ticks before its passenger: pass its position after that tick.
pub fn tick_passenger<R: Rules, W: BlockView<State = R::State>>(
    p: &mut PlayerState,
    x: &mut PlayerExt,
    around: &super::Surroundings<'_>,
    seat: &Seat,
    input: &TickInput,
    world: &W,
    out: &mut Packets,
) {
    if input.yaw.is_finite() {
        p.yaw = input.yaw;
    }
    if input.pitch.is_finite() {
        p.pitch = (input.pitch % 360.0).clamp(-90.0, 90.0);
    }
    p.vel = [0.0; 3];
    let mut g = Glide {
        fall_flying: x.fall_flying,
        glider: x.glider,
        spin_attack: x.spin_attack,
        auto_spin_ticks: x.auto_spin_ticks,
        riding: Some(Ride::vehicle(false, false, seat.pos)),
        last_yd: 0.0,
        ..Glide::with_attributes(x)
    };
    let pending = x.pending_move.take();
    let keep = TickInput {
        yaw: f32::NAN,
        pitch: f32::NAN,
        ..*input
    };
    tick_core::<R, W, Ents<'_>>(p, &mut g, pending, Ents(around.entities), &keep, world, out);
    x.fall_flying = g.fall_flying;
    x.auto_spin_ticks = g.auto_spin_ticks;
    let attach = 0.6 * f64::from(p.scale());
    let y = seat.pos[1] + seat.height;
    p.set_pos(seat.pos[0], y - attach, seat.pos[2]);
    p.fall_distance = 0.0;
}

/// The passenger's rotation packet of this tick (the last rotation-only move) with `look`.
#[cold]
fn retarget_look(out: &mut Packets, look: (f32, f32)) {
    if let Some(Packet::Move {
        pos: None,
        look: l @ Some(_),
        ..
    }) = out.as_mut_slice().iter_mut().rev().find(|p| {
        matches!(
            p,
            Packet::Move {
                pos: None,
                look: Some(_),
                ..
            }
        )
    }) {
        *l = Some(look);
    }
}

/// `Mth.wrapDegrees(float)`.
fn wrap_degrees(v: f32) -> f32 {
    let mut f = v % 360.0;
    if f >= 180.0 {
        f -= 360.0;
    }
    if f < -180.0 {
        f += 360.0;
    }
    f
}

/// `AbstractBoat.clampRotation`: the passenger's yaw within 105 degrees of the boat's.
fn clamp_rotation(p: &mut PlayerState, boat_yaw: f32) {
    let delta = wrap_degrees(p.yaw - boat_yaw);
    let target = if delta < -105.0 {
        -105.0
    } else {
        f32::min(delta, 105.0)
    };
    p.yaw = p.yaw + target - delta;
}

/// `positionRider`: the passenger on the boat's attachment point (minus its own vehicle
/// attachment, 0.6 blocks scaled), turned with the boat, rotation clamped.
fn position_rider(boat: &Boat, p: &mut PlayerState) {
    // AbstractBoat.getPassengerAttachmentPoint: with two passengers 0.2 forward for the first,
    // 0.6 back for the other (a player is not an `Animal`: no extra 0.2).
    let off = f64::from(if boat.s.passengers > 1 {
        if boat.s.seat == 0 { 0.2f32 } else { -0.6 }
    } else {
        boat.kind.seat_offset()
    });
    let a = -boat.e.yaw * DEG_TO_RAD;
    let (c, s) = (
        f64::from(m::cos(f64::from(a))),
        f64::from(m::sin(f64::from(a))),
    );
    // Vec3(0, rideHeight, offset).yRot(a)
    let px = 0.0 * c + off * s;
    let pz = off * c - 0.0 * s;
    let pos = V3::of(boat.e.pos).add(V3::new(px, boat.kind.ride_height(), pz));
    let attach = 0.6 * f64::from(p.scale());
    p.set_pos(pos.x, pos.y - attach, pos.z);
    p.yaw += boat.s.delta_rotation;
    clamp_rotation(p, boat.e.yaw);
}

/// `AbstractBoat.tick` of the controlled boat (`isLocalInstanceAuthoritative`, client side).
fn boat_tick<R: Rules, W: BlockView<State = R::State>>(
    b: &mut Boat,
    colliders: &[Aabb],
    world: &W,
    out: &mut Packets,
) {
    let (e, b) = (&mut b.e, &mut b.s);
    let cache = TickCache::new(world, floor(e.pos[0]), floor(e.pos[1]), floor(e.pos[2]));
    let border = Border::new(&world.world_border());
    let w = World::<R, TickCache<'_, W>, Vehicle<'_>> {
        w: &cache,
        border: &border,
        min_y: e.level.min_y,
        max_y: e.level.min_y + e.level.height - 1,
        ext: Vehicle(colliders),
        _r: core::marker::PhantomData,
    };
    if R::INTERPOLATE_IN_COMMON_TICK
        && let Some((pos, yaw, pitch)) = b.follow.take()
    {
        // 26.3 commonTick: the interpolation step before the boat's tick.
        follow(e, pos, yaw, pitch);
    }
    // ClientLevel.tickNonPassenger: old position, tick count.
    let old_pos = V3::of(e.pos);
    e.age += 1;
    b.old_status = b.status;
    let st = get_status::<R, _>(&w, e, &mut b.water_level, &mut b.land_friction, b.last_yd);
    let status = st;
    b.status = Some(status);
    if matches!(
        status,
        BoatStatus::UnderWater | BoatStatus::UnderFlowingWater
    ) {
        b.out_of_control += 1.0;
    } else {
        b.out_of_control = 0.0;
    }
    let mut t = Ctx {
        p: e,
        x: Glide {
            last_yd: b.last_yd,
            ..Glide::default()
        },
        w: &w,
        old_pos,
        moves: [None; 4],
        using_item: false,
    };
    // Entity.baseTick
    t.p.was_in_powder_snow = t.p.in_powder_snow;
    t.p.in_powder_snow = false;
    t.update_fluid_interaction();
    if t.p.in_lava() {
        t.p.fall_distance *= 0.5;
    }
    if let Some((pos, yaw, pitch)) = b.follow.take() {
        // The interpolation step of a steered boat (after the base tick until 26.2).
        follow(t.p, pos, yaw, pitch);
    }
    b.no_controller = false;
    // floatBoat, controlBoat, the paddle packet, the move.
    float_boat(b, &mut t, status);
    control_boat(b, &mut t);
    out.push(Packet::PaddleBoat {
        left: b.paddle[0],
        right: b.paddle[1],
    });
    let v = V3::of(t.p.vel);
    t.entity_move(v);
    t.apply_effects_from_blocks();
    t.apply_effects_from_blocks();
    b.last_yd = t.x.last_yd;
    // The paddles: where a stroke's sound would play, getPaddleSound() calls getStatus() again.
    let tau = (core::f64::consts::PI * 2.0) as f32;
    let quarter = (core::f64::consts::PI / 4.0) as f32;
    let eighth = (core::f64::consts::PI / 8.0) as f32;
    for i in 0..2 {
        if b.paddle[i] {
            let pp = b.paddle_pos[i];
            if pp % tau <= quarter && (pp + eighth) % tau >= quarter {
                let _ = get_status::<R, _>(&w, t.p, &mut b.water_level, &mut b.land_friction, 0.0);
            }
            b.paddle_pos[i] = pp + eighth;
        } else {
            b.paddle_pos[i] = 0.0;
        }
    }
}

/// `AbstractBoat.tick` of a boat someone else steers (not `isLocalInstanceAuthoritative`): its
/// status, the base tick, the interpolation step (the tracker's position, [`Boat::follow_tracker`]),
/// no motion, its block effects and the paddles (the paddle entity data as it is, someone
/// steers); no packet.
#[cold]
#[inline(never)]
fn boat_tick_moved<R: Rules, W: BlockView<State = R::State>>(
    b: &mut Boat,
    colliders: &[Aabb],
    world: &W,
) {
    let (e, b) = (&mut b.e, &mut b.s);
    let cache = TickCache::new(world, floor(e.pos[0]), floor(e.pos[1]), floor(e.pos[2]));
    let border = Border::new(&world.world_border());
    let w = World::<R, TickCache<'_, W>, Vehicle<'_>> {
        w: &cache,
        border: &border,
        min_y: e.level.min_y,
        max_y: e.level.min_y + e.level.height - 1,
        ext: Vehicle(colliders),
        _r: core::marker::PhantomData,
    };
    if R::INTERPOLATE_IN_COMMON_TICK
        && let Some((pos, yaw, pitch)) = b.follow.take()
    {
        // 26.3 commonTick: the interpolation step before the boat's tick.
        follow(e, pos, yaw, pitch);
    }
    let old_pos = V3::of(e.pos);
    e.age += 1;
    b.old_status = b.status;
    let status = get_status::<R, _>(&w, e, &mut b.water_level, &mut b.land_friction, b.last_yd);
    b.status = Some(status);
    if matches!(
        status,
        BoatStatus::UnderWater | BoatStatus::UnderFlowingWater
    ) {
        b.out_of_control += 1.0;
    } else {
        b.out_of_control = 0.0;
    }
    let mut t = Ctx {
        p: e,
        x: Glide {
            last_yd: b.last_yd,
            ..Glide::default()
        },
        w: &w,
        old_pos,
        moves: [None; 4],
        using_item: false,
    };
    t.p.was_in_powder_snow = t.p.in_powder_snow;
    t.p.in_powder_snow = false;
    t.update_fluid_interaction();
    if t.p.in_lava() {
        t.p.fall_distance *= 0.5;
    }
    if let Some((pos, yaw, pitch)) = b.follow.take() {
        follow(t.p, pos, yaw, pitch);
    }
    t.p.vel = [0.0; 3];
    if R::UNSTEERED_BOAT_STOPS_TURNING {
        b.delta_rotation = 0.0;
    }
    // getPaddleState: the paddle entity data, while someone steers.
    b.no_controller = R::UNSTEERED_BOAT_STOPS_TURNING && b.front_not_player;
    let paddle = if b.no_controller {
        [false; 2]
    } else {
        b.paddle
    };
    t.apply_effects_from_blocks();
    t.apply_effects_from_blocks();
    b.last_yd = t.x.last_yd;
    let tau = (core::f64::consts::PI * 2.0) as f32;
    let quarter = (core::f64::consts::PI / 4.0) as f32;
    let eighth = (core::f64::consts::PI / 8.0) as f32;
    for i in 0..2 {
        if paddle[i] {
            let pp = b.paddle_pos[i];
            if pp % tau <= quarter && (pp + eighth) % tau >= quarter {
                let _ = get_status::<R, _>(&w, t.p, &mut b.water_level, &mut b.land_friction, 0.0);
            }
            b.paddle_pos[i] = pp + eighth;
        } else {
            b.paddle_pos[i] = 0.0;
        }
    }
}

/// The interpolation step's `setPos` / `setRot` with the tracker's values.
fn follow(e: &mut PlayerState, pos: [f64; 3], yaw: f32, pitch: f32) {
    e.set_pos(pos[0], pos[1], pos[2]);
    if yaw.is_finite() {
        e.yaw = yaw;
    }
    if pitch.is_finite() {
        e.pitch = pitch.clamp(-90.0, 90.0);
    }
}

/// `getStatus()` (also sets the water level and the land friction).
fn get_status<R: Rules, W: BlockView<State = R::State>>(
    w: &World<'_, R, W, Vehicle<'_>>,
    e: &PlayerState,
    water_level: &mut f64,
    land_friction: &mut f32,
    _last_yd: f64,
) -> BoatStatus {
    let bb = &e.bb;
    if let Some(s) = under_water(w, bb) {
        *water_level = bb.max_y;
        return s;
    }
    if check_in_water(w, bb, water_level) {
        return BoatStatus::InWater;
    }
    let f = ground_friction(w, bb);
    if f > 0.0 {
        *land_friction = f;
        BoatStatus::OnLand
    } else {
        BoatStatus::InAir
    }
}

/// `isUnderwater()`: water above the top of the box (+0.001); flowing water answers at once.
fn under_water<R: Rules, W: BlockView<State = R::State>>(
    w: &World<'_, R, W, Vehicle<'_>>,
    bb: &Aabb,
) -> Option<BoatStatus> {
    let max_y = bb.max_y + 0.001;
    let (x0, x1) = (floor(bb.min_x), ceil(bb.max_x));
    let (y0, y1) = (floor(bb.max_y), ceil(max_y));
    let (z0, z1) = (floor(bb.min_z), ceil(bb.max_z));
    let mut under = false;
    for x in x0..x1 {
        for y in y0..y1 {
            for z in z0..z1 {
                let f = w.fluid(x, y, z);
                if kind(f) == 1 && max_y < f64::from(y) + f64::from(w.fluid_height(x, y, z, f)) {
                    if !is_source(f) {
                        return Some(BoatStatus::UnderFlowingWater);
                    }
                    under = true;
                }
            }
        }
    }
    under.then_some(BoatStatus::UnderWater)
}

/// `FluidState.isSource()`: amount 8, not falling.
fn is_source(f: u8) -> bool {
    amount(f) == 8 && f & 0x10 == 0
}

/// `checkInWater()`: water at the bottom of the box; the water level is the highest water top.
fn check_in_water<R: Rules, W: BlockView<State = R::State>>(
    w: &World<'_, R, W, Vehicle<'_>>,
    bb: &Aabb,
    water_level: &mut f64,
) -> bool {
    let (x0, x1) = (floor(bb.min_x), ceil(bb.max_x));
    let (y0, y1) = (floor(bb.min_y), ceil(bb.min_y + 0.001));
    let (z0, z1) = (floor(bb.min_z), ceil(bb.max_z));
    let mut in_water = false;
    *water_level = -f64::MAX;
    for x in x0..x1 {
        for y in y0..y1 {
            for z in z0..z1 {
                let f = w.fluid(x, y, z);
                if kind(f) == 1 {
                    let height = y as f32 + w.fluid_height(x, y, z, f);
                    *water_level = m::java_max(f64::from(height), *water_level);
                    in_water |= bb.min_y < f64::from(height);
                }
            }
        }
    }
    in_water
}

/// `getGroundFriction()`: the mean friction of the blocks whose collision shapes touch a 0.001
/// thin box under the boat (lily pads excluded; corner columns and the top / bottom rows of edge
/// columns skipped). NaN when nothing touches.
fn ground_friction<R: Rules, W: BlockView<State = R::State>>(
    w: &World<'_, R, W, Vehicle<'_>>,
    bb: &Aabb,
) -> f32 {
    let q = Aabb {
        min_x: bb.min_x,
        min_y: bb.min_y - 0.001,
        min_z: bb.min_z,
        max_x: bb.max_x,
        max_y: bb.min_y,
        max_z: bb.max_z,
    };
    let (x0, x1) = (floor(q.min_x) - 1, ceil(q.max_x) + 1);
    let (y0, y1) = (floor(q.min_y) - 1, ceil(q.max_y) + 1);
    let (z0, z1) = (floor(q.min_z) - 1, ceil(q.max_z) + 1);
    let mut friction = 0.0f32;
    let mut count = 0i32;
    for x in x0..x1 {
        for z in z0..z1 {
            let edges = i32::from(x == x0 || x == x1 - 1) + i32::from(z == z0 || z == z1 - 1);
            if edges == 2 {
                continue;
            }
            for y in y0..y1 {
                if edges <= 0 || y != y0 && y != y1 - 1 {
                    let s = w.state(x, y, z);
                    if R::block_name(s) != "lily_pad" && w.shape_overlaps(s, x, y, z, &q) {
                        friction += R::friction(s);
                        count += 1;
                    }
                }
            }
        }
    }
    friction / count as f32
}

/// `getWaterLevelAbove()`: the first row above the box top (down to `lastYd` below it) whose
/// water does not fill a block.
fn water_level_above<R: Rules, W: BlockView<State = R::State>>(
    w: &World<'_, R, W, Vehicle<'_>>,
    bb: &Aabb,
    last_yd: f64,
) -> f32 {
    let (x0, x1) = (floor(bb.min_x), ceil(bb.max_x));
    let (y0, y1) = (floor(bb.max_y), ceil(bb.max_y - last_yd));
    let (z0, z1) = (floor(bb.min_z), ceil(bb.max_z));
    'rows: for y in y0..y1 {
        let mut block_height = 0.0f32;
        for x in x0..x1 {
            for z in z0..z1 {
                let f = w.fluid(x, y, z);
                if kind(f) == 1 {
                    block_height = f32::max(block_height, w.fluid_height(x, y, z, f));
                }
                if block_height >= 1.0 {
                    continue 'rows;
                }
            }
        }
        if block_height < 1.0 {
            return y as f32 + block_height;
        }
    }
    (y1 + 1) as f32
}

/// `floatBoat()`.
fn float_boat<R: Rules, W: BlockView<State = R::State>>(
    b: &mut BoatState,
    t: &mut Ctx<'_, '_, R, TickCache<'_, W>, Vehicle<'_>>,
    status: BoatStatus,
) {
    let vspeed = -0.04;
    let mut buoyancy = 0.0;
    let inv_friction: f32;
    if b.old_status == Some(BoatStatus::InAir)
        && status != BoatStatus::InAir
        && status != BoatStatus::OnLand
    {
        b.water_level = t.p.pos[1] + f64::from(HEIGHT);
        let target_y = f64::from(water_level_above(t.w, &t.p.bb, t.x.last_yd) - HEIGHT) + 0.101;
        let q = super::math::mv(&t.p.bb, 0.0, target_y - t.p.pos[1], 0.0);
        let ctx = super::collide::ShapeCtx {
            feet_y: t.p.pos[1],
            descending: false,
            fall_distance: t.p.fall_distance,
            leather_boots: false,
        };
        if t.w.no_collision(t.p.pos, &q, &ctx) {
            let [x, _, z] = t.p.pos;
            t.p.set_pos(x, target_y, z);
            t.p.vel = [t.p.vel[0] * 1.0, t.p.vel[1] * 0.0, t.p.vel[2] * 1.0];
            t.x.last_yd = 0.0;
        }
        b.status = Some(BoatStatus::InWater);
        return;
    }
    let mut vs = vspeed;
    match status {
        BoatStatus::InWater => {
            buoyancy = (b.water_level - t.p.pos[1]) / f64::from(HEIGHT);
            inv_friction = 0.9;
        }
        BoatStatus::UnderFlowingWater => {
            vs = -7.0e-4;
            inv_friction = 0.9;
        }
        BoatStatus::UnderWater => {
            buoyancy = f64::from(0.01f32);
            inv_friction = 0.45;
        }
        BoatStatus::InAir => inv_friction = 0.9,
        BoatStatus::OnLand => {
            inv_friction = b.land_friction;
            // The controlling passenger is a player.
            b.land_friction /= 2.0;
        }
    }
    let v = t.p.vel;
    let k = f64::from(inv_friction);
    t.p.vel = [v[0] * k, v[1] + vs, v[2] * k];
    b.delta_rotation *= inv_friction;
    if buoyancy > 0.0 {
        let v = t.p.vel;
        t.p.vel = [v[0], (v[1] + buoyancy * (0.04 / 0.65)) * 0.75, v[2]];
    }
}

/// `controlBoat()` (the boat has a passenger): turning, acceleration, paddles.
fn control_boat<R: Rules, W: BlockView<State = R::State>>(
    b: &mut BoatState,
    t: &mut Ctx<'_, '_, R, TickCache<'_, W>, Vehicle<'_>>,
) {
    let [left, right, up, down] = b.input;
    let mut acc = 0.0f32;
    if left {
        b.delta_rotation -= 1.0;
    }
    if right {
        b.delta_rotation += 1.0;
    }
    if right != left && !up && !down {
        acc += 0.005;
    }
    t.p.yaw += b.delta_rotation;
    if up {
        acc += 0.04;
    }
    if down {
        acc -= 0.005;
    }
    let yaw = t.p.yaw;
    let dx = m::sin(f64::from(-yaw * DEG_TO_RAD)) * acc;
    let dz = m::cos(f64::from(yaw * DEG_TO_RAD)) * acc;
    let v = V3::of(t.p.vel).add(V3::new(f64::from(dx), 0.0, f64::from(dz)));
    t.p.vel = v.arr();
    b.paddle = [right && !left || up, left && !right || up];
}