ghast-client 0.2.1

Headless Minecraft Java Edition client for the ghast library: connection, login, fixed 50 ms tick loop and a version-agnostic bot API (1.8.9 and 1.16.5)
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
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
//! The deterministic client core: game state, the behaviour contract and the tick.
//!
//! No I/O and no clock here: the connection feeds decoded [`Inbound`] packets and the time
//! in, and takes the [`Outbound`] packets out, so the tick order can be tested without a
//! socket.
//!
//! # Tick model
//!
//! Mirrors 1.8.9 `MinecraftClient.tick` as recorded in vanilla-client packet traces (and is the same
//! in 1.16.5):
//!
//! 1. inbound packets that arrived since the last tick have been applied (on arrival, like
//!    vanilla's per-frame task queue; keep-alives, teleports and window confirms are
//!    answered then);
//! 2. [`Behavior::tick`] runs. Actions (`chat`, `select_slot`, `swing`, `respawn`, …) are
//!    written immediately, in call order, before this tick's movement packet;
//!    [`TickContext::set_aim`] / [`TickContext::set_controls`] only record intent;
//! 3. held slot sync → rotation step (whole mouse counts) → movement step → sprint change →
//!    sneak change → exactly one movement packet.
//!
//! The server therefore judges this tick's actions with the *previous* tick's sent pose
//! ([`TickContext::sent`]).

use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::common::{
    Abilities, ChatMessage, ClientSettings, Controls, GameMode, Health, Inbound, MovePacket,
    Outbound, Pose, Vec3,
};
use crate::hands::{self, BlockHit, Buttons, DigFactors, Hands, HandsIo};
use crate::interest::Interests;
use crate::rotation::Rotation;
use crate::sim::{Sim, WorldView};
use crate::version::GameVersion;

/// Pending chat messages kept for the behaviour; more are dropped (and counted) while the
/// behaviour is not ticking.
const MAX_PENDING_CHAT: usize = 512;

/// A bot behaviour: the code that decides what the player does each tick.
///
/// Behaviours never see packets or versions: they read a [`TickContext`] and call its
/// actions. A panic in a behaviour stops that behaviour (its [`Behavior::stop`] is called with
/// `"panic"`) but not the connection: the player keeps standing and answering the server.
pub trait Behavior {
    /// Name for logs.
    fn name(&self) -> &str {
        "behavior"
    }

    /// Optional packet groups this behaviour needs. Read every tick; a change updates the
    /// packet filter. Everything not asked for is dropped before it is decoded.
    fn interests(&self) -> Interests {
        Interests::NONE
    }

    /// Extra clientbound play packet ids (of `version`) to copy undecoded into
    /// [`TickEvents::raw`], including packets the core consumes itself (the core's handling
    /// does not change). Read every tick like [`Behavior::interests`]; a change updates the
    /// packet filter. Decoding is the behaviour's job (`ghast::protocol::v1_8::play::clientbound`
    /// / `v1_16_5`).
    fn raw_packets(&self, version: GameVersion) -> std::borrow::Cow<'static, [i32]> {
        let _ = version;
        std::borrow::Cow::Borrowed(&[])
    }

    /// Called on the first tick the player is in the world, before that tick's
    /// [`Behavior::tick`] (both see the same events).
    fn start(&mut self, ctx: &mut TickContext<'_>) {
        let _ = ctx;
    }

    /// Called every tick while the player is in the world and the server is answering.
    fn tick(&mut self, ctx: &mut TickContext<'_>);

    /// Whether the behaviour still wants ticks. Once it returns `false` the behaviour is done:
    /// it is not ticked again (the player keeps standing and answering the server) and
    /// [`Behavior::stop`] still runs when the session ends. Read after every start / tick.
    fn running(&self) -> bool {
        true
    }

    /// Called after every tick's movement step (also while the behaviour is on hold or has
    /// finished), with a read-only view of what the server now has: the pose sent this tick,
    /// the world, entities and inventory. For observers (a viewer, a recorder); it cannot act.
    /// Default: nothing.
    ///
    /// ```
    /// use ghast_client::{Behavior, TickContext, TickView};
    ///
    /// /// Logs where the server has the player, once a second.
    /// struct Tracker;
    ///
    /// impl Behavior for Tracker {
    ///     fn tick(&mut self, _ctx: &mut TickContext<'_>) {}
    ///
    ///     fn after_tick(&mut self, view: &TickView<'_>) {
    ///         if view.tick() % 20 == 0 {
    ///             let p = view.sent();
    ///             println!("{} {:?} in {}", view.tick(), p.pos, view.dimension());
    ///         }
    ///     }
    /// }
    /// ```
    fn after_tick(&mut self, view: &TickView<'_>) {
        let _ = view;
    }

    /// Called once when the session ends (or the behaviour panicked).
    fn stop(&mut self, reason: &str) {
        let _ = reason;
    }
}

/// A boxed behaviour is a behaviour (a runner can pick one from its config at run time).
impl<B: Behavior + ?Sized> Behavior for Box<B> {
    fn name(&self) -> &str {
        (**self).name()
    }
    fn interests(&self) -> Interests {
        (**self).interests()
    }
    fn raw_packets(&self, version: GameVersion) -> std::borrow::Cow<'static, [i32]> {
        (**self).raw_packets(version)
    }
    fn start(&mut self, ctx: &mut TickContext<'_>) {
        (**self).start(ctx);
    }
    fn tick(&mut self, ctx: &mut TickContext<'_>) {
        (**self).tick(ctx);
    }
    fn running(&self) -> bool {
        (**self).running()
    }
    fn after_tick(&mut self, view: &TickView<'_>) {
        (**self).after_tick(view);
    }
    fn stop(&mut self, reason: &str) {
        (**self).stop(reason);
    }
}

/// Why [`TickContext::click`] (or opening / closing a window) sent nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClickRefused {
    /// The player is turning: click only while still.
    Turning,
    /// The inventory refused it.
    Inventory(ghast_inventory::ClickError),
}

impl std::fmt::Display for ClickRefused {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ClickRefused::Turning => f.write_str("the player is turning"),
            ClickRefused::Inventory(e) => write!(f, "{e}"),
        }
    }
}

impl std::error::Error for ClickRefused {}

/// A click that was sent: match it with [`TickEvents::inventory`]'s `clicks` on a later tick
/// (window id and action number, as the server's confirm carries them).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClickTicket {
    /// Window id the click was sent for.
    pub window_id: i32,
    /// Its action number.
    pub action: i16,
}

impl ClickTicket {
    /// Whether `r` is the result of this click.
    #[must_use]
    pub fn matches(&self, r: &ghast_inventory::ClickResult) -> bool {
        r.window_id == self.window_id && r.action == self.action
    }
}

/// A behaviour that does nothing (the player stands still).
#[derive(Debug, Clone, Copy, Default)]
pub struct Idle;

impl Behavior for Idle {
    fn name(&self) -> &str {
        "idle"
    }

    fn tick(&mut self, _ctx: &mut TickContext<'_>) {}
}

/// A clientbound play packet copied for the behaviour ([`Behavior::raw_packets`]).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct RawInbound {
    /// Packet id (version-specific).
    pub id: i32,
    /// The body after the id, undecoded (a cheap reference-counted copy).
    pub body: bytes::Bytes,
    /// When the frame was decoded from the socket (frames read together share it).
    pub at: Instant,
}

/// A server teleport / correction (`position`) with the poses around it.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct TeleportEvent {
    /// The packet's values as received: absolute or relative per
    /// [`crate::common::teleport_flags`] in `flags`.
    pub teleport: crate::common::Teleport,
    /// What the server had from us just before ([`TickContext::sent`] before the overwrite):
    /// compare with [`TeleportEvent::after`] to see what the server moved / turned us by.
    pub sent_before: Pose,
    /// The client pose just before (it may be ahead of `sent_before` within a tick).
    pub pose_before: Pose,
    /// The pose after applying it (relative fields resolved).
    pub after: Pose,
}

/// Most teleports kept between two behaviour ticks.
const MAX_PENDING_TELEPORTS: usize = 64;

/// Most raw packets kept between two behaviour ticks.
const MAX_PENDING_RAW: usize = 4096;

/// What happened since the behaviour's previous tick.
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct TickEvents {
    /// Chat messages in arrival order (only with [`Interests::CHAT`]).
    pub chat: Vec<ChatMessage>,
    /// Chat messages dropped because too many piled up while the behaviour was not ticking.
    pub chat_dropped: usize,
    /// The server teleported / corrected the player.
    pub teleported: bool,
    /// Every teleport in arrival order, with the sent pose before it (at most 64 per tick).
    pub teleports: Vec<TeleportEvent>,
    /// A join game packet arrived (first join or a proxy server switch).
    pub joined: bool,
    /// The player respawned or changed dimension.
    pub respawned: bool,
    /// The player died (health reached 0).
    pub died: bool,
    /// Health, food or saturation changed.
    pub health_changed: bool,
    /// The game mode changed.
    pub game_mode_changed: bool,
    /// Inventory / window changes and click results (ghast-inventory).
    pub inventory: ghast_inventory::Events,
    /// Block changes in arrival order (only with [`Interests::BLOCK_UPDATES`]): position and
    /// the version's raw block state (1.8 `id << 4 | meta`, 1.16 the global palette id).
    /// The world already holds them when the behaviour ticks.
    pub block_updates: Vec<(ghast_protocol::BlockPos, u32)>,
    /// Block changes dropped because too many piled up between two behaviour ticks.
    pub block_updates_dropped: usize,
    /// The packets asked for with [`Behavior::raw_packets`], in arrival order (kept while the
    /// behaviour is on hold, like chat).
    pub raw: Vec<RawInbound>,
    /// Raw packets dropped because more than 4096 piled up between two behaviour ticks.
    pub raw_dropped: usize,
}

impl TickEvents {
    fn clear(&mut self) {
        self.chat.clear();
        self.chat_dropped = 0;
        self.teleported = false;
        self.teleports.clear();
        self.joined = false;
        self.respawned = false;
        self.died = false;
        self.health_changed = false;
        self.game_mode_changed = false;
        self.inventory = ghast_inventory::Events::default();
        self.block_updates.clear();
        self.block_updates_dropped = 0;
        self.raw.clear();
        self.raw_dropped = 0;
    }
}

/// Tunables of the core.
#[derive(Debug, Clone)]
pub(crate) struct CoreOptions {
    pub name: Arc<str>,
    pub version: GameVersion,
    pub settings: ClientSettings,
    pub brand: Option<String>,
    pub sensitivity: f32,
    pub silent_hold: Duration,
    pub inventory: ghast_inventory::Version,
    /// Keep the look still on a tick that dug / placed when the turn would take the
    /// crosshair off the clicked block (Grim RotationPlace re-checks with the next look).
    pub hold_turn_on_action: bool,
}

/// Client state shared by the connection and the behaviour context.
#[derive(Debug)]
pub(crate) struct Core {
    opts: CoreOptions,
    tick: u64,
    entity_id: i32,
    joined: bool,
    teleported_once: bool,
    ready: bool,
    pose: Pose,
    sent: Pose,
    rotation: Rotation,
    aimed: bool,
    controls: Controls,
    health: Health,
    dead: bool,
    abilities: Abilities,
    game_mode: GameMode,
    held_slot: u8,
    sent_slot: u8,
    open_window: i32,
    dimension: String,
    respawn_screen: bool,
    pending: TickEvents,
    current: TickEvents,
    out: Vec<Outbound>,
    last_inbound: Option<Instant>,
    silent: bool,
    quit: Option<String>,
    status: String,
    hands: Hands,
    buttons: Buttons,
    /// Inventory and windows (ghast-inventory).
    inv: ghast_inventory::Inventory,
    /// The look changed in the last movement step (window clicks need a still player).
    turned: bool,
    /// Our active status effects: id → amplifier.
    effects: std::collections::BTreeMap<i8, i8>,
    /// Aqua affinity on the helmet (dig speed under water).
    aqua_affinity: bool,
    /// Blocks broken on the client this tick (predicted air).
    broken_now: Vec<ghast_protocol::BlockPos>,
    /// Flight wanted by the behaviour this tick ([`TickContext::set_flying`]).
    want_flying: Option<bool>,
    /// Turns held back because they would have moved the crosshair off a clicked block.
    held_turns: u64,
    /// Arm swings (main and off hand) written in finished ticks.
    swings: u64,
    /// Test hook: this tick's look, set instead of a rotation step.
    #[cfg(test)]
    forced_look: Option<(f32, f32)>,
}

