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
//! The boat the local player rides, as the 1.12.2 client ticks it (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 (`Entity.increaseTransforms` -> `BoatEntity.onPassengerLookAround` ->
//!    `copyEntityData`).
//! 2. The boat's tick (`BoatEntity.tick`): its location (in water, under water, under flowing
//!    water, on land, in the air), the entity base tick (the water flow push), 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, slime / bed bounces, no stepping effects) and its
//!    block contacts (twice, as vanilla: cobweb, soul sand; a lily pad it touches is broken in the
//!    client's world). A boat with a passenger is never pushed by other entities.
//! 3. The passenger's tick (`World.checkChunk` -> `tickRiding`): its motion is zeroed, the whole
//!    player tick runs as a passenger (no water contact, no item slowdown, no flight travel), then
//!    the client sends a rotation-only movement packet with its own ground flag, `steer_vehicle`
//!    (the input after the tick) and `vehicle_move` with the boat's position and rotation. No
//!    sprint / sneak packets while riding.
//! 4. The boat places its passenger (`updatePassengerPosition`): 0.45 below the boat's position,
//!    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 (`ClientPlayerEntity.tickRiding`).
//!
//! Unlike 1.16 the 1.12 client never dismounts by itself: the sneak key only reaches the server in
//! `steer_vehicle`, whose `set_passengers` then takes the player off.
//!
//! [`tick_boat`](super::tick_boat) runs all five. Server packets: `set_passengers` naming the
//! player puts it on the boat; one without it takes it off (the client then walks again from
//! where it sits); `vehicle_move` is a correction ([`Boat::server_move`], which also gives the
//! packet the client answers with).

// 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::v1_12::BlockState;

use super::blocks::{
    BoxList, BoxSink, add_collision_boxes, id, is_air, is_tall_support, is_water, slipperiness,
    state_at,
};
use super::ext::PlayerExt;
use super::fluid::{contains_lava, water_contact};
use super::player::{PlayerState, box_at};
use crate::input::{Packet, Packets, TickInput};
use crate::math::{self, Aabb, BoxOps, floor};
use crate::world::{BlockView, WorldBorder};

/// Boat box width (`float`).
pub const BOAT_WIDTH: f32 = 1.375;
/// Boat box height (`float`).
pub const BOAT_HEIGHT: f32 = 0.5625;

/// The boat's location of a tick (`BoatEntity.Location`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum BoatLocation {
    /// 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,
}

/// Where the player sits in a boat (`BoatEntity.updatePassengerPosition`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum BoatSeat {
    /// The only passenger: on the boat's position.
    Alone,
    /// The first of two passengers: 0.2 forward along the boat's yaw.
    Front,
    /// The second of two passengers: 0.6 back.
    Back,
}

/// A boat the player rides. Create it from the spawn packet ([`Boat::new`]); it starts as a
/// boat the player rides alone and controls. [`Boat::set_seat`] says where the player sits:
/// only a boat whose first passenger is the player is moved by the client (`method_13003`);
/// behind another player or a mob the boat interpolates the server's packets
/// ([`Boat::server_teleport`], [`Boat::server_update`]).
#[derive(Clone, Debug, PartialEq)]
pub struct Boat {
    /// Position.
    pub pos: [f64; 3],
    /// Box.
    pub bb: Aabb,
    /// Velocity.
    pub vel: [f64; 3],
    /// Yaw.
    pub yaw: f32,
    /// Pitch.
    pub pitch: f32,
    /// On ground after its last move.
    pub on_ground: bool,
    /// Horizontal collision of its last move.
    pub horizontal_collision: bool,
    /// Vertical collision of its last move.
    pub vertical_collision: bool,
    /// Fall distance.
    pub fall_distance: f32,
    /// Below y = -64 the boat is destroyed (its passenger gets off).
    pub removed: bool,
    in_web: bool,
    outside_world_border: bool,
    location: Option<BoatLocation>,
    last_location: Option<BoatLocation>,
    ticks_underwater: f32,
    yaw_velocity: f32,
    water_level: f64,
    land_friction: f32,
    fall_velocity: f64,
    /// Paddle input: left, right, forward, back.
    input: [bool; 4],
    paddle: [bool; 2],
    /// Lily pads broken in the last tick (the caller sets them to air in its world).
    broken: Vec<[i32; 3]>,
    seat: BoatSeat,
    /// The player is the first passenger (the client moves the boat).
    driver: bool,
    /// The server's position in 1/4096 blocks (`tracedX` ...: what relative moves add to).
    tracked: [i64; 3],
    /// Interpolation target (`boatX` ... `boatPitch`) and steps left (`field_12223`).
    target: [f64; 3],
    target_yaw: f64,
    target_pitch: f64,
    steps: i32,
}

