tish_gba_game_engine 0.3.1

RPG-Maker-class game engine for tish-on-GBA: SoA entity store + fixed per-frame pipeline (imported via cargo:tish_gba_game_engine)
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
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
//! `tish_gba_game_engine` — the RPG-Maker-class game engine for tish-on-GBA.
//!
//! Shape (Unity analogy): this Rust crate is the *engine* (written once, hot path,
//! agb-coupled); a tish game is the *components + game logic* (what users write and
//! modify). It's imported like any binding — `import { … } from 'cargo:tish_gba_game_engine'`.
//!
//! This first cut is the foundation everything else hangs off: a **SoA entity
//! store** (component columns + a presence bitmask + generational ids) and a **fixed
//! per-frame pipeline** (`world_step` = movement → render → commit). Positions are
//! `fixed` (agb `Num<i32,8>`), so integration is native integer math. Sprites are
//! tish-agb handles: the engine *drives* tish-agb (it doesn't own rendering), so the
//! low level stays reachable and handles are shared.
#![no_std]
// Engine natives take their full argument lists deliberately — the tish ABI passes scalars, not
// config structs, so arity mirrors the script-side signature.
#![allow(clippy::too_many_arguments)]

extern crate alloc;

use alloc::string::String;
use alloc::vec::Vec;
use core::cell::RefCell;

// Q24.8 fixed-point (`Fixed` = agb `Num<i32,8>`) — the engine's position/velocity type,
// matching tish `fixed`. Sourced from the runtime facade (the canonical `Fixed` definition,
// CONTRACT §5) rather than depending on `agb` directly: the engine drives tish-agb + the facade
// and needs no direct agb dependency of its own.
use tishlang_runtime_gba::{get_prop, set_prop, value_call, Fixed, SingleCore, Value};

fn to_fixed(n: f64) -> Fixed {
    Fixed::from_raw((n * 256.0) as i32)
}
fn from_fixed(f: Fixed) -> f64 {
    f.to_raw() as f64 / 256.0
}

// ── Components (fixnum POD, one dense column each) ───────────────────────────
// The presence mask is a u16, so up to 16 component types.
const C_TRANSFORM: u32 = 1 << 0;
const C_BODY: u32 = 1 << 1;
const C_SPRITE: u32 = 1 << 2;
const C_COLLIDER: u32 = 1 << 3;
const C_GRIDPOS: u32 = 1 << 4;
const C_ANIM: u32 = 1 << 5;
const C_WALK: u32 = 1 << 6;
const C_PLATFORMER: u32 = 1 << 7;
const C_HEALTH: u32 = 1 << 8;
const C_PATROL: u32 = 1 << 9;
/// Auto-despawn: a time-to-live and/or "gone when it leaves the screen" flag. The lifeblood of a
/// shoot-'em-up (bullets, spent shots, explosions) and of any spawn-and-forget FX — without it a
/// static-screen game (no camera to cull against) would leak a sprite per shot until OAM overflows.
const C_LIFE: u32 = 1 << 10;
/// Hurt box: this entity deals damage on contact to entities carrying a target tag (a bullet to
/// enemies, an enemy body to the player). The `combat_system` resolves it natively — no per-bullet
/// tish callback and no `makeEntity` wrapper — so a screen full of shots stays cheap.
const C_HURT: u32 = 1 << 11;
/// Native movement pattern (straight / sideways-weave). The `mover_system` drives the entity's
/// `Body` velocity in pure Rust every frame, so a screen full of weaving enemies costs NO per-frame
/// tish `tick` — the shmup analogue of `patrol_system`. Firing/AI decisions stay in tish; only the
/// (cheap, formulaic) MOVEMENT is native.
const C_MOVER: u32 = 1 << 12;
/// Free 8-directional top-down movement with solid-tile collision (the top-down action-RPG genre).
/// Unlike `GridPos` (tile-locked, grid-RPG-style) the entity moves by pixels in any of eight
/// directions and its `Collider` box is resolved axis-by-axis against the solid grid — the
/// platformer's collision, minus gravity. `topdown_system` owns integration; drive it with
/// `topdown_move` each frame. Both the player and chase-AI enemies use it, so they slide along
/// walls instead of snapping to tiles.
const C_TOPDOWN: u32 = 1 << 13;
/// Native top-down "seek the player" AI (component `C_CHASE`). `chase_system` steers a `C_TOPDOWN`
/// entity toward the camera target each frame and picks its walk frame — ALL in Rust, so a room of
/// enemies costs ZERO per-frame tish `tick`s (the shmup lesson: keep movement/animation native, only
/// decisions in tish). This is the top-down counterpart to `patrol_system`/`mover_system`.
const C_CHASE: u32 = 1 << 14;
/// Solid entity blocker: its `Collider` stops top-down movers the way a solid tile does (NPCs,
/// closed gates, pushable crates). The box moves with the entity — no spawn-tile `setSolid` hack —
/// so a walked-away NPC leaves no invisible wall behind and blocks wherever it stands now.
const C_BLOCKER: u32 = 1 << 15;
const C_HOPPER: u32 = 1 << 16;
const C_JUMPER: u32 = 1 << 17;
/// A rank-and-file enemy that SHOOTS (`shooter_system`). The two most common enemies in a top-down
/// action game of this kind — projectile-spitting walkers and spear-throwers — are ranged, and modelling them as plain
/// hoppers makes them melee, which changes how most of the map plays. Native, so a screen full of
/// them still costs no per-frame tish call: the bullet style in force when `set_shooter` is called
/// is snapshotted into the component and restored around each shot.
const C_SHOOTER: u32 = 1 << 18;
/// An enemy that bolts along an axis once it lines up with the player (`charger_system`) — the
/// classic striking snake. It rides on top of whatever normally moves the entity, taking over only while lined up.
const C_CHARGER: u32 = 1 << 19;
/// The entity blocks damage arriving from the direction it FACES. In the genre this is one rule wearing
/// two hats: the player's shield stops arrows and fireballs they are walking into, and an armoured knight
/// stops the sword unless you get behind it. Bit 1 guards melee/contact, bit 2 guards projectiles,
/// so a shield and a suit of armour are the same component with a different mask.
const C_GUARD: u32 = 1 << 20;

/// Directional walk animation on a SHARED sprite strip (`diranim_system`).
///
/// The older directional path is `set_chase`'s, and it assumes the entity owns its sheet: it plays
/// `facing * stride` with no per-actor origin, so on a strip that holds thirty-three actors every
/// chasing enemy animated whatever sits at frame 0. It also assumes five columns per row (idle at
/// the base, four walk frames after it).
///
/// This is the layout an NES-era baked actor strip actually has: `base + facing * stride` for
/// `frames` frames, no idle column, with `base` naming where THIS actor starts.
const C_DIRANIM: u32 = 1 << 21;

/// The entity's `Collider` is a DISC of diameter `Collider.w`, centred in the (square) box.
///
/// Deliberately a TAG on the existing box rather than a new column: every AABB path in the engine —
/// `slots_overlap`, `combat_system`, `collect_collisions`, off-screen culling, `box_hits_solid` —
/// keeps working unchanged and treats the disc as its bounding box, which is correct as a
/// broadphase and perfectly adequate for a goal trigger or a pickup. Only `dynamic_system` reads
/// this bit and does the exact circle test. Radius is `w >> 1`, a shift.
const C_CIRCLE: u32 = 1 << 22;
/// A rigid body that integrates, bounces off tiles, resolves against other discs and comes to REST
/// (`dynamic_system`). Golf's ball, soccer's ball and players, a pinball, a pool table.
const C_DYNAMIC: u32 = 1 << 23;
/// On overlap with a tagged target, briefly stun it (`grabber_system`) — the classic shield-eater / ceiling-hand grab, lite.
const C_GRABBER: u32 = 1 << 24;
/// Inert until the player shares its row/col, then dash (`trap_system`) — Blade Trap-lite.
const C_TRAP: u32 = 1 << 25;
/// Keep this entity glued to a parent transform (`follow_system`): part / train segment / orbiter.
const C_FOLLOW: u32 = 1 << 26;
/// Projectile that reverses toward its owner after N frames (`boomerang_system`).
const C_BOOMERANG: u32 = 1 << 27;
/// Walk down a shared flow field toward its goal (`seek_system`) — the RTS "move here" order.
const C_SEEK: u32 = 1 << 28;
/// Attack-move: seek while nothing is in range, break to engage, resume (`soldier_system`).
const C_SOLDIER: u32 = 1 << 29;
/// Reveals fog of war around itself each frame (`fog_system`).
const C_VISION: u32 = 1 << 30;
/// DORMANT. A pooled slot that has been created but is not in play: the per-frame systems skip it
/// entirely, so a pool costs ROM and VRAM but not frame time.
///
/// This is the fix for "a pooled entity is not free" — 26 parked entities measured 2,631 ticks of a
/// 4,389-tick frame doing nothing at all, which forces a game to choose between a big enough unit
/// pool and a playable frame rate. It is NOT off-screen culling: an off-screen unit must keep
/// walking, a sleeping one has nothing to do by definition.
const C_SLEEP: u32 = 1 << 31;

/// `guard` mask bits.
pub const GUARD_MELEE: i32 = 1;
pub const GUARD_SHOT: i32 = 2;

// Weapon kinds, mirroring the NES original's per-weapon damage-type enum.
// A hurt box carries ONE of these; a victim carries a MASK of the ones that bounce off it, which is
// how the genre says "this armoured boss ignores the sword" and "only an arrow opens that boss's eye"
// without a per-monster special case in the collision code.
pub const DMG_SWORD: i32 = 1;
pub const DMG_BOOMERANG: i32 = 2;
pub const DMG_ARROW: i32 = 4;
pub const DMG_BOMB: i32 = 8;
pub const DMG_MAGIC: i32 = 0x10;
pub const DMG_FIRE: i32 = 0x20;

/// Tile size in pixels for the grid/RPG genre (grid-RPG overworld).
const TILE: i32 = 16;
/// Off-screen culling margin (px). An entity whose box is farther than this outside the camera
/// view is "inactive": its behaviour/physics/animation are skipped until it scrolls back near.
const CULL_MARGIN: i32 = 32;
/// Grid walk speed in pixels/frame (`TILE`/`GRID_SPEED` frames per tile step).
const GRID_SPEED: i32 = 2;

// Side-scrolling platformer tuning, in fixed-point raw units (pixels/frame · 256).
const P_WALK: i32 = 320; // 1.25 px/frame walk speed
const P_RUN: i32 = 576; // 2.25 px/frame run speed (hold the run button)
const P_GRAVITY: i32 = 77; // ~0.3 px/frame² downward acceleration
const P_JUMP: i32 = 1280; // 5 px/frame initial jump velocity (≈2.5-tile apex)
const P_TERMINAL: i32 = 1536; // 6 px/frame terminal fall speed
                              // "Game feel" windows, in frames.
const P_COYOTE: i32 = 6; // can still jump this many frames after walking off a ledge
const P_JUMP_BUFFER: i32 = 6; // a jump press within this window before landing still fires
const P_DROP: i32 = 8; // frames a down-drop lets the box fall through one-way platforms
                       // Vertical slack on `platformer_interact`'s probe, in px. Side-scroller NPCs stand on steps,
                       // doorsills and awnings, and their art rarely shares the player's hitbox height — without slack the
                       // talk only lands when the two boxes happen to line up, which reads as the button not working.
const PF_INTERACT_PAD: i32 = 8;
const INVULN_FRAMES: i32 = 40; // post-hit invincibility (~0.67s); sprite flickers during it

// Top-down (action-RPG) tuning, in fixed-point raw units (px/frame · 256).
const TD_WALK: i32 = 320; // 1.25 px/frame default top-down move speed
const TD_DIAG: i32 = 181; // Q8 scale (~0.707) applied per axis on a diagonal so it isn't faster
                          // The hit reaction, matching the NES original's behaviour: the shove steps 1px FOUR times per
                          // frame with a collision test on each step, so a shove is 4 px/frame; the distance decides how
                          // long it lasts. The player is shoved 0x20 = 32 px and a monster 0x40 = 64 px — so 8 frames and
                          // 16 frames respectively.
                          //
                          // This was 2.5 px/frame for 8 frames = 20 px, symmetric for both. Wrong in three ways at once: too
                          // slow, too short, and identical for the player and the thing that hit him.
const TD_KNOCK: i32 = 1024; // 4 px/frame — the original moves 1px x4 per frame
const TD_KNOCK_FRAMES: i32 = 8; // 8 frames x 4 px = the player's 0x20 shove

// Top-down snap profiles (`topdown_snap`). These are whole character-controller personalities, not
// flags that combine: each one owns how intent becomes motion, so a game picks exactly one per
// entity. 0 (the default) is free 8-direction movement — no snapping at all.
/// Tile stepping: a direction commits the entity to one full 16px cell, and it rests centred on the
/// cell it lands on. Board-game / grid-RPG feel. Input during the step is ignored until it finishes.
const TD_SNAP_TILE: u8 = 2;
#[derive(Clone, Copy, Default)]
struct Transform {
    x: Fixed,
    y: Fixed,
}
#[derive(Clone, Copy, Default)]
struct Body {
    vx: Fixed,
    vy: Fixed,
}
#[derive(Clone, Copy)]
struct SpriteRef {
    handle: i32,
    /// Draw offset (px) of the sprite's top-left from the entity's transform. Lets a sprite be
    /// larger than its collider — e.g. a 32×32 character on a 16×16 hitbox draws at (-8, -16) so it
    /// centres horizontally on the box with its feet on the box's bottom edge. Default (0, 0).
    pub ox: i32,
    pub oy: i32,
}
/// An axis-aligned box `w`×`h` with its top-left at the entity's transform (matching
/// agb's top-left sprite positioning).
#[derive(Clone, Copy, Default)]
struct Collider {
    w: Fixed,
    h: Fixed,
}
/// The shared configuration a burst of bullets is spawned with. Set once (`set_bullet_style`) before a
/// pattern call, so `fire_ring`/`fire_spread`/etc. can spawn every bullet natively without the game
/// re-resolving an options object per bullet. `size` is the square hitbox in px; `target` is the tag
/// the bullet damages; `tag` is the bullet's own kind tag; `ttl` is its lifetime in frames.
#[derive(Clone, Copy, Default)]
struct BulletStyle {
    sheet: i32,
    frame: i32,
    size: i32,
    damage: i32,
    target: i32,
    tag: i32,
    ttl: i32,
    /// Which weapon the bullets of this burst are, as a `DMG_*` bit. Sticky like every other field
    /// here: set immediately before firing, never once at boot. 0 = untyped, which is what every
    /// existing caller gets, so `set_bullet_style`'s seven arguments stay exactly as they were.
    damage_type: i32,
}
/// Grid/RPG position: which tile the entity occupies, its facing, and (while stepping)
/// the target pixel position it's sliding toward. The grid system drives `Transform`
/// from this; entities move a whole tile at a time (tile-locked), blocked by solids.
#[derive(Clone, Copy, Default)]
struct GridPos {
    col: i32,
    row: i32,
    moving: bool,
    tx: Fixed,
    ty: Fixed,
    /// Facing direction (last attempted step), for the interact probe.
    fx: i32,
    fy: i32,
}
/// Sprite-sheet animation: cycle `frames` frames, advancing every `speed` game frames.
/// A playing animation **clip**: the frame range `[from, from+len)` advanced one step
/// every `speed` frames, looping (or stopping on the last frame when `looping` is false).
/// `cur` is the offset within the range. This is the low-level primitive a tish-side
/// animation controller drives via `anim_play` (bind a state → a clip).
#[derive(Clone, Copy, Default)]
struct Anim {
    from: i32,
    len: i32,
    speed: i32,
    timer: i32,
    cur: i32,
    looping: bool,
    playing: bool,
}

/// Directional walk animation driven by `GridPos` facing + movement. Expects a
/// character sheet laid out as ROWS of `cols` frames: row 0 = facing down, row 1 = up,
/// row 2 = side (used for left as-is, and for right horizontally flipped). Within a row,
/// column 1 is the standing frame and columns 0 / 2 are the two walking steps. `speed`
/// is frames-per-step-toggle; `phase` flips between the two step columns while moving.
#[derive(Clone, Copy, Default)]
struct Walk {
    cols: i32,
    speed: i32,
    timer: i32,
    phase: bool,
}

/// The tile column/row containing a box's FAR EDGE, given that edge's coordinate.
///
/// A box spanning `[a, a+w)` ends at the pixel just before `a+w`, so the cell it reaches is the one
/// holding `a+w - 1/256`, not the one holding `a+w`. This must agree EXACTLY with how
/// `box_hits_solid` picks its last cell (`((v).to_raw() - 1) >> 8`), because one decides that a move
/// is blocked and the other decides where to put the box back.
///
/// Getting it wrong is not a rounding error, it is a teleport. The old form —
/// `(v.floor() - 1) / TILE` — differs from this only when the far edge lands in the first PIXEL of a
/// cell (say a box bottom at 320.3 against a floor at 320): `box_hits_solid` sees the box in row 20
/// and blocks it, while `floor(320.3) - 1 = 319` names row 19, and the box is clamped a whole tile
/// back the way it came. A character standing still on a floor penetrates by exactly that much on
/// its first gravity step, so it was flung a tile upward, fell, was flung again — grounded flickering
/// on and off forever, an animation that never settled, and a hero hovering a tile above the street.
fn last_cell(edge: Fixed) -> i32 {
    ((edge.to_raw() - 1) >> 8).div_euclid(TILE)
}

/// Side-scrolling platformer body. Unlike `Body`, the plain movement system does NOT touch
/// it — `platformer_system` owns integration so it can apply gravity and resolve the entity's
/// `Collider` box against the solid tile grid axis-by-axis. `grounded` is true the frame the
/// box is resting on a solid (gates jumping and drives animation). Carries the "game feel"
/// state (coyote time, jump buffer, variable-height jump, one-way drop-through).
#[derive(Clone, Copy, Default)]
struct Platformer {
    vx: Fixed,
    vy: Fixed,
    grounded: bool,
    dir: i32, // horizontal move intent this frame (-1/0/1), set by `platformer_walk`
    // Which way the entity is FACING (-1 left, +1 right). `dir` is the intent this frame and goes
    // to 0 the moment input stops, so it cannot answer "which way am I looking?" — every platformer
    // example ended up shadowing it with its own `data.face`. This keeps the last non-zero `dir`,
    // which is what animation (sprite flip) and `platformer_interact` both need. Starts facing
    // right so a freshly spawned, never-moved entity still has a defined interact direction.
    face: i32,
    run: bool,        // run speed instead of walk (set by `platformer_run`)
    coyote: i32,      // frames of coyote time left (jump shortly after leaving ground)
    jump_buffer: i32, // frames a buffered jump press stays live
    jumping: bool,    // rising from a jump — lets `platformer_jump_release` cut the height
    drop: i32,        // frames left to fall through one-way platforms (down-drop)
    blocked: bool, // pushed into a wall this frame (a wall stopped the intended move) — patrol AI
    held: bool,    // frozen in place (no gravity/movement) — e.g. hanging on a ledge grab
    // Per-entity ground speeds in Fixed raw (1/256 px/frame), or 0 to take the engine's P_WALK /
    // P_RUN. Zero-means-default rather than seeding every body with the constants, so a `Platformer`
    // still has a meaningful `Default` and the six games that never ask are bit-for-bit unaffected.
    // A hero should be able to be quicker than the shared default without retuning every other game.
    walk_raw: i32,
    run_raw: i32,
    // Per-entity jump impulse / gravity in Fixed raw, 0 = the engine's P_JUMP / P_GRAVITY — the
    // same zero-means-default contract as the speeds above. Set by `platformer_set_physics`: a
    // hero carrying something heavy jumps lower; carrying something buoyant falls slower.
    jump_raw: i32,
    grav_raw: i32,
    // One-shot persistent horizontal velocity in Fixed raw (a throw arc). While non-zero it
    // REPLACES the dir*speed walk velocity, and it clears itself on grounding or on hitting a
    // wall — a plain platformer body has no persistent vx (it is recomputed from `dir` every
    // frame), so a thrown entity needs this to fly. Set by `platformer_launch`.
    launch_raw: i32,
    // Standing on a carrier (`set_carrier`) this frame. `riding` gates `carrier` — an encoded
    // entity id, which may legitimately be 0, so the bool is the validity bit, not the id.
    riding: bool,
    carrier: i32,
}

/// Hit points with post-hit invincibility. `damage` ignores hits while `invuln > 0` and, on a
/// real hit, starts the i-frame window (the render flickers the sprite during it). At `hp <= 0`
/// the health system fires the entity's `onDeath` hook, or despawns it if none is defined.
#[derive(Clone, Copy, Default)]
struct Health {
    hp: i32,
    max: i32,
    invuln: i32,
    /// How many i-frames a hit grants (0 = none). The player wants a mercy window after being hit;
    /// a shmup enemy wants 0 so every bullet in a stream chips it. Defaults to `INVULN_FRAMES`.
    invuln_max: i32,
    dead: bool,
}

/// Native patrol AI for a platformer entity: walk in `dir`, reversing at walls and ledges. Runs
/// entirely in Rust (`patrol_system`) so a screen full of enemies costs no per-frame tish calls —
/// only their `onCollide` (which fires on contact). Needs `Platformer` (for movement + collision).
#[derive(Clone, Copy)]
struct Patrol {
    dir: i32,
    /// How the sprite should mirror to match `dir`. 0 = leave the sprite alone, 1 = hflip when
    /// walking RIGHT (art drawn facing left), 2 = hflip when walking LEFT (art drawn facing right).
    /// Without this a game has to add a per-frame tish `tick` back purely to call `setFlip`, which
    /// costs far more than the patrol it was meant to replace — see `patrol_system`.
    flip_mode: i32,
    /// Last direction the flip was issued for, so the sprite is only told when it changes.
    flipped_for: i32,
}
impl Default for Patrol {
    fn default() -> Self {
        Patrol {
            dir: -1,
            flip_mode: 0,
            flipped_for: 0,
        }
    }
}

/// Auto-despawn timer + off-screen flag (component `C_LIFE`). `ttl > 0` counts down each frame and
/// despawns the entity at 0 (spent bullets, a finished explosion). `offscreen` despawns it the moment
/// its box leaves the visible area (bullets that fly off the top; enemies that drift off the bottom).
/// The two combine — a bullet typically has both (a TTL backstop and off-screen cleanup).
#[derive(Clone, Copy, Default)]
struct Life {
    ttl: i32,
    offscreen: bool,
}

/// A rigid disc (component `C_DYNAMIC`). Golf and soccer are the same physics with different
/// tuning, which is why there is one of these and not two genre packages.
///
/// ⚠️ CONTACT RANK, NOT MASS, and that is a hardware decision. The textbook impulse split is
/// `m2/(m1+m2)` — a DIVISION per contact per iteration, and this chip has no divide instruction
/// (`docs/perf-rules.md` §3: one `% RING` on a hot path cost 1,400 of a frame's 4,389 ticks). Golf
/// and soccer need exactly two relationships: *equal* (agent vs agent, ball vs ball), where each
/// takes half the correction with a `>> 1`; and *one side wins* (a wall, a goalpost, an agent vs
/// the ball), where the lower rank takes all of it. 255 is immovable. That is zero divisions in
/// contact resolution, and it is a real limitation: there is no way to say "this crate is three
/// times heavier than that one".
#[derive(Clone, Copy, Default)]
struct Dynamic {
    /// Bounce, Q8 (256 = elastic, 0 = dead stop). Applied to the NORMAL component only.
    restitution: i32,
    /// Per-frame velocity retention, Q8. One multiply and a shift — never a divide.
    friction: i32,
    /// Speed SQUARED (raw Q8, `>> 8`) below which the body parks. Squared so the rest test needs
    /// no sqrt and no divide.
    rest_v2: i32,
    rank: u8,
    asleep: u8,
    /// Who last pushed this body. Soccer's last-toucher — own goals and assists — with no
    /// per-contact tish callback.
    last_hit: i32,
}

/// What a surface class DOES: a constant acceleration in px/frame^2 (a slope, wind, a conveyor)
/// plus a Q8 per-frame velocity retention (240 ~ green, 200 ~ rough, 150 ~ sand, 256 = ice).
///
/// This is `kart.rs`'s `top_speed_for` surface table generalised, so a slope, a sand trap and a
/// boost pad are ONE mechanism rather than three special cases.
#[derive(Clone, Copy, Default)]
struct SurfaceDef {
    ax: Fixed,
    ay: Fixed,
    friction: i32,
}

impl SurfaceDef {
    /// `Default::default()` is not callable in a const context, and the table lives in a `const`
    /// initialiser, so class 0 — "plain ground, no acceleration, use the body's own friction" —
    /// is spelled out here instead.
    const fn flat() -> Self {
        SurfaceDef {
            ax: Fixed::from_raw(0),
            ay: Fixed::from_raw(0),
            friction: 0,
        }
    }
}

/// A fixed set of entities created ONCE and re-armed forever after — the shape six examples in this
/// repo hand-rolled identically before it lived here (the topdown RPG port's cast, sub-weapons and
/// floor drops; its pool and cast spikes; `sunny-land`'s FX). A spawn costs ~1,400 ticks on the frame it
/// happens and reallocates sprite VRAM (`docs/perf-rules.md` §6); a re-arm is a few dozen stores.
///
/// ⚠️ `kind` IS the live flag. Three of the six hand-rolled versions had already collapsed the two
/// into one column because a pooled slot's payload is never meaningful while it is free — so -1 is
/// free and anything >= 0 is live and carries whatever the caller stored there.
///
/// There is deliberately no ttl column: retirement runs through `life_system`, which already owns
/// four rules a pooled projectile wants (ttl, off-screen, hurt box hits a solid tile, room cutoff).
/// A second countdown here would be a second answer to the same question.
struct Pool {
    ent: Vec<i32>,
    /// tish-agb sprite handle per slot, or -1 for an entity-only pool.
    spr: Vec<i32>,
    kind: Vec<i32>,
    /// The sprite offset, held on the POOL rather than re-applied per arm: it is a property of the
    /// sheet, not of the shot, and every hand-rolled version repeated `set_sprite_offset(e, -8, -8)`
    /// at each arm site only because `reset_entity` rebuilds `SpriteRef` with `ox`/`oy` at 0.
    ox: i32,
    oy: i32,
    live: i32,
    /// High-water live count. The pool spike's verify.sh already asserted this and computed it by
    /// scanning the whole pool on every arm.
    high: i32,
}

/// A contact hurt box (component `C_HURT`). On overlapping an entity whose `tag == target_tag` that
/// has `Health`, it deals `damage`; a bullet (`despawn_on_hit`) is then consumed, while a body-contact
/// hazard (an enemy ramming the player) stays and relies on the victim's i-frames to rate-limit. The
/// `combat_system` applies this in pure Rust every frame, so bullet-hell density costs no tish calls.
#[derive(Clone, Copy, Default)]
struct Hurt {
    damage: i32,
    target_tag: i32,
    despawn_on_hit: bool,
    /// Frames to stun the victim for on a landed hit. The classic boomerang is the reason this exists:
    /// it is not primarily a damage weapon, it is a *stopper* — it freezes what it touches for a
    /// moment and only outright kills the one-hit enemies.
    stun: i32,
    /// Which WEAPON this box is, as a `DMG_*` bit, or 0 for "untyped — lands on everything".
    /// Untyped is the default so every hurt box that predates this keeps its behaviour exactly.
    /// Paired with `World::immune` on the victim: a hit is discarded when `immune & damage_type`.
    damage_type: i32,
}

/// Native movement pattern (component `C_MOVER`). `mover_system` drives `Body` velocity each frame in
/// pure Rust: `pattern` 0 = straight (down at `base_vy`), 1 = weave (a sideways triangle wave of
/// amplitude `amp` over `period` frames while descending at `base_vy`). `t` is the internal phase
/// counter. This is what keeps a screen full of weaving enemies at zero per-frame tish cost — the
/// shmup counterpart to `patrol_system`; only firing/AI decisions stay in tish `tick`s.
#[derive(Clone, Copy, Default)]
struct Mover {
    pattern: u8,
    t: i32,
    base_vy: Fixed,
    amp: Fixed,
    period: i32,
}

/// Free top-down movement (component `C_TOPDOWN`). Each frame `topdown_move` sets the move intent
/// (`dx`/`dy` ∈ {-1,0,1}); `topdown_system` scales it by `speed` (× `TD_DIAG` per axis on a diagonal),
/// resolves the `Collider` box against solids axis-by-axis (no gravity), and clears the intent so a
/// frame with no input stops (arcade feel). `facing` persists while idle (0 down/1 up/2 left/3 right)
/// so a swing/animation knows which way the entity looks. `knock*`/`knock` carry a brief hit-shove
/// that overrides input (action-RPG hit-shove knockback), decaying to nothing over `TD_KNOCK_FRAMES`.
#[derive(Clone, Copy, Default)]
struct TopDown {
    dx: i32,
    dy: i32,
    facing: i32,
    moving: bool,
    speed: i32, // raw fixed px/frame
    kx: Fixed,
    ky: Fixed,
    knock: i32,
    snap_mode: u8,
    snap_dx: i32,
    snap_dy: i32,
    snap_target_x: Fixed,
    snap_target_y: Fixed,
}

/// Native chase AI: seek the camera target within `aggro` (manhattan px). `stride` > 0 = a directional
/// Directional walk clip on a shared strip: `base + facing * stride`, `frames` long.
#[derive(Clone, Copy, Default)]
struct DirAnim {
    base: i32,
    stride: i32,
    frames: i32,
    speed: i32,
}

/// sheet (row = facing, `stride` cols/row: idle at base, walk at base+1..4), animated at `anim_speed`;
/// `stride` == 0 = a non-directional creature (e.g. a bat) looped over frames `0..flap` (facing frozen).
#[derive(Clone, Copy, Default)]
struct Chase {
    aggro: i32,
    stride: i32,
    flap: i32,
    anim_speed: i32,
}

/// RTS "move to a destination". The destination itself lives in a shared `FlowField`, so N units
/// ordered to the same place cost ONE breadth-first search between them rather than N path queries:
/// this component only remembers which field to read and how fast to walk.
#[derive(Clone, Copy, Default)]
struct Seek {
    field: i32,
    /// Manhattan px from the field's goal at which the unit considers itself arrived and stops.
    /// Without it a crowd converging on one cell grinds against each other forever.
    arrive: i32,
    stride: i32,
    anim_speed: i32,
    done: bool,
}

/// Attack-move. `range`/`dmg`/`cooldown` are the unit's own weapon; `team` is what makes another
/// soldier an enemy — deliberately NOT the `tag`, because a game wants tags free for its own kinds
/// and an RTS re-uses one unit kind across both sides.
#[derive(Clone, Copy, Default)]
struct Soldier {
    team: i32,
    range: i32,
    dmg: i32,
    cooldown: i32,
    timer: i32,
    /// Entity id currently being engaged, or -1. Held across frames so a unit does not re-acquire
    /// (and re-scan every other unit) every single frame.
    target: i32,
    recheck: i32,
}

/// Fog-of-war emitter: reveals a disc of `radius` cells around itself every frame.
#[derive(Clone, Copy, Default)]
struct Vision {
    radius: i32,
    /// Last cell this entity was seen in, so `fog_system` can skip the whole pass on a frame where
    /// nobody crossed a cell boundary.
    last_col: i32,
    last_row: i32,
}

/// How many flow fields can exist at once. Not a hardware limit — a budget: a field is 2 bytes per
/// map cell, and an RTS wants one per STANDING ORDER, not one per unit.
///
/// Six, because a harvest run needs TWO: one to the resource and one back to the drop-off. Sharing
/// a single field between the two legs means an outbound worker re-goals the field under a
/// returning one and the whole crew follows whoever moved last — measured as an economy that
/// simply never accrued anything.
const MAX_FLOWS: usize = 6;

const FOG_UNSEEN: u8 = 0;
const FOG_EXPLORED: u8 = 1;
const FOG_VISIBLE: u8 = 2;

/// A breadth-first distance field over the collision grid, measured in steps FROM the goal. Every
/// unit ordered to the same place shares one of these; walking is then "step to whichever neighbour
/// has a smaller number", which is two array reads per axis with no call, divide or float.
struct FlowField {
    cols: i32,
    rows: i32,
    goal_col: i32,
    goal_row: i32,
    /// `u16::MAX` = unreachable. Sized to the grid on first use and reused thereafter.
    dist: Vec<u16>,
    /// BFS frontier, kept as a field so a rebuild allocates nothing.
    queue: Vec<i32>,
    ready: bool,
}

impl FlowField {
    const fn new() -> Self {
        FlowField {
            cols: 0,
            rows: 0,
            goal_col: -1,
            goal_row: -1,
            dist: Vec::new(),
            queue: Vec::new(),
            ready: false,
        }
    }
}

/// Fog of war: one byte per map cell of `FOG_*`, plus what was last painted to the shroud layer so
/// a blit only writes what changed.
struct Fog {
    cols: i32,
    rows: i32,
    state: Vec<u8>,
    /// Per BG cell (256 of them): the fog state currently painted there.
    shown: Vec<u8>,
    /// Per BG cell: which MAP cell it is currently showing, or -1. The shroud layer wraps every 16
    /// cells, so a BG cell is reused by a different map cell as the camera scrolls — without this
    /// the layer would keep stale shroud from wherever the camera used to be.
    win: Vec<i32>,
    on: bool,
}

impl Fog {
    const fn new() -> Self {
        Fog {
            cols: 0,
            rows: 0,
            state: Vec::new(),
            shown: Vec::new(),
            win: Vec::new(),
            on: false,
        }
    }
}

#[derive(Clone, Copy, Default)]
/// A continuously-walking, tile-aligned wanderer.
///
/// The behaviour `set_hopper` could not express, and the reason enemy movement was wrong. A hopper
/// idles ~30 frames, picks a RANDOM cardinal direction and lurches a whole 16 px. A wanderer walks
/// without stopping and may only change direction when it is standing exactly on a tile boundary,
/// and its turns are never random — it always turns onto the PERPENDICULAR axis, toward the target
/// (the NES original's target-player / turn-if-time / turn-on-axis walker rules).
///
/// Generic on purpose: nothing here knows about any one game. `turn_rate` and the walker's speed are the
/// caller's data.
struct Wanderer {
    /// 0..255. ⚠️ HIGHER MEANS MORE AGGRESSIVE, which reads backwards until you follow it through:
    /// the roll is `rand(256) > turn_rate -> drift`, so a big `turn_rate` makes the drift branch
    /// rare and the "line up on the target" branch common.
    turn_rate: i32,
    /// Counts down; a turn is only allowed at zero, so this is what spaces turns out in time.
    turn_timer: i32,
    /// Set when the walker has just turned to face the target — i.e. it is lined up and would like
    /// to shoot. A shooting system consumes it; the walker itself never reads it.
    want_shoot: i32,
    /// Last RAW fixed-point position (`Fixed::to_raw`), used only to notice that the walker is
    /// BLOCKED. Raw, not floored: at 0.5 px/frame the floor only changes every other frame, so a
    /// floored comparison calls a perfectly healthy walker "blocked" half the time.
    last_x: i32,
    last_y: i32,
    /// The room this walker belongs to, captured when it was configured.
    ///
    /// ⚠️ NOT the camera's room. Confining to `room_cam.cur_rx/cur_ry` looked equivalent and was
    /// not: the camera follows the player, and when the player was knocked into the next room the
    /// clamp rect jumped with it and TELEPORTED every wanderer — measured as a -38.5 px step in one
    /// frame. A monster belongs to the room it was placed in, whatever the camera is showing.
    home_rx: i32,
    home_ry: i32,
}

#[derive(Clone, Copy, Default)]
struct Hopper {
    stride: i32,
    timer: i32,
    state: i32,
    start_x: Fixed,
    start_y: Fixed,
    // The hop's direction, re-asserted into topdown intent EVERY frame of the hop:
    // topdown_system consumes dx/dy each frame, so setting them only on the hop's
    // first frame moved the enemy exactly 1px per 16px hop. Every hopper walker in
    // every game was effectively stationary (caught by a basic-foes spike).
    dir_x: i32,
    dir_y: i32,
}

#[derive(Clone, Copy, Default)]
struct Shooter {
    interval: i32,
    timer: i32,
    speed: Fixed,
    /// false → fire along the entity's facing (a spitter shoots the way it walks); true → fire at
    /// wherever the player is standing.
    aimed: bool,
    style: BulletStyle,
}

#[derive(Clone, Copy, Default)]
struct Charger {
    /// Raw fixed px/frame while charging, and the entity's normal speed to restore afterwards.
    speed: i32,
    base: i32,
    /// px of slack that still counts as "lined up" on the other axis.
    band: i32,
    active: bool,
}

/// Stun-on-overlap grabber (`C_GRABBER`). Minimal: brief stun on the tagged target, no teleport.
#[derive(Clone, Copy, Default)]
struct Grabber {
    target_tag: i32,
}

/// Blade-trap dash (`C_TRAP`). Parks at `home_*` until the player shares a row/col within `band`,
/// then bolts like a charger. Restores idle (zero intent, parked speed) when the line breaks.
#[derive(Clone, Copy, Default)]
struct Trap {
    home_x: Fixed,
    home_y: Fixed,
    speed: i32,
    base: i32,
    band: i32,
    active: bool,
}

/// Follow / link modes for `C_FOLLOW`.
const FOLLOW_PART: u8 = 0;
const FOLLOW_TRAIN: u8 = 1;
const FOLLOW_ORBIT: u8 = 2;

/// Parent-linked transform follow (`C_FOLLOW`): a part/train segment keeps a fixed offset from its
/// parent; an orbiter circles at `radius` px (angle in 1/256ths of a turn).
#[derive(Clone, Copy, Default)]
struct Follow {
    kind: u8,
    parent: i32,
    radius: i32,
    ox: Fixed,
    oy: Fixed,
    angle: i32,
}

/// Boomerang return-mover (`C_BOOMERANG`): after `timer` frames, reverse `Body` velocity toward
/// `owner`. Pair with `set_lifetime` / `set_despawn_offscreen` like any other projectile.
#[derive(Clone, Copy, Default)]
struct Boomerang {
    timer: i32,
    owner: i32,
    returning: bool,
}

// ── Second component mask (`mask2`) ──────────────────────────────────────────────────────────────
// All 32 bits of `mask` are taken (C_SLEEP is bit 31), so the NES-era enemy/boss natives live on a
// parallel `mask2` column. Every bit here defaults to 0, so entities that never call the new
// natives behave exactly as before — the guards below are strictly additive.
/// Native AI state machine (`nai` column): ambusher / drifter / flicker-caster / bouncer.
const M2_NAI: u32 = 1 << 0;
/// Invulnerable right now (a drifter in flight, a caster mid-flicker). `damage()` refuses, but the
/// entity is still tangible — its contact hurt box still lands. Readable via `entity_phased`.
const M2_PHASED: u32 = 1 << 1;
/// Fully intangible AND invisible (an ambusher underground, a caster between appearances): no damage
/// taken, no contact damage dealt, sprite hidden. Implies the `M2_PHASED` damage refusal.
const M2_HIDDEN: u32 = 1 << 2;
/// Damage to this entity is re-routed to `zx.proxy` (boss neck → head, body segment → tail).
const M2_PROXY: u32 = 1 << 3;
/// Damage only lands while `zx.gate != 0` (an eye-open window, a boss's last-hit window).
const M2_GATE: u32 = 1 << 4;
/// On death, push `zx.code` to the world's death-note queue (part-death notification to the
/// parent's tish logic — read with `death_note()`).
const M2_NOTE: u32 = 1 << 5;
/// Rideable: this entity's top edge acts as one-way moving ground for platformer bodies
/// (`set_carrier`). A rider inherits the carrier's frame-to-frame motion while standing on it.
/// The carrier may itself be a platformer body (a walking beast — integrated in pass 1 of
/// `platformer_system`) or a Body/mover float (a raft — integrated earlier, in `movement_system`).
const M2_CARRIER: u32 = 1 << 6;

/// A continuous tile-aligned wanderer walker (the NES-era rank-and-file walk pattern).
///
/// ⚠️ This lives in `mask2` because the `mask` word is FULL — bit 31 (C_SLEEP) is the last one.
const M2_WANDERER: u32 = 1 << 7;

/// Gravity for the bouncer's hop arc, Q8 px/frame² (~0.16 px/f²).
const NAI_BOUNCE_G: i32 = 40;

/// Enemy AI column (`M2_NAI`). One state machine struct for all four kinds keeps the spawn
/// and reset paths to a single extra column; each kind is O(1) per frame (a few compares + stores).
/// `kind`: 1 ambusher, 2 drifter, 3 flicker-caster, 4 bouncer. `a`/`b` are the two phase lengths
/// (hidden/surfaced, rest/fly, hidden/visible, rest/hop). `speed` is Q8 px/frame.
#[derive(Clone, Copy, Default)]
struct Nai {
    kind: u8,
    state: u8,
    timer: i32,
    a: i32,
    b: i32,
    speed: i32,
    /// Drifter spin-up/down increment per frame (Q8), computed once at configure time so the ramp
    /// costs no division on the hot path.
    step: i32,
    /// Current heading (-1/0/1 each axis).
    dx: i32,
    dy: i32,
    /// Kind-specific scratch: drifter = current ramped speed; flicker = fired-this-window flag;
    /// bouncer = the sprite's base `oy` (the hop arc writes `oy = base - z`).
    aux: i32,
    /// Bouncer hop arc, Q8 height + Q8 vertical speed.
    z: i32,
    vz: i32,
    /// Flicker-caster: the bullet style captured at configure time (same contract as `set_shooter`).
    style: BulletStyle,
}

/// Boss-glue column (`M2_PROXY` / `M2_GATE` / `M2_NOTE`): hit re-routing, vulnerability gate and
/// part-death notification. Read only when the matching mask2 bit is set.
#[derive(Clone, Copy, Default)]
struct Zx {
    proxy: i32,
    gate: i32,
    code: i32,
}

#[derive(Clone, Copy, Default)]
struct Jumper {
    timer: i32,
    state: i32, // 0 = idle, 1 = jumping
    dx: Fixed,
    dy: Fixed,
    z: Fixed,
    dz: Fixed,
}

/// A triangle wave in `[-amp, amp]` over `period` frames — a soft-float-free stand-in for `sin`, so a
/// weave costs a few integer/fixed ops (no `libm` on the FPU-less ARM7TDMI). `0` outside a valid period.
fn tri_fixed(t: i32, period: i32, amp: Fixed) -> Fixed {
    if period <= 0 {
        return Fixed::from_raw(0);
    }
    let half = (period / 2).max(1);
    let p = t.rem_euclid(period);
    // `frac` ramps 0→1→0 across the period (Q8 fixed).
    let frac_raw = if p < half {
        (p * 256) / half
    } else {
        ((period - p) * 256) / half
    };
    let frac = Fixed::from_raw(frac_raw);
    // (frac*2 - 1) ∈ [-1, 1], then scale by amp.
    (frac * Fixed::from_raw(2 * 256) - Fixed::from_raw(256)) * amp
}

/// Room-locked camera: the map is a grid of screen-sized "rooms" (`room_w`×`room_h` tiles).
/// The camera locks to the player's current room instead of smooth-following; when the player
/// steps across a room boundary the view SLIDES to the next room over `dur` frames (input locked),
/// the player sliding from one screen edge to the other. `enabled=false` = classic follow camera.
#[derive(Clone, Copy, Default)]
struct RoomCam {
    enabled: bool,
    room_w: i32,
    room_h: i32,
    cur_rx: i32,
    cur_ry: i32,
    transitioning: bool,
    timer: i32,
    dur: i32,
    from_cam: (i32, i32),
    to_cam: (i32, i32),
    from_px: (i32, i32),
    to_px: (i32, i32),
}

// ── Behaviour bridge (the "Unity component" feel) ────────────────────────────
// A tish `defineComponent(name, { start, update })` registers a `ComponentDef`
// (the callbacks, held as `Value`s). `addBehaviour(entity, name, data)` attaches a
// `BehaviourInstance` (which def + the per-instance data object). Each frame the
// pipeline invokes `update(self, entity, dt)` — the callback reads/writes its `self`
// data and drives the entity through the engine API (`set_body`, `entity_x`, …).

/// A registered component type: its name + `start`/`update` callbacks (`Value::Null`
/// when absent). Callbacks are tish functions boxed as `Value::Function`.
struct ComponentDef {
    name: String,
    start: Value,
    update: Value,
    on_collide: Value,
    on_interact: Value,
    on_death: Value,
    /// Fast per-frame hook. Unlike `update` (which builds a rich method-bearing `this` and makes an
    /// ABI round-trip per operation), `tick` is handed the entity's `data` object pre-filled with
    /// its state (x/y/grounded/blocked/…) and reads its decisions back out (move/jump/…) — so a
    /// per-frame behaviour costs ONE tish call plus plain field reads/writes, not ~8 ABI trips.
    tick: Value,
    /// A `lean: true` component's tick is called with just the entity id (a number) and NO boxed
    /// context object: no per-frame `set_prop` marshalling of x/y/vx/vy in, no `prop_num` readback
    /// out. The tish tick reads state via the typed getters (`entity_x`, `cvar`, …) and writes via
    /// the typed setters (`set_body`, `set_cvar`, …), so a fully-typed tick pays zero boxed field ops.
    lean: bool,
}

/// A behaviour attached to one entity: which def, its mutable per-instance `data`
/// (a tish object, shared by `Rc` so callback mutations persist), and whether
/// `start` has run yet.
struct BehaviourInstance {
    def: usize,
    data: Value,
    started: bool,
}

// ── The SoA world ────────────────────────────────────────────────────────────
// Parallel columns indexed by entity SLOT. `mask[s]` says which components slot `s`
// has; systems iterate slots and test the mask. Generational ids (`gen[s]`) make a
// reused slot's id distinct so a stale entity handle can't alias a new entity.
struct World {
    gen: Vec<u16>,
    alive: Vec<bool>,
    mask: Vec<u32>,
    free: Vec<u32>,
    /// Which pool owns this slot, packed `(pool << 16) | poolSlot`, or -1 for an unpooled entity.
    ///
    /// ⚠️ This is OWNERSHIP, not state. `reset_entity` must NOT clear it — a re-armed slot is still
    /// the pool's, and clearing it would strand the pool's `live` count on the next retire. `despawn`
    /// and `clear_world` must, because the slot has left the pool's hands entirely.
    pool_of: Vec<i32>,
    pools: Vec<Pool>,
    dynamic: Vec<Dynamic>,
    /// Per-tile SURFACE class, 4 bits per cell (16 classes). Parallel to `solid` and — like
    /// `oneway` — EMPTY until the first write, so a game with no surfaces pays nothing. A 64x32
    /// golf course is 1 KB; a 240x80 overworld would be 9.6 KB, which is why it is a nibble plane
    /// and not a byte one.
    surface: Vec<u8>,
    surf: [SurfaceDef; 16],
    /// Reused scratch for the disc broadphase — never allocated per frame, same reason
    /// `buf_updates`/`buf_ticks` exist.
    buf_dyn: Vec<u32>,
    transform: Vec<Transform>,
    body: Vec<Body>,
    sprite: Vec<SpriteRef>,
    collider: Vec<Collider>,
    gridpos: Vec<GridPos>,
    anim: Vec<Anim>,
    walk: Vec<Walk>,
    platformer: Vec<Platformer>,
    health: Vec<Health>,
    patrol: Vec<Patrol>,
    life: Vec<Life>,
    hurt: Vec<Hurt>,
    guard: Vec<i32>,
    /// Weapon kinds that bounce off this entity, as `DMG_*` bits. 0 = hurt by everything, which is
    /// every entity until something calls `set_immunity`.
    ///
    /// A slab of its own rather than a field on `Health`, because `set_health` rebuilds `Health`
    /// wholesale — a boss that re-arms its hit points mid-fight (multi-phase bosses do) would
    /// silently lose its immunities.
    immune: Vec<i32>,
    /// Damage-type vulnerability mask (`DMG_*` bits). 0 = hurt by everything; non-zero ALLOWS only
    /// matching weapon kinds (the complement of `immune`). Same slab rationale as `immune`.
    weak: Vec<i32>,
    diranim: Vec<DirAnim>,
    mover: Vec<Mover>,
    topdown: Vec<TopDown>,
    /// Shared destination fields (RTS move orders) and the fog-of-war plane. Both are world state
    /// rather than separate globals, because both are derived from the collision grid the World
    /// already owns — a second global would be a second thing to keep in step.
    flows: [FlowField; MAX_FLOWS],
    fog: Fog,
    /// Terrain as a streamed window (an alternative to `scene:`; see `terrain_load`).
    terr: Vec<i32>,
    terr_cols: i32,
    terr_rows: i32,
    terr_shown: Vec<i32>,
    terr_win: Vec<i32>,
    /// False until fog has been computed once, so the first frame always runs a full pass.
    fog_settled: bool,
    chase: Vec<Chase>,
    seek: Vec<Seek>,
    soldier: Vec<Soldier>,
    vision: Vec<Vision>,
    hopper: Vec<Hopper>,
    jumper: Vec<Jumper>,
    /// Per-entity game-defined kind tag (0 = untagged). Not a component — a lightweight label so a
    /// collision/interaction handler can tell *what* it hit (player vs enemy vs pickup) without
    /// threading the other entity's component data through. Set with `set_tag`, read with `entity_tag`.
    tag: Vec<i32>,
    shooter: Vec<Shooter>,
    charger: Vec<Charger>,
    grabber: Vec<Grabber>,
    trap: Vec<Trap>,
    follow: Vec<Follow>,
    boomerang: Vec<Boomerang>,
    /// Second component mask — see the `M2_*` bits. Additive: 0 means "nothing new applies".
    mask2: Vec<u32>,
    /// S1: every mask bit EVER attached this scene, so a system whose component has no users can be
    /// skipped without scanning. STICKY on purpose — cleared only by `clear_world` — because
    /// per-despawn recount bookkeeping is exactly the kind of many-site invariant that rots; a
    /// system whose last user died keeps scanning until the scene changes, which is today's cost.
    used: u32,
    used2: u32,
    nai: Vec<Nai>,
    wanderer: Vec<Wanderer>,
    zx: Vec<Zx>,
    /// Carrier support (`M2_CARRIER`): each carrier's top-left transform as of the END of last
    /// frame's `platformer_system`, so riders can inherit this frame's motion delta. Gated by the
    /// mask2 bit — a stale value in a recycled slot is unreachable.
    carr_prev: Vec<(Fixed, Fixed)>,
    /// Part-death notification FIFO (`set_death_note` / `death_note`), capped small — a boss has a
    /// handful of parts, not hundreds.
    death_notes: Vec<i32>,
    /// Boomerang catches since the game last asked (`boomerang_caught`).
    boomer_catches: i32,
    /// Frames of stun remaining. While non-zero the native AI systems (hopper / chase / jumper)
    /// hold the entity still. Written by `hurt_system` from the hurt box's own `stun`.
    stun: Vec<i32>,
    /// A decoy the native AI prefers over the player while it lasts: `(entity, radius px, frames)`.
    /// This is the classic bait item — enemies inside the radius walk to it instead of at the player.
    lure: (i32, i32, i32),
    /// Per-entity TYPED state slots — 8 raw `i32` cells a component's tish `tick` can read/write
    /// natively (via `cvar`/`set_cvar` typed externs), instead of storing counters/flags on a boxed
    /// `Value` context object and paying a hashmap `get_prop`/`set_prop` for every field access every
    /// frame. `fixed` values are stored as their raw i32 (`cvarf`/`set_cvarf` do the ×256 conversion).
    ndata: Vec<[i32; 8]>,
    /// Per-entity behaviour (most entities have none → `Option`, and `Value` isn't `Copy`).
    behaviour: Vec<Option<BehaviourInstance>>,
    /// Registered component types (the `defineComponent` registry, not per-entity).
    defs: Vec<ComponentDef>,
    /// Tile-collision grid for the RPG genre. Bit-packed (1 bit/cell) so a 240×80 overworld
    /// is ~2.4KB instead of ~19KB per plane — critical for EWRAM when warping to/from caves.
    grid_cols: i32,
    grid_rows: i32,
    solid: Vec<u8>,
    /// One-way platform bits (parallel to `solid`). Empty until first `grid_set_oneway`.
    oneway: Vec<u8>,
    /// Climbable (ladder / vine / rope) bits, parallel to `solid`. Empty until first
    /// `grid_set_ladder`, so nothing but a side-scroller with ladders pays for the plane.
    /// The physics does not read this — climbing is a state a game drives with `platformer_hold`
    /// + `set_transform`; the grid just answers "is there something to climb here?".
    ladder: Vec<u8>,
    /// Logical cell count the bitplanes cover (may exceed cols*rows after a shrink).
    grid_cells: usize,
    /// Entity the camera follows (its transform is centred on screen, clamped to the map).
    /// `None` = no camera (fixed screen; small maps).
    camera_target: Option<i32>,
    rng: u32,
    /// Room-locked camera (screen-by-screen with edge-slide transitions). Off by default;
    /// `set_room_camera` turns it on and takes over from the smooth-follow path.
    room_cam: RoomCam,
    /// Last camera top-left in world px (set by `update_camera`), used for off-screen culling.
    cam_x: i32,
    cam_y: i32,
    /// Current bullet spawn configuration for the native pattern emitters (`fire_ring` et al.).
    bullet_style: BulletStyle,
    /// Wrap-around ("toroidal") arena: the Asteroids rule, where leaving one edge re-enters the
    /// opposite one. Off by default; `set_arena_wrap` turns it on for the whole world.
    arena_wrap: bool,
    buf_updates: Vec<(Value, Value, i32)>,
    buf_ticks: Vec<TickJob>,
    buf_results: Vec<(i32, TickOut)>,
}

fn grid_bit_bytes(cells: usize) -> usize {
    cells.div_ceil(8)
}

fn grid_bit(bits: &[u8], i: usize) -> bool {
    let b = i / 8;
    if b >= bits.len() {
        return false;
    }
    (bits[b] & (1u8 << (i & 7))) != 0
}

fn grid_bit_set(bits: &mut [u8], i: usize, on: bool) {
    let b = i / 8;
    if b >= bits.len() {
        return;
    }
    if on {
        bits[b] |= 1u8 << (i & 7);
    } else {
        bits[b] &= !(1u8 << (i & 7));
    }
}

/// Pack `(slot, generation)` into the i32 entity id a tish program holds.
fn encode(slot: u32, gen: u16) -> i32 {
    ((gen as i32) << 16) | (slot as i32 & 0xFFFF)
}
/// Unpack an entity id into `(slot, generation)`.
fn decode(e: i32) -> (u32, u16) {
    ((e & 0xFFFF) as u32, ((e >> 16) & 0x7FFF) as u16)
}

/// Read a Value property as a number (0 if absent/not a number).
fn prop_num(obj: &Value, key: &str) -> f64 {
    match get_prop(obj, key) {
        Value::Number(x) => x,
        _ => 0.0,
    }
}
/// Read a Value property as a bool (a nonzero number counts as true).
fn prop_truthy(obj: &Value, key: &str) -> bool {
    match get_prop(obj, key) {
        Value::Bool(b) => b,
        Value::Number(x) => x != 0.0,
        _ => false,
    }
}

/// One entity's `tick` job: the callback + its data ctx + the state to pre-fill.
#[derive(Clone)]
struct TickJob {
    cb: Value,
    data: Value,
    entity: i32,
    x: i32,
    y: i32,
    grounded: bool,
    blocked: bool,
    /// Current free-movement velocity (px/frame), pre-filled into the ctx so a hook that ignores
    /// `vx`/`vy` keeps its heading. 0 for entities without a `Body`.
    vx: f64,
    vy: f64,
    /// Which output group this entity actually uses — so the pre-fill/read-back only touches the
    /// props that matter (a free-flying shmup enemy pays for `vx`/`vy`, not the 9 platformer props).
    platformer: bool,
    body: bool,
    /// A `lean` tick gets only the entity id (no boxed ctx); the loop skips all marshalling/readback.
    lean: bool,
}
/// A `tick`'s decisions, read back from the data ctx after the callback.
#[derive(Clone, Copy, Default)]
struct TickOut {
    move_dir: i32,
    jump: bool,
    jump_cut: bool,
    run: bool,
    drop: bool,
    flip: bool,
    bounce: i32,
    /// Free-movement velocity (px/frame) for a top-down / free-flying entity — a shmup enemy's
    /// weave/dive pattern writes these and the engine applies them to `Body`. Independent of the
    /// platformer `move`/`jump` outputs (an entity uses one movement model or the other). Pre-filled
    /// with the entity's current velocity, so a hook that leaves them alone keeps flying straight.
    vx: f64,
    vy: f64,
}

/// Move `*v` toward `target` by at most `step`, snapping on arrival. Returns whether it
/// reached the target this call.
fn approach(v: &mut Fixed, target: Fixed, step: Fixed) -> bool {
    if *v < target {
        *v += step;
        if *v >= target {
            *v = target;
            return true;
        }
        false
    } else if *v > target {
        *v -= step;
        if *v <= target {
            *v = target;
            return true;
        }
        false
    } else {
        true
    }
}

impl World {
    const fn new() -> Self {
        World {
            gen: Vec::new(),
            alive: Vec::new(),
            mask: Vec::new(),
            free: Vec::new(),
            pool_of: Vec::new(),
            pools: Vec::new(),
            dynamic: Vec::new(),
            surface: Vec::new(),
            surf: [SurfaceDef::flat(); 16],
            buf_dyn: Vec::new(),
            transform: Vec::new(),
            body: Vec::new(),
            sprite: Vec::new(),
            collider: Vec::new(),
            gridpos: Vec::new(),
            anim: Vec::new(),
            walk: Vec::new(),
            platformer: Vec::new(),
            health: Vec::new(),
            patrol: Vec::new(),
            life: Vec::new(),
            hurt: Vec::new(),
            guard: Vec::new(),
            immune: Vec::new(),
            weak: Vec::new(),
            diranim: Vec::new(),
            mover: Vec::new(),
            topdown: Vec::new(),
            flows: [
                FlowField::new(),
                FlowField::new(),
                FlowField::new(),
                FlowField::new(),
                FlowField::new(),
                FlowField::new(),
            ],
            fog: Fog::new(),
            terr: Vec::new(),
            terr_cols: 0,
            terr_rows: 0,
            terr_shown: Vec::new(),
            terr_win: Vec::new(),
            fog_settled: false,
            chase: Vec::new(),
            seek: Vec::new(),
            soldier: Vec::new(),
            vision: Vec::new(),
            hopper: Vec::new(),
            jumper: Vec::new(),
            tag: Vec::new(),
            shooter: Vec::new(),
            charger: Vec::new(),
            grabber: Vec::new(),
            trap: Vec::new(),
            follow: Vec::new(),
            boomerang: Vec::new(),
            mask2: Vec::new(),
            used: 0,
            used2: 0,
            nai: Vec::new(),
            wanderer: Vec::new(),
            zx: Vec::new(),
            carr_prev: Vec::new(),
            death_notes: Vec::new(),
            boomer_catches: 0,
            stun: Vec::new(),
            lure: (-1, 0, 0),
            ndata: Vec::new(),
            behaviour: Vec::new(),
            defs: Vec::new(),
            grid_cols: 0,
            grid_rows: 0,
            solid: Vec::new(),
            oneway: Vec::new(),
            ladder: Vec::new(),
            grid_cells: 0,
            camera_target: None,
            rng: 123456789,
            room_cam: RoomCam {
                enabled: false,
                room_w: 15,
                room_h: 10,
                cur_rx: 0,
                cur_ry: 0,
                transitioning: false,
                timer: 0,
                dur: 24,
                from_cam: (0, 0),
                to_cam: (0, 0),
                from_px: (0, 0),
                to_px: (0, 0),
            },
            cam_x: 0,
            cam_y: 0,
            bullet_style: BulletStyle {
                sheet: 0,
                frame: 0,
                size: 6,
                damage: 1,
                target: 0,
                tag: 0,
                ttl: 240,
                // 0 = untyped, which `BulletStyle::damage_type`'s own comment names as what every
                // existing caller gets.
                damage_type: 0,
            },
            arena_wrap: false,
            buf_updates: Vec::new(),
            buf_ticks: Vec::new(),
            buf_results: Vec::new(),
        }
    }

    fn spawn(&mut self) -> i32 {
        let slot = if let Some(s) = self.free.pop() {
            let s = s as usize;
            self.alive[s] = true;
            self.mask[s] = 0;
            self.behaviour[s] = None;
            self.tag[s] = 0; // a recycled slot must not inherit the previous entity's kind tag
            self.stun[s] = 0;
            // Same reason as `tag`: `immune` is not gated behind a component mask bit, so without
            // this a recycled slot would inherit whatever the last entity in it was immune to —
            // a boss's immunities turning up on a rank-and-file walker.
            self.immune[s] = 0;
            // Same reason as `immune`: weakness is not gated by a mask bit.
            self.weak[s] = 0;
            self.ndata[s] = [0; 8]; // clear typed state slots so a reused entity starts fresh
                                    // A reused slot must not carry the previous entity's sprite handle (despawn/clear_world
                                    // already free + clear it; reset defensively so a stale handle can't alias a recycled
                                    // tish-agb sprite).
            self.sprite[s].handle = -1;
            self.sprite[s].ox = 0;
            self.sprite[s].oy = 0;
            // A recycled slot belongs to nobody until a pool claims it, for the same reason `tag`
            // and `immune` are cleared here: inheriting the last occupant's owner is how a retire
            // ends up decrementing a pool that never armed this entity.
            self.pool_of[s] = -1;
            // mask2 gates every new column, but clear the columns too so a recycled slot cannot
            // leak a previous life's proxy target or death code if a bit is set later.
            self.mask2[s] = 0;
            self.nai[s] = Nai::default();
            self.wanderer[s] = Wanderer::default();
            self.zx[s] = Zx::default();
            self.carr_prev[s] = (Fixed::from_raw(0), Fixed::from_raw(0));
            s
        } else {
            let s = self.gen.len();
            self.gen.push(0);
            self.alive.push(true);
            self.mask.push(0);
            self.pool_of.push(-1);
            self.dynamic.push(Dynamic::default());
            self.transform.push(Transform::default());
            self.body.push(Body::default());
            self.sprite.push(SpriteRef {
                handle: -1,
                ox: 0,
                oy: 0,
            });
            self.collider.push(Collider::default());
            self.gridpos.push(GridPos::default());
            self.anim.push(Anim::default());
            self.walk.push(Walk::default());
            self.platformer.push(Platformer::default());
            self.health.push(Health::default());
            self.patrol.push(Patrol::default());
            self.life.push(Life::default());
            self.hurt.push(Hurt::default());
            self.guard.push(0);
            self.immune.push(0);
            self.weak.push(0);
            self.diranim.push(DirAnim::default());
            self.mover.push(Mover::default());
            self.topdown.push(TopDown::default());
            self.chase.push(Chase::default());
            self.seek.push(Seek::default());
            self.soldier.push(Soldier::default());
            self.vision.push(Vision::default());
            self.hopper.push(Hopper::default());
            self.jumper.push(Jumper::default());
            self.tag.push(0);
            self.shooter.push(Shooter::default());
            self.charger.push(Charger::default());
            self.grabber.push(Grabber::default());
            self.trap.push(Trap::default());
            self.follow.push(Follow::default());
            self.boomerang.push(Boomerang::default());
            self.mask2.push(0);
            self.nai.push(Nai::default());
            self.wanderer.push(Wanderer::default());
            self.zx.push(Zx::default());
            self.carr_prev
                .push((Fixed::from_raw(0), Fixed::from_raw(0)));
            self.stun.push(0);
            self.ndata.push([0; 8]);
            self.behaviour.push(None);
            s
        };
        encode(slot as u32, self.gen[slot])
    }

    /// Register a component type from a `defineComponent(name, config)` call, returning
    /// its def index. `config.start` / `config.update` are the tish callbacks.
    fn define_component(&mut self, name: String, config: &Value) -> usize {
        let start = get_prop(config, "start");
        let update = get_prop(config, "update");
        let on_collide = get_prop(config, "onCollide");
        let on_interact = get_prop(config, "onInteract");
        let on_death = get_prop(config, "onDeath");
        let tick = get_prop(config, "tick");
        let lean = get_prop(config, "lean").is_truthy();
        let idx = self.defs.len();
        self.defs.push(ComponentDef {
            name,
            start,
            update,
            on_collide,
            on_interact,
            on_death,
            tick,
            lean,
        });
        idx
    }

    fn def_index_by_name(&self, name: &str) -> Option<usize> {
        self.defs.iter().position(|d| d.name == name)
    }

    /// Collect the behaviour callbacks to run this frame as `(callback, self_data,
    /// entity)` tuples — under the world borrow — WITHOUT invoking them. The caller
    /// runs them after dropping the borrow, so a callback may re-enter the engine
    /// (`set_body`, …) without a `RefCell` double-borrow. A fresh behaviour's `start`
    /// is queued before its `update`.
    fn collect_behaviours(&mut self) {
        self.buf_updates.clear();
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.behaviour[s].is_none() {
                continue;
            }
            // Off-screen culling: skip the (expensive tish) behaviour callbacks for entities that
            // have scrolled out of view. The camera target is always active.
            if !self.is_active(s) {
                continue;
            }
            let (def_idx, was_started) = {
                let b = self.behaviour[s].as_ref().unwrap();
                (b.def, b.started)
            };
            // Peek what actually needs to run BEFORE cloning the data ctx — a component with only an
            // `onDeath` (e.g. a straight-flying shmup grunt) runs nothing here, and cloning its data
            // every frame for nothing is pure overhead at bullet-hell entity counts.
            let has_start = !was_started && !matches!(self.defs[def_idx].start, Value::Null);
            let has_update = !matches!(self.defs[def_idx].update, Value::Null);
            if !was_started {
                self.behaviour[s].as_mut().unwrap().started = true;
            }
            if !has_start && !has_update {
                continue;
            }
            let data = self.behaviour[s].as_ref().unwrap().data.clone();
            let entity = encode(s as u32, self.gen[s]);
            if has_start {
                self.buf_updates
                    .push((self.defs[def_idx].start.clone(), data.clone(), entity));
            }
            if has_update {
                self.buf_updates
                    .push((self.defs[def_idx].update.clone(), data, entity));
            }
        }
    }

    /// Gather the `tick` jobs for active entities (the fast per-frame hook). Called after
    /// `collect_behaviours` (so `start` has run). The caller fills each job's data ctx with state,
    /// invokes the callback, and applies the outputs — all without the world borrow held.
    fn collect_ticks(&mut self) {
        self.buf_ticks.clear();
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.behaviour[s].is_none() || !self.is_active(s) {
                continue;
            }
            let def_idx = self.behaviour[s].as_ref().unwrap().def;
            // Peek `tick` BEFORE cloning the data ctx — most entities have no tick (a straight grunt,
            // a bullet with no behaviour), so cloning their data every frame is wasted work.
            if matches!(self.defs[def_idx].tick, Value::Null) {
                continue;
            }
            let cb = self.defs[def_idx].tick.clone();
            let entity = encode(s as u32, self.gen[s]);
            // Lean tick: no ctx marshalling at all — the callback gets just the entity id and reads/
            // writes state through the typed getters/setters. Skips the data clone + x/y/vx/vy prep.
            if self.defs[def_idx].lean {
                self.buf_ticks.push(TickJob {
                    cb,
                    data: Value::Null,
                    entity,
                    x: 0,
                    y: 0,
                    grounded: false,
                    blocked: false,
                    vx: 0.0,
                    vy: 0.0,
                    platformer: false,
                    body: false,
                    lean: true,
                });
                continue;
            }
            let data = self.behaviour[s].as_ref().unwrap().data.clone();
            let has_p = self.any(s, C_PLATFORMER);
            let has_b = self.any(s, C_BODY);
            self.buf_ticks.push(TickJob {
                cb,
                data,
                entity,
                x: self.transform[s].x.floor(),
                y: self.transform[s].y.floor(),
                grounded: has_p && self.platformer[s].grounded,
                blocked: has_p && self.platformer[s].blocked,
                vx: if has_b {
                    from_fixed(self.body[s].vx)
                } else {
                    0.0
                },
                vy: if has_b {
                    from_fixed(self.body[s].vy)
                } else {
                    0.0
                },
                platformer: has_p,
                body: has_b,
                lean: false,
            });
        }
    }

    /// Apply a `tick`'s decisions to its entity's platformer (move intent, jump, run, drop, bounce)
    /// and sprite flip — all native, no ABI round-trip.
    fn apply_tick(&mut self, entity: i32, out: &TickOut) {
        let Some(s) = self.slot_of(entity) else {
            return;
        };
        if self.any(s, C_PLATFORMER) {
            let p = &mut self.platformer[s];
            p.dir = out.move_dir.signum();
            p.run = out.run;
            if out.drop {
                p.drop = P_DROP;
            }
            if out.jump {
                p.jump_buffer = P_JUMP_BUFFER;
            }
            if out.jump_cut && p.jumping && p.vy.to_raw() < 0 {
                p.vy = Fixed::from_raw(p.vy.to_raw() / 2);
                p.jumping = false;
            }
            if out.bounce > 0 {
                p.vy = Fixed::from_raw(-out.bounce.abs() * 256);
                p.grounded = false;
                p.jumping = false;
            }
        }
        // Free-movement steering (top-down / flying entities): a `tick`'s `vx`/`vy` (pre-filled with
        // the current velocity, so an untouched hook flies straight) write into `Body`, which
        // `movement_system` integrates. Platformer entities steer with `move`/`jump` instead, so this
        // is scoped to free-movers (has `Body`, not `Platformer`).
        if self.any(s, C_BODY) && !self.any(s, C_PLATFORMER) {
            self.body[s].vx = to_fixed(out.vx);
            self.body[s].vy = to_fixed(out.vy);
        }
        if self.any(s, C_SPRITE) {
            let h = self.sprite[s].handle;
            if h >= 0 {
                tish_agb::native_sprite_set_flip(h, out.flip);
            }
        }
    }

    /// The live slot for entity id `e`, or `None` if `e` is stale/dead/out-of-range.
    fn slot_of(&self, e: i32) -> Option<usize> {
        let (slot, g) = decode(e);
        let s = slot as usize;
        if s < self.alive.len() && self.alive[s] && self.gen[s] == g {
            Some(s)
        } else {
            None
        }
    }

    /// Read/write a component's typed i32 state slot (`k` clamped to 0..8). Native — the tish tick
    /// keeps its counters/flags here instead of on a boxed `Value` object.
    fn cvar(&self, e: i32, k: usize) -> i32 {
        self.slot_of(e).map(|s| self.ndata[s][k & 7]).unwrap_or(0)
    }
    fn set_cvar(&mut self, e: i32, k: usize, v: i32) {
        if let Some(s) = self.slot_of(e) {
            self.ndata[s][k & 7] = v;
        }
    }

    fn despawn(&mut self, e: i32) {
        if let Some(s) = self.slot_of(e) {
            // FREE the tish-agb sprite (releases its VRAM + recycles the arena slot), not just hide
            // it — otherwise every despawn/respawn cycle (projectiles, enemies) would leak a sprite.
            // The engine owns the reference, so drop it too so a reused entity slot can't reach it.
            if self.any(s, C_SPRITE) && self.sprite[s].handle >= 0 {
                tish_agb::native_sprite_destroy(self.sprite[s].handle);
                self.sprite[s].handle = -1;
            }
            self.alive[s] = false;
            self.mask[s] = 0;
            self.mask2[s] = 0;
            self.behaviour[s] = None;
            // Leaving a pool's hands for good. The pool's `ent[]` entry is now a stale id, which
            // `pool_arm` detects through `slot_of` and refuses rather than resetting a dead slot.
            self.pool_of[s] = -1;
            self.gen[s] = self.gen[s].wrapping_add(1);
            self.free.push(s as u32);
        }
    }

    /// Reset a LIVE entity to a blank slate without despawning it: every component bit,
    /// native-AI system, timer, cvar, tag, immunity, weakness, stun and behaviour is cleared, but
    /// the slot, its generation (the id stays valid) and its tish-agb sprite handle all
    /// survive. This is the keystone of a pooled cast: room population reconfigures a
    /// fixed set of entities instead of despawn/spawn churn — a spawn costs ~1,400 ticks
    /// on the frame it happens (docs/perf-rules.md §6) and reallocates sprite VRAM, while
    /// a reset is a few dozen stores. The kept sprite is HIDDEN (not freed) so a parked
    /// slot draws nothing; the caller re-points it with `sprite_set_sheet`/`sprite_set_frame`
    /// and shows it again when the slot is reconfigured. `C_SPRITE` stays set for a kept
    /// handle so a later `despawn`/`clear_world` still frees the VRAM.
    fn reset_entity(&mut self, e: i32) {
        if let Some(s) = self.slot_of(e) {
            let handle = self.sprite[s].handle;
            if handle >= 0 {
                tish_agb::native_sprite_set_visible(handle, false);
            }
            self.mask[s] = if handle >= 0 { C_SPRITE } else { 0 };
            self.used |= C_SPRITE;
            self.behaviour[s] = None;
            self.tag[s] = 0;
            self.stun[s] = 0;
            self.immune[s] = 0;
            self.weak[s] = 0;
            self.ndata[s] = [0; 8];
            self.transform[s] = Transform::default();
            self.body[s] = Body::default();
            self.collider[s] = Collider::default();
            self.gridpos[s] = GridPos::default();
            self.anim[s] = Anim::default();
            self.walk[s] = Walk::default();
            self.platformer[s] = Platformer::default();
            self.health[s] = Health::default();
            self.patrol[s] = Patrol::default();
            self.life[s] = Life::default();
            self.hurt[s] = Hurt::default();
            self.guard[s] = 0;
            self.diranim[s] = DirAnim::default();
            self.mover[s] = Mover::default();
            self.topdown[s] = TopDown::default();
            self.chase[s] = Chase::default();
            self.seek[s] = Seek::default();
            self.soldier[s] = Soldier::default();
            self.vision[s] = Vision::default();
            self.hopper[s] = Hopper::default();
            self.jumper[s] = Jumper::default();
            self.shooter[s] = Shooter::default();
            self.charger[s] = Charger::default();
            self.grabber[s] = Grabber::default();
            self.trap[s] = Trap::default();
            self.follow[s] = Follow::default();
            self.boomerang[s] = Boomerang::default();
            self.mask2[s] = 0;
            self.nai[s] = Nai::default();
            self.wanderer[s] = Wanderer::default();
            self.zx[s] = Zx::default();
            self.carr_prev[s] = (Fixed::from_raw(0), Fixed::from_raw(0));
            self.dynamic[s] = Dynamic::default();
            self.sprite[s] = SpriteRef {
                handle,
                ox: 0,
                oy: 0,
            };
        }
    }

    // ── Rigid discs ──────────────────────────────────────────────────────────
    // Golf, soccer, pinball, pool. See `struct Dynamic`.

    fn surface_at(&self, col: i32, row: i32) -> u8 {
        if self.surface.is_empty()
            || col < 0
            || row < 0
            || col >= self.grid_cols
            || row >= self.grid_rows
        {
            return 0;
        }
        let i = (row * self.grid_cols + col) as usize;
        let b = self.surface[i >> 1];
        if i & 1 == 0 {
            b & 0x0f
        } else {
            b >> 4
        }
    }

    fn grid_set_surface(&mut self, col: i32, row: i32, id: i32) {
        if col < 0 || row < 0 || col >= self.grid_cols || row >= self.grid_rows {
            return;
        }
        if self.surface.is_empty() {
            // Allocated on FIRST WRITE, exactly like `oneway`: a game with no surfaces pays nothing.
            self.surface = alloc::vec![0u8; self.grid_cells.div_ceil(2)];
        }
        let i = (row * self.grid_cols + col) as usize;
        let v = (id.clamp(0, 15) as u8) & 0x0f;
        let b = &mut self.surface[i >> 1];
        if i & 1 == 0 {
            *b = (*b & 0xf0) | v
        } else {
            *b = (*b & 0x0f) | (v << 4)
        }
    }

    /// How many substeps this velocity needs so a single-step clamp per axis stays EXACT.
    ///
    /// ⚠️ A COMPARE LADDER, NOT A DIVISION. The alternative is a swept `tmax/tdelta` DDA, which is
    /// a software division per axis per body per frame. Substepping instead preserves the sub-tile
    /// speed invariant that makes `last_cell` exact — the comment there records what violating it
    /// costs: "not a rounding error, it is a teleport".
    fn substeps(vx: Fixed, vy: Fixed) -> i32 {
        const D_MAX_STEP: i32 = 2048; // 8 px/frame, against TILE = 16
        let m = vx.to_raw().abs().max(vy.to_raw().abs());
        if m <= D_MAX_STEP {
            1
        } else if m <= D_MAX_STEP << 1 {
            2
        } else if m <= D_MAX_STEP << 2 {
            4
        } else {
            8
        }
    }

    fn dynamic_system(&mut self) {
        let n = self.alive.len();

        // ── Pass 1: surface acceleration + friction ──────────────────────────
        for s in 0..n {
            if !self.alive[s]
                || self.mask[s] & C_SLEEP != 0
                || !self.has(s, C_DYNAMIC | C_TRANSFORM | C_BODY)
                || self.dynamic[s].asleep != 0
            {
                continue;
            }
            let (cx, cy) = self.center_of(s);
            let id = self.surface_at(cx.to_raw() / (TILE << 8), cy.to_raw() / (TILE << 8)) as usize;
            let sd = self.surf[id];
            let f = if sd.friction != 0 {
                sd.friction
            } else {
                self.dynamic[s].friction
            };
            let mut b = self.body[s];
            b.vx += sd.ax;
            b.vy += sd.ay;
            // Q8 retention: a multiply and a shift, never a divide.
            b.vx = Fixed::from_raw((b.vx.to_raw() * f) >> 8);
            b.vy = Fixed::from_raw((b.vy.to_raw() * f) >> 8);
            self.body[s] = b;
        }

        // ── Pass 2: integrate, bouncing off solid tiles ──────────────────────
        //
        // ⚠️ `is_solid` REPORTS OUT-OF-BOUNDS AS SOLID, so a game that has set up no collision grid
        // has every cell reading solid and a ball bounces off the air on its first frame, loses its
        // energy to restitution and parks about two pixels from where it started. `life_system`
        // guards its own solid check the same way and says why: "a shmup sets up no grid (every
        // cell would read solid)". Without the grid there is nothing to bounce off, so integrate
        // plainly — the arena is whatever the game draws.
        let walled = self.grid_cols > 0;
        for s in 0..n {
            if !self.alive[s]
                || !self.has(s, C_DYNAMIC | C_TRANSFORM | C_BODY | C_COLLIDER)
                || self.dynamic[s].asleep != 0
            {
                continue;
            }
            let steps = Self::substeps(self.body[s].vx, self.body[s].vy);
            let rest = self.dynamic[s].restitution;
            for _ in 0..steps {
                let (vx, vy) = (self.body[s].vx / steps, self.body[s].vy / steps);
                let c = self.collider[s];
                // X, then Y — the same axis-by-axis clamp `platformer_system` uses, but REFLECTING
                // instead of zeroing.
                let t = self.transform[s];
                let nx = t.x + vx;
                if walled && self.box_hits_solid(nx, t.y, c.w, c.h) {
                    self.body[s].vx = Fixed::from_raw(-((self.body[s].vx.to_raw() * rest) >> 8));
                } else {
                    self.transform[s].x = nx;
                }
                let t = self.transform[s];
                let ny = t.y + vy;
                if walled && self.box_hits_solid(t.x, ny, c.w, c.h) {
                    self.body[s].vy = Fixed::from_raw(-((self.body[s].vy.to_raw() * rest) >> 8));
                } else {
                    self.transform[s].y = ny;
                }
            }
        }

        // ── Pass 3: sleep ────────────────────────────────────────────────────
        //
        // ⚠️ BEFORE CONTACT, NOT AFTER, and the ordering is the whole of a bug that took a soak to
        // see. Contact wakes a body it shoves, so that the tile-collision pass can test where it was
        // shoved TO. With the sleep test running last, that wake was undone in the same frame — the
        // body was asleep again by the time the next frame's tile check ran, so it skipped, and six
        // players herding a resting ball walked it straight through the hoarding: at frame 1,536 it
        // was at x=431 on a 352-wide pitch and still drifting, a fraction of a pixel per frame,
        // forever. Sleeping first means a body woken by contact stays awake for a frame and gets
        // its wall check.
        for s in 0..n {
            if !self.alive[s] || !self.has(s, C_DYNAMIC | C_BODY) || self.dynamic[s].asleep != 0 {
                continue;
            }
            let (vx, vy) = (self.body[s].vx.to_raw(), self.body[s].vy.to_raw());
            // Speed SQUARED, in raw Q8 shifted down — no sqrt, no divide.
            let v2 = ((vx >> 4) * (vx >> 4)) + ((vy >> 4) * (vy >> 4));
            if v2 <= self.dynamic[s].rest_v2 {
                self.body[s].vx = Fixed::from_raw(0);
                self.body[s].vy = Fixed::from_raw(0);
                self.dynamic[s].asleep = 1;
            }
        }
        // ── Pass 4: disc vs disc ─────────────────────────────────────────────
        // A SORTED SWEEP, not a spatial hash. Soccer is 12 agents plus a ball = 13 bodies = 78
        // pairs; a uniform-grid hash costs a bucket rebuild and an indirection every frame and
        // LOSES at n = 13. `collect_collisions`' own history is the precedent — its last
        // optimisation was a loop restructure, not a data structure.
        self.buf_dyn.clear();
        for s in 0..n {
            if self.alive[s] && self.has(s, C_DYNAMIC | C_CIRCLE | C_TRANSFORM | C_COLLIDER) {
                self.buf_dyn.push(s as u32);
            }
        }
        // Insertion sort by x: near-sorted frame to frame, so O(n) in practice.
        for i in 1..self.buf_dyn.len() {
            let mut j = i;
            while j > 0 {
                let (a, b) = (self.buf_dyn[j - 1] as usize, self.buf_dyn[j] as usize);
                if self.transform[a].x.to_raw() <= self.transform[b].x.to_raw() {
                    break;
                }
                self.buf_dyn.swap(j - 1, j);
                j -= 1;
            }
        }
        let m = self.buf_dyn.len();
        for i in 0..m {
            let a = self.buf_dyn[i] as usize;
            let ra = self.collider[a].w.to_raw() >> 1;
            for k in (i + 1)..m {
                let b = self.buf_dyn[k] as usize;
                let rb = self.collider[b].w.to_raw() >> 1;
                let (ca, cb) = (self.center_of(a), self.center_of(b));
                let dx = cb.0.to_raw() - ca.0.to_raw();
                // Sorted by x, so once the gap exceeds the widest possible contact nothing further
                // right can touch `a` either.
                if dx > ra + rb {
                    break;
                }
                let dy = cb.1.to_raw() - ca.1.to_raw();
                let rsum = ra + rb;
                // Integer pixel space for the test: raw Q8 squared overflows i32 fast, and the
                // radius-squared idiom is `kart.rs`'s (BOX_R2/HAZARD_R2).
                let (pdx, pdy, prs) = (dx >> 8, dy >> 8, rsum >> 8);
                if pdx * pdx + pdy * pdy > prs * prs {
                    continue;
                }
                if pdx == 0 && pdy == 0 {
                    continue;
                } // exactly concentric: no normal to push along
                self.resolve_pair(a, b, dx, dy, rsum);
            }
        }
    }

    /// One contact. ONE division here — the normalise — and it is paid per COLLIDING PAIR, never
    /// per body per frame. `Num::sqrt` is a digit-at-a-time restoring sqrt (shifts and subtracts,
    /// no division), which is why the cheap thing on this chip is the square root and the expensive
    /// one is the divide that follows it.
    fn resolve_pair(&mut self, a: usize, b: usize, dx: i32, dy: i32, rsum: i32) {
        let d2 = ((dx >> 8) * (dx >> 8) + (dy >> 8) * (dy >> 8)).max(1);
        let mut len = 1i32;
        while len * len < d2 {
            len += 1;
        } // integer sqrt, ~a dozen iterations at these radii
        let len_raw = (len << 8).max(1);
        let pen = rsum - len_raw;
        if pen <= 0 {
            return;
        }
        // Unit normal in Q8.
        let nx = (dx << 8) / len_raw;
        let ny = (dy << 8) / len_raw;

        // Rank decides who moves. EQUAL ranks split it with a shift; otherwise the lower rank takes
        // all of it. Zero divisions — see `struct Dynamic`.
        let (rka, rkb) = (self.dynamic[a].rank, self.dynamic[b].rank);
        let (sa, sb) = if rka == rkb {
            (pen >> 1, pen >> 1)
        } else if rka < rkb {
            (pen, 0)
        } else {
            (0, pen)
        };
        // ⚠️ MOVING A BODY WAKES IT, even when no impulse is exchanged.
        //
        // Pass 2 — the one that bounces a body off solid tiles — skips sleeping bodies, because a
        // parked body has nowhere to go. But a sleeping body can still be SHOVED here, and if it
        // stays asleep nothing ever tests where it was shoved TO. Six players herding a resting
        // ball walked it straight through the hoarding and out of the map: at frame 1,536 it was at
        // x=431 on a 352-wide pitch and still drifting, pushed a little further every frame by the
        // chasers that had followed it out. Waking it here puts it back under the wall check on the
        // very next frame.
        // ⚠️ A SHOVE MAY NOT PUT A BODY IN A WALL — checked HERE, not left to the integrator.
        //
        // The tile-collision pass runs before this one and skips sleeping bodies, so a resting body
        // shoved into a wall is never tested against it: six players herding a resting ball walked
        // it through the hoarding and out of the map, a fraction of a pixel per frame, forever
        // (x=431 on a 352-wide pitch and still going). Waking the body is not enough on its own —
        // the shove has already happened by the time anything else could look. Reverting a shove
        // that lands in a solid tile is local, needs no ordering to be right, and is correct for a
        // sleeping body and an awake one alike.
        if sa > 0 {
            let old = self.transform[a];
            self.transform[a].x -= Fixed::from_raw((nx * sa) >> 8);
            self.transform[a].y -= Fixed::from_raw((ny * sa) >> 8);
            let c = self.collider[a];
            if self.grid_cols > 0
                && self.box_hits_solid(self.transform[a].x, self.transform[a].y, c.w, c.h)
            {
                self.transform[a] = old;
            }
            self.dynamic[a].asleep = 0;
        }
        if sb > 0 {
            let old = self.transform[b];
            self.transform[b].x += Fixed::from_raw((nx * sb) >> 8);
            self.transform[b].y += Fixed::from_raw((ny * sb) >> 8);
            let c = self.collider[b];
            if self.grid_cols > 0
                && self.box_hits_solid(self.transform[b].x, self.transform[b].y, c.w, c.h)
            {
                self.transform[b] = old;
            }
            self.dynamic[b].asleep = 0;
        }

        // Exchange the normal component, scaled by the softer restitution of the two.
        let rest = self.dynamic[a].restitution.min(self.dynamic[b].restitution);
        let van = (self.body[a].vx.to_raw() * nx + self.body[a].vy.to_raw() * ny) >> 8;
        let vbn = (self.body[b].vx.to_raw() * nx + self.body[b].vy.to_raw() * ny) >> 8;
        let approaching = van - vbn;
        if approaching > 0 {
            let j = (approaching * (256 + rest)) >> 8;
            let (ja, jb) = if rka == rkb {
                (j >> 1, j >> 1)
            } else if rka < rkb {
                (j, 0)
            } else {
                (0, j)
            };
            if ja > 0 {
                self.body[a].vx -= Fixed::from_raw((nx * ja) >> 8);
                self.body[a].vy -= Fixed::from_raw((ny * ja) >> 8);
                self.dynamic[a].asleep = 0;
                self.dynamic[a].last_hit = encode(b as u32, self.gen[b]);
            }
            if jb > 0 {
                self.body[b].vx += Fixed::from_raw((nx * jb) >> 8);
                self.body[b].vy += Fixed::from_raw((ny * jb) >> 8);
                self.dynamic[b].asleep = 0;
                self.dynamic[b].last_hit = encode(a as u32, self.gen[a]);
            }
        }
    }

    // ── Entity pools ─────────────────────────────────────────────────────────
    // See `struct Pool`. The whole API is six calls and no policy: the pool does not decide when to
    // fire and carries no per-slot callback, because a tish closure per slot is ~151 bytes plus a
    // boxed `value_call` per retire — the cost a pool exists to remove.

    /// Create a pool of `count` entities, each with a sprite off `sheet` (or -1 for an entity-only
    /// pool). Every slot is spawned, hidden and left free. Returns the pool id, or -1 for `count<=0`.
    fn pool_new(&mut self, count: i32, sheet: i32, ox: i32, oy: i32) -> i32 {
        if count <= 0 {
            return -1;
        }
        let mut pool = Pool {
            ent: Vec::new(),
            spr: Vec::new(),
            kind: Vec::new(),
            ox,
            oy,
            live: 0,
            high: 0,
        };
        let p = self.pools.len() as i32;
        for i in 0..count {
            let e = self.spawn();
            let s = self.slot_of(e).unwrap();
            self.pool_of[s] = (p << 16) | i;
            let h = if sheet >= 0 {
                let h = tish_agb::sprite_new_typed(sheet);
                if h >= 0 {
                    tish_agb::native_sprite_set_visible(h, false);
                    self.sprite[s] = SpriteRef { handle: h, ox, oy };
                    self.mask[s] |= C_SPRITE;
                    self.used |= C_SPRITE;
                }
                h
            } else {
                -1
            };
            pool.ent.push(e);
            pool.spr.push(h);
            pool.kind.push(-1);
        }
        self.pools.push(pool);
        p
    }

    /// Arm a slot: reset it, restore the pool's sprite offset, show the sprite, store `kind`, and —
    /// when `ttl > 0` — hand retirement to `life_system`. Returns the entity id, so an arm site needs
    /// no second call before `set_transform`/`set_hurt`/…, or -1 if it could not arm.
    ///
    /// `slot >= 0` takes that exact slot and **refuses a live one**. That is a deliberate change from
    /// every hand-rolled version, which would stomp a live slot and relied on the caller checking
    /// `slotFree()` first; making refusal the engine's job deletes the guard at each site and closes
    /// the bug. A caller that means to re-arm calls `pool_retire` first.
    /// `POOL_ANY` (-1) takes the lowest free slot. `POOL_STEAL` (-2) takes the lowest free slot, or
    /// recycles the live one with the least time left.
    fn pool_arm(&mut self, p: i32, slot: i32, kind: i32, ttl: i32) -> i32 {
        let pi = match self.pools.get(p as usize) {
            Some(_) => p as usize,
            None => return -1,
        };
        let n = self.pools[pi].ent.len();
        let idx = if slot >= 0 {
            let i = slot as usize;
            if i >= n || self.pools[pi].kind[i] >= 0 {
                return -1;
            }
            i
        } else {
            match (0..n).find(|&i| self.pools[pi].kind[i] < 0) {
                Some(i) => i,
                // POOL_STEAL: no free slot, so take the live one closest to retiring. `sunny-land`'s
                // rule — an effect that steals the oldest reads better than one that does not appear.
                None if slot == -2 => {
                    let mut best = 0usize;
                    let mut best_ttl = i32::MAX;
                    for i in 0..n {
                        let t = self
                            .slot_of(self.pools[pi].ent[i])
                            .map(|s| self.life[s].ttl)
                            .unwrap_or(0);
                        if t < best_ttl {
                            best_ttl = t;
                            best = i;
                        }
                    }
                    self.pool_retire(p, best as i32);
                    best
                }
                None => return -1,
            }
        };
        let e = self.pools[pi].ent[idx];
        let s = match self.slot_of(e) {
            Some(s) => s,
            // The slot was despawned out from under the pool. Refuse rather than reset a dead entity.
            None => return -1,
        };
        self.reset_entity(e);
        let (ox, oy) = (self.pools[pi].ox, self.pools[pi].oy);
        self.sprite[s].ox = ox;
        self.sprite[s].oy = oy;
        let h = self.pools[pi].spr[idx];
        if h >= 0 {
            tish_agb::native_sprite_set_visible(h, true);
        }
        if ttl > 0 {
            self.life[s] = Life {
                ttl,
                offscreen: false,
            };
            self.mask[s] |= C_LIFE;
            self.used |= C_LIFE;
        }
        self.pools[pi].kind[idx] = kind;
        self.pools[pi].live += 1;
        if self.pools[pi].live > self.pools[pi].high {
            self.pools[pi].high = self.pools[pi].live;
        }
        e
    }

    /// Park a slot: reset the entity, hide its sprite, mark the slot free. Never despawns — the
    /// entity and its sprite VRAM outlive every arm/retire cycle, which is the point.
    fn pool_retire(&mut self, p: i32, slot: i32) {
        let pi = p as usize;
        let i = slot as usize;
        if pi >= self.pools.len() || i >= self.pools[pi].ent.len() || self.pools[pi].kind[i] < 0 {
            return;
        }
        let e = self.pools[pi].ent[i];
        self.reset_entity(e);
        if let Some(s) = self.slot_of(e) {
            self.mask[s] &= !C_LIFE;
        }
        if self.pools[pi].spr[i] >= 0 {
            tish_agb::native_sprite_set_visible(self.pools[pi].spr[i], false);
        }
        self.pools[pi].kind[i] = -1;
        self.pools[pi].live -= 1;
    }

    /// `life_system`'s hook. Takes the packed `pool_of` value so the retire path costs one unpack
    /// rather than a search.
    fn pool_retire_packed(&mut self, packed: i32) {
        self.pool_retire(packed >> 16, packed & 0xffff);
    }

    /// Retire every live slot. One call instead of the caller's ten, which matters on a room change.
    fn pool_clear(&mut self, p: i32) {
        let pi = p as usize;
        if pi >= self.pools.len() {
            return;
        }
        for i in 0..self.pools[pi].ent.len() as i32 {
            self.pool_retire(p, i);
        }
    }

    /// One slot's field: 0 kind · 1 ttl · 2 entity · 3 sprite. A selector rather than four
    /// accessors, matching `fx_set(id, field, value)` — every export is boot heap for every ROM that
    /// imports the engine, so four names for four integers is four names too many.
    fn pool_get(&self, p: i32, slot: i32, field: i32) -> i32 {
        let pi = p as usize;
        let i = slot as usize;
        if pi >= self.pools.len() || i >= self.pools[pi].ent.len() {
            return -1;
        }
        match field {
            0 => self.pools[pi].kind[i],
            1 => self
                .slot_of(self.pools[pi].ent[i])
                .map(|s| self.life[s].ttl)
                .unwrap_or(0),
            2 => self.pools[pi].ent[i],
            3 => self.pools[pi].spr[i],
            _ => -1,
        }
    }

    /// One pool's counter: 0 count · 1 live · 2 high-water.
    fn pool_stat(&self, p: i32, field: i32) -> i32 {
        let pi = p as usize;
        if pi >= self.pools.len() {
            return -1;
        }
        match field {
            0 => self.pools[pi].ent.len() as i32,
            1 => self.pools[pi].live,
            2 => self.pools[pi].high,
            _ => -1,
        }
    }

    /// Tear down the current scene: despawn every live entity and reset the grid, so a
    /// fresh scene can be built. Component definitions (from `mount`) survive; the caller
    /// resets tish-agb's sprite/background arenas (`sprite_clear` / `bg_clear`).
    fn clear_world(&mut self) {
        // S1: a fresh scene starts with no components attached anywhere.
        self.used = 0;
        self.used2 = 0;
        for s in 0..self.alive.len() {
            if self.alive[s] {
                if self.any(s, C_SPRITE) && self.sprite[s].handle >= 0 {
                    tish_agb::native_sprite_destroy(self.sprite[s].handle);
                    self.sprite[s].handle = -1;
                }
                self.alive[s] = false;
                self.mask[s] = 0;
                self.behaviour[s] = None;
                self.pool_of[s] = -1;
                self.gen[s] = self.gen[s].wrapping_add(1);
                self.free.push(s as u32);
            }
        }
        // Every pooled entity was just despawned above (which freed its sprite), so the pools now
        // describe nothing. Held pool ids become invalid; games rebuild after a scene load, which is
        // what `castBuild`/`weaponsBuild` already do at the same point in the frame.
        self.pools.clear();
        self.grid_cols = 0;
        self.grid_rows = 0;
        self.grid_cells = 0;
        // Keep bitplane capacity — grow-only across scene loads (see grid_setup).
        self.solid.clear();
        self.oneway.clear();
        self.ladder.clear();
        // ⚠️ The surface plane is scene state and MUST go with the scene. Left behind it is worse
        // than a leak: the next map inherits the last one's classes, so a fairway sits on the
        // previous hole's sand and the ball dies on grass that looks perfectly green.
        self.surface.clear();
    }

    fn has(&self, s: usize, bits: u32) -> bool {
        self.mask[s] & bits == bits
    }

    fn any(&self, s: usize, bits: u32) -> bool {
        (self.mask[s] & bits) != 0
    }

    /// Movement system (pure Rust): integrate `Body` into `Transform`.
    fn movement_system(&mut self) {
        for s in 0..self.alive.len() {
            // ⚠️ A RIGID DISC IS NOT MOVED HERE. `dynamic_system` owns its integration, because that
            // is where the tile bounce, the substepping and the sleep state live. Moved in both
            // places a disc travels at DOUBLE its velocity, and — far worse — half of that travel
            // is this unchecked `+=`, which walks it straight through a solid tile. It presented as
            // a football drifting out of the stadium a fraction of a pixel at a time with its
            // velocity reading zero, and it survived three wrong fixes aimed at the sleep logic
            // before anyone looked at what else writes a transform.
            if self.alive[s]
                && self.mask[s] & C_SLEEP == 0
                && self.has(s, C_TRANSFORM | C_BODY)
                && !self.any(s, C_DYNAMIC)
            {
                self.transform[s].x += self.body[s].vx;
                self.transform[s].y += self.body[s].vy;
            }
        }
    }

    /// Turn the screen into a torus: anything that leaves an edge re-enters the opposite one. This
    /// is the Asteroids rule, and it is a WORLD flag rather than a per-entity component because in
    /// such a game it holds for everything at once — ship, rocks, saucer and shots alike — and the
    /// shots are spawned natively by `fire_bullet`, where there is no tish-side handle to flag.
    ///
    /// A per-frame tish loop cannot do this: 20-odd rocks × (read x, read y, write transform) is
    /// several thousand ticks of call overhead, most of a 60fps frame, for arithmetic that costs
    /// nothing here.
    fn wrap_system(&mut self) {
        if !self.arena_wrap {
            return;
        }
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_TRANSFORM) {
                continue;
            }
            // The valid range for the top-left corner is [-w, 240) — i.e. an entity is teleported
            // only once it is FULLY past the edge, and lands exactly one span over, fully past the
            // opposite edge. So it slides off and back on with no gap and no visible jump, and the
            // wrap costs it not one frame of being invisible.
            let (w, h) = if self.has(s, C_COLLIDER) {
                (self.collider[s].w, self.collider[s].h)
            } else {
                (Fixed::from_raw(TILE << 8), Fixed::from_raw(TILE << 8))
            };
            let span_x = Fixed::from_raw(240 << 8) + w;
            let span_y = Fixed::from_raw(160 << 8) + h;
            let t = &mut self.transform[s];
            // `while`, not `if`: a teleport (a fresh spawn, a scene load) can leave something more
            // than one span out, and a single subtraction would strand it off-screen forever.
            while t.x >= Fixed::from_raw(240 << 8) {
                t.x -= span_x;
            }
            while t.x < -w {
                t.x += span_x;
            }
            while t.y >= Fixed::from_raw(160 << 8) {
                t.y -= span_y;
            }
            while t.y < -h {
                t.y += span_y;
            }
        }
    }

    /// Turn the wrap-around arena on or off for the whole world.
    fn set_arena_wrap(&mut self, on: bool) {
        self.arena_wrap = on;
    }

    // ── Side-scrolling platformer genre ──────────────────────────────────────
    /// Give an entity platformer physics (gravity + tile collision). It also needs a
    /// `Collider` (its hitbox) and a `Transform`; drive it with `platformer_walk`/`_jump`.
    fn set_platformer(&mut self, e: i32) {
        if let Some(s) = self.slot_of(e) {
            self.platformer[s] = Platformer {
                face: 1,
                ..Platformer::default()
            };
            self.mask[s] |= C_PLATFORMER;
            self.used |= C_PLATFORMER;
        }
    }

    /// Set the horizontal move intent this frame (dir < 0 left, 0 stop, > 0 right). Call every
    /// frame from input — 0 stops immediately (arcade feel). The system applies it at walk or run
    /// speed. No-op while a room slide has input locked.
    fn platformer_walk(&mut self, e: i32, dir: i32) {
        if self.input_locked(e) {
            return;
        }
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            let d = dir.signum();
            self.platformer[s].dir = d;
            // Only a real move updates facing — releasing the d-pad keeps you looking where you were.
            if d != 0 {
                self.platformer[s].face = d;
            }
        }
    }

    /// Set run mode for this frame (hold the run button). Off = walk speed. Call every frame.
    fn platformer_run(&mut self, e: i32, on: bool) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            self.platformer[s].run = on && !self.input_locked(e);
        }
    }

    /// Buffer a jump press (edge-trigger this from the player). The system fires it if the entity
    /// is grounded OR within its coyote-time window, so a press just before landing or just after
    /// a ledge still jumps. No-op while a room slide has input locked.
    fn platformer_jump(&mut self, e: i32) {
        if self.input_locked(e) {
            return;
        }
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            self.platformer[s].jump_buffer = P_JUMP_BUFFER;
        }
    }

    /// Release the jump button: if still rising from a jump, cut the upward velocity so a short
    /// tap makes a short hop (variable jump height). Call on the button-release edge.
    fn platformer_jump_release(&mut self, e: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            let p = &mut self.platformer[s];
            if p.jumping && p.vy.to_raw() < 0 {
                p.vy = Fixed::from_raw(p.vy.to_raw() / 2);
                p.jumping = false;
            }
        }
    }

    /// Request dropping through a one-way platform (hold Down + jump). Opens a short window in
    /// which one-way floors are ignored so the box falls through.
    fn platformer_drop(&mut self, e: i32) {
        if self.input_locked(e) {
            return;
        }
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            self.platformer[s].drop = P_DROP;
        }
    }

    /// Launch the entity upward at `vel` px/frame (stomp bounce, springs, knockback). Clears
    /// grounded so gravity takes over from the apex.
    fn platformer_bounce(&mut self, e: i32, vel: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            self.platformer[s].vy = Fixed::from_raw(-vel.abs() * 256);
            self.platformer[s].grounded = false;
            self.platformer[s].jumping = false;
        }
    }

    /// Set vertical velocity outright, in px/frame (negative rises, positive falls). `bounce` can
    /// only ever launch UPWARD (`-vel.abs()`), which is right for a stomp or a spring but cannot
    /// express the other half of the vocabulary: clamping a fall to a slow scrape down a wall,
    /// an updraft, a downward slam. This is the general form.
    fn platformer_set_vy(&mut self, e: i32, vy_raw: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            self.platformer[s].vy = Fixed::from_raw(vy_raw);
            // A jump that is being overridden is over — otherwise a later `jump_release` would try
            // to halve a velocity this call chose deliberately.
            self.platformer[s].jumping = false;
        }
    }

    /// Set this body's ground speeds, in Fixed raw. Either may be 0 to keep the engine default.
    fn platformer_set_speed(&mut self, e: i32, walk_raw: i32, run_raw: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            self.platformer[s].walk_raw = walk_raw;
            self.platformer[s].run_raw = run_raw;
        }
    }

    /// Per-entity jump impulse / gravity in Fixed raw; 0 keeps the engine default (P_JUMP /
    /// P_GRAVITY) — the same zero-means-default contract as `platformer_set_speed`, and for the
    /// same reason: held-weight physics belongs to one body, not to every game sharing the engine.
    fn platformer_set_physics(&mut self, e: i32, jump_raw: i32, grav_raw: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            self.platformer[s].jump_raw = jump_raw;
            self.platformer[s].grav_raw = grav_raw;
        }
    }

    /// Launch a platformer body on a throw arc: a persistent horizontal velocity (clears itself on
    /// landing or on hitting a wall — see `launch_raw`) plus an immediate vertical velocity.
    /// Un-holds and un-rides: a thrown body is airborne by definition.
    fn platformer_launch(&mut self, e: i32, vx_raw: i32, vy_raw: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_PLATFORMER)) {
            let p = &mut self.platformer[s];
            p.launch_raw = vx_raw;
            p.vy = Fixed::from_raw(vy_raw);
            p.held = false;
            p.riding = false;
            p.grounded = false;
        }
    }

    /// Is player input frozen because the camera target is mid room-slide?
    fn input_locked(&self, e: i32) -> bool {
        self.room_cam.enabled && self.room_cam.transitioning && self.camera_target == Some(e)
    }

    /// Does the AABB (top-left `x,y`, size `w×h`) overlap any solid tile? Uses inclusive
    /// pixel bounds so a box flush against a wall doesn't read the next tile as a hit.
    fn box_hits_solid(&self, x: Fixed, y: Fixed, w: Fixed, h: Fixed) -> bool {
        let left = x.floor();
        let top = y.floor();
        let right = ((x + w).to_raw() - 1) >> 8;
        let bottom = ((y + h).to_raw() - 1) >> 8;
        if right < left || bottom < top {
            return false;
        }
        let (c0, c1) = (left.div_euclid(TILE), right.div_euclid(TILE));
        let (r0, r1) = (top.div_euclid(TILE), bottom.div_euclid(TILE));
        let mut r = r0;
        while r <= r1 {
            let mut c = c0;
            while c <= c1 {
                if self.is_solid(c, r) {
                    return true;
                }
                c += 1;
            }
            r += 1;
        }
        false
    }

    /// Is a floor tile directly beneath the box's bottom edge — i.e., is the box resting on ground?
    /// Tests the tile ROW JUST BELOW the box, unlike `box_hits_solid` which tests the box's own
    /// cells: a box clamped exactly onto a tile top has no solid *inside* it, so without this it would
    /// micro-fall and re-land every few frames (flickering `grounded` and any grounded-driven
    /// animation). `oneway` counts one-way platforms as floor — pass false while dropping through one.
    fn on_floor(&self, x: Fixed, y: Fixed, w: Fixed, h: Fixed, oneway: bool) -> bool {
        let row = (y + h).floor().div_euclid(TILE);
        let c0 = x.floor().div_euclid(TILE);
        let c1 = (x + w).floor().saturating_sub(1).div_euclid(TILE);
        let mut c = c0;
        while c <= c1 {
            if self.is_solid(c, row) || (oneway && self.is_oneway(c, row)) {
                return true;
            }
            c += 1;
        }
        false
    }

    /// If the box's bottom crosses a one-way platform's top this move (landing on it from above),
    /// return the tile row to rest on; else `None`. One-way tiles only block downward from above.
    fn oneway_floor(
        &self,
        x: Fixed,
        prev_y: Fixed,
        new_y: Fixed,
        w: Fixed,
        h: Fixed,
    ) -> Option<i32> {
        let prev_bottom = (prev_y + h).floor();
        let new_bottom = (new_y + h).floor();
        let row = (new_bottom - 1).div_euclid(TILE);
        let top = row * TILE;
        if prev_bottom <= top && new_bottom > top {
            let (c0, c1) = (
                x.floor().div_euclid(TILE),
                (x + w).floor().saturating_sub(1).div_euclid(TILE),
            );
            let mut c = c0;
            while c <= c1 {
                if self.is_oneway(c, row) {
                    return Some(row);
                }
                c += 1;
            }
        }
        None
    }

    /// Platformer system: apply gravity, then move + resolve each platformer entity's box
    /// against the solid grid axis-by-axis (X then Y), snapping to the tile edge on a hit and
    /// setting `grounded` when it lands. Also handles game feel — coyote time, jump buffering,
    /// variable jump height, and one-way platforms. Speeds stay below `TILE`, so a single-step
    /// clamp per axis is exact. The camera target is skipped while a room slide freezes it.
    fn platformer_system(&mut self) {
        // Gather this frame's carriers once (a handful at most; fixed cap, no per-frame alloc —
        // extras past the cap simply act as plain entities this frame).
        let mut carriers = [0usize; 16];
        let mut ncar = 0usize;
        for s in 0..self.alive.len() {
            if self.alive[s]
                && self.mask2[s] & M2_CARRIER != 0
                && self.has(s, C_TRANSFORM | C_COLLIDER)
                && ncar < carriers.len()
            {
                carriers[ncar] = s;
                ncar += 1;
            }
        }
        let frozen = if self.room_cam.enabled && self.room_cam.transitioning {
            self.camera_target.and_then(|e| self.slot_of(e))
        } else {
            None
        };
        // Pass 1: carriers that are themselves platformer bodies (a walking beast) integrate FIRST,
        // so a rider always resolves against its carrier's FINAL position this frame — slot order
        // stops mattering. Body/mover carriers (a drifting raft) already moved in movement_system.
        for i in 0..ncar {
            let s = carriers[i];
            if !self.has(s, C_PLATFORMER) || Some(s) == frozen || !self.is_active(s) {
                continue;
            }
            self.platformer_integrate(s, &carriers[..ncar]);
        }
        // Pass 2: everyone else (riders included). Carriers are skipped: a platformer carrier ran
        // in pass 1, and a carrier cannot itself ride another carrier.
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_PLATFORMER | C_TRANSFORM | C_COLLIDER) {
                continue;
            }
            if self.mask2[s] & M2_CARRIER != 0 {
                continue;
            }
            if Some(s) == frozen || !self.is_active(s) {
                continue; // frozen mid room-slide, or culled off-screen (freeze until it scrolls back)
            }
            self.platformer_integrate(s, &carriers[..ncar]);
        }
        // Snapshot carrier positions: next frame's rider delta is measured from here. Done for
        // every carrier (even culled ones) so a carrier scrolling back on screen has no stale
        // multi-frame delta waiting to yank its rider.
        for &s in carriers.iter().take(ncar) {
            self.carr_prev[s] = (self.transform[s].x, self.transform[s].y);
        }
    }

    /// One platformer body's integration for this frame: gravity, jump/coyote/buffer game feel,
    /// axis-by-axis solid-grid resolution, one-way platforms, and carrier ride/landing. Extracted
    /// from `platformer_system` so pass 1 (carriers) and pass 2 (everyone else) share it exactly.
    fn platformer_integrate(&mut self, s: usize, carriers: &[usize]) {
        let term = Fixed::from_raw(P_TERMINAL);
        let zero = Fixed::from_raw(0);
        if self.platformer[s].held {
            // Held in place (e.g. hanging on a ledge grab): no gravity, no movement. The game
            // logic decides when to release (drop → fall) or climb (teleport up + release). Also
            // CLEAR any pending jump — otherwise a jump buffered just before the grab survives the
            // (skipped) physics and fires the instant a climb sets the body back down, launching it.
            let p = &mut self.platformer[s];
            p.vx = Fixed::from_raw(0);
            p.vy = Fixed::from_raw(0);
            p.grounded = false;
            p.blocked = false;
            p.jump_buffer = 0;
            p.coyote = 0;
            p.jumping = false;
            p.riding = false;
            p.launch_raw = 0;
            return;
        }
        // Rider pre-step: inherit the carrier's motion since last frame (horizontal AND vertical,
        // so a lift or a bobbing raft carries its rider) — or detach if the carrier despawned,
        // left the room, or stopped being a carrier. Generation-checked ids make the despawn case
        // a clean resolve failure, never a stale slot.
        if self.platformer[s].riding {
            let car = self.platformer[s].carrier;
            match self
                .slot_of(car)
                .filter(|&c| self.mask2[c] & M2_CARRIER != 0 && self.same_room(s, c))
            {
                Some(c) => {
                    let (px, py) = self.carr_prev[c];
                    let dx = self.transform[c].x - px;
                    let dy = self.transform[c].y - py;
                    self.transform[s].x += dx;
                    self.transform[s].y += dy;
                }
                None => self.platformer[s].riding = false,
            }
        }
        let (w, h) = (self.collider[s].w, self.collider[s].h);
        let p = self.platformer[s];
        // Zero-means-default per-entity gravity/jump (`platformer_set_physics`): held-weight
        // physics — a body carrying something heavy jumps lower, something buoyant falls slower.
        let g = Fixed::from_raw(if p.grav_raw != 0 {
            p.grav_raw
        } else {
            P_GRAVITY
        });
        let was_grounded = p.grounded;
        let mut coyote = if was_grounded {
            P_COYOTE
        } else {
            (p.coyote - 1).max(0)
        };
        let mut jump_buffer = p.jump_buffer;
        let mut jumping = p.jumping;
        let speed = if p.run {
            if p.run_raw != 0 {
                p.run_raw
            } else {
                P_RUN
            }
        } else if p.walk_raw != 0 {
            p.walk_raw
        } else {
            P_WALK
        };
        // A launch (`platformer_launch` — a thrown body's arc) replaces the dir*speed walk
        // velocity until it lands or hits a wall; a plain platformer body has no persistent vx.
        let mut launch = p.launch_raw;
        let mut vx = if launch != 0 {
            Fixed::from_raw(launch)
        } else {
            Fixed::from_raw(p.dir.signum() * speed)
        };
        let mut vy = p.vy;
        // Fire a buffered jump if grounded or still within the coyote window.
        if jump_buffer > 0 && (was_grounded || coyote > 0) {
            vy = Fixed::from_raw(-(if p.jump_raw != 0 { p.jump_raw } else { P_JUMP }));
            jumping = true;
            jump_buffer = 0;
            coyote = 0;
        }
        jump_buffer = (jump_buffer - 1).max(0);
        let drop = (p.drop - 1).max(0);
        // Gravity; once falling, the jump is over (later releases can't cut it).
        vy += g;
        if vy > term {
            vy = term;
        }
        if vy.to_raw() >= 0 {
            jumping = false;
        }
        // X axis: move, then clamp against the leading vertical edge if it hit a wall.
        let (x, y) = (self.transform[s].x, self.transform[s].y);
        let nx = x + vx;
        let rx = if vx > zero && self.box_hits_solid(nx, y, w, h) {
            let col = last_cell(nx + w);
            vx = zero;
            launch = 0;
            Fixed::from_raw(col * TILE * 256) - w
        } else if vx < zero && self.box_hits_solid(nx, y, w, h) {
            let col = nx.floor().div_euclid(TILE);
            vx = zero;
            launch = 0;
            Fixed::from_raw((col + 1) * TILE * 256)
        } else {
            nx
        };
        // Y axis: floor/ceiling against solids, one-way platforms, then carriers when falling.
        let ny = y + vy;
        let mut grounded = false;
        let mut riding = false;
        let mut carrier_id = p.carrier;
        let mut ry = if vy > zero {
            if self.box_hits_solid(rx, ny, w, h) {
                let row = last_cell(ny + h);
                vy = zero;
                grounded = true;
                Fixed::from_raw(row * TILE * 256) - h
            } else if drop == 0 {
                match self.oneway_floor(rx, y, ny, w, h) {
                    Some(row) => {
                        vy = zero;
                        grounded = true;
                        Fixed::from_raw(row * TILE * 256) - h
                    }
                    // A carrier's top edge is one-way moving ground. Down+jump (`drop`) falls
                    // through it exactly like a one-way tile. A flush rider re-lands here every
                    // frame (gravity pulls its bottom across the carrier top), which is what keeps
                    // `grounded` stable while riding — the carrier analogue of `on_floor`.
                    None => match self.carrier_land(s, carriers, rx, y, ny, w, h) {
                        Some((top, id)) => {
                            vy = zero;
                            grounded = true;
                            riding = true;
                            carrier_id = id;
                            top - h
                        }
                        None => ny,
                    },
                }
            } else {
                ny
            }
        } else if vy < zero && self.box_hits_solid(rx, ny, w, h) {
            let row = ny.floor().div_euclid(TILE);
            vy = zero;
            Fixed::from_raw((row + 1) * TILE * 256)
        } else {
            ny
        };
        // Stable ground contact: if the box came to rest exactly on a tile top (or is falling the
        // last sub-pixel onto one), `box_hits_solid` misses it, so probe the row beneath. Keeps a
        // resting entity `grounded` every frame and snaps it flush to the tile — no landing
        // jitter, no idle/jump animation flicker. Skipped while moving upward or dropping through.
        if !grounded && vy >= zero && self.on_floor(rx, ry, w, h, drop == 0) {
            grounded = true;
            vy = zero;
            let floor_row = (ry + h).floor().div_euclid(TILE);
            ry = Fixed::from_raw(floor_row * TILE * 256) - h;
        }
        if grounded {
            launch = 0; // a throw arc ends on landing (tile, one-way or carrier alike)
        }
        self.transform[s].x = rx;
        self.transform[s].y = ry;
        // "blocked" = had horizontal intent but a wall zeroed the move (patrol AI turns on this).
        let blocked = p.dir != 0 && vx.to_raw() == 0;
        let p = &mut self.platformer[s];
        p.vx = vx;
        p.vy = vy;
        p.grounded = grounded;
        p.coyote = coyote;
        p.jump_buffer = jump_buffer;
        p.jumping = jumping;
        p.drop = drop;
        p.blocked = blocked;
        p.launch_raw = launch;
        p.riding = riding;
        p.carrier = carrier_id;
    }

    /// If the box's bottom crosses a carrier's top edge this move (landing on it from above),
    /// return `(top, carrier id)` for the highest such carrier; else None. The 4px upward
    /// tolerance lets a RISING carrier catch a body it lifted into (the raft coming up under a
    /// standing rider) without being large enough to teleport a body that walked into its side.
    fn carrier_land(
        &self,
        s: usize,
        carriers: &[usize],
        x: Fixed,
        prev_y: Fixed,
        new_y: Fixed,
        w: Fixed,
        h: Fixed,
    ) -> Option<(Fixed, i32)> {
        let prev_bottom = prev_y + h;
        let new_bottom = new_y + h;
        let tol = Fixed::from_raw(4 * 256);
        let mut best: Option<(Fixed, i32)> = None;
        for &c in carriers {
            if c == s || !self.alive[c] || !self.same_room(s, c) {
                continue;
            }
            let cx = self.transform[c].x;
            let cw = self.collider[c].w;
            if x + w <= cx || x >= cx + cw {
                continue;
            }
            let top = self.transform[c].y;
            if prev_bottom <= top + tol && new_bottom > top {
                let better = match best {
                    Some((bt, _)) => top < bt,
                    None => true,
                };
                if better {
                    best = Some((top, encode(c as u32, self.gen[c])));
                }
            }
        }
        best
    }

    // ── Free top-down (action-RPG) genre ────────────────────────────────────
    /// Give an entity free 8-directional top-down movement with solid-tile collision. It also needs
    /// a `Collider` (its hitbox) and a `Transform`; drive it with `topdown_move` each frame.
    fn set_topdown(&mut self, e: i32) {
        if let Some(s) = self.slot_of(e) {
            self.topdown[s] = TopDown {
                speed: TD_WALK,
                ..TopDown::default()
            };
            self.mask[s] |= C_TOPDOWN;
            self.used |= C_TOPDOWN;
        }
    }

    /// Mark an entity's collider as a solid blocker for top-down movers (an NPC the player can't
    /// walk through). Needs `C_COLLIDER` + `C_TRANSFORM`; the box travels with the entity.
    fn set_blocker(&mut self, e: i32) {
        if let Some(s) = self
            .slot_of(e)
            .filter(|&s| self.has(s, C_TRANSFORM | C_COLLIDER))
        {
            self.mask[s] |= C_BLOCKER;
            self.used |= C_BLOCKER;
        }
    }

    /// Make this entity's top edge one-way moving ground for platformer bodies (`M2_CARRIER`):
    /// stand on a walking beast or a drifting raft, inherit its motion, jump off normally, and
    /// Down+jump drops through it like a one-way tile. Needs `C_TRANSFORM` + `C_COLLIDER`; the
    /// surface travels with the entity. Carriers are ground, not walls — one moving sideways INTO
    /// a standing body pushes nothing — and a carrier cannot itself ride another carrier.
    fn set_carrier(&mut self, e: i32) {
        if let Some(s) = self
            .slot_of(e)
            .filter(|&s| self.has(s, C_TRANSFORM | C_COLLIDER))
        {
            self.mask2[s] |= M2_CARRIER;
            self.used2 |= M2_CARRIER;
            self.carr_prev[s] = (self.transform[s].x, self.transform[s].y);
        }
    }

    /// Would the box `(x,y,w,h)` overlap any `C_BLOCKER` other than `mover`? Returns the first hit's
    /// box so the caller can snap to its edge. Room-gated: with a room camera, only blockers in the
    /// same room count (no phantom walls through a doorway).
    fn first_blocker_hit(
        &self,
        mover: usize,
        x: Fixed,
        y: Fixed,
        w: Fixed,
        h: Fixed,
    ) -> Option<(Fixed, Fixed, Fixed, Fixed)> {
        for b in 0..self.alive.len() {
            if b == mover
                || !self.alive[b]
                || !self.has(b, C_BLOCKER | C_TRANSFORM | C_COLLIDER)
                || !self.is_active(b)
            {
                continue;
            }
            if !self.same_room(mover, b) {
                continue;
            }
            let bx = self.transform[b].x;
            let by = self.transform[b].y;
            let bw = self.collider[b].w;
            let bh = self.collider[b].h;
            if x < bx + bw && bx < x + w && y < by + bh && by < y + h {
                return Some((bx, by, bw, bh));
            }
        }
        None
    }

    /// Give a top-down entity native chase-the-player AI (no per-frame tish `tick`). `stride` = the
    /// character sheet's columns per direction row (5 for idle+4walk), or 0 for a non-directional
    /// creature that just loops frames `0..flap`.
    /// Make `e` shoot every `interval` frames at `speed` px/frame, using the bullet style that is
    /// in force RIGHT NOW (so the caller sets the style once, at spawn, and never again).
    fn set_shooter(&mut self, e: i32, interval: i32, speed: Fixed, aimed: bool) {
        let style = self.bullet_style;
        if let Some(s) = self.slot_of(e) {
            // Stagger the first shot so a room of them does not volley in lockstep.
            self.rng = self.rng.wrapping_mul(1664525).wrapping_add(1013904223);
            let jitter = ((self.rng >> 16) as i32).rem_euclid(interval.max(1));
            self.shooter[s] = Shooter {
                interval: interval.max(1),
                timer: jitter,
                speed,
                aimed,
                style,
            };
            self.mask[s] |= C_SHOOTER;
            self.used |= C_SHOOTER;
        }
    }

    /// Native shooter AI: no per-frame tish call, and it obeys the same room cutoff and stun the
    /// other AI systems do — a stunned enemy must not keep firing.
    ///
    /// Shots are collected first and fired after the scan, because firing spawns entities and that
    /// would reallocate the very columns being walked.
    fn shooter_system(&mut self) {
        let Some(target) = self.camera_target else {
            return;
        };
        let Some(ts) = self.slot_of(target) else {
            return;
        };
        let (ptx, pty) = self.center_of(ts);
        // A fixed-size buffer rather than a Vec: this runs every frame and must not allocate.
        // Anything past the cap simply fires on its next tick.
        let mut shots: [(Fixed, Fixed, Fixed, Fixed, BulletStyle); 8] = [(
            Fixed::from_raw(0),
            Fixed::from_raw(0),
            Fixed::from_raw(0),
            Fixed::from_raw(0),
            BulletStyle::default(),
        ); 8];
        let mut n = 0usize;
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_SHOOTER | C_TRANSFORM) || !self.is_active(s) {
                continue;
            }
            if self.stun[s] > 0 || !self.same_room(s, ts) {
                continue;
            }
            self.shooter[s].timer -= 1;
            if self.shooter[s].timer > 0 {
                continue;
            }
            let sh = self.shooter[s];
            self.shooter[s].timer = sh.interval;
            if n == shots.len() {
                continue;
            }
            // From the shooter's box CENTRE, not its transform. `fire_bullet` reads the point it is
            // given as the bullet's centre, so passing the top-left put every shot half a box up and
            // left of the enemy — usually inside the neighbouring tile, where the projectile's
            // solid-tile check retired it on the frame it spawned. 20 of every 21 shots died there.
            let (cx, cy) = self.center_of(s);
            // Unit heading: at the player for an aimed shooter, along its facing otherwise (which is
            // what makes a spitter something you dodge by staying out of its lane).
            let (ux, uy) = if sh.aimed {
                let (dx, dy) = (ptx - cx, pty - cy);
                let len = (dx * dx + dy * dy).sqrt();
                if len.to_raw() == 0 {
                    (Fixed::from_raw(0), Fixed::from_raw(256))
                } else {
                    (dx / len, dy / len)
                }
            } else {
                match self.topdown[s].facing {
                    1 => (Fixed::from_raw(0), Fixed::from_raw(-256)),
                    2 => (Fixed::from_raw(-256), Fixed::from_raw(0)),
                    3 => (Fixed::from_raw(256), Fixed::from_raw(0)),
                    _ => (Fixed::from_raw(0), Fixed::from_raw(256)),
                }
            };
            // Start it clear of the shooter's own body for the same reason.
            let muzzle = Fixed::from_raw(12 * 256);
            shots[n] = (
                cx + ux * muzzle,
                cy + uy * muzzle,
                ux * sh.speed,
                uy * sh.speed,
                sh.style,
            );
            n += 1;
        }
        for &(x, y, vx, vy, style) in shots.iter().take(n) {
            let save = self.bullet_style;
            self.bullet_style = style;
            self.fire_bullet(x, y, vx, vy);
            self.bullet_style = save;
        }
    }

    /// The centre of slot `s`'s collider box in world px (its transform if it has no collider).
    fn center_of(&self, s: usize) -> (Fixed, Fixed) {
        if self.has(s, C_COLLIDER) {
            (
                self.transform[s].x + self.collider[s].w / 2,
                self.transform[s].y + self.collider[s].h / 2,
            )
        } else {
            (self.transform[s].x, self.transform[s].y)
        }
    }

    /// Give `e` a charge: while it lines up with the player on an axis (within `band` px on the
    /// other one) it bolts along that axis at `speed` px/frame instead of doing whatever it
    /// normally does.
    fn set_charger(&mut self, e: i32, speed: i32, band: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TOPDOWN)) {
            self.charger[s] = Charger {
                speed: speed.max(1) * 256,
                base: self.topdown[s].speed,
                band: band.max(1),
                active: false,
            };
            self.mask[s] |= C_CHARGER;
            self.used |= C_CHARGER;
        }
    }

    /// Runs AFTER the hopper/chase systems so a charge overrides the ordinary move intent, and
    /// hands the speed back the moment the line is broken.
    fn charger_system(&mut self) {
        let Some(target) = self.camera_target else {
            return;
        };
        let Some(ts) = self.slot_of(target) else {
            return;
        };
        let (px, py) = (self.transform[ts].x.floor(), self.transform[ts].y.floor());
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_CHARGER | C_TOPDOWN) || !self.is_active(s) {
                continue;
            }
            let c = self.charger[s];
            let lined = if self.stun[s] > 0 || !self.same_room(s, ts) {
                None
            } else {
                let dx = px - self.transform[s].x.floor();
                let dy = py - self.transform[s].y.floor();
                if dy.abs() <= c.band && dx != 0 {
                    Some((dx.signum(), 0))
                } else if dx.abs() <= c.band && dy != 0 {
                    Some((0, dy.signum()))
                } else {
                    None
                }
            };
            match lined {
                Some((mx, my)) => {
                    if !self.charger[s].active {
                        self.charger[s].active = true;
                        self.charger[s].base = self.topdown[s].speed;
                        self.topdown[s].speed = c.speed;
                    }
                    self.topdown[s].dx = mx;
                    self.topdown[s].dy = my;
                    self.topdown[s].facing = if mx < 0 {
                        2
                    } else if mx > 0 {
                        3
                    } else if my < 0 {
                        1
                    } else {
                        0
                    };
                }
                None => {
                    if self.charger[s].active {
                        self.charger[s].active = false;
                        self.topdown[s].speed = c.base;
                    }
                }
            }
        }
    }

    /// `set_weakness(e, mask)` — damage-type vulnerability mask (`DMG_*` bits). 0 = hurt by
    /// everything; non-zero ALLOWS only matching weapon kinds. Complement of `set_immunity`.
    fn set_weakness(&mut self, e: i32, mask: i32) {
        if let Some(s) = self.slot_of(e) {
            self.weak[s] = mask;
        }
    }

    /// `set_grabber(e, targetTag)` — on overlap with a tagged target, briefly stun it.
    fn set_grabber(&mut self, e: i32, target_tag: i32) {
        if let Some(s) = self.slot_of(e) {
            self.grabber[s] = Grabber { target_tag };
            self.mask[s] |= C_GRABBER;
            self.used |= C_GRABBER;
        }
    }

    /// Stun any overlapping tagged target for a short grab window.
    fn grabber_system(&mut self) {
        const GRAB_STUN: i32 = 24;
        let n = self.alive.len();
        let mut stuns: Vec<(usize, i32)> = Vec::new();
        for s in 0..n {
            if !self.alive[s]
                || !self.has(s, C_GRABBER | C_TRANSFORM | C_COLLIDER)
                || !self.is_active(s)
            {
                continue;
            }
            let want = self.grabber[s].target_tag;
            for t in 0..n {
                if t == s || !self.alive[t] || self.tag[t] != want {
                    continue;
                }
                if !self.has(t, C_TRANSFORM | C_COLLIDER) || !self.slots_overlap(s, t) {
                    continue;
                }
                if !self.same_room(s, t) {
                    continue;
                }
                stuns.push((t, GRAB_STUN));
            }
        }
        for (t, frames) in stuns {
            self.stun[t] = self.stun[t].max(frames);
        }
    }

    /// `set_trap(e)` — blade-trap: inert until the player shares a row/col, then dash.
    fn set_trap(&mut self, e: i32) {
        if let Some(s) = self
            .slot_of(e)
            .filter(|&s| self.has(s, C_TOPDOWN | C_TRANSFORM))
        {
            let base = self.topdown[s].speed;
            self.trap[s] = Trap {
                home_x: self.transform[s].x,
                home_y: self.transform[s].y,
                speed: 4 * 256,
                base,
                band: 8,
                active: false,
            };
            self.mask[s] |= C_TRAP;
            self.used |= C_TRAP;
        }
    }

    /// Charger-like dash while lined up with the player; otherwise hold still at home speed.
    fn trap_system(&mut self) {
        let Some(target) = self.camera_target else {
            return;
        };
        let Some(ts) = self.slot_of(target) else {
            return;
        };
        let (px, py) = (self.transform[ts].x.floor(), self.transform[ts].y.floor());
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_TRAP | C_TOPDOWN) || !self.is_active(s) {
                continue;
            }
            let c = self.trap[s];
            let lined = if self.stun[s] > 0 || !self.same_room(s, ts) {
                None
            } else {
                let dx = px - self.transform[s].x.floor();
                let dy = py - self.transform[s].y.floor();
                if dy.abs() <= c.band && dx != 0 {
                    Some((dx.signum(), 0))
                } else if dx.abs() <= c.band && dy != 0 {
                    Some((0, dy.signum()))
                } else {
                    None
                }
            };
            match lined {
                Some((mx, my)) => {
                    if !self.trap[s].active {
                        self.trap[s].active = true;
                        self.trap[s].base = self.topdown[s].speed;
                        self.topdown[s].speed = c.speed;
                    }
                    self.topdown[s].dx = mx;
                    self.topdown[s].dy = my;
                    self.topdown[s].facing = if mx < 0 {
                        2
                    } else if mx > 0 {
                        3
                    } else if my < 0 {
                        1
                    } else {
                        0
                    };
                }
                None => {
                    if self.trap[s].active {
                        self.trap[s].active = false;
                        self.topdown[s].speed = c.base;
                    }
                    // Drift back to the home corner when the line breaks.
                    let dx = c.home_x.floor() - self.transform[s].x.floor();
                    let dy = c.home_y.floor() - self.transform[s].y.floor();
                    if dx.abs() <= 1 && dy.abs() <= 1 {
                        self.transform[s].x = c.home_x;
                        self.transform[s].y = c.home_y;
                        self.topdown[s].dx = 0;
                        self.topdown[s].dy = 0;
                    } else if dx.abs() >= dy.abs() {
                        self.topdown[s].dx = dx.signum();
                        self.topdown[s].dy = 0;
                    } else {
                        self.topdown[s].dx = 0;
                        self.topdown[s].dy = dy.signum();
                    }
                }
            }
        }
    }

    fn set_follow(&mut self, e: i32, kind: u8, parent: i32, radius: i32) {
        let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TRANSFORM)) else {
            return;
        };
        let (ox, oy) = match self.slot_of(parent).filter(|&p| self.has(p, C_TRANSFORM)) {
            Some(p) => (
                self.transform[s].x - self.transform[p].x,
                self.transform[s].y - self.transform[p].y,
            ),
            None => (Fixed::from_raw(0), Fixed::from_raw(0)),
        };
        self.follow[s] = Follow {
            kind,
            parent,
            radius: radius.max(0),
            ox,
            oy,
            angle: 0,
        };
        self.mask[s] |= C_FOLLOW;
        self.used |= C_FOLLOW;
    }

    /// Glue `C_FOLLOW` entities to their parent each frame (offset follow / orbit).
    fn follow_system(&mut self) {
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask[s] & C_SLEEP != 0 || !self.has(s, C_FOLLOW | C_TRANSFORM)
            {
                continue;
            }
            let f = self.follow[s];
            let Some(p) = self
                .slot_of(f.parent)
                .filter(|&p| self.alive[p] && self.has(p, C_TRANSFORM))
            else {
                continue;
            };
            let (px, py) = (self.transform[p].x, self.transform[p].y);
            match f.kind {
                FOLLOW_ORBIT => {
                    let rev = Fixed::from_raw(f.angle & 255);
                    let r = Fixed::from_raw(f.radius * 256);
                    self.transform[s].x = px + rev.cos() * r;
                    self.transform[s].y = py + rev.sin() * r;
                    self.follow[s].angle = f.angle.wrapping_add(2);
                }
                _ => {
                    // part + train: simple offset follow
                    self.transform[s].x = px + f.ox;
                    self.transform[s].y = py + f.oy;
                }
            }
        }
    }

    /// `set_boomerang(e, returnFrames)` — after N frames, reverse Body velocity toward the owner
    /// (camera target at configure time). A return-mover companion to `set_mover` / `set_lifetime`.
    fn set_boomerang(&mut self, e: i32, return_frames: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_BODY)) {
            let owner = self.camera_target.unwrap_or(-1);
            self.boomerang[s] = Boomerang {
                timer: return_frames.max(1),
                owner,
                returning: false,
            };
            self.mask[s] |= C_BOOMERANG;
            self.used |= C_BOOMERANG;
        }
    }

    fn boomerang_system(&mut self) {
        // Catches collected first, despawned after the scan (despawning mid-walk would mutate the
        // very columns being iterated). A tiny fixed buffer: more than 4 live boomerangs is not a
        // thing a top-down action-RPG frame does.
        let mut caught: [i32; 4] = [-1; 4];
        let mut nc = 0usize;
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_BOOMERANG | C_BODY) || !self.is_active(s) {
                continue;
            }
            if !self.boomerang[s].returning {
                self.boomerang[s].timer -= 1;
                if self.boomerang[s].timer > 0 {
                    continue;
                }
                self.boomerang[s].returning = true;
                // Flip the outbound velocity as a first approximation; the seek below retargets.
                self.body[s].vx = -self.body[s].vx;
                self.body[s].vy = -self.body[s].vy;
            }
            let owner = self.boomerang[s].owner;
            let Some(os) = self
                .slot_of(owner)
                .filter(|&os| self.alive[os] && self.has(os, C_TRANSFORM))
            else {
                continue;
            };
            if !self.has(s, C_TRANSFORM) {
                continue;
            }
            let (ox, oy) = (self.transform[os].x, self.transform[os].y);
            let (ex, ey) = (self.transform[s].x, self.transform[s].y);
            let (dx, dy) = (ox - ex, oy - ey);
            // The catch: a returning boomerang within reach of its owner is caught — despawned,
            // and the catch reported to `boomerang_caught()`. Before this the return contract was
            // unfinished: the boomerang orbited its owner until a lifetime retired it.
            if dx.to_raw().abs() < 10 * 256 && dy.to_raw().abs() < 10 * 256 {
                if nc < caught.len() {
                    caught[nc] = encode(s as u32, self.gen[s]);
                    nc += 1;
                    self.boomer_catches += 1;
                }
                continue;
            }
            let speed = {
                let vx = self.body[s].vx.to_raw();
                let vy = self.body[s].vy.to_raw();
                // Cheap length stand-in: max-norm, good enough to keep return speed.
                vx.abs().max(vy.abs()).max(256)
            };
            let adx = dx.to_raw().abs().max(1);
            let ady = dy.to_raw().abs().max(1);
            let dom = adx.max(ady);
            self.body[s].vx = Fixed::from_raw((dx.to_raw() * speed) / dom);
            self.body[s].vy = Fixed::from_raw((dy.to_raw() * speed) / dom);
        }
        for &id in caught.iter().take(nc) {
            self.despawn(id);
        }
    }

    /// Put a decoy in the world. Enemies within `radius` px steer at it instead of the player for
    /// `frames`. Passing a dead/absent entity, or letting the timer run out, clears it.
    fn set_lure(&mut self, e: i32, radius: i32, frames: i32) {
        self.lure = (e, radius, frames);
    }

    /// The lure's position this frame, if one is live. Also ages it out — called once per frame from
    /// the AI phase, before hopper/chase read it.
    fn lure_point(&mut self) -> Option<(i32, i32)> {
        let (e, _r, f) = self.lure;
        if e < 0 || f <= 0 {
            return None;
        }
        self.lure.2 = f - 1;
        let s = self.slot_of(e)?;
        if !self.alive[s] || !self.has(s, C_TRANSFORM) {
            self.lure = (-1, 0, 0);
            return None;
        }
        Some((self.transform[s].x.floor(), self.transform[s].y.floor()))
    }

    /// Is slot `s` close enough to the lure to be distracted by it?
    fn lured_to(&self, s: usize, lp: Option<(i32, i32)>) -> Option<(i32, i32)> {
        let (lx, ly) = lp?;
        if self.lure.0 >= 0 && self.slot_of(self.lure.0) == Some(s) {
            return None; // the bait does not chase itself
        }
        let d = (lx - self.transform[s].x.floor()).abs() + (ly - self.transform[s].y.floor()).abs();
        if d <= self.lure.1 {
            Some((lx, ly))
        } else {
            None
        }
    }

    // ── Native enemy AI (`nai` column, `M2_NAI`) ─────────────────────────────
    // Four state machines from the NES-era top-down bestiary that the existing AI natives could
    // not express, all O(1) per entity per frame: the burrower (submerge cycle), the hovering
    // drifter (spin-up flight, hittable only at rest), the teleporting caster (teleport-flicker +
    // aimed cast), and the bouncer (parabolic hops).

    /// Advance the world RNG and return 16 usable bits (same LCG `set_shooter` staggers with).
    fn rnd16(&mut self) -> u32 {
        self.rng = self.rng.wrapping_mul(1664525).wrapping_add(1013904223);
        self.rng >> 16
    }

    /// Store heading `(mx,my)` on both the nai record and the top-down mover, updating facing the
    /// same way `topdown_move` does (horizontal wins) so `set_dir_anim` composes.
    fn nai_head(&mut self, s: usize, mx: i32, my: i32) {
        self.nai[s].dx = mx;
        self.nai[s].dy = my;
        self.topdown[s].dx = mx;
        self.topdown[s].dy = my;
        if mx < 0 {
            self.topdown[s].facing = 2;
        } else if mx > 0 {
            self.topdown[s].facing = 3;
        } else if my < 0 {
            self.topdown[s].facing = 1;
        } else if my > 0 {
            self.topdown[s].facing = 0;
        }
    }

    /// Pick a random 8-direction heading.
    fn nai_pick_dir8(&mut self, s: usize) {
        const DIRS: [(i32, i32); 8] = [
            (0, 1),
            (0, -1),
            (1, 0),
            (-1, 0),
            (1, 1),
            (1, -1),
            (-1, 1),
            (-1, -1),
        ];
        let (mx, my) = DIRS[(self.rnd16() & 7) as usize];
        self.nai_head(s, mx, my);
    }

    /// Install a nai record. Every kind starts in state 0 with `timer = a` (plus a random stagger
    /// so a room of them does not cycle in lockstep — same rationale as `set_shooter`'s jitter).
    fn nai_install(&mut self, e: i32, kind: u8, a: i32, b: i32, speed: i32) -> Option<usize> {
        let s = self.slot_of(e)?;
        let a = a.max(1);
        let jitter = (self.rnd16() as i32).rem_euclid(a);
        self.nai[s] = Nai {
            kind,
            state: 0,
            timer: a - (jitter >> 1).min(a - 1),
            a,
            b: b.max(1),
            speed,
            ..Nai::default()
        };
        self.mask2[s] |= M2_NAI;
        self.used2 |= M2_NAI;
        Some(s)
    }

    /// `set_ambusher(e, hideFrames, surfaceFrames, speedQ8)` — burrower ground-emergence cycle.
    /// Hidden (`hideFrames`): intangible + invisible, drifting toward the player underground at
    /// `speedQ8` (Q8 px/frame) on the dominant axis. Surfaced (`surfaceFrames`): visible,
    /// vulnerable, stationary — compose with `set_shooter` for a surfacing shooter (the shooter
    /// fires whenever it can; while hidden the entity deals/takes nothing anyway, and its shots
    /// pause because hidden is also a fine time for the interval to elapse unanswered). Needs `C_TOPDOWN`.
    fn set_ambusher(&mut self, e: i32, hide: i32, surface: i32, speed: i32) {
        if let Some(s) = self.nai_install(e, 1, hide, surface, speed) {
            if !self.has(s, C_TOPDOWN) {
                self.mask2[s] &= !M2_NAI;
                return;
            }
            self.mask2[s] |= M2_PHASED | M2_HIDDEN;
            self.used2 |= M2_PHASED | M2_HIDDEN;
        }
    }

    /// `set_drifter(e, restFrames, flyFrames, speedQ8)` — hovering-drifter floating wander. Rest
    /// (`restFrames`): stationary and VULNERABLE — the only window the drifter can be hit. Fly
    /// (`flyFrames`): invulnerable (`M2_PHASED`, still tangible — contact still hurts), speed
    /// ramping up over the first quarter and down over the last (the spin-up/spin-down), heading
    /// re-picked every 32 frames. Needs `C_TOPDOWN`. The damage path reads the flag natively;
    /// game code can too, via `entity_phased`.
    fn set_drifter(&mut self, e: i32, rest: i32, fly: i32, speed: i32) {
        if let Some(s) = self.nai_install(e, 2, rest, fly.max(4), speed) {
            if !self.has(s, C_TOPDOWN) {
                self.mask2[s] &= !M2_NAI;
                return;
            }
            // Spin-up increment per frame, computed ONCE here (a divide costs nothing at
            // configure time; on the per-frame path it would be a software call — no divide
            // instruction on the ARM7TDMI).
            self.nai[s].step = (speed * 4 / fly.max(4)).max(1);
        }
    }

    /// `set_flicker_caster(e, hideFrames, visFrames, shotSpeed)` — teleporting caster. Hidden
    /// (`hideFrames`): gone (intangible + invisible). On appearing it teleports to a random
    /// walkable tile near the player (up to 8 tries against the solid grid; stays put if all
    /// fail), flickers in for 12 frames (blinking, still unhittable), then stands vulnerable for
    /// the rest of `visFrames`, casting ONE aimed shot at the window's midpoint using the bullet
    /// style in force at configure time (same capture contract as `set_shooter`). Needs
    /// `C_TRANSFORM`.
    fn set_flicker_caster(&mut self, e: i32, hide: i32, vis: i32, shot_speed: Fixed) {
        let style = self.bullet_style;
        if let Some(s) = self.nai_install(e, 3, hide, vis.max(16), shot_speed.to_raw()) {
            if !self.has(s, C_TRANSFORM) {
                self.mask2[s] &= !M2_NAI;
                return;
            }
            self.nai[s].style = style;
            self.mask2[s] |= M2_PHASED | M2_HIDDEN;
            self.used2 |= M2_PHASED | M2_HIDDEN;
        }
    }

    /// `set_bouncer(e, restFrames, hopFrames, speedQ8)` — parabolic bouncing movement. Rest
    /// (`restFrames`): grounded pause. Hop (`hopFrames`): moves at `speedQ8` on a heading that is
    /// 50% at-the-player (dominant axis), 50% random 8-dir, with a parabolic arc drawn by writing
    /// the sprite's `oy` (the entity OWNS its sprite offset while a bouncer — the base captured
    /// here is restored on landing). Collision stays the top-down mover's. Needs `C_TOPDOWN`.
    /// `set_ricochet(e, speedQ8)` — a diagonal that reflects off whatever stops it: tiles,
    /// blockers, the room's edge. Movement integrates through the top-down mover, so collision is
    /// what turns "this axis stopped" into the bounce — the system just watches the transform and
    /// flips the heading on the axis that failed to advance. a downstream game's NINE-BOUNCE bug ("frozen
    /// mid-leap nine thousand years ago and still going") is the reason it exists: it shipped on
    /// the shmup `set_mover`, whose Body integration ignores tiles entirely, and sailed through
    /// its room's walls off the map. Needs `C_TOPDOWN`.
    fn set_ricochet(&mut self, e: i32, speed: i32) {
        if let Some(s) = self.nai_install(e, 5, 1, 1, speed) {
            if !self.has(s, C_TOPDOWN) {
                self.mask2[s] &= !M2_NAI;
                return;
            }
            // Heading from the slot index, so a room of these fans out instead of marching in step.
            self.nai[s].dx = if s & 1 == 0 { 1 } else { -1 };
            self.nai[s].dy = if s & 2 == 0 { 1 } else { -1 };
            // Last-position scratch (a/b), primed OFF the spawn point so frame one cannot read as
            // a stall and flip the heading before the creature has moved at all.
            self.nai[s].a = self.transform[s].x.to_raw() - 1;
            self.nai[s].b = self.transform[s].y.to_raw() - 1;
        }
    }

    fn set_bouncer(&mut self, e: i32, rest: i32, hop: i32, speed: i32) {
        if let Some(s) = self.nai_install(e, 4, rest, hop.max(8), speed) {
            if !self.has(s, C_TOPDOWN) {
                self.mask2[s] &= !M2_NAI;
                return;
            }
            self.nai[s].aux = self.sprite[s].oy;
        }
    }

    /// The nai state machines, one pass per frame (pipeline phase 2, just before
    /// `topdown_system` integrates the intent they set). Stun freezes a machine in place — a
    /// stunned caster does not finish vanishing. Shots are deferred exactly like
    /// `shooter_system`'s, because firing spawns entities into the columns being walked.
    /// Continuous tile-aligned walking with perpendicular, target-seeking turns.
    ///
    /// A faithful port of the NES original's wanderer target-player routine. The shape
    /// that matters, and that no existing native expressed:
    ///
    ///   * it NEVER STOPS. Intent is re-asserted from `facing` every single frame, so the walker
    ///     is always moving. (The reference only halts for a shooting windup, which is the
    ///     shooter's business, not the walker's.)
    ///   * it may only TURN ON A TILE BOUNDARY. The original bails out unless the actor's
    ///     tile offset is 0. Positions here are world Fixed and actors spawn tile-aligned,
    ///     so testing the floored position against the 16 px grid is the same predicate.
    ///   * its turns are NOT RANDOM. Only the TIMING is. `TurnX`/`TurnY` always face the target;
    ///     `TurnIfTime` always switches to the perpendicular axis.
    ///
    /// ⚠️ `topdown_system` CONSUMES dx/dy every frame. Setting intent once makes the entity travel
    /// a single pixel and stop — the exact bug documented on `Hopper.dir_x`. Hence the
    /// unconditional re-assert at the bottom of the loop.
    fn wanderer_system(&mut self) {
        let Some(target) = self.camera_target else {
            return;
        };
        let Some(ts) = self.slot_of(target) else {
            return;
        };
        let tx = self.transform[ts].x.floor();
        let ty = self.transform[ts].y.floor();

        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask2[s] & M2_WANDERER == 0 || !self.has(s, C_TOPDOWN) {
                continue;
            }

            // ⚠️⚠️ CONFINE FIRST, BEFORE ANY CULLING GATE — ORDER IS THE WHOLE BUG.
            //
            // This was below `is_active`, and could therefore never fire. `camera_focus` adds half
            // the collider, so a walker at transform x 1792 focuses at 1798 and is not yet "at the
            // edge"; by the time its focus reaches the boundary its transform is ~1786, which is
            // already off-screen, so `is_active` culled it and `continue`d before the edge test ran.
            // The walker then sat outside the room — skipped by `same_room` too — motionless for
            // 268 frames. Confinement has to run for a walker the camera has given up on, which is
            // exactly the one that needs it.
            //
            // Clamping, not merely steering: the original stops a monster at the room margin
            // rather than trusting it to turn in time.
            let mut at_edge = false;
            if self.room_cam.enabled {
                let (rw, rh) = (self.room_cam.room_w * TILE, self.room_cam.room_h * TILE);
                let (left, top) = (self.wanderer[s].home_rx * rw, self.wanderer[s].home_ry * rh);
                let (cw, ch) = if self.has(s, C_COLLIDER) {
                    (self.collider[s].w.floor(), self.collider[s].h.floor())
                } else {
                    (0, 0)
                };
                let (right, bottom) = (left + rw - cw, top + rh - ch);
                let (x, y) = (self.transform[s].x.floor(), self.transform[s].y.floor());
                if x < left {
                    self.transform[s].x = Fixed::from_raw(left << 8);
                    at_edge = true;
                } else if x > right {
                    self.transform[s].x = Fixed::from_raw(right << 8);
                    at_edge = true;
                }
                if y < top {
                    self.transform[s].y = Fixed::from_raw(top << 8);
                    at_edge = true;
                } else if y > bottom {
                    self.transform[s].y = Fixed::from_raw(bottom << 8);
                    at_edge = true;
                }
            }

            if !self.is_active(s) {
                continue;
            }
            if self.stun[s] > 0 {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }
            if !self.same_room(s, ts) {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }
            // The turn timer ticks even while the walker cannot act (as in the original).
            if self.wanderer[s].turn_timer > 0 {
                self.wanderer[s].turn_timer -= 1;
            }
            // Knockback owns the entity while it lasts: the original returns early during a
            // shove. Steering during a shove fights the knock and cancels it.
            if self.topdown[s].knock > 0 {
                continue;
            }

            let px = self.transform[s].x.floor();
            let py = self.transform[s].y.floor();
            let aligned = (px & 0xF) == 0 && (py & 0xF) == 0;

            // ⚠️⚠️ BLOCKED IS ALSO A DECISION POINT, OR THE WALKER WEDGES FOREVER.
            //
            // Deriving "on a tile boundary" from the world position is only equivalent to the
            // reference's `tileOffset` while the walker is actually moving. Walk into a wall
            // mid-tile and the position stops changing at, say, x = 1706.5 — `aligned` is false and
            // can never become true again, so the walker never re-steers and stands pushing into
            // the wall for the rest of the run. Observed exactly that: a walker walked past its
            // target to x 1706.5 and was still facing left into a wall 400 frames later.
            //
            // The reference does not have this failure because `tileOffset` is its own counter that
            // collision resolution maintains, not a reading of the position. Treating "did not move
            // although it wanted to" as a decision point restores the intent — the walker gets to
            // pick a new direction the moment it is stuck — without inventing a second notion of
            // alignment.
            // ⚠️ COMPARE THE RAW FIXED POSITION, NOT THE FLOORED ONE. A walker at StdSpeed covers
            // 0.5 px/frame, so its FLOORED position is unchanged on every other frame — comparing
            // floors reports "blocked" half the time, which cleared the turn timer and let it
            // re-steer mid-tile. That produced 202 off-grid turns, the first only 7 frames in.
            let (rx_now, ry_now) = (self.transform[s].x.to_raw(), self.transform[s].y.to_raw());
            let blocked = rx_now == self.wanderer[s].last_x && ry_now == self.wanderer[s].last_y;
            self.wanderer[s].last_x = rx_now;
            self.wanderer[s].last_y = ry_now;

            // ⚠️⚠️ CONFINE THE WALKER TO ITS ROOM, OR IT WALKS OUT AND FREEZES FOR GOOD.
            //
            // A hopper moves in 16 px lurches and rarely escapes; a wanderer walks continuously and
            // WILL leave. The moment it does, `same_room` above is false, its intent is zeroed and
            // it is skipped forever — a live entity standing off-screen for the rest of the run.
            // Measured exactly that: a walker spawned at world x 1856 inside the room spanning
            // 1792..2047, walked left to 1706.5, and never moved again from frame 347 to 599.
            // The original confines monsters with an explicit world-margin check.
            // Being stuck IS the trigger to choose again. Without clearing the timer the walker
            // still could not act: `wanderer_turn_if_time` returns early while `turn_timer` is
            // non-zero, and that timer is re-armed with up to 255 frames on every turn — so a
            // blocked walker stood motionless for 252 frames waiting it out.
            let stuck = blocked || at_edge;
            if stuck {
                self.wanderer[s].turn_timer = 0;
            }

            if self.topdown[s].speed != 0 && (aligned || stuck) {
                let r = (self.rnd16() & 0xFF) as i32;
                if r > self.wanderer[s].turn_rate {
                    self.wanderer_turn_if_time(s, px, py, tx, ty);
                } else if (px - tx).abs() < 9 {
                    // already sharing the target's column — line up vertically
                    self.wanderer_turn_y(s, py, ty);
                } else if (py - ty).abs() < 9 {
                    self.wanderer_turn_x(s, px, tx);
                } else {
                    self.wanderer_turn_if_time(s, px, py, tx, ty);
                }
            }

            // moving = facing, as in the original.
            let (dx, dy) = match self.topdown[s].facing {
                1 => (0, -1),
                2 => (-1, 0),
                3 => (1, 0),
                _ => (0, 1),
            };
            self.topdown[s].dx = dx;
            self.topdown[s].dy = dy;
        }
    }

    /// Turn-if-time rule: drift onto the perpendicular axis, but only
    /// once the turn timer has expired.
    fn wanderer_turn_if_time(&mut self, s: usize, px: i32, py: i32, tx: i32, ty: i32) {
        self.wanderer[s].want_shoot = 0;
        if self.wanderer[s].turn_timer != 0 {
            return;
        }
        // facing 0=down 1=up are the vertical ones, so turn horizontally, and vice versa.
        if self.topdown[s].facing <= 1 {
            self.wanderer_turn_x(s, px, tx);
        } else {
            self.wanderer_turn_y(s, py, ty);
        }
    }

    /// Horizontal turn. Faces the target horizontally and re-arms the
    /// timer with a fresh random interval.
    fn wanderer_turn_x(&mut self, s: usize, px: i32, tx: i32) {
        self.topdown[s].facing = if tx < px { 2 } else { 3 };
        let t = (self.rnd16() & 0xFF) as i32;
        self.wanderer[s].turn_timer = t;
        self.wanderer[s].want_shoot = 1;
    }

    /// Vertical turn (mirror of the horizontal one).
    fn wanderer_turn_y(&mut self, s: usize, py: i32, ty: i32) {
        self.topdown[s].facing = if ty < py { 1 } else { 0 };
        let t = (self.rnd16() & 0xFF) as i32;
        self.wanderer[s].turn_timer = t;
        self.wanderer[s].want_shoot = 1;
    }

    /// Has this walker just lined up on the target? A shooting system reads and clears it.
    fn wanderer_wants_shot(&self, s: usize) -> bool {
        self.wanderer[s].want_shoot != 0
    }

    fn nai_system(&mut self) {
        let ts = self.camera_target.and_then(|t| self.slot_of(t));
        let mut shots: [(Fixed, Fixed, Fixed, Fixed, BulletStyle); 4] = [(
            Fixed::from_raw(0),
            Fixed::from_raw(0),
            Fixed::from_raw(0),
            Fixed::from_raw(0),
            BulletStyle::default(),
        ); 4];
        let mut nsh = 0usize;
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask2[s] & M2_NAI == 0 || !self.is_active(s) {
                continue;
            }
            if self.stun[s] > 0 {
                continue;
            }
            let near = ts.filter(|&t| self.same_room(s, t));
            let z = self.nai[s];
            match z.kind {
                // ── Ambusher: hidden-approach → surface → repeat ─────────────
                1 => {
                    if z.state == 0 {
                        if let Some(t) = near {
                            let dx = self.transform[t].x.floor() - self.transform[s].x.floor();
                            let dy = self.transform[t].y.floor() - self.transform[s].y.floor();
                            let (mx, my) = if dx.abs() >= dy.abs() {
                                (dx.signum(), 0)
                            } else {
                                (0, dy.signum())
                            };
                            self.topdown[s].speed = z.speed;
                            self.nai_head(s, mx, my);
                        } else {
                            self.nai_head(s, 0, 0);
                        }
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            self.nai[s].state = 1;
                            self.nai[s].timer = z.b;
                            self.mask2[s] &= !(M2_PHASED | M2_HIDDEN);
                            self.nai_head(s, 0, 0);
                        }
                    } else {
                        self.topdown[s].dx = 0;
                        self.topdown[s].dy = 0;
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            self.nai[s].state = 0;
                            self.nai[s].timer = z.a;
                            self.mask2[s] |= M2_PHASED | M2_HIDDEN;
                            self.used2 |= M2_PHASED | M2_HIDDEN;
                        }
                    }
                }
                // ── Drifter: rest (vulnerable) → spin-up flight (phased) ─────
                2 => {
                    if z.state == 0 {
                        self.topdown[s].dx = 0;
                        self.topdown[s].dy = 0;
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            self.nai[s].state = 1;
                            self.nai[s].timer = z.b;
                            self.nai[s].aux = z.step; // current ramped speed
                            self.mask2[s] |= M2_PHASED;
                            self.used2 |= M2_PHASED;
                            self.nai_pick_dir8(s);
                        }
                    } else {
                        let quarter = (z.b >> 2).max(1);
                        let elapsed = z.b - z.timer;
                        let cur = if elapsed < quarter {
                            (z.aux + z.step).min(z.speed)
                        } else if z.timer < quarter {
                            (z.aux - z.step).max(z.step)
                        } else {
                            z.speed
                        };
                        self.nai[s].aux = cur;
                        self.topdown[s].speed = cur;
                        if z.timer & 31 == 0 {
                            self.nai_pick_dir8(s);
                        } else {
                            self.topdown[s].dx = z.dx;
                            self.topdown[s].dy = z.dy;
                        }
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            self.nai[s].state = 0;
                            self.nai[s].timer = z.a;
                            self.mask2[s] &= !M2_PHASED;
                            self.nai_head(s, 0, 0);
                        }
                    }
                }
                // ── Flicker caster: vanish → teleport near player → flicker in → cast ──
                3 => {
                    if z.state == 0 {
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            if let Some(t) = near {
                                let pc = self.transform[t].x.floor() / TILE;
                                let pr = self.transform[t].y.floor() / TILE;
                                for _ in 0..8 {
                                    let r = self.rnd16();
                                    let dc = ((r & 7) as i32) - 3;
                                    let dr = (((r >> 3) & 7) as i32) - 3;
                                    if dc.abs() + dr.abs() < 2 {
                                        continue; // never right on top of the player
                                    }
                                    let (c, rw) = (pc + dc, pr + dr);
                                    if !self.is_solid(c, rw) {
                                        self.transform[s].x = Fixed::from_raw(c * TILE * 256);
                                        self.transform[s].y = Fixed::from_raw(rw * TILE * 256);
                                        break;
                                    }
                                }
                            }
                            self.nai[s].state = 1;
                            self.nai[s].timer = z.b;
                            self.nai[s].aux = 0; // not yet cast this window
                        }
                    } else {
                        let elapsed = z.b - z.timer;
                        if elapsed < 12 {
                            // Flicker-in: blinking and still unhittable.
                            self.mask2[s] |= M2_PHASED;
                            self.used2 |= M2_PHASED;
                            if elapsed & 2 == 0 {
                                self.mask2[s] |= M2_HIDDEN;
                                self.used2 |= M2_HIDDEN;
                            } else {
                                self.mask2[s] &= !M2_HIDDEN;
                            }
                        } else {
                            self.mask2[s] &= !(M2_PHASED | M2_HIDDEN);
                        }
                        if elapsed == z.b >> 1 && self.nai[s].aux == 0 && nsh < shots.len() {
                            if let Some(t) = near {
                                self.nai[s].aux = 1;
                                let (ptx, pty) = self.center_of(t);
                                let (cx, cy) = self.center_of(s);
                                let (dx, dy) = (ptx - cx, pty - cy);
                                let len = (dx * dx + dy * dy).sqrt();
                                let (ux, uy) = if len.to_raw() == 0 {
                                    (Fixed::from_raw(0), Fixed::from_raw(256))
                                } else {
                                    (dx / len, dy / len)
                                };
                                let muzzle = Fixed::from_raw(12 * 256);
                                let spd = Fixed::from_raw(z.speed);
                                shots[nsh] = (
                                    cx + ux * muzzle,
                                    cy + uy * muzzle,
                                    ux * spd,
                                    uy * spd,
                                    z.style,
                                );
                                nsh += 1;
                            }
                        }
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            self.nai[s].state = 0;
                            self.nai[s].timer = z.a;
                            self.mask2[s] |= M2_PHASED | M2_HIDDEN;
                            self.used2 |= M2_PHASED | M2_HIDDEN;
                        }
                    }
                }
                // ── Bouncer: grounded pause → parabolic hop ──────────────────
                4 => {
                    if z.state == 0 {
                        self.topdown[s].dx = 0;
                        self.topdown[s].dy = 0;
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            self.nai[s].state = 1;
                            self.nai[s].timer = z.b;
                            let r = self.rnd16();
                            if let (true, Some(t)) = (r & 1 == 0, near) {
                                let dx = self.transform[t].x.floor() - self.transform[s].x.floor();
                                let dy = self.transform[t].y.floor() - self.transform[s].y.floor();
                                let (mx, my) = if dx.abs() >= dy.abs() {
                                    (dx.signum(), 0)
                                } else {
                                    (0, dy.signum())
                                };
                                self.nai_head(s, mx, my);
                            } else {
                                self.nai_pick_dir8(s);
                            }
                            self.topdown[s].speed = z.speed;
                            self.nai[s].z = 0;
                            self.nai[s].vz = (NAI_BOUNCE_G * z.b) >> 1;
                        }
                    } else {
                        self.topdown[s].dx = z.dx;
                        self.topdown[s].dy = z.dy;
                        self.nai[s].z = (self.nai[s].z + self.nai[s].vz).max(0);
                        self.nai[s].vz -= NAI_BOUNCE_G;
                        if self.has(s, C_SPRITE) {
                            self.sprite[s].oy = z.aux - (self.nai[s].z >> 8);
                        }
                        self.nai[s].timer -= 1;
                        if self.nai[s].timer <= 0 {
                            self.nai[s].state = 0;
                            self.nai[s].timer = z.a;
                            self.nai[s].z = 0;
                            if self.has(s, C_SPRITE) {
                                self.sprite[s].oy = z.aux;
                            }
                            self.nai_head(s, 0, 0);
                        }
                    }
                }
                // ── Ricochet: flip the axis that stopped, keep going forever ─
                5 => {
                    let px = self.transform[s].x.to_raw();
                    let py = self.transform[s].y.to_raw();
                    if px == z.a && z.dx != 0 {
                        self.nai[s].dx = -z.dx;
                    }
                    if py == z.b && z.dy != 0 {
                        self.nai[s].dy = -z.dy;
                    }
                    self.nai[s].a = px;
                    self.nai[s].b = py;
                    self.topdown[s].speed = z.speed;
                    self.topdown[s].dx = self.nai[s].dx;
                    self.topdown[s].dy = self.nai[s].dy;
                }
                _ => {}
            }
            // Sprite visibility tracks the hidden bit every frame (idempotent when unchanged;
            // `health_system` runs later and agrees — it carries the same M2_HIDDEN guard).
            if self.has(s, C_SPRITE) {
                let h = self.sprite[s].handle;
                if h >= 0 {
                    tish_agb::native_sprite_set_visible(h, self.mask2[s] & M2_HIDDEN == 0);
                }
            }
        }
        for &(x, y, vx, vy, style) in shots.iter().take(nsh) {
            let save = self.bullet_style;
            self.bullet_style = style;
            self.fire_bullet(x, y, vx, vy);
            self.bullet_style = save;
        }
    }

    // ── Multi-part boss glue (`zx` column) ───────────────────────────────────
    // Minimal primitives the boss_parts.json taxonomy needs beyond set_part/set_train/set_orbiter:
    // per-part hit ROUTING (neck→head, segment→shrinking tail), a vulnerability GATE (an eye that
    // must be open, a last-hit window), part-death NOTIFICATION to the parent (an all-parts-dead
    // check), and detached-part PROMOTION (a head that flies off on part death — here: clear the
    // follow glue and give the same entity new AI).

    /// `set_hit_proxy(e, target)` — damage dealt to `e` lands on `target` instead (≤4 hops,
    /// re-pointable every frame for the shrinking-tail rule). `target < 0` clears.
    fn set_hit_proxy(&mut self, e: i32, target: i32) {
        if let Some(s) = self.slot_of(e) {
            if target < 0 {
                self.mask2[s] &= !M2_PROXY;
            } else {
                self.zx[s].proxy = target;
                self.mask2[s] |= M2_PROXY;
                self.used2 |= M2_PROXY;
            }
        }
    }

    /// `set_vuln_gate(e, open)` — while closed (`open == 0`), `damage()` refuses every hit on `e`
    /// (and every hit routed INTO `e` by a proxy). Installing the gate open is how a game arms a
    /// gated-eye boss: close it while the eye is shut, open it on the eye-open frames.
    fn set_vuln_gate(&mut self, e: i32, open: i32) {
        if let Some(s) = self.slot_of(e) {
            self.zx[s].gate = open;
            self.mask2[s] |= M2_GATE;
            self.used2 |= M2_GATE;
        }
    }

    /// `set_death_note(e, code)` — when `e` dies, push `code` (non-zero) to the death-note queue
    /// the parent's logic drains with `death_note()`.
    fn set_death_note(&mut self, e: i32, code: i32) {
        if let Some(s) = self.slot_of(e) {
            self.zx[s].code = code;
            self.mask2[s] |= M2_NOTE;
            self.used2 |= M2_NOTE;
        }
    }

    /// `set_phased(e, on)` — direct control of the invulnerable flag for state machines the game
    /// owns (a core invulnerable while its orbiting children live, a boss awaiting its trigger
    /// item). Tangibility is
    /// kept: contact damage still lands on the player.
    fn set_phased(&mut self, e: i32, on: bool) {
        if let Some(s) = self.slot_of(e) {
            if on {
                self.mask2[s] |= M2_PHASED;
                self.used2 |= M2_PHASED;
            } else {
                self.mask2[s] &= !M2_PHASED;
            }
        }
    }

    /// `detach_part(e)` — promotion: sever the `set_part`/`set_train`/`set_orbiter` glue so the
    /// entity moves on its own again (a severed flying head: detach, then give it drifter +
    /// shooter AI — same entity, same sprite, no respawn).
    fn detach_part(&mut self, e: i32) {
        if let Some(s) = self.slot_of(e) {
            self.mask[s] &= !C_FOLLOW;
        }
    }

    fn set_chase(&mut self, e: i32, aggro: i32, stride: i32, flap: i32, anim_speed: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TOPDOWN)) {
            self.chase[s] = Chase {
                aggro,
                stride,
                flap: flap.max(1),
                anim_speed: anim_speed.max(1),
            };
            self.mask[s] |= C_CHASE;
            self.used |= C_CHASE;
        }
    }

    /// Native chase system (pure Rust, zero tish callbacks): each `C_CHASE` entity steers toward the
    /// camera target when within aggro and animates from its facing — a room of enemies for free.
    fn chase_system(&mut self) {
        let Some(target) = self.camera_target else {
            return;
        };
        let Some(ts) = self.slot_of(target) else {
            return;
        };
        let ptx = self.transform[ts].x.floor();
        let pty = self.transform[ts].y.floor();
        let lp = self.lure_point();
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_CHASE | C_TOPDOWN) || !self.is_active(s) {
                continue;
            }
            if self.stun[s] > 0 {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }
            // Bait outranks the player: a distracted enemy walks to the food, which is the whole
            // point of carrying it past a hungry guard.
            let (tx, ty) = self.lured_to(s, lp).unwrap_or((ptx, pty));
            let c = self.chase[s];
            let dx = tx - self.transform[s].x.floor();
            let dy = ty - self.transform[s].y.floor();
            let (mut mx, mut my) = (0, 0);
            // Room-confined chase: with a room camera, an enemy only pursues while it shares the
            // player's current room. Otherwise it holds position — no chasing across the screen
            // boundary into the next room — and it freezes entirely during a room slide.
            let same_room = match self.camera_target.and_then(|e| self.slot_of(e)) {
                Some(ts) => self.same_room(s, ts),
                None => true,
            };
            // Chase only in the [contact, aggro] band: within ~12px the enemy is already touching the
            // player (contact damage handles it), so it STOPS instead of jittering ±1px around it —
            // that jitter flipped the facing every frame and made the sprite look like it was spinning.
            let dist = dx.abs() + dy.abs();
            if same_room && dist < c.aggro && dist > 12 {
                if dx.abs() > 6 {
                    mx = dx.signum();
                }
                if dy.abs() > 6 {
                    my = dy.signum();
                }
            }
            // Read the component test BEFORE taking the mutable borrow of `topdown`.
            let owns_frame = self.has(s, C_DIRANIM);
            let td = &mut self.topdown[s];
            td.dx = mx;
            td.dy = my;
            if c.stride > 0 {
                // directional: face the move (horizontal wins) + play the walk / idle clip
                if mx < 0 {
                    td.facing = 2;
                } else if mx > 0 {
                    td.facing = 3;
                } else if my < 0 {
                    td.facing = 1;
                } else if my > 0 {
                    td.facing = 0;
                }
                // `C_DIRANIM` owns the frame when present: it knows where this actor starts on a
                // SHARED strip, which the lines below do not. Without this guard every chasing
                // enemy on a game's 33-actor strip animated frames 0..n — i.e. only the one actor
                // whose base the strip happened to start at.
                if !owns_frame {
                    let base = td.facing * c.stride;
                    let moving = mx != 0 || my != 0;
                    let e = encode(s as u32, self.gen[s]);
                    if moving {
                        self.anim_play(e, base + 1, 4, c.anim_speed, true);
                    } else {
                        self.anim_play(e, base, 1, 1, false);
                    }
                }
            } else if !owns_frame {
                // non-directional (a bat) — just loop the flap frames
                let e = encode(s as u32, self.gen[s]);
                self.anim_play(e, 0, c.flap, c.anim_speed, true);
            }
        }
    }

    // ── RTS: flow fields, seek, attack-move, fog of war ──────────────────────
    //
    // The genre's defining problem is many units moving at once under ONE player intent, and the
    // engine had no shape for it. `isob_path` answers "the route for THIS unit, right now", which is
    // the turn-based question; twelve units ordered to the same place want the opposite — one
    // breadth-first search whose result every unit reads in O(1), rebuilt only when the destination
    // moves. Everything below exists so that a screen of units costs no tish calls at all; see
    // `docs/perf-rules.md` rule 7 for why a per-unit `tick` hook is not an option.

    /// Ensure flow field `id` exists and is sized to the current collision grid. Fields are
    /// allocated lazily and reused: the `dist`/`queue` buffers are the only allocation an RTS makes
    /// after load, and they are made once.
    fn flow_ensure(&mut self, id: usize) -> bool {
        if id >= MAX_FLOWS || self.grid_cols <= 0 || self.grid_rows <= 0 {
            return false;
        }
        let cells = (self.grid_cols * self.grid_rows) as usize;
        let f = &mut self.flows[id];
        if f.cols != self.grid_cols || f.rows != self.grid_rows {
            f.cols = self.grid_cols;
            f.rows = self.grid_rows;
            f.dist = alloc::vec![u16::MAX; cells];
            f.queue = Vec::with_capacity(cells);
            f.goal_col = -1;
            f.goal_row = -1;
            f.ready = false;
        }
        true
    }

    /// Breadth-first fill of `id` outward from (col,row) over walkable cells. Uniform cost, so a
    /// plain FIFO queue is exact — no priority queue, no sorting, no division.
    ///
    /// Re-running with the goal it already has is a no-op, which is what makes this affordable: a
    /// move order rebuilds the field once and then every unit reads it for free until the player
    /// gives another order.
    fn flow_goal(&mut self, id: usize, col: i32, row: i32) {
        if !self.flow_ensure(id) {
            return;
        }
        if self.flows[id].ready && self.flows[id].goal_col == col && self.flows[id].goal_row == row
        {
            return;
        }
        let (cols, rows) = (self.grid_cols, self.grid_rows);
        if col < 0 || row < 0 || col >= cols || row >= rows {
            return;
        }
        // Walkability is read from the World's own collision grid, so a flow field automatically
        // agrees with what actually blocks movement — there is no second map to keep in step.
        let mut walk = alloc::vec![false; (cols * rows) as usize];
        for r in 0..rows {
            for c in 0..cols {
                walk[(r * cols + c) as usize] = !self.is_solid(c, r);
            }
        }
        let f = &mut self.flows[id];
        f.goal_col = col;
        f.goal_row = row;
        f.ready = true;
        for d in f.dist.iter_mut() {
            *d = u16::MAX;
        }
        f.queue.clear();
        let start = (row * cols + col) as usize;
        f.dist[start] = 0;
        f.queue.push(start as i32);
        let mut head = 0usize;
        while head < f.queue.len() {
            let cur = f.queue[head] as usize;
            head += 1;
            let d = f.dist[cur];
            if d == u16::MAX {
                continue;
            }
            let nd = d.saturating_add(1);
            let cc = (cur as i32) % cols;
            let cr = (cur as i32) / cols;
            // Four-neighbour, written out rather than looped over an offset table: the bounds test
            // differs per direction and a table would need a multiply per step.
            if cc > 0 {
                let i = cur - 1;
                if walk[i] && f.dist[i] > nd {
                    f.dist[i] = nd;
                    f.queue.push(i as i32);
                }
            }
            if cc < cols - 1 {
                let i = cur + 1;
                if walk[i] && f.dist[i] > nd {
                    f.dist[i] = nd;
                    f.queue.push(i as i32);
                }
            }
            if cr > 0 {
                let i = cur - cols as usize;
                if walk[i] && f.dist[i] > nd {
                    f.dist[i] = nd;
                    f.queue.push(i as i32);
                }
            }
            if cr < rows - 1 {
                let i = cur + cols as usize;
                if walk[i] && f.dist[i] > nd {
                    f.dist[i] = nd;
                    f.queue.push(i as i32);
                }
            }
        }
    }

    /// Steps-to-goal at (col,row), or -1 if the cell cannot reach the goal. The value a unit reads.
    fn flow_dist(&self, id: usize, col: i32, row: i32) -> i32 {
        if id >= MAX_FLOWS {
            return -1;
        }
        let f = &self.flows[id];
        if !f.ready || col < 0 || row < 0 || col >= f.cols || row >= f.rows {
            return -1;
        }
        let d = f.dist[(row * f.cols + col) as usize];
        if d == u16::MAX {
            -1
        } else {
            d as i32
        }
    }

    fn set_seek(&mut self, e: i32, field: i32, arrive: i32, stride: i32, anim_speed: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TOPDOWN)) {
            self.seek[s] = Seek {
                field,
                arrive: arrive.max(0),
                stride,
                anim_speed: anim_speed.max(1),
                done: false,
            };
            self.mask[s] |= C_SEEK;
            self.used |= C_SEEK;
        }
    }

    fn clear_seek(&mut self, e: i32) {
        if let Some(s) = self.slot_of(e) {
            self.mask[s] &= !C_SEEK;
            self.topdown[s].dx = 0;
            self.topdown[s].dy = 0;
        }
    }

    fn seek_arrived(&self, e: i32) -> bool {
        self.slot_of(e).map(|s| self.seek[s].done).unwrap_or(true)
    }

    /// Walk every seeking entity one step down its flow field. Per unit this is two array reads per
    /// axis and no call, no divide and no float — the entire reason the field exists.
    fn seek_system(&mut self) {
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask[s] & C_SLEEP != 0 || !self.has(s, C_SEEK | C_TOPDOWN) {
                continue;
            }
            if self.stun[s] > 0 {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }
            // A soldier that has broken off to fight owns its own movement this frame — it is either
            // closing on its target or standing still to swing, and `soldier_system` (which runs
            // first) has already written that intent. Zeroing it here instead would mean a unit
            // could never walk the last few pixels to something it had already decided to attack.
            if self.has(s, C_SOLDIER) && self.soldier[s].target >= 0 {
                continue;
            }
            let sk = self.seek[s];
            let id = sk.field as usize;
            if id >= MAX_FLOWS || !self.flows[id].ready {
                continue;
            }
            // A transform is the collider's TOP-LEFT (see `box_hits_solid`), so every cell question
            // below has to be asked about the box's CENTRE. Asking it about the corner puts the unit
            // a half-box off, which reads as "it paths into the wall beside the gap".
            let (px, py) = self.seek_centre(s);
            let col = px >> 4;
            let row = py >> 4;
            let (gc, gr) = (self.flows[id].goal_col, self.flows[id].goal_row);
            // Arrival is measured in PIXELS against the goal cell's centre, not in cells: a unit
            // whose cell index matches the goal can still be 15px away, and a crowd that stops on
            // "same cell" piles into one tile and jitters.
            let d_px = (px - (gc * 16 + 8)).abs() + (py - (gr * 16 + 8)).abs();
            if d_px <= sk.arrive.max(4) {
                self.seek[s].done = true;
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                if sk.stride > 0 && !self.has(s, C_DIRANIM) {
                    let base = self.topdown[s].facing * sk.stride;
                    let e = encode(s as u32, self.gen[s]);
                    self.anim_play(e, base, 1, 1, false);
                }
                continue;
            }
            self.seek[s].done = false;
            let here = self.flow_dist(id, col, row);
            // Off the field (spawned inside a wall, or walled off from the goal): hold still rather
            // than wander. A unit that cannot reach its order is a UI problem, not a movement one.
            if here < 0 {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }
            // Inside the goal cell the field is flat — every neighbour is FARTHER, so the "step to a
            // smaller number" rule finds no move and the unit parks wherever it entered the tile,
            // up to 15px off centre and permanently short of its arrive radius. The last step of a
            // move order is therefore in PIXELS, not in cells.
            if here == 0 {
                let gx = gc * 16 + 8;
                let gy = gr * 16 + 8;
                let td = &mut self.topdown[s];
                td.dx = if (px - gx).abs() <= 2 {
                    0
                } else if px < gx {
                    1
                } else {
                    -1
                };
                td.dy = if (py - gy).abs() <= 2 {
                    0
                } else if py < gy {
                    1
                } else {
                    -1
                };
                continue;
            }
            let west = self.flow_dist(id, col - 1, row);
            let east = self.flow_dist(id, col + 1, row);
            let north = self.flow_dist(id, col, row - 1);
            let south = self.flow_dist(id, col, row + 1);
            let better = |n: i32| n >= 0 && n < here;
            let mut mx = 0;
            let mut my = 0;
            // Both axes may improve at once, which is what gives diagonal movement around corners
            // without storing a direction per cell.
            if better(west) && (!better(east) || west <= east) {
                mx = -1;
            } else if better(east) {
                mx = 1;
            }
            if better(north) && (!better(south) || north <= south) {
                my = -1;
            } else if better(south) {
                my = 1;
            }
            // Corridor centring. The field reasons in CELLS; a unit is a BOX, and a box walking
            // along row 3 with its bottom edge poking into row 4 is blocked by row 4's wall even
            // though every cell of its route is clear. It then wedges permanently at the first gap.
            //
            // So when the step is purely along one axis, drift toward the centre of the lane on the
            // other. This is free (it uses movement the unit was making anyway) and it is what stops
            // "the path is right but the unit is stuck" — the single most confusing failure a flow
            // field produces.
            if mx != 0 && my == 0 {
                let cy = row * 16 + 8;
                if (py - cy).abs() > 2 {
                    my = if py < cy { 1 } else { -1 };
                }
            } else if my != 0 && mx == 0 {
                let cx = col * 16 + 8;
                if (px - cx).abs() > 2 {
                    mx = if px < cx { 1 } else { -1 };
                }
            }
            let owns_frame = self.has(s, C_DIRANIM);
            let td = &mut self.topdown[s];
            td.dx = mx;
            td.dy = my;
            if sk.stride > 0 {
                if mx < 0 {
                    td.facing = 2;
                } else if mx > 0 {
                    td.facing = 3;
                } else if my < 0 {
                    td.facing = 1;
                } else if my > 0 {
                    td.facing = 0;
                }
                if !owns_frame {
                    let base = td.facing * sk.stride;
                    let moving = mx != 0 || my != 0;
                    let e = encode(s as u32, self.gen[s]);
                    if moving {
                        self.anim_play(e, base + 1, 4, sk.anim_speed, true);
                    } else {
                        self.anim_play(e, base, 1, 1, false);
                    }
                }
            }
        }
    }

    // ── Terrain as a streamed window ─────────────────────────────────────────
    //
    // An alternative to the `scene:` Tiled pipeline, for a game whose terrain and whose OVERLAY
    // (fog, in practice) must share one palette.
    //
    // `scene:` bakes an atlas from the tiles a map actually uses, and `tilemap_new` uploads the
    // whole tileset's palettes — and the GBA has ONE set of sixteen background palette banks, so
    // whichever ran last owns the colours on screen. Two bakers over the same PNG produce two
    // orderings, and the loser draws in the winner's colours: measured in warforge as a black map,
    // then a brown shroud, depending which way round they were built.
    //
    // Loading the map here instead means one asset, one palette, and no conflict possible. The map
    // costs 4 bytes a cell in EWRAM (a 40x26 map is ~4KB) and is written once per mission.
    /// `gids` / `solid` are the tish arrays, read STRAIGHT into the reused buffer.
    ///
    /// The obvious version — collect each into a `Vec<i32>`, then `to_vec()` into `self.terr` —
    /// allocates three 1,040-element buffers per mission load and drops them again. On a small heap
    /// that churn fragmented it enough that the first mission transition died in the allocator.
    /// `clear()` keeps the capacity, so after the first map no allocation happens at all.
    fn terrain_load(&mut self, cols: i32, rows: i32, gids: Option<&Value>, solid: Option<&Value>) {
        self.terr_cols = cols;
        self.terr_rows = rows;
        self.terr.clear();
        if let Some(Value::Array(a)) = gids {
            let b = a.borrow();
            self.terr.reserve(b.len());
            for v in b.iter() {
                self.terr.push(match v {
                    Value::Number(f) => *f as i32,
                    _ => 0,
                });
            }
        }
        self.terr_shown = alloc::vec![-1i32; 256];
        self.terr_win = alloc::vec![-1i32; 256];
        // The collision grid comes from the same arrays, so terrain and pathing cannot disagree.
        self.grid_setup(cols, rows);
        if let Some(Value::Array(a)) = solid {
            let b = a.borrow();
            for r in 0..rows {
                for c in 0..cols {
                    let i = (r * cols + c) as usize;
                    let on = match b.get(i) {
                        Some(Value::Number(f)) => *f != 0.0,
                        _ => false,
                    };
                    if on {
                        self.grid_set_solid(c, r, true);
                    }
                }
            }
        }
    }

    /// Repaint a single terrain cell — a razed building reverting to ground, a felled tree becoming
    /// a stump. The window blit picks the change up on its next pass.
    fn terrain_set(&mut self, col: i32, row: i32, gid: i32, solid: i32) {
        if col < 0 || row < 0 || col >= self.terr_cols || row >= self.terr_rows {
            return;
        }
        let i = (row * self.terr_cols + col) as usize;
        if i < self.terr.len() {
            self.terr[i] = gid;
        }
        self.grid_set_solid(col, row, solid != 0);
    }

    /// Paint the terrain window for the camera, writing only what changed — the same wrapping
    /// 16x16-cell torus `fog_blit` uses, and for the same reason: a `tilemap_new` layer is 256x256
    /// px and wraps, and that wrap is what lets one small layer cover a map of any size.
    ///
    /// The fog is painted **into this same layer**: an unseen cell writes `gid_unseen` instead of
    /// its terrain. A separate shroud layer is the obvious design and was built first — it works in
    /// isolation (examples/rts-fog) but a second `tilemap_new` layer stacked over a first one, with
    /// a UI canvas also claiming a slot, would not come to the front however its priority was set.
    /// Folding it in costs one blit instead of two and removes the ordering question entirely.
    ///
    /// The model this gives is Warcraft's own: unseen is black, explored stays visible as terrain,
    /// and what the fog actually hides is UNITS — which the game does by hiding their sprites.
    fn terrain_blit(
        &mut self,
        bg: i32,
        tileset: i32,
        ts_cols: i32,
        cam_x: i32,
        cam_y: i32,
        gid_unseen: i32,
    ) -> i32 {
        if self.terr_cols <= 0 {
            return 0;
        }
        let (cols, rows) = (self.terr_cols, self.terr_rows);
        let c0 = cam_x >> 4;
        let r0 = cam_y >> 4;
        let mut wrote = 0;
        for dr in 0..11 {
            let r = r0 + dr;
            if r < 0 || r >= rows {
                continue;
            }
            for dc in 0..16 {
                let c = c0 + dc;
                if c < 0 || c >= cols {
                    continue;
                }
                let map_i = r * cols + c;
                let mut gid = self.terr[map_i as usize];
                if gid_unseen > 0
                    && self.fog.on
                    && self
                        .fog
                        .state
                        .get(map_i as usize)
                        .copied()
                        .unwrap_or(FOG_VISIBLE)
                        == FOG_UNSEEN
                {
                    gid = gid_unseen;
                }
                let bgi = (((r & 15) * 16) + (c & 15)) as usize;
                // `terr_shown` holds the gid actually painted — which may be the shroud — so a
                // cell repaints when the FOG changes as well as when the terrain does.
                if self.terr_win[bgi] == map_i && self.terr_shown[bgi] == gid {
                    continue;
                }
                self.terr_win[bgi] = map_i;
                self.terr_shown[bgi] = gid;
                tish_agb::native_tilemap_set(bg, tileset, ts_cols, c & 15, r & 15, gid);
                wrote += 1;
            }
        }
        wrote
    }

    /// Centre of an entity's collider in whole pixels. The engine's transform is a box's top-left,
    /// which is right for collision and wrong for every "which cell am I in" question.
    fn seek_centre(&self, s: usize) -> (i32, i32) {
        let (mut hw, mut hh) = (0, 0);
        if self.has(s, C_COLLIDER) {
            hw = self.collider[s].w.floor() / 2;
            hh = self.collider[s].h.floor() / 2;
        }
        (
            self.transform[s].x.floor() + hw,
            self.transform[s].y.floor() + hh,
        )
    }

    fn set_soldier(&mut self, e: i32, team: i32, range: i32, dmg: i32, cooldown: i32) {
        if let Some(s) = self.slot_of(e) {
            self.soldier[s] = Soldier {
                team,
                range: range.max(1),
                dmg: dmg.max(0),
                cooldown: cooldown.max(1),
                timer: 0,
                target: -1,
                recheck: 0,
            };
            self.mask[s] |= C_SOLDIER;
            self.used |= C_SOLDIER;
        }
    }

    fn soldier_team(&self, e: i32) -> i32 {
        self.slot_of(e).map(|s| self.soldier[s].team).unwrap_or(-1)
    }

    fn soldier_target(&self, e: i32) -> i32 {
        self.slot_of(e)
            .map(|s| self.soldier[s].target)
            .unwrap_or(-1)
    }

    /// Attack-move: hold fire while nothing hostile is in reach, engage the nearest enemy soldier
    /// when one is, resume walking when it dies. Composed from the health machinery that already
    /// exists, so the only new work here is the acquire scan.
    ///
    /// The scan is **staggered**: a unit re-acquires every `ACQUIRE_PERIOD` frames on a phase
    /// derived from its slot, so twenty units never scan on the same frame. That turns an O(n²)
    /// every frame into O(n²/8) with no behavioural difference a player can see.
    fn soldier_system(&mut self) {
        const ACQUIRE_PERIOD: i32 = 8;
        let n = self.alive.len();
        for s in 0..n {
            if !self.alive[s] || self.mask[s] & C_SLEEP != 0 || !self.has(s, C_SOLDIER) {
                continue;
            }
            if self.soldier[s].timer > 0 {
                self.soldier[s].timer -= 1;
            }
            if self.stun[s] > 0 {
                continue;
            }
            let (sx, sy) = self.seek_centre(s);
            let team = self.soldier[s].team;
            let range = self.soldier[s].range;
            // A unit ACQUIRES far outside the distance at which it can swing, then walks the rest.
            // With one radius for both, a unit only ever fights what it physically bumps into: two
            // armies marched past each other on this exact bug (see examples/rts-select).
            let aggro = range * 4;

            // Drop a target that died, was recycled, or ran away. The give-up distance is wider than
            // the acquire one so a target hovering at the edge does not flicker on and off.
            let mut tgt = self.soldier[s].target;
            if tgt >= 0 {
                match self.slot_of(tgt) {
                    Some(ts) if self.alive[ts] && self.health_alive_slot(ts) => {
                        let (ox, oy) = self.seek_centre(ts);
                        if (ox - sx).abs() + (oy - sy).abs() > aggro + 16 {
                            tgt = -1;
                        }
                    }
                    _ => tgt = -1,
                }
            }

            self.soldier[s].recheck -= 1;
            if tgt < 0 && self.soldier[s].recheck <= 0 {
                self.soldier[s].recheck = ACQUIRE_PERIOD;
                let mut best = -1;
                let mut best_d = aggro + 1;
                for o in 0..n {
                    if o == s || !self.alive[o] || !self.has(o, C_SOLDIER) {
                        continue;
                    }
                    if self.soldier[o].team == team || !self.health_alive_slot(o) {
                        continue;
                    }
                    let (ox, oy) = self.seek_centre(o);
                    let d = (ox - sx).abs() + (oy - sy).abs();
                    if d <= best_d {
                        best_d = d;
                        best = encode(o as u32, self.gen[o]);
                    }
                }
                tgt = best;
            } else if tgt < 0 {
                // Not this unit's frame to scan — stagger by slot so the cost spreads.
                self.soldier[s].recheck = self.soldier[s].recheck.max(s as i32 & 7);
            }
            self.soldier[s].target = tgt;

            if tgt < 0 {
                continue;
            }
            let Some(ts) = self.slot_of(tgt) else {
                continue;
            };
            let (ox, oy) = self.seek_centre(ts);
            let (dx, dy) = (ox - sx, oy - sy);
            // Face the thing before doing anything to it, so both the walk and the swing read.
            if self.has(s, C_TOPDOWN) {
                self.topdown[s].facing = if dx.abs() > dy.abs() {
                    if dx < 0 {
                        2
                    } else {
                        3
                    }
                } else if dy < 0 {
                    1
                } else {
                    0
                };
            }
            if dx.abs() + dy.abs() > range {
                // Closing. Steer straight at the target rather than down the flow field: the field
                // points at the ORDER, and the fight is somewhere else. This is short-range and
                // unobstructed by construction — anything further away was never acquired.
                if self.has(s, C_TOPDOWN) {
                    let td = &mut self.topdown[s];
                    td.dx = if dx.abs() <= 2 {
                        0
                    } else if dx > 0 {
                        1
                    } else {
                        -1
                    };
                    td.dy = if dy.abs() <= 2 {
                        0
                    } else if dy > 0 {
                        1
                    } else {
                        -1
                    };
                }
                continue;
            }
            // In range: stand and swing on the cooldown.
            if self.has(s, C_TOPDOWN) {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
            }
            if self.soldier[s].timer == 0 {
                let dmg = self.soldier[s].dmg;
                self.soldier[s].timer = self.soldier[s].cooldown;
                if dmg > 0 {
                    self.damage(tgt, dmg);
                }
            }
        }
    }

    fn health_alive_slot(&self, s: usize) -> bool {
        !self.has(s, C_HEALTH) || self.health[s].hp > 0
    }

    fn set_vision(&mut self, e: i32, radius: i32) {
        if let Some(s) = self.slot_of(e) {
            self.vision[s] = Vision {
                radius: radius.max(0),
                last_col: i32::MIN,
                last_row: i32::MIN,
            };
            if radius > 0 {
                self.mask[s] |= C_VISION;
                self.used |= C_VISION;
            } else {
                self.mask[s] &= !C_VISION;
            }
        }
    }

    fn fog_init(&mut self, cols: i32, rows: i32) {
        let cells = (cols.max(0) * rows.max(0)) as usize;
        self.fog.cols = cols;
        self.fog.rows = rows;
        self.fog.state = alloc::vec![0u8; cells];
        // `shown`/`win` are indexed by the 256 cells of the wrapping shroud LAYER, not by the map.
        // They start at values nothing can match, so the first blit paints the whole window once and
        // every blit after it paints only what changed.
        self.fog.shown = alloc::vec![0xFFu8; 256];
        self.fog.win = alloc::vec![-1i32; 256];
        self.fog.on = cells > 0;
        self.fog_settled = false;
    }

    fn fog_reveal(&mut self, col: i32, row: i32, radius: i32) {
        if !self.fog.on {
            return;
        }
        let (cols, rows) = (self.fog.cols, self.fog.rows);
        let rr = radius * radius;
        for dr in -radius..=radius {
            let r = row + dr;
            if r < 0 || r >= rows {
                continue;
            }
            let base = r * cols;
            for dc in -radius..=radius {
                let c = col + dc;
                if c < 0 || c >= cols {
                    continue;
                }
                if dc * dc + dr * dr <= rr {
                    self.fog.state[(base + c) as usize] = FOG_VISIBLE;
                }
            }
        }
    }

    /// Demote last frame's visible cells to "explored", then re-stamp from every seeing entity.
    /// Explored never goes back to unseen — that is the difference between fog and a black screen.
    fn fog_system(&mut self) {
        if !self.fog.on {
            return;
        }
        // Nothing to do unless a seeing entity crossed a CELL boundary.
        //
        // The sweep below is O(map) — 1,040 cells on a 40x26 map — and it recomputes an identical
        // answer on every frame where nobody moved far enough to change what is visible. Units walk
        // at 1px/frame and cells are 16px, so a given unit changes cell about once every 16 frames;
        // this skips the great majority of frames outright.
        //
        // (The obvious alternative — tracking lit cells in a list and demoting only those — was
        // built and measured WORSE: `world_step` 2,121 -> 2,550, because pushing ~300 bounds-checked
        // indices costs more than sweeping 1,040 bytes. Skipping whole frames is the win; making
        // the sweep itself incremental is not.)
        let mut moved = false;
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask[s] & C_SLEEP != 0 || !self.has(s, C_VISION | C_TRANSFORM)
            {
                continue;
            }
            let (cx, cy) = self.seek_centre(s);
            let (col, row) = (cx >> 4, cy >> 4);
            if self.vision[s].last_col != col || self.vision[s].last_row != row {
                self.vision[s].last_col = col;
                self.vision[s].last_row = row;
                moved = true;
            }
        }
        if !moved && self.fog_settled {
            return;
        }
        self.fog_settled = true;

        // A flat sweep of the whole state array, deliberately.
        //
        // The O(map) cost here looks wrong — 1,040 cells a frame on a 40x26 map, independent of
        // what is happening — so it was rewritten to remember which cells were lit and demote only
        // those. That measured WORSE: `world_step` went 2,121 -> 2,550. A vision disc is ~300 cells
        // across four units, and pushing 300 bounds-checked i32s costs more than sweeping 1,040
        // bytes linearly. Left as a sweep; do not "optimise" it again without measuring.
        for v in self.fog.state.iter_mut() {
            if *v == FOG_VISIBLE {
                *v = FOG_EXPLORED;
            }
        }
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask[s] & C_SLEEP != 0 || !self.has(s, C_VISION | C_TRANSFORM)
            {
                continue;
            }
            let (cx, cy) = self.seek_centre(s);
            let col = cx >> 4;
            let row = cy >> 4;
            let r = self.vision[s].radius;
            self.fog_reveal(col, row, r);
        }
    }

    /// Push changed fog cells to a shroud background layer, and return how many cells were written.
    ///
    /// The shroud is a **streamed window**, not a whole-map layer, because a `tilemap_new`
    /// background is `Background32x32` — 32×32 8px tiles = 16×16 of our 16px cells — and it WRAPS.
    /// A map any bigger than 256×256px cannot be painted into one. That wrap is not the problem, it
    /// is the mechanism: map cell (c,r) is painted at BG cell (c & 15, r & 15), and since the screen
    /// is only 15×10 cells no two visible cells ever collide. Point the layer at the camera with
    /// `bg_parallax(bg, 256, 256)` and the wrap lines up on its own.
    ///
    /// `shown` therefore tracks what each of the 256 BG cells is currently displaying — the map cell
    /// AND its state packed together — so a cell is rewritten either when its fog changes or when
    /// the camera scrolls a different map cell onto it. `tileset` supplies three 16px cells:
    /// **1 = opaque shroud, 2 = dithered half-shroud**; a visible cell is written blank.
    #[allow(clippy::too_many_arguments)]
    fn fog_blit(
        &mut self,
        bg: i32,
        tileset: i32,
        ts_cols: i32,
        cam_x: i32,
        cam_y: i32,
        gid_unseen: i32,
        gid_explored: i32,
    ) -> i32 {
        if !self.fog.on {
            return 0;
        }
        if self.fog.shown.len() < 256 {
            self.fog.shown = alloc::vec![0xFFu8; 256];
            self.fog.win = alloc::vec![-1i32; 256];
        }
        let (cols, rows) = (self.fog.cols, self.fog.rows);
        let c0 = cam_x >> 4;
        let r0 = cam_y >> 4;
        let mut wrote = 0;
        // 16×11 cells covers a 240×160 screen plus the partial row/column at each edge.
        for dr in 0..11 {
            let r = r0 + dr;
            if r < 0 || r >= rows {
                continue;
            }
            for dc in 0..16 {
                let c = c0 + dc;
                if c < 0 || c >= cols {
                    continue;
                }
                let map_i = r * cols + c;
                let st = self.fog.state[map_i as usize];
                let bgi = (((r & 15) * 16) + (c & 15)) as usize;
                if self.fog.win[bgi] == map_i && self.fog.shown[bgi] == st {
                    continue;
                }
                self.fog.win[bgi] = map_i;
                self.fog.shown[bgi] = st;
                let gid = match st {
                    FOG_VISIBLE => 0,
                    FOG_EXPLORED => gid_explored,
                    _ => gid_unseen,
                };
                tish_agb::native_tilemap_set(bg, tileset, ts_cols, c & 15, r & 15, gid);
                wrote += 1;
            }
        }
        wrote
    }

    /// Rest a top-down collider on the center of the 16px tile under its center.
    /// Same formula as snap_mode 2 targets — pad = 8 - w/2 (e.g. 12px box → +2).
    fn snap_topdown_to_tile(&mut self, s: usize) {
        if !self.has(s, C_TRANSFORM | C_COLLIDER) {
            return;
        }
        let w = self.collider[s].w;
        let h = self.collider[s].h;
        let cx = self.transform[s].x.floor() + w.floor() / 2;
        let cy = self.transform[s].y.floor() + h.floor() / 2;
        let col = cx.div_euclid(TILE);
        let row = cy.div_euclid(TILE);
        self.transform[s].x = Fixed::from_raw(col * TILE * 256) + Fixed::from_raw(8 * 256) - w / 2;
        self.transform[s].y = Fixed::from_raw(row * TILE * 256) + Fixed::from_raw(8 * 256) - h / 2;
    }

    /// Native hopper system: moves 8px, snaps to grid, and pauses.
    fn hopper_system(&mut self) {
        let Some(target) = self.camera_target else {
            return;
        };
        let Some(_ts) = self.slot_of(target) else {
            return;
        };

        let lp = self.lure_point();
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_HOPPER | C_TOPDOWN) || !self.is_active(s) {
                continue;
            }
            if self.stun[s] > 0 {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }

            // Similar room confinement check to chase_system
            let same_room = match self.camera_target.and_then(|e| self.slot_of(e)) {
                Some(ts) => self.same_room(s, ts),
                None => true,
            };
            if !same_room {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }

            let h = &mut self.hopper[s];
            let px = self.transform[s].x;
            let py = self.transform[s].y;

            if h.state == 0 {
                // idle
                h.timer -= 1;
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                if h.timer <= 0 {
                    h.state = 1;
                    h.timer = 32; // max frames to complete the 16px hop
                    h.start_x = px;
                    h.start_y = py;

                    // Baited: take the hop toward the food rather than a random one. Still a hop —
                    // the movement stays the enemy's own, only the direction is chosen.
                    if let Some((lx, ly)) = self.lured_to(s, lp) {
                        let ddx = lx - px.floor();
                        let ddy = ly - py.floor();
                        if ddx.abs() > ddy.abs() {
                            self.hopper[s].dir_x = ddx.signum();
                            self.hopper[s].dir_y = 0;
                        } else {
                            self.hopper[s].dir_x = 0;
                            self.hopper[s].dir_y = ddy.signum();
                        }
                        continue;
                    }
                    self.rng = self.rng.wrapping_mul(1664525).wrapping_add(1013904223);
                    let r = (self.rng >> 16) % 4;

                    if r == 0 {
                        self.hopper[s].dir_x = 1;
                        self.hopper[s].dir_y = 0;
                    } else if r == 1 {
                        self.hopper[s].dir_x = -1;
                        self.hopper[s].dir_y = 0;
                    } else if r == 2 {
                        self.hopper[s].dir_x = 0;
                        self.hopper[s].dir_y = 1;
                    } else {
                        self.hopper[s].dir_x = 0;
                        self.hopper[s].dir_y = -1;
                    }
                }
            } else {
                // moving
                let dist = (px - h.start_x).abs() + (py - h.start_y).abs();
                if dist >= Fixed::from_raw(16 * 256) || h.timer <= 0 {
                    h.state = 0;
                    h.timer = 30 + (px.floor() % 30).abs();
                    h.dir_x = 0;
                    h.dir_y = 0;
                    self.topdown[s].dx = 0;
                    self.topdown[s].dy = 0;
                    self.topdown[s].snap_dx = 0;
                    self.topdown[s].snap_dy = 0;
                    // Tile center when snap-locked; legacy 8px half-tile otherwise.
                    if self.topdown[s].snap_mode == TD_SNAP_TILE {
                        self.snap_topdown_to_tile(s);
                    } else {
                        let snap_x = (px.floor() + 4) & !7;
                        let snap_y = (py.floor() + 4) & !7;
                        self.transform[s].x = Fixed::from_raw(snap_x * 256);
                        self.transform[s].y = Fixed::from_raw(snap_y * 256);
                    }
                } else {
                    h.timer -= 1;
                }
            }

            // While a hop is in flight, drive the intent EVERY frame (the mover
            // consumed last frame's) — mirrors how chase_system stays in motion.
            if self.hopper[s].state == 1 {
                self.topdown[s].dx = self.hopper[s].dir_x;
                self.topdown[s].dy = self.hopper[s].dir_y;
            }
            let td = &mut self.topdown[s];
            let c_stride = self.hopper[s].stride;
            if c_stride > 0 {
                let mx = td.dx;
                let my = td.dy;
                if mx < 0 {
                    td.facing = 2;
                } else if mx > 0 {
                    td.facing = 3;
                } else if my < 0 {
                    td.facing = 1;
                } else if my > 0 {
                    td.facing = 0;
                }

                let base = td.facing * c_stride;
                let moving = mx != 0 || my != 0;
                let e = encode(s as u32, self.gen[s]);
                if moving {
                    self.anim_play(e, base + 1, 4, 8, true);
                } else {
                    self.anim_play(e, base, 1, 1, false);
                }
            } else {
                let e = encode(s as u32, self.gen[s]);
                self.anim_play(e, 0, 2, 6, true);
            }
        }
    }

    /// Native jumper system: parabolic jump over walls.
    fn jumper_system(&mut self) {
        let Some(target) = self.camera_target else {
            return;
        };
        let Some(_ts) = self.slot_of(target) else {
            return;
        };

        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_JUMPER | C_TOPDOWN) || !self.is_active(s) {
                continue;
            }
            if self.stun[s] > 0 {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }

            let same_room = match self.camera_target.and_then(|e| self.slot_of(e)) {
                Some(ts) => self.same_room(s, ts),
                None => true,
            };
            if !same_room {
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                continue;
            }

            let px = self.transform[s].x;
            let _py = self.transform[s].y;

            if self.jumper[s].state == 0 {
                // idle
                self.jumper[s].timer -= 1;
                self.topdown[s].dx = 0;
                self.topdown[s].dy = 0;
                if self.jumper[s].timer <= 0 {
                    self.jumper[s].state = 1; // jumping
                    self.jumper[s].z = Fixed::from_raw(0);
                    self.jumper[s].dz = Fixed::from_raw(400); // upward velocity

                    self.rng = self.rng.wrapping_mul(1664525).wrapping_add(1013904223);
                    let r = (self.rng >> 16) % 8;

                    if r == 0 {
                        self.jumper[s].dx = Fixed::from_raw(2 * 256);
                        self.jumper[s].dy = Fixed::from_raw(2 * 256);
                    } else if r == 1 {
                        self.jumper[s].dx = Fixed::from_raw(-2 * 256);
                        self.jumper[s].dy = Fixed::from_raw(2 * 256);
                    } else if r == 2 {
                        self.jumper[s].dx = Fixed::from_raw(-2 * 256);
                        self.jumper[s].dy = Fixed::from_raw(-2 * 256);
                    } else if r == 3 {
                        self.jumper[s].dx = Fixed::from_raw(2 * 256);
                        self.jumper[s].dy = Fixed::from_raw(-2 * 256);
                    } else if r == 4 {
                        self.jumper[s].dx = Fixed::from_raw(2 * 256);
                        self.jumper[s].dy = Fixed::from_raw(0);
                    } else if r == 5 {
                        self.jumper[s].dx = Fixed::from_raw(-2 * 256);
                        self.jumper[s].dy = Fixed::from_raw(0);
                    } else if r == 6 {
                        self.jumper[s].dx = Fixed::from_raw(0);
                        self.jumper[s].dy = Fixed::from_raw(2 * 256);
                    } else {
                        self.jumper[s].dx = Fixed::from_raw(0);
                        self.jumper[s].dy = Fixed::from_raw(-2 * 256);
                    }
                }
            } else {
                // jumping
                // apply jump manual movement, bypassing collision
                let dx = self.jumper[s].dx;
                let dy = self.jumper[s].dy;
                self.transform[s].x += dx;
                self.transform[s].y += dy;
                let dz = self.jumper[s].dz;
                self.jumper[s].z += dz;
                self.jumper[s].dz -= Fixed::from_raw(40); // gravity

                if self.jumper[s].z <= Fixed::from_raw(0) {
                    self.jumper[s].z = Fixed::from_raw(0);
                    self.jumper[s].state = 0;
                    self.jumper[s].timer = 30 + (px.floor() % 60).abs();
                    if self.topdown[s].snap_mode == TD_SNAP_TILE {
                        self.snap_topdown_to_tile(s);
                    } else {
                        let snap_x = (self.transform[s].x.floor() + 4) & !7;
                        let snap_y = (self.transform[s].y.floor() + 4) & !7;
                        self.transform[s].x = Fixed::from_raw(snap_x * 256);
                        self.transform[s].y = Fixed::from_raw(snap_y * 256);
                    }
                }

                if self.has(s, C_SPRITE) {
                    self.sprite[s].oy = -self.jumper[s].z.floor();
                }
            }

            let e = encode(s as u32, self.gen[s]);
            if self.jumper[s].state == 1 {
                self.anim_play(e, 1, 1, 1, false);
            } else {
                self.anim_play(e, 0, 1, 1, false);
            }
        }
    }
    /// Set the move speed in px/frame (default 1.25). Keep it below one tile (16) so the single-step
    /// collision clamp stays exact.
    fn topdown_speed(&mut self, e: i32, px: f64) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TOPDOWN)) {
            self.topdown[s].speed = (px * 256.0) as i32;
        }
    }

    /// Set the move intent this frame (dx/dy ∈ {-1,0,1}); call every frame from input or AI. 0/0
    /// stops. Also updates the persistent facing (horizontal wins, matching the 4-row char sheets).
    /// No-op while a room slide has this entity's input locked.
    fn topdown_move(&mut self, e: i32, dx: i32, dy: i32) {
        if self.input_locked(e) {
            return;
        }
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TOPDOWN)) {
            let (dx, dy) = (dx.signum(), dy.signum());
            self.topdown[s].dx = dx;
            self.topdown[s].dy = dy;
            if dx < 0 {
                self.topdown[s].facing = 2;
            } else if dx > 0 {
                self.topdown[s].facing = 3;
            } else if dy < 0 {
                self.topdown[s].facing = 1;
            } else if dy > 0 {
                self.topdown[s].facing = 0;
            }
        }
    }

    /// Briefly shove the entity in (dx, dy), overriding input for `TD_KNOCK_FRAMES` frames — the
    /// classic hit-reaction. `power` is the raw fixed impulse per axis (0 = the default `TD_KNOCK`).
    fn topdown_knockback(&mut self, e: i32, dx: i32, dy: i32, power: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TOPDOWN)) {
            let p = if power > 0 { power } else { TD_KNOCK };
            self.topdown[s].kx = Fixed::from_raw(dx.signum() * p);
            self.topdown[s].ky = Fixed::from_raw(dy.signum() * p);
            self.topdown[s].knock = TD_KNOCK_FRAMES;
        }
    }

    /// Top-down system: for each `C_TOPDOWN` entity, turn its move intent (or an active knockback)
    /// into a velocity, resolve its box against the solid grid axis-by-axis (no gravity), and clear
    /// the intent. Culled/room-frozen entities are skipped, like the other movers.
    fn topdown_system(&mut self) {
        let zero = Fixed::from_raw(0);
        let frozen = if self.room_cam.enabled && self.room_cam.transitioning {
            self.camera_target.and_then(|e| self.slot_of(e))
        } else {
            None
        };
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_TOPDOWN | C_TRANSFORM | C_COLLIDER) {
                continue;
            }
            if Some(s) == frozen || !self.is_active(s) {
                continue;
            }
            let mut td = self.topdown[s];

            if td.snap_mode == TD_SNAP_TILE
                && td.snap_dx == 0
                && td.snap_dy == 0
                && (td.dx != 0 || td.dy != 0)
            {
                // Idle and got input: start a new snap (ortho only)
                if td.dx != 0 {
                    td.dy = 0;
                }
                td.snap_dx = td.dx;
                td.snap_dy = td.dy;

                let base_x =
                    self.transform[s].x - Fixed::from_raw(8 * 256) + self.collider[s].w / 2;
                let base_y =
                    self.transform[s].y - Fixed::from_raw(8 * 256) + self.collider[s].h / 2;

                let col = if td.dx > 0 {
                    base_x.floor().div_euclid(TILE) + 1
                } else if td.dx < 0 {
                    (base_x.to_raw() + TILE * 256 - 1).div_euclid(TILE * 256) - 1
                } else {
                    (base_x + Fixed::from_raw(8 * 256)).floor().div_euclid(TILE)
                };

                let row = if td.dy > 0 {
                    base_y.floor().div_euclid(TILE) + 1
                } else if td.dy < 0 {
                    (base_y.to_raw() + TILE * 256 - 1).div_euclid(TILE * 256) - 1
                } else {
                    (base_y + Fixed::from_raw(8 * 256)).floor().div_euclid(TILE)
                };

                td.snap_target_x = Fixed::from_raw(col * TILE * 256) + Fixed::from_raw(8 * 256)
                    - self.collider[s].w / 2;
                td.snap_target_y = Fixed::from_raw(row * TILE * 256) + Fixed::from_raw(8 * 256)
                    - self.collider[s].h / 2;
                self.topdown[s] = td;
            }

            let (w, h) = (self.collider[s].w, self.collider[s].h);
            let had_intent = td.dx != 0 || td.dy != 0;
            // A knockback shove overrides input while it lasts; otherwise scale the intent by speed
            // (× TD_DIAG per axis on a diagonal so a diagonal isn't faster than a cardinal move).
            let (mut vx, mut vy) = if td.knock > 0 {
                (td.kx, td.ky)
            } else {
                let sp = if td.speed == 0 { TD_WALK } else { td.speed };
                let (use_dx, use_dy) =
                    if td.snap_mode == TD_SNAP_TILE && (td.snap_dx != 0 || td.snap_dy != 0) {
                        (td.snap_dx, td.snap_dy)
                    } else {
                        (td.dx, td.dy)
                    };
                let diag = use_dx != 0 && use_dy != 0;
                let axis = |d: i32| {
                    if d == 0 {
                        zero
                    } else {
                        let raw = if diag { (sp * TD_DIAG) / 256 } else { sp };
                        Fixed::from_raw(d.signum() * raw)
                    }
                };
                (axis(use_dx), axis(use_dy))
            };
            // X axis, then Y axis: clamp to the tile edge on a wall hit (speeds stay below a tile).
            let (x, y) = (self.transform[s].x, self.transform[s].y);
            // Chase AI must stay in its current room — doorway tiles are walkable for the player,
            // so without this clamp enemies walk through into adjacent rooms while chasing.
            let confine = self.room_cam.enabled && self.has(s, C_CHASE);
            let (rmin_x, rmax_x, rmin_y, rmax_y) = if confine {
                let tw = self.room_cam.room_w * TILE;
                let th = self.room_cam.room_h * TILE;
                // Room from pre-move center so knockback / chase can't push into the next room.
                let cx = x.floor() + w.floor() / 2;
                let cy = y.floor() + h.floor() / 2;
                let room_x = cx.div_euclid(tw);
                let room_y = cy.div_euclid(th);
                (
                    Fixed::from_raw(room_x * tw * 256),
                    Fixed::from_raw((room_x + 1) * tw * 256) - w,
                    Fixed::from_raw(room_y * th * 256),
                    Fixed::from_raw((room_y + 1) * th * 256) - h,
                )
            } else {
                (zero, zero, zero, zero)
            };
            // A body that is ALREADY inside a solid must be allowed to walk out of it. Every clamp
            // below tests the destination, and from inside a wall every destination is also inside
            // one — so both axes get zeroed, every frame, forever: the entity is stuck for good and
            // nothing reports it. Skipping the clamps while embedded lets it walk free, and the
            // frame it clears the tile the normal clamps take over again. A body resting AGAINST a
            // wall is unaffected: it is adjacent, not overlapping, and `box_hits_solid` is
            // inclusive-exclusive precisely so a box clamped onto a tile edge has no solid inside it.
            let embedded = self.box_hits_solid(x, y, w, h);
            let nx = x + vx;
            let mut rx = if embedded {
                nx
            } else if vx > zero && self.box_hits_solid(nx, y, w, h) {
                let col = (((nx + w).to_raw() - 1) >> 8).div_euclid(TILE);
                vx = zero;
                Fixed::from_raw(col * TILE * 256) - w
            } else if vx < zero && self.box_hits_solid(nx, y, w, h) {
                let col = nx.floor().div_euclid(TILE);
                vx = zero;
                Fixed::from_raw((col + 1) * TILE * 256)
            } else {
                nx
            };
            // Entity blockers (NPCs, …): snap to the hit box's edge on the axis we moved, same as a
            // solid tile — so the player's collision follows the NPC, not a leftover spawn tile.
            if vx != zero {
                if let Some((bx, _, bw, _)) = self.first_blocker_hit(s, rx, y, w, h) {
                    rx = if vx > zero { bx - w } else { bx + bw };
                    vx = zero;
                }
            }
            let ny = y + vy;
            let mut ry = if embedded {
                ny
            } else if vy > zero && self.box_hits_solid(rx, ny, w, h) {
                let row = (((ny + h).to_raw() - 1) >> 8).div_euclid(TILE);
                vy = zero;
                Fixed::from_raw(row * TILE * 256) - h
            } else if vy < zero && self.box_hits_solid(rx, ny, w, h) {
                let row = ny.floor().div_euclid(TILE);
                vy = zero;
                Fixed::from_raw((row + 1) * TILE * 256)
            } else {
                ny
            };
            if vy != zero {
                if let Some((_, by, _, bh)) = self.first_blocker_hit(s, rx, ry, w, h) {
                    ry = if vy > zero { by - h } else { by + bh };
                    vy = zero;
                }
            }
            if confine {
                if rx < rmin_x {
                    rx = rmin_x;
                } else if rx > rmax_x {
                    rx = rmax_x;
                }
                if ry < rmin_y {
                    ry = rmin_y;
                } else if ry > rmax_y {
                    ry = rmax_y;
                }
            }
            if td.snap_mode == TD_SNAP_TILE && (td.snap_dx != 0 || td.snap_dy != 0) {
                let over_x = if td.snap_dx > 0 {
                    rx >= td.snap_target_x
                } else {
                    rx <= td.snap_target_x
                };
                let over_y = if td.snap_dy > 0 {
                    ry >= td.snap_target_y
                } else {
                    ry <= td.snap_target_y
                };
                let hit_wall = (td.snap_dx != 0 && vx == zero) || (td.snap_dy != 0 && vy == zero);
                if (td.snap_dx != 0 && over_x) || (td.snap_dy != 0 && over_y) || hit_wall {
                    let match_intent = if td.snap_dx != 0 {
                        td.dx == td.snap_dx
                    } else {
                        td.dy == td.snap_dy
                    };
                    if !hit_wall && match_intent {
                        // Land exactly on this cell, then aim at the next. Without the clamp,
                        // speed that doesn't divide 16 leaves a permanent sub-tile drift while
                        // holding a direction — entities never rest on the floor grid.
                        rx = td.snap_target_x;
                        ry = td.snap_target_y;
                        self.topdown[s].snap_target_x =
                            td.snap_target_x + Fixed::from_raw(td.snap_dx * TILE * 256);
                        self.topdown[s].snap_target_y =
                            td.snap_target_y + Fixed::from_raw(td.snap_dy * TILE * 256);
                    } else {
                        if hit_wall {
                            // Wall clamp can leave a sub-tile offset — rest on the cell we occupy.
                            self.transform[s].x = rx;
                            self.transform[s].y = ry;
                            self.snap_topdown_to_tile(s);
                            rx = self.transform[s].x;
                            ry = self.transform[s].y;
                        } else {
                            rx = td.snap_target_x;
                            ry = td.snap_target_y;
                        }
                        self.topdown[s].snap_dx = 0;
                        self.topdown[s].snap_dy = 0;
                    }
                }
            }

            self.transform[s].x = rx;
            self.transform[s].y = ry;

            let td = &mut self.topdown[s];
            td.moving = td.knock <= 0 && had_intent; // walk anim even when pushing into a wall
            if td.knock > 0 {
                td.knock -= 1;
                if td.knock == 0 {
                    td.snap_dx = 0;
                    td.snap_dy = 0;
                }
            }
            // Hard idle lock: nothing with snap_mode 2 may rest between tiles (script teleports,
            // knock settle, wall clamps, hopper land). Mid-step / knock transit may be between.
            let knock_left = td.knock;
            let snap_busy = td.snap_dx != 0 || td.snap_dy != 0;
            let mode = td.snap_mode;
            td.dx = 0;
            td.dy = 0;
            if mode == TD_SNAP_TILE && knock_left == 0 && !snap_busy {
                self.snap_topdown_to_tile(s);
            }
        }
    }

    /// Spawn a short-lived melee hurt box in front of `attacker` (offset by its top-down facing): a
    /// `size`×`size` box `reach` px past the attacker's edge that deals `damage` to `target_tag`
    /// entities for `ttl` frames. The victim's i-frames make one swing land once. Invisible — the
    /// attacker's own attack animation is the visual. Returns the hitbox entity id.
    fn swing(
        &mut self,
        attacker: i32,
        target_tag: i32,
        damage: i32,
        reach: i32,
        size: i32,
        ttl: i32,
    ) -> i32 {
        let Some(s) = self.slot_of(attacker) else {
            return 0;
        };
        // ⚠️ A platformer has no top-down facing, and `0` means DOWN — so before this, every melee
        // swing in a side-scroller spawned its hurt box under the attacker's feet and the sword
        // visibly missed everything in front of it. `Platformer.face` is the sticky -1/+1 heading
        // (it keeps the last non-zero direction, unlike `dir`, which is this frame's intent), which
        // is exactly what a swing wants.
        let facing = if self.has(s, C_TOPDOWN) {
            self.topdown[s].facing
        } else if self.has(s, C_PLATFORMER) {
            if self.platformer[s].face < 0 {
                2
            } else {
                3
            }
        } else {
            0
        };
        let ax = self.transform[s].x.floor();
        let ay = self.transform[s].y.floor();
        let cw = self.collider[s].w.floor();
        let ch = self.collider[s].h.floor();
        let (cx, cy) = (ax + cw / 2, ay + ch / 2);
        let (bx, by) = match facing {
            1 => (cx - size / 2, ay - reach - size), // up
            2 => (ax - reach - size, cy - size / 2), // left
            3 => (ax + cw + reach, cy - size / 2),   // right
            _ => (cx - size / 2, ay + ch + reach),   // down
        };
        let e = self.spawn();
        if let Some(ss) = self.slot_of(e) {
            self.transform[ss] = Transform {
                x: to_fixed(bx as f64),
                y: to_fixed(by as f64),
            };
            self.collider[ss] = Collider {
                w: to_fixed(size as f64),
                h: to_fixed(size as f64),
            };
            self.hurt[ss] = Hurt {
                damage,
                target_tag,
                despawn_on_hit: false,
                stun: 0,
                damage_type: 0,
            };
            self.life[ss] = Life {
                ttl: ttl.max(1),
                offscreen: false,
            };
            self.mask[ss] |= C_TRANSFORM | C_COLLIDER | C_HURT | C_LIFE;
            self.used |= C_TRANSFORM | C_COLLIDER | C_HURT | C_LIFE;
        }
        e
    }

    // ── Health / combat ──────────────────────────────────────────────────────
    /// Give an entity `max` hit points (starts full). Enables `damage`/`heal` + the health system.
    fn set_health(&mut self, e: i32, max: i32, invuln_max: i32) {
        if let Some(s) = self.slot_of(e) {
            let m = max.max(1);
            self.health[s] = Health {
                hp: m,
                max: m,
                invuln: 0,
                invuln_max: invuln_max.max(0),
                dead: false,
            };
            self.mask[s] |= C_HEALTH;
            self.used |= C_HEALTH;
        }
    }

    /// Apply `amount` damage unless mid i-frames / already dead. A real hit starts the i-frame
    /// window; reaching 0 hp flags death (the health phase fires onDeath / despawns). Returns
    /// whether the hit landed.
    fn damage(&mut self, e: i32, amount: i32) -> bool {
        // Boss glue (all gated on mask2 bits, so an entity that never used the new natives
        // takes the plain path below unchanged):
        //   - phased/hidden (drifter in flight, burrower underground): the hit is refused outright.
        //   - closed vulnerability gate (the weak-point eye shut): refused.
        //   - hit proxy (boss neck → head, segment → current tail): the damage is
        //     re-routed, following at most 4 hops so a mis-wired cycle cannot hang the frame.
        //     Each hop's own phase/gate is honoured — routing INTO a closed part still refuses.
        let mut e = e;
        for _ in 0..4 {
            let Some(s) = self.slot_of(e) else { break };
            let m2 = self.mask2[s];
            if m2 & (M2_PHASED | M2_HIDDEN) != 0 {
                return false;
            }
            if m2 & M2_GATE != 0 && self.zx[s].gate == 0 {
                return false;
            }
            if m2 & M2_PROXY != 0 {
                let p = self.zx[s].proxy;
                if p >= 0 && p != e && self.slot_of(p).is_some() {
                    e = p;
                    continue;
                }
            }
            break;
        }
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_HEALTH)) {
            if self.health[s].invuln > 0 || self.health[s].dead || amount <= 0 {
                return false;
            }
            let h = &mut self.health[s];
            h.hp -= amount;
            h.invuln = h.invuln_max;
            if h.hp <= 0 {
                h.hp = 0;
                h.dead = true;
            }
            return true;
        }
        false
    }

    /// Heal up to `max`; no effect once dead.
    fn heal(&mut self, e: i32, amount: i32) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_HEALTH)) {
            let h = &mut self.health[s];
            if !h.dead {
                h.hp = (h.hp + amount.max(0)).min(h.max);
            }
        }
    }

    // ── Auto-despawn + contact damage (the shoot-'em-up core) ────────────────
    /// Give an entity a time-to-live: it despawns `ttl` frames from now (an explosion that clears
    /// once its clip finishes, a bullet with a bounded range). Preserves any off-screen flag.
    fn set_lifetime(&mut self, e: i32, ttl: i32) {
        if let Some(s) = self.slot_of(e) {
            let offscreen = self.has(s, C_LIFE) && self.life[s].offscreen;
            self.life[s] = Life {
                ttl: ttl.max(0),
                offscreen,
            };
            self.mask[s] |= C_LIFE;
            self.used |= C_LIFE;
        }
    }

    /// Mark an entity for off-screen cleanup: it despawns the moment its box leaves the visible area
    /// (bullets that fly off-screen, enemies that exit past the player). Preserves any TTL.
    fn set_despawn_offscreen(&mut self, e: i32, on: bool) {
        if let Some(s) = self.slot_of(e) {
            let ttl = if self.has(s, C_LIFE) {
                self.life[s].ttl
            } else {
                0
            };
            self.life[s] = Life { ttl, offscreen: on };
            self.mask[s] |= C_LIFE;
            self.used |= C_LIFE;
        }
    }

    /// `set_guard(e, mask)` — block damage arriving from the direction this entity FACES.
    ///
    /// `GUARD_MELEE` (1) stops swings and body contact, `GUARD_SHOT` (2) stops projectiles; a mask
    /// of 0 removes the guard. The genre needs both halves of the same rule — the player's shield
    /// stops what they walk into, an armoured knight stops the sword until you get behind it — so this is one
    /// component rather than two.
    ///
    /// Requires `C_TOPDOWN` for the facing. Without it there is no "front" and nothing is blocked.
    /// Animate `frames` frames starting at `base + facing * stride`, following the entity's facing.
    ///
    /// Facing is whatever the movement systems last set (0 down, 1 up, 2 left, 3 right), so this
    /// composes with `set_chase`, `set_hopper`, `set_jumper` or plain top-down movement — it reads
    /// the facing rather than producing it.
    fn set_dir_anim(&mut self, e: i32, base: i32, stride: i32, frames: i32, speed: i32) {
        if let Some(s) = self.slot_of(e) {
            self.diranim[s] = DirAnim {
                base,
                stride: stride.max(0),
                frames: frames.max(1),
                speed: speed.max(1),
            };
            self.mask[s] |= C_DIRANIM;
            self.used |= C_DIRANIM;
        }
    }

    /// Re-point each directional entity's clip at its current facing, every frame.
    ///
    /// `anim_play` is idempotent for an unchanged clip, so calling it every frame costs a compare
    /// and only restarts the cycle when the facing actually changes — which is the behaviour you
    /// want anyway: turning a corner should show the new direction's first frame at once.
    fn diranim_system(&mut self) {
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask[s] & C_SLEEP != 0 || !self.has(s, C_DIRANIM | C_TOPDOWN)
            {
                continue;
            }
            let d = self.diranim[s];
            let facing = self.topdown[s].facing.clamp(0, 3);
            let e = encode(s as u32, self.gen[s]);
            self.anim_play(e, d.base + facing * d.stride, d.frames, d.speed, true);
        }
    }

    fn set_guard(&mut self, e: i32, mask: i32) {
        if let Some(s) = self.slot_of(e) {
            self.guard[s] = mask;
            if mask == 0 {
                self.mask[s] &= !C_GUARD;
            } else {
                self.mask[s] |= C_GUARD;
                self.used |= C_GUARD;
            }
        }
    }

    /// Does slot `t`'s guard stop a hit whose hurt box is centred at (hcx, hcy)?
    ///
    /// `despawn_on_hit` is what tells a projectile from a swing: in this engine a hurt box consumed
    /// by its own hit IS a bullet, and one that survives is a sword arc or a body. That is a real
    /// invariant of `set_hurt`/`swing` rather than a guess, and it keeps the guard from needing a
    /// new field on every hurt box in the game.
    ///
    /// The direction test uses the DOMINANT axis, so a hit from the side is not blocked by a front
    /// guard — walking into a spitter still hurts, and an armoured knight can still be flanked.
    fn guard_blocks(&self, t: usize, hcx: i32, hcy: i32, is_shot: bool) -> bool {
        let want = if is_shot { GUARD_SHOT } else { GUARD_MELEE };
        if self.guard[t] & want == 0 || !self.has(t, C_TOPDOWN) {
            return false;
        }
        let vcx = self.transform[t].x.floor() + self.collider[t].w.floor() / 2;
        let vcy = self.transform[t].y.floor() + self.collider[t].h.floor() / 2;
        let (dx, dy) = (hcx - vcx, hcy - vcy);
        match self.topdown[t].facing {
            1 => dy < 0 && dy.abs() >= dx.abs(),
            2 => dx < 0 && dx.abs() >= dy.abs(),
            3 => dx > 0 && dx.abs() >= dy.abs(),
            _ => dy > 0 && dy.abs() >= dx.abs(),
        }
    }

    /// Make an entity a contact hurt box (bullet / body hazard). On overlap with a `target_tag`
    /// entity that has health it deals `damage`; `despawn_on_hit` consumes a bullet on contact.
    fn set_hurt(&mut self, e: i32, damage: i32, target_tag: i32, despawn_on_hit: bool, stun: i32) {
        if let Some(s) = self.slot_of(e) {
            self.hurt[s] = Hurt {
                damage,
                target_tag,
                despawn_on_hit,
                stun,
                damage_type: 0,
            };
            self.mask[s] |= C_HURT;
            self.used |= C_HURT;
        }
    }

    // ── Native bullet emitters (the bullet-hell hot path) ────────────────────
    // A pattern like a 16-bullet ring used to cost, per bullet, a boxed tish call chain
    // (fireAngle → fireBullet → mkSprite + 6× default-pick + a dozen `set_*`) — all `Value`
    // dispatch. These build the whole pure-component bullet natively from the current
    // `bullet_style`, so a ring is one native call plus one `Value` opts-resolve, not sixteen.

    fn set_bullet_style(
        &mut self,
        sheet: i32,
        frame: i32,
        size: i32,
        damage: i32,
        target: i32,
        tag: i32,
        ttl: i32,
    ) {
        // Preserve the weapon type across a style change: `bullet_damage_type` is set beside
        // `bullet_style` by the caller, and the two orderings are equally natural to write.
        let dt = self.bullet_style.damage_type;
        self.bullet_style = BulletStyle {
            sheet,
            frame,
            size,
            damage,
            target,
            tag,
            ttl,
            damage_type: dt,
        };
    }

    /// Spawn one bullet at screen point (`cx`,`cy`) with velocity (`vx`,`vy`), configured by the
    /// current `bullet_style`. A pure-component entity: it flies (`Body`), damages `style.target`
    /// (`Hurt`), and auto-despawns on hit / off-screen / after `style.ttl` — no per-frame tish tick.
    fn fire_bullet(&mut self, cx: Fixed, cy: Fixed, vx: Fixed, vy: Fixed) -> i32 {
        let st = self.bullet_style;
        let size_f = Fixed::from_raw(st.size * 256);
        let half = size_f / 2;
        // Off-screen bullets get their VRAM released just like `attach_sprite` does; `render_system`
        // restores the sprite the moment the bullet is on screen, so a dense pattern doesn't try to
        // hold a VRAM sprite for every bullet at spawn time.
        let sp = tish_agb::sprite_new_typed(st.sheet);
        tish_agb::sprite_set_frame_typed(sp, st.frame);
        let off = (st.size - 16) / 2;
        let e = self.spawn();
        if let Some(s) = self.slot_of(e) {
            self.transform[s] = Transform {
                x: cx - half,
                y: cy - half,
            };
            self.collider[s] = Collider {
                w: size_f,
                h: size_f,
            };
            self.body[s] = Body { vx, vy };
            self.sprite[s] = SpriteRef {
                handle: sp,
                ox: off,
                oy: off,
            };
            self.tag[s] = st.tag;
            self.mask[s] |= C_TRANSFORM | C_COLLIDER | C_BODY | C_SPRITE;
            self.used |= C_TRANSFORM | C_COLLIDER | C_BODY | C_SPRITE;
        }
        self.set_hurt(e, st.damage, st.target, true, 0);
        if st.damage_type != 0 {
            if let Some(s) = self.slot_of(e) {
                self.hurt[s].damage_type = st.damage_type;
            }
        }
        self.set_lifetime(e, st.ttl);
        self.set_despawn_offscreen(e, true);
        // NB: unlike `attach_sprite` we do NOT release the sprite's VRAM here. Bullets spawn on-screen
        // and live briefly, so releasing now only to have `render_system` re-create the Object next
        // frame is pure churn; `set_despawn_offscreen` reclaims it the moment the bullet exits.
        e
    }

    /// Fire one bullet at a heading in degrees (0 = right, 90 = down, −90 = up) and scalar `speed`.
    /// agb's `Num::{cos,sin}` take revolutions, so degrees fold to `deg/360`; with the screen's
    /// y-down axis those components are already the correct velocity (no sign flips).
    fn fire_angle(&mut self, cx: Fixed, cy: Fixed, deg: Fixed, speed: Fixed) -> i32 {
        let rev = deg / Fixed::from_raw(360 * 256);
        self.fire_bullet(cx, cy, speed * rev.cos(), speed * rev.sin())
    }

    /// A full ring of `count` bullets evenly spaced around (`cx`,`cy`) at `speed` — the staple.
    fn fire_ring(&mut self, cx: Fixed, cy: Fixed, count: i32, speed: Fixed) {
        if count <= 0 {
            return;
        }
        let count_f = Fixed::from_raw(count * 256);
        for k in 0..count {
            let rev = Fixed::from_raw(k * 256) / count_f;
            self.fire_bullet(cx, cy, speed * rev.cos(), speed * rev.sin());
        }
    }

    /// A fan of `count` bullets `spread_deg` wide, centred on `center_deg`.
    fn fire_spread(
        &mut self,
        cx: Fixed,
        cy: Fixed,
        center_deg: Fixed,
        count: i32,
        spread_deg: Fixed,
        speed: Fixed,
    ) {
        if count <= 0 {
            return;
        }
        if count == 1 {
            self.fire_angle(cx, cy, center_deg, speed);
            return;
        }
        let start = center_deg - spread_deg / 2;
        let step = spread_deg / Fixed::from_raw((count - 1) * 256);
        for k in 0..count {
            let deg = start + step * Fixed::from_raw(k * 256);
            self.fire_angle(cx, cy, deg, speed);
        }
    }

    /// Fire one bullet from (`cx`,`cy`) aimed at (`tox`,`toy`) at `speed`. Normalises the direction
    /// vector directly (a `sqrt`) — no `atan2`, no angle round-trip.
    fn fire_aimed(&mut self, cx: Fixed, cy: Fixed, tox: Fixed, toy: Fixed, speed: Fixed) -> i32 {
        let dx = tox - cx;
        let dy = toy - cy;
        let len = (dx * dx + dy * dy).sqrt();
        if len.to_raw() == 0 {
            return self.fire_bullet(cx, cy, Fixed::from_raw(0), speed);
        }
        self.fire_bullet(cx, cy, speed * dx / len, speed * dy / len)
    }

    fn set_topdown_snap(&mut self, e: i32, mode: u8) {
        if let Some(s) = self.slot_of(e).filter(|&s| self.has(s, C_TOPDOWN)) {
            self.topdown[s].snap_mode = mode;
        }
    }

    /// Give an entity a native movement pattern (also ensures it has a `Body` to drive). `pattern`
    /// 0 = straight down at `base_vy`; 1 = weave (sideways triangle of amplitude `amp` over `period`
    /// frames while descending at `base_vy`).
    fn set_mover(&mut self, e: i32, pattern: u8, base_vy: Fixed, amp: Fixed, period: i32) {
        if let Some(s) = self.slot_of(e) {
            self.mover[s] = Mover {
                pattern,
                t: 0,
                base_vy,
                amp,
                period,
            };
            self.body[s] = Body {
                vx: Fixed::from_raw(0),
                vy: base_vy,
            };
            self.mask[s] |= C_MOVER | C_BODY;
            self.used |= C_MOVER | C_BODY;
        }
    }

    /// Movement-pattern system (pipeline phase 2, before `movement_system` integrates): drive each
    /// mover's `Body` velocity in pure Rust. No per-frame tish `tick` — a screen full of weaving
    /// enemies is as cheap as the native patrol AI. Off-screen movers are skipped like everything else.
    fn mover_system(&mut self) {
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_MOVER | C_BODY) || !self.is_active(s) {
                continue;
            }
            let m = self.mover[s];
            let t = m.t + 1;
            self.mover[s].t = t;
            let vx = if m.pattern == 1 {
                tri_fixed(t, m.period, m.amp)
            } else {
                Fixed::from_raw(0)
            };
            self.body[s].vx = vx;
            self.body[s].vy = m.base_vy;
        }
    }

    /// Lifetime system (pipeline phase 2): tick TTLs down and despawn anything that expired or (if
    /// flagged) has left the screen. Runs before collision so a spent bullet doesn't hit or render.
    /// Uses the same camera-relative bounds as culling, so it works with or without a scrolling camera
    /// (a fixed-screen shmup has `cam=(0,0)` ⇒ the plain 240×160 view plus the cull margin).
    fn life_system(&mut self) {
        let mut dead: Vec<i32> = Vec::new();
        let mut pooled: Vec<i32> = Vec::new();
        for s in 0..self.alive.len() {
            if !self.alive[s] || self.mask[s] & C_SLEEP != 0 || !self.has(s, C_LIFE) {
                continue;
            }
            let mut remove = false;
            if self.life[s].ttl > 0 {
                self.life[s].ttl -= 1;
                if self.life[s].ttl == 0 {
                    remove = true;
                }
            }
            if !remove && self.life[s].offscreen && self.has(s, C_TRANSFORM) && !self.on_screen(s) {
                remove = true;
            }
            // A flying hurt box (thrown star, shot) is stopped by the level: once the game has a
            // collision grid, retire it the frame its box overlaps a solid tile, so a projectile
            // can't fly through a dungeon wall into the next room. A shmup sets up no grid (every
            // cell would read solid), and a body hazard like a charging enemy has no `Body`.
            if !remove
                && self.grid_cols > 0
                && self.has(s, C_BODY | C_HURT | C_TRANSFORM | C_COLLIDER)
            {
                let (t, c) = (self.transform[s], self.collider[s]);
                if self.box_hits_solid(t.x, t.y, c.w, c.h) {
                    remove = true;
                }
            }
            // Room cutoff: doorway tiles are walkable, so the solid check above won't catch a star
            // sailing through an open door. With a room camera, retire any flying hurt box the
            // moment it leaves the player's current room — hard cutoff, no next-room hits.
            if !remove
                && self.room_cam.enabled
                && self.has(s, C_BODY | C_HURT | C_TRANSFORM)
                && !self.in_current_room(s)
            {
                remove = true;
            }
            if remove {
                // ⚠️ A POOLED ENTITY IS PARKED, NOT DESPAWNED. This one branch is why the pool needs
                // no ttl column and no system of its own: every rule above — the timer, off-screen,
                // a hurt box meeting a solid tile, the room cutoff — now retires a pool slot instead
                // of freeing its sprite VRAM and pushing the entity onto the free list, which is the
                // exact churn a pool exists to avoid.
                //
                // It also fixes something by arriving: the hand-rolled sub-weapon pools counted
                // their own ttl and never called `set_lifetime`, so `life_system` skipped them
                // entirely and their shots flew through walls into the next room.
                if self.pool_of[s] >= 0 {
                    pooled.push(self.pool_of[s]);
                } else {
                    dead.push(encode(s as u32, self.gen[s]));
                }
            }
        }
        for p in pooled {
            self.pool_retire_packed(p);
        }
        for e in dead {
            self.despawn(e);
        }
    }

    /// Combat system (pipeline phase 2): every hurt box vs every health entity carrying its target
    /// tag. On overlap: deal damage (honouring the victim's i-frames) and, for a bullet, despawn it.
    /// All native — a bullet never needs a tish callback, so a bullet-hell frame stays affordable.
    /// Death itself is flagged in `Health` and dispatched by the normal `collect_deaths` phase, so an
    /// enemy killed by a bullet still runs its tish `onDeath` (score, explosion) exactly once.
    fn combat_system(&mut self) {
        let n = self.alive.len();
        // Gather (victim, damage) hits and (bullet) despawns under an immutable read, then apply —
        // avoids aliasing `self` while iterating, and matches the deferred style of the other systems.
        let mut hits: Vec<(i32, i32, i32, i32)> = Vec::new(); // (victim id, damage, hurt cx, hurt cy)
        let mut consumed: Vec<i32> = Vec::new();
        // Health boxes (player, enemies, boss) are FEW; hurt boxes (bullets) can number in the
        // hundreds during bullet-hell. Collect the victim slots once so the inner test is
        // O(hurt × health) instead of O(hurt × N) — the whole-slab rescan was the cost that grew
        // as the level filled with bullets.
        let mut victims: Vec<usize> = Vec::new();
        for t in 0..n {
            // `is_active` matters twice here: a sleeping pooled unit still carries
            // C_HEALTH|C_TRANSFORM|C_COLLIDER, so without it a dormant slot is tested by every
            // hurt box AND can take damage while parked in the pool.
            if self.alive[t] && self.is_active(t) && self.has(t, C_HEALTH | C_TRANSFORM | C_COLLIDER)
                // Fully intangible right now (burrower underground, caster between appearances):
                // weapons pass straight through, they are not consumed. A merely PHASED entity
                // (drifter in flight) stays a victim — `damage()` refuses the hit, but a bullet
                // still pings off and is consumed.
                && self.mask2[t] & M2_HIDDEN == 0
            {
                victims.push(t);
            }
        }
        for h in 0..n {
            if !self.alive[h]
                || !self.has(h, C_HURT | C_TRANSFORM | C_COLLIDER)
                || !self.is_active(h)
            {
                continue;
            }
            // A hidden (submerged/teleported-away) entity deals no contact damage either.
            if self.mask2[h] & M2_HIDDEN != 0 {
                continue;
            }
            let hurt = self.hurt[h];
            for &t in &victims {
                if t == h {
                    continue;
                }
                if self.tag[t] != hurt.target_tag || !self.slots_overlap(h, t) {
                    continue;
                }
                // Hard room cutoff: a sword reach / shuriken / contact hurt must not land on an
                // entity whose focus sits in a different room (doorway edges + cull margin would
                // otherwise let next-room enemies take damage or deal it).
                if !self.same_room(h, t) {
                    continue;
                }
                let hcx = self.transform[h].x.floor() + self.collider[h].w.floor() / 2;
                let hcy = self.transform[h].y.floor() + self.collider[h].h.floor() / 2;
                // Blocked by a shield / armour facing the hit. The centre is already computed here
                // for the knockback shove, so the direction the blow came from costs nothing extra.
                // A blocked BULLET is still consumed — it hits the shield and stops, it does not
                // sail on through — while a blocked swing simply does nothing.
                if self.has(t, C_GUARD) && self.guard_blocks(t, hcx, hcy, hurt.despawn_on_hit) {
                    if hurt.despawn_on_hit {
                        consumed.push(encode(h as u32, self.gen[h]));
                    }
                    break;
                }
                // Immune to this WEAPON. Placed after the guard test so a directional shield still
                // wins first, and before `hits.push` so an immune target takes no damage, no
                // knockback and no stun — the original discards the whole interaction on an
                // immune hit. A bullet is still consumed: an arrow that pings off an armoured
                // knight stops, it does not sail onward.
                if hurt.damage_type != 0 && (self.immune[t] & hurt.damage_type) != 0 {
                    if hurt.despawn_on_hit {
                        consumed.push(encode(h as u32, self.gen[h]));
                    }
                    break;
                }
                // Weakness: when non-zero, ONLY matching `DMG_*` bits land (vulnerability mask —
                // the complement of `immune`). Untyped damage never matches a listed weakness.
                if self.weak[t] != 0
                    && (hurt.damage_type == 0 || (self.weak[t] & hurt.damage_type) == 0)
                {
                    if hurt.despawn_on_hit {
                        consumed.push(encode(h as u32, self.gen[h]));
                    }
                    break;
                }
                hits.push((encode(t as u32, self.gen[t]), hurt.damage, hcx, hcy));
                if hurt.stun > 0 {
                    self.stun[t] = self.stun[t].max(hurt.stun);
                }
                if hurt.despawn_on_hit {
                    consumed.push(encode(h as u32, self.gen[h]));
                }
                break; // one hurt box damages one victim per frame (non-piercing)
            }
        }
        for (victim, dmg, hcx, hcy) in hits {
            // Only a landed hit (not blocked by i-frames) shoves the victim — a top-down entity gets
            // knocked back away from the hurt box's centre (classic hit-reaction).
            if self.damage(victim, dmg) {
                if let Some(t) = self.slot_of(victim).filter(|&t| self.has(t, C_TOPDOWN)) {
                    let vcx = self.transform[t].x.floor() + self.collider[t].w.floor() / 2;
                    let vcy = self.transform[t].y.floor() + self.collider[t].h.floor() / 2;
                    self.topdown[t].kx = Fixed::from_raw((vcx - hcx).signum() * TD_KNOCK);
                    self.topdown[t].ky = Fixed::from_raw((vcy - hcy).signum() * TD_KNOCK);
                    self.topdown[t].knock = TD_KNOCK_FRAMES;
                }
            }
        }
        for bullet in consumed {
            self.despawn(bullet);
        }
    }

    /// Health system (pipeline phase 4): tick i-frames down and flicker the sprite while invincible.
    fn health_system(&mut self) {
        for s in 0..self.alive.len() {
            // Dormant first: the stun tick below sits BEFORE any component filter, so without this
            // a sleeping pool slot pays for stun bookkeeping and the i-frame sprite blink.
            if self.mask[s] & C_SLEEP != 0 {
                continue;
            }
            if self.alive[s] && self.stun[s] > 0 {
                self.stun[s] -= 1;
            }
            if !self.alive[s] || !self.has(s, C_HEALTH) {
                continue;
            }
            let inv = self.health[s].invuln;
            if inv > 0 {
                self.health[s].invuln = inv - 1;
            }
            if self.has(s, C_SPRITE) {
                let handle = self.sprite[s].handle;
                if handle >= 0 {
                    // hidden on alternating 4-frame spans while invincible, visible otherwise —
                    // and never visible while a nai state machine holds the entity M2_HIDDEN
                    // (submerged burrower, vanished caster), or the i-frame blink would undo it.
                    let vis = (inv <= 0 || (inv / 4) % 2 == 0) && self.mask2[s] & M2_HIDDEN == 0;
                    tish_agb::native_sprite_set_visible(handle, vis);
                }
            }
        }
    }

    /// Collect dead entities as `(onDeath, data, entity)` (clearing the dead flag) for the caller
    /// to dispatch after dropping the borrow. A `Null` callback means "no onDeath hook" → despawn.
    fn collect_deaths(&mut self) -> Vec<(Value, Value, i32)> {
        let mut out = Vec::new();
        for s in 0..self.alive.len() {
            if !self.alive[s]
                || self.mask[s] & C_SLEEP != 0
                || !self.has(s, C_HEALTH)
                || !self.health[s].dead
            {
                continue;
            }
            self.health[s].dead = false;
            // Part-death notification: a boss part carrying `set_death_note` reports its code to
            // the queue the parent's tish logic drains with `death_note()`. Capped so a runaway
            // cannot grow the heap; a boss has a handful of parts.
            if self.mask2[s] & M2_NOTE != 0 && self.death_notes.len() < 16 {
                self.death_notes.push(self.zx[s].code);
            }
            let entity = encode(s as u32, self.gen[s]);
            let (cb, data) = match &self.behaviour[s] {
                Some(b) => (self.defs[b.def].on_death.clone(), b.data.clone()),
                None => (Value::Null, Value::Null),
            };
            out.push((cb, data, entity));
        }
        out
    }

    // ── Native patrol AI ─────────────────────────────────────────────────────
    /// Enable native patrol on a platformer entity (walk + turn at walls/ledges), starting facing
    /// left. Runs in Rust every frame — no tish callback — so many on-screen enemies stay cheap.
    fn set_patrol(&mut self, e: i32, flip_mode: i32) {
        if let Some(s) = self.slot_of(e) {
            self.patrol[s] = Patrol {
                flip_mode,
                ..Patrol::default()
            };
            self.mask[s] |= C_PATROL;
            self.used |= C_PATROL;
        }
    }

    /// Patrol system (pipeline phase 2, before `platformer_system`): each on-screen patrol entity
    /// reverses at a wall (last frame's `blocked`) or a ledge (no ground on the tile ahead at foot
    /// level) while grounded, then feeds its direction to the platformer as this frame's move
    /// intent, and mirrors the sprite to match if the entity asked for it (`flip_mode`).
    fn patrol_system(&mut self) {
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_PATROL | C_PLATFORMER | C_TRANSFORM | C_COLLIDER) {
                continue;
            }
            if !self.is_active(s) {
                continue;
            }
            if self.stun[s] > 0 {
                // Stunned — or carried (a grab sets a long stun): AI suspended, matching every
                // other AI system's stun gate. Move intent is zeroed so the body stands still
                // instead of walking on with its last direction and no one steering.
                self.platformer[s].dir = 0;
                continue;
            }
            let dir = self.patrol[s].dir;
            // ⚠️ Only reverse while GROUNDED. An airborne probe (mid-fall, knocked back, spawned in
            // the air) sees no ground ahead at foot level, reads that as a ledge, and flips direction
            // EVERY frame — so the entity lands moving an arbitrary way. A flat-ground patroller is
            // unaffected (it is always grounded); anything that can leave the floor needed this.
            if !self.platformer[s].grounded {
                self.platformer[s].dir = dir; // keep walking the way it was already going
                continue;
            }
            let x = self.transform[s].x.floor();
            let y = self.transform[s].y.floor();
            let w = self.collider[s].w.floor();
            let h = self.collider[s].h.floor();
            let probe_x = if dir > 0 { x + w } else { x - 1 };
            let ahead_col = probe_x.div_euclid(TILE);
            let foot_row = (y + h).div_euclid(TILE);
            let ledge = !self.is_solid(ahead_col, foot_row);
            let dir = if self.platformer[s].blocked || ledge {
                -dir
            } else {
                dir
            };
            self.patrol[s].dir = dir;
            self.platformer[s].dir = dir; // move intent → platformer_system walks it that way

            // Mirror the sprite to match, only when the direction actually changed. This is here
            // rather than in the game because a tish `tick` that exists purely to call `setFlip`
            // reintroduces the whole per-enemy boxed-callback cost that `set_patrol` removes.
            let mode = self.patrol[s].flip_mode;
            if mode != 0 && self.patrol[s].flipped_for != dir && self.has(s, C_SPRITE) {
                self.patrol[s].flipped_for = dir;
                let handle = self.sprite[s].handle;
                if handle >= 0 {
                    let flip = if mode == 1 { dir > 0 } else { dir < 0 };
                    tish_agb::native_sprite_set_flip(handle, flip);
                }
            }
        }
    }

    /// Start a looping sprite-sheet animation on an entity: cycle `frames` frames,
    /// advancing one every `speed` game frames.
    fn set_anim(&mut self, e: i32, frames: i32, speed: i32) {
        // Convenience: loop the whole sheet from frame 0 (the common "spin these N frames").
        self.anim_play(e, 0, frames, speed, true);
    }

    /// Play the clip `[from, from+len)` at `speed` frames-per-step. Idempotent: calling it
    /// with the clip already playing does NOT restart it (so a controller can call it every
    /// frame while a state holds); a different `from`/`len` switches clips and shows the
    /// first frame at once.
    fn anim_play(&mut self, e: i32, from: i32, len: i32, speed: i32, looping: bool) {
        let Some(s) = self.slot_of(e) else {
            return;
        };
        let len = len.max(1);
        let same = {
            let a = &self.anim[s];
            self.has(s, C_ANIM) && a.playing && a.from == from && a.len == len
        };
        let a = &mut self.anim[s];
        a.from = from;
        a.len = len;
        a.speed = speed.max(1);
        a.looping = looping;
        a.playing = true;
        self.mask[s] |= C_ANIM;
        self.used |= C_ANIM;
        if !same {
            a.timer = 0;
            a.cur = 0;
            let handle = self.sprite[s].handle;
            if handle >= 0 {
                tish_agb::native_sprite_set_frame(handle, from);
            }
        }
    }

    fn set_walk(&mut self, e: i32, cols: i32, speed: i32) {
        if let Some(s) = self.slot_of(e) {
            self.walk[s] = Walk {
                cols: cols.max(1),
                speed: speed.max(1),
                timer: 0,
                phase: false,
            };
            self.mask[s] |= C_WALK;
            self.used |= C_WALK;
        }
    }

    /// The entity's grid facing as a direction code: 0 down, 1 up, 2 left, 3 right (a
    /// tish animation controller maps this + movement to a clip). Default down.
    /// The entity's current grid tile column/row (its logical tile, not the pixel transform).
    /// `-1` if the entity has no grid position — a sentinel no teleporter/tile check will match.
    fn grid_col(&self, e: i32) -> i32 {
        self.slot_of(e)
            .filter(|&s| self.has(s, C_GRIDPOS))
            .map(|s| self.gridpos[s].col)
            .unwrap_or(-1)
    }
    fn grid_row(&self, e: i32) -> i32 {
        self.slot_of(e)
            .filter(|&s| self.has(s, C_GRIDPOS))
            .map(|s| self.gridpos[s].row)
            .unwrap_or(-1)
    }

    fn grid_facing(&self, e: i32) -> i32 {
        if let Some(s) = self.slot_of(e) {
            if self.has(s, C_GRIDPOS) {
                let g = &self.gridpos[s];
                return if g.fy > 0 {
                    0
                } else if g.fy < 0 {
                    1
                } else if g.fx < 0 {
                    2
                } else {
                    3
                };
            }
        }
        0
    }

    /// Animation system (pure Rust): advance each playing animation's frame and push it
    /// to the entity's tish-agb sprite.
    fn anim_system(&mut self) {
        for s in 0..self.alive.len() {
            if !self.alive[s]
                || self.mask[s] & C_SLEEP != 0
                || !self.has(s, C_ANIM | C_SPRITE)
                || !self.anim[s].playing
            {
                continue;
            }
            if !self.is_active(s) {
                continue; // off-screen — no need to advance animation
            }
            let a = &mut self.anim[s];
            a.timer += 1;
            if a.timer >= a.speed {
                a.timer = 0;
                a.cur += 1;
                if a.cur >= a.len {
                    if a.looping {
                        a.cur = 0;
                    } else {
                        a.cur = a.len - 1;
                        a.playing = false;
                    }
                }
                let frame = a.from + a.cur;
                let handle = self.sprite[s].handle;
                if handle >= 0 {
                    tish_agb::native_sprite_set_frame(handle, frame);
                }
            }
        }
    }

    /// Directional walk animation: choose the sprite frame + horizontal flip from each
    /// walking entity's `GridPos` facing and movement. Standing shows the row's neutral
    /// column; moving toggles the two step columns at `speed`.
    fn walk_system(&mut self) {
        for s in 0..self.alive.len() {
            if !self.alive[s] || !self.has(s, C_WALK | C_GRIDPOS | C_SPRITE) {
                continue;
            }
            if !self.is_active(s) {
                continue;
            }
            let (fx, fy, moving) = {
                let g = &self.gridpos[s];
                (g.fx, g.fy, g.moving)
            };
            // Facing → row (down / up / side) and flip (right reuses the side row).
            let (row, flip) = if fy > 0 {
                (0, false) // down
            } else if fy < 0 {
                (1, false) // up
            } else if fx < 0 {
                (2, false) // left
            } else if fx > 0 {
                (2, true) // right
            } else {
                (0, false) // default: face down
            };
            let cols = self.walk[s].cols.max(1);
            let col = if moving {
                let w = &mut self.walk[s];
                w.timer += 1;
                if w.timer >= w.speed {
                    w.timer = 0;
                    w.phase = !w.phase;
                }
                if w.phase {
                    2
                } else {
                    0
                }
            } else {
                let w = &mut self.walk[s];
                w.timer = 0;
                w.phase = false;
                1 // standing
            };
            let frame = row * cols + col.min(cols - 1);
            let handle = self.sprite[s].handle;
            if handle >= 0 {
                tish_agb::native_sprite_set_frame(handle, frame);
                tish_agb::native_sprite_set_flip(handle, flip);
            }
        }
    }

    /// Render system (pure Rust): drive tish-agb's sprites (no boxing per sprite).
    fn render_system(&mut self) {
        // (sleeping slots are skipped below via the mask test)
        for s in 0..self.alive.len() {
            // A sleeping slot is parked off-map with a hidden sprite; there is nothing to draw and
            // nothing to compute a screen position for.
            if self.alive[s] && self.mask[s] & C_SLEEP == 0 && self.has(s, C_TRANSFORM | C_SPRITE) {
                let h = self.sprite[s].handle;
                if h < 0 {
                    continue;
                }
                // Off-screen sprites release their VRAM `Object` so a large scrolling level with many
                // entities holds a sprite allocation only for what's on screen (GBA sprite VRAM is
                // small); they rebuild when they scroll back in. `is_active` keeps the camera target
                // (and every entity in a non-scrolling game) resident, so small scenes are unchanged.
                if self.is_active(s) {
                    tish_agb::native_sprite_restore(h);
                    tish_agb::native_sprite_set_pos(
                        h,
                        self.transform[s].x.floor() + self.sprite[s].ox,
                        self.transform[s].y.floor() + self.sprite[s].oy,
                    );
                } else {
                    tish_agb::native_sprite_release(h);
                }
            }
        }
    }

    fn set_camera_target(&mut self, e: i32) {
        self.camera_target = Some(e);
        self.room_cam.enabled = false;
    }

    /// Enable the room-locked camera following `e`, with rooms `room_w`×`room_h` tiles
    /// (0 → the screen default 15×10). The starting room is taken from the entity's current tile.
    fn set_room_camera(&mut self, e: i32, room_w: i32, room_h: i32) {
        self.camera_target = Some(e);
        let rw = if room_w > 0 { room_w } else { 15 };
        let rh = if room_h > 0 { room_h } else { 10 };
        // Starting room: from the grid tile (RPG) or, for a free-moving platformer, the box
        // centre's pixel position.
        let (rx, ry) = match self.slot_of(e) {
            Some(s) if self.has(s, C_GRIDPOS) => {
                (self.gridpos[s].col / rw, self.gridpos[s].row / rh)
            }
            Some(s) if self.has(s, C_TRANSFORM) => {
                let (cx, cy) = self.camera_focus(s);
                (cx.div_euclid(rw * TILE), cy.div_euclid(rh * TILE))
            }
            _ => (0, 0),
        };
        self.room_cam.enabled = true;
        self.room_cam.room_w = rw;
        self.room_cam.room_h = rh;
        self.room_cam.cur_rx = rx;
        self.room_cam.cur_ry = ry;
        self.room_cam.transitioning = false;
    }

    /// Centre the camera on the target entity, clamped so it never scrolls past the map
    /// edge (and stays at 0 on an axis where the map is smaller than the screen). Pushes the
    /// result to tish-agb, which scrolls the streamed layers and offsets the sprites.
    fn update_camera(&mut self) {
        // Room camera: lock to the current room, or interpolate across rooms mid-slide.
        if self.room_cam.enabled {
            let rc = &self.room_cam;
            let (cx, cy) = if rc.transitioning {
                (
                    rc.from_cam.0 + (rc.to_cam.0 - rc.from_cam.0) * rc.timer / rc.dur,
                    rc.from_cam.1 + (rc.to_cam.1 - rc.from_cam.1) * rc.timer / rc.dur,
                )
            } else {
                (rc.cur_rx * rc.room_w * TILE, rc.cur_ry * rc.room_h * TILE)
            };
            self.cam_x = cx;
            self.cam_y = cy;
            tish_agb::native_camera_set(cx, cy);
            return;
        }
        let Some(e) = self.camera_target else { return };
        let Some(s) = self.slot_of(e) else { return };
        if !self.has(s, C_TRANSFORM) {
            return;
        }
        let (px, py) = (self.transform[s].x.floor(), self.transform[s].y.floor());
        let max_x = (self.grid_cols * TILE - 240).max(0);
        let max_y = (self.grid_rows * TILE - 160).max(0);
        let cx = (px + TILE / 2 - 120).clamp(0, max_x);
        let cy = (py + TILE / 2 - 80).clamp(0, max_y);
        self.cam_x = cx;
        self.cam_y = cy;
        tish_agb::native_camera_set(cx, cy);
    }

    /// Is slot `s`'s box within the camera view (plus `CULL_MARGIN`)? Entities with no transform
    /// count as on-screen (nothing to place them off it).
    fn on_screen(&self, s: usize) -> bool {
        if !self.has(s, C_TRANSFORM) {
            return true;
        }
        let x = self.transform[s].x.floor();
        let y = self.transform[s].y.floor();
        let (w, h) = if self.has(s, C_COLLIDER) {
            (
                self.collider[s].w.floor().max(1),
                self.collider[s].h.floor().max(1),
            )
        } else {
            (TILE, TILE)
        };
        let m = CULL_MARGIN;
        x + w >= self.cam_x - m
            && x <= self.cam_x + 240 + m
            && y + h >= self.cam_y - m
            && y <= self.cam_y + 160 + m
    }

    /// Should slot `s` run its game loop this frame? The camera target and, for a non-scrolling
    /// game (no camera), everything is always active; otherwise off-screen entities are culled.
    fn is_active(&self, s: usize) -> bool {
        // A dormant pool slot is inert everywhere, whatever the camera is doing.
        if self.mask[s] & C_SLEEP != 0 {
            return false;
        }
        match self.camera_target {
            // No camera target: the game drives its own camera, and everything stays active.
            //
            // This looks like a missed optimisation — an RTS with a unit pool pays full per-entity
            // cost for parked slots — and an opt-in `set_cull_offscreen` was built and measured to
            // close it. It is not a missed optimisation: an RTS army ordered across the map walks
            // off screen and MUST keep walking, and with culling on it simply stopped. The saving
            // was ~600 ticks and the cost was the game. Left here so the next person does not spend
            // the afternoon rediscovering it.
            None => true,
            Some(e) if self.slot_of(e) == Some(s) => true,
            _ => self.on_screen(s),
        }
    }

    /// The pixel point the camera aims at for slot `s`: its collider box centre, or the bare
    /// transform when it has no collider.
    fn camera_focus(&self, s: usize) -> (i32, i32) {
        let (bx, by) = if self.has(s, C_COLLIDER) {
            (
                self.collider[s].w.floor() / 2,
                self.collider[s].h.floor() / 2,
            )
        } else {
            (0, 0)
        };
        (
            self.transform[s].x.floor() + bx,
            self.transform[s].y.floor() + by,
        )
    }

    /// Room-grid coords `(rx, ry)` for slot `s` from its focus point. Doorway tiles sit on a
    /// room edge; the focus decides which room owns the entity for combat / interact gating.
    fn room_of(&self, s: usize) -> (i32, i32) {
        let (rw, rh) = (self.room_cam.room_w, self.room_cam.room_h);
        let (cx, cy) = self.camera_focus(s);
        (cx.div_euclid(rw * TILE), cy.div_euclid(rh * TILE))
    }

    /// Hard room cutoff: with a room camera, two entities only interact (hurt, collide, talk)
    /// while they share a room. Off / no room-cam → always true. Mid room-slide → nothing
    /// interacts (the world is frozen for the pan).
    fn same_room(&self, a: usize, b: usize) -> bool {
        if !self.room_cam.enabled {
            return true;
        }
        if self.room_cam.transitioning {
            return false;
        }
        self.room_of(a) == self.room_of(b)
    }

    /// Is slot `s` in the player's locked room? Flying hurt boxes (stars, shots) that leave it
    /// are retired so a projectile can't sail through an open doorway and hit the next room.
    fn in_current_room(&self, s: usize) -> bool {
        if !self.room_cam.enabled {
            return true;
        }
        if self.room_cam.transitioning {
            return false;
        }
        let (rx, ry) = self.room_of(s);
        rx == self.room_cam.cur_rx && ry == self.room_cam.cur_ry
    }

    /// Nearest OTHER entity with `tag` within `radius` px (manhattan, box centers), or -1.
    /// Deliberately NOT gated on `is_active`: simulation entities keep mattering off screen.
    /// O(n) scan — callers stagger queries (every 8–16 frames, cache the id) rather than asking
    /// for every entity every frame.
    fn nearest_tag(&self, e: i32, tag: i32, radius: i32) -> i32 {
        let Some(s) = self.slot_of(e) else {
            return -1;
        };
        let (cx, cy) = self.center_of(s);
        let mut best = -1;
        let mut best_d = i32::MAX;
        for c in 0..self.alive.len() {
            if c == s || !self.alive[c] || self.tag[c] != tag || !self.has(c, C_TRANSFORM) {
                continue;
            }
            if !self.same_room(s, c) {
                continue;
            }
            let (ox, oy) = self.center_of(c);
            let d = (ox - cx).to_raw().abs() + (oy - cy).to_raw().abs();
            if d < best_d {
                best_d = d;
                best = encode(c as u32, self.gen[c]);
            }
        }
        if best_d <= radius.saturating_mul(256) {
            best
        } else {
            -1
        }
    }

    /// Manhattan distance between two entities' box centers in whole px, or -1 if either is gone.
    fn entity_dist(&self, a: i32, b: i32) -> i32 {
        match (self.slot_of(a), self.slot_of(b)) {
            (Some(sa), Some(sb)) => {
                let (ax, ay) = self.center_of(sa);
                let (bx, by) = self.center_of(sb);
                ((ax - bx).to_raw().abs() + (ay - by).to_raw().abs()) >> 8
            }
            _ => -1,
        }
    }

    /// For a free-moving (platformer) camera target, start a room slide when its box centre
    /// crosses into a new room. Grid entities trigger from `grid_step` instead; this is the
    /// side-scrolling counterpart. Runs each frame before `room_transition_system`.
    fn room_track_free(&mut self) {
        if !self.room_cam.enabled || self.room_cam.transitioning {
            return;
        }
        let Some(e) = self.camera_target else { return };
        let Some(s) = self.slot_of(e) else { return };
        if self.has(s, C_GRIDPOS) || !self.has(s, C_TRANSFORM) {
            return;
        }
        let (rw, rh) = (self.room_cam.room_w, self.room_cam.room_h);
        let (cx, cy) = self.camera_focus(s);
        let (nrx, nry) = (cx.div_euclid(rw * TILE), cy.div_euclid(rh * TILE));
        if (nrx, nry) != (self.room_cam.cur_rx, self.room_cam.cur_ry) {
            self.begin_room_transition_free(s, nrx, nry);
        }
    }

    /// Slide the camera one room over while the player holds still: `from_px == to_px`, so
    /// `room_transition_system` pins it at its current pixel for the slide (input is locked by
    /// `input_locked`). The metroidvania-style "walk through the door, screen pans" transition.
    fn begin_room_transition_free(&mut self, s: usize, nrx: i32, nry: i32) {
        let rc = &self.room_cam;
        let (rw, rh) = (rc.room_w, rc.room_h);
        let from_cam = (rc.cur_rx * rw * TILE, rc.cur_ry * rh * TILE);
        let to_cam = (nrx * rw * TILE, nry * rh * TILE);
        let px = (self.transform[s].x.floor(), self.transform[s].y.floor());
        let dx = nrx - rc.cur_rx;
        let dy = nry - rc.cur_ry;
        let rc = &mut self.room_cam;
        rc.cur_rx = nrx;
        rc.cur_ry = nry;
        rc.transitioning = true;
        rc.timer = 0;
        rc.from_cam = from_cam;
        rc.to_cam = to_cam;
        rc.from_px = px;
        rc.to_px = (px.0 + dx * 24, px.1 + dy * 24);
    }

    // ── Grid / RPG genre ─────────────────────────────────────────────────────
    /// Is tile `(col,row)` solid? Out-of-bounds is solid (the map edge blocks).
    fn is_solid(&self, col: i32, row: i32) -> bool {
        if col < 0 || row < 0 || col >= self.grid_cols || row >= self.grid_rows {
            return true;
        }
        let i = (row * self.grid_cols + col) as usize;
        grid_bit(&self.solid, i)
    }

    /// Is tile `(col,row)` a one-way platform? Out-of-bounds is never one-way.
    fn is_oneway(&self, col: i32, row: i32) -> bool {
        if col < 0 || row < 0 || col >= self.grid_cols || row >= self.grid_rows {
            return false;
        }
        if self.oneway.is_empty() {
            return false;
        }
        let i = (row * self.grid_cols + col) as usize;
        grid_bit(&self.oneway, i)
    }

    /// Is tile `(col,row)` climbable (a ladder / vine / rope)? Out-of-bounds is never climbable.
    fn is_ladder(&self, col: i32, row: i32) -> bool {
        if col < 0 || row < 0 || col >= self.grid_cols || row >= self.grid_rows {
            return false;
        }
        if self.ladder.is_empty() {
            return false;
        }
        let i = (row * self.grid_cols + col) as usize;
        grid_bit(&self.ladder, i)
    }

    fn grid_setup(&mut self, cols: i32, rows: i32) {
        self.grid_cols = cols.max(0);
        self.grid_rows = rows.max(0);
        let n = (self.grid_cols * self.grid_rows) as usize;
        self.grid_cells = n;
        let bytes = grid_bit_bytes(n);
        // Grow-only bitplanes: never free the overworld-sized buffer when entering a small
        // cave (and never re-allocate 19KB on return — that OOMs after heap fragmentation).
        if bytes > self.solid.capacity() {
            self.solid = alloc::vec![0u8; bytes];
        } else {
            self.solid.resize(bytes, 0);
            self.solid.fill(0);
        }
        // Oneway stays empty until first grid_set_oneway (topdown games never touch it).
        if !self.oneway.is_empty() {
            if bytes > self.oneway.capacity() {
                self.oneway = alloc::vec![0u8; bytes];
            } else {
                self.oneway.resize(bytes, 0);
                self.oneway.fill(0);
            }
        }
        // Same deal for ladders — only a game that has placed one carries the plane.
        if !self.ladder.is_empty() {
            if bytes > self.ladder.capacity() {
                self.ladder = alloc::vec![0u8; bytes];
            } else {
                self.ladder.resize(bytes, 0);
                self.ladder.fill(0);
            }
        }
    }

    fn grid_set_solid(&mut self, col: i32, row: i32, solid: bool) {
        if col >= 0 && row >= 0 && col < self.grid_cols && row < self.grid_rows {
            let i = (row * self.grid_cols + col) as usize;
            grid_bit_set(&mut self.solid, i, solid);
        }
    }

    fn grid_set_oneway(&mut self, col: i32, row: i32, on: bool) {
        if col >= 0 && row >= 0 && col < self.grid_cols && row < self.grid_rows {
            let bytes = grid_bit_bytes(self.grid_cells);
            if self.oneway.len() < bytes {
                self.oneway.resize(bytes, 0);
            }
            let i = (row * self.grid_cols + col) as usize;
            grid_bit_set(&mut self.oneway, i, on);
        }
    }

    fn grid_set_ladder(&mut self, col: i32, row: i32, on: bool) {
        if col >= 0 && row >= 0 && col < self.grid_cols && row < self.grid_rows {
            let bytes = grid_bit_bytes(self.grid_cells);
            if self.ladder.len() < bytes {
                self.ladder.resize(bytes, 0);
            }
            let i = (row * self.grid_cols + col) as usize;
            grid_bit_set(&mut self.ladder, i, on);
        }
    }

    /// Place an entity on the grid at tile `(col,row)`: sets `GridPos` + `Transform`
    /// (pixel = tile·TILE) and marks it grid-controlled. Default facing = down.
    fn attach_grid(&mut self, e: i32, col: i32, row: i32) {
        if let Some(s) = self.slot_of(e) {
            let px = Fixed::from_raw(col * TILE * 256);
            let py = Fixed::from_raw(row * TILE * 256);
            self.gridpos[s] = GridPos {
                col,
                row,
                moving: false,
                tx: px,
                ty: py,
                fx: 0,
                fy: 1,
            };
            self.transform[s] = Transform { x: px, y: py };
            self.mask[s] |= C_GRIDPOS | C_TRANSFORM;
            self.used |= C_GRIDPOS | C_TRANSFORM;
        }
    }

    /// Request a tile step in direction `(dx,dy)` (4-directional; horizontal wins if
    /// both). Always faces that way; only actually steps if idle and the target tile
    /// isn't solid. The grid system then slides the transform over several frames.
    fn grid_step(&mut self, e: i32, dx: i32, dy: i32) {
        // Room camera locks player input while a screen-slide transition plays.
        if self.room_cam.enabled && self.room_cam.transitioning && self.camera_target == Some(e) {
            return;
        }
        let Some(s) = self.slot_of(e) else {
            return;
        };
        if !self.has(s, C_GRIDPOS) {
            return;
        }
        let (sdx, sdy) = if dx != 0 {
            (dx.signum(), 0)
        } else if dy != 0 {
            (0, dy.signum())
        } else {
            return;
        };
        let (col, row, moving) = {
            let g = &mut self.gridpos[s];
            g.fx = sdx;
            g.fy = sdy;
            (g.col, g.row, g.moving)
        };
        if moving {
            return;
        }
        let (tc, tr) = (col + sdx, row + sdy);
        if self.is_solid(tc, tr) || self.tile_occupied(tc, tr, s) {
            return;
        }
        // Room camera: stepping across a room boundary starts a screen-slide instead of a step —
        // the player's tile updates now (so they're logically in the new room) but the visual slide
        // of both player and camera is driven by `room_transition_system`.
        if self.room_cam.enabled && self.camera_target == Some(e) {
            let (rw, rh) = (self.room_cam.room_w, self.room_cam.room_h);
            let (old_rx, old_ry) = (col / rw, row / rh);
            let (new_rx, new_ry) = (tc / rw, tr / rh);
            if (new_rx, new_ry) != (old_rx, old_ry) {
                self.begin_room_transition(s, tc, tr, new_rx, new_ry);
                return;
            }
        }
        let g = &mut self.gridpos[s];
        g.col = tc;
        g.row = tr;
        g.tx = Fixed::from_raw(tc * TILE * 256);
        g.ty = Fixed::from_raw(tr * TILE * 256);
        g.moving = true;
    }

    /// Kick off a room-to-room screen slide: the player's tile jumps to (tc,tr) in the new room,
    /// and `room_transition_system` interpolates both the camera (one full room over) and the
    /// player's pixel position (one tile) over `dur` frames while input stays locked.
    fn begin_room_transition(&mut self, s: usize, tc: i32, tr: i32, new_rx: i32, new_ry: i32) {
        let rc = &self.room_cam;
        let (rw, rh) = (rc.room_w, rc.room_h);
        let from_cam = (rc.cur_rx * rw * TILE, rc.cur_ry * rh * TILE);
        let to_cam = (new_rx * rw * TILE, new_ry * rh * TILE);
        let from_px = (self.gridpos[s].tx.floor(), self.gridpos[s].ty.floor());
        let to_px = (tc * TILE, tr * TILE);
        // logical tile is the new room's entry tile immediately
        let g = &mut self.gridpos[s];
        g.col = tc;
        g.row = tr;
        g.tx = Fixed::from_raw(tc * TILE * 256);
        g.ty = Fixed::from_raw(tr * TILE * 256);
        g.moving = false;
        let rc = &mut self.room_cam;
        rc.cur_rx = new_rx;
        rc.cur_ry = new_ry;
        rc.transitioning = true;
        rc.timer = 0;
        rc.from_cam = from_cam;
        rc.to_cam = to_cam;
        rc.from_px = from_px;
        rc.to_px = to_px;
    }

    /// Advance an in-progress room slide one frame: lerp the player's pixel transform toward the
    /// new-room entry tile (the camera lerp is computed in `update_camera` from the same timer),
    /// and finalise on arrival. Runs before `render_system` so the sprite draws at the slid pos.
    fn room_transition_system(&mut self) {
        if !self.room_cam.enabled || !self.room_cam.transitioning {
            return;
        }
        let Some(e) = self.camera_target else { return };
        let Some(s) = self.slot_of(e) else { return };
        let rc = &mut self.room_cam;
        rc.timer += 1;
        let done = rc.timer >= rc.dur;
        let (px, py) = if done {
            rc.transitioning = false;
            rc.to_px
        } else {
            (
                rc.from_px.0 + (rc.to_px.0 - rc.from_px.0) * rc.timer / rc.dur,
                rc.from_px.1 + (rc.to_px.1 - rc.from_px.1) * rc.timer / rc.dur,
            )
        };
        self.transform[s].x = Fixed::from_raw(px * 256);
        self.transform[s].y = Fixed::from_raw(py * 256);
    }

    /// Is tile `(col,row)` claimed by a grid entity other than slot `except`? (Entities
    /// block each other — you can't step onto an NPC's tile, so you face it instead.)
    fn tile_occupied(&self, col: i32, row: i32, except: usize) -> bool {
        for o in 0..self.alive.len() {
            if o != except
                && self.alive[o]
                && self.has(o, C_GRIDPOS)
                && self.gridpos[o].col == col
                && self.gridpos[o].row == row
            {
                return true;
            }
        }
        false
    }

    /// Grid system: slide any mid-step entity's transform toward its target tile.
    fn grid_system(&mut self) {
        let speed = Fixed::from_raw(GRID_SPEED * 256);
        for s in 0..self.alive.len() {
            if !self.alive[s]
                || self.mask[s] & C_SLEEP != 0
                || !self.has(s, C_GRIDPOS)
                || !self.gridpos[s].moving
            {
                continue;
            }
            let (tx, ty) = (self.gridpos[s].tx, self.gridpos[s].ty);
            let ax = approach(&mut self.transform[s].x, tx, speed);
            let ay = approach(&mut self.transform[s].y, ty, speed);
            if ax && ay {
                self.gridpos[s].moving = false;
            }
        }
    }

    /// The entity the faced tile holds that has an `onInteract` — gathered as
    /// `(callback, target_data, target_entity, actor_entity)` for reentrancy-safe dispatch.
    fn collect_interact(&self, e: i32) -> Option<(Value, Value, i32, i32)> {
        let s = self.slot_of(e)?;
        if !self.has(s, C_GRIDPOS) {
            return None;
        }
        let g = self.gridpos[s];
        let (tc, tr) = (g.col + g.fx, g.row + g.fy);
        for o in 0..self.alive.len() {
            if o == s || !self.alive[o] || !self.has(o, C_GRIDPOS) {
                continue;
            }
            if self.gridpos[o].col == tc && self.gridpos[o].row == tr {
                if let Some(b) = &self.behaviour[o] {
                    let cb = self.defs[b.def].on_interact.clone();
                    if !matches!(cb, Value::Null) {
                        let target = encode(o as u32, self.gen[o]);
                        return Some((cb, b.data.clone(), target, e));
                    }
                }
            }
        }
        None
    }

    /// Top-down interact probe: the strip of ground `reach` px deep in front of the entity (by its
    /// top-down facing, as wide as its own box); if an entity overlapping it defines `onInteract`,
    /// return the (cb, data, target, actor) to fire. Mirrors `collect_interact` for the
    /// free-movement genre ("talk to the NPC" / "open the chest").
    ///
    /// A strip, not the single point this used to test: pressed up against a 14px NPC the point
    /// `reach` px out already sat PAST it, so the talk silently missed — and with one context button
    /// (talk-or-attack) a miss means swinging your sword at the person you meant to greet.
    fn collect_topdown_interact(&self, e: i32, reach: i32) -> Option<(Value, Value, i32, i32)> {
        let s = self.slot_of(e)?;
        if !self.has(s, C_TOPDOWN | C_TRANSFORM | C_COLLIDER) {
            return None;
        }
        let ax = self.transform[s].x.floor();
        let ay = self.transform[s].y.floor();
        let cw = self.collider[s].w.floor();
        let ch = self.collider[s].h.floor();
        let r = reach.max(1);
        let (px, py, pw, ph) = match self.topdown[s].facing {
            1 => (ax, ay - r, cw, r),
            2 => (ax - r, ay, r, ch),
            3 => (ax + cw, ay, r, ch),
            _ => (ax, ay + ch, cw, r),
        };
        for o in 0..self.alive.len() {
            if o == s
                || !self.alive[o]
                || !self.has(o, C_TRANSFORM | C_COLLIDER)
                || self.behaviour[o].is_none()
            {
                continue;
            }
            // Room cutoff: talking / opening never reaches into the next room through a doorway.
            if !self.same_room(s, o) {
                continue;
            }
            let ox = self.transform[o].x.floor();
            let oy = self.transform[o].y.floor();
            let ow = self.collider[o].w.floor();
            let oh = self.collider[o].h.floor();
            if px < ox + ow && ox < px + pw && py < oy + oh && oy < py + ph {
                let b = self.behaviour[o].as_ref().unwrap();
                let cb = self.defs[b.def].on_interact.clone();
                if !matches!(cb, Value::Null) {
                    return Some((cb, b.data.clone(), encode(o as u32, self.gen[o]), e));
                }
            }
        }
        None
    }

    /// Side-scrolling counterpart of `collect_topdown_interact`: probe the column of space `reach`
    /// px wide beside the entity, on the side it is FACING, over its own box height; if something
    /// there defines `onInteract`, return the (cb, data, target, actor) to fire.
    ///
    /// A platformer has no `TopDown.facing`, and its `Platformer.dir` is zeroed the moment the
    /// d-pad is released — you talk to an NPC while standing still, so the probe reads
    /// `Platformer.face` (the last direction actually moved) instead.
    ///
    /// The probe is deliberately TALLER than the box: `PF_INTERACT_PAD` px of slack above and below
    /// so you can talk to an NPC standing on a step, or one whose art sits on a different hitbox
    /// height, without having to line the boxes up exactly.
    fn collect_platformer_interact(&self, e: i32, reach: i32) -> Option<(Value, Value, i32, i32)> {
        let s = self.slot_of(e)?;
        if !self.has(s, C_PLATFORMER | C_TRANSFORM | C_COLLIDER) {
            return None;
        }
        let ax = self.transform[s].x.floor();
        let ay = self.transform[s].y.floor();
        let cw = self.collider[s].w.floor();
        let ch = self.collider[s].h.floor();
        let r = reach.max(1);
        // The strip covers our OWN box as well as `reach` px beyond the facing edge. Probing only
        // the space in front looks right on paper and fails in practice for the two commonest cases:
        // a doorway is a cell you STAND IN, and an NPC you have walked all the way up to overlaps
        // you rather than sitting beside you. Both would answer "there is nothing here".
        let px = if self.platformer[s].face < 0 {
            ax - r
        } else {
            ax
        };
        let pw = cw + r;
        let py = ay - PF_INTERACT_PAD;
        let ph = ch + 2 * PF_INTERACT_PAD;
        for o in 0..self.alive.len() {
            if o == s
                || !self.alive[o]
                || !self.has(o, C_TRANSFORM | C_COLLIDER)
                || self.behaviour[o].is_none()
            {
                continue;
            }
            if !self.same_room(s, o) {
                continue;
            }
            let ox = self.transform[o].x.floor();
            let oy = self.transform[o].y.floor();
            let ow = self.collider[o].w.floor();
            let oh = self.collider[o].h.floor();
            if px < ox + ow && ox < px + pw && py < oy + oh && oy < py + ph {
                let b = self.behaviour[o].as_ref().unwrap();
                let cb = self.defs[b.def].on_interact.clone();
                if !matches!(cb, Value::Null) {
                    return Some((cb, b.data.clone(), encode(o as u32, self.gen[o]), e));
                }
            }
        }
        None
    }

    /// Do entity slots `a` and `b` (both `TRANSFORM | COLLIDER`) overlap? AABB test in
    /// fixed-point, each box top-left at its transform.
    fn slots_overlap(&self, a: usize, b: usize) -> bool {
        let (ta, ca) = (self.transform[a], self.collider[a]);
        let (tb, cb) = (self.transform[b], self.collider[b]);
        ta.x < tb.x + cb.w && tb.x < ta.x + ca.w && ta.y < tb.y + cb.h && tb.y < ta.y + ca.h
    }

    /// Whether two entity ids currently overlap (for the `overlaps(a, b)` query). `false`
    /// if either lacks a collider or is stale.
    fn entities_overlap(&self, ea: i32, eb: i32) -> bool {
        match (self.slot_of(ea), self.slot_of(eb)) {
            (Some(a), Some(b))
                if self.has(a, C_TRANSFORM | C_COLLIDER)
                    && self.has(b, C_TRANSFORM | C_COLLIDER) =>
            {
                self.slots_overlap(a, b)
            }
            _ => false,
        }
    }

    /// Collision detection: find every overlapping pair among collidable entities
    /// (O(n²) — a spatial hash is the later optimization) and, for each side that has
    /// an `onCollide` behaviour, gather `(callback, me_data, self_entity, other_entity)`.
    /// Gathered under the world borrow; the caller invokes them after dropping it
    /// (reentrancy-safe, like `collect_behaviours`).
    fn collect_collisions(&self) -> Vec<(Value, Value, i32, i32)> {
        let mut out = Vec::new();
        let n = self.alive.len();
        // Only entities whose component defines `onCollide` need collision EVENTS — usually just the
        // player. Colliders, by contrast, are many (every bullet carries one for the hurt system). The
        // old all-pairs loop tested every bullet against every other bullet (O(n²)) to discover neither
        // responds — the cost that grew as the screen filled with shots. Iterate responders × colliders
        // instead (O(responders·n)); each responder still fires its own `onCollide` for every overlap,
        // and firing only the responder's side (not both) avoids the double-dispatch the pair loop
        // needed care to avoid, with identical semantics.
        for a in 0..n {
            if !self.alive[a] || !self.has(a, C_TRANSFORM | C_COLLIDER) || !self.is_active(a) {
                continue;
            }
            let responds = match &self.behaviour[a] {
                Some(b) => !matches!(self.defs[b.def].on_collide, Value::Null),
                None => false,
            };
            if !responds {
                continue;
            }
            let ea = encode(a as u32, self.gen[a]);
            for b in 0..n {
                if b == a
                    || !self.alive[b]
                    || !self.has(b, C_TRANSFORM | C_COLLIDER)
                    || !self.is_active(b)
                {
                    continue;
                }
                if !self.slots_overlap(a, b) {
                    continue;
                }
                // Room cutoff: onCollide (pickup, trigger) never fires across a room boundary.
                if !self.same_room(a, b) {
                    continue;
                }
                let eb = encode(b as u32, self.gen[b]);
                self.push_on_collide(&mut out, a, ea, eb);
            }
        }
        out
    }

    fn push_on_collide(
        &self,
        out: &mut Vec<(Value, Value, i32, i32)>,
        slot: usize,
        me_entity: i32,
        other_entity: i32,
    ) {
        if let Some(b) = &self.behaviour[slot] {
            let cb = self.defs[b.def].on_collide.clone();
            if !matches!(cb, Value::Null) {
                out.push((cb, b.data.clone(), me_entity, other_entity));
            }
        }
    }
}

static WORLD: SingleCore<RefCell<World>> = SingleCore::new(RefCell::new(World::new()));

fn with_world<R>(f: impl FnOnce(&mut World) -> R) -> R {
    WORLD.with(|c| f(&mut c.borrow_mut()))
}

/// Read a tish array of numbers into a plain Vec. Used only at load time — passing a typed module
/// array to a native de-optimises every other read of it (see examples/probe-arrayarg), so this
/// crosses the boundary once per mission and never per frame.
#[allow(dead_code)] // load-time boundary helper; callers moved out with the extracted games
fn read_i32_arr(v: Option<&Value>) -> Vec<i32> {
    match v {
        Some(Value::Array(a)) => a
            .borrow()
            .iter()
            .map(|x| match x {
                Value::Number(f) => *f as i32,
                _ => 0,
            })
            .collect(),
        _ => Vec::new(),
    }
}

fn n(args: &[Value], i: usize) -> f64 {
    match args.get(i) {
        Some(Value::Number(x)) => *x,
        _ => 0.0,
    }
}

fn name_of(args: &[Value], i: usize) -> String {
    args.get(i)
        .map(|v| v.to_display_string())
        .unwrap_or_default()
}

// ── tish-facing API (native-module ABI) ──────────────────────────────────────

/// `spawn()` — create an entity, returning its id.
pub fn spawn(_args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.spawn()) as f64)
}

/// `despawn(entity)` — remove an entity (its id becomes stale).
pub fn despawn(args: &[Value]) -> Value {
    with_world(|w| w.despawn(n(args, 0) as i32));
    Value::Null
}

/// `reset_entity(entity)` — clear every component/system/timer/tag from a LIVE entity,
/// keeping its id and (hidden) sprite handle, so a pooled slot can be reconfigured
/// without despawn/spawn churn. See `World::reset_entity`.
pub fn reset_entity(args: &[Value]) -> Value {
    with_world(|w| w.reset_entity(n(args, 0) as i32));
    Value::Null
}

/// `set_stun(entity, frames)` — hold an entity still for `frames`. The native AI systems
/// (hopper/chase/jumper/shooter/charger) already honour the same stun the hurt system
/// writes; this is the direct handle for the boomerang and the Clock.
pub fn set_stun(args: &[Value]) -> Value {
    set_stun_typed(n(args, 0) as i32, n(args, 1) as i32);
    Value::Null
}

/// `is_stunned(entity)` — 1 while the entity's stun timer is running, else 0.
pub fn is_stunned(args: &[Value]) -> Value {
    Value::Number(is_stunned_typed(n(args, 0) as i32) as f64)
}

// ── Rigid discs ──────────────────────────────────────────────────────────────
// `dynamic_system` runs in world_step phase 2, between `topdown_system` and `wrap_system`. See
// `struct Dynamic` for why contact uses a RANK rather than a mass.

/// `set_dynamic(e, diameter, restitution, friction, restSpeed, rank)` — one call: make the collider
/// a DISC of `diameter`, attach `Dynamic`, set the contact rank. `restitution`/`friction` are Q8
/// (256 = 1.0). Below `restSpeed` px/frame the body parks and stops costing anything.
///
/// One call rather than three (`set_circle` + `set_dynamic` + `set_rank`) because every export is
/// boot heap for EVERY ROM that imports the engine, golf or not — `examples/bench-boot` measured
/// the namespace table's cost and three names for one concept is two names too many.
pub fn set_dynamic(args: &[Value]) -> Value {
    set_dynamic_typed(
        n(args, 0) as i32,
        to_fixed(n(args, 1)),
        n(args, 2) as i32,
        n(args, 3) as i32,
        to_fixed(n(args, 4)),
        n(args, 5) as i32,
    );
    Value::Null
}

/// `body_impulse(e, turn, speed)` — add `speed` px/frame along `turn`, and WAKE the body.
///
/// ⚠️ `turn` is in 1/256ths of a turn, NOT degrees. `fire_angle` divides by `360*256` on every
/// single call; `kart.rs` shows a 1/256th yaw reaching agb's sin/cos table with no arithmetic at
/// all. This is golf's aim-and-power, and it costs zero divisions.
pub fn body_impulse(args: &[Value]) -> Value {
    body_impulse_typed(n(args, 0) as i32, n(args, 1) as i32, to_fixed(n(args, 2)));
    Value::Null
}

/// `body_kick(e, fromX, fromY, speed)` — impulse directly AWAY from a point. One sqrt and one
/// division, paid per kick, never per frame.
pub fn body_kick(args: &[Value]) -> Value {
    body_kick_typed(
        n(args, 0) as i32,
        to_fixed(n(args, 1)),
        to_fixed(n(args, 2)),
        to_fixed(n(args, 3)),
    );
    Value::Null
}

/// `body_asleep(e)` — 1 once the body has come to rest.
///
/// This is golf's whole game loop. A caller diffing `entity_x` two frames apart cannot tell
/// "resting" from "moving very slowly", and guessing a threshold in tish re-implements — badly —
/// a decision the engine already made with the real velocity.
pub fn body_asleep(args: &[Value]) -> Value {
    Value::Number(body_asleep_typed(n(args, 0) as i32) as f64)
}

/// `body_speed2(e)` — speed SQUARED, raw Q8. Squared because a power meter and a verify.sh
/// assertion both work fine on it and neither needs the square root.
pub fn body_speed2(args: &[Value]) -> Value {
    Value::Number(body_speed2_typed(n(args, 0) as i32) as f64)
}

/// `body_last_hit(e)` — the entity that last pushed `e`, or 0. Soccer's last-toucher (own goals,
/// assists) with no per-contact tish callback.
pub fn body_last_hit(args: &[Value]) -> Value {
    Value::Number(body_last_hit_typed(n(args, 0) as i32) as f64)
}

/// `grid_set_surface(col, row, id)` — one tile's surface class, 0..15.
///
/// For RUNTIME edits only (a divot, a broken wall). Author a course through `grid_from_gids`:
/// `examples/bench-boot` measured a per-tile tish marking loop at ~0.175 frames PER TILE, and that
/// measurement was one example's four-second boot.
pub fn grid_set_surface(args: &[Value]) -> Value {
    grid_set_surface_typed(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32);
    Value::Null
}

/// `surface_def(id, ax, ay, friction)` — what a class DOES. `ax`/`ay` are px/frame^2 (a slope,
/// wind, a conveyor); `friction` is Q8 retention (240 ~ green, 200 ~ rough, 150 ~ sand, 256 = ice).
pub fn surface_def(args: &[Value]) -> Value {
    surface_def_typed(
        n(args, 0) as i32,
        to_fixed(n(args, 1)),
        to_fixed(n(args, 2)),
        n(args, 3) as i32,
    );
    Value::Null
}

// ── Entity pools ─────────────────────────────────────────────────────────────
// A pool is a fixed set of entities created ONCE and re-armed forever after (docs/perf-rules.md §6:
// a spawn costs ~1,400 ticks on the frame it happens). It owns no policy — no `maxLive`, no
// cooldown, no per-slot callback — because the six hand-rolled pools this replaces each enforced
// different rules, structurally, by which slot they armed. See `packages/pool.tish` for the field
// and sentinel names, which is a constants-only file with no functions in it at all.

/// `pool_new(count, sheet, ox, oy)` — create `count` entities with sprites off `sheet` (-1 for an
/// entity-only pool), hidden and free. Returns the pool id.
pub fn pool_new(args: &[Value]) -> Value {
    Value::Number(pool_new_typed(
        n(args, 0) as i32,
        n(args, 1) as i32,
        n(args, 2) as i32,
        n(args, 3) as i32,
    ) as f64)
}

/// `pool_arm(p, slot, kind, ttl)` — arm a slot and return its entity id, or -1.
/// `slot >= 0` refuses a LIVE slot; -1 takes the lowest free; -2 steals the shortest-lived.
/// `ttl > 0` hands retirement to `life_system`; `ttl = 0` means the slot lives until retired.
pub fn pool_arm(args: &[Value]) -> Value {
    Value::Number(pool_arm_typed(
        n(args, 0) as i32,
        n(args, 1) as i32,
        n(args, 2) as i32,
        n(args, 3) as i32,
    ) as f64)
}

/// `pool_retire(p, slot)` — park a slot. Never despawns.
pub fn pool_retire(args: &[Value]) -> Value {
    pool_retire_typed(n(args, 0) as i32, n(args, 1) as i32);
    Value::Null
}

/// `pool_clear(p)` — retire every live slot in one call.
pub fn pool_clear(args: &[Value]) -> Value {
    pool_clear_typed(n(args, 0) as i32);
    Value::Null
}

/// `pool_get(p, slot, field)` — 0 kind · 1 ttl · 2 entity · 3 sprite.
pub fn pool_get(args: &[Value]) -> Value {
    Value::Number(pool_get_typed(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32) as f64)
}

/// `pool_stat(p, field)` — 0 count · 1 live · 2 high-water.
pub fn pool_stat(args: &[Value]) -> Value {
    Value::Number(pool_stat_typed(n(args, 0) as i32, n(args, 1) as i32) as f64)
}

/// `clear_world()` — despawn every entity and reset the grid, for a scene transition.
/// The sugar's `loadScene` calls this, then resets tish-agb's arenas, then builds anew.
pub fn clear_world(_args: &[Value]) -> Value {
    with_world(|w| w.clear_world());
    Value::Null
}

/// `set_transform(entity, x, y)` — position (fixed-point).
pub fn set_transform(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let x = to_fixed(n(args, 1));
    let y = to_fixed(n(args, 2));
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.transform[s] = Transform { x, y };
            w.mask[s] |= C_TRANSFORM;
            w.used |= C_TRANSFORM;
        }
    });
    Value::Null
}

/// `set_body(entity, vx, vy)` — velocity (fixed-point per frame); the movement
/// system integrates it into the transform every `world_step`.
pub fn set_body(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let vx = to_fixed(n(args, 1));
    let vy = to_fixed(n(args, 2));
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.body[s] = Body { vx, vy };
            w.mask[s] |= C_BODY;
            w.used |= C_BODY;
        }
    });
    Value::Null
}

/// `set_collider(entity, w, h)` — give an entity a `w`×`h` collision box (top-left at
/// its transform). Overlapping colliders fire `onCollide` behaviour callbacks.
pub fn set_collider(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let w = to_fixed(n(args, 1));
    let h = to_fixed(n(args, 2));
    with_world(|w2| {
        if let Some(s) = w2.slot_of(e) {
            w2.collider[s] = Collider { w, h };
            w2.mask[s] |= C_COLLIDER;
            w2.used |= C_COLLIDER;
        }
    });
    Value::Null
}

/// `overlaps(a, b)` — do two entities' colliders currently overlap? For imperative
/// checks (triggers, range tests) alongside the `onCollide` event.
pub fn overlaps(args: &[Value]) -> Value {
    let a = n(args, 0) as i32;
    let b = n(args, 1) as i32;
    Value::Bool(with_world(|w| w.entities_overlap(a, b)))
}

// ── Side-scrolling platformer genre ABI ────────────────────────────────────────

/// `set_platformer(entity)` — give an entity platformer physics (gravity + solid-tile
/// collision). It also needs a `set_collider` hitbox and a transform; the solid grid comes
/// from `grid_setup`/map loading. Drive it with `platformer_walk` / `platformer_jump`.
pub fn set_platformer(args: &[Value]) -> Value {
    with_world(|w| w.set_platformer(n(args, 0) as i32));
    Value::Null
}

/// `platformer_walk(entity, dir)` — horizontal move intent this frame (dir<0 left, 0 stop,
/// >0 right). Call every frame from input.
pub fn platformer_walk(args: &[Value]) -> Value {
    with_world(|w| w.platformer_walk(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `platformer_run(entity, on)` — run speed instead of walk this frame (hold the run button).
pub fn platformer_run(args: &[Value]) -> Value {
    with_world(|w| w.platformer_run(n(args, 0) as i32, truthy(args, 1)));
    Value::Null
}

/// `platformer_jump(entity)` — buffer a jump (edge-trigger on press). Fires if grounded or within
/// coyote time, so presses just before landing / just after a ledge still jump.
pub fn platformer_jump(args: &[Value]) -> Value {
    with_world(|w| w.platformer_jump(n(args, 0) as i32));
    Value::Null
}

/// `platformer_jump_release(entity)` — cut the jump short if still rising (variable jump height).
/// Edge-trigger on release.
pub fn platformer_jump_release(args: &[Value]) -> Value {
    with_world(|w| w.platformer_jump_release(n(args, 0) as i32));
    Value::Null
}

/// `platformer_drop(entity)` — fall through a one-way platform (hold Down + jump).
pub fn platformer_drop(args: &[Value]) -> Value {
    with_world(|w| w.platformer_drop(n(args, 0) as i32));
    Value::Null
}

/// `platformer_grounded(entity)` — is the entity resting on a solid this frame? (Jump gating,
/// idle/run/fall animation.)
pub fn platformer_grounded(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Bool(with_world(|w| {
        w.slot_of(e)
            .map(|s| w.has(s, C_PLATFORMER) && w.platformer[s].grounded)
            .unwrap_or(false)
    }))
}

/// `platformer_face(entity)` — which way the entity is facing: -1 left, +1 right. This is the last
/// direction it actually MOVED, not this frame's input, so it survives the d-pad being released —
/// which is what sprite-flip and `platformer_interact` need. Every platformer example used to keep
/// its own `data.face` copy of exactly this.
pub fn platformer_face(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_PLATFORMER))
            .map(|s| w.platformer[s].face as f64)
            .unwrap_or(1.0)
    }))
}

/// `platformer_blocked(entity)` — did a wall stop the entity's horizontal move this frame? Patrol
/// AI turns around on this.
pub fn platformer_blocked(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Bool(with_world(|w| {
        w.slot_of(e)
            .map(|s| w.has(s, C_PLATFORMER) && w.platformer[s].blocked)
            .unwrap_or(false)
    }))
}

/// `platformer_bounce(entity, vel)` — launch the entity upward at `vel` px/frame (stomp, spring).
pub fn platformer_bounce(args: &[Value]) -> Value {
    with_world(|w| w.platformer_bounce(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `platformer_set_vy(entity, vy)` — set vertical velocity in px/frame (fractions allowed;
/// negative rises, positive falls). The counterpart to `platformer_vy`, and the general form of
/// `platformer_bounce`, which can only launch upward. A wall slide clamps the fall with this
/// (`platformer_set_vy(e, 0.75)`); so does an updraft, a slam, or a slow-fall item.
pub fn platformer_set_vy(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let vy_raw = (n(args, 1) * 256.0) as i32;
    with_world(|w| w.platformer_set_vy(e, vy_raw));
    Value::Null
}

/// `platformer_set_speed(entity, walk, run)` — this body's ground speeds in px/frame, overriding the
/// engine defaults (1.25 walk / 2.25 run). Pass 0 for either to keep the default.
///
/// Per ENTITY, not per game: the defaults are shared by every platformer in the repo, so tuning them
/// to make one game's hero quicker would silently retune five others. Set once from the component's
/// `start`; it is not a per-frame call. Slide inherits the run speed, since a slide IS a run burst.
pub fn platformer_set_speed(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let walk_raw = (n(args, 1) * 256.0) as i32;
    let run_raw = (n(args, 2) * 256.0) as i32;
    with_world(|w| w.platformer_set_speed(e, walk_raw, run_raw));
    Value::Null
}

/// `platformer_set_physics(entity, jumpVel, gravity)` — this body's jump impulse and gravity in
/// px/frame (0 keeps the engine default). Held-weight physics: heavy cargo lowers `jumpVel`,
/// buoyant cargo lowers `gravity`. Same zero-means-default contract as `platformer_set_speed`.
pub fn platformer_set_physics(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let jump_raw = (n(args, 1) * 256.0) as i32;
    let grav_raw = (n(args, 2) * 256.0) as i32;
    with_world(|w| w.platformer_set_physics(e, jump_raw, grav_raw));
    Value::Null
}

/// `platformer_launch(entity, vx, vy)` — throw arc, px/frame: a persistent horizontal velocity
/// (clears itself on landing or on hitting a wall) plus an immediate vertical velocity (negative
/// = up). This is how a thrown body flies: a plain platformer body has no persistent vx.
pub fn platformer_launch(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let vx_raw = (n(args, 1) * 256.0) as i32;
    let vy_raw = (n(args, 2) * 256.0) as i32;
    with_world(|w| w.platformer_launch(e, vx_raw, vy_raw));
    Value::Null
}

/// `platformer_vy(entity)` — the entity's vertical velocity in px/frame (negative = rising, positive
/// = falling). Lets a component tell a Jump (rising) from a Fall (falling) for animation.
pub fn platformer_vy(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_PLATFORMER))
            .map(|s| w.platformer[s].vy.to_raw() as f64 / 256.0)
            .unwrap_or(0.0)
    }))
}

/// `platformer_hold(entity, on)` — freeze the entity's platformer body in place (no gravity, no
/// movement) while `on`, e.g. hanging on a ledge grab. Release (`on=0`) resumes normal physics
/// (it falls). Position it and set `held` on grab; teleport + release to climb up.
pub fn platformer_hold(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let on = n(args, 1) != 0.0;
    with_world(|w| {
        if let Some(s) = w.slot_of(e).filter(|&s| w.has(s, C_PLATFORMER)) {
            w.platformer[s].held = on;
        }
    });
    Value::Null
}

/// `set_patrol(entity, flipMode?)` — native patrol AI (walk + turn at walls/ledges), all in Rust.
/// Needs a `set_platformer` body. No per-frame tish callback, so many on-screen enemies stay cheap.
/// `flipMode`: 0/omitted = don't touch the sprite, 1 = mirror when walking RIGHT (art faces left),
/// 2 = mirror when walking LEFT (art faces right).
pub fn set_patrol(args: &[Value]) -> Value {
    with_world(|w| w.set_patrol(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `tile_solid(col, row)` — is that map tile solid? (AI probing the level; out-of-bounds = solid.)
pub fn tile_solid(args: &[Value]) -> Value {
    Value::Bool(with_world(|w| {
        w.is_solid(n(args, 0) as i32, n(args, 1) as i32)
    }))
}

// ── Free top-down (action-RPG) genre ABI ───────────────────────────────────────

/// `set_topdown(entity)` — give an entity free 8-directional movement with solid-tile collision. It
/// also needs a `set_collider` hitbox + a transform; the solid grid comes from the loaded map. Drive
/// it with `topdown_move` each frame (player input or chase AI).
pub fn set_topdown(args: &[Value]) -> Value {
    with_world(|w| w.set_topdown(n(args, 0) as i32));
    Value::Null
}

/// `set_blocker(entity)` — this entity's collider blocks top-down movers (an NPC you can't walk
/// through). Needs a collider + transform; the box moves with the entity.
pub fn set_blocker(args: &[Value]) -> Value {
    with_world(|w| w.set_blocker(n(args, 0) as i32));
    Value::Null
}

/// `topdown_snap(entity, mode)` — pick the entity's character-controller profile. These are whole
/// personalities, not combinable flags:
///   0 — free 8-direction movement (the default): pixel-exact, diagonals allowed, no grid.
///   2 — tile stepping: one direction commits a full 16px cell and it rests centred on the cell it
///       lands on. Input during the step is ignored until the step finishes.
pub fn topdown_snap(args: &[Value]) -> Value {
    with_world(|w| w.set_topdown_snap(n(args, 0) as i32, n(args, 1) as u8));
    Value::Null
}

/// `set_chase(entity, aggro, stride, flap, animSpeed)` — native chase-the-player AI (no per-frame
/// tish tick). `stride` = the sheet's cols/direction-row (5 for idle+4walk), or 0 for a
/// non-directional flap loop over frames `0..flap`.
pub fn set_chase(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_chase(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            n(args, 4) as i32,
        )
    });
    Value::Null
}

// ── RTS ABI: flow fields, seek, attack-move, fog ─────────────────────────────

/// `flow_goal(field, col, row)` — (re)build shared flow field `field` (0..3) so that every cell
/// holds its step count to (col,row). Idempotent: re-issuing the goal it already has costs nothing,
/// so a game may call this every frame from its order handler without checking.
///
/// This is the RTS counterpart to `isob_path`. `isob_path` is one route for one unit; a flow field is
/// one search that any number of units read in O(1), which is the only affordable shape when twelve
/// units share a destination.
pub fn flow_goal(args: &[Value]) -> Value {
    with_world(|w| w.flow_goal(n(args, 0) as usize, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `flow_dist(field, col, row)` — steps from that cell to the field's goal, or -1 if it cannot get
/// there. Useful to grey out an unreachable order before issuing it.
pub fn flow_dist(args: &[Value]) -> Value {
    Value::Number(with_world(|w| {
        w.flow_dist(n(args, 0) as usize, n(args, 1) as i32, n(args, 2) as i32)
    }) as f64)
}

/// `flow_ready(field)` — has this field been built at least once?
pub fn flow_ready(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    Value::Bool(with_world(|w| id < MAX_FLOWS && w.flows[id].ready))
}

/// `set_seek(entity, field, arrivePx, stride, animSpeed)` — walk this entity down flow field
/// `field` until it is within `arrivePx` (manhattan) of the goal. Needs `set_topdown` for the
/// movement and tile collision. `stride` is the sheet's columns-per-facing-row (0 = the game owns
/// the frames).
///
/// The missing sibling of `set_chase` (which follows an *entity*) and `set_mover` (which follows a
/// *pattern*): this follows a *destination*, which is the only order an RTS ever gives.
pub fn set_seek(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_seek(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            n(args, 4) as i32,
        )
    });
    Value::Null
}

/// `clear_seek(entity)` — cancel the move order and stop where it stands.
pub fn clear_seek(args: &[Value]) -> Value {
    with_world(|w| w.clear_seek(n(args, 0) as i32));
    Value::Null
}

/// `seek_arrived(entity)` — true once it is inside its arrive radius (or has no order).
pub fn seek_arrived(args: &[Value]) -> Value {
    Value::Bool(with_world(|w| w.seek_arrived(n(args, 0) as i32)))
}

/// `set_soldier(entity, team, rangePx, damage, cooldown)` — attack-move. The unit walks its seek
/// order while nothing hostile is in range, stops to hit the nearest enemy soldier when one is, and
/// resumes when that target dies. `team` (not the entity tag) decides who is hostile, so a game can
/// use one unit kind on both sides and keep tags for its own purposes.
pub fn set_soldier(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_soldier(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            n(args, 4) as i32,
        )
    });
    Value::Null
}

/// `soldier_target(entity)` — the entity it is currently engaging, or -1. This is what a HUD reads
/// to draw a target reticle, and what an AI reads to decide it is already busy.
pub fn soldier_target(args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.soldier_target(n(args, 0) as i32)) as f64)
}

/// `soldier_team(entity)` — the team set by `set_soldier`, or -1.
pub fn soldier_team(args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.soldier_team(n(args, 0) as i32)) as f64)
}

/// `terrain_load(cols, rows, gids, solid)` — load a map from two flat arrays and size the collision
/// grid from the same data, so terrain and pathing cannot disagree.
///
/// Use this INSTEAD of `scene:` when the game also paints an overlay layer (fog) from the same
/// tileset: two bakers over one PNG give two palette orderings, and the GBA has one set of
/// background palettes, so one of the two layers ends up drawing in the other's colours.
pub fn terrain_load(args: &[Value]) -> Value {
    let cols = n(args, 0) as i32;
    let rows = n(args, 1) as i32;
    with_world(|w| w.terrain_load(cols, rows, args.get(2), args.get(3)));
    Value::Null
}

/// `terrain_set(col, row, gid, solid)` — repaint one cell and set its collision.
pub fn terrain_set(args: &[Value]) -> Value {
    with_world(|w| {
        w.terrain_set(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
        )
    });
    Value::Null
}

/// `terrain_blit(bg, tileset, tsCols, camX, camY, gidUnseen)` — paint the camera's terrain window,
/// writing only the cells that changed, and return how many were written.
///
/// `gidUnseen` folds the fog in: a cell the viewing team has never seen is painted with that tile
/// instead of its terrain (pass 0 to disable and draw plain terrain).
pub fn terrain_blit(args: &[Value]) -> Value {
    Value::Number(with_world(|w| {
        w.terrain_blit(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            n(args, 4) as i32,
            n(args, 5) as i32,
        )
    }) as f64)
}

/// `set_sleeping(entity, on)` — mark a pooled slot dormant (1) or in play (0).
///
/// A sleeping entity is skipped by every per-frame system, so a game can hold a large unit pool
/// without paying for it. Arm the slot (`set_sleeping(e, 0)`) to bring it back.
pub fn set_sleeping(args: &[Value]) -> Value {
    with_world(|w| {
        if let Some(s) = w.slot_of(n(args, 0) as i32) {
            if n(args, 1) != 0.0 {
                w.mask[s] |= C_SLEEP;
                w.used |= C_SLEEP;
            } else {
                w.mask[s] &= !C_SLEEP;
            }
        }
    });
    Value::Null
}

/// `fog_init(cols, rows)` — allocate the fog plane at map size. Every cell starts unseen.
pub fn fog_init(args: &[Value]) -> Value {
    with_world(|w| w.fog_init(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `set_vision(entity, radiusCells)` — this entity reveals fog around itself every frame (0 = off).
pub fn set_vision(args: &[Value]) -> Value {
    with_world(|w| w.set_vision(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `fog_reveal(col, row, radiusCells)` — reveal a disc by hand (a scripted reveal, a building's
/// static sight). Entities with `set_vision` do not need this.
pub fn fog_reveal(args: &[Value]) -> Value {
    with_world(|w| w.fog_reveal(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `fog_state(col, row)` — 0 unseen, 1 explored, 2 visible. The gate an RTS puts in front of
/// "should this enemy sprite be drawn" and "may I click this".
pub fn fog_state(args: &[Value]) -> Value {
    let (c, r) = (n(args, 0) as i32, n(args, 1) as i32);
    Value::Number(with_world(|w| {
        if !w.fog.on || c < 0 || r < 0 || c >= w.fog.cols || r >= w.fog.rows {
            FOG_UNSEEN as i32
        } else {
            w.fog.state[(r * w.fog.cols + c) as usize] as i32
        }
    }) as f64)
}

/// `fog_blit(bg, tileset, tsCols, camX, camY, gidUnseen, gidExplored)` — paint the shroud layer for
/// the camera's window, writing only the cells that changed, and return how many were written.
/// A visible cell is blanked; the other two states use the gids you pass.
///
/// Pair the layer with `bg_parallax(bg, 256, 256)` so it tracks the camera: the shroud is a 16x16
/// wrapping window, and that wrap is what lets one small layer cover a map of any size.
///
/// ⚠️ `tileset` should be **the map's own tileset**, not a separate shroud image. `tilemap_new`
/// uploads its asset's palettes to all sixteen background banks, so a shroud built from its own
/// two-colour PNG repaints the whole map in those two colours — and building it in the other order
/// leaves the shroud drawing in whatever colour the map happens to keep at that palette index
/// (measured in `examples/rts-fog`: a brown shroud). Bake the shroud cells into the map's tileset
/// and both layers share one palette by construction.
pub fn fog_blit(args: &[Value]) -> Value {
    Value::Number(with_world(|w| {
        w.fog_blit(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            n(args, 4) as i32,
            n(args, 5) as i32,
            n(args, 6) as i32,
        )
    }) as f64)
}

/// `topdown_speed(entity, px)` — set move speed in px/frame (default 1.25). Keep it under 16.
pub fn set_wanderer(args: &[Value]) -> Value {
    if let [Value::Number(e), Value::Number(turn_rate)] = args {
        let (e, tr) = (*e as i32, *turn_rate as i32);
        with_world(|w| {
            if let Some(s) = w.slot_of(e).filter(|&s| w.has(s, C_TOPDOWN)) {
                w.wanderer[s] = Wanderer {
                    turn_rate: tr.clamp(0, 255),
                    turn_timer: 0,
                    want_shoot: 0,
                    // A sentinel no world position can equal, so the FIRST frame never looks
                    // "blocked" just because there is no previous sample yet.
                    last_x: i32::MIN,
                    last_y: i32::MIN,
                    home_rx: 0,
                    home_ry: 0,
                };
                // Capture the room it was placed in. castPlace sets the transform before reaching
                // the AI switch, so the position is already correct here.
                if w.room_cam.enabled {
                    let (rw, rh) = (w.room_cam.room_w * TILE, w.room_cam.room_h * TILE);
                    let (fx, fy) = w.camera_focus(s);
                    w.wanderer[s].home_rx = fx.div_euclid(rw);
                    w.wanderer[s].home_ry = fy.div_euclid(rh);
                }
                w.mask2[s] |= M2_WANDERER;
                w.used2 |= M2_WANDERER;
            }
        });
    }
    Value::Null
}

/// Sub-pixel movement speed, in raw 8.8 fixed point (256 = 1 px/frame).
///
/// ⚠️⚠️ THIS EXISTS BECAUSE `topdown_speed` TAKES WHOLE PIXELS, AND THAT SILENTLY FLATTENED AN
/// ENTIRE BESTIARY. Its minimum non-zero speed is 1 px/frame, but most NES-era walkers are slower
/// than that: the standard speed of 0x20 over the original's four sub-steps per frame is 0.5
/// px/frame, and the armoured knight's 0x28 is 0.625. Neither is representable, so every enemy in the game was given speed
/// 1 — twice the intended pace for the common case, and identical for all 63 species. Speed is
/// stored as 8.8 internally either way; only the setter's units were lossy.
pub fn topdown_speed_raw(args: &[Value]) -> Value {
    if let [Value::Number(e), Value::Number(raw)] = args {
        let (e, raw) = (*e as i32, *raw as i32);
        with_world(|w| {
            if let Some(s) = w.slot_of(e).filter(|&s| w.has(s, C_TOPDOWN)) {
                w.topdown[s].speed = raw.max(0);
            }
        });
    }
    Value::Null
}

/// Has this wanderer just turned to face the target (i.e. is it lined up)? Drives a shot windup.
pub fn wanderer_wants_shot(args: &[Value]) -> Value {
    if let [Value::Number(e)] = args {
        let e = *e as i32;
        return with_world(|w| match w.slot_of(e) {
            Some(s) if w.mask2[s] & M2_WANDERER != 0 && w.wanderer_wants_shot(s) => {
                Value::Number(1.0)
            }
            _ => Value::Number(0.0),
        });
    }
    Value::Number(0.0)
}

pub fn set_hopper(args: &[Value]) -> Value {
    if let [Value::Number(e), Value::Number(stride)] = args {
        let e = *e as i32;
        with_world(|w| {
            if let Some(s) = w.slot_of(e) {
                w.hopper[s] = Hopper {
                    stride: *stride as i32,
                    timer: 30,
                    state: 0,
                    start_x: Fixed::from_raw(0),
                    start_y: Fixed::from_raw(0),
                    dir_x: 0,
                    dir_y: 0,
                };
                w.mask[s] |= C_HOPPER;
                w.used |= C_HOPPER;
            }
        });
    }
    Value::Null
}

pub fn set_jumper(args: &[Value]) -> Value {
    if let [Value::Number(e)] = args {
        let e = *e as i32;
        with_world(|w| {
            if let Some(s) = w.slot_of(e) {
                w.jumper[s] = Jumper::default();
                w.mask[s] |= C_JUMPER;
                w.used |= C_JUMPER;
            }
        });
    }
    Value::Null
}

pub fn topdown_speed(args: &[Value]) -> Value {
    with_world(|w| w.topdown_speed(n(args, 0) as i32, n(args, 1)));
    Value::Null
}

/// `topdown_move(entity, dx, dy)` — move intent this frame (each ∈ {-1,0,1}). Call every frame; 0/0
/// stops. Updates facing (horizontal priority).
pub fn topdown_move(args: &[Value]) -> Value {
    with_world(|w| w.topdown_move(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `topdown_facing(entity)` — 0 down / 1 up / 2 left / 3 right (persists while idle). For animation
/// and to aim a `swing`.
pub fn topdown_facing(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_TOPDOWN))
            .map(|s| w.topdown[s].facing)
            .unwrap_or(0)
    }) as f64)
}

/// `topdown_moving(entity)` — did it have move intent this frame? (walk vs idle animation.)
pub fn topdown_moving(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Bool(with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_TOPDOWN))
            .map(|s| w.topdown[s].moving)
            .unwrap_or(false)
    }))
}

/// `topdown_knockback(entity, dx, dy, power?)` — shove the entity in (dx, dy) for a few frames,
/// overriding input (a scripted push; contact hits already knock back automatically). `power` raw = 0
/// uses the default.
pub fn topdown_knockback(args: &[Value]) -> Value {
    with_world(|w| {
        w.topdown_knockback(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
        )
    });
    Value::Null
}

/// `swing(attacker, targetTag, damage, reach, size, ttl)` — spawn a short-lived melee hurt box in
/// front of a top-down `attacker` (aimed by its facing) that deals `damage` to `targetTag` entities
/// for `ttl` frames. Returns the hitbox entity id. Pair with the attacker's attack animation / a
/// slash FX for the visual.
pub fn swing(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let target = n(args, 1) as i32;
    let dmg = n(args, 2) as i32;
    let reach = n(args, 3) as i32;
    let size = n(args, 4) as i32;
    let ttl = n(args, 5) as i32;
    Value::Number(with_world(|w| w.swing(e, target, dmg, reach, size, ttl)) as f64)
}

// ── Health / combat ABI ────────────────────────────────────────────────────────

/// `set_health(entity, max, invuln?)` — give an entity `max` hit points (starts full). The optional
/// `invuln` sets how many i-frames a hit grants: omit it for the usual post-hit mercy window
/// (`INVULN_FRAMES`), or pass `0` for a shmup enemy that should take every bullet in a stream.
pub fn set_health(args: &[Value]) -> Value {
    let invuln = match args.get(2) {
        Some(Value::Number(v)) => *v as i32,
        _ => INVULN_FRAMES,
    };
    with_world(|w| w.set_health(n(args, 0) as i32, n(args, 1) as i32, invuln));
    Value::Null
}

/// `set_lifetime(entity, ttl)` — despawn the entity `ttl` frames from now (a timed bullet, an
/// explosion that clears when its clip ends). Combine with `set_despawn_offscreen` for a bullet.
pub fn set_lifetime(args: &[Value]) -> Value {
    with_world(|w| w.set_lifetime(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `set_despawn_offscreen(entity, on)` — when `on`, despawn the entity as soon as its box leaves the
/// visible area. The auto-cleanup a static-screen shooter needs (nothing culls without a camera).
/// `set_arena_wrap(on)` — when `on`, the 240×160 screen becomes a torus for EVERY entity: whatever
/// leaves one edge re-enters the opposite one, fully off-screen, so it slides across without a gap.
///
/// The Asteroids playfield. It is a world switch, not a per-entity component, because in a game that
/// wants it the rule is universal — and natively-spawned bullets have no tish handle to flag. Note
/// that nothing is ever off-screen while it is on, so `set_despawn_offscreen` stops retiring shots:
/// give them a `ttl` (`bullet_style`) instead, which is the classic behaviour anyway.
pub fn set_arena_wrap(args: &[Value]) -> Value {
    with_world(|w| w.set_arena_wrap(truthy(args, 0)));
    Value::Null
}

pub fn set_despawn_offscreen(args: &[Value]) -> Value {
    with_world(|w| w.set_despawn_offscreen(n(args, 0) as i32, truthy(args, 1)));
    Value::Null
}

/// `set_hurt(entity, damage, targetTag, despawnOnHit)` — make the entity deal `damage` on contact to
/// entities tagged `targetTag` that have health. A bullet passes `despawnOnHit = true` (consumed on
/// impact); a body hazard (an enemy the player can crash into) passes `false` and lets i-frames
/// rate-limit it. Resolved natively each frame, so a bullet needs no per-frame tish callback.
/// `set_shooter(entity, interval, speed, aimed)` — make an enemy fire every `interval` frames at
/// `speed` px/frame, with the bullet style that is in force at the time of this call. `aimed` 0 =
/// along its facing, 1 = at the player.
pub fn set_shooter(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_shooter(
            n(args, 0) as i32,
            n(args, 1) as i32,
            to_fixed(n(args, 2)),
            n(args, 3) as i32 != 0,
        )
    });
    Value::Null
}

/// `set_charger(entity, speed, band)` — bolt along an axis at `speed` while lined up with the
/// player within `band` px on the other axis.
/// `set_guard(entity, mask)` — block damage arriving from the direction the entity faces.
/// `mask` is 1 (melee/contact), 2 (projectiles) or 3 (both); 0 removes it. Needs a top-down entity,
/// because the guard is a direction. The player's shield and an armoured knight's armour are the same rule.
pub fn set_guard(args: &[Value]) -> Value {
    with_world(|w| w.set_guard(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `set_dir_anim(entity, base, stride, frames, speed)` — animate `frames` frames starting at
/// `base + facing * stride`, following the entity's facing (0 down, 1 up, 2 left, 3 right).
///
/// For a sheet SHARED by many actors, where `base` says where this one begins. `set_chase`'s own
/// directional mode assumes the entity owns frame 0 of its sheet and that rows are five wide.
pub fn set_dir_anim(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_dir_anim(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            n(args, 4) as i32,
        )
    });
    Value::Null
}

pub fn set_charger(args: &[Value]) -> Value {
    with_world(|w| w.set_charger(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `entity_alive(entity)` — 1 while the entity still exists. A game that keeps a handle to
/// something it spawned (the one boomerang that may be in the air) needs to ask rather than track:
/// the thing can also be retired by its lifetime, by the room cutoff, or by a scene load, and a
/// tracked bool goes stale on every one of those and strands the weapon forever.
pub fn entity_alive(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| match w.slot_of(e) {
        Some(s) if w.alive[s] => 1.0,
        _ => 0.0,
    }))
}

/// `entity_hp(entity) -> i32` — current hit points, or 0 if the entity has no health component.
///
/// This existed only as a method on the boxed `this` wrapper, which meant that any behaviour needing
/// to read its own health could not run as a `lean` tick — one method call forced the whole
/// component back onto the `update:` path and its ~8 ABI round trips per frame. A scalar getter is
/// all it ever needed. Pairs with `entity_hp_max` so a "am I at full health" test (a full-health
/// sword beam, a low-health warning) costs two typed calls and no boxing.
pub fn entity_hp(args: &[Value]) -> Value {
    Value::Number(entity_hp_typed(n(args, 0) as i32) as f64)
}

/// `entity_hp_max(entity) -> i32` — maximum hit points, or 0 if the entity has no health component.
pub fn entity_hp_max(args: &[Value]) -> Value {
    Value::Number(entity_hp_max_typed(n(args, 0) as i32) as f64)
}

/// `set_lure(entity, radius, frames)` — make `entity` a decoy the native AI prefers over the player
/// while it lasts. Enemies within `radius` px walk to it instead. `frames <= 0` clears it.
pub fn set_lure(args: &[Value]) -> Value {
    with_world(|w| w.set_lure(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `set_hurt(entity, damage, targetTag, despawnOnHit, stun = 0)` — see `set_hurt` on `World`.
pub fn set_hurt(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_hurt(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            truthy(args, 3),
            n(args, 4) as i32,
        )
    });
    Value::Null
}

/// `set_damage_type(entity, dmgBits)` — declare which WEAPON a hurt box is, as one `DMG_*` bit.
/// 0 (the default) is untyped and lands on everything, so nothing that predates this changes.
/// Pair with `set_immunity` on the victim.
pub fn set_damage_type(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let dt = n(args, 1) as i32;
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.hurt[s].damage_type = dt;
        }
    });
    Value::Null
}

/// See [`set_damage_type`].
pub fn set_damage_type_typed(e: i32, dt: i32) {
    set_damage_type(&[Value::Number(e as f64), Value::Number(dt as f64)]);
}

/// `set_immunity(entity, dmgBits)` — the weapon kinds that BOUNCE OFF this entity, as a mask of
/// `DMG_*` bits. 0 (the default) is hurt by everything. This is the classic per-monster
/// invincibility mask: it is what makes one boss ignore the sword and another answer only to an
/// arrow, without the collision code knowing what either monster is.
///
/// Survives `set_health`, deliberately — a boss that re-arms its hit points mid-fight must not
/// silently lose its immunities.
pub fn set_immunity(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let mask = n(args, 1) as i32;
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.immune[s] = mask;
        }
    });
    Value::Null
}

/// See [`set_immunity`].
pub fn set_immunity_typed(e: i32, mask: i32) {
    set_immunity(&[Value::Number(e as f64), Value::Number(mask as f64)]);
}

/// `set_weakness(entity, dmgBits)` — damage-type vulnerability mask (`DMG_*` bits). 0 = hurt by
/// everything; non-zero ALLOWS only matching weapon kinds. Complement of `set_immunity`: immunity
/// bounces listed types, weakness requires a listed type to land.
pub fn set_weakness(args: &[Value]) -> Value {
    with_world(|w| w.set_weakness(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// See [`set_weakness`].
pub fn set_weakness_typed(e: i32, mask: i32) {
    with_world(|w| w.set_weakness(e, mask));
}

/// `set_grabber(entity, targetTag)` — on overlap with a tagged target, briefly stun it (the
/// classic grab-on-contact enemy, lite). Needs a collider on both sides.
pub fn set_grabber(args: &[Value]) -> Value {
    with_world(|w| w.set_grabber(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// See [`set_grabber`].
pub fn set_grabber_typed(e: i32, target_tag: i32) {
    with_world(|w| w.set_grabber(e, target_tag));
}

/// `set_trap(entity)` — blade trap: inert until the player shares a row/col, then dash. Needs
/// `C_TOPDOWN`. Home position is snapshotted at configure time.
pub fn set_trap(args: &[Value]) -> Value {
    with_world(|w| w.set_trap(n(args, 0) as i32));
    Value::Null
}

/// See [`set_trap`].
pub fn set_trap_typed(e: i32) {
    with_world(|w| w.set_trap(e));
}

/// `set_carrier(entity)` — the entity's top edge becomes one-way moving ground for platformer
/// bodies: stand on a walking beast or a drifting raft, inherit its motion, jump off normally,
/// Down+jump drops through. Needs a transform + collider; the surface travels with the entity.
pub fn set_carrier(args: &[Value]) -> Value {
    with_world(|w| w.set_carrier(n(args, 0) as i32));
    Value::Null
}

/// See [`set_carrier`].
pub fn set_carrier_typed(e: i32) {
    with_world(|w| w.set_carrier(e));
}

/// `set_part(entity, parentId)` — glue this entity to `parentId` with the offset at configure time.
pub fn set_part(args: &[Value]) -> Value {
    with_world(|w| w.set_follow(n(args, 0) as i32, FOLLOW_PART, n(args, 1) as i32, 0));
    Value::Null
}

/// See [`set_part`].
pub fn set_part_typed(e: i32, parent_id: i32) {
    with_world(|w| w.set_follow(e, FOLLOW_PART, parent_id, 0));
}

/// `set_train(entity, headId)` — train-segment follow: same offset glue as `set_part`, parented to
/// the head (or previous car). Minimal stub for segmented-worm chains.
pub fn set_train(args: &[Value]) -> Value {
    with_world(|w| w.set_follow(n(args, 0) as i32, FOLLOW_TRAIN, n(args, 1) as i32, 0));
    Value::Null
}

/// See [`set_train`].
pub fn set_train_typed(e: i32, head_id: i32) {
    with_world(|w| w.set_follow(e, FOLLOW_TRAIN, head_id, 0));
}

/// `set_orbiter(entity, centerId, radius)` — circle `centerId` at `radius` px each frame.
pub fn set_orbiter(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_follow(
            n(args, 0) as i32,
            FOLLOW_ORBIT,
            n(args, 1) as i32,
            n(args, 2) as i32,
        )
    });
    Value::Null
}

/// See [`set_orbiter`].
pub fn set_orbiter_typed(e: i32, center_id: i32, radius: i32) {
    with_world(|w| w.set_follow(e, FOLLOW_ORBIT, center_id, radius));
}

/// `set_boomerang(entity, returnFrames)` — boomerang return-mover: after `returnFrames`, reverse
/// `Body` velocity toward the owner (camera target at configure time). Pair with `set_lifetime` /
/// `set_despawn_offscreen` like any other projectile. See also `set_mover`.
pub fn set_boomerang(args: &[Value]) -> Value {
    with_world(|w| w.set_boomerang(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// See [`set_boomerang`].
pub fn set_boomerang_typed(e: i32, return_frames: i32) {
    with_world(|w| w.set_boomerang(e, return_frames));
}

/// `boomerang_caught()` — how many boomerangs completed their return and were caught by their
/// owner since the last call (the counter clears on read). The catch also despawns the boomerang,
/// so `entity_alive` on it goes 0 — this is the "report catch" half of the return contract.
pub fn boomerang_caught(_args: &[Value]) -> Value {
    Value::Number(with_world(|w| {
        let c = w.boomer_catches;
        w.boomer_catches = 0;
        c
    }) as f64)
}

/// See [`boomerang_caught`].
pub fn boomerang_caught_typed() -> i32 {
    with_world(|w| {
        let c = w.boomer_catches;
        w.boomer_catches = 0;
        c
    })
}

// ── NES-era enemy AI natives ──────────────────────────────────────────────────────────────────

/// `set_ambusher(entity, hideFrames, surfaceFrames, speedQ8)` — burrower submerge-move-surface
/// cycle: hidden (intangible + invisible) drifting toward the player at `speedQ8` (1/256 px per
/// frame; 320 = 1.25 px), then surfaced (visible, vulnerable, stationary) — repeat. Needs
/// `C_TOPDOWN`. Compose with `set_shooter` for a surfacing shooter.
pub fn set_ambusher(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_ambusher(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
        )
    });
    Value::Null
}

/// See [`set_ambusher`].
pub fn set_ambusher_typed(e: i32, hide: i32, surface: i32, speed_q8: i32) {
    with_world(|w| w.set_ambusher(e, hide, surface, speed_q8));
}

/// `set_drifter(entity, restFrames, flyFrames, speedQ8)` — hovering-drifter floating wander with
/// spin-up/spin-down, INVULNERABLE while moving (only hittable at rest — the damage path enforces
/// it natively; `entity_phased` reads the same flag). Needs `C_TOPDOWN`.
pub fn set_drifter(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_drifter(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
        )
    });
    Value::Null
}

/// See [`set_drifter`].
pub fn set_drifter_typed(e: i32, rest: i32, fly: i32, speed_q8: i32) {
    with_world(|w| w.set_drifter(e, rest, fly, speed_q8));
}

/// `set_flicker_caster(entity, hideFrames, visFrames, shotSpeed)` — teleporting-caster flicker +
/// one aimed cast per appearance, with the bullet style in force at THIS call (the `set_shooter`
/// capture contract). `shotSpeed` is px/frame (fixed — NOT an integer; see the typed twin).
pub fn set_flicker_caster(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_flicker_caster(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            to_fixed(n(args, 3)),
        )
    });
    Value::Null
}

/// See [`set_flicker_caster`]. ⚠️ `shot_speed` is `Fixed`, matching the `fixed` declaration —
/// declaring it `i32` would truncate every sub-pixel speed to 0/1 (the platformer_set_speed trap).
pub fn set_flicker_caster_typed(e: i32, hide: i32, vis: i32, shot_speed: Fixed) {
    with_world(|w| w.set_flicker_caster(e, hide, vis, shot_speed));
}

/// `set_bouncer(entity, restFrames, hopFrames, speedQ8)` — parabolic bouncing hops: an
/// arc (drawn via the sprite `oy` offset), heading 50% at the player / 50% random per hop, tile
/// collision from the top-down mover. Needs `C_TOPDOWN`.
pub fn set_bouncer(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_bouncer(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
        )
    });
    Value::Null
}

/// See [`set_bouncer`].
pub fn set_bouncer_typed(e: i32, rest: i32, hop: i32, speed_q8: i32) {
    with_world(|w| w.set_bouncer(e, rest, hop, speed_q8));
}

/// `set_ricochet(entity, speedQ8)` — a diagonal that reflects off whatever stops it. See the
/// World impl for the full story; the NINE-BOUNCE fix was its first consumer.
pub fn set_ricochet(args: &[Value]) -> Value {
    with_world(|w| w.set_ricochet(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// See [`set_ricochet`].
pub fn set_ricochet_typed(e: i32, speed_q8: i32) {
    with_world(|w| w.set_ricochet(e, speed_q8));
}

/// `entity_phased(e)` — 1 while `e` is currently invulnerable/intangible under a nai state
/// machine or `set_phased` (drifter in flight, submerged burrower, vanished caster). The native
/// damage path already refuses such hits; this is for game-side damage logic (a custom sword
/// swing) to honour the same flag.
pub fn entity_phased(args: &[Value]) -> Value {
    Value::Number(entity_phased_typed(n(args, 0) as i32) as f64)
}

/// See [`entity_phased`].
pub fn entity_phased_typed(e: i32) -> i32 {
    with_world(|w| {
        w.slot_of(e)
            .map(|s| (w.mask2[s] & (M2_PHASED | M2_HIDDEN) != 0) as i32)
            .unwrap_or(0)
    })
}

/// `set_phased(e, on)` — set/clear the invulnerable flag directly (a core invulnerable while its
/// orbiting children live, a boss awaiting its trigger item). Tangible: contact damage still lands on the player.
pub fn set_phased(args: &[Value]) -> Value {
    with_world(|w| w.set_phased(n(args, 0) as i32, n(args, 1) as i32 != 0));
    Value::Null
}

/// See [`set_phased`].
pub fn set_phased_typed(e: i32, on: i32) {
    with_world(|w| w.set_phased(e, on != 0));
}

// ── Multi-part boss glue ──────────────────────────────────────────────────────────────────────

/// `set_hit_proxy(entity, targetId)` — route damage dealt to `entity` onto `targetId` instead
/// (boss neck→head; re-point per frame for the shrinking-tail rule). `targetId < 0`
/// clears. Follows at most 4 hops; each hop honours its own gate/phase.
pub fn set_hit_proxy(args: &[Value]) -> Value {
    with_world(|w| w.set_hit_proxy(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// See [`set_hit_proxy`].
pub fn set_hit_proxy_typed(e: i32, target: i32) {
    with_world(|w| w.set_hit_proxy(e, target));
}

/// `set_vuln_gate(entity, open)` — while `open == 0`, no damage lands on `entity` (an eye-open
/// gate, a boss's last-hit window). Combine with `set_weakness` for "arrow only AND eye open".
pub fn set_vuln_gate(args: &[Value]) -> Value {
    with_world(|w| w.set_vuln_gate(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// See [`set_vuln_gate`].
pub fn set_vuln_gate_typed(e: i32, open: i32) {
    with_world(|w| w.set_vuln_gate(e, open));
}

/// `set_death_note(entity, code)` — when `entity` dies, push `code` to the death-note queue.
/// The parent's logic drains it with `death_note()` — part-death notification without a per-part
/// tish callback.
pub fn set_death_note(args: &[Value]) -> Value {
    with_world(|w| w.set_death_note(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// See [`set_death_note`].
pub fn set_death_note_typed(e: i32, code: i32) {
    with_world(|w| w.set_death_note(e, code));
}

/// `death_note()` — pop the next part-death code, or 0 when the queue is empty. Use a non-zero
/// code per part.
pub fn death_note(_args: &[Value]) -> Value {
    Value::Number(death_note_typed() as f64)
}

/// See [`death_note`].
pub fn death_note_typed() -> i32 {
    with_world(|w| {
        if w.death_notes.is_empty() {
            0
        } else {
            w.death_notes.remove(0)
        }
    })
}

/// `detach_part(entity)` — sever the `set_part`/`set_train`/`set_orbiter` glue: the entity keeps
/// its sprite/health/position but moves on its own again (flying-head promotion — detach,
/// then give the same entity `set_drifter` + `set_shooter`).
pub fn detach_part(args: &[Value]) -> Value {
    with_world(|w| w.detach_part(n(args, 0) as i32));
    Value::Null
}

/// See [`detach_part`].
pub fn detach_part_typed(e: i32) {
    with_world(|w| w.detach_part(e));
}

/// See [`set_dir_anim`]. Typed twin: this is called once per enemy per room population, on a
/// shared 33-actor sprite strip, so the boxed path was pure marshalling overhead.
pub fn set_dir_anim_typed(e: i32, base: i32, stride: i32, frames: i32, speed: i32) {
    with_world(|w| w.set_dir_anim(e, base, stride, frames, speed));
}

/// `bullet_damage_type(dmgBits)` — stamp the CURRENT bullet style with a weapon kind, so the
/// bullets a `fire_*` spawns carry it. Sticky and set beside `bullet_style`, in either order.
/// Kept out of `bullet_style`'s argument list so its seven-argument signature — and every game
/// already calling it — is untouched.
pub fn bullet_damage_type(args: &[Value]) -> Value {
    let dt = n(args, 0) as i32;
    with_world(|w| w.bullet_style.damage_type = dt);
    Value::Null
}

/// See [`bullet_damage_type`].
pub fn bullet_damage_type_typed(dt: i32) {
    bullet_damage_type(&[Value::Number(dt as f64)]);
}

/// `entity_immunity(entity)` — the entity's current immunity mask, so a test can assert it without
/// having to infer it from damage that did or did not land.
pub fn entity_immunity(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| w.slot_of(e).map(|s| w.immune[s]).unwrap_or(0)) as f64)
}

/// See [`entity_immunity`].
pub fn entity_immunity_typed(e: i32) -> i32 {
    with_world(|w| w.slot_of(e).map(|s| w.immune[s]).unwrap_or(0))
}

// ── Native bullet emitters ABI ──────────────────────────────────────────────────
// `bullet_style` sets the shared config once; the `fire_*` calls then spawn whole patterns natively.
// A game resolves its options object once per burst (not per bullet) and hands the scalars here.

/// `bullet_style(sheet, frame, size, damage, target, tag, ttl)` — configure the next burst of bullets.
pub fn bullet_style(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_bullet_style(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            n(args, 4) as i32,
            n(args, 5) as i32,
            n(args, 6) as i32,
        )
    });
    Value::Null
}
pub fn bullet_style_typed(
    sheet: i32,
    frame: i32,
    size: i32,
    damage: i32,
    target: i32,
    tag: i32,
    ttl: i32,
) {
    with_world(|w| w.set_bullet_style(sheet, frame, size, damage, target, tag, ttl));
}

/// `fire_bullet(cx, cy, vx, vy)` — one bullet with the current style. Returns its entity id.
pub fn fire_bullet(args: &[Value]) -> Value {
    Value::Number(with_world(|w| {
        w.fire_bullet(
            to_fixed(n(args, 0)),
            to_fixed(n(args, 1)),
            to_fixed(n(args, 2)),
            to_fixed(n(args, 3)),
        )
    }) as f64)
}
pub fn fire_bullet_typed(cx: Fixed, cy: Fixed, vx: Fixed, vy: Fixed) -> i32 {
    with_world(|w| w.fire_bullet(cx, cy, vx, vy))
}

/// `fire_angle(cx, cy, deg, speed)` — one bullet at a heading (0 right, 90 down, −90 up).
pub fn fire_angle(args: &[Value]) -> Value {
    Value::Number(with_world(|w| {
        w.fire_angle(
            to_fixed(n(args, 0)),
            to_fixed(n(args, 1)),
            to_fixed(n(args, 2)),
            to_fixed(n(args, 3)),
        )
    }) as f64)
}
pub fn fire_angle_typed(cx: Fixed, cy: Fixed, deg: Fixed, speed: Fixed) -> i32 {
    with_world(|w| w.fire_angle(cx, cy, deg, speed))
}

/// `fire_ring(cx, cy, count, speed)` — a full ring of `count` evenly-spaced bullets.
pub fn fire_ring(args: &[Value]) -> Value {
    with_world(|w| {
        w.fire_ring(
            to_fixed(n(args, 0)),
            to_fixed(n(args, 1)),
            n(args, 2) as i32,
            to_fixed(n(args, 3)),
        )
    });
    Value::Null
}
pub fn fire_ring_typed(cx: Fixed, cy: Fixed, count: i32, speed: Fixed) {
    with_world(|w| w.fire_ring(cx, cy, count, speed));
}

/// `fire_spread(cx, cy, centerDeg, count, spreadDeg, speed)` — a fan of `count` bullets.
pub fn fire_spread(args: &[Value]) -> Value {
    with_world(|w| {
        w.fire_spread(
            to_fixed(n(args, 0)),
            to_fixed(n(args, 1)),
            to_fixed(n(args, 2)),
            n(args, 3) as i32,
            to_fixed(n(args, 4)),
            to_fixed(n(args, 5)),
        )
    });
    Value::Null
}
pub fn fire_spread_typed(
    cx: Fixed,
    cy: Fixed,
    center_deg: Fixed,
    count: i32,
    spread_deg: Fixed,
    speed: Fixed,
) {
    with_world(|w| w.fire_spread(cx, cy, center_deg, count, spread_deg, speed));
}

/// `fire_aimed(cx, cy, tox, toy, speed)` — one bullet aimed at a target point. Returns its entity id.
pub fn fire_aimed(args: &[Value]) -> Value {
    Value::Number(with_world(|w| {
        w.fire_aimed(
            to_fixed(n(args, 0)),
            to_fixed(n(args, 1)),
            to_fixed(n(args, 2)),
            to_fixed(n(args, 3)),
            to_fixed(n(args, 4)),
        )
    }) as f64)
}
pub fn fire_aimed_typed(cx: Fixed, cy: Fixed, tox: Fixed, toy: Fixed, speed: Fixed) -> i32 {
    with_world(|w| w.fire_aimed(cx, cy, tox, toy, speed))
}

/// `set_mover(entity, pattern, vy, amp, period)` — give an entity a NATIVE movement pattern driven by
/// `mover_system` in pure Rust (no per-frame tish `tick`). `pattern` 0 = straight down at `vy`;
/// 1 = weave (a sideways triangle of amplitude `amp` px over `period` frames while descending at `vy`).
/// The shmup counterpart to `set_patrol` — a screen full of weaving enemies costs zero tish calls.
/// For a boomerang-style return after N frames, see `set_boomerang` (reverses `Body` toward owner).
pub fn set_mover(args: &[Value]) -> Value {
    with_world(|w| {
        w.set_mover(
            n(args, 0) as i32,
            n(args, 1) as u8,
            to_fixed(n(args, 2)),
            to_fixed(n(args, 3)),
            n(args, 4) as i32,
        )
    });
    Value::Null
}

/// `damage(entity, amount)` — deal damage (ignored during i-frames). Returns whether it landed.
pub fn damage(args: &[Value]) -> Value {
    Value::Bool(with_world(|w| {
        w.damage(n(args, 0) as i32, n(args, 1) as i32)
    }))
}

/// `heal(entity, amount)` — restore hit points up to max.
pub fn heal(args: &[Value]) -> Value {
    with_world(|w| w.heal(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `health_hp(entity)` / `health_max(entity)` — current / maximum hit points (0 if no health).
/// `set_hp(entity, hp)` — set CURRENT hit points, leaving the maximum alone.
///
/// For restoring a carried or saved health value. `set_health` cannot do it (it resets the maximum
/// too) and `damage` must not (it fires i-frames and a death check), so without this a game that
/// wants a hero to keep his wounds across a scene load has no way to say so.
pub fn set_hp(args: &[Value]) -> Value {
    with_world(|w| {
        let e = n(args, 0) as i32;
        let hp = n(args, 1) as i32;
        if let Some(s) = w.slot_of(e).filter(|&s| w.has(s, C_HEALTH)) {
            let m = w.health[s].max;
            let v = hp.clamp(0, m);
            w.health[s].hp = v;
            w.health[s].dead = v <= 0;
        }
    });
    Value::Null
}

pub fn health_hp(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_HEALTH))
            .map(|s| w.health[s].hp)
            .unwrap_or(0)
    }) as f64)
}
pub fn health_max(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_HEALTH))
            .map(|s| w.health[s].max)
            .unwrap_or(0)
    }) as f64)
}

/// `health_alive(entity)` — is the entity present with hp > 0?
pub fn health_alive(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Bool(with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_HEALTH))
            .map(|s| w.health[s].hp > 0)
            .unwrap_or(false)
    }))
}

/// `entity_on_screen(entity)` — is the entity within the camera view (plus the cull margin)?
/// Off-screen entities have their behaviour/physics/animation/collision skipped automatically; this
/// query lets game logic react too (e.g. only spawn or activate something once it's on screen).
pub fn entity_on_screen(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Bool(with_world(|w| {
        w.slot_of(e).map(|s| w.on_screen(s)).unwrap_or(false)
    }))
}

/// `set_tag(entity, tag)` — label an entity with a game-defined integer kind (e.g. 1 = player,
/// 2 = enemy, 3 = pickup). A collision/interaction handler reads it back with `entity_tag` to tell
/// what it touched, so a pickup can grant only to the player and ignore an enemy that walks over it.
pub fn set_tag(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let t = n(args, 1) as i32;
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.tag[s] = t;
        }
    });
    Value::Null
}

/// `entity_tag(entity)` — the entity's kind tag (0 if untagged or unknown). See `set_tag`.
pub fn entity_tag(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| w.slot_of(e).map(|s| w.tag[s]).unwrap_or(0)) as f64)
}

// ── Typed per-entity component state (the fast tick path) ───────────────────────
// `cvar(e, k)` / `set_cvar(e, k, v)` give a component's `tick` 8 native i32 slots to keep its
// counters and flags in, instead of a boxed `Value` context object whose every `d.field` is a hashmap
// lookup. With typed args these lower to direct `cvar_typed`/`set_cvar_typed` calls — no boxing.

/// `cvar(entity, k)` — read typed state slot `k` (0..7). 0 for an unknown entity.
pub fn cvar(args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.cvar(n(args, 0) as i32, n(args, 1) as usize)) as f64)
}
pub fn cvar_typed(e: i32, k: i32) -> i32 {
    with_world(|w| w.cvar(e, k.max(0) as usize))
}
/// `set_cvar(entity, k, v)` — write typed state slot `k` (0..7).
pub fn set_cvar(args: &[Value]) -> Value {
    with_world(|w| w.set_cvar(n(args, 0) as i32, n(args, 1) as usize, n(args, 2) as i32));
    Value::Null
}
pub fn set_cvar_typed(e: i32, k: i32, v: i32) {
    with_world(|w| w.set_cvar(e, k.max(0) as usize, v));
}

// ── Grid / RPG genre ABI ──────────────────────────────────────────────────────

/// `grid_setup(cols, rows)` — create the tile-collision grid (all walkable).
pub fn grid_setup(args: &[Value]) -> Value {
    with_world(|w| w.grid_setup(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `grid_set_solid(col, row, solid)` — mark a tile solid (non-zero) or walkable (0).
pub fn grid_set_solid(args: &[Value]) -> Value {
    with_world(|w| w.grid_set_solid(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) != 0.0));
    Value::Null
}

/// `grid_from_map()` — size the collision grid to the loaded ROM map (`map_stream` /
/// `scene_stream`) and mark every solid tile, all in Rust. The tish equivalent was a w×h
/// interpreter loop with two native calls per cell, which dominated area load time.
pub fn grid_from_map(_args: &[Value]) -> Value {
    let grid = tish_agb::native_map_solid_grid();
    // The one-way and ladder planes are optional: a top-down map has neither, and every map baked
    // before they existed simply ends after its spawn list.
    let oneway = tish_agb::native_map_oneway_grid();
    let ladder = tish_agb::native_map_ladder_grid();
    if let Some((solid, width, height)) = grid {
        with_world(|w| {
            w.grid_setup(width, height);
            for row in 0..height {
                let base = (row * width) as usize;
                for col in 0..width {
                    let i = base + col as usize;
                    if let Some(l) = ladder {
                        if l[i] != 0 {
                            w.grid_set_ladder(col, row, true);
                        }
                    }
                    // One-way wins over solid where a map marks both, matching `grid_from_gids`:
                    // the softer rule leaves a passable platform rather than an invisible wall.
                    if oneway.map(|o| o[i] != 0).unwrap_or(false) {
                        w.grid_set_oneway(col, row, true);
                    } else if solid[i] != 0 {
                        w.grid_set_solid(col, row, true);
                    }
                }
            }
        });
    }
    Value::Null
}

/// Read a tish array of numbers into a `Vec<i32>`. Costs one `get_prop` plus one `get_index` per
/// element — which is exactly the crossing this module exists to make ONCE instead of per cell.
///
/// Anything that is not an array reads as empty, and that case is REACHED, not defensive: a map is a
/// plain tish object, so a platformer map with no one-way platforms simply has no `oneway` key and
/// passes null. The tish helper this replaced opened with `if (!list) { return false }` for exactly
/// that reason; taking `.length` off a null instead leaves a pending throw that kills the ROM at the
/// next check, which shows up as a white screen rather than as an error.
fn read_i32_array(v: &Value) -> Vec<i32> {
    match v {
        Value::Array(_) => {}
        _ => return Vec::new(),
    }
    let len = match get_prop(v, "length") {
        Value::Number(n) => n as usize,
        _ => 0,
    };
    let mut out = Vec::with_capacity(len);
    let mut i = 0;
    while i < len {
        out.push(
            match tishlang_runtime_gba::get_index(v, &Value::Number(i as f64)) {
                Value::Number(n) => n as i32,
                _ => 0,
            },
        );
        i += 1;
    }
    out
}

/// `grid_from_gids(width, height, data, solid, oneway)` — size the collision grid and mark every
/// solid / one-way cell from a tish GID array, all in Rust.
///
/// This is [`grid_from_map`]'s counterpart for maps that live in the GAME rather than in ROM. A
/// `scene:`-imported map is packed at build time and `grid_from_map` reads it straight out of ROM;
/// a map written as a tish literal (`{ width, height, solid: [...], layers: [{ data: [...] }] }`)
/// has no ROM form, so `loadStreamMap` walked it in the interpreter instead — a w×h loop doing four
/// property lookups, two function calls and up to two native calls **per cell**.
///
/// That loop was the entire boot time of every game using it. Measured on `sunny-land` (102×15):
/// rendering all 1,530 tiles through `tilemap_stream` cost 16 frames, and marking their collision in
/// tish cost **218** — 3.65 seconds of black screen, ~40,000 CPU cycles per cell. Same work here is
/// one crossing per array and a native double loop.
///
/// `solid` and `oneway` are GID lists rather than a per-cell mask because that is the shape a Tiled
/// map already has, and they are scanned linearly for the same reason the tish version did: they
/// hold a handful of entries, and a set would cost more to build than the scan saves.
pub fn grid_from_gids(args: &[Value]) -> Value {
    let width = n(args, 0) as i32;
    let height = n(args, 1) as i32;
    if width <= 0 || height <= 0 {
        return Value::Null;
    }
    let data = match args.get(2) {
        Some(v) => read_i32_array(v),
        None => return Value::Null,
    };
    let solid = args.get(3).map(read_i32_array).unwrap_or_default();
    let oneway = args.get(4).map(read_i32_array).unwrap_or_default();
    let ladder = args.get(5).map(read_i32_array).unwrap_or_default();
    with_world(|w| {
        w.grid_setup(width, height);
        for row in 0..height {
            let base = (row * width) as usize;
            for col in 0..width {
                let gid = match data.get(base + col as usize) {
                    Some(g) => *g,
                    None => continue,
                };
                // Ladder is a SEPARATE plane, not an alternative to solid/one-way: a ladder tile
                // that ends in a landing is both climbable and a one-way floor, and a ladder set
                // into a wall is climbable and solid. So mark it, then fall through to the
                // solidity rules rather than `else`-ing them out.
                if ladder.contains(&gid) {
                    w.grid_set_ladder(col, row, true);
                }
                // One-way is tested first and wins, matching the tish version's if/else — a GID in
                // both lists is a map-authoring mistake, and picking the softer rule keeps it a
                // passable platform rather than an invisible wall.
                if oneway.contains(&gid) {
                    w.grid_set_oneway(col, row, true);
                } else if solid.contains(&gid) {
                    w.grid_set_solid(col, row, true);
                }
            }
        }
    });
    Value::Null
}

/// `grid_set_oneway(col, row, on)` — mark a tile a one-way platform (block a platformer box only
/// from above). Independent of `grid_set_solid`.
pub fn grid_set_oneway(args: &[Value]) -> Value {
    with_world(|w| w.grid_set_oneway(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) != 0.0));
    Value::Null
}

/// `grid_set_ladder(col, row, on)` — mark a tile climbable (ladder, vine, rope). Independent of
/// `grid_set_solid` and `grid_set_oneway`: a ladder can be set into a wall or capped by a one-way
/// landing tile, and the three planes describe different things.
pub fn grid_set_ladder(args: &[Value]) -> Value {
    with_world(|w| w.grid_set_ladder(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) != 0.0));
    Value::Null
}

/// `tile_ladder(col, row)` — is this tile climbable? The probe a climb state machine runs to decide
/// whether Up should grab a ladder, and whether it has run off the top of one. Out-of-bounds is
/// false (unlike `tile_solid`, where out-of-bounds is a wall).
pub fn tile_ladder(args: &[Value]) -> Value {
    let col = n(args, 0) as i32;
    let row = n(args, 1) as i32;
    Value::Bool(with_world(|w| w.is_ladder(col, row)))
}

/// `attach_grid(entity, col, row)` — place an entity on the grid (tile-locked
/// movement). Sets its transform to the tile's pixel position.
pub fn attach_grid(args: &[Value]) -> Value {
    with_world(|w| w.attach_grid(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `grid_step(entity, dx, dy)` — try to walk one tile (4-directional). Faces that way;
/// steps only if idle and the target tile isn't solid.
pub fn grid_step(args: &[Value]) -> Value {
    with_world(|w| w.grid_step(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `grid_moving(entity)` — is the entity mid-step? (Gate input so one press = one tile.)
pub fn grid_moving(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Bool(with_world(|w| {
        w.slot_of(e)
            .map(|s| w.has(s, C_GRIDPOS) && w.gridpos[s].moving)
            .unwrap_or(false)
    }))
}

/// `grid_interact(entity)` — probe the tile the entity faces; if an entity there defines
/// `onInteract`, fire it (the "talk to the NPC" event). Reentrancy-safe.
pub fn grid_interact(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let call = with_world(|w| w.collect_interact(e));
    if let Some((cb, data, target, actor)) = call {
        value_call(
            &cb,
            &[
                data,
                Value::Number(target as f64),
                Value::Number(actor as f64),
            ],
        );
    }
    Value::Null
}

/// `topdown_interact(entity, reach)` — the free-movement counterpart to `grid_interact`: probe a
/// point `reach` px in front of a top-down entity (by its facing) and fire the `onInteract` of an
/// entity there (talk to an NPC, read a sign, open a chest). Reentrancy-safe. Returns 1 when
/// something ahead handled it, 0 when nothing did — so one button can talk when facing an NPC and
/// swing otherwise (the classic context action).
pub fn topdown_interact(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let reach = n(args, 1) as i32;
    let call = with_world(|w| w.collect_topdown_interact(e, reach));
    if let Some((cb, data, target, actor)) = call {
        value_call(
            &cb,
            &[
                data,
                Value::Number(target as f64),
                Value::Number(actor as f64),
            ],
        );
        return Value::Number(1.0);
    }
    Value::Number(0.0)
}

/// `platformer_interact(entity, reach)` — the side-scrolling counterpart to `topdown_interact`:
/// probe `reach` px to the side the entity is facing (`platformer_face`) over its own box height
/// plus a little slack, and fire the `onInteract` of an entity there — talk to a townsperson, read
/// a signpost, open a door. Reentrancy-safe. Returns 1 when something ahead handled it, 0 otherwise,
/// so one button can talk when facing an NPC and do something else when it isn't.
pub fn platformer_interact(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let reach = n(args, 1) as i32;
    let call = with_world(|w| w.collect_platformer_interact(e, reach));
    if let Some((cb, data, target, actor)) = call {
        value_call(
            &cb,
            &[
                data,
                Value::Number(target as f64),
                Value::Number(actor as f64),
            ],
        );
        return Value::Number(1.0);
    }
    Value::Number(0.0)
}

/// `platformer_can_interact(entity, reach)` — is there something to interact with in front of this
/// entity right now? Runs the same probe as `platformer_interact` but fires nothing.
///
/// This is what a context PROMPT needs — the little "A" that appears over your head when you are
/// standing in front of a townsperson. Without it a game has to either show the prompt always (which
/// teaches the player nothing about who will talk) or discover the answer by pressing the button
/// (which is too late to be a prompt).
pub fn platformer_can_interact(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let reach = n(args, 1) as i32;
    let found = with_world(|w| w.collect_platformer_interact(e, reach).is_some());
    Value::Number(if found { 1.0 } else { 0.0 })
}

/// `set_anim(entity, frames, speed)` — loop a sprite-sheet animation (needs a `sheet:`
/// sprite): cycle `frames` frames, one every `speed` game frames.
pub fn set_anim(args: &[Value]) -> Value {
    with_world(|w| w.set_anim(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `set_walk(entity, cols, speed)` — enable directional walk animation on a grid entity.
/// Its sprite must be a `sheet:` laid out as rows of `cols` frames (down / up / side; see
/// `Walk`). `speed` is frames per step toggle. The grid facing + movement drive it.
pub fn set_walk(args: &[Value]) -> Value {
    with_world(|w| w.set_walk(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// Read argument `i` as a boolean (accepts a bool or a nonzero number).
fn truthy(args: &[Value], i: usize) -> bool {
    match args.get(i) {
        Some(Value::Bool(b)) => *b,
        Some(Value::Number(x)) => *x != 0.0,
        _ => false,
    }
}

/// `anim_play(entity, from, len, speed, loop)` — the low-level clip primitive an animation
/// controller drives: play frames `[from, from+len)` at `speed` frames-per-step, looping or
/// stopping on the last frame. Idempotent (re-issuing the active clip won't restart it).
pub fn anim_play(args: &[Value]) -> Value {
    with_world(|w| {
        w.anim_play(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            truthy(args, 4),
        )
    });
    Value::Null
}

/// `grid_facing(entity)` — the entity's facing as a direction code (0 down, 1 up, 2 left,
/// 3 right), for a controller to select a directional clip.
pub fn grid_facing(args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.grid_facing(n(args, 0) as i32)) as f64)
}

/// `grid_col(entity)` / `grid_row(entity)` — the entity's current grid tile. Used to detect when a
/// player stands on a teleporter/door tile (then `attach_grid`/`onGrid` warps them to the target).
pub fn grid_col(args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.grid_col(n(args, 0) as i32)) as f64)
}
pub fn grid_row(args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.grid_row(n(args, 0) as i32)) as f64)
}

/// `entity_sprite(entity)` — the entity's tish-agb sprite handle (or -1). For manual
/// frame control (`sprite_set_frame`), e.g. directional/facing frames.
pub fn entity_sprite(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    Value::Number(with_world(|w| w.slot_of(e).map(|s| w.sprite[s].handle).unwrap_or(-1)) as f64)
}

/// `attach_sprite(entity, spriteHandle)` — bind a tish-agb sprite (from `sprite_new`)
/// to an entity; the render system keeps it at the entity's transform.
pub fn attach_sprite(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let h = n(args, 1) as i32;
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.sprite[s] = SpriteRef {
                handle: h,
                ox: 0,
                oy: 0,
            };
            w.mask[s] |= C_SPRITE;
            w.used |= C_SPRITE;
        }
    });
    // Release the sprite's VRAM immediately: `render_system` restores it the moment the entity is on
    // screen and re-releases it when it leaves. This keeps setup from holding a VRAM sprite for every
    // entity at once (a big level's off-screen enemies/pickups would otherwise exhaust sprite VRAM
    // before the first frame renders); at most a couple are resident during spawning.
    tish_agb::native_sprite_release(h);
    Value::Null
}

/// `set_sprite_offset(entity, ox, oy)` — draw the entity's sprite `ox`/`oy` px from its transform,
/// so the sprite can be bigger than its collider. A 32×32 character on a 16×16 hitbox uses (-8, -16):
/// centred over the box, feet on the box's bottom edge. Call after `attach_sprite`.
pub fn set_sprite_offset(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let ox = n(args, 1) as i32;
    let oy = n(args, 2) as i32;
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.sprite[s].ox = ox;
            w.sprite[s].oy = oy;
        }
    });
    Value::Null
}

/// `entity_x(entity)` / `entity_y(entity)` — read back a transform coordinate (for
/// game logic / debugging). `null` for a stale/unknown entity.
pub fn entity_x(args: &[Value]) -> Value {
    with_world(|w| {
        w.slot_of(n(args, 0) as i32)
            .map(|s| Value::Number(from_fixed(w.transform[s].x)))
            .unwrap_or(Value::Null)
    })
}

/// See [`entity_x`].
pub fn entity_y(args: &[Value]) -> Value {
    with_world(|w| {
        w.slot_of(n(args, 0) as i32)
            .map(|s| Value::Number(from_fixed(w.transform[s].y)))
            .unwrap_or(Value::Null)
    })
}

// ── Typed exports (the "typed externs" perf path) ─────────────────────────────
// Each mirrors a boxed `fn(&[Value]) -> Value` export above, but takes NATIVE arguments and returns a
// native value — so a typed tish call site lowers to a DIRECT `tish_gba_game_engine::name_typed(..)`
// Rust call (no `Value` boxing, no `value_call` dispatch). The compiler emits these when a game
// `declare fun`s the matching typed signature (shipped in the module's `.d.tish`); the boxed shims
// above stay for dynamic call sites. Keep the two in sync — same effect, native vs boxed marshalling.
pub fn spawn_typed() -> i32 {
    with_world(|w| w.spawn())
}
pub fn despawn_typed(e: i32) {
    with_world(|w| w.despawn(e));
}
pub fn reset_entity_typed(e: i32) {
    with_world(|w| w.reset_entity(e));
}
pub fn set_dynamic_typed(
    e: i32,
    diameter: Fixed,
    restitution: i32,
    friction: i32,
    rest_speed: Fixed,
    rank: i32,
) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.collider[s] = Collider {
                w: diameter,
                h: diameter,
            };
            let rv = rest_speed.to_raw() >> 4;
            w.dynamic[s] = Dynamic {
                restitution: restitution.clamp(0, 256),
                friction: friction.clamp(0, 256),
                rest_v2: rv * rv,
                rank: rank.clamp(0, 255) as u8,
                asleep: 0,
                last_hit: 0,
            };
            w.mask[s] |= C_DYNAMIC | C_CIRCLE | C_COLLIDER | C_BODY | C_TRANSFORM;
            w.used |= C_DYNAMIC | C_CIRCLE | C_COLLIDER | C_BODY | C_TRANSFORM;
        }
    });
}
pub fn body_impulse_typed(e: i32, turn: i32, speed: Fixed) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            // ⚠️ THIS IS WHY `turn` IS IN 1/256ths AND NOT DEGREES. agb's sin/cos table is 256
            // entries over one turn and `Fixed`'s raw units ARE 1/256ths of a turn, so the
            // conversion is `from_raw` and NO ARITHMETIC AT ALL. `fire_angle`, which takes degrees,
            // pays a software division by 360*256 on every single call.
            let a = Fixed::from_raw(turn.rem_euclid(256));
            let (c, si) = (a.cos(), a.sin());
            w.body[s].vx += Fixed::from_raw((speed.to_raw() * c.to_raw()) >> 8);
            w.body[s].vy += Fixed::from_raw((speed.to_raw() * si.to_raw()) >> 8);
            w.dynamic[s].asleep = 0;
        }
    });
}
pub fn body_kick_typed(e: i32, from_x: Fixed, from_y: Fixed, speed: Fixed) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            let (cx, cy) = w.center_of(s);
            let dx = (cx.to_raw() - from_x.to_raw()) >> 8;
            let dy = (cy.to_raw() - from_y.to_raw()) >> 8;
            let d2 = (dx * dx + dy * dy).max(1);
            let mut len = 1i32;
            while len * len < d2 {
                len += 1;
            }
            let nx = (dx << 8) / len;
            let ny = (dy << 8) / len;
            w.body[s].vx += Fixed::from_raw((speed.to_raw() * nx) >> 8);
            w.body[s].vy += Fixed::from_raw((speed.to_raw() * ny) >> 8);
            w.dynamic[s].asleep = 0;
        }
    });
}
pub fn body_asleep_typed(e: i32) -> i32 {
    with_world(|w| {
        w.slot_of(e)
            .map(|s| w.dynamic[s].asleep as i32)
            .unwrap_or(1)
    })
}
pub fn body_speed2_typed(e: i32) -> i32 {
    with_world(|w| {
        w.slot_of(e)
            .map(|s| {
                let (vx, vy) = (w.body[s].vx.to_raw(), w.body[s].vy.to_raw());
                ((vx >> 4) * (vx >> 4)) + ((vy >> 4) * (vy >> 4))
            })
            .unwrap_or(0)
    })
}
pub fn body_last_hit_typed(e: i32) -> i32 {
    with_world(|w| w.slot_of(e).map(|s| w.dynamic[s].last_hit).unwrap_or(0))
}
pub fn grid_set_surface_typed(col: i32, row: i32, id: i32) {
    with_world(|w| w.grid_set_surface(col, row, id));
}
pub fn surface_def_typed(id: i32, ax: Fixed, ay: Fixed, friction: i32) {
    with_world(|w| {
        let i = (id.clamp(0, 15)) as usize;
        w.surf[i] = SurfaceDef {
            ax,
            ay,
            friction: friction.clamp(0, 256),
        };
    });
}
pub fn pool_new_typed(count: i32, sheet: i32, ox: i32, oy: i32) -> i32 {
    with_world(|w| w.pool_new(count, sheet, ox, oy))
}
pub fn pool_arm_typed(p: i32, slot: i32, kind: i32, ttl: i32) -> i32 {
    with_world(|w| w.pool_arm(p, slot, kind, ttl))
}
pub fn pool_retire_typed(p: i32, slot: i32) {
    with_world(|w| w.pool_retire(p, slot));
}
pub fn pool_clear_typed(p: i32) {
    with_world(|w| w.pool_clear(p));
}
pub fn pool_get_typed(p: i32, slot: i32, field: i32) -> i32 {
    with_world(|w| w.pool_get(p, slot, field))
}
pub fn pool_stat_typed(p: i32, field: i32) -> i32 {
    with_world(|w| w.pool_stat(p, field))
}
pub fn set_stun_typed(e: i32, frames: i32) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.stun[s] = frames.max(0);
        }
    });
}
pub fn is_stunned_typed(e: i32) -> i32 {
    with_world(|w| w.slot_of(e).map(|s| (w.stun[s] > 0) as i32).unwrap_or(0))
}
pub fn set_transform_typed(e: i32, x: Fixed, y: Fixed) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.transform[s] = Transform { x, y };
            w.mask[s] |= C_TRANSFORM;
            w.used |= C_TRANSFORM;
        }
    });
}
pub fn set_body_typed(e: i32, vx: Fixed, vy: Fixed) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.body[s] = Body { vx, vy };
            w.mask[s] |= C_BODY;
            w.used |= C_BODY;
        }
    });
}
pub fn set_collider_typed(e: i32, cw: Fixed, ch: Fixed) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.collider[s] = Collider { w: cw, h: ch };
            w.mask[s] |= C_COLLIDER;
            w.used |= C_COLLIDER;
        }
    });
}
pub fn set_sprite_offset_typed(e: i32, ox: i32, oy: i32) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.sprite[s].ox = ox;
            w.sprite[s].oy = oy;
        }
    });
}
pub fn attach_sprite_typed(e: i32, h: i32) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.sprite[s] = SpriteRef {
                handle: h,
                ox: 0,
                oy: 0,
            };
            w.mask[s] |= C_SPRITE;
            w.used |= C_SPRITE;
        }
    });
    tish_agb::native_sprite_release(h);
}
pub fn set_tag_typed(e: i32, t: i32) {
    with_world(|w| {
        if let Some(s) = w.slot_of(e) {
            w.tag[s] = t;
        }
    });
}
pub fn set_hurt_typed(e: i32, damage: i32, target_tag: i32, despawn_on_hit: i32, stun: i32) {
    with_world(|w| w.set_hurt(e, damage, target_tag, despawn_on_hit != 0, stun));
}
pub fn set_lure_typed(e: i32, radius: i32, frames: i32) {
    with_world(|w| w.set_lure(e, radius, frames));
}
pub fn set_shooter_typed(e: i32, interval: i32, speed: Fixed, aimed: i32) {
    with_world(|w| w.set_shooter(e, interval, speed, aimed != 0));
}
pub fn set_charger_typed(e: i32, speed: i32, band: i32) {
    with_world(|w| w.set_charger(e, speed, band));
}
/// Typed sibling of `topdown_interact`. Without this, ONE `this.interactTD(reach)` call forces a
/// whole component onto the boxed `update:` path — the interact itself is cheap, the wrapper is not.
pub fn topdown_interact_typed(e: i32, reach: i32) -> i32 {
    match topdown_interact(&[Value::Number(e as f64), Value::Number(reach as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn entity_hp_typed(e: i32) -> i32 {
    with_world(|w| match w.slot_of(e) {
        Some(s) if w.has(s, C_HEALTH) => w.health[s].hp,
        _ => 0,
    })
}

pub fn entity_hp_max_typed(e: i32) -> i32 {
    with_world(|w| match w.slot_of(e) {
        Some(s) if w.has(s, C_HEALTH) => w.health[s].max,
        _ => 0,
    })
}

pub fn entity_alive_typed(e: i32) -> i32 {
    with_world(|w| match w.slot_of(e) {
        Some(s) if w.alive[s] => 1,
        _ => 0,
    })
}
pub fn set_lifetime_typed(e: i32, ttl: i32) {
    with_world(|w| w.set_lifetime(e, ttl));
}
pub fn set_despawn_offscreen_typed(e: i32, on: i32) {
    with_world(|w| w.set_despawn_offscreen(e, on != 0));
}
pub fn set_arena_wrap_typed(on: i32) {
    with_world(|w| w.set_arena_wrap(on != 0));
}
pub fn set_mover_typed(e: i32, pattern: i32, vy: Fixed, amp: Fixed, period: i32) {
    with_world(|w| w.set_mover(e, pattern as u8, vy, amp, period));
}
pub fn set_health_typed(e: i32, max: i32, invuln: i32) {
    with_world(|w| w.set_health(e, max, invuln));
}
pub fn damage_typed(e: i32, amount: i32) {
    with_world(|w| w.damage(e, amount));
}
pub fn entity_x_typed(e: i32) -> Fixed {
    with_world(|w| {
        w.slot_of(e)
            .map(|s| w.transform[s].x)
            .unwrap_or(Fixed::from_raw(0))
    })
}
pub fn entity_y_typed(e: i32) -> Fixed {
    with_world(|w| {
        w.slot_of(e)
            .map(|s| w.transform[s].y)
            .unwrap_or(Fixed::from_raw(0))
    })
}
pub fn entity_sprite_typed(e: i32) -> i32 {
    with_world(|w| w.slot_of(e).map(|s| w.sprite[s].handle).unwrap_or(-1))
}
pub fn health_hp_typed(e: i32) -> i32 {
    with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_HEALTH))
            .map(|s| w.health[s].hp)
            .unwrap_or(0)
    })
}
pub fn health_max_typed(e: i32) -> i32 {
    with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_HEALTH))
            .map(|s| w.health[s].max)
            .unwrap_or(0)
    })
}

pub fn topdown_move_typed(e: i32, dx: i32, dy: i32) {
    with_world(|w| w.topdown_move(e, dx, dy));
}

/// Typed twin of `topdown_speed` — px/frame as i32 (hot path; avoid boxed `value_call`).
pub fn topdown_speed_typed(e: i32, px: i32) {
    with_world(|w| w.topdown_speed(e, px as f64));
}

pub fn topdown_facing_typed(e: i32) -> i32 {
    with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_TOPDOWN))
            .map(|s| w.topdown[s].facing)
            .unwrap_or(0)
    })
}

pub fn anim_play_typed(e: i32, from: i32, len: i32, speed: i32, looping: bool) {
    with_world(|w| w.anim_play(e, from, len, speed, looping));
}

/// `defineComponent(name, { start(self,e,dt), update(self,e,dt) })` — register a
/// reusable component type (the Unity-component feel). The callbacks run each frame
/// for every entity the component is attached to.
pub fn define_component(args: &[Value]) -> Value {
    let name = name_of(args, 0);
    let config = args.get(1).cloned().unwrap_or(Value::Null);
    with_world(|w| w.define_component(name, &config));
    Value::Null
}

/// `addBehaviour(entity, name, data)` — attach a defined component to an entity with
/// its own mutable `data` object. `null` name/entity is a no-op.
pub fn add_behaviour(args: &[Value]) -> Value {
    let e = n(args, 0) as i32;
    let name = name_of(args, 1);
    let data = args.get(2).cloned().unwrap_or(Value::Null);
    with_world(|w| {
        if let (Some(s), Some(def)) = (w.slot_of(e), w.def_index_by_name(&name)) {
            w.behaviour[s] = Some(BehaviourInstance {
                def,
                data,
                started: false,
            });
        }
    });
    Value::Null
}

/// `set_camera_target(entity)` — make the camera follow this entity (centred on screen,
/// clamped to the map edges). Required for maps larger than the screen; small maps skip it.
pub fn set_camera_target(args: &[Value]) -> Value {
    with_world(|w| w.set_camera_target(n(args, 0) as i32));
    Value::Null
}

#[no_mangle]
pub fn camera_transitioning(_args: &[Value]) -> Value {
    Value::Bool(with_world(|w| {
        w.room_cam.enabled && w.room_cam.transitioning
    }))
}

#[no_mangle]
pub fn set_room_camera(args: &[Value]) -> Value {
    with_world(|w| w.set_room_camera(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `world_step()` — advance the world one frame. Two phases: (1) **behaviours** —
/// collect each attached component's callback under the world borrow, then invoke
/// them WITHOUT the borrow held (so a callback may re-enter the engine — `set_body`
/// etc. — safely); (2) **systems** — movement + render (pure Rust). Then commit the
/// display via tish-agb (draw, vblank, audio, input). The engine loop's one call/frame.
fn timer_now() -> u32 {
    match tish_agb::timer_read(&[]) {
        Value::Number(n) => n as u32,
        _ => 0,
    }
}

/// Per-phase Timer2 tick breakdown of the last `world_step` (~4389 ticks = one frame): index
/// 0 = total work, 1 = behaviours+ticks, 2 = native systems (movement/combat/life), 3 = collisions +
/// deaths, 4 = health/anim/render/camera. Read with `step_ticks(i)`.
static STEP_TICKS: SingleCore<RefCell<[u32; 5]>> = SingleCore::new(RefCell::new([0; 5]));

/// `step_ticks(phase?)` — Timer2 ticks the previous `world_step` spent (excluding the vblank wait).
/// `phase`: 0/omitted = total, 1 = behaviours+ticks, 2 = native systems, 3 = collisions+deaths,
/// 4 = render group. ~4389 ticks = one 59.7fps frame; over budget = slowdown.
pub fn step_ticks(args: &[Value]) -> Value {
    let i = (n(args, 0) as usize).min(4);
    Value::Number(STEP_TICKS.with(|c| c.borrow()[i]) as f64)
}

/// Worst-case ticks for each `world_step` phase since the last [`step_peak_reset`] — same indices as
/// [`step_ticks`]. Lets one screenshot capture the true per-phase spike (e.g. a boss ring frame).
static STEP_PEAK: SingleCore<RefCell<[u32; 5]>> = SingleCore::new(RefCell::new([0; 5]));

/// `[last_stamp, initialised, peak_period, last_period, ema_period]` — full-frame wall-clock tracker.
static FRAME_PERIOD: SingleCore<RefCell<[u32; 5]>> = SingleCore::new(RefCell::new([0; 5]));

/// `frame_period(mode?)` — the whole frame's Timer2 ticks (game + HUD + main loop + vblank), which
/// `step_ticks` can't see. `mode`: 0/omitted = last frame, 1 = peak since [`step_peak_reset`],
/// 2 = EMA (the stable "typical frame" — use this for sustained cost, the peak is dominated by the
/// one-time boot frame and the last frame is noisy). ~4389 = a clean 60fps frame; a sustained ~8778
/// means the game is holding 30fps.
pub fn frame_period(args: &[Value]) -> Value {
    let mode = n(args, 0) as i32;
    Value::Number(FRAME_PERIOD.with(|c| {
        let p = c.borrow();
        match mode {
            1 => p[2],
            2 => p[4],
            _ => p[3],
        }
    }) as f64)
}

/// `step_peak(phase?)` — the highest `step_ticks(phase)` seen since the last `step_peak_reset`.
pub fn step_peak(args: &[Value]) -> Value {
    let i = (n(args, 0) as usize).min(4);
    Value::Number(STEP_PEAK.with(|c| c.borrow()[i]) as f64)
}

/// `step_peak_reset()` — clear the per-phase worst-case tracker (and the frame-period peak).
pub fn step_peak_reset(_args: &[Value]) -> Value {
    STEP_PEAK.with(|c| *c.borrow_mut() = [0; 5]);
    FRAME_PERIOD.with(|c| {
        let mut p = c.borrow_mut();
        p[1] = 0; // re-arm (next frame re-stamps without counting the gap)
        p[2] = 0; // peak
        p[4] = 0; // EMA re-seeds on the next sample
    });
    Value::Null
}

/// `entity_count()` — how many entities are currently ALIVE (for leak/accumulation diagnostics: a
/// value that climbs and never settles means something isn't despawning).
pub fn entity_count(_args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.alive.iter().filter(|&&a| a).count()) as f64)
}

/// `stun_all(tag, frames)` — freeze every live entity carrying `tag` for `frames`.
///
/// Stun already existed, but only as something a hurt box did to whatever it touched. A screen-wide
/// freeze is a different shape and a common one: the classic action-RPG clock item stops every monster in the room the
/// moment it is picked up, and the same primitive covers a time-stop item, a boss's opening
/// cutscene, or holding the world still while a menu animates in.
///
/// Returns how many entities it froze, so a caller can tell "nothing was there" from "it worked".
pub fn stun_all(args: &[Value]) -> Value {
    Value::Number(stun_all_typed(n(args, 0) as i32, n(args, 1) as i32) as f64)
}

/// Typed twin of `stun_all` — see there.
pub fn stun_all_typed(tag: i32, frames: i32) -> i32 {
    with_world(|w| {
        let mut hit = 0;
        for s in 0..w.alive.len() {
            if w.alive[s] && w.tag.get(s).copied().unwrap_or(0) == tag {
                w.stun[s] = w.stun[s].max(frames);
                hit += 1;
            }
        }
        hit
    })
}

/// `entity_count_tag(tag)` — how many live entities carry `tag`. Counting a tag needed a Tish loop
/// over every slot before, which is why "is this room cleared yet?" was never actually asked: a
/// dungeon shutter has to notice the last enemy dying on the frame it happens.
/// `nearest_tag(entity, tag, radius)` — the nearest OTHER entity carrying `tag` within `radius`
/// px (manhattan, box centers), or -1. Not gated on the off-screen cull: an ecosystem's
/// creatures keep mattering off screen. O(entities) — stagger calls, don't ask every frame.
pub fn nearest_tag(args: &[Value]) -> Value {
    let r = with_world(|w| w.nearest_tag(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Number(r as f64)
}

/// See [`nearest_tag`].
pub fn nearest_tag_typed(e: i32, tag: i32, radius: i32) -> i32 {
    with_world(|w| w.nearest_tag(e, tag, radius))
}

/// `entity_dist(a, b)` — manhattan distance between two entities' box centers in whole px, or -1
/// if either id no longer resolves.
pub fn entity_dist(args: &[Value]) -> Value {
    let r = with_world(|w| w.entity_dist(n(args, 0) as i32, n(args, 1) as i32));
    Value::Number(r as f64)
}

/// See [`entity_dist`].
pub fn entity_dist_typed(a: i32, b: i32) -> i32 {
    with_world(|w| w.entity_dist(a, b))
}

pub fn entity_count_tag(args: &[Value]) -> Value {
    let want = match args.first() {
        Some(Value::Number(n)) => *n as i32,
        _ => 0,
    };
    Value::Number(with_world(|w| {
        let mut n = 0;
        for (i, &a) in w.alive.iter().enumerate() {
            if a && w.tag.get(i).copied().unwrap_or(0) == want {
                n += 1;
            }
        }
        n
    }) as f64)
}

/// `entity_slots()` — the SoA store's slot high-water mark (`alive.len()`), which only grows. The
/// per-frame system loops iterate this many slots, so a big gap between it and `entity_count` means a
/// past burst is still taxing every frame (the O(n) / O(n²) passes walk dead slots too).
pub fn entity_slots(_args: &[Value]) -> Value {
    Value::Number(with_world(|w| w.alive.len()) as f64)
}

// ── S4: which SYSTEM ate the frame ──────────────────────────────────────────
// `step_ticks` attributes the frame to a PHASE; this attributes phases 2 and 4 to a SYSTEM. Off by
// default — profiling costs two timer reads per system per frame — `sys_prof(1)` turns it on for a
// profiling run. Indices are the CALL ORDER in `world_step`; read names with `sys_name(i)` so a log
// reader never hard-codes this table.
//
// WHY THIS EXISTS: "most games lag at a certain point" and the only tool was a five-phase split.
// The lag mechanism is ~30 systems each scanning every slot per frame (~60 ticks per live entity,
// and an EMPTY reserved slot still bills ~24 across the pass) — but WHICH systems carry that cost
// in a given game was a bisect. Now it is a log line.
const SYS_N: usize = 32;
static SYS_NAMES: [&str; SYS_N] = [
    "patrol",
    "mover",
    "boomerang",
    "hopper",
    "jumper",
    "movement",
    "grid",
    "platformer",
    "chase",
    "soldier",
    "seek",
    "shooter",
    "charger",
    "trap",
    "wanderer",
    "nai",
    "topdown",
    "dynamic",
    "wrap",
    "follow",
    "diranim",
    "grabber",
    "combat",
    "life",
    "health",
    "anim",
    "walk",
    "room_free",
    "room_trans",
    "fog",
    "render",
    "camera",
];
static SYS_PROF: SingleCore<RefCell<bool>> = SingleCore::new(RefCell::new(false));
static SYS_TICKS: SingleCore<RefCell<[u32; SYS_N]>> = SingleCore::new(RefCell::new([0; SYS_N]));

/// Run one system, attributing its ticks when profiling is on. A fn POINTER, not a closure, so the
/// call is equally direct either way and the `prof` branch is the only overhead when off.
fn ts(w: &mut World, prof: bool, i: usize, need: u32, need2: u32, f: fn(&mut World)) {
    // S1: a system whose defining component has never been attached this scene has nothing to scan
    // for — measured at ~1.5 ticks per slot per system of pure mask-checking otherwise, which is
    // where "most games lag at a certain point" lived. `need == need2 == 0` means "always runs".
    if (need != 0 || need2 != 0) && (w.used & need) == 0 && (w.used2 & need2) == 0 {
        return;
    }
    if !prof {
        f(w);
        return;
    }
    let t0 = timer_now();
    f(w);
    let dt = timer_now().wrapping_sub(t0) & 0xFFFF;
    SYS_TICKS.with(|c| c.borrow_mut()[i] += dt);
}

/// `sys_prof(on)` — enable per-system tick attribution (see `sys_ticks`).
pub fn sys_prof(args: &[Value]) -> Value {
    SYS_PROF.with(|c| *c.borrow_mut() = n(args, 0) as i32 != 0);
    Value::Null
}
/// See [`sys_prof`].
pub fn sys_prof_typed(on: i32) {
    SYS_PROF.with(|c| *c.borrow_mut() = on != 0);
}

/// `sys_ticks(i)` — Timer2 ticks system `i` spent LAST frame; 0 unless `sys_prof(1)` is on.
pub fn sys_ticks(args: &[Value]) -> Value {
    let i = (n(args, 0) as usize).min(SYS_N - 1);
    Value::Number(SYS_TICKS.with(|c| c.borrow()[i]) as f64)
}
/// See [`sys_ticks`].
pub fn sys_ticks_typed(i: i32) -> i32 {
    SYS_TICKS.with(|c| c.borrow()[(i as usize).min(SYS_N - 1)]) as i32
}

/// `sys_count()` — how many systems `sys_ticks` indexes.
pub fn sys_count(_args: &[Value]) -> Value {
    Value::Number(SYS_N as f64)
}
/// See [`sys_count`].
pub fn sys_count_typed() -> i32 {
    SYS_N as i32
}

/// `sys_name(i)` — the system's name, for logs. Boxed-only on purpose: it is read a handful of
/// times per profiling run, never on a hot path.
pub fn sys_name(args: &[Value]) -> Value {
    let i = (n(args, 0) as usize).min(SYS_N - 1);
    Value::String(SYS_NAMES[i].into())
}

pub fn world_step(_args: &[Value]) -> Value {
    // Full-frame period: world_step runs once per frame and ends with the vblank wait, so the gap
    // between consecutive starts is the WHOLE frame's wall-clock (game logic + HUD + the main loop +
    // vblank). ~4389 Timer2 ticks = a 60fps frame; a sustained higher value = dropped frames. This is
    // the only figure that captures cost OUTSIDE world_step (e.g. the tish HUD draw).
    {
        let now = timer_now();
        FRAME_PERIOD.with(|c| {
            let mut p = c.borrow_mut();
            let period = now.wrapping_sub(p[0]) & 0xFFFF;
            p[0] = now;
            if p[1] != 0 {
                p[3] = period; // instantaneous last-frame period
                if period > p[2] {
                    p[2] = period; // peak (skip the very first, which has no valid previous stamp)
                }
                // EMA (÷16) — a stable "typical frame" read that a single noisy screenshot can trust.
                // Seed on the first real sample so it converges immediately rather than ramping from 0.
                p[4] = if p[4] == 0 {
                    period
                } else {
                    (p[4] as i32 + (period as i32 - p[4] as i32) / 16) as u32
                };
            }
            p[1] = 1;
        });
    }
    let work_start = timer_now();
    // Phase 1 — behaviour update (reentrant): collect, drop borrow, then call.
    let num_updates = with_world(|w| {
        w.collect_behaviours();
        w.buf_updates.len()
    });
    for i in 0..num_updates {
        let (callback, data, entity) = with_world(|w| w.buf_updates[i].clone());
        value_call(
            &callback,
            &[
                data,
                Value::Number(entity as f64),
                Value::Number(1.0),
                Value::native(set_hopper),
                Value::native(set_jumper),
            ],
        );
    }
    // Phase 1b — fast `tick` hooks: fill each entity's data ctx with state, call the hook (ONE
    // tish call, no per-op ABI trip), read decisions back, apply. Reentrancy-safe (no borrow held
    // during the callback), same as the behaviour dispatch.
    let num_ticks = with_world(|w| {
        w.collect_ticks();
        w.buf_ticks.len()
    });
    if num_ticks > 0 {
        with_world(|w| w.buf_results.clear());
        for i in 0..num_ticks {
            let job = with_world(|w| w.buf_ticks[i].clone());
            // Lean tick: call with just the entity id — no boxed ctx, no marshalling, no readback.
            if job.lean {
                value_call(&job.cb, &[Value::Number(job.entity as f64)]);
                continue;
            }
            set_prop(&job.data, "e", Value::Number(job.entity as f64));
            set_prop(&job.data, "x", Value::Number(job.x as f64));
            set_prop(&job.data, "y", Value::Number(job.y as f64));
            // Platformer entities get the 9 gravity/jump props; free-movers skip them entirely (each
            // set_prop/prop_num is a boxed hashmap op — a real cost at bullet-hell tick counts).
            if job.platformer {
                set_prop(&job.data, "grounded", Value::Bool(job.grounded));
                set_prop(&job.data, "blocked", Value::Bool(job.blocked));
                set_prop(&job.data, "move", Value::Number(0.0));
                set_prop(&job.data, "jump", Value::Bool(false));
                set_prop(&job.data, "jumpCut", Value::Bool(false));
                set_prop(&job.data, "run", Value::Bool(false));
                set_prop(&job.data, "drop", Value::Bool(false));
                set_prop(&job.data, "flip", Value::Bool(false));
                set_prop(&job.data, "bounce", Value::Number(0.0));
            }
            // Free-movement velocity: seed with the current heading so a hook that leaves vx/vy alone
            // keeps flying straight (and one that stops sets them to 0).
            if job.body {
                set_prop(&job.data, "vx", Value::Number(job.vx));
                set_prop(&job.data, "vy", Value::Number(job.vy));
            }
            // Pass the ctx by reference (the hook mutates it in place and we read outputs back after)
            // — a `job.data.clone()` here is a DEEP copy of the whole object every tick every frame,
            // which dominated the frame at any real enemy count.
            value_call(&job.cb, core::slice::from_ref(&job.data));
            let mut out = TickOut::default();
            if job.platformer {
                out.move_dir = prop_num(&job.data, "move") as i32;
                out.jump = prop_truthy(&job.data, "jump");
                out.jump_cut = prop_truthy(&job.data, "jumpCut");
                out.run = prop_truthy(&job.data, "run");
                out.drop = prop_truthy(&job.data, "drop");
                out.flip = prop_truthy(&job.data, "flip");
                out.bounce = prop_num(&job.data, "bounce") as i32;
            }
            if job.body {
                out.vx = prop_num(&job.data, "vx");
                out.vy = prop_num(&job.data, "vy");
            }
            with_world(|w| w.buf_results.push((job.entity, out)));
        }
        with_world(|w| {
            for j in 0..w.buf_results.len() {
                let (e, out) = {
                    let tuple = &w.buf_results[j];
                    (tuple.0, tuple.1)
                };
                w.apply_tick(e, &out);
            }
        });
    }
    let t_p1 = timer_now();
    // S4: read the profiling flag once and zero the attribution for this frame's system work.
    let prof = SYS_PROF.with(|c| *c.borrow());
    if prof {
        SYS_TICKS.with(|c| *c.borrow_mut() = [0; SYS_N]);
    }
    // Phase 2 — native AI + movement + grid stepping + platformer physics (pure Rust).
    with_world(|w| {
        ts(w, prof, 0, C_PATROL, 0, World::patrol_system); // sets move intent for patrol enemies before the platformer integrates it
        ts(w, prof, 1, C_MOVER, 0, World::mover_system); // native shmup movement patterns → Body velocity, before integration
                                                         // Boomerang return-mover: after N frames reverse Body velocity toward the owner. Runs after
                                                         // `mover_system` (so a weave pattern doesn't overwrite the return heading) and before
                                                         // `movement_system` integrates. Pair with `set_lifetime` / `set_despawn_offscreen`.
        ts(w, prof, 2, C_BOOMERANG, 0, World::boomerang_system);
        ts(w, prof, 4, C_JUMPER, 0, World::jumper_system);
        ts(w, prof, 5, C_BODY, 0, World::movement_system);
        ts(w, prof, 6, C_GRIDPOS, 0, World::grid_system);
        ts(
            w,
            prof,
            7,
            C_PLATFORMER,
            M2_CARRIER,
            World::platformer_system,
        );
        ts(w, prof, 8, C_CHASE, 0, World::chase_system); // native top-down enemy AI (sets intent + anim) — no per-frame tish tick
                                                         // ⚠️ HOPPER RUNS AFTER CHASE, OUT OF NUMERIC ORDER, ON PURPOSE. `chase_system`'s idle path
                                                         // writes dx=dy=0 every frame — that zero is what stops a chaser when the player leaves its
                                                         // aggro band — so any intent written before it is erased. When hopper ran at slot 3, a
                                                         // creature armed with BOTH (every pooled creature in the reporting game carried a chase) never hopped at
                                                         // all: measured frozen at its spawn for an entire OAM capture, and un-frozen the moment the
                                                         // chase was dropped. The pipeline's rule is already "a lined-up charger overrides whatever
                                                         // intent the above set" — the hop is the same kind of specialist and wins the same way.
                                                         // The index stays 3: it is this system's attribution ID, not its position.
        ts(w, prof, 3, C_HOPPER, 0, World::hopper_system);
        // RTS pair, in this order: `soldier_system` decides whether a unit is fighting, then
        // `seek_system` walks everyone who is not. Reversing them would spend a frame's movement on
        // a unit that is already in combat, which reads as enemies sliding while they swing.
        ts(w, prof, 9, C_SOLDIER, 0, World::soldier_system);
        ts(w, prof, 10, C_SEEK, 0, World::seek_system);
        ts(w, prof, 11, C_SHOOTER, 0, World::shooter_system); // ranged enemies fire on their own timer
        ts(w, prof, 12, C_CHARGER, 0, World::charger_system); // …and a lined-up charger overrides whatever intent the above set
        ts(w, prof, 13, C_TRAP, 0, World::trap_system); // blade traps: inert until lined up, then dash (after charger so both compose)
        ts(w, prof, 14, 0, M2_WANDERER, World::wanderer_system); // continuous tile-aligned walkers → intent, before integration
        ts(w, prof, 15, 0, M2_NAI, World::nai_system); // NES-era AI (ambusher/drifter/flicker-caster/bouncer) → intent, before integration
        ts(w, prof, 16, C_TOPDOWN, 0, World::topdown_system); // free 8-dir action-RPG movement + tile collision (top-down genre)
                                                              // Rigid discs (golf, soccer): surfaces, tile bounce, disc-vs-disc, sleep. The position is
                                                              // FORCED by the two comments around it — the agents must have moved already, so the ball
                                                              // reacts to where they are, and the goal trigger in `combat_system` must see the resolved
                                                              // position rather than the pre-contact one.
        ts(w, prof, 17, C_DYNAMIC, 0, World::dynamic_system);
        // Toroidal arena (Asteroids), after everything that writes a position and BEFORE combat +
        // lifetimes — so a shot that crosses an edge is re-entering rather than "off-screen", and
        // the collision below sees where things actually are.
        ts(w, prof, 18, 0, 0, World::wrap_system);
        // Parent-linked parts / train segments / orbiters: re-snap after parents have moved.
        ts(w, prof, 19, C_FOLLOW, 0, World::follow_system);
        ts(w, prof, 20, C_DIRANIM, 0, World::diranim_system); // facing -> frame, after every system that can turn an entity
                                                              // Shoot-'em-up core: resolve contact damage (bullets → enemies, hazards → player), then
                                                              // retire spent bullets / off-screen entities — before collision + death dispatch below, so a
                                                              // bullet's kill flows through the normal `onDeath` and a spent shot neither hits twice nor
                                                              // renders. Cheap no-ops for a game that uses neither component.
        ts(w, prof, 21, C_GRABBER, 0, World::grabber_system); // stun-on-overlap before combat so a grab lands this frame
        ts(w, prof, 22, C_HURT, 0, World::combat_system);
        ts(w, prof, 23, C_LIFE, 0, World::life_system);
    });
    let t_p2 = timer_now();
    // Phase 3 — collision: detect overlaps, then dispatch onCollide (reentrant).
    let collisions = with_world(|w| w.collect_collisions());
    for (callback, data, me, other) in collisions {
        value_call(
            &callback,
            &[data, Value::Number(me as f64), Value::Number(other as f64)],
        );
    }
    // Phase 3b — deaths: dispatch each dead entity's onDeath (reentrant — may respawn / load a
    // scene), or despawn it if it has no onDeath hook.
    let deaths = with_world(|w| w.collect_deaths());
    for (callback, data, entity) in deaths {
        if matches!(callback, Value::Null) {
            // ⚠️ A POOLED ENTITY'S DEATH IS THE POOL OWNER'S TO NOTICE — a no-op here, not a retire
            // and not a despawn. Despawning would free a pooled sprite the pool still holds a handle
            // to; auto-retiring would look tidier and break the caller, because `castReapOne` reads
            // the corpse's `entity_x`/`entity_y` on a LATER frame to place the drop, and a retire
            // clears the transform first. Doing nothing is exactly today's semantics, so no game
            // changes when the pool arrives.
            let pooled = with_world(|w| {
                w.slot_of(entity)
                    .map(|s| w.pool_of[s] >= 0)
                    .unwrap_or(false)
            });
            if !pooled {
                with_world(|w| w.despawn(entity));
            }
        } else {
            value_call(&callback, &[data, Value::Number(entity as f64)]);
        }
    }
    let t_p3 = timer_now();
    // Phase 4 — i-frames + animation + room-slide + render + camera (pure Rust).
    with_world(|w| {
        ts(w, prof, 24, C_HEALTH, 0, World::health_system);
        ts(w, prof, 25, C_ANIM, 0, World::anim_system);
        ts(w, prof, 26, C_WALK, 0, World::walk_system);
        ts(w, prof, 27, 0, 0, World::room_track_free); // side-scroller: start a room slide when the player crosses a boundary
        ts(w, prof, 28, 0, 0, World::room_transition_system); // slide the player mid-transition before render reads its pos
                                                              // Fog last among the simulation systems: it must see where everything ENDED UP this frame,
                                                              // or the shroud trails a moving army by one frame. The blit itself is the game's call.
        ts(w, prof, 29, 0, 0, World::fog_system);
        ts(w, prof, 30, C_SPRITE, 0, World::render_system);
        ts(w, prof, 31, 0, 0, World::update_camera);
    });
    let work_end = timer_now();
    let d = |a: u32, b: u32| b.wrapping_sub(a) & 0xFFFF;
    let now = [
        d(work_start, work_end),
        d(work_start, t_p1),
        d(t_p1, t_p2),
        d(t_p2, t_p3),
        d(t_p3, work_end),
    ];
    STEP_TICKS.with(|c| *c.borrow_mut() = now);
    // Running worst-case per phase since the last reset — a single HUD read then shows the true spike
    // (a bullet-hell ring frame) regardless of which frame the screenshot lands on.
    STEP_PEAK.with(|c| {
        let mut p = c.borrow_mut();
        for i in 0..5 {
            if now[i] > p[i] {
                p[i] = now[i];
            }
        }
    });
    tish_agb::frame(&[]);
    Value::Null
}

// ═══════════════════════════════════════════════════════════════════════════════
// Iso grid — a reusable isometric, height-mapped board with SRPG-style movement.
// Standalone global state (independent of the SoA `World`): a game renders however it likes and uses
// these for the logical grid, move-range flood fill, and pathfinding. See the SRPG plan doc outside this repo.
// ═══════════════════════════════════════════════════════════════════════════════

#[derive(Clone, Copy)]
struct IsoBoardCell {
    height: u8, // elevation in steps (raises the tile + whatever stands on it)
    tile: u8,   // terrain/type id (rendering + move-cost hooks)
    walkable: bool,
    occupant: i32, // unit id standing here, or -1
}

/// The reference SRPG's turn-counter costs. A unit is charged 500 simply for having had a turn, another 300 if it
/// moved and another 200 if it acted — so a unit that does everything pays the full 1000 and starts
/// its next wait from zero, while one that stands still pays 500 and is already halfway there.
///
/// That difference is the whole reason to model it: Wait stops being "the button you press when you
/// have nothing to do" and becomes a tempo play, because holding position buys the unit its next
/// turn twice as soon.
const COST_TURN: i32 = 500;
const COST_MOVE: i32 = 300;
const COST_ACTION: i32 = 200;

/// How much counter a unit may carry PAST the threshold while waiting for slower units to catch up.
/// Without a cap, a fast unit parked behind a queue of slow ones banks arbitrarily much and then
/// takes several turns in a row the moment it is called; the reference SRPG caps the overflow, which is why a unit
/// never needs more than 1500 to be guaranteed its turn.
const RESERVE_MAX: i32 = 500;

/// A unit's logical state (position/team/stats/turn-counter). The game owns the sprite; the engine
/// owns this. The reference SRPG's turn order is speed-based: `ct` accrues by `speed`, and the first to reach the
/// threshold acts.
#[derive(Clone, Copy)]
struct IsoBoardUnit {
    col: i32,
    row: i32,
    team: u8,
    speed: u16,
    mov: u8,
    jump: u8,
    hp: i16,
    max_hp: i16,
    ct: i32,
    alive: bool,
    /// A flier ignores terrain cost and height deltas — it still cannot enter a solid cell or one
    /// another unit occupies. This is a movement *type*, not a bonus: it does not extend the step
    /// budget, it changes which tiles that budget can spend itself on.
    flying: bool,
    /// Percent applied to `speed` when the turn counter ticks; 100 is normal. This is how Haste and
    /// Slow are expressed — the reference SRPG's own rule is that Haste adds double Speed per tick and Slow adds
    /// half, rounding down, which is 200 and 50 through integer division.
    ///
    /// It scales the ACCRUAL rather than the stat, so nothing that reads `speed` (the AI's threat
    /// tables, the Status screen) has to know the unit is hasted, and the effect lasts exactly as
    /// long as the game leaves the scale set.
    speed_scale: u16,
}

struct IsoBoardGrid {
    pub w: i32,
    pub h: i32,
    cells: Vec<IsoBoardCell>,
    units: Vec<IsoBoardUnit>,
    in_move: Vec<bool>,     // reachable mask from the last move_range
    parent: Vec<i32>,       // BFS parent cell index (-1) for path reconstruction
    reach: Vec<(i16, i16)>, // reachable (col,row) list, in BFS order
    start: (i32, i32),      // origin of the last move_range (for isob_path)
    path: Vec<(i16, i16)>,  // last reconstructed path (start..=target)
    /// Move points to ENTER a cell, indexed by its terrain id. The engine owns the search; the game
    /// owns what its terrain means, so this is filled from the example via `isob_set_terrain_cost`
    /// and defaults to 1 (flat cost) for every id — a game that never calls it gets the old
    /// uniform-cost behaviour exactly.
    cost: [u8; 256],
    /// Reused across calls so a move-range query allocates nothing. The AI asks for a range once per
    /// unit per turn, but this also runs inside the player's cursor loop.
    dist: Vec<i32>,
    /// Cells adjacent to a living enemy of whoever is currently being pathed. Rebuilt per query (it
    /// depends on the mover's team) into a buffer that is allocated once.
    zoc: Vec<bool>,
    /// Whether zone-of-control is in force at all. Off by default, so a game that never opts in keeps
    /// the movement rules it already had.
    zoc_on: bool,
}

impl IsoBoardGrid {
    const fn new() -> Self {
        IsoBoardGrid {
            w: 0,
            h: 0,
            cells: Vec::new(),
            units: Vec::new(),
            in_move: Vec::new(),
            parent: Vec::new(),
            reach: Vec::new(),
            start: (0, 0),
            path: Vec::new(),
            cost: [1u8; 256],
            dist: Vec::new(),
            zoc: Vec::new(),
            zoc_on: false,
        }
    }
    fn idx(&self, c: i32, r: i32) -> Option<usize> {
        if c >= 0 && r >= 0 && c < self.w && r < self.h {
            Some((r * self.w + c) as usize)
        } else {
            None
        }
    }
    fn init(&mut self, w: i32, h: i32) {
        self.w = w.max(0);
        self.h = h.max(0);
        let n = (self.w * self.h) as usize;
        self.cells =
            alloc::vec![IsoBoardCell { height: 0, tile: 0, walkable: true, occupant: -1 }; n];
        self.in_move = alloc::vec![false; n];
        self.parent = alloc::vec![-1i32; n];
        self.dist = alloc::vec![i32::MAX; n];
        self.zoc = alloc::vec![false; n];
        self.reach.clear();
        self.path.clear();
        self.units.clear();
    }

    fn add_unit(
        &mut self,
        col: i32,
        row: i32,
        team: u8,
        speed: u16,
        mov: u8,
        jump: u8,
        hp: i16,
    ) -> i32 {
        let id = self.units.len() as i32;
        self.units.push(IsoBoardUnit {
            col,
            row,
            team,
            speed,
            mov,
            jump,
            hp,
            max_hp: hp,
            ct: 0,
            alive: true,
            flying: false,
            speed_scale: 100,
        });
        if let Some(i) = self.idx(col, row) {
            self.cells[i].occupant = id;
        }
        id
    }

    /// Move a unit to (c,r): clears its old cell's occupant and claims the new one.
    fn unit_set_pos(&mut self, id: i32, c: i32, r: i32) {
        let (oc, or) = match self.units.get(id as usize) {
            Some(u) => (u.col, u.row),
            None => return,
        };
        if let Some(i) = self.idx(oc, or) {
            if self.cells[i].occupant == id {
                self.cells[i].occupant = -1;
            }
        }
        if let Some(i) = self.idx(c, r) {
            self.cells[i].occupant = id;
        }
        if let Some(u) = self.units.get_mut(id as usize) {
            u.col = c;
            u.row = r;
        }
    }

    /// What a unit actually adds to its turn counter per tick, after Haste or Slow.
    ///
    /// Floored at 1, which matters: a slow caster's Speed can be low enough that halving it rounds to
    /// zero, and a unit accruing zero never reaches the threshold — it would not be slowed, it would
    /// be removed from the game until the status wore off, and `turn_next` would spin looking for a
    /// tick count that lets it act.
    fn tick_speed(u: &IsoBoardUnit) -> i32 {
        (((u.speed as i32) * (u.speed_scale as i32)) / 100).max(1)
    }

    /// Advance the speed-based turn counters until an alive unit reaches the threshold; returns that
    /// unit's id (the highest counter breaks ties), or -1 if no unit is alive.
    fn turn_next(&mut self) -> i32 {
        const THRESH: i32 = 1000;
        let mut best_t = i32::MAX;
        for u in &self.units {
            if !u.alive {
                continue;
            }
            let sp = Self::tick_speed(u);
            let t = (THRESH - u.ct + sp - 1) / sp; // ceil ticks to reach threshold
            if t < best_t {
                best_t = t;
            }
        }
        if best_t == i32::MAX {
            return -1;
        }
        for i in 0..self.units.len() {
            if self.units[i].alive {
                let sp = Self::tick_speed(&self.units[i]);
                self.units[i].ct = (self.units[i].ct + best_t * sp).min(THRESH + RESERVE_MAX);
            }
        }
        let mut who = -1i32;
        let mut hi = i32::MIN;
        for (i, u) in self.units.iter().enumerate() {
            if u.alive && u.ct >= THRESH && u.ct > hi {
                hi = u.ct;
                who = i as i32;
            }
        }
        if who >= 0 {
            // Only the BASE cost of having had a turn comes off here. What the unit does with the
            // turn costs more, and it is not known yet — `turn_end` charges the rest.
            self.units[who as usize].ct -= COST_TURN;
        }
        who
    }

    /// Weighted flood fill: mark every cell reachable from (sc,sr) for `budget` move points, where
    /// entering a cell costs `self.cost[tile]` and each step's height delta must be ≤ `jump`. Blocked
    /// by unwalkable tiles and by other units' occupied cells (the start cell is always allowed).
    /// A `flying` mover pays 1 for every cell and ignores height entirely. Records a parent tree so
    /// `path_to` can walk back.
    ///
    /// This is Dial's algorithm rather than a queue-based Dijkstra: costs are small positive integers
    /// and the budget is a single-digit stat, so sweeping the board once per cost level is both
    /// simpler to read and allocation-free, where a priority queue would want a heap on a machine
    /// that has 256 KB of it. The board is tens of cells, so the O(budget × n × 4) is nothing.
    ///
    /// Correctness depends on every cost being ≥ 1, which `set_terrain_cost` enforces: a zero-cost
    /// tile would let a later sweep improve an already-settled cell and the parent tree would then
    /// disagree with the distances.
    ///
    /// `team` is the mover's, used only for zone of control; pass -1 to path with no ZoC at all.
    /// When ZoC is on, stepping into a tile adjacent to a living enemy ENDS the move: the tile is
    /// reachable, but nothing is reachable through it. The tile the unit starts on is exempt, or a
    /// unit that began its turn next to an enemy could never move again — the rule is meant to stop
    /// people running *past* a defender, not to freeze whoever is already in contact.
    fn move_range(&mut self, sc: i32, sr: i32, budget: i32, jump: i32, flying: bool, team: i32) {
        let n = self.cells.len();
        for i in 0..n {
            self.in_move[i] = false;
            self.parent[i] = -1;
            self.dist[i] = i32::MAX;
            self.zoc[i] = false;
        }
        if self.zoc_on && team >= 0 {
            for u in 0..self.units.len() {
                let e = self.units[u];
                if !e.alive || e.team as i32 == team {
                    continue;
                }
                const AROUND: [(i32, i32); 4] = [(1, 0), (-1, 0), (0, 1), (0, -1)];
                for (dc, dr) in AROUND {
                    if let Some(i) = self.idx(e.col + dc, e.row + dr) {
                        self.zoc[i] = true;
                    }
                }
            }
        }
        self.reach.clear();
        self.start = (sc, sr);
        let start = match self.idx(sc, sr) {
            Some(i) => i,
            None => return,
        };
        self.dist[start] = 0;
        self.in_move[start] = true;
        self.reach.push((sc as i16, sr as i16));
        // One sweep per cost level, in increasing order. `reach` therefore comes out ordered by
        // distance, which is what the callers that read it in order expect.
        for d in 0..budget {
            for cur in 0..n {
                if self.dist[cur] != d {
                    continue;
                }
                if cur != start && self.zoc[cur] {
                    continue; // reached, but the enemy's reach ends the move here
                }
                let cc = (cur as i32) % self.w;
                let cr = (cur as i32) / self.w;
                let ch = self.cells[cur].height as i32;
                const DIRS: [(i32, i32); 4] = [(1, 0), (-1, 0), (0, 1), (0, -1)];
                for (dc, dr) in DIRS {
                    let (nc, nr) = (cc + dc, cr + dr);
                    let ni = match self.idx(nc, nr) {
                        Some(i) => i,
                        None => continue,
                    };
                    let cell = self.cells[ni];
                    if !cell.walkable || cell.occupant != -1 {
                        continue; // blocked terrain or another unit stands there
                    }
                    if !flying && (cell.height as i32 - ch).abs() > jump {
                        continue; // too steep to climb/drop
                    }
                    let step = if flying {
                        1
                    } else {
                        self.cost[cell.tile as usize] as i32
                    };
                    let nd = d + step;
                    if nd > budget || nd >= self.dist[ni] {
                        continue;
                    }
                    if self.dist[ni] == i32::MAX {
                        self.reach.push((nc as i16, nr as i16));
                    }
                    self.dist[ni] = nd;
                    self.parent[ni] = cur as i32;
                    self.in_move[ni] = true;
                }
            }
        }
    }

    /// Where a shove from (sc,sr) would put unit `id`, and how far it would fall getting there —
    /// `None` if nothing would move. The single home of the knockback rule, so that resolving a shove
    /// and predicting one cannot disagree.
    fn knock_dest(&self, id: i32, sc: i32, sr: i32) -> Option<(i32, i32, i32)> {
        let u = match self.units.get(id as usize) {
            Some(u) if u.alive => *u,
            _ => return None,
        };
        let (dc, dr) = (u.col - sc, u.row - sr);
        if dc == 0 && dr == 0 {
            return None; // no direction to push along
        }
        // Cardinal push along whichever axis the victim lies furthest down, so a diagonal blow still
        // resolves to one of the four directions the board actually has.
        let (sx, sy) = if dc.abs() >= dr.abs() {
            (if dc >= 0 { 1 } else { -1 }, 0)
        } else {
            (0, if dr >= 0 { 1 } else { -1 })
        };
        let (nc, nr) = (u.col + sx, u.row + sy);
        let ni = self.idx(nc, nr)?;
        if !self.cells[ni].walkable || self.cells[ni].occupant != -1 {
            return None;
        }
        let here = self.idx(u.col, u.row)?;
        Some((
            nc,
            nr,
            self.cells[here].height as i32 - self.cells[ni].height as i32,
        ))
    }

    /// Reconstruct the path from the last `move_range` start to (tc,tr) if it's in range.
    fn path_to(&mut self, tc: i32, tr: i32) {
        self.path.clear();
        let ti = match self.idx(tc, tr) {
            Some(i) if self.in_move[i] => i,
            _ => return,
        };
        let mut chain: Vec<(i16, i16)> = Vec::new();
        let mut cur = ti as i32;
        while cur >= 0 {
            let c = cur % self.w;
            let r = cur / self.w;
            chain.push((c as i16, r as i16));
            let p = self.parent[cur as usize];
            if cur == p {
                break;
            }
            cur = p;
        }
        chain.reverse(); // start .. target
        self.path = chain;
    }
}

static ISO_GRID: SingleCore<RefCell<IsoBoardGrid>> =
    SingleCore::new(RefCell::new(IsoBoardGrid::new()));

fn with_iso_grid<R>(f: impl FnOnce(&mut IsoBoardGrid) -> R) -> R {
    ISO_GRID.with(|c| f(&mut c.borrow_mut()))
}

// ── Baked iso boards (the `isoboard:` import scheme) ───────────────────────
/// A whole battlefield baked from a Tiled `.tmj` at build time by `include_isoboard!`: the floor
/// background handle plus per-cell terrain/elevation/walkability and the unit spawns. `isob_load`
/// builds the grid straight from it and the game reads the spawns to place units — so the map comes
/// entirely from the Tiled file, with no hand-generated `.tish` data module and no import script.
pub struct IsoBoard {
    pub bg: i32,
    pub w: i32,
    pub h: i32,
    /// Iso projection origin used when the floor atlas was baked (must match game `isoX`/`isoY`).
    pub ox: i32,
    pub oy: i32,
    /// Pixels the floor art rises per elevation unit, as BAKED.
    ///
    /// Normally 8 — Tiled's half-block convention, and what `ISO_LIFT_PER_STEP` assumes. But a board
    /// imported from external board data raises a level by whatever its source chose: the SRPG demos use **2**. A unit
    /// must be lifted by the same number the ART was lifted by, or it stands 6px per level above the
    /// ground it is on — invisible on a flat board, wrong on every terraced one.
    pub lift: i32,
    pub frames: &'static [u8],
    pub heights: &'static [u8],
    pub walk: &'static [u8],
    // Per-cell RAISED stack (the tiles ABOVE ground), in CSR form: cell `i`'s entries are
    // `stack_[elev|tile][stack_off[i] .. stack_off[i+1]]`. Each entry is one block: its TOP elevation
    // (8px units) and its OWN tile frame — so the render draws every level with its real tile, and a
    // tower can mix half/full blocks and different tiles.
    pub stack_off: &'static [u16],
    pub stack_elev: &'static [u8],
    pub stack_tile: &'static [u8],
    pub spawns: &'static [(u8, u8, u8, u8)], // (col, row, cls, team)
    /// The background layer's size in PIXELS. NOT the same as the atlas or the board's own extent:
    /// a direct-upload board is 512x256 (a 64x32 layer), a composited one is a square canvas. The
    /// camera must clamp to THIS or it scrolls past the end of the map and shows the wrap — terrain,
    /// a band of bare backdrop, then terrain again.
    pub mapw: i32,
    pub maph: i32,
    /// Right/bottom edge of the PAINTED content, in pixels. The camera clamps to this; the board's
    /// cell diamond is smaller than what actually gets drawn.
    pub cw: i32,
    pub ch: i32,
    /// One BGR555 backdrop colour per visible scanline, or empty for a flat backdrop.
    ///
    /// The GBA's backdrop is a single palette word, so a board whose real sky is a vertical ramp can
    /// only reproduce it by rewriting that word every scanline. `isob_load` hands this to
    /// `tish_agb::native_sky_set`, which runs it off HBlank DMA. Empty disarms — a board without a
    /// captured sky must not inherit the previous board's.
    pub sky: &'static [u16],
}

/// ⚠️ A FIXED TABLE OF REFERENCES, not a `Vec` of boards, and the difference is the whole reason a
/// game can carry many boards at all.
///
/// Every board is `&'static` data baked into the cartridge, so the registry never needed to own
/// one. As a `Vec<IsoBoard>` it did: registering N boards ran N pushes with log2(N) reallocs,
/// all during module init, before the program body — and the freed buffers left the heap too
/// fragmented for the one big block a GBA UI canvas wants. A 16-board game booted, a 48-board game
/// died in the allocator with plenty of total heap free.
///
/// This costs `MAX_BOARDS * 4` bytes of STATIC memory (1 KB) and allocates nothing, ever.
///
/// ⚠️ Not a `Vec` with `reserve` either — that was tried. Reserving for the worst case charges a
/// two-board game for the 162-board one and broke the ordinary case.
const MAX_BOARDS: usize = 256;
static ISO_BOARDS: SingleCore<RefCell<[Option<&'static IsoBoard>; MAX_BOARDS]>> =
    SingleCore::new(RefCell::new([None; MAX_BOARDS]));
static ISO_BOARDS_N: SingleCore<RefCell<usize>> = SingleCore::new(RefCell::new(0));

/// Register a baked iso board, returning its i32 handle. Called (as plain Rust, not a Value fn)
/// by the `isoboard:` scheme's generated registration in import order, before the program body.
/// Register a board whose `bg` handle is only known at runtime (the background registers itself
/// first). The static board carries `bg: 0`; this remembers the real handle beside the reference.
pub fn native_isoboard_register_bg(board: &'static IsoBoard, bg: i32) -> i32 {
    native_isoboard_register_bg2(board, bg, -1)
}

/// Register a board with BOTH of its backgrounds.
///
/// An SRPG battlefield is two layers: the ground, and a FOREGROUND of scenery — snow mounds, trees,
/// chimneys — that has to draw in front of the units standing behind it. `fg` is `-1` for a board
/// that has no such layer, which is every hand-authored Tiled board.
pub fn native_isoboard_register_bg2(board: &'static IsoBoard, bg: i32, fg: i32) -> i32 {
    let idx = native_isoboard_register(board);
    if idx >= 0 {
        ISO_BOARD_BG.with(|c| c.borrow_mut()[idx as usize] = bg);
        ISO_BOARD_FG.with(|c| c.borrow_mut()[idx as usize] = fg);
    }
    idx
}

static ISO_BOARD_BG: SingleCore<RefCell<[i32; MAX_BOARDS]>> =
    SingleCore::new(RefCell::new([0; MAX_BOARDS]));
static ISO_BOARD_FG: SingleCore<RefCell<[i32; MAX_BOARDS]>> =
    SingleCore::new(RefCell::new([-1; MAX_BOARDS]));

pub fn native_isoboard_register(board: &'static IsoBoard) -> i32 {
    ISO_BOARDS_N.with(|n| {
        let mut n = n.borrow_mut();
        if *n >= MAX_BOARDS {
            return -1;
        }
        let idx = *n as i32;
        ISO_BOARDS.with(|c| c.borrow_mut()[*n] = Some(board));
        *n += 1;
        idx
    })
}

/// Index of cell `(col,row)`'s CSR span in a board, plus its length. `None` if off-board.
fn board_stack_span(b: &IsoBoard, col: i32, row: i32) -> Option<(usize, usize)> {
    if col < 0 || row < 0 || col >= b.w || row >= b.h {
        return None;
    }
    let idx = (row * b.w + col) as usize;
    let start = *b.stack_off.get(idx)? as usize;
    let end = *b.stack_off.get(idx + 1)? as usize;
    Some((start, end - start))
}

/// `isob_stack_count(board, col, row)` — how many RAISED blocks are stacked on a cell (0 for flat).
pub fn isob_stack_count(args: &[Value]) -> Value {
    let (h, c, r) = (n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32);
    Value::Number(
        with_board(h, |b| {
            board_stack_span(b, c, r).map_or(0, |(_, len)| len as i32)
        })
        .unwrap_or(0) as f64,
    )
}
fn stack_field(args: &[Value], f: impl Fn(&IsoBoard, usize) -> i32) -> Value {
    let (h, c, r, i) = (
        n(args, 0) as i32,
        n(args, 1) as i32,
        n(args, 2) as i32,
        n(args, 3) as usize,
    );
    Value::Number(
        with_board(h, |b| match board_stack_span(b, c, r) {
            Some((start, len)) if i < len => f(b, start + i),
            _ => 0,
        })
        .unwrap_or(0) as f64,
    )
}
/// `isob_stack_elev(board, col, row, i)` — the i-th stacked block's TOP elevation (8px units).
pub fn isob_stack_elev(args: &[Value]) -> Value {
    stack_field(args, |b, j| b.stack_elev[j] as i32)
}
/// `isob_stack_tile(board, col, row, i)` — the i-th stacked block's own tile frame.
pub fn isob_stack_tile(args: &[Value]) -> Value {
    stack_field(args, |b, j| b.stack_tile[j] as i32)
}

fn with_board<R>(handle: i32, f: impl FnOnce(&IsoBoard) -> R) -> Option<R> {
    if handle < 0 || handle as usize >= MAX_BOARDS {
        return None;
    }
    ISO_BOARDS.with(|c| c.borrow()[handle as usize].map(f))
}

/// `isob_load(board)` — build the grid from a `isoboard:` board: size it, then fill every cell's
/// elevation / terrain id / walkability from the baked map. Replaces `isob_init` + a per-cell
/// `isob_set_cell` loop. Unit spawns are read separately (`isob_spawn_*`) so the game maps each to its
/// class stats before `isob_add_unit`.
pub fn isob_load(args: &[Value]) -> Value {
    let handle = n(args, 0) as i32;
    // The board's fields are all Copy (ints + `'static` slices), so copy them out and drop the
    // registry borrow before touching ISO_GRID — no nested `SingleCore` borrows.
    if let Some((w, h, frames, heights, walk, sky)) =
        with_board(handle, |b| (b.w, b.h, b.frames, b.heights, b.walk, b.sky))
    {
        // The board's own sky, or a disarm. Done here rather than left to the game so that loading a
        // board never leaves the PREVIOUS board's gradient running behind it.
        tish_agb::native_sky_set(sky);
        with_iso_grid(|t| {
            t.init(w, h);
            for i in 0..(w * h) as usize {
                if let Some(ci) = t.idx(i as i32 % w, i as i32 / w) {
                    t.cells[ci].height = *heights.get(i).unwrap_or(&0);
                    t.cells[ci].tile = *frames.get(i).unwrap_or(&0);
                    t.cells[ci].walkable = *walk.get(i).unwrap_or(&1) != 0;
                }
            }
        });
    }
    Value::Null
}

/// `isob_board_bg(board)` — the floor background handle to hand to `bg_new`. `-1` if unknown.
pub fn isob_board_bg(args: &[Value]) -> Value {
    // ⚠️ From the SIDE TABLE, not the board. The board is a `static` baked at compile time and the
    // background handle is only assigned at registration, so `IsoBoard::bg` is always 0.
    let h = n(args, 0) as i32;
    if h < 0 || h as usize >= MAX_BOARDS {
        return Value::Number(-1.0);
    }
    Value::Number(ISO_BOARD_BG.with(|c| c.borrow()[h as usize]) as f64)
}

/// `isob_board_mapw(board)` / `isob_board_maph(board)` — the layer size in pixels, for camera clamps.
pub fn isob_board_mapw(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.mapw).unwrap_or(512) as f64)
}

pub fn isob_board_maph(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.maph).unwrap_or(512) as f64)
}

/// `isob_board_cw(board)` / `isob_board_ch(board)` — the painted content's right/bottom edge in px.
pub fn isob_board_cw(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.cw).unwrap_or(512) as f64)
}

pub fn isob_board_ch(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.ch).unwrap_or(512) as f64)
}

/// `isob_board_fg(board)` — the FOREGROUND background handle, or `-1` if the board has none.
///
/// Hand it to `bg_new` at a priority ABOVE the unit sprites (a lower number; sprites sit at 2) so
/// that scenery in it — mounds, trees, chimneys — occludes the units standing behind it, which is
/// the whole reason such games keep it on their own layer.
pub fn isob_board_fg(args: &[Value]) -> Value {
    let h = n(args, 0) as i32;
    if h < 0 || h as usize >= MAX_BOARDS {
        return Value::Number(-1.0);
    }
    Value::Number(ISO_BOARD_FG.with(|c| c.borrow()[h as usize]) as f64)
}

/// `isob_board_ox(board)` / `isob_board_oy(board)` — iso projection origin used when the floor was baked.
pub fn isob_board_ox(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.ox).unwrap_or(96) as f64)
}
pub fn isob_board_oy(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.oy).unwrap_or(24) as f64)
}

/// `isob_board_lift(board)` — pixels the floor art rises per elevation unit, as baked. See
/// `IsoBoard::lift`; defaults to the classic 8 for boards that predate the field.
pub fn isob_board_lift(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.lift).unwrap_or(8) as f64)
}

/// `isob_w()` / `isob_h()` — the loaded grid's dimensions (valid after `isob_load` / `isob_init`).
pub fn isob_w(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.w) as f64)
}
pub fn isob_h(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.h) as f64)
}

/// `isob_spawn_count(board)` — how many unit spawns the map defines.
pub fn isob_spawn_count(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.spawns.len() as i32).unwrap_or(0) as f64)
}
fn spawn_field(args: &[Value], f: impl Fn(&(u8, u8, u8, u8)) -> i32) -> Value {
    let (h, i) = (n(args, 0) as i32, n(args, 1) as usize);
    Value::Number(with_board(h, |b| b.spawns.get(i).map(&f).unwrap_or(0)).unwrap_or(0) as f64)
}
/// `isob_spawn_col/row/cls/team(board, i)` — the i-th spawn's grid column / row / class index / team.
pub fn isob_spawn_col(args: &[Value]) -> Value {
    spawn_field(args, |s| s.0 as i32)
}
pub fn isob_spawn_row(args: &[Value]) -> Value {
    spawn_field(args, |s| s.1 as i32)
}
pub fn isob_spawn_cls(args: &[Value]) -> Value {
    spawn_field(args, |s| s.2 as i32)
}
pub fn isob_spawn_team(args: &[Value]) -> Value {
    spawn_field(args, |s| s.3 as i32)
}

/// `isob_init(w, h)` — (re)create a `w×h` iso board (all cells walkable, height 0, unoccupied).
pub fn isob_init(args: &[Value]) -> Value {
    with_iso_grid(|t| t.init(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `isob_set_cell(col, row, height, tile, walkable)` — set a cell's elevation, terrain id, and
/// whether a unit may stand on it. Occupancy is separate (`isob_set_occupant`).
pub fn isob_set_cell(args: &[Value]) -> Value {
    with_iso_grid(|t| {
        if let Some(i) = t.idx(n(args, 0) as i32, n(args, 1) as i32) {
            t.cells[i].height = n(args, 2) as u8;
            t.cells[i].tile = n(args, 3) as u8;
            t.cells[i].walkable = n(args, 4) != 0.0;
        }
    });
    Value::Null
}

/// `isob_height(col, row)` — a cell's elevation (0 off-board).
pub fn isob_height(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(0, |i| t.cells[i].height as i32)
    }) as f64)
}

/// `isob_tile(col, row)` — a cell's terrain/type id.
pub fn isob_tile(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(0, |i| t.cells[i].tile as i32)
    }) as f64)
}

/// `isob_walkable(col, row)` — 1 if a unit may stand on the cell, else 0.
pub fn isob_walkable(args: &[Value]) -> Value {
    Value::Bool(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .is_some_and(|i| t.cells[i].walkable)
    }))
}

/// `isob_set_occupant(col, row, entity)` — mark which unit stands on a cell (-1 = empty). Occupied
/// cells block other units' movement.
pub fn isob_set_occupant(args: &[Value]) -> Value {
    with_iso_grid(|t| {
        if let Some(i) = t.idx(n(args, 0) as i32, n(args, 1) as i32) {
            t.cells[i].occupant = n(args, 2) as i32;
        }
    });
    Value::Null
}

/// `isob_occupant(col, row)` — entity id standing on the cell, or -1.
pub fn isob_occupant(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(-1, |i| t.cells[i].occupant)
    }) as f64)
}

/// `isob_move_range(col, row, move, jump)` — flood-fill the tiles reachable from (col,row) for `move`
/// move points and `jump` max height delta; returns the count. Query with `isob_in_range` /
/// `isob_range_*`, and `isob_path` for a route into it.
///
/// The unit-less form takes the movement type as an optional 5th argument (`isob_move_range(c, r,
/// move, jump, flying)`), defaulting to walking when it is omitted. That is what lets a caller ask
/// the hypothetical — "where could a flier get to from here?" — without a unit to ask it about.
pub fn isob_move_range(args: &[Value]) -> Value {
    let flying = n(args, 4) != 0.0;
    // 6th arg is the mover's team for zone-of-control; absent (or -1) means path with no ZoC.
    let team = if args.len() > 5 {
        n(args, 5) as i32
    } else {
        -1
    };
    with_iso_grid(|t| {
        t.move_range(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            flying,
            team,
        )
    });
    Value::Number(with_iso_grid(|t| t.reach.len()) as f64)
}

/// `isob_move_cost(col, row)` — what the last computed move-range spent to REACH that cell, or -1 if
/// it never did.
///
/// The cell list alone can't tell you what the search decided. On an open 4-connected board almost
/// everything stays reachable whatever the rules say — terrain costs and zone of control change how
/// DEARLY, and detour into the price rather than out of the set. This is the number that moves, so
/// it's the one worth asking for, both for a UI that wants to show what a destination costs and for
/// a test that wants to prove a rule fired.
pub fn isob_move_cost(args: &[Value]) -> Value {
    let (c, r) = (n(args, 0) as i32, n(args, 1) as i32);
    Value::Number(with_iso_grid(|t| match t.idx(c, r) {
        Some(i) if t.in_move[i] => t.dist[i],
        _ => -1,
    }) as f64)
}

/// `isob_in_range(col, row)` — 1 if the cell is in the last computed move-range, else 0 (Number so
/// tish `isob_in_range(...) > 0` works).
pub fn isob_in_range(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(0, |i| t.in_move[i] as i32)
    }) as f64)
}

/// `isob_range_count()` — number of reachable cells from the last `isob_move_range`.
pub fn isob_range_count(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.reach.len()) as f64)
}
/// `isob_range_col(i)` / `isob_range_row(i)` — the i-th reachable cell.
pub fn isob_range_col(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.reach
            .get(n(args, 0) as usize)
            .map_or(-1, |&(c, _)| c as i32)
    }) as f64)
}
pub fn isob_range_row(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.reach
            .get(n(args, 0) as usize)
            .map_or(-1, |&(_, r)| r as i32)
    }) as f64)
}

/// `isob_path(col, row)` — reconstruct the route from the last move-range's origin to (col,row);
/// returns its length (0 if unreachable). Read with `isob_path_len` / `isob_path_col` / `isob_path_row`.
pub fn isob_path(args: &[Value]) -> Value {
    with_iso_grid(|t| t.path_to(n(args, 0) as i32, n(args, 1) as i32));
    Value::Number(with_iso_grid(|t| t.path.len()) as f64)
}
pub fn isob_path_len(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.path.len()) as f64)
}
pub fn isob_path_col(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.path
            .get(n(args, 0) as usize)
            .map_or(-1, |&(c, _)| c as i32)
    }) as f64)
}
pub fn isob_path_row(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.path
            .get(n(args, 0) as usize)
            .map_or(-1, |&(_, r)| r as i32)
    }) as f64)
}

// ── Units & turn order ──────────────────────────────────────────────────────────

/// `isob_add_unit(col, row, team, speed, move, jump, hp)` — register a unit on the board (claiming the
/// cell) and return its id. Ids count up from 0 in registration order.
pub fn isob_add_unit(args: &[Value]) -> Value {
    let id = with_iso_grid(|t| {
        t.add_unit(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as u8,
            n(args, 3) as u16,
            n(args, 4) as u8,
            n(args, 5) as u8,
            n(args, 6) as i16,
        )
    });
    Value::Number(id as f64)
}

/// `isob_clear_units()` — drop every unit, keeping the board.
///
/// ⚠️ Before this existed the ONLY way to empty the unit table was `isob_load`, which reloads the
/// whole board. A game that fights two battles on the SAME board therefore could not clear it:
/// `isoUseBoard` returns early when the board is already current, so `isob_load` never runs, the old
/// units stay registered, and the new battle's ids are appended after them. The symptoms are not
/// obviously a leak — the turn queue offers turns to units from a fight that ended, and the tish
/// side's own count disagrees with the engine's.
pub fn isob_clear_units(_args: &[Value]) -> Value {
    with_iso_grid(|t| {
        t.units.clear();
        for c in t.cells.iter_mut() {
            c.occupant = -1;
        }
    });
    Value::Null
}

/// `isob_unit_count()` — number of registered units.
pub fn isob_unit_count(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.units.len()) as f64)
}

fn unit_field(args: &[Value], f: impl Fn(&IsoBoardUnit) -> i32) -> Value {
    Value::Number(with_iso_grid(|t| t.units.get(n(args, 0) as usize).map_or(-1, f)) as f64)
}

/// Per-unit getters: `isob_unit_col/row/team/hp/maxhp/move/jump/speed(id)`, `isob_unit_alive(id)`.
pub fn isob_unit_col(args: &[Value]) -> Value {
    unit_field(args, |u| u.col)
}
pub fn isob_unit_row(args: &[Value]) -> Value {
    unit_field(args, |u| u.row)
}
pub fn isob_unit_team(args: &[Value]) -> Value {
    unit_field(args, |u| u.team as i32)
}
pub fn isob_unit_hp(args: &[Value]) -> Value {
    unit_field(args, |u| u.hp as i32)
}
pub fn isob_unit_maxhp(args: &[Value]) -> Value {
    unit_field(args, |u| u.max_hp as i32)
}
pub fn isob_unit_move(args: &[Value]) -> Value {
    unit_field(args, |u| u.mov as i32)
}
pub fn isob_unit_jump(args: &[Value]) -> Value {
    unit_field(args, |u| u.jump as i32)
}
pub fn isob_unit_speed(args: &[Value]) -> Value {
    unit_field(args, |u| u.speed as i32)
}
pub fn isob_unit_alive(args: &[Value]) -> Value {
    // Number (1/0), not Bool, so tish `isob_unit_alive(id) > 0` comparisons work on GBA.
    Value::Number(with_iso_grid(|t| {
        t.units
            .get(n(args, 0) as usize)
            .map_or(0, |u| u.alive as i32)
    }) as f64)
}

/// `isob_unit_move_range(id)` — flood-fill the reachable tiles for a unit using its own Move/Jump and
/// movement type (from its current cell). Then query with `isob_in_range` / `isob_range_*` / `isob_path`.
pub fn isob_unit_move_range(args: &[Value]) -> Value {
    with_iso_grid(|t| {
        if let Some(u) = t.units.get(n(args, 0) as usize).copied() {
            t.move_range(
                u.col,
                u.row,
                u.mov as i32,
                u.jump as i32,
                u.flying,
                u.team as i32,
            );
        }
    });
    Value::Number(with_iso_grid(|t| t.reach.len()) as f64)
}

/// `isob_set_terrain_cost(tile, cost)` — move points needed to ENTER a cell of terrain id `tile`.
///
/// The engine deliberately does not know that id 33 is water. It owns the search; the game owns what
/// its tiles mean, so cost is pushed in from the example's terrain table. Defaults to 1 everywhere,
/// so a game that never calls this behaves exactly as it did before costs existed.
///
/// Clamped to ≥ 1: a free tile would break the search's assumption that distance only ever increases
/// as it sweeps outward, and the cheapest way to say "this tile is free to cross" is a flier.
pub fn isob_set_terrain_cost(args: &[Value]) -> Value {
    let tile = n(args, 0) as usize;
    let cost = (n(args, 1) as i32).clamp(1, 255) as u8;
    with_iso_grid(|t| {
        if tile < t.cost.len() {
            t.cost[tile] = cost;
        }
    });
    Value::Null
}

/// `isob_knockback(id, from_col, from_row)` — shove a unit one tile directly away from (from_col,
/// from_row). Returns 1 if it moved, 0 if something stopped it.
///
/// Height is deliberately NOT a reason to refuse: being shoved off a ledge is the interesting case,
/// and a `jump` check here would quietly make knockback do nothing near exactly the terrain that
/// makes it worth having. The board edge, a solid cell and another unit all still stop it — a unit
/// backed against any of those takes the hit where it stands.
///
/// The engine moves the body and reports that it did. What a fall COSTS is the game's to decide, so
/// the caller compares the heights either side and applies its own damage; the same split as the
/// damage formula.
pub fn isob_knockback(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let (sc, sr) = (n(args, 1) as i32, n(args, 2) as i32);
    let moved = with_iso_grid(|t| match t.knock_dest(id, sc, sr) {
        Some((nc, nr, _)) => {
            t.unit_set_pos(id, nc, nr);
            true
        }
        None => false,
    });
    Value::Number(moved as i32 as f64)
}

/// `isob_knock_drop(id, from_col, from_row)` — how far the unit WOULD fall if shoved from there, or 0
/// if the shove is blocked or lands level or uphill.
///
/// This exists so an AI can price a shove before committing to one. It deliberately answers with the
/// drop rather than with the destination: the destination would make the caller re-derive the rule
/// for which direction a shove goes and what stops it, and the moment two copies of that rule exist
/// they start to disagree. Blocked and level both answer 0 because they are worth the same to anyone
/// asking — no fall, no damage.
pub fn isob_knock_drop(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let (sc, sr) = (n(args, 1) as i32, n(args, 2) as i32);
    Value::Number(
        with_iso_grid(|t| t.knock_dest(id, sc, sr).map_or(0, |(_, _, d)| d.max(0))) as f64,
    )
}

/// `isob_set_zoc(on)` — turn zone of control on or off for every subsequent move-range query.
///
/// With it on, stepping into a tile adjacent to a living enemy ends the move — the tile is reachable,
/// nothing through it is. That turns a defender from an obstacle you route around into one you have
/// to deal with, which is the entire point of a front line. Off by default so this changes nothing
/// for a game that does not ask for it.
pub fn isob_set_zoc(args: &[Value]) -> Value {
    let on = n(args, 0) != 0.0;
    with_iso_grid(|t| t.zoc_on = on);
    Value::Null
}

/// `isob_revive(id, hp)` — bring a KO'd unit back on the cell it fell on, at `hp` (clamped to its
/// maximum, and to at least 1). Returns 1 if it stood up, 0 if it was already alive or something has
/// since walked onto its cell.
///
/// The occupancy check is the whole reason this cannot live in the example: death RELEASES the cell,
/// so a body is not a wall, and by the time anyone reaches it with a Phoenix Down somebody may be
/// standing there. The engine owns that map, so it is the only place that can answer honestly —
/// and returning 0 rather than reviving onto an occupied cell means the caller can decline to spend
/// the item instead of quietly creating two units on one tile.
pub fn isob_revive(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let hp = n(args, 1) as i16;
    Value::Number(with_iso_grid(|t| {
        let (col, row, max_hp) = match t.units.get(id as usize) {
            Some(u) if !u.alive => (u.col, u.row, u.max_hp),
            _ => return 0,
        };
        match t.idx(col, row) {
            Some(i) if t.cells[i].occupant < 0 => t.cells[i].occupant = id,
            _ => return 0,
        }
        if let Some(u) = t.units.get_mut(id as usize) {
            u.alive = true;
            u.hp = hp.clamp(1, max_hp);
        }
        1
    }) as f64)
}

/// `isob_turn_end(id, moved, acted)` — charge the rest of the turn's counter cost, once the game knows
/// what the unit actually did with it. The base cost was taken when the turn was handed out.
///
/// The split exists so that forgetting to call this cannot wedge the queue: a game that never calls
/// it still has every unit paying 500 a turn and the order still advances, it just loses the tempo
/// rule. Making `turn_next` charge nothing and rely on this would mean one missed call returns the
/// same unit forever.
pub fn isob_turn_end(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    let moved = n(args, 1) != 0.0;
    let acted = n(args, 2) != 0.0;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id) {
            let mut cost = 0;
            if moved {
                cost += COST_MOVE;
            }
            if acted {
                cost += COST_ACTION;
            }
            u.ct = (u.ct - cost).max(0);
        }
    });
    Value::Null
}

/// `isob_unit_ct(id)` — the unit's current turn counter, for a UI that wants to show who is next.
pub fn isob_unit_ct(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    Value::Number(with_iso_grid(|t| t.units.get(id).map_or(0, |u| u.ct)) as f64)
}

/// `isob_unit_set_speed_scale(id, percent)` — scale how fast a unit's turn counter fills. 100 is
/// normal; the reference SRPG's Haste and Slow are 200 and 50.
///
/// The engine owns the turn queue and therefore owns this; which status sets it, for how long, and
/// what the numbers are stay the game's. It scales ACCRUAL and not the Speed stat, so the AI's threat
/// tables and the Status screen go on reading an unmodified stat block, and clearing the status is
/// just setting it back to 100 — there is no original value to remember and no way to double-apply.
///
/// Clamped to 1..=1000: a scale of 0 would stop the unit accruing at all, which is not a slow but a
/// removal, and would leave `turn_next` hunting for a tick count that never arrives.
pub fn isob_unit_set_speed_scale(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    let pct = (n(args, 1) as i32).clamp(1, 1000) as u16;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id) {
            u.speed_scale = pct;
        }
    });
    Value::Null
}

/// `isob_unit_set_flying(id, flying)` — a flier pays 1 move point per tile and ignores height deltas.
///
/// It is NOT a bigger move budget: it changes which tiles the budget can be spent on, so a flier
/// crosses a ford or a cliff that would cost a walker two points or stop it outright, and is no
/// faster than anyone else over open ground. Solid cells and occupied cells still block it.
pub fn isob_unit_set_flying(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    let f = n(args, 1) != 0.0;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id) {
            u.flying = f;
        }
    });
    Value::Null
}

/// `isob_unit_set_pos(id, col, row)` — move a unit (updates board occupancy).
pub fn isob_unit_set_pos(args: &[Value]) -> Value {
    with_iso_grid(|t| t.unit_set_pos(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `isob_damage(id, amount)` — subtract HP; a unit at ≤0 HP dies (alive=false, cell freed). Returns
/// remaining HP.
pub fn isob_damage(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let amt = n(args, 1) as i16;
    with_iso_grid(|t| {
        let (dead, c, r) = match t.units.get_mut(id as usize) {
            Some(u) if u.alive => {
                u.hp -= amt;
                if u.hp <= 0 {
                    u.hp = 0;
                    u.alive = false;
                }
                (!u.alive, u.col, u.row)
            }
            _ => (false, 0, 0),
        };
        if dead {
            if let Some(i) = t.idx(c, r) {
                if t.cells[i].occupant == id {
                    t.cells[i].occupant = -1;
                }
            }
        }
    });
    unit_field(args, |u| u.hp as i32)
}

/// `isob_heal(id, amount)` — restore HP to a living unit, capped at its `max_hp`. Returns the unit's
/// new HP (0 if the unit is dead/unknown — dead units cannot be healed here). For support classes.
pub fn isob_heal(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let amt = n(args, 1) as i16;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id as usize) {
            if u.alive {
                u.hp = (u.hp + amt).min(u.max_hp);
            }
        }
    });
    unit_field(args, |u| if u.alive { u.hp as i32 } else { 0 })
}

/// `isob_unit_set_maxhp(id, v)` — set a unit's HP ceiling, keeping current HP in range. Returns the
/// new maximum (0 for an unknown unit).
///
/// ⚠️ This exists because the SRPG growth rules (no longer in this repo) documented a rule they could not implement: "what
/// a level does NOT raise is HP … there is no setter for the maximum, so growing it would mean a
/// Rust change." Real SRPG levels raise HP, so the engine was quietly deciding a game-design
/// question. A missing setter is not a neutral omission — it becomes a rule.
///
/// Raising the ceiling raises current HP by the same amount rather than leaving the unit wounded: a
/// level-up mid-battle that hands you a bigger empty bar reads as a penalty. Lowering it (a curse, a
/// job change) clamps current HP down instead.
pub fn isob_unit_set_maxhp(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let v = (n(args, 1) as i16).max(1);
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id as usize) {
            let delta = v - u.max_hp;
            u.max_hp = v;
            if delta > 0 {
                u.hp += delta;
            }
            if u.hp > u.max_hp {
                u.hp = u.max_hp;
            }
        }
    });
    unit_field(args, |u| u.max_hp as i32)
}

/// `isob_turn_next()` — advance the speed-based turn queue and return the id of the unit whose turn it
/// is (or -1 if none are alive).
pub fn isob_turn_next(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.turn_next()) as f64)
}

// ── Attack targeting ────────────────────────────────────────────────────────────

impl IsoBoardGrid {
    /// A living enemy of `unit_id` standing on one of its 4 orthogonal neighbours, or -1.
    fn adjacent_enemy(&self, unit_id: i32) -> i32 {
        let u = match self.units.get(unit_id as usize) {
            Some(u) => *u,
            None => return -1,
        };
        const DIRS: [(i32, i32); 4] = [(1, 0), (-1, 0), (0, 1), (0, -1)];
        for (dc, dr) in DIRS {
            if let Some(i) = self.idx(u.col + dc, u.row + dr) {
                let occ = self.cells[i].occupant;
                if occ >= 0 {
                    if let Some(other) = self.units.get(occ as usize) {
                        if other.alive && other.team != u.team {
                            return occ;
                        }
                    }
                }
            }
        }
        -1
    }
}

/// `isob_adjacent_enemy(unitId)` — a living enemy on one of the unit's 4 neighbouring tiles (in
/// range of a melee attack), or -1 if none. Used for attack targeting and AI.
pub fn isob_adjacent_enemy(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.adjacent_enemy(n(args, 0) as i32)) as f64)
}

// ── isob_* typed siblings ────────────────────────────────────────────────────────
// Thin adapters over the boxed exports above: the SAME body executes, so behaviour
// cannot drift — what a typed call removes is the caller's namespace lookup +
// value_call dispatch + per-argument Value boxing (~72 ticks a call vs ~7). The
// stack-enum Value construction in here is noise next to that. Generated shape;
// invert any adapter to a direct internal call if it ever shows in a profile.

pub fn isob_stack_count_typed(p0: i32, p1: i32, p2: i32) -> i32 {
    match isob_stack_count(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_stack_elev_typed(p0: i32, p1: i32, p2: i32, p3: i32) -> i32 {
    match isob_stack_elev(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_stack_tile_typed(p0: i32, p1: i32, p2: i32, p3: i32) -> i32 {
    match isob_stack_tile(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_load_typed(p0: i32) {
    isob_load(&[Value::Number(p0 as f64)]);
}
pub fn isob_board_bg_typed(p0: i32) -> i32 {
    match isob_board_bg(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_board_cw_typed(p0: i32) -> i32 {
    match isob_board_cw(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}
pub fn isob_board_ch_typed(p0: i32) -> i32 {
    match isob_board_ch(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}
pub fn isob_board_mapw_typed(p0: i32) -> i32 {
    match isob_board_mapw(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}
pub fn isob_board_maph_typed(p0: i32) -> i32 {
    match isob_board_maph(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}
pub fn isob_board_fg_typed(p0: i32) -> i32 {
    match isob_board_fg(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => -1,
    }
}
pub fn isob_board_ox_typed(p0: i32) -> i32 {
    match isob_board_ox(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_board_oy_typed(p0: i32) -> i32 {
    match isob_board_oy(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_board_lift_typed(p0: i32) -> i32 {
    match isob_board_lift(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_w_typed() -> i32 {
    match isob_w(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_h_typed() -> i32 {
    match isob_h(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_spawn_count_typed(p0: i32) -> i32 {
    match isob_spawn_count(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_spawn_col_typed(p0: i32, p1: i32) -> i32 {
    match isob_spawn_col(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_spawn_row_typed(p0: i32, p1: i32) -> i32 {
    match isob_spawn_row(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_spawn_cls_typed(p0: i32, p1: i32) -> i32 {
    match isob_spawn_cls(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_spawn_team_typed(p0: i32, p1: i32) -> i32 {
    match isob_spawn_team(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_init_typed(p0: i32, p1: i32) {
    isob_init(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn isob_set_cell_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32) {
    isob_set_cell(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
    ]);
}
pub fn isob_height_typed(p0: i32, p1: i32) -> i32 {
    match isob_height(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_tile_typed(p0: i32, p1: i32) -> i32 {
    match isob_tile(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_walkable_typed(p0: i32, p1: i32) -> i32 {
    match isob_walkable(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Bool(b) => b as i32,
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_set_occupant_typed(p0: i32, p1: i32, p2: i32) {
    isob_set_occupant(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}
pub fn isob_occupant_typed(p0: i32, p1: i32) -> i32 {
    match isob_occupant(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_move_range_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32, p5: i32) -> i32 {
    match isob_move_range(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
        Value::Number(p5 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_move_cost_typed(p0: i32, p1: i32) -> i32 {
    match isob_move_cost(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_in_range_typed(p0: i32, p1: i32) -> i32 {
    match isob_in_range(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_range_count_typed() -> i32 {
    match isob_range_count(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_range_col_typed(p0: i32) -> i32 {
    match isob_range_col(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_range_row_typed(p0: i32) -> i32 {
    match isob_range_row(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_path_typed(p0: i32, p1: i32) -> i32 {
    match isob_path(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_path_len_typed() -> i32 {
    match isob_path_len(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_path_col_typed(p0: i32) -> i32 {
    match isob_path_col(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_path_row_typed(p0: i32) -> i32 {
    match isob_path_row(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_add_unit_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32, p5: i32, p6: i32) -> i32 {
    match isob_add_unit(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
        Value::Number(p5 as f64),
        Value::Number(p6 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_clear_units_typed() {
    isob_clear_units(&[]);
}
pub fn isob_unit_count_typed() -> i32 {
    match isob_unit_count(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_col_typed(p0: i32) -> i32 {
    match isob_unit_col(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_row_typed(p0: i32) -> i32 {
    match isob_unit_row(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_team_typed(p0: i32) -> i32 {
    match isob_unit_team(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_hp_typed(p0: i32) -> i32 {
    match isob_unit_hp(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_maxhp_typed(p0: i32) -> i32 {
    match isob_unit_maxhp(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_move_typed(p0: i32) -> i32 {
    match isob_unit_move(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_jump_typed(p0: i32) -> i32 {
    match isob_unit_jump(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_speed_typed(p0: i32) -> i32 {
    match isob_unit_speed(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_alive_typed(p0: i32) -> i32 {
    match isob_unit_alive(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_move_range_typed(p0: i32) -> i32 {
    match isob_unit_move_range(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_set_terrain_cost_typed(p0: i32, p1: i32) {
    isob_set_terrain_cost(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn isob_knockback_typed(p0: i32, p1: i32, p2: i32) -> i32 {
    match isob_knockback(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_knock_drop_typed(p0: i32, p1: i32, p2: i32) -> i32 {
    match isob_knock_drop(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_set_zoc_typed(p0: i32) {
    isob_set_zoc(&[Value::Number(p0 as f64)]);
}
pub fn isob_revive_typed(p0: i32, p1: i32) -> i32 {
    match isob_revive(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_turn_end_typed(p0: i32, p1: i32, p2: i32) {
    isob_turn_end(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}
pub fn isob_unit_ct_typed(p0: i32) -> i32 {
    match isob_unit_ct(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_set_speed_scale_typed(p0: i32, p1: i32) {
    isob_unit_set_speed_scale(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn isob_unit_set_flying_typed(p0: i32, p1: i32) {
    isob_unit_set_flying(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn isob_unit_set_pos_typed(p0: i32, p1: i32, p2: i32) {
    isob_unit_set_pos(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}
pub fn isob_damage_typed(p0: i32, p1: i32) -> i32 {
    match isob_damage(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_heal_typed(p0: i32, p1: i32) -> i32 {
    match isob_heal(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_unit_set_maxhp_typed(p0: i32, p1: i32) -> i32 {
    match isob_unit_set_maxhp(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_turn_next_typed() -> i32 {
    match isob_turn_next(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn isob_adjacent_enemy_typed(p0: i32) -> i32 {
    match isob_adjacent_enemy(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

// ── typed siblings (grid/platformer genre + hot entity surface) ──────────────────
pub fn entity_count_typed() -> i32 {
    match entity_count(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn entity_count_tag_typed(p0: i32) -> i32 {
    match entity_count_tag(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn entity_tag_typed(p0: i32) -> i32 {
    match entity_tag(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn frame_period_typed(p0: i32) -> i32 {
    match frame_period(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn grid_col_typed(p0: i32) -> i32 {
    match grid_col(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn grid_facing_typed(p0: i32) -> i32 {
    match grid_facing(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn grid_from_map_typed() {
    grid_from_map(&[]);
}
pub fn grid_interact_typed(p0: i32) -> i32 {
    match grid_interact(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn grid_moving_typed(p0: i32) -> i32 {
    match grid_moving(&[Value::Number(p0 as f64)]) {
        Value::Bool(b) => b as i32,
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn grid_row_typed(p0: i32) -> i32 {
    match grid_row(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn grid_set_ladder_typed(p0: i32, p1: i32, p2: i32) {
    grid_set_ladder(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}
pub fn grid_set_oneway_typed(p0: i32, p1: i32, p2: i32) {
    grid_set_oneway(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}
pub fn grid_set_solid_typed(p0: i32, p1: i32, p2: i32) {
    grid_set_solid(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}
pub fn grid_setup_typed(p0: i32, p1: i32) {
    grid_setup(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn grid_step_typed(p0: i32, p1: i32, p2: i32) {
    grid_step(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}
pub fn heal_typed(p0: i32, p1: i32) {
    heal(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn overlaps_typed(p0: i32, p1: i32) -> i32 {
    match overlaps(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Bool(b) => b as i32,
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn platformer_blocked_typed(p0: i32) -> i32 {
    match platformer_blocked(&[Value::Number(p0 as f64)]) {
        Value::Bool(b) => b as i32,
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn platformer_bounce_typed(p0: i32, p1: i32) {
    platformer_bounce(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn platformer_can_interact_typed(p0: i32, p1: i32) -> i32 {
    match platformer_can_interact(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn platformer_drop_typed(p0: i32) {
    platformer_drop(&[Value::Number(p0 as f64)]);
}
pub fn platformer_face_typed(p0: i32) -> i32 {
    match platformer_face(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn platformer_grounded_typed(p0: i32) -> i32 {
    match platformer_grounded(&[Value::Number(p0 as f64)]) {
        Value::Bool(b) => b as i32,
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn platformer_hold_typed(p0: i32, p1: i32) {
    platformer_hold(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn platformer_interact_typed(p0: i32, p1: i32) -> i32 {
    match platformer_interact(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn platformer_jump_typed(p0: i32) {
    platformer_jump(&[Value::Number(p0 as f64)]);
}
pub fn platformer_jump_release_typed(p0: i32) {
    platformer_jump_release(&[Value::Number(p0 as f64)]);
}
pub fn platformer_run_typed(p0: i32) {
    platformer_run(&[Value::Number(p0 as f64)]);
}
/// Typed twin of [`platformer_set_speed`]. `walk`/`run` are FIXED — the boxed form multiplies the
/// incoming float by 256, and `Fixed::to_raw()` is already that same 8-bit fraction, so the two
/// paths agree. Taking i32 here (as this did) truncated every fractional speed to a whole pixel per
/// frame: `walkSpeed: 1.9` arrived as 1, and the engine's own documented defaults (1.25 / 2.25)
/// became 1 and 2. The symptom is a hero who "barely moves", and no amount of tuning the number
/// fixes it because every value between 1 and 2 is the same value.
pub fn platformer_set_speed_typed(e: i32, walk: Fixed, run: Fixed) {
    with_world(|w| w.platformer_set_speed(e, walk.to_raw(), run.to_raw()));
}
/// See [`platformer_set_physics`]. `fixed` args for the same reason as the speeds: 3.4 px/frame
/// must not truncate to 3.
pub fn platformer_set_physics_typed(e: i32, jump: Fixed, grav: Fixed) {
    with_world(|w| w.platformer_set_physics(e, jump.to_raw(), grav.to_raw()));
}
/// See [`platformer_launch`].
pub fn platformer_launch_typed(e: i32, vx: Fixed, vy: Fixed) {
    with_world(|w| w.platformer_launch(e, vx.to_raw(), vy.to_raw()));
}
/// ⚠️ `vy` IS FIXED-POINT, NOT AN INTEGER — the same trap `platformer_set_speed` was fixed for.
/// The boxed native multiplies by 256, so an `i32` fast path truncated every fractional velocity
/// on the way in: `platformer_set_vy(e, 0.75)` (the wall-slide scrape) arrived as 0 and the
/// character hung motionless on the wall instead of sliding down it.
pub fn platformer_set_vy_typed(e: i32, vy: Fixed) {
    with_world(|w| w.platformer_set_vy(e, vy.to_raw()));
}
/// ⚠️ Likewise on the way OUT: truncating to whole px reported a body rising at -0.5 px/frame as
/// 0, so `vy < 0` was false and the state machine called a jump a fall for its first frames.
pub fn platformer_vy_typed(e: i32) -> Fixed {
    with_world(|w| {
        w.slot_of(e)
            .filter(|&s| w.has(s, C_PLATFORMER))
            .map(|s| w.platformer[s].vy)
            .unwrap_or(Fixed::from_raw(0))
    })
}
pub fn platformer_walk_typed(p0: i32, p1: i32) {
    platformer_walk(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn set_chase_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32) {
    set_chase(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
    ]);
}
pub fn flow_goal_typed(p0: i32, p1: i32, p2: i32) {
    with_world(|w| w.flow_goal(p0 as usize, p1, p2));
}
pub fn flow_dist_typed(p0: i32, p1: i32, p2: i32) -> i32 {
    with_world(|w| w.flow_dist(p0 as usize, p1, p2))
}
pub fn set_seek_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32) {
    with_world(|w| w.set_seek(p0, p1, p2, p3, p4));
}
pub fn clear_seek_typed(p0: i32) {
    with_world(|w| w.clear_seek(p0));
}
pub fn seek_arrived_typed(p0: i32) -> i32 {
    with_world(|w| w.seek_arrived(p0)) as i32
}
pub fn set_soldier_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32) {
    with_world(|w| w.set_soldier(p0, p1, p2, p3, p4));
}
pub fn soldier_target_typed(p0: i32) -> i32 {
    with_world(|w| w.soldier_target(p0))
}
pub fn soldier_team_typed(p0: i32) -> i32 {
    with_world(|w| w.soldier_team(p0))
}
pub fn fog_init_typed(p0: i32, p1: i32) {
    with_world(|w| w.fog_init(p0, p1));
}
pub fn set_sleeping_typed(p0: i32, p1: i32) {
    set_sleeping(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn terrain_set_typed(p0: i32, p1: i32, p2: i32, p3: i32) {
    with_world(|w| w.terrain_set(p0, p1, p2, p3));
}
pub fn terrain_blit_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32, p5: i32) -> i32 {
    with_world(|w| w.terrain_blit(p0, p1, p2, p3, p4, p5))
}
pub fn set_vision_typed(p0: i32, p1: i32) {
    with_world(|w| w.set_vision(p0, p1));
}
pub fn fog_reveal_typed(p0: i32, p1: i32, p2: i32) {
    with_world(|w| w.fog_reveal(p0, p1, p2));
}
pub fn fog_state_typed(p0: i32, p1: i32) -> i32 {
    match fog_state(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn fog_blit_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32, p5: i32, p6: i32) -> i32 {
    with_world(|w| w.fog_blit(p0, p1, p2, p3, p4, p5, p6))
}
pub fn set_guard_typed(p0: i32, p1: i32) {
    set_guard(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn set_hopper_typed(p0: i32, p1: i32) {
    set_hopper(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn set_wanderer_typed(p0: i32, p1: i32) {
    set_wanderer(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn topdown_speed_raw_typed(p0: i32, p1: i32) {
    topdown_speed_raw(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}
pub fn wanderer_wants_shot_typed(p0: i32) -> i32 {
    match wanderer_wants_shot(&[Value::Number(p0 as f64)]) {
        Value::Number(n) => n as i32,
        _ => 0,
    }
}
pub fn set_jumper_typed(p0: i32) {
    set_jumper(&[Value::Number(p0 as f64)]);
}
pub fn step_ticks_typed(p0: i32) -> i32 {
    match step_ticks(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn swing_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32, p5: i32) -> i32 {
    match swing(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
        Value::Number(p5 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}
pub fn world_step_typed() -> i32 {
    match world_step(&[]) {
        Value::Bool(b) => b as i32,
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

/// `iso_stack_count(board, col, row)` — how many RAISED blocks are stacked on a cell (0 for flat).
pub fn iso_stack_count(args: &[Value]) -> Value {
    let (h, c, r) = (n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32);
    Value::Number(
        with_board(h, |b| {
            board_stack_span(b, c, r).map_or(0, |(_, len)| len as i32)
        })
        .unwrap_or(0) as f64,
    )
}

/// `iso_stack_elev(board, col, row, i)` — the i-th stacked block's TOP elevation (8px units).
pub fn iso_stack_elev(args: &[Value]) -> Value {
    stack_field(args, |b, j| b.stack_elev[j] as i32)
}

/// `iso_stack_tile(board, col, row, i)` — the i-th stacked block's own tile frame.
pub fn iso_stack_tile(args: &[Value]) -> Value {
    stack_field(args, |b, j| b.stack_tile[j] as i32)
}

/// `iso_load(board)` — build the grid from a `tactics:` board: size it, then fill every cell's
/// elevation / terrain id / walkability from the baked map. Replaces `iso_init` + a per-cell
/// `iso_set_cell` loop. Unit spawns are read separately (`iso_spawn_*`) so the game maps each to its
/// class stats before `iso_add_unit`.
pub fn iso_load(args: &[Value]) -> Value {
    let handle = n(args, 0) as i32;
    // The board's fields are all Copy (ints + `'static` slices), so copy them out and drop the
    // registry borrow before touching ISO_GRID — no nested `SingleCore` borrows.
    if let Some((w, h, frames, heights, walk, sky)) =
        with_board(handle, |b| (b.w, b.h, b.frames, b.heights, b.walk, b.sky))
    {
        // The board's own sky, or a disarm. Done here rather than left to the game so that loading a
        // board never leaves the PREVIOUS board's gradient running behind it.
        tish_agb::native_sky_set(sky);
        with_iso_grid(|t| {
            t.init(w, h);
            for i in 0..(w * h) as usize {
                if let Some(ci) = t.idx(i as i32 % w, i as i32 / w) {
                    t.cells[ci].height = *heights.get(i).unwrap_or(&0);
                    t.cells[ci].tile = *frames.get(i).unwrap_or(&0);
                    t.cells[ci].walkable = *walk.get(i).unwrap_or(&1) != 0;
                }
            }
        });
    }
    Value::Null
}

/// `iso_board_bg(board)` — the floor background handle to hand to `bg_new`. `-1` if unknown.
pub fn iso_board_bg(args: &[Value]) -> Value {
    // ⚠️ From the SIDE TABLE, not the board. The board is a `static` baked at compile time and the
    // background handle is only assigned at registration, so `IsoBoard::bg` is always 0.
    let h = n(args, 0) as i32;
    if h < 0 || h as usize >= MAX_BOARDS {
        return Value::Number(-1.0);
    }
    Value::Number(ISO_BOARD_BG.with(|c| c.borrow()[h as usize]) as f64)
}

/// `iso_board_mapw(board)` / `iso_board_maph(board)` — the layer size in pixels, for camera clamps.
pub fn iso_board_mapw(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.mapw).unwrap_or(512) as f64)
}

pub fn iso_board_maph(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.maph).unwrap_or(512) as f64)
}

/// `iso_board_cw(board)` / `iso_board_ch(board)` — the painted content's right/bottom edge in px.
pub fn iso_board_cw(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.cw).unwrap_or(512) as f64)
}

pub fn iso_board_ch(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.ch).unwrap_or(512) as f64)
}

/// `iso_board_fg(board)` — the FOREGROUND background handle, or `-1` if the board has none.
///
/// Hand it to `bg_new` at a priority ABOVE the unit sprites (a lower number; sprites sit at 2) so
/// that scenery in it — mounds, trees, chimneys — occludes the units standing behind it, which is
/// the whole reason the foreground is a layer of its own.
pub fn iso_board_fg(args: &[Value]) -> Value {
    let h = n(args, 0) as i32;
    if h < 0 || h as usize >= MAX_BOARDS {
        return Value::Number(-1.0);
    }
    Value::Number(ISO_BOARD_FG.with(|c| c.borrow()[h as usize]) as f64)
}

/// `iso_board_ox(board)` / `iso_board_oy(board)` — iso projection origin used when the floor was baked.
pub fn iso_board_ox(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.ox).unwrap_or(96) as f64)
}

pub fn iso_board_oy(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.oy).unwrap_or(24) as f64)
}

/// `iso_board_lift(board)` — pixels the floor art rises per elevation unit, as baked. See
/// `IsoBoard::lift`; defaults to the classic 8 for boards that predate the field.
pub fn iso_board_lift(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.lift).unwrap_or(8) as f64)
}

/// `iso_w()` / `iso_h()` — the loaded grid's dimensions (valid after `iso_load` / `iso_init`).
pub fn iso_w(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.w) as f64)
}

pub fn iso_h(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.h) as f64)
}

/// `iso_spawn_count(board)` — how many unit spawns the map defines.
pub fn iso_spawn_count(args: &[Value]) -> Value {
    Value::Number(with_board(n(args, 0) as i32, |b| b.spawns.len() as i32).unwrap_or(0) as f64)
}

/// `iso_spawn_col/row/cls/team(board, i)` — the i-th spawn's grid column / row / class index / team.
pub fn iso_spawn_col(args: &[Value]) -> Value {
    spawn_field(args, |s| s.0 as i32)
}

pub fn iso_spawn_row(args: &[Value]) -> Value {
    spawn_field(args, |s| s.1 as i32)
}

pub fn iso_spawn_cls(args: &[Value]) -> Value {
    spawn_field(args, |s| s.2 as i32)
}

pub fn iso_spawn_team(args: &[Value]) -> Value {
    spawn_field(args, |s| s.3 as i32)
}

/// `iso_init(w, h)` — (re)create a `w×h` tactics board (all cells walkable, height 0, unoccupied).
pub fn iso_init(args: &[Value]) -> Value {
    with_iso_grid(|t| t.init(n(args, 0) as i32, n(args, 1) as i32));
    Value::Null
}

/// `iso_set_cell(col, row, height, tile, walkable)` — set a cell's elevation, terrain id, and
/// whether a unit may stand on it. Occupancy is separate (`iso_set_occupant`).
pub fn iso_set_cell(args: &[Value]) -> Value {
    with_iso_grid(|t| {
        if let Some(i) = t.idx(n(args, 0) as i32, n(args, 1) as i32) {
            t.cells[i].height = n(args, 2) as u8;
            t.cells[i].tile = n(args, 3) as u8;
            t.cells[i].walkable = n(args, 4) != 0.0;
        }
    });
    Value::Null
}

/// `iso_height(col, row)` — a cell's elevation (0 off-board).
pub fn iso_height(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(0, |i| t.cells[i].height as i32)
    }) as f64)
}

/// `iso_tile(col, row)` — a cell's terrain/type id.
pub fn iso_tile(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(0, |i| t.cells[i].tile as i32)
    }) as f64)
}

/// `iso_walkable(col, row)` — 1 if a unit may stand on the cell, else 0.
pub fn iso_walkable(args: &[Value]) -> Value {
    Value::Bool(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .is_some_and(|i| t.cells[i].walkable)
    }))
}

/// `iso_set_occupant(col, row, entity)` — mark which unit stands on a cell (-1 = empty). Occupied
/// cells block other units' movement.
pub fn iso_set_occupant(args: &[Value]) -> Value {
    with_iso_grid(|t| {
        if let Some(i) = t.idx(n(args, 0) as i32, n(args, 1) as i32) {
            t.cells[i].occupant = n(args, 2) as i32;
        }
    });
    Value::Null
}

/// `iso_occupant(col, row)` — entity id standing on the cell, or -1.
pub fn iso_occupant(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(-1, |i| t.cells[i].occupant)
    }) as f64)
}

/// `iso_move_range(col, row, move, jump)` — flood-fill the tiles reachable from (col,row) for `move`
/// move points and `jump` max height delta; returns the count. Query with `iso_in_range` /
/// `iso_range_*`, and `iso_path` for a route into it.
///
/// The unit-less form takes the movement type as an optional 5th argument (`iso_move_range(c, r,
/// move, jump, flying)`), defaulting to walking when it is omitted. That is what lets a caller ask
/// the hypothetical — "where could a flier get to from here?" — without a unit to ask it about.
pub fn iso_move_range(args: &[Value]) -> Value {
    let flying = n(args, 4) != 0.0;
    // 6th arg is the mover's team for zone-of-control; absent (or -1) means path with no ZoC.
    let team = if args.len() > 5 {
        n(args, 5) as i32
    } else {
        -1
    };
    with_iso_grid(|t| {
        t.move_range(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as i32,
            n(args, 3) as i32,
            flying,
            team,
        )
    });
    Value::Number(with_iso_grid(|t| t.reach.len()) as f64)
}

/// `iso_move_cost(col, row)` — what the last computed move-range spent to REACH that cell, or -1 if
/// it never did.
///
/// The cell list alone can't tell you what the search decided. On an open 4-connected board almost
/// everything stays reachable whatever the rules say — terrain costs and zone of control change how
/// DEARLY, and detour into the price rather than out of the set. This is the number that moves, so
/// it's the one worth asking for, both for a UI that wants to show what a destination costs and for
/// a test that wants to prove a rule fired.
pub fn iso_move_cost(args: &[Value]) -> Value {
    let (c, r) = (n(args, 0) as i32, n(args, 1) as i32);
    Value::Number(with_iso_grid(|t| match t.idx(c, r) {
        Some(i) if t.in_move[i] => t.dist[i],
        _ => -1,
    }) as f64)
}

/// `iso_in_range(col, row)` — 1 if the cell is in the last computed move-range, else 0 (Number so
/// tish `iso_in_range(...) > 0` works).
pub fn iso_in_range(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.idx(n(args, 0) as i32, n(args, 1) as i32)
            .map_or(0, |i| t.in_move[i] as i32)
    }) as f64)
}

/// `iso_range_count()` — number of reachable cells from the last `iso_move_range`.
pub fn iso_range_count(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.reach.len()) as f64)
}

/// `iso_range_col(i)` / `iso_range_row(i)` — the i-th reachable cell.
pub fn iso_range_col(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.reach
            .get(n(args, 0) as usize)
            .map_or(-1, |&(c, _)| c as i32)
    }) as f64)
}

pub fn iso_range_row(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.reach
            .get(n(args, 0) as usize)
            .map_or(-1, |&(_, r)| r as i32)
    }) as f64)
}

/// `iso_path(col, row)` — reconstruct the route from the last move-range's origin to (col,row);
/// returns its length (0 if unreachable). Read with `iso_path_len` / `iso_path_col` / `iso_path_row`.
pub fn iso_path(args: &[Value]) -> Value {
    with_iso_grid(|t| t.path_to(n(args, 0) as i32, n(args, 1) as i32));
    Value::Number(with_iso_grid(|t| t.path.len()) as f64)
}

pub fn iso_path_len(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.path.len()) as f64)
}

pub fn iso_path_col(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.path
            .get(n(args, 0) as usize)
            .map_or(-1, |&(c, _)| c as i32)
    }) as f64)
}

pub fn iso_path_row(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| {
        t.path
            .get(n(args, 0) as usize)
            .map_or(-1, |&(_, r)| r as i32)
    }) as f64)
}

/// `iso_add_unit(col, row, team, speed, move, jump, hp)` — register a unit on the board (claiming the
/// cell) and return its id. Ids count up from 0 in registration order.
pub fn iso_add_unit(args: &[Value]) -> Value {
    let id = with_iso_grid(|t| {
        t.add_unit(
            n(args, 0) as i32,
            n(args, 1) as i32,
            n(args, 2) as u8,
            n(args, 3) as u16,
            n(args, 4) as u8,
            n(args, 5) as u8,
            n(args, 6) as i16,
        )
    });
    Value::Number(id as f64)
}

/// `iso_clear_units()` — drop every unit, keeping the board.
///
/// ⚠️ Before this existed the ONLY way to empty the unit table was `iso_load`, which reloads the
/// whole board. A game that fights two battles on the SAME board therefore could not clear it:
/// `isoUseBoard` returns early when the board is already current, so `iso_load` never runs, the old
/// units stay registered, and the new battle's ids are appended after them. The symptoms are not
/// obviously a leak — the turn queue offers turns to units from a fight that ended, and the tish
/// side's own count disagrees with the engine's.
pub fn iso_clear_units(_args: &[Value]) -> Value {
    with_iso_grid(|t| {
        t.units.clear();
        for c in t.cells.iter_mut() {
            c.occupant = -1;
        }
    });
    Value::Null
}

/// `iso_unit_count()` — number of registered units.
pub fn iso_unit_count(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.units.len()) as f64)
}

/// Per-unit getters: `iso_unit_col/row/team/hp/maxhp/move/jump/speed(id)`, `iso_unit_alive(id)`.
pub fn iso_unit_col(args: &[Value]) -> Value {
    unit_field(args, |u| u.col)
}

pub fn iso_unit_row(args: &[Value]) -> Value {
    unit_field(args, |u| u.row)
}

pub fn iso_unit_team(args: &[Value]) -> Value {
    unit_field(args, |u| u.team as i32)
}

pub fn iso_unit_hp(args: &[Value]) -> Value {
    unit_field(args, |u| u.hp as i32)
}

pub fn iso_unit_maxhp(args: &[Value]) -> Value {
    unit_field(args, |u| u.max_hp as i32)
}

pub fn iso_unit_move(args: &[Value]) -> Value {
    unit_field(args, |u| u.mov as i32)
}

pub fn iso_unit_jump(args: &[Value]) -> Value {
    unit_field(args, |u| u.jump as i32)
}

pub fn iso_unit_speed(args: &[Value]) -> Value {
    unit_field(args, |u| u.speed as i32)
}

pub fn iso_unit_alive(args: &[Value]) -> Value {
    // Number (1/0), not Bool, so tish `iso_unit_alive(id) > 0` comparisons work on GBA.
    Value::Number(with_iso_grid(|t| {
        t.units
            .get(n(args, 0) as usize)
            .map_or(0, |u| u.alive as i32)
    }) as f64)
}

/// `iso_unit_move_range(id)` — flood-fill the reachable tiles for a unit using its own Move/Jump and
/// movement type (from its current cell). Then query with `iso_in_range` / `iso_range_*` / `iso_path`.
pub fn iso_unit_move_range(args: &[Value]) -> Value {
    with_iso_grid(|t| {
        if let Some(u) = t.units.get(n(args, 0) as usize).copied() {
            t.move_range(
                u.col,
                u.row,
                u.mov as i32,
                u.jump as i32,
                u.flying,
                u.team as i32,
            );
        }
    });
    Value::Number(with_iso_grid(|t| t.reach.len()) as f64)
}

/// `iso_set_terrain_cost(tile, cost)` — move points needed to ENTER a cell of terrain id `tile`.
///
/// The engine deliberately does not know that id 33 is water. It owns the search; the game owns what
/// its tiles mean, so cost is pushed in from the example's terrain table. Defaults to 1 everywhere,
/// so a game that never calls this behaves exactly as it did before costs existed.
///
/// Clamped to ≥ 1: a free tile would break the search's assumption that distance only ever increases
/// as it sweeps outward, and the cheapest way to say "this tile is free to cross" is a flier.
pub fn iso_set_terrain_cost(args: &[Value]) -> Value {
    let tile = n(args, 0) as usize;
    let cost = (n(args, 1) as i32).clamp(1, 255) as u8;
    with_iso_grid(|t| {
        if tile < t.cost.len() {
            t.cost[tile] = cost;
        }
    });
    Value::Null
}

/// `iso_knockback(id, from_col, from_row)` — shove a unit one tile directly away from (from_col,
/// from_row). Returns 1 if it moved, 0 if something stopped it.
///
/// Height is deliberately NOT a reason to refuse: being shoved off a ledge is the interesting case,
/// and a `jump` check here would quietly make knockback do nothing near exactly the terrain that
/// makes it worth having. The board edge, a solid cell and another unit all still stop it — a unit
/// backed against any of those takes the hit where it stands.
///
/// The engine moves the body and reports that it did. What a fall COSTS is the game's to decide, so
/// the caller compares the heights either side and applies its own damage; the same split as the
/// damage formula.
pub fn iso_knockback(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let (sc, sr) = (n(args, 1) as i32, n(args, 2) as i32);
    let moved = with_iso_grid(|t| match t.knock_dest(id, sc, sr) {
        Some((nc, nr, _)) => {
            t.unit_set_pos(id, nc, nr);
            true
        }
        None => false,
    });
    Value::Number(moved as i32 as f64)
}

/// `iso_knock_drop(id, from_col, from_row)` — how far the unit WOULD fall if shoved from there, or 0
/// if the shove is blocked or lands level or uphill.
///
/// This exists so an AI can price a shove before committing to one. It deliberately answers with the
/// drop rather than with the destination: the destination would make the caller re-derive the rule
/// for which direction a shove goes and what stops it, and the moment two copies of that rule exist
/// they start to disagree. Blocked and level both answer 0 because they are worth the same to anyone
/// asking — no fall, no damage.
pub fn iso_knock_drop(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let (sc, sr) = (n(args, 1) as i32, n(args, 2) as i32);
    Value::Number(
        with_iso_grid(|t| t.knock_dest(id, sc, sr).map_or(0, |(_, _, d)| d.max(0))) as f64,
    )
}

/// `iso_set_zoc(on)` — turn zone of control on or off for every subsequent move-range query.
///
/// With it on, stepping into a tile adjacent to a living enemy ends the move — the tile is reachable,
/// nothing through it is. That turns a defender from an obstacle you route around into one you have
/// to deal with, which is the entire point of a front line. Off by default so this changes nothing
/// for a game that does not ask for it.
pub fn iso_set_zoc(args: &[Value]) -> Value {
    let on = n(args, 0) != 0.0;
    with_iso_grid(|t| t.zoc_on = on);
    Value::Null
}

/// `iso_revive(id, hp)` — bring a KO'd unit back on the cell it fell on, at `hp` (clamped to its
/// maximum, and to at least 1). Returns 1 if it stood up, 0 if it was already alive or something has
/// since walked onto its cell.
///
/// The occupancy check is the whole reason this cannot live in the example: death RELEASES the cell,
/// so a body is not a wall, and by the time anyone reaches it with a Phoenix Down somebody may be
/// standing there. The engine owns that map, so it is the only place that can answer honestly —
/// and returning 0 rather than reviving onto an occupied cell means the caller can decline to spend
/// the item instead of quietly creating two units on one tile.
pub fn iso_revive(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let hp = n(args, 1) as i16;
    Value::Number(with_iso_grid(|t| {
        let (col, row, max_hp) = match t.units.get(id as usize) {
            Some(u) if !u.alive => (u.col, u.row, u.max_hp),
            _ => return 0,
        };
        match t.idx(col, row) {
            Some(i) if t.cells[i].occupant < 0 => t.cells[i].occupant = id,
            _ => return 0,
        }
        if let Some(u) = t.units.get_mut(id as usize) {
            u.alive = true;
            u.hp = hp.clamp(1, max_hp);
        }
        1
    }) as f64)
}

/// `iso_turn_end(id, moved, acted)` — charge the rest of the turn's counter cost, once the game knows
/// what the unit actually did with it. The base cost was taken when the turn was handed out.
///
/// The split exists so that forgetting to call this cannot wedge the queue: a game that never calls
/// it still has every unit paying 500 a turn and the order still advances, it just loses the tempo
/// rule. Making `turn_next` charge nothing and rely on this would mean one missed call returns the
/// same unit forever.
pub fn iso_turn_end(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    let moved = n(args, 1) != 0.0;
    let acted = n(args, 2) != 0.0;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id) {
            let mut cost = 0;
            if moved {
                cost += COST_MOVE;
            }
            if acted {
                cost += COST_ACTION;
            }
            u.ct = (u.ct - cost).max(0);
        }
    });
    Value::Null
}

/// `iso_unit_ct(id)` — the unit's current turn counter, for a UI that wants to show who is next.
pub fn iso_unit_ct(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    Value::Number(with_iso_grid(|t| t.units.get(id).map_or(0, |u| u.ct)) as f64)
}

/// `iso_unit_set_speed_scale(id, percent)` — scale how fast a unit's turn counter fills. 100 is
/// normal; a haste/slow pair would typically be 200 and 50.
///
/// The engine owns the turn queue and therefore owns this; which status sets it, for how long, and
/// what the numbers are stay the game's. It scales ACCRUAL and not the Speed stat, so the AI's threat
/// tables and the Status screen go on reading an unmodified stat block, and clearing the status is
/// just setting it back to 100 — there is no original value to remember and no way to double-apply.
///
/// Clamped to 1..=1000: a scale of 0 would stop the unit accruing at all, which is not a slow but a
/// removal, and would leave `turn_next` hunting for a tick count that never arrives.
pub fn iso_unit_set_speed_scale(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    let pct = (n(args, 1) as i32).clamp(1, 1000) as u16;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id) {
            u.speed_scale = pct;
        }
    });
    Value::Null
}

/// `iso_unit_set_flying(id, flying)` — a flier pays 1 move point per tile and ignores height deltas.
///
/// It is NOT a bigger move budget: it changes which tiles the budget can be spent on, so a flier
/// crosses a ford or a cliff that would cost a walker two points or stop it outright, and is no
/// faster than anyone else over open ground. Solid cells and occupied cells still block it.
pub fn iso_unit_set_flying(args: &[Value]) -> Value {
    let id = n(args, 0) as usize;
    let f = n(args, 1) != 0.0;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id) {
            u.flying = f;
        }
    });
    Value::Null
}

/// `iso_unit_set_pos(id, col, row)` — move a unit (updates board occupancy).
pub fn iso_unit_set_pos(args: &[Value]) -> Value {
    with_iso_grid(|t| t.unit_set_pos(n(args, 0) as i32, n(args, 1) as i32, n(args, 2) as i32));
    Value::Null
}

/// `iso_damage(id, amount)` — subtract HP; a unit at ≤0 HP dies (alive=false, cell freed). Returns
/// remaining HP.
pub fn iso_damage(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let amt = n(args, 1) as i16;
    with_iso_grid(|t| {
        let (dead, c, r) = match t.units.get_mut(id as usize) {
            Some(u) if u.alive => {
                u.hp -= amt;
                if u.hp <= 0 {
                    u.hp = 0;
                    u.alive = false;
                }
                (!u.alive, u.col, u.row)
            }
            _ => (false, 0, 0),
        };
        if dead {
            if let Some(i) = t.idx(c, r) {
                if t.cells[i].occupant == id {
                    t.cells[i].occupant = -1;
                }
            }
        }
    });
    unit_field(args, |u| u.hp as i32)
}

/// `iso_heal(id, amount)` — restore HP to a living unit, capped at its `max_hp`. Returns the unit's
/// new HP (0 if the unit is dead/unknown — dead units cannot be healed here). For support classes.
pub fn iso_heal(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let amt = n(args, 1) as i16;
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id as usize) {
            if u.alive {
                u.hp = (u.hp + amt).min(u.max_hp);
            }
        }
    });
    unit_field(args, |u| if u.alive { u.hp as i32 } else { 0 })
}

/// `iso_unit_set_maxhp(id, v)` — set a unit's HP ceiling, keeping current HP in range. Returns the
/// new maximum (0 for an unknown unit).
///
/// ⚠️ This exists because a level-growth package documented a rule it could not implement: "what
/// a level does NOT raise is HP … there is no setter for the maximum, so growing it would mean a
/// Rust change." Levelling normally raises HP, so the engine was quietly deciding a game-design
/// question. A missing setter is not a neutral omission — it becomes a rule.
///
/// Raising the ceiling raises current HP by the same amount rather than leaving the unit wounded: a
/// level-up mid-battle that hands you a bigger empty bar reads as a penalty. Lowering it (a curse, a
/// job change) clamps current HP down instead.
pub fn iso_unit_set_maxhp(args: &[Value]) -> Value {
    let id = n(args, 0) as i32;
    let v = (n(args, 1) as i16).max(1);
    with_iso_grid(|t| {
        if let Some(u) = t.units.get_mut(id as usize) {
            let delta = v - u.max_hp;
            u.max_hp = v;
            if delta > 0 {
                u.hp += delta;
            }
            if u.hp > u.max_hp {
                u.hp = u.max_hp;
            }
        }
    });
    unit_field(args, |u| u.max_hp as i32)
}

/// `iso_turn_next()` — advance the speed-based turn queue and return the id of the unit whose turn it
/// is (or -1 if none are alive).
pub fn iso_turn_next(_args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.turn_next()) as f64)
}

/// `iso_adjacent_enemy(unitId)` — a living enemy on one of the unit's 4 neighbouring tiles (in
/// range of a melee attack), or -1 if none. Used for attack targeting and AI.
pub fn iso_adjacent_enemy(args: &[Value]) -> Value {
    Value::Number(with_iso_grid(|t| t.adjacent_enemy(n(args, 0) as i32)) as f64)
}

pub fn iso_stack_count_typed(p0: i32, p1: i32, p2: i32) -> i32 {
    match iso_stack_count(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_stack_elev_typed(p0: i32, p1: i32, p2: i32, p3: i32) -> i32 {
    match iso_stack_elev(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_stack_tile_typed(p0: i32, p1: i32, p2: i32, p3: i32) -> i32 {
    match iso_stack_tile(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_load_typed(p0: i32) {
    iso_load(&[Value::Number(p0 as f64)]);
}

pub fn iso_board_bg_typed(p0: i32) -> i32 {
    match iso_board_bg(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_board_cw_typed(p0: i32) -> i32 {
    match iso_board_cw(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}

pub fn iso_board_ch_typed(p0: i32) -> i32 {
    match iso_board_ch(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}

pub fn iso_board_mapw_typed(p0: i32) -> i32 {
    match iso_board_mapw(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}

pub fn iso_board_maph_typed(p0: i32) -> i32 {
    match iso_board_maph(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 512,
    }
}

pub fn iso_board_fg_typed(p0: i32) -> i32 {
    match iso_board_fg(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => -1,
    }
}

pub fn iso_board_ox_typed(p0: i32) -> i32 {
    match iso_board_ox(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_board_oy_typed(p0: i32) -> i32 {
    match iso_board_oy(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_board_lift_typed(p0: i32) -> i32 {
    match iso_board_lift(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_w_typed() -> i32 {
    match iso_w(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_h_typed() -> i32 {
    match iso_h(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_spawn_count_typed(p0: i32) -> i32 {
    match iso_spawn_count(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_spawn_col_typed(p0: i32, p1: i32) -> i32 {
    match iso_spawn_col(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_spawn_row_typed(p0: i32, p1: i32) -> i32 {
    match iso_spawn_row(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_spawn_cls_typed(p0: i32, p1: i32) -> i32 {
    match iso_spawn_cls(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_spawn_team_typed(p0: i32, p1: i32) -> i32 {
    match iso_spawn_team(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_init_typed(p0: i32, p1: i32) {
    iso_init(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}

pub fn iso_set_cell_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32) {
    iso_set_cell(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
    ]);
}

pub fn iso_height_typed(p0: i32, p1: i32) -> i32 {
    match iso_height(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_tile_typed(p0: i32, p1: i32) -> i32 {
    match iso_tile(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_walkable_typed(p0: i32, p1: i32) -> i32 {
    match iso_walkable(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Bool(b) => b as i32,
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_set_occupant_typed(p0: i32, p1: i32, p2: i32) {
    iso_set_occupant(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}

pub fn iso_occupant_typed(p0: i32, p1: i32) -> i32 {
    match iso_occupant(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_move_range_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32, p5: i32) -> i32 {
    match iso_move_range(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
        Value::Number(p5 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_move_cost_typed(p0: i32, p1: i32) -> i32 {
    match iso_move_cost(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_in_range_typed(p0: i32, p1: i32) -> i32 {
    match iso_in_range(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_range_count_typed() -> i32 {
    match iso_range_count(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_range_col_typed(p0: i32) -> i32 {
    match iso_range_col(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_range_row_typed(p0: i32) -> i32 {
    match iso_range_row(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_path_typed(p0: i32, p1: i32) -> i32 {
    match iso_path(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_path_len_typed() -> i32 {
    match iso_path_len(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_path_col_typed(p0: i32) -> i32 {
    match iso_path_col(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_path_row_typed(p0: i32) -> i32 {
    match iso_path_row(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_add_unit_typed(p0: i32, p1: i32, p2: i32, p3: i32, p4: i32, p5: i32, p6: i32) -> i32 {
    match iso_add_unit(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
        Value::Number(p3 as f64),
        Value::Number(p4 as f64),
        Value::Number(p5 as f64),
        Value::Number(p6 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_clear_units_typed() {
    iso_clear_units(&[]);
}

pub fn iso_unit_count_typed() -> i32 {
    match iso_unit_count(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_col_typed(p0: i32) -> i32 {
    match iso_unit_col(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_row_typed(p0: i32) -> i32 {
    match iso_unit_row(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_team_typed(p0: i32) -> i32 {
    match iso_unit_team(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_hp_typed(p0: i32) -> i32 {
    match iso_unit_hp(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_maxhp_typed(p0: i32) -> i32 {
    match iso_unit_maxhp(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_move_typed(p0: i32) -> i32 {
    match iso_unit_move(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_jump_typed(p0: i32) -> i32 {
    match iso_unit_jump(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_speed_typed(p0: i32) -> i32 {
    match iso_unit_speed(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_alive_typed(p0: i32) -> i32 {
    match iso_unit_alive(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_move_range_typed(p0: i32) -> i32 {
    match iso_unit_move_range(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_set_terrain_cost_typed(p0: i32, p1: i32) {
    iso_set_terrain_cost(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}

pub fn iso_knockback_typed(p0: i32, p1: i32, p2: i32) -> i32 {
    match iso_knockback(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_knock_drop_typed(p0: i32, p1: i32, p2: i32) -> i32 {
    match iso_knock_drop(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_set_zoc_typed(p0: i32) {
    iso_set_zoc(&[Value::Number(p0 as f64)]);
}

pub fn iso_revive_typed(p0: i32, p1: i32) -> i32 {
    match iso_revive(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_turn_end_typed(p0: i32, p1: i32, p2: i32) {
    iso_turn_end(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}

pub fn iso_unit_ct_typed(p0: i32) -> i32 {
    match iso_unit_ct(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_set_speed_scale_typed(p0: i32, p1: i32) {
    iso_unit_set_speed_scale(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}

pub fn iso_unit_set_flying_typed(p0: i32, p1: i32) {
    iso_unit_set_flying(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]);
}

pub fn iso_unit_set_pos_typed(p0: i32, p1: i32, p2: i32) {
    iso_unit_set_pos(&[
        Value::Number(p0 as f64),
        Value::Number(p1 as f64),
        Value::Number(p2 as f64),
    ]);
}

pub fn iso_damage_typed(p0: i32, p1: i32) -> i32 {
    match iso_damage(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_heal_typed(p0: i32, p1: i32) -> i32 {
    match iso_heal(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_unit_set_maxhp_typed(p0: i32, p1: i32) -> i32 {
    match iso_unit_set_maxhp(&[Value::Number(p0 as f64), Value::Number(p1 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_turn_next_typed() -> i32 {
    match iso_turn_next(&[]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}

pub fn iso_adjacent_enemy_typed(p0: i32) -> i32 {
    match iso_adjacent_enemy(&[Value::Number(p0 as f64)]) {
        Value::Number(v) => v as i32,
        _ => 0,
    }
}