/// Behaviour bookkeeping of the engine.
#[derive(Debug, Default)]
struct BehaviorState {
    started: bool,
    failed: bool,
    /// The behaviour reported [`Behavior::running`] `false`.
    done: bool,
}

/// The core plus the behaviour's lifecycle.
#[derive(Debug)]
pub(crate) struct Engine<S> {
    pub core: Core,
    pub sim: S,
    state: BehaviorState,
}

impl Core {
    fn new(opts: CoreOptions) -> Self {
        let rotation = Rotation::new(opts.sensitivity);
        let inv = ghast_inventory::Inventory::new(opts.inventory);
        Core {
            opts,
            tick: 0,
            entity_id: 0,
            joined: false,
            teleported_once: false,
            ready: false,
            pose: Pose::default(),
            sent: Pose::default(),
            rotation,
            aimed: false,
            controls: Controls::default(),
            health: Health::default(),
            dead: false,
            abilities: Abilities::default(),
            game_mode: GameMode::default(),
            held_slot: 0,
            sent_slot: 0,
            open_window: 0,
            dimension: String::new(),
            respawn_screen: true,
            pending: TickEvents::default(),
            current: TickEvents::default(),
            out: Vec::with_capacity(16),
            last_inbound: None,
            silent: false,
            quit: None,
            status: String::new(),
            hands: Hands::default(),
            buttons: Buttons::default(),
            inv,
            turned: false,
            effects: std::collections::BTreeMap::new(),
            aqua_affinity: false,
            broken_now: Vec::new(),
            want_flying: None,
            held_turns: 0,
            swings: 0,
            #[cfg(test)]
            forced_look: None,
        }
    }

    /// A new client player entity (join, respawn): `lastReported*` zero, nothing held down,
    /// waiting for the server's position.
    fn reset_player(&mut self, why: &str) {
        self.teleported_once = false;
        self.ready = false;
        self.pose = Pose::default();
        self.sent = Pose::default();
        self.controls = Controls::default();
        self.aimed = false;
        self.open_window = 0;
        self.dead = false;
        self.hands.reset();
        self.buttons = Buttons::default();
        self.effects.clear();
        log::debug!(
            "[{}] t{} {why}: waiting for the server position",
            self.opts.name,
            self.tick
        );
    }

    fn column_loaded(&self, world: &dyn WorldView) -> bool {
        // BlockPos(posX, 0, posZ) floors the coordinates.
        let cx = (self.pose.pos.x.floor() as i64 >> 4) as i32;
        let cz = (self.pose.pos.z.floor() as i64 >> 4) as i32;
        world.is_column_loaded(cx, cz)
    }
}

/// The held stack's dig / use facts.
fn held_item<W: Sim + ?Sized>(
    inv: &ghast_inventory::Inventory,
    sim: &W,
) -> Option<hands::HeldItem> {
    let s = inv.held()?;
    let id = u16::try_from(s.id).ok()?;
    let count = u8::try_from(s.count.clamp(0, 255)).unwrap_or(0);
    Some(sim.held_item(id, count, efficiency(s)))
}

/// Whether rays hit the entity (`Entity.collides`): living entities, players,
/// paintings and the solid objects (boats, minecarts, TNT, falling blocks, item frames, armor
/// stands, leash knots, end crystals); not items, orbs, projectiles.
fn collidable(e: &ghast_entity::Entity) -> bool {
    use ghast_entity::{EntityClass, EntityType};
    match e.class() {
        EntityClass::Player | EntityClass::Mob | EntityClass::Painting => true,
        EntityClass::Item | EntityClass::ExperienceOrb => false,
        EntityClass::Object => match e.entity_type() {
            EntityType::LegacyObject(t) => matches!(t, 1 | 10 | 50 | 51 | 70 | 71 | 77 | 78),
            EntityType::Registry(_) => {
                ghast_entity::v1_16_5::info(e.entity_type()).is_some_and(|i| {
                    matches!(
                        i.name,
                        "boat"
                            | "tnt"
                            | "end_crystal"
                            | "falling_block"
                            | "item_frame"
                            | "leash_knot"
                            | "armor_stand"
                            | "shulker_bullet"
                    ) || i.name.contains("minecart")
                })
            }
            _ => false,
        },
    }
}

/// The entity under the crosshair from `eyes` along `look` (vanilla `GameRenderer.updateTargetedEntity`), if
/// entities are tracked.
fn entity_pick<W: WorldView + ?Sized>(
    w: &W,
    version: GameVersion,
    eyes: Vec3,
    look: Vec3,
    block_distance: Option<f64>,
) -> Option<hands::EntityHit> {
    let ents = w.entities()?;
    let riding = ents.me().and_then(ghast_entity::Entity::vehicle);
    // 1.8 grows entity boxes by the 0.1 collision border; 1.9+ does not.
    let border = if version == GameVersion::V1_8_9 {
        0.1
    } else {
        0.0
    };
    let cands = ents
        .within([eyes.x, eyes.y, eyes.z], 8.0)
        .filter(|e| collidable(e))
        .filter_map(|e| {
            let p = e.position();
            Some((e.id(), e.bounding_box()?, Vec3::new(p[0], p[1], p[2])))
        });
    hands::pick_entity(cands, eyes, look, block_distance, border, riding)
}

/// The off-hand stack's facts (1.9+).
fn off_held_item<W: Sim + ?Sized>(
    inv: &ghast_inventory::Inventory,
    sim: &W,
) -> Option<hands::HeldItem> {
    let s = inv.offhand()?;
    let id = u16::try_from(s.id).ok()?;
    let count = u8::try_from(s.count.clamp(0, 255)).unwrap_or(0);
    Some(sim.held_item(id, count, 0))
}

/// Efficiency level of a stack (see [`enchant_level`]).
fn efficiency(s: &ghast_inventory::Stack) -> u8 {
    enchant_level(s, 32, "minecraft:efficiency")
}

/// An enchantment level from the stack NBT: the 1.8 `ench` list with numeric ids, or the 1.13+
/// `Enchantments` list with names. Parsed only when the stack has NBT.
fn enchant_level(s: &ghast_inventory::Stack, id_1_8: i32, name: &str) -> u8 {
    use ghast_protocol::nbt::Tag;
    let Some(root) = s.nbt.as_ref().and_then(|n| n.parse().ok().flatten()) else {
        return 0;
    };
    let Some(c) = root.tag.as_compound() else {
        return 0;
    };
    let num = |t: Option<&Tag>| match t {
        Some(Tag::Short(v)) => i32::from(*v),
        Some(Tag::Int(v)) => *v,
        Some(Tag::Byte(v)) => i32::from(*v),
        _ => -1,
    };
    for key in ["ench", "Enchantments"] {
        if let Some(Tag::List(l)) = c.get(key) {
            for e in l.items.iter().filter_map(Tag::as_compound) {
                let hit = match e.get("id") {
                    Some(Tag::String(n)) => n == name,
                    other => num(other) == id_1_8,
                };
                if hit {
                    return u8::try_from(num(e.get("lvl")).clamp(0, 255)).unwrap_or(0);
                }
            }
        }
    }
    0
}

impl<S: Sim> Engine<S> {
    pub(crate) fn new(opts: CoreOptions, sim: S) -> Self {
        Engine {
            core: Core::new(opts),
            sim,
            state: BehaviorState::default(),
        }
    }

    /// Bytes arrived from the server (liveness for the silent-server guard).
    pub(crate) fn note_inbound(&mut self, now: Instant) {
        self.core.last_inbound = Some(now);
    }

    /// Time of the last inbound bytes.
    pub(crate) fn last_inbound(&self) -> Option<Instant> {
        self.core.last_inbound
    }

    /// The bot's name (for logs).
    pub(crate) fn name(&self) -> &str {
        &self.core.opts.name
    }

    /// Queues a raw packet copy for the behaviour's next tick.
    pub(crate) fn push_raw(&mut self, p: RawInbound) {
        let ev = &mut self.core.pending;
        if ev.raw.len() < MAX_PENDING_RAW {
            ev.raw.push(p);
        } else {
            ev.raw_dropped += 1;
        }
    }