/// 1/4096 fixed point (`EntityTracker.method_12764`: `lfloor(v * 4096)`).
fn fixed(v: f64) -> i64 {
    (v * 4096.0).floor() as i64
}

/// An angle byte as `float` degrees (`byte * 360 / 256.0F`).
fn angle(b: i8) -> f32 {
    f32::from(b) * 360.0 / 256.0
}

fn boat_box(x: f64, y: f64, z: f64) -> Aabb {
    box_at(x, y, z, BOAT_WIDTH, BOAT_HEIGHT)
}

impl Boat {
    /// A boat as the spawn packet creates it: at `pos` with the packet's rotation (`yaw`, `pitch`
    /// decoded from the angle bytes: `byte * 360 / 256`).
    #[must_use]
    pub fn new(pos: [f64; 3], yaw: f32, pitch: f32) -> Boat {
        Boat {
            pos,
            bb: boat_box(pos[0], pos[1], pos[2]),
            vel: [0.0; 3],
            yaw,
            pitch,
            on_ground: false,
            horizontal_collision: false,
            vertical_collision: false,
            fall_distance: 0.0,
            removed: false,
            in_web: false,
            outside_world_border: false,
            location: None,
            last_location: None,
            ticks_underwater: 0.0,
            yaw_velocity: 0.0,
            water_level: 0.0,
            land_friction: 0.0,
            fall_velocity: 0.0,
            input: [false; 4],
            paddle: [false; 2],
            broken: Vec::new(),
            seat: BoatSeat::Alone,
            driver: true,
            tracked: pos.map(fixed),
            target: pos,
            target_yaw: 0.0,
            target_pitch: 0.0,
            steps: 0,
        }
    }

    /// The passenger list changed (`set_passengers`): the player is passenger `index` (0 is the
    /// primary passenger) of `count` (1 or 2). The client moves the boat while `index` is 0.
    pub fn set_seat(&mut self, index: usize, count: usize) {
        self.driver = index == 0;
        self.seat = match (count > 1, index) {
            (false, _) => BoatSeat::Alone,
            (true, 0) => BoatSeat::Front,
            (true, _) => BoatSeat::Back,
        };
    }

    /// The player left the boat (a set-passengers packet without it): the client no longer
    /// moves it.
    pub fn leave(&mut self) {
        self.driver = false;
    }

    /// Where the player sits.
    #[must_use]
    pub fn seat(&self) -> BoatSeat {
        self.seat
    }

    /// The client moves this boat (the player is its first passenger).
    #[must_use]
    pub fn controlled(&self) -> bool {
        self.driver
    }

    /// Steps of interpolation left (`field_12223`; frozen while the client moves the boat).
    #[must_use]
    pub fn interpolation_steps(&self) -> i32 {
        self.steps
    }

    /// The server's position in 1/4096 blocks.
    #[must_use]
    pub fn tracked(&self) -> [i64; 3] {
        self.tracked
    }

    /// The interpolation target (`boatX`, `boatY`, `boatZ`).
    #[must_use]
    pub fn interpolation_target(&self) -> [f64; 3] {
        self.target
    }

    /// What the client holds of a boat when the player boards it: the interpolation target and
    /// rotation, the steps left and the server's position (1/4096 blocks).
    pub fn set_interpolation(
        &mut self,
        target: [f64; 3],
        yaw: f32,
        pitch: f32,
        steps: i32,
        tracked: [i64; 3],
    ) {
        self.target = target;
        self.target_yaw = f64::from(yaw);
        self.target_pitch = f64::from(pitch);
        self.steps = steps;
        self.tracked = tracked;
    }

    /// What a boat the player drove before keeps on the client: its turn (`yawVelocity`, never
    /// decayed while the client does not move it) and its paddle flags (entity data).
    pub fn set_turn(&mut self, yaw_velocity: f32, paddle: [bool; 2]) {
        self.yaw_velocity = yaw_velocity;
        self.paddle = paddle;
    }

    /// The server's entity teleport (`onEntityPosition`): position, angle bytes, on ground. The
    /// 1/4096 server position always; a boat the client does not move then interpolates there
    /// (or, within 1/32 and 1/64 vertically, to where it is) over 10 steps.
    pub fn server_teleport(&mut self, pos: [f64; 3], yaw: i8, pitch: i8, on_ground: bool) {
        self.tracked = pos.map(fixed);
        if self.driver {
            return;
        }
        #[allow(clippy::neg_cmp_op_on_partial_ord)]
        let near = !((self.pos[0] - pos[0]).abs() >= 0.03125)
            && !((self.pos[1] - pos[1]).abs() >= 0.015625)
            && !((self.pos[2] - pos[2]).abs() >= 0.03125);
        let target = if near { self.pos } else { pos };
        self.retarget(target, angle(yaw), angle(pitch));
        self.on_ground = on_ground;
    }

    /// The server's relative move, look, move-look (`onEntityUpdate`): `delta` in 1/4096 blocks
    /// (zero for a look), `rotation` the angle bytes when the packet has them. The server
    /// position always moves; a boat the client does not move interpolates there.
    pub fn server_update(&mut self, delta: [i16; 3], rotation: Option<(i8, i8)>, on_ground: bool) {
        for (t, d) in self.tracked.iter_mut().zip(delta) {
            *t += i64::from(d);
        }
        if self.driver {
            return;
        }
        let target = self.tracked.map(|v| v as f64 / 4096.0);
        let (yaw, pitch) = rotation.map_or((self.yaw, self.pitch), |(y, p)| (angle(y), angle(p)));
        self.retarget(target, yaw, pitch);
        self.on_ground = on_ground;
    }

    /// `BoatEntity.updateTrackedPositionAndAngles`: always 10 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 = 10;
    }

    /// `method_11335`: a step towards the server's position, only while the client does not
    /// move the boat (a pending interpolation waits meanwhile).
    fn interpolate(&mut self) {
        if self.steps > 0 && !self.driver {
            let n = f64::from(self.steps);
            let x = self.pos[0] + (self.target[0] - self.pos[0]) / n;
            let y = self.pos[1] + (self.target[1] - self.pos[1]) / n;
            let z = self.pos[2] + (self.target[2] - self.pos[2]) / n;
            let mut g = (self.target_yaw - f64::from(self.yaw)) % 360.0;
            if g >= 180.0 {
                g -= 360.0;
            }
            if g < -180.0 {
                g += 360.0;
            }
            let yaw = (f64::from(self.yaw) + g / n) as f32;
            let pitch =
                (f64::from(self.pitch) + (self.target_pitch - f64::from(self.pitch)) / n) as f32;
            self.steps -= 1;
            self.set_position(x, y, z);
            self.yaw = yaw % 360.0;
            self.pitch = pitch % 360.0;
        }
    }

    /// A client tick of the boat while the player is not in it ([`Boat::leave`]): it
    /// interpolates the server's moves like any boat the client does not move.
    pub fn tick_unridden<W: BlockView<State = BlockState>>(&mut self, world: &W) {
        self.driver = false;
        passive_tick(self, world);
    }

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

    /// The turn of the last tick (`yawVelocity`, degrees).
    #[must_use]
    pub fn yaw_velocity(&self) -> f32 {
        self.yaw_velocity
    }

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

    /// The paddles moving (the `steer_boat` packet of the last tick): left, right.
    #[must_use]
    pub fn paddles(&self) -> [bool; 2] {
        self.paddle
    }

    /// The lily pads the boat broke in the last tick (`LilyPadBlock.onEntityCollision` removes
    /// them from the client's world at once): set them to air in the world.
    #[must_use]
    pub fn broken_lily_pads(&self) -> &[[i32; 3]] {
        &self.broken
    }

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

    /// The server's `vehicle_move` (`onVehicleMove`: `updatePositionAndAngles`); returns the
    /// `vehicle_move` the client answers with at once.
    pub fn server_move(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) -> Packet {
        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),
        );
        self.vehicle_move()
    }

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

    /// `ClientPlayerEntity.tickRiding`: the passenger's keys become the paddle input.
    pub fn set_input(&mut self, left: bool, right: bool, forward: bool, back: bool) {
        self.input = [left, right, forward, back];
    }

    /// `copyEntityData`: the passenger's yaw clamped to 105 degrees either side of the boat's.
    #[must_use]
    pub fn clamp_passenger_yaw(&self, yaw: f32) -> f32 {
        let f = crate::rotation::wrap_degrees(yaw - self.yaw);
        let g = f.clamp(-105.0, 105.0);
        yaw + (g - f)
    }

    /// The passenger's position after the boat moved (`updatePassengerPosition` of the player:
    /// `getMountedHeightOffset() + getHeightOffset()` as a float, and with two passengers the
    /// seat's offset along the boat's yaw).
    #[must_use]
    pub fn passenger_pos(&self) -> [f64; 3] {
        let g = (-0.1f64 + -0.35f64) as f32;
        let f: f32 = match self.seat {
            BoatSeat::Alone => return [self.pos[0], self.pos[1] + f64::from(g), self.pos[2]],
            BoatSeat::Front => 0.2,
            BoatSeat::Back => -0.6,
        };
        // Vec3d(f, 0, 0).rotateY(-yaw * (PI / 180) - PI / 2), in floats.
        let a =
            -self.yaw * (std::f64::consts::PI / 180.0) as f32 - std::f64::consts::FRAC_PI_2 as f32;
        let (c, s) = (math::cos(a), math::sin(a));
        let f = f64::from(f);
        let dx = f * f64::from(c) + 0.0 * f64::from(s);
        let dz = 0.0 * f64::from(c) - f * f64::from(s);
        [
            self.pos[0] + dx,
            self.pos[1] + f64::from(g),
            self.pos[2] + dz,
        ]
    }
}