    /// The raw packet ids the behaviour wants (none once it failed or finished).
    pub(crate) fn raw_packets<B: Behavior>(&self, b: &B) -> std::borrow::Cow<'static, [i32]> {
        if self.state.failed || self.state.done {
            std::borrow::Cow::Borrowed(&[])
        } else {
            b.raw_packets(self.core.opts.version)
        }
    }

    /// The inventory (the connection feeds it its packets).
    pub(crate) fn inventory_mut(&mut self) -> &mut ghast_inventory::Inventory {
        &mut self.core.inv
    }

    /// Outbound packets produced since the last call, in send order.
    pub(crate) fn outbound(&mut self) -> &mut Vec<Outbound> {
        &mut self.core.out
    }

    /// Set when the behaviour asked to disconnect.
    pub(crate) fn quit_requested(&self) -> Option<&str> {
        self.core.quit.as_deref()
    }

    pub(crate) fn core_name(&self) -> &str {
        &self.core.opts.name
    }

    pub(crate) fn tick_count(&self) -> u64 {
        self.core.tick
    }

    /// Applies one inbound packet (except disconnect and compression, which the connection
    /// handles).
    pub(crate) fn handle(&mut self, p: Inbound) {
        let c = &mut self.core;
        let sim = &mut self.sim;
        match p {
            Inbound::KeepAlive(id) => c.out.push(Outbound::KeepAlive(id)),
            Inbound::JoinGame(j) => {
                c.entity_id = j.entity_id;
                c.game_mode = j.game_mode;
                c.respawn_screen = j.respawn_screen;
                // A join always creates a new client world (the world itself was cleared by
                // the world packet routing).
                c.dimension = j.dimension;
                c.joined = true;
                c.pending.joined = true;
                c.health = Health::default();
                c.abilities = Abilities::default();
                c.reset_player("join game");
                sim.reset_player();
                sim.spectator(c.game_mode == GameMode::Spectator);
                // ClientPlayNetworkHandler.onGameJoin: settings, then the brand.
                c.out.push(Outbound::Settings(c.opts.settings.clone()));
                if let Some(brand) = &c.opts.brand {
                    c.out.push(Outbound::Brand(brand.clone()));
                }
                log::info!(
                    "[{}] joined: entity {} {:?} dimension {}",
                    c.opts.name,
                    c.entity_id,
                    c.game_mode,
                    c.dimension
                );
            }
            Inbound::Respawn {
                dimension,
                game_mode,
            } => {
                if dimension != c.dimension {
                    c.dimension = dimension;
                }
                c.game_mode = game_mode;
                c.pending.respawned = true;
                c.reset_player("respawn");
                sim.reset_player();
                sim.spectator(c.game_mode == GameMode::Spectator);
            }
            Inbound::Teleport(t) => {
                if c.teleported_once {
                    log::debug!(
                        "[{}] t{} teleport {:?} {:?} {:?} yaw {:?} pitch {:?} flags {}; we had sent \
                         {:?} {:?} {:?} yaw {:?} pitch {:?} ground {}",
                        c.opts.name,
                        c.tick,
                        t.x,
                        t.y,
                        t.z,
                        t.yaw,
                        t.pitch,
                        t.flags,
                        c.sent.pos.x,
                        c.sent.pos.y,
                        c.sent.pos.z,
                        c.sent.yaw,
                        c.sent.pitch,
                        c.sent.on_ground
                    );
                }
                let (sent_before, pose_before) = (c.sent, c.pose);
                sim.teleport(&mut c.pose, &t);
                if c.pending.teleports.len() < MAX_PENDING_TELEPORTS {
                    c.pending.teleports.push(TeleportEvent {
                        teleport: t,
                        sent_before,
                        pose_before,
                        after: c.pose,
                    });
                }
                if let Some(id) = t.teleport_id {
                    c.out.push(Outbound::TeleportConfirm(id));
                }
                c.out.push(Outbound::Move(MovePacket::PositionLook {
                    pos: c.pose.pos,
                    yaw: c.pose.yaw,
                    pitch: c.pose.pitch,
                    on_ground: false,
                }));
                // The server now has this pose; vanilla's lastReported* are not touched.
                c.sent = Pose {
                    on_ground: false,
                    ..c.pose
                };
                if !c.teleported_once {
                    log::debug!(
                        "[{}] t{} spawn position {:.3} {:.3} {:.3}",
                        c.opts.name,
                        c.tick,
                        c.pose.pos.x,
                        c.pose.pos.y,
                        c.pose.pos.z
                    );
                }
                c.teleported_once = true;
                c.pending.teleported = true;
            }
            Inbound::Chat(m) => {
                if c.pending.chat.len() < MAX_PENDING_CHAT {
                    c.pending.chat.push(m);
                } else {
                    c.pending.chat_dropped += 1;
                }
            }
            Inbound::Health(h) => {
                let was_dead = c.dead;
                c.health = h;
                sim.food(h.food);
                c.pending.health_changed = true;
                if h.health <= 0.0 && !was_dead {
                    c.dead = true;
                    c.pending.died = true;
                    log::info!("[{}] t{} died", c.opts.name, c.tick);
                }
            }
            Inbound::Died { entity_id } => {
                if entity_id == c.entity_id && !c.respawn_screen {
                    // 1.16 immediate respawn: the client respawns without a death screen.
                    c.out.push(Outbound::Respawn);
                }
            }
            Inbound::Abilities(a) => {
                c.abilities = a;
                c.pose.flying = a.flying();
                sim.abilities(a);
            }
            Inbound::HeldSlot(s) => {
                if (0..9).contains(&s) {
                    // Only the inventory's selection changes; the controller's copy does not,
                    // so the next sync echoes it (as vanilla does).
                    if c.held_slot != s as u8 {
                        c.hands.stop_using();
                    }
                    c.held_slot = s as u8;
                }
            }
            Inbound::GameState { reason, value } => match reason {
                3 => {
                    c.game_mode = GameMode::from_wire(value as u8);
                    sim.spectator(c.game_mode == GameMode::Spectator);
                    c.pending.game_mode_changed = true;
                }
                // 1.15+: "immediate respawn" gamerule (1 = no respawn screen).
                11 if c.opts.version != GameVersion::V1_8_9 => c.respawn_screen = value == 0.0,
                _ => {}
            },
            // Window confirms are answered by the inventory (exactly once).
            Inbound::Transaction { .. } => {}
            Inbound::OpenWindow(id) => c.open_window = id,
            Inbound::CloseWindow(_) => c.open_window = 0,
            Inbound::Velocity { entity_id, motion } => {
                if entity_id == c.entity_id {
                    sim.velocity(motion, false);
                }
            }
            Inbound::ExplosionPush(motion) => sim.velocity(motion, true),
            Inbound::MovementSpeed {
                entity_id,
                base,
                modifiers,
            } => {
                if entity_id == c.entity_id {
                    log::debug!(
                        "[{}] t{} movement speed base {base:?} modifiers {modifiers:?}",
                        c.opts.name,
                        c.tick
                    );
                    let mods: Vec<(u128, f64, u8)> = modifiers
                        .iter()
                        .map(|&(id, amount, op)| (id.as_u128(), amount, op as u8))
                        .collect();
                    sim.movement_speed(base, &mods);
                }
            }
            Inbound::Effect {
                entity_id,
                id,
                amplifier,
            } => {
                if entity_id == c.entity_id {
                    c.effects.insert(id, amplifier);
                    sim.status_effects(&c.effects);
                }
            }
            Inbound::EffectRemoved { entity_id, id } => {
                if entity_id == c.entity_id {
                    c.effects.remove(&id);
                    sim.status_effects(&c.effects);
                }
            }
            Inbound::EntityStatus { entity_id, status } => {
                // 9: the server finished our item use (eating, drinking).
                if entity_id == c.entity_id && status == 9 {
                    c.hands.stop_using();
                }
            }
            Inbound::Disconnect(_) | Inbound::SetCompression(_) => {}
        }
    }

    /// The interests of the behaviour (the packet filter follows them).
    pub(crate) fn interests<B: Behavior>(&self, b: &B) -> Interests {
        if self.state.failed {
            Interests::NONE
        } else {
            b.interests()
        }
    }

    /// Ends the behaviour (session over).
    pub(crate) fn stop<B: Behavior>(&mut self, b: &mut B, reason: &str) {
        if self.state.started && !self.state.failed {
            self.state.failed = true;
            let name = self.core.opts.name.clone();
            if catch_unwind(AssertUnwindSafe(|| b.stop(reason))).is_err() {
                log::error!("[{name}] behaviour {} panicked in stop", b.name());
            }
        }
    }

    /// One client tick at `now`.
    pub(crate) fn tick<B: Behavior>(&mut self, b: &mut B, now: Instant) {
        let c = &mut self.core;
        c.tick += 1;
        // Per-tick packet-order guard and click timeout of the inventory.
        c.inv.tick();
        let was_ready = c.ready;
        c.ready = c.teleported_once && c.column_loaded(&self.sim);
        if c.ready != was_ready {
            if c.ready {
                log::info!(
                    "[{}] t{} in the world at {:.3} {:.3} {:.3}",
                    c.opts.name,
                    c.tick,
                    c.pose.pos.x,
                    c.pose.pos.y,
                    c.pose.pos.z
                );
            } else {
                log::debug!(
                    "[{}] t{} player column not loaded: not ticking",
                    c.opts.name,
                    c.tick
                );
            }
        }
        if !c.ready {
            // The client player does not tick outside a loaded column (1.16 vanilla; on 1.8
            // the bot waits for its column as well).
            return;
        }

        let silent_for = c.last_inbound.map(|t| now.saturating_duration_since(t));
        let silent = silent_for.is_some_and(|d| d > c.opts.silent_hold);
        if silent != c.silent {
            c.silent = silent;
            if silent {
                log::warn!(
                    "[{}] t{} server silent for {} ms: behaviour on hold",
                    c.opts.name,
                    c.tick,
                    silent_for.unwrap_or_default().as_millis()
                );
            } else {
                log::info!(
                    "[{}] t{} server answering again: behaviour resumes",
                    c.opts.name,
                    c.tick
                );
            }
        }

        if silent {
            // Freeze: release the keys, keep only the vanilla movement packet.
            c.controls = Controls::default();
            c.aimed = false;
        } else if !self.state.failed && !self.state.done {
            std::mem::swap(&mut c.pending, &mut c.current);
            c.pending.clear();
            c.current.inventory = c.inv.drain_events();
            c.current.block_updates_dropped =
                self.sim.drain_block_updates(&mut c.current.block_updates);
            if c.current.inventory.player_changed {
                // Armour enchantments that change movement / digging.
                let ench = |a: usize, id: i32, name: &str| {
                    c.inv.armor(a).map_or(0, |s| enchant_level(s, id, name))
                };
                let depth = ench(3, 8, "minecraft:depth_strider");
                let soul = ench(3, -1, "minecraft:soul_speed");
                c.aqua_affinity = ench(0, 6, "minecraft:aqua_affinity") > 0;
                self.sim.boots(depth, soul);
            }
            let started = self.state.started;
            self.state.started = true;
            if !started {
                c.rotation.reset(c.pose.yaw, c.pose.pitch);
            }
            let name = c.opts.name.clone();
            let result = catch_unwind(AssertUnwindSafe(|| {
                let held = held_item(&c.inv, &self.sim);
                let mut ctx = TickContext {
                    core: c,
                    world: &self.sim,
                    held,
                };
                if !started {
                    b.start(&mut ctx);
                }
                b.tick(&mut ctx);
                b.running()
            }));
            match result {
                Ok(true) => {}
                Ok(false) => {
                    self.state.done = true;
                    log::info!("[{name}] behaviour {} finished", b.name());
                }
                Err(_) => {
                    log::error!("[{name}] behaviour {} panicked: stopped", b.name());
                    self.stop(b, "panic");
                }
            }
        }

        let c = &mut self.core;
        // ClientPlayerInteractionManager.tick (syncSelectedSlot) runs before the key
        // processing: a held slot the server changed is echoed before any click.
        if c.held_slot != c.sent_slot {
            c.sent_slot = c.held_slot;
            c.out.push(Outbound::HeldSlot(i16::from(c.sent_slot)));
        }
        // MinecraftClient.tick key processing (a GUI takes the mouse: buttons up).
        {
            // The screen as the inventory models it (vanilla: a window the client has no
            // screen for opens none; a window the client closed is gone at once).
            let gui = c.inv.gui_open();
            let eye = if c.pose.sneaking {
                c.opts.version.sneak_eye_height()
            } else {
                c.opts.version.eye_height()
            };
            let eyes = Vec3::new(c.pose.pos.x, c.pose.pos.y + eye, c.pose.pos.z);
            let sim = &self.sim;
            let blocks = |x: i32, y: i32, z: i32| sim.block_query(x, y, z);
            let look = hands::look_vector(c.pose.yaw, c.pose.pitch);
            let target = hands::pick(&blocks, eyes, look, hands::REACH);
            let entity = entity_pick(sim, c.opts.version, eyes, look, target.map(|t| t.distance));
            let in_water = blocks(
                eyes.x.floor() as i32,
                eyes.y.floor() as i32,
                eyes.z.floor() as i32,
            )
            .is_some_and(|b| b.water);
            let held = held_item(&c.inv, sim);
            let mut placed = false;
            let first = c.out.len();
            let mut io = HandsIo {
                blocks: &blocks,
                target,
                held,
                held_stack: c.inv.held().cloned(),
                food: c.health.food,
                modern: c.opts.version != GameVersion::V1_8_9,
                off_held: off_held_item(&c.inv, sim),
                off_stack: c.inv.offhand().cloned(),
                entity,
                factors: DigFactors {
                    on_ground: c.pose.on_ground,
                    in_water,
                    aqua_affinity: c.aqua_affinity,
                    haste: c.effects.get(&3).map_or(0, |a| (*a as u8).wrapping_add(1)),
                    fatigue: c.effects.get(&4).map_or(0, |a| (*a as u8).wrapping_add(1)),
                },
                sneaking: c.pose.sneaking,
                out: &mut c.out,
                broken: &mut c.broken_now,
                placed: &mut placed,
            };
            let (left, right) = (c.buttons.left && !gui, c.buttons.right && !gui);
            c.hands.tick(left, right, &mut io);
            // Other placements: the server's set_slot brings the new count of the held stack.
            let _ = placed;
            predict_planting(c, sim, first);
        }
        for p in c.broken_now.drain(..) {
            self.sim.predict_air(p.x, p.y, p.z);
        }
        // ClientPlayerEntity.tickMovement: a flight toggle is told to the server before the move.
        if let Some(f) = c.want_flying.take() {
            let first = c.out.len();
            self.sim.set_flying(f, &mut c.out);
            if c.out.len() > first {
                c.pose.flying = f;
            } else if f && !c.pose.flying {
                log::debug!(
                    "[{}] t{} set_flying(true) refused: flight not allowed",
                    c.opts.name,
                    c.tick
                );
            }
        }
        // Rotation runs only on ticks the behaviour aimed (budgets untouched otherwise).
        c.turned = false;
        #[cfg(test)]
        if let Some((yaw, pitch)) = c.forced_look.take() {
            c.turned = yaw != c.pose.yaw || pitch != c.pose.pitch;
            c.pose.yaw = yaw;
            c.pose.pitch = pitch;
            c.aimed = false;
        }
        if c.aimed {
            let (yaw, pitch) = c.rotation.step(c.pose.yaw, c.pose.pitch);
            if c.opts.hold_turn_on_action && !post_look_ok(c, yaw, pitch) {
                // Grim RotationPlace re-checks a dig / place with the look of the movement
                // packet that follows it: keep the mouse still on the clicked block this tick.
                c.held_turns += 1;
            } else {
                c.turned = yaw != c.pose.yaw || pitch != c.pose.pitch;
                c.pose.yaw = yaw;
                c.pose.pitch = pitch;
            }
            c.aimed = false;
        }
        let first = c.out.len();
        let using = c.hands.using_item();
        self.sim
            .step(&mut c.pose, c.controls, using, c.entity_id, &mut c.out);
        for o in &c.out[first..] {
            let Outbound::Move(m) = o else { continue };
            match *m {
                MovePacket::Position { pos, .. } => c.sent.pos = pos,
                MovePacket::Look { yaw, pitch, .. } => {
                    c.sent.yaw = yaw;
                    c.sent.pitch = pitch;
                }
                MovePacket::PositionLook {
                    pos, yaw, pitch, ..
                } => {
                    c.sent.pos = pos;
                    c.sent.yaw = yaw;
                    c.sent.pitch = pitch;
                }
                MovePacket::Flying { .. } => {}
            }
            if matches!(
                m,
                MovePacket::Position { .. } | MovePacket::PositionLook { .. }
            ) {
                c.sent.motion = c.pose.motion;
            }
        }
        c.sent.on_ground = c.pose.on_ground;
        c.sent.sneaking = c.pose.sneaking;
        c.sent.sprinting = c.pose.sprinting;
        c.sent.flying = c.pose.flying;
        c.swings += count_swings(&c.out);

        if self.state.started && !self.state.failed {
            let name = c.opts.name.clone();
            let view = TickView {
                core: &self.core,
                world: &self.sim,
            };
            if catch_unwind(AssertUnwindSafe(|| b.after_tick(&view))).is_err() {
                log::error!(
                    "[{name}] behaviour {} panicked in after_tick: stopped",
                    b.name()
                );
                self.stop(b, "panic");
            }
        }
    }
}