/// The world as the rest of a tick sees it after the boat broke lily pads: air there.
struct Gone<'a, W> {
    w: &'a W,
    gone: &'a [[i32; 3]],
}

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

    #[inline]
    fn block(&self, x: i32, y: i32, z: i32) -> BlockState {
        if self.gone.contains(&[x, y, z]) {
            BlockState::AIR
        } else {
            self.w.block(x, y, z)
        }
    }

    #[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()
    }
}

/// One client tick while riding `boat` (the module docs list the steps): the turn clamp, the
/// boat's tick (with `boat_entities`: the boxes it collides with, i.e. other boats, shulkers and
/// the living entities other than its passenger, as `World.doesBoxCollide` adds them for the
/// boat), the player's passenger tick (with `entities`: the boxes the player collides with, as
/// for [`tick_ext`](super::tick_ext), its own boat excluded), the `vehicle_move`, the seat and the
/// next paddle input. Packets in vanilla order: `steer_boat`, `look`, `steer_vehicle`,
/// `vehicle_move`. Lily pads the boat broke are in [`Boat::broken_lily_pads`].
///
/// Returns `false` when the boat was destroyed below y = -64: the player got off without its
/// own tick (only its age counted) and walks from the next tick on.
#[allow(clippy::too_many_arguments)]
pub fn tick_boat<W: BlockView<State = BlockState>>(
    p: &mut PlayerState,
    x: &mut PlayerExt,
    boat: &mut Boat,
    entities: &[Aabb],
    boat_entities: &[Aabb],
    input: &TickInput,
    world: &W,
    out: &mut Packets,
) -> bool {
    // The mouse turned the passenger: the boat clamps it (copyEntityData).
    let mut ti = *input;
    ti.yaw = boat.clamp_passenger_yaw(input.yaw);
    if boat.driver {
        boat_tick(boat, world, boat_entities, out);
    } else {
        passive_tick(boat, world);
    }
    if boat.removed {
        // Entity.tickRiding: the vehicle is gone, the player gets off without ticking.
        p.ticks_alive += 1;
        p.yaw = ti.yaw;
        p.pitch = ti.pitch;
        return false;
    }
    let gone = std::mem::take(&mut boat.broken);
    if gone.is_empty() {
        super::player::tick_passenger(p, x, entities, &ti, world, out);
    } else {
        let w = Gone {
            w: world,
            gone: &gone,
        };
        super::player::tick_passenger(p, x, entities, &ti, &w, out);
    }
    boat.broken = gone;
    if boat.driver {
        out.push(boat.vehicle_move());
    }
    // updatePassengerPosition, then LivingEntity.tickRiding resets the fall distance.
    let [sx, sy, sz] = boat.passenger_pos();
    p.set_position(sx, sy, sz);
    p.yaw += boat.yaw_velocity;
    p.yaw = boat.clamp_passenger_yaw(p.yaw);
    p.fall_distance = 0.0;
    let k = &input.keys;
    boat.set_input(k.left, k.right, k.forward, k.back);
    true
}

/// One boat tick (`BoatEntity.tick` with the main player as its primary passenger).
fn boat_tick<W: BlockView<State = BlockState>>(
    b: &mut Boat,
    w: &W,
    entities: &[Aabb],
    out: &mut Packets,
) {
    b.broken.clear();
    b.last_location = b.location;
    b.location = Some(check_location(b, w));
    if matches!(
        b.location,
        Some(BoatLocation::UnderWater | BoatLocation::UnderFlowingWater)
    ) {
        b.ticks_underwater += 1.0;
    } else {
        b.ticks_underwater = 0.0;
    }
    base_tick(b, w);
    update_velocity(b, w);
    update_paddles(b);
    out.push(Packet::PaddleBoat {
        left: b.paddle[0],
        right: b.paddle[1],
    });
    let [vx, vy, vz] = b.vel;
    boat_move(b, w, entities, vx, vy, vz);
    // After the move: the block contacts once more (BoatEntity.tick -> checkBlockCollision).
    block_contacts(b, w);
}

/// `BoatEntity.tick` of a boat the client does not move (another player or a mob is its first
/// passenger, or none): location, base tick, a step of interpolation, zero velocity, block
/// contacts.
#[cold]
#[inline(never)]
fn passive_tick<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) {
    b.broken.clear();
    b.last_location = b.location;
    b.location = Some(check_location(b, w));
    if matches!(
        b.location,
        Some(BoatLocation::UnderWater | BoatLocation::UnderFlowingWater)
    ) {
        b.ticks_underwater += 1.0;
    } else {
        b.ticks_underwater = 0.0;
    }
    base_tick(b, w);
    b.interpolate();
    b.vel = [0.0; 3];
    // The paddle flags are entity data: they keep what the client last set.
    block_contacts(b, w);
}

/// `AbstractFluidBlock.method_13709`: the water's height in its block (a full block under
/// water above a source or falling block).
fn water_height<W: BlockView<State = BlockState>>(
    w: &W,
    x: i32,
    y: i32,
    z: i32,
    s: BlockState,
) -> f32 {
    let level = s.meta();
    if level & 7 == 0 && is_water(state_at(w, x, y + 1, z)) {
        1.0
    } else {
        let l = if level >= 8 { 0 } else { level };
        1.0 - (f32::from(l) + 1.0) / 9.0
    }
}

fn check_location<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) -> BoatLocation {
    if let Some(l) = under_water_location(b, w) {
        b.water_level = b.bb.max_y;
        return l;
    }
    if boat_in_water(b, w) {
        return BoatLocation::InWater;
    }
    let f = nearby_slipperiness(b, w);
    if f > 0.0 {
        b.land_friction = f;
        BoatLocation::OnLand
    } else {
        BoatLocation::InAir
    }
}

fn under_water_location<W: BlockView<State = BlockState>>(b: &Boat, w: &W) -> Option<BoatLocation> {
    let bb = b.bb;
    let d = bb.max_y + 0.001;
    let (x0, x1) = (floor(bb.min_x), ceil(bb.max_x));
    let (y0, y1) = (floor(bb.max_y), ceil(d));
    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 s = state_at(w, x, y, z);
                if is_water(s) && d < f64::from(y as f32 + water_height(w, x, y, z, s)) {
                    if s.meta() != 0 {
                        return Some(BoatLocation::UnderFlowingWater);
                    }
                    under = true;
                }
            }
        }
    }
    under.then_some(BoatLocation::UnderWater)
}

fn boat_in_water<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) -> bool {
    let bb = b.bb;
    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 found = false;
    // Java's Double.MIN_VALUE: the smallest positive subnormal.
    b.water_level = f64::from_bits(1);
    for x in x0..x1 {
        for y in y0..y1 {
            for z in z0..z1 {
                let s = state_at(w, x, y, z);
                if is_water(s) {
                    let top = y as f32 + water_height(w, x, y, z, s);
                    b.water_level = math::java_max(f64::from(top), b.water_level);
                    found |= bb.min_y < f64::from(top);
                }
            }
        }
    }
    found
}

/// `method_11333`: the average slipperiness of the blocks whose collision boxes touch the thin
/// box under the boat (lily pads have none for a boat).
fn nearby_slipperiness<W: BlockView<State = BlockState>>(b: &Boat, w: &W) -> f32 {
    let bb = b.bb;
    let q = Aabb::new(
        bb.min_x,
        bb.min_y - 0.001,
        bb.min_z,
        bb.max_x,
        bb.min_y,
        bb.max_z,
    );
    let i = floor(q.min_x) - 1;
    let j = ceil(q.max_x) + 1;
    let k = floor(q.min_y) - 1;
    let l = ceil(q.max_y) + 1;
    let m = floor(q.min_z) - 1;
    let n = ceil(q.max_z) + 1;
    let mut f = 0.0f32;
    let mut o = 0i32;
    let mut list = BoxList::new();
    for p in i..j {
        for qz in m..n {
            let r = i32::from(p == i || p == j - 1) + i32::from(qz == m || qz == n - 1);
            if r == 2 {
                continue;
            }
            for s in k..l {
                if r > 0 && (s == k || s == l - 1) {
                    continue;
                }
                let st = state_at(w, p, s, qz);
                list.clear();
                let mut sink = BoxSink {
                    query: q,
                    out: &mut list,
                };
                // LilyPadBlock.appendCollisionBoxes: no box for a boat.
                if st.block().id() != id::LILY_PAD {
                    add_collision_boxes(w, p, s, qz, st, &mut sink);
                }
                if !list.is_empty() {
                    f += slipperiness(st);
                    o += 1;
                }
            }
        }
    }
    f / o as f32
}