/// Arm swings (main or off hand) in a packet list.
fn count_swings(out: &[Outbound]) -> u64 {
    out.iter()
        .filter(|o| matches!(o, Outbound::Swing | Outbound::SwingOffHand))
        .count() as u64
}

/// A read-only view after the movement step ([`Behavior::after_tick`]).
pub struct TickView<'a> {
    core: &'a Core,
    world: &'a dyn WorldView,
}

impl std::fmt::Debug for TickView<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TickView")
            .field("tick", &self.core.tick)
            .field("sent", &self.core.sent)
            .finish_non_exhaustive()
    }
}

impl TickView<'_> {
    /// Ticks since the session started.
    #[must_use]
    pub fn tick(&self) -> u64 {
        self.core.tick
    }

    /// The protocol version.
    #[must_use]
    pub fn version(&self) -> GameVersion {
        self.core.opts.version
    }

    /// The client pose after this tick's movement step.
    #[must_use]
    pub fn pose(&self) -> &Pose {
        &self.core.pose
    }

    /// What the server has after this tick's movement packet (position, look, flags).
    #[must_use]
    pub fn sent(&self) -> &Pose {
        &self.core.sent
    }

    /// The world.
    #[must_use]
    pub fn world(&self) -> &dyn WorldView {
        self.world
    }

    /// The tracked entities (with [`Interests::ENTITIES`]).
    #[must_use]
    pub fn entities(&self) -> Option<&ghast_entity::Entities> {
        self.world.entities()
    }

    /// The inventory and the open window.
    #[must_use]
    pub fn inventory(&self) -> &ghast_inventory::Inventory {
        &self.core.inv
    }

    /// What the behaviour saw this tick (the events of [`TickContext::events`]).
    #[must_use]
    pub fn events(&self) -> &TickEvents {
        &self.core.current
    }

    /// Our entity id.
    #[must_use]
    pub fn entity_id(&self) -> i32 {
        self.core.entity_id
    }

    /// Health, food and saturation.
    #[must_use]
    pub fn health(&self) -> Health {
        self.core.health
    }

    /// Game mode.
    #[must_use]
    pub fn game_mode(&self) -> GameMode {
        self.core.game_mode
    }

    /// The selected hotbar slot.
    #[must_use]
    pub fn held_slot(&self) -> u8 {
        self.core.held_slot
    }

    /// The block being broken and its progress 0 … 1.
    #[must_use]
    pub fn mining(&self) -> Option<(ghast_protocol::BlockPos, f32)> {
        self.core.hands.mining()
    }

    /// Arm swings sent so far (see [`TickContext::swings`]).
    #[must_use]
    pub fn swings(&self) -> u64 {
        self.core.swings
    }

    /// Blocks broken so far.
    #[must_use]
    pub fn broken(&self) -> u64 {
        self.core.hands.broken
    }

    /// The current dimension (`"0"` / `"-1"` / `"1"` on 1.8, the world name on 1.16), as
    /// [`TickContext::dimension`].
    #[must_use]
    pub fn dimension(&self) -> &str {
        &self.core.dimension
    }

    /// Using the held item (eating, blocking, drawing a bow): movement input is slowed. As
    /// [`TickContext::using_item`].
    #[must_use]
    pub fn using_item(&self) -> bool {
        self.core.hands.using_item()
    }
}

#[cfg(test)]
impl Core {
    /// Test hook: the held slot as both the client and the server have it (no sync packet).
    pub(crate) fn test_set_slot(&mut self, slot: u8) {
        self.held_slot = slot;
        self.sent_slot = slot;
        self.inv.select(slot);
    }

    /// Test hook: the pose and the sent pose.
    pub(crate) fn test_poses(&mut self) -> (&mut Pose, &mut Pose) {
        (&mut self.pose, &mut self.sent)
    }

    /// Test hook: turns held back by the post-look check.
    pub(crate) fn test_held_turns(&self) -> u64 {
        self.held_turns
    }
}

#[cfg(test)]
impl TickContext<'_> {
    /// Test hook: this tick's look without a rotation step.
    pub(crate) fn test_force_look(&mut self, yaw: f32, pitch: f32) {
        self.core.forced_look = Some((yaw, pitch));
    }

    /// Test hook: the rotation controller (to predict a step).
    pub(crate) fn test_rotation(&self) -> Rotation {
        self.core.rotation
    }
}

/// 1.8 `SeedItem.use` / `CropItem.use` on the client: planting on the top of
/// its soil with air above uses one item at once (the server's set_slot follows later; it
/// sends none when the count already agrees). Applied to the `block_place` packets written
/// from `first` on. Other placements wait for the server's set_slot: predicting them needs
/// vanilla's full placement rules, and a wrong prediction would never be corrected.
fn predict_planting(c: &mut Core, world: &dyn WorldView, first: usize) {
    if c.opts.version != GameVersion::V1_8_9 || c.game_mode == GameMode::Creative {
        return;
    }
    let mut uses = 0u8;
    for o in &c.out[first..] {
        let Outbound::Place {
            pos,
            face: crate::common::Face::Up,
            held: Some(held),
            off_hand: false,
            ..
        } = o
        else {
            continue;
        };
        // Seed item → its soil (wheat, pumpkin, melon seeds, carrot, potato: farmland;
        // nether wart: soul sand).
        let soil = match held.id {
            295 | 361 | 362 | 391 | 392 => 60,
            372 => 88,
            _ => continue,
        };
        let id = |y: i32| world.raw_block(pos.x, y, pos.z).map(|s| s >> 4);
        if held.count > i32::from(uses) && id(pos.y) == Some(soil) && id(pos.y + 1) == Some(0) {
            uses += 1;
        }
    }
    if uses > 0 {
        c.inv.consume_held(uses);
    }
}

/// Whether the look `yaw`/`pitch` still points at every block dug or placed this tick (its
/// full cube, from the sent and the current feet, standing or sneaking eyes, with the old or the
/// new yaw) — the post-look check that keeps Grim's RotationPlace check satisfied.
fn post_look_ok(c: &Core, yaw: f32, pitch: f32) -> bool {
    let looks = [
        hands::look_vector(c.pose.yaw, pitch),
        hands::look_vector(yaw, pitch),
    ];
    let heights = [
        c.opts.version.eye_height(),
        c.opts.version.sneak_eye_height(),
    ];
    c.out
        .iter()
        .filter_map(|o| match o {
            Outbound::Dig {
                status: crate::common::DigStatus::Start | crate::common::DigStatus::Finish,
                pos,
                ..
            }
            | Outbound::Place { pos, .. } => Some(*pos),
            _ => None,
        })
        .all(|t| {
            [c.sent.pos, c.pose.pos].iter().all(|o| {
                heights.iter().any(|h| {
                    let eyes = Vec3::new(o.x, o.y + *h, o.z);
                    looks
                        .iter()
                        .any(|l| hands::ray_hits_cube(eyes, *l, t, ACTION_LOOK_REACH))
                })
            })
        })
}

/// Reach of the post-look check (6 blocks).
const ACTION_LOOK_REACH: f64 = 6.0;

/// The behaviour's view of the client during one tick.
///
/// Reading is free; actions are written to the server in call order before this tick's
/// movement packet.
pub struct TickContext<'a> {
    core: &'a mut Core,
    world: &'a dyn WorldView,
    held: Option<hands::HeldItem>,
}

impl std::fmt::Debug for TickContext<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TickContext")
            .field("tick", &self.core.tick)
            .field("pose", &self.core.pose)
            .finish_non_exhaustive()
    }
}