/// `method_11332`: the water surface over the boat's box (used when it lands in water).
fn water_height_below<W: BlockView<State = BlockState>>(b: &Boat, w: &W) -> f32 {
    let bb = b.bb;
    let (i, j) = (floor(bb.min_x), ceil(bb.max_x));
    let k = floor(bb.max_y);
    let l = ceil(bb.max_y - b.fall_velocity);
    let (m, n) = (floor(bb.min_z), ceil(bb.max_z));
    'rows: for o in k..l {
        let mut f = 0.0f32;
        for p in i..j {
            for q in m..n {
                let s = state_at(w, p, o, q);
                if is_water(s) {
                    f = f.max(water_height(w, p, o, q, s));
                }
                if f >= 1.0 {
                    continue 'rows;
                }
            }
        }
        if f < 1.0 {
            return o as f32 + f;
        }
    }
    (l + 1) as f32
}

/// `Entity.baseTick` of the boat: water contact with its (normalised) flow push, lava halving
/// the fall distance, destroyed below y = -64.
fn base_tick<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) {
    let probe =
        b.bb.grow(0.0, -f64::from(0.4f32), 0.0)
            .shrink(0.001, 0.001, 0.001);
    let (touching, flow) = water_contact(w, &probe);
    if touching {
        if flow.length() > 0.0 {
            let f = flow.normalize();
            const PUSH: f64 = 0.014;
            b.vel[0] += f.x * PUSH;
            b.vel[1] += f.y * PUSH;
            b.vel[2] += f.z * PUSH;
        }
        b.fall_distance = 0.0;
    }
    let lava =
        b.bb.grow(-f64::from(0.1f32), -f64::from(0.4f32), -f64::from(0.1f32));
    if contains_lava(w, &lava) {
        b.fall_distance *= 0.5;
    }
    if b.pos[1] < -64.0 {
        b.removed = true;
    }
}

fn update_velocity<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) {
    let mut e = f64::from(-0.04f32);
    let mut f = 0.0;
    let mut decay = 0.05f32;
    let loc = b.location;
    if b.last_location == Some(BoatLocation::InAir)
        && loc != Some(BoatLocation::InAir)
        && loc != Some(BoatLocation::OnLand)
    {
        b.water_level = b.bb.min_y + f64::from(BOAT_HEIGHT);
        let y = f64::from(water_height_below(b, w)) - f64::from(BOAT_HEIGHT) + 0.101;
        b.set_position(b.pos[0], y, b.pos[2]);
        b.vel[1] = 0.0;
        b.fall_velocity = 0.0;
        b.location = Some(BoatLocation::InWater);
        return;
    }
    match loc {
        Some(BoatLocation::InWater) => {
            f = (b.water_level - b.bb.min_y) / f64::from(BOAT_HEIGHT);
            decay = 0.9;
        }
        Some(BoatLocation::UnderFlowingWater) => {
            e = -7.0e-4;
            decay = 0.9;
        }
        Some(BoatLocation::UnderWater) => {
            f = f64::from(0.01f32);
            decay = 0.45;
        }
        Some(BoatLocation::InAir) => decay = 0.9,
        Some(BoatLocation::OnLand) => {
            decay = b.land_friction;
            // The primary passenger is a player.
            b.land_friction /= 2.0;
        }
        None => {}
    }
    let d = f64::from(decay);
    b.vel[0] *= d;
    b.vel[2] *= d;
    b.yaw_velocity *= decay;
    b.vel[1] += e;
    if f > 0.0 {
        b.vel[1] += f * 0.061_538_460_162_969_73;
        b.vel[1] *= 0.75;
    }
}

fn update_paddles(b: &mut Boat) {
    let [left, right, forward, back] = b.input;
    let mut f = 0.0f32;
    if left {
        b.yaw_velocity += -1.0;
    }
    if right {
        b.yaw_velocity += 1.0;
    }
    if right != left && !forward && !back {
        f += 0.005;
    }
    b.yaw += b.yaw_velocity;
    if forward {
        f += 0.04;
    }
    if back {
        f -= 0.005;
    }
    let r = (std::f64::consts::PI / 180.0) as f32;
    b.vel[0] += f64::from(math::sin(-b.yaw * r) * f);
    b.vel[2] += f64::from(math::cos(b.yaw * r) * f);
    b.paddle = [right && !left || forward, left && !right || forward];
}

/// Java `MathHelper.ceil(double)`.
#[inline]
fn ceil(v: f64) -> i32 {
    let i = v as i32;
    if v > f64::from(i) { i + 1 } else { i }
}

/// `World.doesBoxCollide` for the boat: the block boxes intersecting `query` (1.9+ scan: one
/// block wider on x / z without the four corner columns and the top layer of the edge columns;
/// outside the world border blocks collide like stone while the boat is inside the world; lily
/// pads have no box for a boat), then the entity boxes intersecting it.
fn gather<W: BlockView<State = BlockState>>(
    b: &mut Boat,
    w: &W,
    border: &WorldBorder,
    entities: &[Aabb],
    query: Aabb,
    out: &mut BoxList,
) {
    out.clear();
    let (x0, x1) = (floor(query.min_x) - 1, ceil(query.max_x) + 1);
    let (y0, y1) = (floor(query.min_y) - 1, ceil(query.max_y) + 1);
    let (z0, z1) = (floor(query.min_z) - 1, ceil(query.max_z) + 1);
    let was_outside = b.outside_world_border;
    let inside = {
        let m = if b.outside_world_border { 1.0 } else { -1.0 };
        let (wx, nz, ex, sz) = (
            border.west() + m,
            border.north() + m,
            border.east() - m,
            border.south() - m,
        );
        let (x, z) = (b.pos[0], b.pos[2]);
        x > wx && x < ex && z > nz && z < sz
    };
    let mut visited = false;
    let mut sink = BoxSink { query, out };
    for x in x0..x1 {
        let edge_x = x == x0 || x == x1 - 1;
        for z in z0..z1 {
            let edge_z = z == z0 || z == z1 - 1;
            if edge_x && edge_z {
                continue;
            }
            let substitute = inside && !border.contains_block(x, z);
            for y in y0..y1 {
                if (edge_x || edge_z) && y == y1 - 1 {
                    continue;
                }
                visited = true;
                let s = if substitute {
                    STONE
                } else {
                    state_at(w, x, y, z)
                };
                // LilyPadBlock.appendCollisionBoxes: no box for a boat.
                if s.block().id() != id::LILY_PAD {
                    add_collision_boxes(w, x, y, z, s, &mut sink);
                }
            }
        }
    }
    for e in entities {
        if e.intersects(&query) {
            sink.out.push(*e);
        }
    }
    if visited {
        if was_outside && inside {
            b.outside_world_border = false;
        } else if !was_outside && !inside {
            b.outside_world_border = true;
        }
    }
}

const STONE: BlockState = ghast_data::v1_12::Block::STONE.default_state();

/// `BoatItem` (use in the air) after its ray: a new boat at `pos` (the hit, 0.12 lower in water)
/// has room when `World.doesBoxCollide` finds nothing in its box shrunk by 0.1: no block box (the
/// boat's own scan), no world-border wall and none of `entities` (the boxes the boat collides
/// with: the pushable ones, e.g. the player's own box unless it climbs).
#[must_use]
pub fn boat_item_fits<W: BlockView<State = BlockState>>(
    w: &W,
    pos: [f64; 3],
    entities: &[Aabb],
) -> bool {
    let mut b = Boat::new(pos, 0.0, 0.0);
    let query = b.bb.grow(-0.1, -0.1, -0.1);
    let border = w.world_border();
    let mut boxes = BoxList::new();
    gather(&mut b, w, &border, entities, query, &mut boxes);
    boxes.as_slice().is_empty()
}