impl TickContext<'_> {
    /// Ticks since the connection entered the play state.
    #[must_use]
    pub fn tick(&self) -> u64 {
        self.core.tick
    }

    /// The protocol version of this connection.
    #[must_use]
    pub fn version(&self) -> GameVersion {
        self.core.opts.version
    }

    /// The client pose now (start of the tick, before this tick's rotation and movement).
    #[must_use]
    pub fn pose(&self) -> &Pose {
        &self.core.pose
    }

    /// The last pose written to the server: what it judges this tick's actions against.
    #[must_use]
    pub fn sent(&self) -> &Pose {
        &self.core.sent
    }

    /// Eye position of `pose` (standing or sneaking eye height of this version).
    #[must_use]
    pub fn eyes(&self, pose: &Pose) -> Vec3 {
        let v = self.core.opts.version;
        pose.eyes(if pose.sneaking {
            v.sneak_eye_height()
        } else {
            v.eye_height()
        })
    }

    /// The world (loaded chunk columns and their blocks).
    #[must_use]
    pub fn world(&self) -> &dyn WorldView {
        self.world
    }

    /// What happened since the previous tick.
    #[must_use]
    pub fn events(&self) -> &TickEvents {
        &self.core.current
    }

    /// Our entity id.
    #[must_use]
    pub fn entity_id(&self) -> i32 {
        self.core.entity_id
    }

    /// Arm swings (main and off hand) written so far in the session: every `arm_animation`,
    /// whether from [`TickContext::swing`], an action (place, dig, attack) or the held buttons
    /// (digging swings every tick). Includes this tick's written so far.
    #[must_use]
    pub fn swings(&self) -> u64 {
        self.core.swings + count_swings(&self.core.out)
    }

    /// Health, food and saturation.
    #[must_use]
    pub fn health(&self) -> Health {
        self.core.health
    }

    /// The player is dead (waiting for [`TickContext::respawn`]).
    #[must_use]
    pub fn is_dead(&self) -> bool {
        self.core.dead
    }

    /// Game mode.
    #[must_use]
    pub fn game_mode(&self) -> GameMode {
        self.core.game_mode
    }

    /// Abilities as the server sent them.
    #[must_use]
    pub fn abilities(&self) -> Abilities {
        self.core.abilities
    }

    /// The server allows flight.
    #[must_use]
    pub fn can_fly(&self) -> bool {
        self.core.abilities.may_fly()
    }

    /// The selected hotbar slot (0 … 8).
    #[must_use]
    pub fn held_slot(&self) -> u8 {
        self.core.held_slot
    }

    /// Id of the open container window, if the server opened one.
    #[must_use]
    pub fn open_window(&self) -> Option<i32> {
        (self.core.open_window != 0).then_some(self.core.open_window)
    }

    /// The current dimension (`"0"` / `"-1"` / `"1"` on 1.8, the world name on 1.16).
    #[must_use]
    pub fn dimension(&self) -> &str {
        &self.core.dimension
    }

    /// The movement controls recorded for this tick.
    #[must_use]
    pub fn controls(&self) -> Controls {
        self.core.controls
    }

    /// The rotation target.
    #[must_use]
    pub fn aim(&self) -> (f32, f32) {
        self.core.rotation.target()
    }

    // ---- actions: written now, before this tick's movement packet ----

    /// Sends a chat line or command (truncated to the version's limit, 100 / 256 UTF-16
    /// units, like vanilla).
    pub fn chat(&mut self, text: impl Into<String>) {
        self.core.out.push(Outbound::Chat(text.into()));
    }

    /// Asks the server to respawn the player. Only sent while dead; returns whether it was.
    pub fn respawn(&mut self) -> bool {
        if self.core.dead {
            self.core.out.push(Outbound::Respawn);
            true
        } else {
            false
        }
    }

    /// Selects a hotbar slot (0 … 8); sends `held_item_slot` when it changes. Out-of-range
    /// slots are ignored.
    pub fn select_slot(&mut self, slot: u8) {
        if slot < 9 {
            if self.core.held_slot != slot {
                self.core.hands.stop_using();
                self.core.hands.ticks_since_swing = 0;
            }
            self.core.held_slot = slot;
            self.core.inv.select(slot);
            if self.core.sent_slot != slot {
                self.core.sent_slot = slot;
                self.core.out.push(Outbound::HeldSlot(i16::from(slot)));
            }
        }
    }

    /// Swings the arm (`arm_animation`).
    pub fn swing(&mut self) {
        self.core.out.push(Outbound::Swing);
    }

    /// Ends the session after this tick's packets are written.
    pub fn disconnect(&mut self, reason: impl Into<String>) {
        self.core.quit = Some(reason.into());
    }

    // ---- intent for this tick's movement step ----

    /// Rotation target for this tick: the client turns toward it in whole mouse counts
    /// (at most 14° yaw / 10.5° pitch per tick). Call it every tick you want to turn.
    pub fn set_aim(&mut self, yaw: f32, pitch: f32) {
        self.core.rotation.set_target(yaw, pitch);
        self.core.aimed = true;
    }

    /// Movement keys from now on (kept until changed); the physics turns them into movement
    /// like vanilla's `Input` (sneaking scales walking by 0.3).
    pub fn set_controls(&mut self, controls: Controls) {
        self.core.controls = controls;
    }

    /// The inventory: player slots, cursor, the open window, the pending click.
    #[must_use]
    pub fn inventory(&self) -> &ghast_inventory::Inventory {
        &self.core.inv
    }

    /// Clicks in the open screen (`window_click`, written now). Refused (nothing sent) while
    /// the player is turning (the look changed in the last movement step or an aim is set for
    /// this tick: clicks only while still) or when the inventory refuses it (one click in
    /// flight, same-tick packet order, unpredictable result, …). The result arrives in
    /// [`TickEvents::inventory`] on a later tick.
    pub fn click(&mut self, click: ghast_inventory::Click) -> Result<ClickTicket, ClickRefused> {
        if self.core.turned || self.core.aimed {
            return Err(ClickRefused::Turning);
        }
        let o = self
            .core
            .inv
            .click(click)
            .map_err(ClickRefused::Inventory)?;
        let ticket = match &o {
            ghast_inventory::Outgoing::Click(p) => ClickTicket {
                window_id: i32::from(p.window_id),
                action: p.action,
            },
            _ => ClickTicket {
                window_id: -1,
                action: -1,
            },
        };
        self.core.out.push(Outbound::Inventory(o));
        Ok(ticket)
    }

    /// Opens the player inventory GUI (1.8: the "open inventory" client command; 1.16: nothing
    /// is sent). Window 0 clicks need it open.
    pub fn open_inventory(&mut self) -> Result<(), ClickRefused> {
        if let Some(o) = self
            .core
            .inv
            .open_inventory()
            .map_err(ClickRefused::Inventory)?
        {
            self.core.out.push(Outbound::Inventory(o));
        }
        Ok(())
    }

    /// Closes the open GUI (`close_window` with its id; 0 for the player inventory).
    pub fn close_window(&mut self) -> Result<(), ClickRefused> {
        let o = self.core.inv.close().map_err(ClickRefused::Inventory)?;
        self.core.out.push(Outbound::Inventory(o));
        // Vanilla closeHandledScreen: the container ends on the client too.
        self.core.open_window = 0;
        Ok(())
    }

    /// Mouse buttons from now on (kept until changed; released by a join, a respawn or an
    /// open window). Left held mines what the crosshair is on, right held uses / places
    /// (vanilla's key processing, after the behaviour and before the movement packet).
    pub fn set_buttons(&mut self, buttons: Buttons) {
        self.core.buttons = buttons;
    }

    /// The buttons held.
    #[must_use]
    pub fn buttons(&self) -> Buttons {
        self.core.buttons
    }

    /// What the crosshair is on as the server judges it: the first block within reach 4.5
    /// along the **sent** look from the sent eyes.
    #[must_use]
    pub fn target(&self) -> Option<BlockHit> {
        let s = &self.core.sent;
        let v = self.core.opts.version;
        let eye = if s.sneaking {
            v.sneak_eye_height()
        } else {
            v.eye_height()
        };
        let world = self.world;
        let blocks = |x: i32, y: i32, z: i32| world.block_query(x, y, z);
        hands::pick(
            &blocks,
            Vec3::new(s.pos.x, s.pos.y + eye, s.pos.z),
            hands::look_vector(s.yaw, s.pitch),
            hands::REACH,
        )
    }

    /// The tracked entities (behaviours that ask for [`Interests::ENTITIES`]).
    #[must_use]
    pub fn entities(&self) -> Option<&ghast_entity::Entities> {
        self.world.entities()
    }

    /// The entity the crosshair is on as the server judges it (from the sent pose; vanilla's
    /// entity pick: reach 3, 1.8 boxes grown by 0.1, closer than the block hit).
    #[must_use]
    pub fn target_entity(&self) -> Option<hands::EntityHit> {
        let s = &self.core.sent;
        let v = self.core.opts.version;
        let eye = if s.sneaking {
            v.sneak_eye_height()
        } else {
            v.eye_height()
        };
        let eyes = Vec3::new(s.pos.x, s.pos.y + eye, s.pos.z);
        let look = hands::look_vector(s.yaw, s.pitch);
        let block = self.target().map(|t| t.distance);
        entity_pick(self.world, v, eyes, look, block)
    }

    /// Attacks `entity` (`use_entity` attack and the arm swing in the version's order; 1.16
    /// resets the attack cooldown). Refused (`false`, nothing sent) unless the sent crosshair is
    /// on that entity.
    pub fn attack(&mut self, entity: i32) -> bool {
        if self.target_entity().is_none_or(|e| e.id != entity) {
            return false;
        }
        let world = self.world;
        let blocks = |x: i32, y: i32, z: i32| world.block_query(x, y, z);
        let c = &mut *self.core;
        let modern = c.opts.version != GameVersion::V1_8_9;
        let mut placed = false;
        let mut io = HandsIo {
            blocks: &blocks,
            target: None,
            held: self.held,
            held_stack: None,
            food: c.health.food,
            modern,
            off_held: None,
            off_stack: None,
            entity: None,
            factors: DigFactors::default(),
            sneaking: c.pose.sneaking,
            out: &mut c.out,
            broken: &mut c.broken_now,
            placed: &mut placed,
        };
        if !modern {
            io.out.push(Outbound::Swing);
        }
        c.hands.attack(entity, &mut io);
        if modern {
            c.out.push(Outbound::Swing);
        }
        true
    }

    /// 1.16 attack strength 0 … 1 for an attack speed (4.0 for an empty hand, swords 1.6,
    /// axes 0.8–1.0); 1.0 on 1.8 (no cooldown).
    #[must_use]
    pub fn attack_strength(&self, attack_speed: f64) -> f32 {
        if self.core.opts.version == GameVersion::V1_8_9 {
            1.0
        } else {
            self.core.hands.attack_strength(attack_speed)
        }
    }

    /// The block being broken with the left button and its progress 0 … 1.
    #[must_use]
    pub fn mining(&self) -> Option<(ghast_protocol::BlockPos, f32)> {
        self.core.hands.mining()
    }

    /// Blocks broken so far (finish sent).
    #[must_use]
    pub fn broken(&self) -> u64 {
        self.core.hands.broken
    }

    /// Using the held item (eating, blocking, drawing a bow): movement input is slowed.
    #[must_use]
    pub fn using_item(&self) -> bool {
        self.core.hands.using_item()
    }

    /// Starts digging `pos` on `face` (`block_dig` start, then an arm swing), breaking it at
    /// once when it is instant for the held item. Refused (nothing sent, `false`) unless the
    /// crosshair from the sent pose is on exactly that block face.
    pub fn dig(&mut self, pos: ghast_protocol::BlockPos, face: crate::common::Face) -> bool {
        if !self
            .target()
            .is_some_and(|t| t.pos == pos && t.face == face)
        {
            return false;
        }
        let world = self.world;
        let blocks = |x: i32, y: i32, z: i32| world.block_query(x, y, z);
        let c = &mut *self.core;
        let mut placed = false;
        let mut io = HandsIo {
            blocks: &blocks,
            target: None,
            held: self.held,
            held_stack: c.inv.held().cloned(),
            food: c.health.food,
            modern: c.opts.version != GameVersion::V1_8_9,
            off_held: None,
            off_stack: None,
            entity: None,
            factors: DigFactors {
                on_ground: c.pose.on_ground,
                in_water: false,
                aqua_affinity: c.aqua_affinity,
                haste: c.effects.get(&3).map_or(0, |a| (*a as u8).wrapping_add(1)),
                fatigue: c.effects.get(&4).map_or(0, |a| (*a as u8).wrapping_add(1)),
            },
            sneaking: c.pose.sneaking,
            out: &mut c.out,
            broken: &mut c.broken_now,
            placed: &mut placed,
        };
        c.hands.click_block(pos, face, &mut io);
        c.out.push(Outbound::Swing);
        true
    }

    /// Right-clicks `support`'s `face` at the absolute point `hit` (`block_place`, then an arm
    /// swing): places the held block or activates the block. Refused (nothing sent, `false`)
    /// unless the crosshair from the sent pose is on exactly that block face.
    pub fn place(
        &mut self,
        support: ghast_protocol::BlockPos,
        face: crate::common::Face,
        hit: Vec3,
    ) -> bool {
        let Some(t) = self.target().filter(|t| t.pos == support && t.face == face) else {
            return false;
        };
        let world = self.world;
        let Some(b) = world.block_query(support.x, support.y, support.z) else {
            return false;
        };
        let blocks = |x: i32, y: i32, z: i32| world.block_query(x, y, z);
        let c = &mut *self.core;
        let mut placed = false;
        let first = c.out.len();
        let mut io = HandsIo {
            blocks: &blocks,
            target: None,
            held: self.held,
            held_stack: c.inv.held().cloned(),
            food: c.health.food,
            modern: c.opts.version != GameVersion::V1_8_9,
            off_held: None,
            off_stack: None,
            entity: None,
            factors: DigFactors::default(),
            sneaking: c.pose.sneaking,
            out: &mut c.out,
            broken: &mut c.broken_now,
            placed: &mut placed,
        };
        c.hands
            .right_click_block(BlockHit { hit, ..t }, &b, &mut io);
        let _ = placed;
        predict_planting(c, world, first);
        c.out.push(Outbound::Swing);
        true
    }

    /// When the last inbound bytes arrived (a gap before a setback means lag).
    #[must_use]
    pub fn last_inbound(&self) -> Option<Instant> {
        self.core.last_inbound
    }

    /// The yaw / pitch this tick's movement step will use, from this tick's
    /// [`TickContext::set_aim`] and the digs / places written so far (incl. the turn held by
    /// [`crate::Client::hold_turn_on_action`]). Computed on a copy: the rotation's carried
    /// budget does not change. Clicks of held buttons, which run after the behaviour, are not
    /// seen. Without an aim this tick: the current look.
    #[must_use]
    pub fn planned_look(&self) -> (f32, f32) {
        let c = &*self.core;
        let now = (c.pose.yaw, c.pose.pitch);
        #[cfg(test)]
        if let Some(l) = c.forced_look {
            return l;
        }
        if !c.aimed {
            return now;
        }
        let mut r = c.rotation;
        let (yaw, pitch) = r.step(c.pose.yaw, c.pose.pitch);
        if c.opts.hold_turn_on_action && !post_look_ok(c, yaw, pitch) {
            now
        } else {
            (yaw, pitch)
        }
    }

    /// Starts or stops creative-style flight: the `abilities` packet goes out before this
    /// tick's movement step, like a double-tapped jump (but without the jump). Starting needs
    /// the server's "may fly" ability; asking for the current state sends nothing.
    pub fn set_flying(&mut self, flying: bool) {
        self.core.want_flying = Some(flying);
    }

    /// Turns held back so far because they would have taken the crosshair off a block dug or
    /// placed on the same tick (see [`crate::Client::hold_turn_on_action`]).
    #[must_use]
    pub fn held_turns(&self) -> u64 {
        self.core.held_turns
    }

    /// Behaviour status line (logged when it changes).
    pub fn status(&mut self, text: &str) {
        if self.core.status != text {
            text.clone_into(&mut self.core.status);
            log::info!(
                "[{}] t{} status: {text}",
                self.core.opts.name,
                self.core.tick
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::Teleport;
    use crate::common::{ChatPosition, EntityAction, JoinGame};
    use ghast_protocol::RawPacket;

    fn opts(version: GameVersion) -> CoreOptions {
        CoreOptions {
            name: "test".into(),
            version,
            settings: ClientSettings::default(),
            brand: Some("vanilla".into()),
            sensitivity: 0.5,
            silent_hold: Duration::from_millis(1500),
            hold_turn_on_action: true,
            inventory: match version {
                GameVersion::V1_8_9 => ghast_inventory::Version::V1_8,
                _ => ghast_inventory::Version::V1_16_5,
            },
        }
    }

    /// A 1.8.9 engine (physics) or a 1.16.5 one (stand-still mover).
    fn engine(version: GameVersion) -> Engine<Box<dyn Sim>> {
        let sim: Box<dyn Sim> = match version {
            GameVersion::V1_8_9 => Box::new(crate::sim::Sim18::new()),
            _ => Box::new(crate::sim::Sim16::new()),
        };
        Engine::new(opts(version), sim)
    }

    /// Loads the column of chunk (x, z) the way the connection does.
    fn load(e: &mut Engine<Box<dyn Sim>>, x: i32, z: i32) {
        let raw = match e.core.opts.version {
            GameVersion::V1_8_9 => crate::sim::test_chunks::v1_8(x, z),
            _ => crate::sim::test_chunks::v1_16_5(x, z),
        };
        e.sim.world_packet(&raw).unwrap();
    }

    fn join(e: &mut Engine<Box<dyn Sim>>) {
        e.handle(Inbound::JoinGame(JoinGame {
            entity_id: 7,
            game_mode: GameMode::Survival,
            dimension: "0".into(),
            hardcore: false,
            dimension_nbt: None,
            respawn_screen: true,
        }));
        load(e, 0, 0);
        e.handle(Inbound::Teleport(Teleport {
            x: 8.5,
            y: 64.0,
            z: 8.5,
            yaw: 90.0,
            pitch: 0.0,
            flags: 0,
            teleport_id: None,
        }));
        e.outbound().clear();
    }

    /// Records the tick's packets as names.
    fn names(out: &mut Vec<Outbound>) -> Vec<&'static str> {
        let v = out
            .iter()
            .map(|o| match o {
                Outbound::Move(m) => m.trace_id(),
                Outbound::Chat(_) => "chat",
                Outbound::HeldSlot(_) => "held_slot",
                Outbound::Swing => "swing",
                Outbound::EntityAction { .. } => "entity_action",
                Outbound::KeepAlive(_) => "keep_alive",
                Outbound::Transaction { .. } => "transaction",
                Outbound::TeleportConfirm(_) => "teleport_confirm",
                Outbound::Respawn => "respawn",
                Outbound::Settings(_) => "settings",
                Outbound::Brand(_) => "brand",
                Outbound::Abilities { .. } => "abilities",
                Outbound::Dig { .. } => "block_dig",
                Outbound::Place { .. } | Outbound::UseItem { .. } => "block_place",
                Outbound::SwingOffHand => "swing_off",
                Outbound::UseEntity { .. } => "use_entity",
                Outbound::Inventory(_) => "inventory",
            })
            .collect();
        out.clear();
        v
    }

    struct Script<F: FnMut(&mut TickContext<'_>)>(F);
    impl<F: FnMut(&mut TickContext<'_>)> Behavior for Script<F> {
        fn tick(&mut self, ctx: &mut TickContext<'_>) {
            (self.0)(ctx);
        }
    }

    #[test]
    fn join_sends_settings_then_brand() {
        let mut e = engine(GameVersion::V1_8_9);
        e.handle(Inbound::JoinGame(JoinGame {
            entity_id: 1,
            game_mode: GameMode::Creative,
            dimension: "0".into(),
            hardcore: false,
            dimension_nbt: None,
            respawn_screen: true,
        }));
        assert_eq!(names(e.outbound()), ["settings", "brand"]);
    }

    #[test]
    fn nothing_before_the_first_teleport() {
        let mut e = engine(GameVersion::V1_8_9);
        let mut b = Idle;
        let t0 = Instant::now();
        load(&mut e, 0, 0);
        e.outbound().clear();
        for i in 0..5 {
            e.tick(&mut b, t0 + Duration::from_millis(50 * i));
        }
        assert!(e.outbound().is_empty());
    }

    #[test]
    fn teleport_reply_per_version() {
        let mut e = engine(GameVersion::V1_16_5);
        e.handle(Inbound::Teleport(Teleport {
            x: 1.0,
            y: 2.0,
            z: 3.0,
            yaw: 450.0,
            pitch: 100.0,
            flags: 0,
            teleport_id: Some(5),
        }));
        let out = std::mem::take(e.outbound());
        assert_eq!(out[0], Outbound::TeleportConfirm(5));
        match out[1] {
            Outbound::Move(MovePacket::PositionLook {
                yaw,
                pitch,
                on_ground,
                ..
            }) => {
                assert_eq!((yaw, pitch, on_ground), (90.0, 90.0, false));
            }
            ref o => panic!("unexpected {o:?}"),
        }
        let mut e = engine(GameVersion::V1_8_9);
        e.handle(Inbound::Teleport(Teleport {
            x: 1.0,
            y: 2.0,
            z: 3.0,
            yaw: 450.0,
            pitch: 100.0,
            flags: 0,
            teleport_id: None,
        }));
        let out = std::mem::take(e.outbound());
        assert_eq!(out.len(), 1);
        // 1.8 does not clamp the pitch.
        assert!(matches!(
            out[0],
            Outbound::Move(MovePacket::PositionLook {
                yaw: 90.0,
                pitch: 100.0,
                ..
            })
        ));
    }

    #[test]
    fn actions_precede_the_single_movement_packet() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            if ctx.tick() == 2 {
                ctx.select_slot(3);
                ctx.chat("hello");
                ctx.swing();
                ctx.set_controls(Controls {
                    sneak: true,
                    ..Controls::default()
                });
            }
        });
        e.tick(&mut b, t0);
        assert_eq!(names(e.outbound()), ["C06"]);
        e.tick(&mut b, t0);
        assert_eq!(
            names(e.outbound()),
            ["held_slot", "chat", "swing", "entity_action", "C03"]
        );
        for _ in 0..100 {
            e.tick(&mut b, t0);
            let n = names(e.outbound());
            assert_eq!(n.len(), 1, "one movement packet per tick: {n:?}");
        }
    }

    #[test]
    fn standing_bot_packets() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let mut b = Idle;
        let t0 = Instant::now();
        e.note_inbound(t0);
        // First tick: the position differs from the fresh player's lastReported (origin).
        e.tick(&mut b, t0);
        let out = std::mem::take(e.outbound());
        assert!(matches!(
            out[..],
            [Outbound::Move(MovePacket::PositionLook {
                on_ground: false,
                ..
            })]
        ));
        // Then flying (on ground) with a position refresh every 21st tick.
        let mut kinds = Vec::new();
        for _ in 0..42 {
            e.tick(&mut b, t0);
            kinds.extend(names(e.outbound()));
        }
        assert_eq!(kinds.iter().filter(|k| **k == "C04").count(), 2);
        assert_eq!(kinds.iter().filter(|k| **k == "C03").count(), 40);
        assert_eq!((kinds[20], kinds[41]), ("C04", "C04"));
        assert!(e.core.sent.on_ground);
    }

    #[test]
    fn silent_server_freezes_behaviour() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        let mut ticks = 0;
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            ticks += 1;
            ctx.set_controls(Controls {
                sneak: true,
                ..Controls::default()
            });
        });
        e.tick(&mut b, t0 + Duration::from_millis(100));
        e.tick(&mut b, t0 + Duration::from_millis(1600));
        // Frozen: keys released, so the sneak stops; still one movement packet.
        let n = names(e.outbound());
        assert_eq!(n, ["entity_action", "C06", "entity_action", "C03"]);
        e.note_inbound(t0 + Duration::from_millis(1700));
        e.tick(&mut b, t0 + Duration::from_millis(1700));
        let _ = b;
        assert_eq!(ticks, 2);
    }

    #[test]
    fn keepalive_answered_transactions_left_to_the_inventory() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        e.handle(Inbound::KeepAlive(42));
        // The engine never answers confirms: the inventory does (exactly once), see the
        // fake-server test.
        e.handle(Inbound::Transaction {
            window_id: 0,
            action: -5,
            accepted: false,
        });
        assert_eq!(std::mem::take(e.outbound()), [Outbound::KeepAlive(42)]);
    }

    #[test]
    fn panicking_behaviour_does_not_stop_the_tick() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            if ctx.tick() == 2 {
                panic!("boom");
            }
        });
        let hook = std::panic::take_hook();
        std::panic::set_hook(Box::new(|_| {}));
        for _ in 0..5 {
            e.tick(&mut b, t0);
        }
        std::panic::set_hook(hook);
        assert!(e.state.failed);
        assert_eq!(names(e.outbound()).len(), 5);
    }

    #[test]
    fn chat_events_reach_the_behaviour_once() {
        let mut e = engine(GameVersion::V1_16_5);
        join(&mut e);
        e.handle(Inbound::Chat(ChatMessage {
            json: r#"{"text":"hi"}"#.into(),
            position: ChatPosition::System,
            sender: None,
        }));
        let mut seen = Vec::new();
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            seen.extend(ctx.events().chat.iter().map(ChatMessage::plain));
        });
        let t0 = Instant::now();
        e.tick(&mut b, t0);
        e.tick(&mut b, t0);
        let _ = b;
        assert_eq!(seen, ["hi"]);
    }

    #[test]
    fn immediate_respawn_1_16() {
        let mut e = engine(GameVersion::V1_16_5);
        join(&mut e);
        e.handle(Inbound::GameState {
            reason: 11,
            value: 1.0,
        });
        e.handle(Inbound::Died { entity_id: 7 });
        assert_eq!(e.outbound().as_slice(), [Outbound::Respawn]);
    }

    /// Left button on the floor: START, swings, FINISH, all before the tick's movement
    /// packet; the broken block is predicted air and the player falls into the hole.
    #[test]
    fn dig_with_the_left_button() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            ctx.set_aim(90.0, 89.5);
            let down = ctx.target().is_some_and(|t| {
                t.pos == ghast_protocol::BlockPos::new(8, 63, 8) && t.face == crate::Face::Up
            });
            ctx.set_buttons(Buttons {
                left: down,
                right: false,
            });
        });
        let mut ticks = Vec::new();
        for _ in 0..200 {
            e.tick(&mut b, t0);
            let n = names(e.outbound());
            assert_eq!(n.iter().filter(|x| x.starts_with('C')).count(), 1, "{n:?}");
            assert!(n.last().unwrap().starts_with('C'), "movement last: {n:?}");
            ticks.push(n);
        }
        let flat: Vec<&str> = ticks.iter().flatten().copied().collect();
        assert_eq!(
            flat.iter().filter(|x| **x == "block_dig").count(),
            2,
            "start + finish"
        );
        assert!(e.core.hands.broken == 1);
        // Stone by hand: not harvestable, 1 / 1.5 / 100 per tick -> 151 damage ticks.
        let start = ticks.iter().position(|t| t.contains(&"block_dig")).unwrap();
        let finish = ticks
            .iter()
            .rposition(|t| t.contains(&"block_dig"))
            .unwrap();
        assert_eq!(finish - start, 150);
        assert!(e.sim.block_query(8, 63, 8).unwrap().air);
        assert!(
            e.core.pose.pos.y < 64.0,
            "falls into the hole: {:?}",
            e.core.pose.pos
        );
    }

    /// `place` is refused unless the sent look is on the face; then C08 + C0A before the
    /// movement packet.
    #[test]
    fn place_needs_the_sent_look() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        let floor = ghast_protocol::BlockPos::new(8, 63, 8);
        let mut results = Vec::new();
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            ctx.set_aim(90.0, 89.5);
            results.push(ctx.place(floor, crate::Face::Up, Vec3::new(8.5, 64.0, 8.5)));
        });
        // A stack of stone in the selected hotbar slot (window 0 slot 36).
        e.core
            .inv
            .on_set_slot(0, 36, Some(ghast_inventory::Stack::new(1, 64, 0)));
        let mut placed_tick = None;
        for i in 0..20 {
            e.tick(&mut b, t0);
            let n = names(e.outbound());
            if n.contains(&"block_place") {
                assert_eq!(n[..2], ["block_place", "swing"], "{n:?}");
                placed_tick.get_or_insert(i);
            }
        }
        let _ = b;
        let first = results.iter().position(|r| *r).unwrap();
        assert!(first > 3, "refused while still turning");
        assert_eq!(placed_tick, Some(first));
        assert_eq!(e.core.inv.held().map(|s| s.id), Some(1));
    }

    #[test]
    fn sneak_action_order() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            ctx.set_controls(Controls {
                sneak: true,
                ..Controls::default()
            });
        });
        e.tick(&mut b, Instant::now());
        let out = std::mem::take(e.outbound());
        assert_eq!(
            out[0],
            Outbound::EntityAction {
                entity_id: 7,
                action: EntityAction::StartSneaking
            }
        );
        assert!(matches!(out[1], Outbound::Move(_)));
    }

    /// Block changes reach the behaviour once, in arrival order, only when asked for.
    #[test]
    fn block_updates_per_version() {
        use ghast_protocol::BlockPos;
        for v in [GameVersion::V1_8_9, GameVersion::V1_16_5] {
            let mut e = engine(v);
            join(&mut e);
            e.sim.collect_block_updates(true);
            let t0 = Instant::now();
            e.note_inbound(t0);
            let raws = match v {
                GameVersion::V1_8_9 => {
                    use ghast_protocol::v1_8::play::clientbound as cb;
                    vec![
                        RawPacket::from_packet(&cb::BlockChange {
                            location: BlockPos::new(3, 63, 4),
                            r#type: 59 << 4 | 7,
                        })
                        .unwrap(),
                        RawPacket::from_packet(&cb::MultiBlockChange {
                            chunk_x: 0,
                            chunk_z: 0,
                            records: vec![cb::MultiBlockChangeRecords {
                                horizontal_pos: 5 << 4 | 6,
                                y: 62,
                                block_id: 60 << 4 | 7,
                            }],
                        })
                        .unwrap(),
                    ]
                }
                _ => {
                    use ghast_protocol::v1_16_5::play::clientbound as cb;
                    // Section y = -1 (20-bit two's complement), local x 5 z 6 y 14.
                    let local = (5 << 8 | 6 << 4 | 14) as i64;
                    vec![
                        RawPacket::from_packet(&cb::BlockChange {
                            location: BlockPos::new(3, 63, 4),
                            r#type: 1,
                        })
                        .unwrap(),
                        RawPacket::from_packet(&cb::MultiBlockChange {
                            chunk_coordinates: cb::MultiBlockChangeChunkCoordinates {
                                x: 0,
                                z: 0,
                                y: 0xF_FFFF,
                            },
                            not_trust_edges: false,
                            records: vec![9 << 12 | local],
                        })
                        .unwrap(),
                    ]
                }
            };
            for raw in &raws {
                e.sim.world_packet(raw).unwrap();
            }
            let mut seen = Vec::new();
            let mut b = Script(|ctx: &mut TickContext<'_>| {
                seen.push(ctx.events().block_updates.clone());
            });
            e.tick(&mut b, t0);
            e.tick(&mut b, t0);
            let _ = b;
            let want = match v {
                GameVersion::V1_8_9 => vec![
                    (BlockPos::new(3, 63, 4), 59 << 4 | 7),
                    (BlockPos::new(5, 62, 6), 60 << 4 | 7),
                ],
                _ => vec![(BlockPos::new(3, 63, 4), 1), (BlockPos::new(5, -2, 6), 9)],
            };
            assert_eq!(seen, [want, Vec::new()], "{v:?}");

            // Not collected when not asked for.
            e.sim.collect_block_updates(false);
            e.sim.world_packet(&raws[0]).unwrap();
            let mut n = usize::MAX;
            let mut b = Script(|ctx: &mut TickContext<'_>| n = ctx.events().block_updates.len());
            e.tick(&mut b, t0);
            let _ = b;
            assert_eq!(n, 0);
        }
    }

    /// `set_flying` sends the abilities packet before the move, only when allowed and changed.
    #[test]
    fn set_flying_needs_may_fly() {
        for v in [GameVersion::V1_8_9, GameVersion::V1_16_5] {
            let mut e = engine(v);
            join(&mut e);
            let t0 = Instant::now();
            e.note_inbound(t0);
            // In the air (flight ends on landing, like vanilla).
            e.handle(Inbound::Teleport(Teleport {
                x: 8.5,
                y: 70.0,
                z: 8.5,
                yaw: 90.0,
                pitch: 0.0,
                flags: 0,
                teleport_id: Some(2),
            }));
            e.outbound().clear();
            let mut b = Script(|ctx: &mut TickContext<'_>| ctx.set_flying(true));
            e.tick(&mut b, t0);
            assert!(!names(e.outbound()).contains(&"abilities"), "{v:?}");
            assert!(!e.core.pose.flying);
            e.handle(Inbound::Abilities(Abilities {
                flags: 4,
                ..Abilities::default()
            }));
            e.tick(&mut b, t0);
            let out = std::mem::take(e.outbound());
            assert_eq!(
                out[0],
                Outbound::Abilities {
                    flags: 6,
                    flying_speed: 0.05,
                    walking_speed: 0.1
                },
                "{v:?}"
            );
            assert!(matches!(out[1], Outbound::Move(_)), "{out:?}");
            assert!(e.core.pose.flying && e.core.sent.flying);
            // Already flying: nothing more.
            e.tick(&mut b, t0);
            assert!(!names(e.outbound()).contains(&"abilities"));
            let _ = b;
        }
    }

    /// A behaviour that reports `running() == false` is not ticked again.
    #[test]
    fn finished_behaviour_is_not_ticked() {
        struct Three(std::rc::Rc<std::cell::Cell<u32>>);
        impl Behavior for Three {
            fn tick(&mut self, _ctx: &mut TickContext<'_>) {
                self.0.set(self.0.get() + 1);
            }
            fn running(&self) -> bool {
                self.0.get() < 3
            }
        }
        let ticks = std::rc::Rc::new(std::cell::Cell::new(0));
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        let mut b: Box<dyn Behavior> = Box::new(Three(ticks.clone()));
        for _ in 0..10 {
            e.tick(&mut b, t0);
            // The player keeps sending its movement packet.
            let out = std::mem::take(e.outbound());
            assert!(
                out.iter().any(|o| matches!(o, Outbound::Move(_))),
                "{out:?}"
            );
        }
        assert_eq!(
            ticks.get(),
            3,
            "not ticked after running() == false (through the Box)"
        );
    }

    /// A turn that would take the crosshair off a block placed this tick waits one tick.
    #[test]
    fn turn_held_on_a_place_tick() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        e.core
            .inv
            .on_set_slot(0, 36, Some(ghast_inventory::Stack::new(1, 64, 0)));
        let floor = ghast_protocol::BlockPos::new(8, 63, 8);
        let mut tick = 0;
        let mut placed_at = None;
        let mut pitches = Vec::new();
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            tick += 1;
            if placed_at.is_none() {
                ctx.set_aim(90.0, 89.5);
                if ctx.place(floor, crate::Face::Up, Vec3::new(8.5, 64.0, 8.5)) {
                    placed_at = Some(tick);
                    // Look up on the same tick: held back.
                    ctx.set_aim(90.0, -60.0);
                }
            } else {
                ctx.set_aim(90.0, -60.0);
            }
            pitches.push(ctx.pose().pitch);
        });
        for _ in 0..30 {
            e.tick(&mut b, t0);
            e.outbound().clear();
        }
        let _ = b;
        let p = placed_at.expect("placed");
        assert_eq!(e.core.held_turns, 1);
        // Pose at the start of the tick after the place: still the place look.
        assert_eq!(pitches[p], pitches[p - 1], "{pitches:?}");
        assert!(pitches[p + 1] < pitches[p], "{pitches:?}");
    }

    /// `click` returns the ticket its confirm carries.
    #[test]
    fn click_ticket_matches_the_confirm() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        e.core
            .inv
            .on_set_slot(0, 36, Some(ghast_inventory::Stack::new(1, 64, 0)));
        let mut ticket = None;
        let mut n = 0;
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            n += 1;
            if n == 1 {
                ctx.open_inventory().unwrap();
            } else if ticket.is_none() {
                ticket = ctx
                    .click(ghast_inventory::Click::Pickup {
                        slot: 36,
                        right: false,
                    })
                    .ok();
            }
        });
        for _ in 0..3 {
            e.tick(&mut b, t0);
        }
        let _ = b;
        let t = ticket.expect("click sent");
        assert_eq!(t.window_id, 0);
        assert!(t.matches(&ghast_inventory::ClickResult {
            window_id: 0,
            action: t.action,
            outcome: ghast_inventory::ClickOutcome::Accepted,
        }));
    }

    /// 1.8 planting uses the seed at once (vanilla `SeedItem.use` on the client); a
    /// stone block place waits for the server.
    #[test]
    fn planting_uses_the_seed_at_once() {
        use ghast_protocol::v1_8::play::clientbound as cb;
        for (soil, item, used) in [
            (60 << 4 | 7, 295, true),
            (1 << 4, 295, false),
            (60 << 4, 1, false),
        ] {
            let mut e = engine(GameVersion::V1_8_9);
            join(&mut e);
            let t0 = Instant::now();
            e.note_inbound(t0);
            let floor = ghast_protocol::BlockPos::new(8, 63, 8);
            let raw = RawPacket::from_packet(&cb::BlockChange {
                location: floor,
                r#type: soil,
            })
            .unwrap();
            e.sim.world_packet(&raw).unwrap();
            e.core
                .inv
                .on_set_slot(0, 36, Some(ghast_inventory::Stack::new(item, 64, 0)));
            let mut done = false;
            let mut b = Script(|ctx: &mut TickContext<'_>| {
                ctx.set_aim(90.0, 89.5);
                if !done {
                    done = ctx.place(floor, crate::Face::Up, Vec3::new(8.5, 64.0, 8.5));
                }
            });
            let mut sent = None;
            for _ in 0..20 {
                e.tick(&mut b, t0);
                for o in e.outbound().drain(..) {
                    if let Outbound::Place { held, .. } = o {
                        sent = held.map(|h| h.count);
                    }
                }
            }
            let _ = b;
            assert_eq!(sent, Some(64), "the packet carries the stack before use");
            let left = e.core.inv.held().map(|s| s.count);
            assert_eq!(
                left,
                Some(if used { 63 } else { 64 }),
                "soil {soil} item {item}"
            );
        }
    }

    /// `planned_look` is the look the movement step then uses: a plain turn, a turn held on a
    /// place tick, no aim; and it leaves the rotation's budget alone.
    #[test]
    fn planned_look_is_the_next_look() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        e.core
            .inv
            .on_set_slot(0, 36, Some(ghast_inventory::Stack::new(1, 64, 0)));
        let floor = ghast_protocol::BlockPos::new(8, 63, 8);
        let mut n = 0;
        let mut placed = false;
        let mut plans: Vec<(f32, f32)> = Vec::new();
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            n += 1;
            if n % 5 == 0 {
                // No aim this tick: the current look.
            } else if !placed {
                ctx.set_aim(90.0, 89.5);
                if ctx.place(floor, crate::Face::Up, Vec3::new(8.5, 64.0, 8.5)) {
                    placed = true;
                    ctx.set_aim(90.0, -60.0);
                }
            } else {
                ctx.set_aim(150.0, -60.0);
            }
            // Asking twice changes nothing.
            let p = ctx.planned_look();
            assert_eq!(p, ctx.planned_look());
            plans.push(p);
        });
        let mut looks = Vec::new();
        for _ in 0..40 {
            e.tick(&mut b, t0);
            e.outbound().clear();
            looks.push((e.core.pose.yaw, e.core.pose.pitch));
        }
        let _ = b;
        assert!(placed);
        assert_eq!(e.core.test_held_turns(), 1);
        assert_eq!(plans, looks);
    }

    /// A teleport event keeps the sent pose from before the overwrite, the raw values and the
    /// resolved pose.
    #[test]
    fn teleport_event_has_the_sent_pose_before() {
        for v in [GameVersion::V1_8_9, GameVersion::V1_16_5] {
            let mut e = engine(v);
            join(&mut e);
            let t0 = Instant::now();
            e.note_inbound(t0);
            let mut seen = Vec::new();
            let mut b = Script(|ctx: &mut TickContext<'_>| {
                seen.extend(ctx.events().teleports.iter().copied());
            });
            e.tick(&mut b, t0);
            let sent = e.core.sent;
            // Relative yaw +30, absolute position.
            let tp = Teleport {
                x: 10.5,
                y: 64.0,
                z: 8.5,
                yaw: 30.0,
                pitch: 0.0,
                flags: crate::common::teleport_flags::YAW,
                teleport_id: Some(9),
            };
            e.handle(Inbound::Teleport(tp));
            e.tick(&mut b, t0);
            e.tick(&mut b, t0);
            let _ = b;
            // The spawn position first (join), then ours, each once.
            assert_eq!(seen.len(), 2, "{v:?}");
            let ev = seen[1];
            assert_eq!(ev.teleport, tp);
            assert_eq!(ev.sent_before, sent);
            assert_eq!(ev.after.pos, Vec3::new(10.5, 64.0, 8.5));
            assert_eq!(ev.after.yaw, sent.yaw + 30.0, "{v:?}");
        }
    }

    /// A container the server opened and the behaviour closed (`close_window`) no longer
    /// holds the mouse: the left button digs again (both versions). While it is open the
    /// buttons are up.
    #[test]
    fn closed_window_frees_the_buttons() {
        for v in [GameVersion::V1_8_9, GameVersion::V1_16_5] {
            let mut e = engine(v);
            join(&mut e);
            let t0 = Instant::now();
            e.note_inbound(t0);
            if v == GameVersion::V1_16_5 {
                // The 1.16 test chunk is all air: put a stone floor under the player.
                use ghast_protocol::v1_16_5::play::clientbound as cb;
                let raw = RawPacket::from_packet(&cb::BlockChange {
                    location: ghast_protocol::BlockPos::new(8, 63, 8),
                    r#type: 1,
                })
                .unwrap();
                e.sim.world_packet(&raw).unwrap();
            }
            e.core.inv.on_open_window(
                5,
                Some(ghast_inventory::WindowKind::Chest { rows: 3 }),
                "Vault".into(),
            );
            e.handle(Inbound::OpenWindow(5));
            // Something on the cursor: vanilla's client-side close drops it (the server
            // throws it out), so the client forgets it too.
            e.core
                .inv
                .on_set_slot(-1, -1, Some(ghast_inventory::Stack::new(1, 3, 0)));
            let mut n = 0;
            let mut closed_at = None;
            let mut b = Script(|ctx: &mut TickContext<'_>| {
                n += 1;
                ctx.set_aim(90.0, 89.5);
                ctx.set_buttons(Buttons {
                    left: true,
                    right: false,
                });
                if n == 15 {
                    assert!(ctx.inventory().cursor().is_some());
                    ctx.close_window().unwrap();
                    assert_eq!(ctx.open_window(), None);
                    let inv = ctx.inventory();
                    assert!(!inv.gui_open() && inv.window().is_none() && inv.cursor().is_none());
                    closed_at = Some(n);
                }
            });
            let mut first_dig = None;
            for i in 1..=30 {
                e.tick(&mut b, t0);
                let out = std::mem::take(e.outbound());
                if first_dig.is_none()
                    && out.iter().any(|o| {
                        matches!(
                            o,
                            Outbound::Dig {
                                status: crate::common::DigStatus::Start,
                                ..
                            }
                        )
                    })
                {
                    first_dig = Some(i);
                }
            }
            let _ = b;
            assert_eq!(closed_at, Some(15));
            let d = first_dig.unwrap_or_else(|| panic!("{v:?}: no dig after the close"));
            // The hands run after the behaviour: the closing tick already digs.
            assert!(d >= 15, "{v:?}: dug at tick {d} with the window open");
        }
    }

    /// `swings` counts every arm swing: the behaviour's, an action's and the held button's.
    #[test]
    fn swings_count_every_arm_swing() {
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        let mut n = 0;
        let mut seen = Vec::new();
        let mut b = Script(|ctx: &mut TickContext<'_>| {
            n += 1;
            ctx.set_aim(90.0, 89.5);
            if n == 1 {
                ctx.swing();
                ctx.swing();
            }
            if n == 20 {
                // Hold the left button on the stone floor: a swing every tick while digging.
                ctx.set_buttons(Buttons {
                    left: true,
                    right: false,
                });
            }
            seen.push(ctx.swings());
        });
        let mut sent = 0u64;
        for _ in 0..30 {
            e.tick(&mut b, t0);
            sent += count_swings(&std::mem::take(e.outbound()));
        }
        let _ = b;
        assert_eq!(seen[0], 2, "this tick's own swings count at once");
        assert!(sent > 10, "the held button swings: {sent}");
        // The last reading misses only the hands' swing of the last tick.
        assert_eq!(*seen.last().unwrap() + 1, sent);
        assert_eq!(e.core.swings, sent);
    }

    /// `WorldView::column` gives the stored column; light and biomes only when configured.
    #[test]
    fn column_view_and_world_config() {
        for config in [
            None,
            Some(
                ghast_world::WorldConfig::default()
                    .with_biomes(true)
                    .with_light(true),
            ),
        ] {
            let sim = crate::sim::Sim18::with(&crate::sim::SimOptions {
                world: config,
                ..crate::sim::SimOptions::default()
            });
            let mut e = Engine::new(opts(GameVersion::V1_8_9), Box::new(sim) as Box<dyn Sim>);
            join(&mut e);
            let w: &dyn WorldView = &e.sim;
            let col = w.column(0, 0).expect("loaded");
            assert_eq!(u32::from(col.raw(8, 63, 8)), w.raw_block(8, 63, 8).unwrap());
            assert_eq!(col.raw(8, 63, 8), 1 << 4);
            assert!(w.column(5, 5).is_none());
            let light = col.sky_light(8, 63, 8);
            assert_eq!(light.is_some(), config.is_some(), "{config:?}");
            let biomes = !matches!(col.biomes(), ghast_world::Biomes::None);
            assert_eq!(biomes, config.is_some(), "{config:?}");
        }
    }

    /// `after_tick` runs after the movement step with the sent pose, also once the behaviour
    /// finished; a panic there stops the behaviour only.
    #[test]
    fn after_tick_sees_the_sent_pose() {
        struct Obs {
            n: u32,
            views: Vec<(u64, Pose, u64)>,
        }
        impl Behavior for Obs {
            fn tick(&mut self, ctx: &mut TickContext<'_>) {
                self.n += 1;
                ctx.set_aim(150.0, 10.0);
                ctx.set_controls(Controls {
                    forward: true,
                    ..Controls::default()
                });
            }
            fn running(&self) -> bool {
                self.n < 5
            }
            fn after_tick(&mut self, view: &TickView<'_>) {
                self.views.push((view.tick(), *view.sent(), view.swings()));
                assert!(view.world().is_column_loaded(0, 0));
                assert_eq!(view.version(), GameVersion::V1_8_9);
            }
        }
        let mut e = engine(GameVersion::V1_8_9);
        join(&mut e);
        let t0 = Instant::now();
        e.note_inbound(t0);
        let mut b = Obs {
            n: 0,
            views: Vec::new(),
        };
        let mut sent = Vec::new();
        for _ in 0..10 {
            e.tick(&mut b, t0);
            e.outbound().clear();
            sent.push(e.core.sent);
        }
        assert_eq!(b.n, 5);
        assert_eq!(b.views.len(), 10, "also after the behaviour finished");
        for (i, (_, p, _)) in b.views.iter().enumerate() {
            assert_eq!(*p, sent[i]);
        }
        assert_ne!(
            b.views[0].1.yaw, b.views[4].1.yaw,
            "the view follows the turn"
        );
    }

    /// With block entities on, a 1.8 sign update reaches the column (and the packet passes the
    /// filter); off by default.
    #[test]
    fn block_entities_follow_the_world_config() {
        use ghast_protocol::v1_8::play::clientbound as cb;
        for on in [false, true] {
            let sim = crate::sim::Sim18::with(&crate::sim::SimOptions {
                world: on.then(|| ghast_world::WorldConfig::default().with_block_entities(true)),
                ..crate::sim::SimOptions::default()
            });
            assert_eq!(sim.world_packet_ids().contains(&cb::id::UPDATE_SIGN), on);
            let mut e = Engine::new(opts(GameVersion::V1_8_9), Box::new(sim) as Box<dyn Sim>);
            join(&mut e);
            let raw = RawPacket::from_packet(&cb::UpdateSign {
                location: ghast_protocol::BlockPos::new(3, 64, 4),
                text1: "\"Hello\"".into(),
                text2: "\"\"".into(),
                text3: "\"\"".into(),
                text4: "\"\"".into(),
            })
            .unwrap();
            e.sim.world_packet(&raw).unwrap();
            let w: &dyn WorldView = &e.sim;
            let be = w.column(0, 0).unwrap().block_entity(3, 64, 4);
            assert_eq!(be.is_some(), on, "{be:?}");
        }
    }
}