/// `Entity.move(SELF, …)` of the boat: no step-up (step height 0), no sneak guard, no stepping
/// effects (`canClimb` is false); the boat's own fall.
fn boat_move<W: BlockView<State = BlockState>>(
    b: &mut Boat,
    w: &W,
    entities: &[Aabb],
    mut dx: f64,
    mut dy: f64,
    mut dz: f64,
) {
    if b.in_web {
        b.in_web = false;
        dx *= 0.25;
        dy *= f64::from(0.05f32);
        dz *= 0.25;
        b.vel = [0.0; 3];
    }
    let (want_x, want_y, want_z) = (dx, dy, dz);
    let border = w.world_border();
    let mut boxes = BoxList::new();
    gather(
        b,
        w,
        &border,
        entities,
        b.bb.stretch(dx, dy, dz),
        &mut boxes,
    );
    let boxes = boxes.as_slice();
    let mut bb = b.bb;
    if dy != 0.0 {
        for o in boxes {
            dy = o.clip_y(&bb, dy);
        }
        bb = bb.offset(0.0, dy, 0.0);
    }
    if dx != 0.0 {
        for o in boxes {
            dx = o.clip_x(&bb, dx);
        }
        if dx != 0.0 {
            bb = bb.offset(dx, 0.0, 0.0);
        }
    }
    if dz != 0.0 {
        for o in boxes {
            dz = o.clip_z(&bb, dz);
        }
        if dz != 0.0 {
            bb = bb.offset(0.0, 0.0, dz);
        }
    }
    b.bb = bb;
    b.pos = [
        (bb.min_x + bb.max_x) / 2.0,
        bb.min_y,
        (bb.min_z + bb.max_z) / 2.0,
    ];
    b.horizontal_collision = want_x != dx || want_z != dz;
    b.vertical_collision = want_y != dy;
    b.on_ground = b.vertical_collision && want_y < 0.0;
    let (lx, ly, lz) = (
        floor(b.pos[0]),
        floor(b.pos[1] - f64::from(0.2f32)),
        floor(b.pos[2]),
    );
    let mut landed = state_at(w, lx, ly, lz);
    if is_air(landed) {
        let below = state_at(w, lx, ly - 1, lz);
        if is_tall_support(below) {
            landed = below;
        }
    }
    boat_fall(b, w, dy);
    if want_x != dx {
        b.vel[0] = 0.0;
    }
    if want_z != dz {
        b.vel[2] = 0.0;
    }
    if want_y != dy {
        // Block.setEntityVelocity: slime and beds bounce a non-living entity with 0.8 of the
        // factor (the boat never sneaks); everything else stops the fall.
        match landed.block().id() {
            id::SLIME => {
                if b.vel[1] < 0.0 {
                    b.vel[1] = -b.vel[1] * 0.8;
                }
            }
            id::BED => {
                if b.vel[1] < 0.0 {
                    b.vel[1] = -b.vel[1] * f64::from(0.66f32) * 0.8;
                }
            }
            _ => b.vel[1] = 0.0,
        }
    }
    block_contacts(b, w);
}

/// `BoatEntity.fall`.
fn boat_fall<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W, dy: f64) {
    b.fall_velocity = b.vel[1];
    if b.on_ground {
        // A fall of more than 3 blocks on land only hurts (the server removes the boat).
        b.fall_distance = 0.0;
    } else {
        let (x, y, z) = (floor(b.pos[0]), floor(b.pos[1]) - 1, floor(b.pos[2]));
        if !is_water(state_at(w, x, y, z)) && dy < 0.0 {
            b.fall_distance = (f64::from(b.fall_distance) - dy) as f32;
        }
    }
}

/// `checkBlockCollision` of the boat: the blocks its box (shrunk by 0.001) is in. Cobwebs slow
/// the next move, soul sand slows it at once, lily pads break.
fn block_contacts<W: BlockView<State = BlockState>>(b: &mut Boat, w: &W) {
    let bb = b.bb;
    let (x0, y0, z0) = (
        floor(bb.min_x + 0.001),
        floor(bb.min_y + 0.001),
        floor(bb.min_z + 0.001),
    );
    let (x1, y1, z1) = (
        floor(bb.max_x - 0.001),
        floor(bb.max_y - 0.001),
        floor(bb.max_z - 0.001),
    );
    if !(y1 >= 0 && y0 < 256) {
        return;
    }
    for x in x0..=x1 {
        for y in y0..=y1 {
            for z in z0..=z1 {
                match state_at(w, x, y, z).block().id() {
                    id::WEB => {
                        b.in_web = true;
                        b.fall_distance = 0.0;
                    }
                    id::SOUL_SAND => {
                        b.vel[0] *= 0.4;
                        b.vel[2] *= 0.4;
                    }
                    id::LILY_PAD if !b.broken.contains(&[x, y, z]) => b.broken.push([x, y, z]),
                    _ => {}
                }
            }
        }
    }
}