manabrew-engine 0.4.1

Magic: The Gathering rules engine — a Rust port of Forge
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
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
pub mod activation_table;
mod alt_costs;
mod card_assembly;
pub mod card_changed_words;
pub mod card_clone_states;
pub mod card_collection;
pub mod card_collection_view;
pub mod card_copy_service;
pub mod card_damage_history;
pub mod card_damage_map;
pub mod card_factory;
pub mod card_factory_util;
pub mod card_lists;
pub mod card_play_option;
pub mod card_predicates;
pub mod card_property;
pub mod card_state;
pub mod card_trait_changes;
pub mod card_util;
pub mod card_zone_table;
pub mod counter_enum_type;
pub mod counter_keyword_type;
pub mod counter_type;
pub mod damage_history;
pub mod filter_constants;
mod keyword_gen;
pub mod perpetual;
pub mod svar_cache;
pub mod token;
pub mod token_create_table;
pub mod trait_card_trait_changes;
pub mod valid_filter;
use crate::card::activation_table::ActivationTable;
use crate::core::HasSVars;
pub use counter_type::CounterType;

/// Type alias for the Keyword enum, used by keyword helper methods.
use crate::keyword::keyword_instance::Keyword as Kw;

// ── Keyword marker constants ──────────────────────────────────────────
// These are synthetic keywords injected at runtime to track card state.
// Using constants avoids magic strings scattered across the codebase.

/// Prefix for the Plotted marker. Full keyword is `"Plotted:{turn}"`.
/// The turn number prevents casting on the same turn the card was plotted.
pub const KEYWORD_PLOTTED_PREFIX: &str = "Plotted:";

/// Marker for cards exiled via Warp's end-of-turn trigger.
/// These cards can be cast from exile on a later turn for their normal mana cost.
pub const KEYWORD_WARP_EXILED: &str = "WarpExiled";

use std::collections::{BTreeMap, HashMap, HashSet};

use forge_carddb::CardRules;
use forge_foundation::{CardTypeLine, ColorSet, CoreType, ManaCost, ZoneType};
use serde::{Deserialize, Serialize};

use crate::ability::activated::{parse_activated_ability, ActivatedAbility};
use crate::card::perpetual::perpetual_record::PerpetualRecord;
use crate::card::svar_cache::{ParsedSVar, ParsedSVarCache};
use crate::cost::{parse_cost, Cost};
use crate::game::GameState;
use crate::ids::{CardId, PlayerId};
use crate::parsing::{keys, parse_or_warn, Params, ParsedParams};
use crate::replacement::{parse_replacement_effect, ReplacementEffect};
use crate::spellability::{SpellAbility, TargetRestrictions};
use crate::staticability::{parse_static_ability, StaticAbility};
use crate::trigger::Trigger;

/// Build the full `"Plotted:{turn}"` keyword string.
fn colorless_color_set() -> ColorSet {
    ColorSet::COLORLESS
}

fn parse_literal_target_count(expr: &str) -> Option<i32> {
    if let Ok(n) = expr.trim().parse::<i32>() {
        return Some(n);
    }
    expr.trim().strip_prefix('+')?.parse::<i32>().ok()
}

pub fn make_plotted_keyword(turn: u32) -> String {
    format!("{}{}", KEYWORD_PLOTTED_PREFIX, turn)
}

/// Extract the turn number from a `"Plotted:{turn}"` keyword, if present.
pub fn parse_plotted_turn(kw: &str) -> Option<u32> {
    kw.strip_prefix(KEYWORD_PLOTTED_PREFIX)
        .and_then(|s| s.parse().ok())
}

/// Stores alternate-face characteristics for double-faced cards (DFCs).
/// The `transform()` method swaps `Card` fields with these values.
/// Mirrors Java's `CardState` stored as the "backside" state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CardOtherPart {
    pub name: String,
    #[serde(default)]
    pub oracle_text: String,
    /// True when the card's split type is `Modal` (MDFC). Only a modal back face
    /// may be played from hand; transform/meld backs may not. Mirrors
    /// `Card.isModal()` (`getRules().getSplitType() == CardSplitType.Modal`).
    /// Invariant across `transform()`, so it is not swapped there.
    #[serde(default)]
    pub is_modal: bool,
    pub type_line: CardTypeLine,
    pub mana_cost: ManaCost,
    pub color: ColorSet,
    pub base_power: Option<i32>,
    pub base_toughness: Option<i32>,
    pub keywords: crate::keyword::keyword_collection::KeywordCollection,
    pub abilities: Vec<String>,
    pub triggers: Vec<Trigger>,
    pub static_abilities: Vec<crate::staticability::StaticAbility>,
    pub replacement_effects: Vec<crate::replacement::ReplacementEffect>,
    pub svars: BTreeMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CardActionSpellSpec {
    pub ability_index: usize,
    pub has_valid_tgts: bool,
    pub cost_contains_x: bool,
    #[serde(default)]
    pub target_chain: Vec<CardActionTargetSpec>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CardActionTargetSpec {
    pub target_restrictions: TargetRestrictions,
    pub min_targets: Option<i32>,
}

/// When a `SP$ GainControl` steals a permanent, the revert trigger is stored
/// here. Fires during the appropriate phase or event handler, at which point
/// the card's `original_controller_eot` is restored.
///
/// Mirrors the subset of Java `ControlGainEffect.LoseControl$` variants that
/// schedule a `GameCommand`. Java also has variants we intentionally skip
/// here (`StaticCommandCheck` driven by an SVar comparator, `UntilSourceUnattached`,
/// `UntilTheEndOfYourNextTurn`) — they require either a scheduler that scans
/// every tick or a turn-owner counter that the engine doesn't maintain yet.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, strum_macros::EnumString)]
#[strum(ascii_case_insensitive)]
pub enum LoseControlCondition {
    /// Revert at the end of the current turn (default EOT branch).
    #[strum(serialize = "EOT", serialize = "UntilEOT", serialize = "EndOfTurn")]
    EndOfTurn,
    /// Revert the next time this card untaps.
    #[strum(serialize = "Untap", serialize = "UntilUntap", serialize = "NextUntap")]
    NextUntap,
    /// Revert at end of combat (Threaten-style steal-and-swing).
    EndOfCombat,
    /// Revert when the card leaves the battlefield.
    LeavesPlay,
}

/// Saved pre-animate state for AnimateEffect, restored at cleanup.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnimateState {
    pub original_type_line: CardTypeLine,
    pub original_base_power: Option<i32>,
    pub original_base_toughness: Option<i32>,
    pub original_color: ColorSet,
    /// Snapshot of intrinsic keywords before animate added any. Restored
    /// when the card leaves the battlefield (CR 400.7) so granted keywords
    /// (e.g. Animate `Keywords$ Haste`) do not persist into the new object.
    #[serde(default)]
    pub original_keywords: Option<crate::keyword::keyword_collection::KeywordCollection>,
}

/// Saved pre-clone copiable characteristics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CloneState {
    #[serde(default)]
    pub expires_at_cleanup: bool,
    pub original_card_name: String,
    #[serde(default)]
    pub original_oracle_text: String,
    pub original_type_line: CardTypeLine,
    pub original_mana_cost: ManaCost,
    pub original_color: ColorSet,
    pub original_base_power: Option<i32>,
    pub original_base_toughness: Option<i32>,
    pub original_keywords: crate::keyword::keyword_collection::KeywordCollection,
    pub original_abilities: Vec<String>,
    pub original_activated_abilities: Vec<ActivatedAbility>,
    pub original_triggers: Vec<Trigger>,
    pub original_svars: BTreeMap<String, String>,
    pub original_static_abilities: Vec<StaticAbility>,
    pub original_replacement_effects: Vec<ReplacementEffect>,
    /// Intrinsic ability count *before* the clone overwrote it. The static
    /// layer truncates `activated_abilities` to `base_ability_count` each
    /// pass, so reverting `activated_abilities` without also restoring this
    /// would let the layer trim the recovered abilities right back to the
    /// clone's count.
    #[serde(default)]
    pub original_base_ability_count: usize,
    /// Intrinsic trigger count *before* the clone, for the same reason.
    #[serde(default)]
    pub original_base_trigger_count: usize,
}

/// A card instance in a game. This is the mutable game-state representation,
/// as opposed to CardRules which is the immutable definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Card {
    pub id: CardId,
    /// The card's active face name (front face for single/split cards, active face for DFC).
    /// On the battlefield, this is the name that's displayed.
    pub card_name: String,
    /// The full combined name for split/room cards (e.g. "Walk-In Closet // Forgotten Cellar").
    /// For non-split cards, this equals `card_name`. Used for hand/graveyard display and
    /// database lookups.
    pub full_name: String,
    #[serde(default)]
    pub oracle_text: String,

    // Ownership and control
    pub owner: PlayerId,
    pub controller: PlayerId,

    // Current zone
    pub zone: ZoneType,

    // Type line (can be modified by effects)
    pub type_line: CardTypeLine,

    // Mana cost (can be modified)
    pub mana_cost: ManaCost,

    // Color (can be modified)
    pub color: ColorSet,

    /// Immutable color identity from the card's rules (CR 903.4): mana cost
    /// colors plus any mana symbols found in the oracle text (outside reminder
    /// text). Used for commander color-identity checks and Combo ColorIdentity
    /// mana productions. Mirrors Java `CardRules.getColorIdentity()`.
    #[serde(default = "colorless_color_set")]
    pub color_identity: ColorSet,

    // Power/Toughness (base values, can be modified)
    pub base_power: Option<i32>,
    pub base_toughness: Option<i32>,
    /// Printed starting loyalty for planeswalkers.
    pub initial_loyalty: Option<String>,
    /// Temporary P/T modifications from spells/abilities resolving this turn
    /// (e.g. Giant Growth).  Reset when leaving the battlefield.
    pub power_modifier: i32,
    pub toughness_modifier: i32,
    /// Perpetual P/T modifications — persist across zone changes (never reset).
    /// Applied by `PumpAll` / `Pump` effects with `Duration$ Perpetual`.
    pub perpetual_power_modifier: i32,
    pub perpetual_toughness_modifier: i32,
    /// Java-parity storage of all perpetual effect records applied to this card.
    #[serde(default)]
    pub perpetual: Vec<PerpetualRecord>,
    /// Layer 7b override: set by `SetPower$` / `SetToughness$` continuous effects.
    /// `None` means use `base_power` / `base_toughness` as normal.
    /// Reset to `None` each time [`layer::apply_continuous_effects`] runs.
    pub static_set_power: Option<i32>,
    pub static_set_toughness: Option<i32>,
    /// Layer 7c bonus: accumulated from `AddPower$` / `AddToughness$` anthems.
    /// Reset to 0 each time [`layer::apply_continuous_effects`] runs.
    pub static_power_modifier: i32,
    pub static_toughness_modifier: i32,

    // Combat/state
    pub tapped: bool,
    /// Mana atoms produced the last time this land was tapped for mana.
    /// Used for mana rollback — when untapping, remove exactly this mana from pool.
    /// Covers base production + aura triggers + static doublers + any other source.
    #[serde(skip)]
    pub last_mana_produced: Option<Vec<u16>>,
    pub flipped: bool,
    pub face_down: bool,
    /// True if this card has Morph or Megamorph and can be cast face-down for {3}.
    pub has_morph: bool,
    /// True if this card was discarded (CR 400.7k, for TrackDiscarded$ effects).
    pub discarded: bool,
    /// True if this card was unearthed (should be exiled at EOT or if leaving battlefield).
    pub unearthed: bool,
    /// Class enchantment level (1 = base, 2+ = leveled up).
    pub class_level: i32,
    /// Soulbond: paired creature (if any).
    pub paired_with: Option<CardId>,
    /// True if this card was manifested (face-down as 2/2 creature).
    pub manifested: bool,
    /// True if this card was cloaked (face-down with ward {2}).
    pub cloaked: bool,
    /// True if this card was foretold (exiled face-down via Foretell).
    pub foretold: bool,
    /// Other card(s) melded/merged with this one. When this card changes zones,
    /// all melded parts move together (CR 712.4).
    pub melded_with: Vec<CardId>,
    /// True if foretold cost was set by an effect (not the card's own Foretell ability).
    pub foretold_cost_by_effect: bool,
    /// True if this card is currently bestowed (attached as an Aura via Bestow).
    pub is_bestowed: bool,
    pub summoning_sick: bool,
    #[serde(default)]
    pub came_under_control_since_last_upkeep: bool,
    pub exerted: bool,
    pub damage: i32,
    /// Zone the card was cast from (mirrors Java `Card.castFrom`). `Some` only
    /// while the card represents a spell that was actually cast — set during
    /// cast resolution, cleared on every zone change so the next "object" the
    /// card becomes (CR 400.7) starts with no cast history. Used by
    /// `wasCast`/`wasCastByYou` valid filters (e.g. Sunderflock's ETB).
    pub cast_from: Option<ZoneType>,

    // Counters
    pub counters: BTreeMap<CounterType, i32>,

    // Keywords intrinsic to this card (from its card definition).
    // Now stored as a `KeywordCollection` for structured typed lookups.
    pub keywords: crate::keyword::keyword_collection::KeywordCollection,
    /// Keywords granted by continuous static effects (Layer 6).
    /// Reset and recomputed each time [`layer::apply_continuous_effects`] runs.
    pub granted_keywords: crate::keyword::keyword_collection::KeywordCollection,
    /// SVars supplied by granted text (e.g. AddTrigger$/AddAbility$).
    /// Reset and recomputed each time [`layer::apply_continuous_effects`] runs.
    #[serde(default)]
    pub granted_svars: BTreeMap<String, String>,
    /// Type tokens added by continuous static effects (Layer 4, `AddType$`).
    /// Reset and recomputed each time [`layer::apply_continuous_effects`] runs.
    /// The listed strings may be supertypes, core card types, or subtypes;
    /// keeping a separate list lets us revert on reset without losing the
    /// card's intrinsic type line.
    pub static_added_subtypes: Vec<String>,
    #[serde(skip)]
    pub static_type_line_base: Option<CardTypeLine>,
    #[serde(skip)]
    pub changed_type_line_base: Option<CardTypeLine>,
    #[serde(skip)]
    pub changed_base_power: Option<Option<i32>>,
    #[serde(skip)]
    pub changed_base_toughness: Option<Option<i32>>,
    /// Keywords granted temporarily by pump effects (`KW$` parameter) until end of turn.
    /// Cleared during step_cleanup alongside power_modifier / toughness_modifier.
    pub pump_keywords: crate::keyword::keyword_collection::KeywordCollection,
    /// Number of triggers added temporarily by `DB$ Animate | Triggers$` effects.
    /// At cleanup, this many triggers are popped from the end of the `triggers` vec.
    pub pump_trigger_count: usize,

    // Abilities (raw strings from card definition)
    pub abilities: Vec<String>,
    /// Prebound SP$ ability metadata used by action-space filters.
    #[serde(default)]
    pub action_spell_specs: Vec<CardActionSpellSpec>,
    /// Prebound first SP$ Cost used by action-space non-mana cost checks.
    #[serde(default)]
    pub action_spell_cost: Option<Cost>,
    /// Prebound AIPhyrexianPayment$ policy from printed ability text.
    #[serde(default)]
    pub ai_phyrexian_payment: Option<String>,
    /// Prebound minimum Spree mode cost from Choices$ -> SVar ModeCost$.
    #[serde(default)]
    pub spree_min_mode_cost: Option<i32>,

    // Parsed activated abilities (from AB$ lines in abilities)
    pub activated_abilities: Vec<ActivatedAbility>,
    /// Number of base activated abilities (before continuous effects add more via AddAbility$).
    /// Used by `apply_continuous_effects` to truncate granted abilities on reset.
    pub base_ability_count: usize,
    /// Number of base triggers (before continuous effects add more via AddTrigger$).
    /// Used by `apply_continuous_effects` to truncate granted triggers on reset.
    pub base_trigger_count: usize,
    /// Applied card-trait mutation layers keyed by (timestamp, static_id).
    /// Mirrors Java `changedCardTraits` table.
    pub changed_card_traits:
        std::collections::BTreeMap<(i64, i64), card_trait_changes::CardTraitChanges>,
    /// Text-layer trait changes keyed by (timestamp, static_id).
    /// Mirrors Java `changedCardTraitsByText` table.
    pub changed_card_traits_by_text:
        std::collections::BTreeMap<(i64, i64), card_trait_changes::CardTraitChanges>,

    /// Parsed static abilities (from S$ lines in abilities).
    /// Mirrors Java Forge `Card.getStaticAbilities()`.
    pub static_abilities: Vec<StaticAbility>,

    // Combat tracking
    pub has_deathtouch_damage: bool,
    /// Set by `Mode$ CantAttack` static effects. Reset each time
    /// [`layer::apply_continuous_effects`] runs.
    pub cant_attack_static: bool,
    /// Set by `Mode$ CantBlock` static effects. Reset each time
    /// [`layer::apply_continuous_effects`] runs.
    pub cant_block_static: bool,

    // Turn tracking
    #[serde(default)]
    pub turn_in_zone: u32,
    pub entered_battlefield_this_turn: bool,
    pub attacked_this_turn: bool,
    /// Snapshot of whether this permanent was tapped at the start of its
    /// controller's current turn (before untap step).
    pub started_turn_tapped: bool,

    // Triggers — mirrors Java Card.getTriggers()
    pub triggers: Vec<Trigger>,
    // SVars — mirrors Java Card.getSVars()
    pub svars: BTreeMap<String, String>,
    #[serde(skip, default)]
    pub parsed_svar_cache: ParsedSVarCache,

    // Commander tracking
    /// True if this card is designated as a commander.
    pub is_commander: bool,
    /// True if this commander entered graveyard or exile since the last SBA check
    /// and may still be moved to the command zone.
    pub move_to_command_zone: bool,
    /// How many times this commander has been cast from the command zone (for tax).
    pub commander_cast_count: u32,

    /// True if this permanent is a token or a copy-token (ceases to exist on zone change).
    pub is_token: bool,

    /// Set when the card is cast from graveyard via Flashback. Used by the
    /// flashback replacement effect to exile the card when it leaves the stack.
    pub cast_with_flashback: bool,
    /// Set when the card is cast from graveyard via Harmonize. Used by the
    /// Harmonize replacement effect to exile the card when it leaves the stack.
    pub cast_with_harmonize: bool,

    // Replacement effects — parsed from R$ lines in card abilities.
    // Mirrors Java `Card.getReplacementEffects()`.
    pub replacement_effects: Vec<ReplacementEffect>,

    // Attachment tracking (Auras / Equipment).
    // Mirrors Java `Card.getAttachedTo()` / `Card.getAttachedCards()`.
    /// The permanent this card is currently attached to (for Auras/Equipment).
    pub attached_to: Option<CardId>,
    pub attached_to_player: Option<PlayerId>,
    /// Whether this equipment was attached/moved this turn (AI memory to prevent ping-ponging).
    /// Cleared at start of each turn. Mirrors Java `AiCardMemory.MemorySet.ATTACHED_THIS_TURN`.
    pub attached_this_turn: bool,
    /// Cards currently attached to this permanent (inverse of `attached_to`).
    pub attachments: Vec<CardId>,

    // Memory for "Remember" and "Imprint" parameters
    /// Cards remembered by this card (for RememberCountered, etc.)
    pub remembered_cards: Vec<CardId>,
    /// Players remembered by this card (for Player.IsRemembered checks).
    pub remembered_players: Vec<PlayerId>,
    /// Cards imprinted on this card (for Imprint mechanic, e.g. Chrome Mox).
    pub imprinted_cards: Vec<CardId>,
    /// Cards associated via gain-control effects.
    pub gain_control_targets: Vec<CardId>,
    /// Cards linked by "until leaves battlefield" tracking.
    pub until_leaves_battlefield: Vec<CardId>,
    /// Cards exiled by this card/effect.
    pub exiled_cards: Vec<CardId>,
    /// Cards exiled specifically to pay this card's current activation/cast cost.
    /// This is reset at the start of each cost payment attempt.
    pub paid_cost_exiled_cards: Vec<CardId>,
    /// Cards haunting this card.
    pub haunted_by: Vec<CardId>,
    /// Card currently haunted by this card.
    pub haunting: Option<CardId>,
    /// Per-player chosen card map.
    pub chosen_map: HashMap<PlayerId, Vec<CardId>>,
    /// CMC values remembered by this card
    pub remembered_cmc: Vec<i32>,
    /// Source card that created this effect card (for Card.EffectSource checks).
    pub effect_source: Option<CardId>,
    #[serde(default)]
    pub clone_origin: Option<CardId>,
    #[serde(default)]
    pub copied_permanent: Option<CardId>,
    /// The spell ability used to cast this card instance onto the stack.
    /// Mirrors Java `Card.getCastSA()`. Populated when the card hits the stack,
    /// cleared when it leaves the battlefield.
    #[serde(skip, default)]
    pub cast_sa: Option<Box<SpellAbility>>,
    /// For `SP$ Charm`: last turn each mode (keyed by its SVar name) was chosen
    /// on this card instance. Feeds `ChoiceRestriction$` filtering.
    /// Mirrors the per-card mode history Java keeps on `Card`.
    #[serde(default)]
    pub chosen_charm_modes: HashMap<String, i32>,
    /// LKI (last-known-information) snapshots of cards remembered by this
    /// card via `RememberLKI$`. Each entry is a frozen copy taken at remember
    /// time via `CardCopyService::get_lki_copy`. Callers that care about
    /// "what was this creature when it died" query this list instead of
    /// `remembered_cards` (which stores live IDs and drifts).
    #[serde(skip, default)]
    pub remembered_lki_cards: Vec<Card>,
    /// When set, the card's `original_controller_eot` must be restored on the
    /// trigger described here. Mirrors the Java `ControlGainEffect` set of
    /// `LoseControl$` variants that register distinct GameCommands.
    #[serde(default)]
    pub lose_control_condition: Option<LoseControlCondition>,
    /// True if this temporary effect expires at end of turn cleanup.
    pub temp_effect_until_eot: bool,
    /// Host card this temporary effect is linked to; when host leaves the
    /// battlefield, this effect expires.
    pub temp_effect_host: Option<CardId>,
    /// Forget remembered cards when they move from this origin zone.
    pub forget_on_moved_origin: Option<ZoneType>,
    /// Exile this effect when remembered cards become empty after forget logic.
    pub exile_when_no_remembered: bool,
    /// When this card is in exile, the card that caused it to be exiled here.
    /// Used for `Duration$ UntilHostLeavesPlay` effects (e.g. Deputy of Detention):
    /// when `exiled_by` leaves the battlefield, this card returns to its owner's battlefield.
    pub exiled_by: Option<CardId>,

    /// Original controller to restore at end of turn (for `LoseControl$ EOT`).
    pub original_controller_eot: Option<PlayerId>,

    // Double-faced card (DFC) state
    /// True if this card is currently showing its back face.
    pub is_transformed: bool,
    /// Back-face characteristics for DFC cards. `None` for single-faced cards.
    pub other_part: Option<CardOtherPart>,

    /// Optional set code (e.g., "M21") for specific printings.
    pub set_code: Option<String>,

    /// Optional collector number within a set (e.g., "1", "42").
    /// For tokens, this is the token's collector number in the token set
    /// (e.g., collector "1" in set "THOU" for Adorned Pouncer token).
    pub card_number: Option<String>,

    #[serde(default)]
    pub paper_foil: bool,

    // Phase-out state (issue #22, Phases effect).
    pub phased_out: bool,

    // Regeneration shields (issue #22, Regenerate effect).
    // Decremented instead of destroying; resets at end of turn.
    pub regeneration_shields: i32,

    /// Whether this permanent was kicked when cast.
    /// Mirrors Java `Card.isKicked()`. Stored on the card so triggers
    /// with `ValidCard$ Card.Self+kicked` can check it after resolution.
    pub kicked: bool,
    /// Whether this permanent has become monstrous.
    /// Mirrors Java `Card.isMonstrous()`. Resets when the permanent changes zones.
    pub monstrous: bool,

    /// Colors chosen by ChooseColorEffect (stored for later reference by other effects).
    pub chosen_colors: Vec<String>,
    /// Cards chosen by ChooseCardEffect (stored for later reference by other effects).
    pub chosen_cards: Vec<CardId>,

    /// Saved state for AnimateEffect — restored during step_cleanup.
    pub animate_state: Option<AnimateState>,
    /// Saved state for temporary Clone effects — restored during step_cleanup.
    pub clone_state: Option<CloneState>,

    // ── Issue #53: High-priority effect fields ──────────────────────────
    /// Type chosen by ChooseType effect (e.g. "Goblin", "Artifact").
    pub chosen_type: Option<String>,
    /// Secondary chosen type used by a subset of cards (e.g. Illusionary Terrain).
    pub chosen_type2: Option<String>,
    /// Noted types tracked by effects that accumulate type names.
    pub noted_types: Vec<String>,
    /// Card names chosen by NameCard effect.
    pub named_cards: Vec<String>,
    /// Number chosen by ChooseNumber effect.
    pub chosen_number: Option<i32>,
    /// Player chosen by ChoosePlayer effect.
    pub chosen_player: Option<PlayerId>,
    /// Controller who made the chosen-player choice.
    pub chosen_player_controller: Option<PlayerId>,
    /// Controller who made the chosen-type choice.
    pub chosen_type_controller: Option<PlayerId>,
    /// Whether the chosen player has been revealed.
    pub chosen_player_revealed: bool,
    /// Whether the chosen type has been revealed.
    pub chosen_type_revealed: bool,
    /// Opponent chosen for PromiseGift cost.
    pub promised_gift: Option<PlayerId>,
    /// Attraction lights printed on the card face.
    pub attraction_lights: Vec<u32>,
    /// Attraction sector assignment.
    pub sector: Option<String>,
    /// Chosen sector before assignment effects resolve.
    pub chosen_sector: Option<String>,
    /// Contraption sprocket assignment.
    pub sprocket: i32,
    /// Chosen even/odd marker.
    pub chosen_even_odd: Option<String>,
    /// True if detained — can't attack, block, or activate abilities. Clears at controller's next turn.
    pub detained: bool,
    /// Set during combat to the player this creature is attacking; None if not attacking.
    pub attacking_player: Option<PlayerId>,
    /// Player who goaded this creature. Goaded creature must attack but can't attack goader.
    pub goaded_by: Option<PlayerId>,
    /// Damage prevention shields (decremented when damage would be dealt). Resets at EOT.
    pub damage_prevention: i32,
    /// Damage assigned in current combat assignment step.
    pub assigned_damage: i32,
    /// True if this creature must block if able.
    pub must_block: bool,
    /// Spell cards encoded/ciphered onto this creature.
    pub encoded_cards: Vec<CardId>,
    /// Cards that dealt damage to this creature this turn (for DamagedBy trigger filters).
    /// Mirrors Java `CardDamageHistory.getDamageReceivedThisTurn()`.
    pub damage_sources_this_turn: Vec<CardId>,
    /// Total damage dealt by this card this turn (for Count$TotalDamageDoneByThisTurn).
    /// Mirrors Java `Card.getTotalDamageDoneBy()` via `DamageHistory.getDamageDoneThisTurn()`.
    /// Reset each turn in `new_turn()`.
    pub total_damage_done_this_turn: i32,
    /// Last-known information: power when this card last left the battlefield.
    /// Mirrors Java's LKI system for `TriggeredCard$CardPower`.
    /// `None` means LKI was never captured; `Some(0)` means power was 0.
    pub lki_power: Option<i32>,
    /// Last-known information: toughness when this card last left the battlefield.
    /// `None` means LKI was never captured; `Some(0)` means toughness was 0.
    pub lki_toughness: Option<i32>,
    /// Last-known information: counters when this card last left the battlefield.
    /// Used by `TriggeredCard$CardCounters.TYPE` (e.g. Servant of the Scale death trigger).
    pub lki_counters: Option<std::collections::BTreeMap<CounterType, i32>>,
    /// Damage history tracking (attacks, blocks, damage dealt).
    /// Mirrors Java `CardDamageHistory`.
    #[serde(skip)]
    pub damage_history: damage_history::DamageHistory,
    /// Specific cards this creature must block (set by effects like Lure variants).
    pub must_block_cards: Vec<CardId>,
    /// +1/+1 counters to add on ETB (from mana that adds counters, e.g. Guildmages' Forum).
    pub etb_counters_p1p1: i32,
    /// Bitmask of colors of mana spent to cast this spell (for Sunburst/Converge).
    /// Uses ManaAtom bit flags (W=1, U=2, B=4, R=8, G=16).
    pub colors_spent_to_cast: u16,
    /// Exact mana atoms spent to cast this spell, in payment order.
    /// Mirrors Java's castSA.getPayingMana() use sites such as Adamant.
    pub paying_mana_to_cast: Vec<u16>,
    /// Pre-selected charm/mode indices (for Spree — modes chosen before payment).
    /// If `Some`, charm_effect should use these instead of asking the player again.
    pub chosen_modes: Option<Vec<usize>>,
    /// Number of extra targets paid for via Strive (0 = no extra targets).
    pub strive_extra_targets: u32,
    /// Tracks if this card became a target this turn.
    pub became_target_this_turn: bool,
    /// Temporary controllers layered on this card.
    pub temp_controllers: Vec<PlayerId>,
    /// Players that may look at this card.
    pub may_look_at: Vec<PlayerId>,
    /// Players that may play this card.
    pub may_play: Vec<PlayerId>,
    /// Additional blockers this creature can declare.
    pub can_block_additional: i32,
    /// Whether this creature can block any number of creatures.
    pub can_block_any: bool,
    /// Keywords this card is prevented from having.
    pub cant_have_keywords: HashSet<String>,
    /// Intensity marker value.
    pub intensity: i32,
    /// Card was surveilled this turn.
    pub surveilled: bool,
    /// Card was milled this turn.
    pub milled: bool,
    /// Attraction visited this turn.
    pub visited_this_turn: bool,
    /// Number of times this permanent has crewed this turn.
    pub times_crewed_this_turn: u32,
    /// Whether this permanent is currently crewed.
    pub is_crewed: bool,
    /// Whether this card should ignore legend rule checks.
    pub ignore_legend_rule_flag: bool,
    /// Ability activation counts this turn.
    pub ability_activated_this_turn: u32,
    /// Ability resolution counts this turn.
    pub ability_resolved_this_turn: u32,
    /// Java parity: per-ability activation tracking this turn.
    #[serde(skip)]
    pub number_turn_activations: ActivationTable,
    /// Java parity: per-ability activation tracking this game.
    #[serde(skip)]
    pub number_game_activations: ActivationTable,
    /// Java parity: per-ability resolution tracking this turn.
    #[serde(skip)]
    pub number_ability_resolved: ActivationTable,
    /// Planeswalker activation count this turn.
    pub planeswalker_abilities_activated: u32,
    /// Whether a static effect's increased planeswalker activation limit was used this turn.
    pub planeswalker_activation_limit_used: bool,
    /// Chosen mode count tracking turn marker.
    pub chosen_modes_turn: Option<u32>,
    /// Set when this creature enlisted another creature in the current combat.
    pub enlisted_this_combat: bool,
    /// Per-ability activation count this game (for PowerUp once-per-game restriction).
    pub activations_this_game: std::collections::BTreeMap<usize, u32>,
    /// True once Renown has triggered (creature dealt combat damage to a player).
    /// Mirrors Java `Card.isRenowned()`.
    pub is_renowned: bool,
    /// Monotonically increasing timestamp set each time the card enters a zone.
    /// Used to order same-player triggers by zone entry order, matching
    /// Java's `Zone.cardList` insertion order used by `forEachCardInGame`.
    pub zone_timestamp: u64,

    /// Baseline snapshots used to recompute live lists when trait-change layers
    /// are removed/cleared.
    #[serde(skip)]
    trait_base_activated_abilities: Option<Vec<ActivatedAbility>>,
    #[serde(skip)]
    trait_base_triggers: Option<Vec<Trigger>>,
    #[serde(skip)]
    trait_base_replacement_effects: Option<Vec<ReplacementEffect>>,
    #[serde(skip)]
    trait_base_static_abilities: Option<Vec<StaticAbility>>,
    #[serde(skip)]
    trait_base_keywords: Option<crate::keyword::keyword_collection::KeywordCollection>,
}

/// Transitional alias for downstream code still importing `CardInstance`.
pub type CardInstance = Card;

impl Card {
    pub fn new(
        id: CardId,
        card_name: String,
        owner: PlayerId,
        type_line: CardTypeLine,
        mana_cost: ManaCost,
        color: ColorSet,
        base_power: Option<i32>,
        base_toughness: Option<i32>,
        keywords: Vec<String>,
        abilities: Vec<String>,
    ) -> Self {
        // Parse activated abilities from raw ability strings.
        let activated_abilities: Vec<ActivatedAbility> = abilities
            .iter()
            .enumerate()
            .filter_map(|(i, raw)| {
                parse_or_warn(parse_activated_ability(raw, i), "ActivatedAbility", raw)
            })
            .collect();

        // Parse replacement effects from R$ lines in card abilities.
        // Mirrors Java Card constructor calling ReplacementHandler registration.
        let replacement_effects: Vec<ReplacementEffect> = abilities
            .iter()
            .filter_map(|raw| {
                parse_or_warn(parse_replacement_effect(raw), "ReplacementEffect", raw)
            })
            .collect();

        // Parse static abilities from S$ lines.
        // Mirrors Java Forge Card constructor calling StaticAbility.create().
        let static_abilities: Vec<StaticAbility> = abilities
            .iter()
            .filter_map(|raw| parse_or_warn(parse_static_ability(raw), "StaticAbility", raw))
            .collect();

        let full_name = card_name.clone();
        let color_identity = color;
        let mut card = Card {
            id,
            card_name,
            full_name,
            oracle_text: String::new(),
            owner,
            controller: owner,
            zone: ZoneType::None,
            type_line,
            mana_cost,
            color,
            color_identity,
            base_power,
            base_toughness,
            initial_loyalty: None,
            power_modifier: 0,
            toughness_modifier: 0,
            perpetual_power_modifier: 0,
            perpetual_toughness_modifier: 0,
            perpetual: Vec::new(),
            static_set_power: None,
            static_set_toughness: None,
            static_power_modifier: 0,
            static_toughness_modifier: 0,
            tapped: false,
            last_mana_produced: None,
            flipped: false,
            face_down: false,
            has_morph: false,
            discarded: false,
            unearthed: false,
            class_level: 1,
            paired_with: None,
            manifested: false,
            cloaked: false,
            foretold: false,
            foretold_cost_by_effect: false,
            melded_with: Vec::new(),
            is_bestowed: false,
            summoning_sick: true,
            came_under_control_since_last_upkeep: false,
            exerted: false,
            damage: 0,
            cast_from: None,
            counters: BTreeMap::new(),
            keywords: crate::keyword::keyword_collection::KeywordCollection::from_strings(
                &keywords,
            ),
            granted_keywords: crate::keyword::keyword_collection::KeywordCollection::new(),
            granted_svars: BTreeMap::new(),
            static_added_subtypes: Vec::new(),
            static_type_line_base: None,
            changed_type_line_base: None,
            changed_base_power: None,
            changed_base_toughness: None,
            pump_keywords: crate::keyword::keyword_collection::KeywordCollection::new(),
            pump_trigger_count: 0,
            abilities,
            action_spell_specs: Vec::new(),
            action_spell_cost: None,
            ai_phyrexian_payment: None,
            spree_min_mode_cost: None,
            activated_abilities,
            base_ability_count: 0,
            base_trigger_count: 0,
            changed_card_traits: std::collections::BTreeMap::new(),
            changed_card_traits_by_text: std::collections::BTreeMap::new(),
            static_abilities,
            has_deathtouch_damage: false,
            cant_attack_static: false,
            cant_block_static: false,
            turn_in_zone: 0,
            entered_battlefield_this_turn: false,
            attacked_this_turn: false,
            started_turn_tapped: false,
            triggers: Vec::new(),
            svars: BTreeMap::new(),
            parsed_svar_cache: ParsedSVarCache::default(),
            is_commander: false,
            move_to_command_zone: false,
            commander_cast_count: 0,
            is_token: false,
            cast_with_flashback: false,
            cast_with_harmonize: false,
            replacement_effects,
            attached_to: None,
            attached_to_player: None,
            attached_this_turn: false,
            attachments: Vec::new(),
            remembered_cards: Vec::new(),
            remembered_players: Vec::new(),
            imprinted_cards: Vec::new(),
            gain_control_targets: Vec::new(),
            until_leaves_battlefield: Vec::new(),
            exiled_cards: Vec::new(),
            paid_cost_exiled_cards: Vec::new(),
            haunted_by: Vec::new(),
            haunting: None,
            chosen_map: HashMap::new(),
            remembered_cmc: Vec::new(),
            effect_source: None,
            clone_origin: None,
            copied_permanent: None,
            cast_sa: None,
            chosen_charm_modes: HashMap::new(),
            remembered_lki_cards: Vec::new(),
            lose_control_condition: None,
            temp_effect_until_eot: false,
            temp_effect_host: None,
            forget_on_moved_origin: None,
            exile_when_no_remembered: false,
            exiled_by: None,
            original_controller_eot: None,
            is_transformed: false,
            other_part: None,
            set_code: None,
            card_number: None,
            paper_foil: false,
            phased_out: false,
            regeneration_shields: 0,
            kicked: false,
            monstrous: false,
            chosen_colors: Vec::new(),
            chosen_cards: Vec::new(),
            animate_state: None,
            clone_state: None,
            chosen_type: None,
            chosen_type2: None,
            noted_types: Vec::new(),
            named_cards: Vec::new(),
            chosen_number: None,
            chosen_player: None,
            chosen_player_controller: None,
            chosen_type_controller: None,
            chosen_player_revealed: false,
            chosen_type_revealed: false,
            promised_gift: None,
            attraction_lights: Vec::new(),
            sector: None,
            chosen_sector: None,
            sprocket: 0,
            chosen_even_odd: None,
            detained: false,
            attacking_player: None,
            goaded_by: None,
            damage_prevention: 0,
            assigned_damage: 0,
            must_block: false,
            encoded_cards: Vec::new(),
            damage_sources_this_turn: Vec::new(),
            total_damage_done_this_turn: 0,
            lki_power: None,
            lki_toughness: None,
            lki_counters: None,
            damage_history: damage_history::DamageHistory::default(),
            must_block_cards: Vec::new(),
            etb_counters_p1p1: 0,
            colors_spent_to_cast: 0,
            paying_mana_to_cast: Vec::new(),
            chosen_modes: None,
            strive_extra_targets: 0,
            became_target_this_turn: false,
            temp_controllers: Vec::new(),
            may_look_at: Vec::new(),
            may_play: Vec::new(),
            can_block_additional: 0,
            can_block_any: false,
            cant_have_keywords: HashSet::new(),
            intensity: 0,
            surveilled: false,
            milled: false,
            visited_this_turn: false,
            times_crewed_this_turn: 0,
            is_crewed: false,
            ignore_legend_rule_flag: false,
            ability_activated_this_turn: 0,
            ability_resolved_this_turn: 0,
            number_turn_activations: ActivationTable::default(),
            number_game_activations: ActivationTable::default(),
            number_ability_resolved: ActivationTable::default(),
            planeswalker_abilities_activated: 0,
            planeswalker_activation_limit_used: false,
            chosen_modes_turn: None,
            enlisted_this_combat: false,
            activations_this_game: std::collections::BTreeMap::new(),
            is_renowned: false,
            zone_timestamp: 0,
            trait_base_activated_abilities: None,
            trait_base_triggers: None,
            trait_base_replacement_effects: None,
            trait_base_static_abilities: None,
            trait_base_keywords: None,
        };

        // Generate intrinsic abilities from card properties (mirrors Java CardFactoryUtil)
        card.generate_basic_land_mana_abilities();
        card.generate_keyword_abilities();
        card.generate_keyword_triggers();
        crate::card::card_state::update_types(&mut card);
        crate::card::card_state::update_keywords_cache(&mut card);
        crate::card::card_state::calculate_perpetual_adjusted_mana_cost(&mut card);
        card.refresh_action_specs();
        // Record base ability count so continuous effects can truncate granted abilities.
        card.base_ability_count = card.activated_abilities.len();
        card.base_trigger_count = card.triggers.len();
        card
    }

    pub fn clone_for_parity_snapshot(&self) -> Self {
        let mut out = self.clone();
        out.abilities.clear();
        out.activated_abilities.clear();
        out.triggers.clear();
        for static_ability in &mut out.static_abilities {
            *static_ability.base = crate::card_trait_base::CardTraitBase::default();
        }
        out.replacement_effects.clear();
        out.cast_sa = None;
        out.trait_base_activated_abilities = None;
        out.trait_base_triggers = None;
        out.trait_base_replacement_effects = None;
        out.trait_base_static_abilities = None;
        out.trait_base_keywords = None;
        out
    }

    /// Construct a `Card` from a `CardRules` definition.
    /// This is the single entry point for creating game-ready cards from the
    /// card database. Mirrors Java's `CardFactory.readCard()` + `CardFactoryUtil`.
    ///
    /// Handles:
    /// - Base stats (name, mana cost, type line, color, P/T, keywords, abilities)
    /// - Trigger parsing (T: lines) including SpellCastOrCopy → SpellCopied duplication
    /// - Static ability parsing (S: lines) with alternative cost keyword conversion
    /// - Replacement effect parsing (R: lines)
    /// - SVars
    /// - Double-faced card back face setup
    /// - Intrinsic mana abilities (basic land subtypes)
    /// - Keyword-generated abilities and triggers (Cycling, Prowess, Bushido)
    pub fn from_rules(rules: &CardRules, owner: PlayerId) -> Self {
        card_factory::build_from_rules(rules, owner)
    }

    /// Effective power, accounting for all layer effects and counters.
    ///
    /// Calculation order (CR 613):
    /// - Layer 7b: `static_set_power` overrides `base_power` if set.
    /// - Layer 7c: `static_power_modifier` (anthem bonuses) is added.
    /// - Temporary: `power_modifier` (from spells like Giant Growth) is added.
    /// - Layer 7d: +1/+1 and -1/-1 counters are factored in.
    pub fn power(&self) -> i32 {
        let base = self
            .static_set_power
            .unwrap_or(self.base_power.unwrap_or(0));
        base + self.static_power_modifier
            + self.power_modifier
            + self.perpetual_power_modifier
            + self.counter_count(&CounterType::P1P1)
            - self.counter_count(&CounterType::M1M1)
    }

    /// Effective toughness, accounting for all layer effects and counters.
    pub fn toughness(&self) -> i32 {
        let base = self
            .static_set_toughness
            .unwrap_or(self.base_toughness.unwrap_or(0));
        base + self.static_toughness_modifier
            + self.toughness_modifier
            + self.perpetual_toughness_modifier
            + self.counter_count(&CounterType::P1P1)
            - self.counter_count(&CounterType::M1M1)
    }

    pub fn lethal_damage(&self) -> bool {
        self.damage >= self.toughness()
    }

    pub fn can_be_dealt_damage(&self) -> bool {
        self.zone == ZoneType::Battlefield
            && (self.is_creature()
                || self.type_line.is_planeswalker()
                || self.type_line.core_types.contains(&CoreType::Battle))
    }

    pub fn is_creature(&self) -> bool {
        !self.is_bestowed && self.type_line.is_creature()
    }

    pub fn is_land(&self) -> bool {
        self.type_line.is_land()
    }

    pub fn is_permanent(&self) -> bool {
        self.type_line.is_permanent()
    }

    // CardState-style adapters wired on Card for Java-parity call sites.
    pub fn update_types(&mut self) {
        crate::card::card_state::update_types(self);
    }

    pub fn update_types_for_view(&mut self) {
        crate::card::card_state::update_types_for_view(self);
    }

    pub fn add_type(&mut self, ty: &str) {
        crate::card::card_state::add_type(self, ty);
        self.update_types();
        self.update_types_for_view();
    }

    pub fn remove_type(&mut self, ty: &str) {
        crate::card::card_state::remove_type(self, ty);
        self.update_types();
        self.update_types_for_view();
    }

    pub fn remove_card_types(&mut self) {
        crate::card::card_state::remove_card_types(self);
        self.update_types();
        self.update_types_for_view();
    }

    pub fn set_type(&mut self, type_line: &str) {
        crate::card::card_state::set_type(self, type_line);
        self.update_types();
        self.update_types_for_view();
    }

    pub fn set_type_line(&mut self, type_line: CardTypeLine) {
        self.type_line = type_line;
        self.update_types();
        self.update_types_for_view();
    }

    pub fn add_color(&mut self, color: ColorSet) {
        crate::card::card_state::add_color(self, color);
    }

    pub fn has_intrinsic_keyword(&self, keyword: &str) -> bool {
        crate::card::card_state::has_intrinsic_keyword(self, keyword)
    }

    pub fn add_intrinsic_keyword(&mut self, keyword: &str) -> bool {
        let changed = crate::card::card_state::add_intrinsic_keyword(self, keyword);
        if changed {
            crate::card::card_state::update_keywords_cache(self);
        }
        changed
    }

    pub fn add_intrinsic_keywords<'a>(
        &mut self,
        keywords: impl IntoIterator<Item = &'a str>,
    ) -> bool {
        let changed = crate::card::card_state::add_intrinsic_keywords(self, keywords);
        if changed {
            crate::card::card_state::update_keywords_cache(self);
        }
        changed
    }

    pub fn remove_intrinsic_keyword(&mut self, keyword: &str) -> bool {
        let changed = crate::card::card_state::remove_intrinsic_keyword(self, keyword);
        if changed {
            crate::card::card_state::update_keywords_cache(self);
        }
        changed
    }

    pub fn has_spell_ability(&self, sa: &SpellAbility) -> bool {
        crate::card::card_state::has_spell_ability(self, sa)
    }

    pub fn add_spell_ability(&mut self, sa: &SpellAbility) -> bool {
        let added = crate::card::card_state::add_spell_ability(self, sa);
        self.refresh_action_specs();
        added
    }

    pub fn has_trigger(&self, trigger_id: u32) -> bool {
        crate::card::card_state::has_trigger(self, trigger_id)
    }

    pub fn add_trigger(&mut self, trig: Trigger) -> bool {
        crate::card::card_state::add_trigger(self, trig)
    }

    pub fn clear_pump_triggers(&mut self) {
        let count = self.pump_trigger_count;
        if count == 0 {
            return;
        }
        let new_len = self
            .triggers
            .len()
            .saturating_sub(count)
            .max(self.base_trigger_count);
        self.triggers.truncate(new_len);
        self.pump_trigger_count = 0;
    }

    pub fn copiable_triggers(&self) -> Vec<Trigger> {
        self.trait_base_triggers
            .clone()
            .unwrap_or_else(|| self.triggers.clone())
    }

    pub fn copiable_replacement_effects(&self) -> Vec<ReplacementEffect> {
        self.trait_base_replacement_effects
            .clone()
            .unwrap_or_else(|| self.replacement_effects.clone())
    }

    pub fn add_static_ability(&mut self, st_ab: StaticAbility) -> bool {
        crate::card::card_state::add_static_ability(self, st_ab)
    }

    pub fn remove_static_ability(&mut self, mode: crate::staticability::StaticMode) -> bool {
        crate::card::card_state::remove_static_ability(self, mode)
    }

    pub fn add_replacement_effect(&mut self, re: ReplacementEffect) -> bool {
        crate::card::card_state::add_replacement_effect(self, re)
    }

    pub fn has_replacement_effect(&self) -> bool {
        crate::card::card_state::has_replacement_effect(self)
    }

    pub fn has_s_var(&self, key: &str) -> bool {
        crate::card::card_state::has_s_var(self, key)
    }

    pub fn get_s_var(&self, key: &str) -> Option<&str> {
        self.svars
            .get(key)
            .or_else(|| self.granted_svars.get(key))
            .map(String::as_str)
    }

    pub fn parsed_s_var(&mut self, key: &str) -> Option<ParsedSVar> {
        let raw = self.get_s_var(key)?.to_string();
        Some(self.parsed_svar_cache.get_or_parse(key, &raw).clone())
    }

    pub fn remove_s_var(&mut self, key: &str) {
        crate::card::card_state::remove_s_var(self, key);
        self.parsed_svar_cache.remove(key);
        self.refresh_action_specs_after_svar_change();
    }

    pub fn set_s_var(&mut self, key: impl Into<String>, value: impl Into<String>) {
        let key = key.into();
        self.parsed_svar_cache.remove(&key);
        self.svars.insert(key, value.into());
        self.refresh_action_specs_after_svar_change();
    }

    pub fn set_s_var_if_absent(&mut self, key: impl Into<String>, value: impl Into<String>) {
        let key = key.into();
        if self.svars.contains_key(&key) {
            return;
        }
        self.svars.insert(key, value.into());
        self.refresh_action_specs_after_svar_change();
    }

    pub fn set_svars_map(&mut self, svars: BTreeMap<String, String>) {
        self.svars = svars;
        self.parsed_svar_cache.clear();
        self.refresh_action_specs();
    }

    pub fn copy_from_card_state(&mut self, source: &Card) {
        crate::card::card_state::copy_from(source, self);
    }

    pub fn copy_from(&mut self, source: &Card) {
        self.copy_from_card_state(source);
    }

    pub fn add_abilities_from(&mut self, source: &Card) {
        crate::card::card_state::add_abilities_from(source, self);
    }

    pub fn has_property(&self, property: &str) -> bool {
        crate::card::card_state::has_property(self, property)
    }

    pub fn reset_original_host(&mut self) {
        crate::card::card_state::reset_original_host(self);
    }

    pub fn update_changed_text(&mut self) {
        crate::card::card_state::update_changed_text(self);
    }

    pub fn update_keywords_cache(&mut self) {
        crate::card::card_state::update_keywords_cache(self);
    }

    pub fn change_text_intrinsic(&mut self) {
        crate::card::card_state::change_text_intrinsic(self);
    }

    pub fn has_chapter(&self) -> bool {
        crate::card::card_state::has_chapter(self)
    }

    pub fn get_final_chapter_nr(&self) -> i32 {
        crate::card::card_state::get_final_chapter_nr(self)
    }

    /// Check whether this card has a keyword — intrinsically, granted by a
    /// continuous static effect (Layer 6), or temporarily from a pump effect.
    /// Count distinct colors of mana spent to cast this spell (for Sunburst/Converge).
    pub fn sunburst_count(&self) -> i32 {
        use forge_foundation::mana::ManaAtom;
        let mut count = 0;
        for &bit in &[
            ManaAtom::WHITE,
            ManaAtom::BLUE,
            ManaAtom::BLACK,
            ManaAtom::RED,
            ManaAtom::GREEN,
        ] {
            if (self.colors_spent_to_cast & bit) != 0 {
                count += 1;
            }
        }
        count
    }

    pub fn has_keyword(&self, kw: &str) -> bool {
        crate::card::card_state::has_keyword(self, kw)
    }

    /// Check for a keyword using the typed Keyword enum.
    /// Checks the structured `keyword_collection` first (O(1) HashMap lookup),
    /// then falls back to string matching on granted/pump keywords.
    /// Mirrors Java's `Card.hasKeyword(Keyword)`.
    pub fn has_keyword_enum(&self, kw: Kw) -> bool {
        if self
            .cant_have_keywords
            .contains(&kw.display_name().to_ascii_lowercase())
        {
            return false;
        }
        self.keywords.contains_keyword(kw)
            || self.granted_keywords.contains_keyword(kw)
            || self.pump_keywords.contains_keyword(kw)
    }

    pub fn has_haste(&self) -> bool {
        self.has_keyword_enum(crate::keyword::keyword_instance::Keyword::Haste)
    }

    pub fn has_flying(&self) -> bool {
        self.has_keyword_enum(crate::keyword::keyword_instance::Keyword::Flying)
    }

    pub fn has_reach(&self) -> bool {
        self.has_keyword_enum(Kw::Reach)
    }

    pub fn has_first_strike(&self) -> bool {
        self.has_keyword_enum(Kw::FirstStrike)
    }

    pub fn has_double_strike(&self) -> bool {
        self.has_keyword_enum(Kw::DoubleStrike)
    }

    pub fn has_trample(&self) -> bool {
        self.has_keyword_enum(Kw::Trample)
    }

    pub fn has_deathtouch(&self) -> bool {
        self.has_keyword_enum(Kw::Deathtouch)
    }

    pub fn has_lifelink(&self) -> bool {
        self.has_keyword_enum(Kw::Lifelink)
    }

    pub fn has_vigilance(&self) -> bool {
        self.has_keyword_enum(Kw::Vigilance)
    }

    pub fn has_defender(&self) -> bool {
        self.has_keyword_enum(Kw::Defender)
    }

    pub fn has_hexproof(&self) -> bool {
        self.has_keyword_enum(Kw::Hexproof)
    }

    pub fn has_shroud(&self) -> bool {
        self.has_keyword_enum(Kw::Shroud)
    }

    pub fn has_menace(&self) -> bool {
        self.has_keyword_enum(Kw::Menace)
    }

    pub fn has_fear(&self) -> bool {
        self.has_keyword_enum(Kw::Fear)
    }

    pub fn has_intimidate(&self) -> bool {
        self.has_keyword_enum(Kw::Intimidate)
    }

    pub fn has_shadow(&self) -> bool {
        self.has_keyword_enum(Kw::Shadow)
    }

    pub fn has_skulk(&self) -> bool {
        self.has_keyword_enum(Kw::Skulk)
    }

    pub fn has_horsemanship(&self) -> bool {
        self.has_keyword_enum(Kw::Horsemanship)
    }

    pub fn has_indestructible(&self) -> bool {
        self.has_keyword_enum(Kw::Indestructible)
    }

    pub fn has_infect(&self) -> bool {
        self.has_keyword_enum(Kw::Infect)
    }

    pub fn has_wither(&self) -> bool {
        self.has_keyword_enum(Kw::Wither)
    }

    pub fn has_prowess(&self) -> bool {
        self.has_keyword_enum(Kw::Prowess)
    }

    pub fn has_rebound(&self) -> bool {
        self.has_keyword_enum(Kw::Rebound)
    }

    /// Check "Hexproof from <color>" variants (e.g. "Hexproof from blue").
    pub fn has_hexproof_from(&self, color: &str) -> bool {
        let target = format!("Hexproof from {}", color);
        self.keywords.contains_string_ignore_case(&target)
            || self.granted_keywords.contains_string_ignore_case(&target)
    }

    /// Get Toxic count (e.g. "Toxic:1" → Some(1)).
    pub fn get_toxic_count(&self) -> Option<i32> {
        self.get_keyword_cost("Toxic").and_then(|s| s.parse().ok())
    }

    /// Whether this card has the Storm keyword.
    pub fn has_storm(&self) -> bool {
        self.has_keyword_enum(Kw::Storm)
    }

    pub fn has_cascade(&self) -> bool {
        self.has_keyword_enum(Kw::Cascade)
    }

    /// Converted mana cost (mana value).
    pub fn mana_value(&self) -> i32 {
        self.mana_cost.cmc()
    }

    /// Check "Protection from <quality>" (e.g. "Protection from red").
    pub fn has_protection_from(&self, quality: &str) -> bool {
        let target = format!("Protection from {}", quality);
        self.keywords.contains_string_ignore_case(&target)
            || self.granted_keywords.contains_string_ignore_case(&target)
    }

    /// Get all "Protection from X" values this card has.
    pub fn get_protections(&self) -> Vec<String> {
        let mut prots = Vec::new();
        for kw in self
            .keywords
            .iter_strings()
            .chain(self.granted_keywords.iter_strings())
        {
            if let Some(from) = kw.strip_prefix("Protection from ") {
                prots.push(from.to_lowercase());
            }
        }
        prots
    }

    /// Check if this card is protected from a source card.
    /// Protection from <color> checks source's color.
    /// Protection from <type> checks source's type (e.g. "artifacts", "creatures").
    pub fn is_protected_from(&self, source: &Card) -> bool {
        for prot in self.get_protections() {
            match prot.as_str() {
                "white" => {
                    if source.color.has_white() {
                        return true;
                    }
                }
                "blue" => {
                    if source.color.has_blue() {
                        return true;
                    }
                }
                "black" => {
                    if source.color.has_black() {
                        return true;
                    }
                }
                "red" => {
                    if source.color.has_red() {
                        return true;
                    }
                }
                "green" => {
                    if source.color.has_green() {
                        return true;
                    }
                }
                "colorless" => {
                    if source.color.is_colorless() {
                        return true;
                    }
                }
                "artifacts" => {
                    if source.type_line.is_artifact() {
                        return true;
                    }
                }
                "creatures" => {
                    if source.type_line.is_creature() {
                        return true;
                    }
                }
                "enchantments" => {
                    if source.type_line.is_enchantment() {
                        return true;
                    }
                }
                _ => {}
            }
        }
        false
    }

    pub fn can_attack(&self) -> bool {
        self.is_creature()
            && !self.tapped
            && !self.has_defender()
            && !self.cant_attack_static
            && !self.detained
            && (self.has_haste() || !self.summoning_sick)
            && self.zone == ZoneType::Battlefield
    }

    pub fn can_block(&self) -> bool {
        self.is_creature()
            && !self.tapped
            && !self.cant_block_static
            && !self.detained
            && self.zone == ZoneType::Battlefield
    }

    /// Check if this card can be controlled by the given player
    /// (e.g., checks for "Other players can't gain control of CARDNAME.")
    pub fn can_be_controlled_by(&self, player: PlayerId) -> bool {
        if player == self.controller {
            return true;
        }
        !self.has_keyword("Other players can't gain control of CARDNAME.")
    }

    pub fn counter_count(&self, ct: &CounterType) -> i32 {
        *self.counters.get(ct).unwrap_or(&0)
    }

    pub fn add_counter(&mut self, ct: &CounterType, count: i32) {
        let entry = self.counters.entry(ct.clone()).or_insert(0);
        *entry += count;
    }

    pub fn remove_counter(&mut self, ct: &CounterType, count: i32) {
        let entry = self.counters.entry(ct.clone()).or_insert(0);
        *entry = (*entry - count).max(0);
    }

    /// Reset state when entering the battlefield.
    pub fn enter_battlefield(&mut self) {
        self.tapped = false;
        self.damage = 0;
        self.summoning_sick = true;
        self.came_under_control_since_last_upkeep = true;
        self.has_deathtouch_damage = false;
        self.entered_battlefield_this_turn = true;
        self.attacked_this_turn = false;
        self.damage_sources_this_turn.clear();
    }

    /// Reset per-turn state at start of turn.
    pub fn clear_global_turn_state(&mut self) {
        self.entered_battlefield_this_turn = false;
        self.attacked_this_turn = false;
        self.attached_this_turn = false;
        self.has_deathtouch_damage = false;
        self.damage_sources_this_turn.clear();
        self.total_damage_done_this_turn = 0;
    }

    /// Reset controller-specific state at the start of that player's turn.
    pub fn new_turn(&mut self) {
        self.clear_global_turn_state();
        if self.zone == ZoneType::Battlefield {
            if let Ok(filter) = std::env::var("FORGE_CARD_TRACE") {
                if !filter.is_empty()
                    && self.card_name.eq_ignore_ascii_case(&filter)
                    && self.summoning_sick
                {
                    eprintln!(
                        "[card-trace] new_turn clears sickness on {}#{:?} (controller={:?})",
                        self.card_name, self.id, self.controller,
                    );
                }
            }
            self.summoning_sick = false;
        }
    }

    /// Add a remembered card (for RememberCountered, etc.)
    pub fn add_remembered_card(&mut self, card_id: CardId) {
        if !self.remembered_cards.contains(&card_id) {
            self.remembered_cards.push(card_id);
        }
    }

    /// Add a remembered CMC value
    pub fn add_remembered_cmc(&mut self, cmc: i32) {
        self.remembered_cmc.push(cmc);
    }

    pub fn add_remembered_player(&mut self, player: PlayerId) {
        if !self.remembered_players.contains(&player) {
            self.remembered_players.push(player);
        }
    }

    pub fn add_remembered_players<I>(&mut self, players: I)
    where
        I: IntoIterator<Item = PlayerId>,
    {
        for p in players {
            self.add_remembered_player(p);
        }
    }

    pub fn has_remembered(&self) -> bool {
        !self.remembered_cards.is_empty()
            || !self.remembered_players.is_empty()
            || !self.remembered_cmc.is_empty()
    }

    pub fn add_remembered(&mut self, card_id: CardId) {
        self.add_remembered_card(card_id);
    }

    pub fn remove_remembered(&mut self, card_id: CardId) {
        self.remembered_cards.retain(|&c| c != card_id);
    }

    pub fn clear_remembered(&mut self) {
        self.remembered_cards.clear();
        self.remembered_players.clear();
        self.remembered_cmc.clear();
    }

    pub fn update_remembered(&mut self) {
        let mut seen = HashSet::new();
        self.remembered_cards.retain(|c| seen.insert(*c));
    }

    pub fn has_imprinted_card(&self) -> bool {
        !self.imprinted_cards.is_empty()
    }

    pub fn add_imprinted_card(&mut self, card_id: CardId) {
        if !self.imprinted_cards.contains(&card_id) {
            self.imprinted_cards.push(card_id);
        }
    }

    pub fn add_imprinted_cards(&mut self, cards: impl IntoIterator<Item = CardId>) {
        for c in cards {
            self.add_imprinted_card(c);
        }
    }

    pub fn remove_imprinted_card(&mut self, card_id: CardId) {
        self.imprinted_cards.retain(|&c| c != card_id);
    }

    pub fn remove_imprinted_cards(&mut self, cards: impl IntoIterator<Item = CardId>) {
        for c in cards {
            self.remove_imprinted_card(c);
        }
    }

    pub fn clear_imprinted_cards(&mut self) {
        self.imprinted_cards.clear();
    }

    pub fn add_to_chosen_map(&mut self, player: PlayerId, chosen: Vec<CardId>) {
        self.chosen_map.insert(player, chosen);
    }

    pub fn add_gain_control_target(&mut self, card_id: CardId) {
        if !self.gain_control_targets.contains(&card_id) {
            self.gain_control_targets.push(card_id);
        }
    }

    pub fn remove_gain_control_targets(&mut self, card_id: CardId) {
        self.gain_control_targets.retain(|&c| c != card_id);
    }

    pub fn has_gain_control_target(&self) -> bool {
        !self.gain_control_targets.is_empty()
    }

    pub fn add_until_leaves_battlefield(&mut self, card_id: CardId) {
        if !self.until_leaves_battlefield.contains(&card_id) {
            self.until_leaves_battlefield.push(card_id);
        }
    }

    pub fn remove_until_leaves_battlefield(&mut self, card_id: CardId) {
        self.until_leaves_battlefield.retain(|&c| c != card_id);
    }

    pub fn clear_until_leaves_battlefield(&mut self) {
        self.until_leaves_battlefield.clear();
    }

    pub fn has_exiled_card(&self) -> bool {
        !self.exiled_cards.is_empty()
    }

    pub fn add_exiled_card(&mut self, card_id: CardId) {
        if !self.exiled_cards.contains(&card_id) {
            self.exiled_cards.push(card_id);
        }
    }

    pub fn add_exiled_cards(&mut self, cards: impl IntoIterator<Item = CardId>) {
        for c in cards {
            self.add_exiled_card(c);
        }
    }

    pub fn remove_exiled_card(&mut self, card_id: CardId) {
        self.exiled_cards.retain(|&c| c != card_id);
    }

    pub fn remove_exiled_cards(&mut self, cards: impl IntoIterator<Item = CardId>) {
        for c in cards {
            self.remove_exiled_card(c);
        }
    }

    pub fn clear_exiled_cards(&mut self) {
        self.exiled_cards.clear();
    }

    pub fn add_haunted_by(&mut self, card_id: CardId) {
        if !self.haunted_by.contains(&card_id) {
            self.haunted_by.push(card_id);
        }
    }

    pub fn remove_haunted_by(&mut self, card_id: CardId) {
        self.haunted_by.retain(|&c| c != card_id);
    }

    pub fn has_encoded_card(&self) -> bool {
        !self.encoded_cards.is_empty()
    }

    pub fn add_encoded_card(&mut self, card_id: CardId) {
        if !self.encoded_cards.contains(&card_id) {
            self.encoded_cards.push(card_id);
        }
    }

    pub fn add_encoded_cards(&mut self, cards: impl IntoIterator<Item = CardId>) {
        for c in cards {
            self.add_encoded_card(c);
        }
    }

    pub fn remove_encoded_card(&mut self, card_id: CardId) {
        self.encoded_cards.retain(|&c| c != card_id);
    }

    pub fn clear_encoded_cards(&mut self) {
        self.encoded_cards.clear();
    }

    pub fn has_merged_card(&self) -> bool {
        !self.melded_with.is_empty()
    }

    pub fn add_merged_card(&mut self, card_id: CardId) {
        if !self.melded_with.contains(&card_id) {
            self.melded_with.push(card_id);
        }
    }

    pub fn add_merged_card_to_top(&mut self, card_id: CardId) {
        if !self.melded_with.contains(&card_id) {
            self.melded_with.insert(0, card_id);
        }
    }

    pub fn remove_merged_card(&mut self, card_id: CardId) {
        self.melded_with.retain(|&c| c != card_id);
    }

    pub fn clear_merged_cards(&mut self) {
        self.melded_with.clear();
    }

    pub fn remove_mutated_states(&mut self) {
        self.clear_merged_cards();
    }

    pub fn rebuild_mutated_states(&mut self) {
        let mut seen = HashSet::new();
        self.melded_with.retain(|c| seen.insert(*c));
    }

    pub fn move_merged_to_subgame(&mut self) {
        self.clear_merged_cards();
    }

    pub fn entered_this_turn(&self) -> bool {
        self.entered_battlefield_this_turn
    }

    pub fn entered_current_zone_this_turn(&self, turn_number: u32) -> bool {
        self.turn_in_zone == turn_number
    }

    pub fn calculate_perpetual_adjusted_mana_cost(&mut self) {
        crate::card::card_state::calculate_perpetual_adjusted_mana_cost(self);
    }

    pub fn has_chosen_player(&self) -> bool {
        self.chosen_player.is_some()
    }

    pub fn reveal_chosen_player(&mut self) {
        self.chosen_player_revealed = true;
    }

    pub fn has_promised_gift(&self) -> bool {
        self.promised_gift.is_some()
    }

    pub fn has_chosen_number(&self) -> bool {
        self.chosen_number.is_some()
    }

    pub fn clear_chosen_number(&mut self) {
        self.chosen_number = None;
    }

    pub fn has_chosen_type(&self) -> bool {
        self.chosen_type
            .as_ref()
            .map(|s| !s.is_empty())
            .unwrap_or(false)
    }

    pub fn reveal_chosen_type(&mut self) {
        self.chosen_type_revealed = true;
    }

    pub fn has_chosen_type2(&self) -> bool {
        self.chosen_type2
            .as_ref()
            .map(|s| !s.is_empty())
            .unwrap_or(false)
    }

    pub fn has_any_noted_type(&self) -> bool {
        !self.noted_types.is_empty()
    }

    pub fn add_noted_type(&mut self, ty: &str) {
        self.noted_types.push(ty.to_string());
    }

    pub fn has_chosen_color(&self) -> bool {
        !self.chosen_colors.is_empty()
    }

    pub fn has_chosen_card(&self) -> bool {
        !self.chosen_cards.is_empty()
    }

    pub fn assign_sector(&mut self, sector: &str) {
        self.sector = Some(sector.to_string());
    }

    pub fn has_attraction_light(&self, light: i32) -> bool {
        light > 0 && self.attraction_lights.contains(&(light as u32))
    }

    pub fn has_sector(&self) -> bool {
        self.sector.is_some()
    }

    pub fn handle_changed_controller_sprocket_reset(&mut self) {
        if self.sprocket != 0 {
            self.sprocket = -1;
        }
    }

    pub fn add_named_card(&mut self, name: &str) {
        self.named_cards.push(name.to_string());
    }

    pub fn has_named_card(&self) -> bool {
        !self.named_cards.is_empty()
    }

    pub fn has_chosen_even_odd(&self) -> bool {
        self.chosen_even_odd.is_some()
    }

    pub fn has_no_abilities(&self) -> bool {
        self.abilities.is_empty()
            && self.activated_abilities.is_empty()
            && self.triggers.is_empty()
            && self.static_abilities.is_empty()
            && self.replacement_effects.is_empty()
    }

    pub fn can_tap(&self) -> bool {
        !self.tapped
    }

    pub fn tap(&mut self) -> bool {
        if !self.can_tap() {
            return false;
        }
        self.tapped = true;
        true
    }

    pub fn set_tapped(&mut self, tapped: bool) {
        if tapped {
            self.tap();
        } else {
            self.untap();
        }
    }

    pub fn set_owner(&mut self, owner: PlayerId) {
        self.owner = owner;
    }

    pub fn set_controller(&mut self, controller: PlayerId) {
        if self.controller != controller {
            self.came_under_control_since_last_upkeep = true;
        }
        self.controller = controller;
    }

    pub fn set_is_token(&mut self, is_token: bool) {
        self.is_token = is_token;
    }

    pub fn set_effect_source(&mut self, source: Option<CardId>) {
        self.effect_source = source;
    }

    pub fn set_temp_effect_host(&mut self, host: Option<CardId>) {
        self.temp_effect_host = host;
    }

    pub fn set_temp_effect_until_eot(&mut self, until_eot: bool) {
        self.temp_effect_until_eot = until_eot;
    }

    pub fn set_forget_on_moved_origin(&mut self, zone: Option<ZoneType>) {
        self.forget_on_moved_origin = zone;
    }

    pub fn set_exile_when_no_remembered(&mut self, exile: bool) {
        self.exile_when_no_remembered = exile;
    }

    pub fn set_flipped(&mut self, flipped: bool) {
        self.flipped = flipped;
    }

    pub fn can_untap(&self) -> bool {
        self.tapped
    }

    pub fn untap(&mut self) -> bool {
        if !self.can_untap() {
            return false;
        }
        self.tapped = false;
        true
    }

    pub fn exert(&mut self) {
        self.exerted = true;
    }

    pub fn clear_exerted(&mut self) {
        self.exerted = false;
    }

    pub fn remove_exerted_by(&mut self, _player: PlayerId) {
        self.exerted = false;
    }

    pub fn detain(&mut self) {
        self.detained = true;
    }

    pub fn add_goad(&mut self, player: PlayerId) {
        self.goaded_by = Some(player);
    }

    pub fn remove_goad(&mut self, player: PlayerId) {
        if self.goaded_by == Some(player) {
            self.goaded_by = None;
        }
    }

    pub fn un_goad(&mut self) {
        self.goaded_by = None;
    }

    pub fn remove_detained_by(&mut self, _player: PlayerId) {
        self.detained = false;
    }

    pub fn update_ability_text_for_view(&mut self) {
        self.update_spell_abilities();
    }
    pub fn update_non_ability_text_for_view(&mut self) {
        self.update_changed_text();
    }
    pub fn update_mana_cost_for_view(&mut self) {
        let _ = self.mana_value();
    }
    pub fn update_p_tfor_view(&mut self) {
        let _ = (self.power(), self.toughness());
    }
    pub fn update_color_for_view(&mut self) {
        let _ = self.color;
    }
    pub fn update_attacking_for_view(&mut self) {
        let _ = self.attacking_player;
    }
    pub fn update_blocking_for_view(&mut self) {
        let _ = self.must_block;
    }
    pub fn update_state_for_view(&mut self) {
        let _ = (self.zone, self.tapped, self.face_down);
    }
    pub fn update_namefor_view(&mut self) {
        self.card_name = self.card_name.trim().to_string();
    }
    pub fn update_token_view(&mut self) {
        let _ = self.is_token;
    }
    pub fn update_was_destroyed(&mut self) {
        let _ = self.damage >= self.toughness();
    }
    pub fn update_rules_view(&mut self) {
        let _ = (&self.abilities, &self.keywords);
    }
    pub fn update_commander_view(&mut self) {
        let _ = self.is_commander;
    }
    pub fn update_card(&mut self) {
        self.update_namefor_view();
        self.update_types_for_view();
        self.update_color_for_view();
        self.update_mana_cost_for_view();
        self.update_p_tfor_view();
        self.update_rules_view();
        self.update_state_for_view();
    }
    pub fn dangerously_set_game(&mut self) {
        self.update_card();
    }
    pub fn visit(&mut self) {
        self.update_card();
    }

    pub fn has_state(&self) -> bool {
        self.is_transformed || self.other_part.is_some()
    }

    /// Whether this card is double-faced (has a back side).
    /// Mirrors Java `Card.isDoubleFaced()`.
    pub fn is_double_faced(&self) -> bool {
        self.other_part.is_some()
    }

    pub fn change_to_state(&mut self) {
        self.transform();
    }

    pub fn add_alternate_state(&mut self, other: CardOtherPart) {
        self.other_part = Some(other);
    }

    pub fn clear_states(&mut self) {
        self.other_part = None;
        self.is_transformed = false;
    }

    pub fn change_card_state(&mut self) {
        self.transform();
    }

    pub fn has_alternate_state(&self) -> bool {
        self.other_part.is_some()
    }

    pub fn manifest(&mut self) {
        self.manifested = true;
        self.turn_face_down();
    }

    pub fn cloak(&mut self) {
        self.cloaked = true;
        self.turn_face_down();
    }

    pub fn turn_face_down(&mut self) {
        self.face_down = true;
    }

    pub fn turn_face_down_no_update(&mut self) {
        self.face_down = true;
    }

    pub fn can_be_turned_face_up(&self) -> bool {
        self.face_down
    }

    pub fn force_turn_face_up(&mut self) {
        self.face_down = false;
    }

    pub fn turn_face_up(&mut self) {
        self.face_down = false;
    }

    pub fn set_face_down(&mut self, face_down: bool) {
        if face_down {
            self.turn_face_down();
        } else {
            self.turn_face_up();
        }
    }

    pub fn set_manifested(&mut self, manifested: bool) {
        self.manifested = manifested;
    }

    pub fn set_cloaked(&mut self, cloaked: bool) {
        self.cloaked = cloaked;
    }

    pub fn set_discarded(&mut self, discarded: bool) {
        self.discarded = discarded;
    }

    pub fn set_unearthed(&mut self, unearthed: bool) {
        self.unearthed = unearthed;
    }

    pub fn set_summoning_sick(&mut self, summoning_sick: bool) {
        self.summoning_sick = summoning_sick;
    }

    pub fn set_foretold(&mut self, foretold: bool) {
        self.foretold = foretold;
    }

    pub fn set_foretold_cost_by_effect(&mut self, by_effect: bool) {
        self.foretold_cost_by_effect = by_effect;
    }

    pub fn set_transformed(&mut self, transformed: bool) {
        self.is_transformed = transformed;
    }

    pub fn set_attacking_player(&mut self, player: PlayerId) {
        self.attacking_player = Some(player);
    }

    pub fn clear_attacking_player(&mut self) {
        self.attacking_player = None;
    }

    pub fn mark_attacked_this_turn(&mut self) {
        self.attacked_this_turn = true;
    }

    pub fn add_etb_counters_p1p1(&mut self, amount: i32) {
        self.etb_counters_p1p1 += amount;
    }

    pub fn increment_commander_cast_count(&mut self) {
        self.commander_cast_count += 1;
    }

    pub fn set_kicked(&mut self, kicked: bool) {
        self.kicked = kicked;
    }

    pub fn mark_enlisted_this_combat(&mut self) {
        self.enlisted_this_combat = true;
    }

    pub fn add_enlisted_power(&mut self, amount: i32) {
        self.power_modifier += amount;
    }

    pub fn add_damage_source_this_turn(&mut self, source: CardId) {
        self.damage_sources_this_turn.push(source);
    }

    pub fn mark_deathtouch_damage(&mut self) {
        self.has_deathtouch_damage = true;
    }

    pub fn clear_deathtouch_damage(&mut self) {
        self.has_deathtouch_damage = false;
    }

    pub fn clear_damage(&mut self) {
        self.damage = 0;
    }

    pub fn reset_turn_modifiers(&mut self) {
        self.power_modifier = 0;
        self.toughness_modifier = 0;
    }

    pub fn reset_regeneration_shields(&mut self) {
        self.regeneration_shields = 0;
    }

    pub fn clear_original_controller_eot(&mut self) {
        self.original_controller_eot = None;
    }

    pub fn set_chosen_modes(&mut self, modes: Vec<usize>) {
        self.chosen_modes = Some(modes);
    }

    pub fn set_chosen_cards(&mut self, cards: Vec<CardId>) {
        self.chosen_cards = cards;
    }

    pub fn set_chosen_number(&mut self, number: Option<i32>) {
        self.chosen_number = number;
    }

    pub fn set_chosen_player(
        &mut self,
        player: Option<PlayerId>,
        chooser: Option<PlayerId>,
        revealed: bool,
    ) {
        self.chosen_player = player;
        self.chosen_player_controller = chooser;
        self.chosen_player_revealed = revealed;
    }

    pub fn set_chosen_type(
        &mut self,
        chosen_type: Option<String>,
        chooser: Option<PlayerId>,
        revealed: bool,
    ) {
        self.chosen_type = chosen_type;
        self.chosen_type_controller = chooser;
        self.chosen_type_revealed = revealed;
    }

    pub fn set_strive_extra_targets(&mut self, value: u32) {
        self.strive_extra_targets = value;
    }

    pub fn set_colors_spent_to_cast(&mut self, colors: u16) {
        self.colors_spent_to_cast = colors;
    }

    pub fn set_paying_mana_to_cast(&mut self, paying_mana: Vec<u16>) {
        self.paying_mana_to_cast = paying_mana;
    }

    pub fn set_promised_gift(&mut self, player: Option<PlayerId>) {
        self.promised_gift = player;
    }

    pub fn set_lki_power_toughness(&mut self, power: Option<i32>, toughness: Option<i32>) {
        self.lki_power = power;
        self.lki_toughness = toughness;
    }

    pub fn restore_animate_snapshot(
        &mut self,
        type_line: CardTypeLine,
        base_power: Option<i32>,
        base_toughness: Option<i32>,
        color: ColorSet,
    ) {
        self.set_type_line(type_line);
        self.base_power = base_power;
        self.base_toughness = base_toughness;
        self.color = color;
    }

    pub fn capture_clone_state(&self) -> CloneState {
        CloneState {
            expires_at_cleanup: false,
            original_card_name: self.card_name.clone(),
            original_oracle_text: self.oracle_text.clone(),
            original_type_line: self.type_line.clone(),
            original_mana_cost: self.mana_cost.clone(),
            original_color: self.color,
            original_base_power: self.base_power,
            original_base_toughness: self.base_toughness,
            original_keywords: self.keywords.clone(),
            original_abilities: self.abilities.clone(),
            original_activated_abilities: self.activated_abilities.clone(),
            original_triggers: self.triggers.clone(),
            original_svars: self.svars.clone(),
            original_static_abilities: self.static_abilities.clone(),
            original_replacement_effects: self.replacement_effects.clone(),
            original_base_ability_count: self.base_ability_count,
            original_base_trigger_count: self.base_trigger_count,
        }
    }

    pub fn restore_clone_snapshot(&mut self, state: CloneState) {
        self.card_name = state.original_card_name;
        self.oracle_text = state.original_oracle_text;
        self.type_line = state.original_type_line;
        self.mana_cost = state.original_mana_cost;
        self.color = state.original_color;
        self.base_power = state.original_base_power;
        self.base_toughness = state.original_base_toughness;
        self.keywords = state.original_keywords;
        self.abilities = state.original_abilities;
        self.activated_abilities = state.original_activated_abilities;
        self.triggers = state.original_triggers;
        self.svars = state.original_svars;
        self.static_abilities = state.original_static_abilities;
        self.replacement_effects = state.original_replacement_effects;
        self.base_ability_count = state.original_base_ability_count;
        self.base_trigger_count = state.original_base_trigger_count;
        self.parsed_svar_cache.clear();
        self.refresh_action_specs();
        self.ensure_crew_activated_ability();
        self.remove_clone_state();
    }

    pub fn set_counters_map(&mut self, counters: BTreeMap<CounterType, i32>) {
        self.counters = counters;
    }

    pub fn set_zone(&mut self, zone: ZoneType) {
        self.zone = zone;
    }

    pub fn set_color(&mut self, color: ColorSet) {
        self.color = color;
    }

    pub fn set_animate_state(&mut self, state: Option<AnimateState>) {
        self.animate_state = state;
    }

    pub fn set_clone_state(&mut self, state: Option<CloneState>) {
        self.clone_state = state;
    }

    pub fn set_exiled_by(&mut self, source: Option<CardId>) {
        self.exiled_by = source;
    }

    pub fn set_attached_to(&mut self, target: Option<CardId>) {
        self.attached_to = target;
    }

    pub fn set_original_controller_eot(&mut self, controller: Option<PlayerId>) {
        self.original_controller_eot = controller;
    }

    pub fn set_class_level(&mut self, level: i32) {
        self.class_level = level;
    }

    pub fn set_paired_with(&mut self, pair: Option<CardId>) {
        self.paired_with = pair;
    }

    pub fn set_must_block(&mut self, must_block: bool) {
        self.must_block = must_block;
    }

    pub fn set_detained(&mut self, detained: bool) {
        self.detained = detained;
    }

    pub fn set_goaded_by(&mut self, player: Option<PlayerId>) {
        self.goaded_by = player;
    }

    pub fn set_phased_out(&mut self, phased_out: bool) {
        self.phased_out = phased_out;
    }

    pub fn set_base_power(&mut self, power: Option<i32>) {
        self.base_power = power;
    }

    pub fn set_base_toughness(&mut self, toughness: Option<i32>) {
        self.base_toughness = toughness;
    }

    pub fn set_base_pt(&mut self, power: Option<i32>, toughness: Option<i32>) {
        self.base_power = power;
        self.base_toughness = toughness;
    }

    pub fn capture_changed_characteristics_baseline_if_needed(&mut self) {
        if self.changed_type_line_base.is_none() {
            self.changed_type_line_base = Some(self.type_line.clone());
        }
        if self.changed_base_power.is_none() {
            self.changed_base_power = Some(self.base_power);
        }
        if self.changed_base_toughness.is_none() {
            self.changed_base_toughness = Some(self.base_toughness);
        }
    }

    pub fn restore_changed_characteristics_baseline(&mut self) {
        if let Some(type_line) = self.changed_type_line_base.take() {
            self.set_type_line(type_line);
        }
        if let Some(power) = self.changed_base_power.take() {
            self.base_power = power;
        }
        if let Some(toughness) = self.changed_base_toughness.take() {
            self.base_toughness = toughness;
        }
    }

    pub fn set_static_set_pt(&mut self, power: Option<i32>, toughness: Option<i32>) {
        self.static_set_power = power;
        self.static_set_toughness = toughness;
    }

    pub fn set_power_modifier(&mut self, amount: i32) {
        self.power_modifier = amount;
    }

    pub fn set_toughness_modifier(&mut self, amount: i32) {
        self.toughness_modifier = amount;
    }

    pub fn set_card_name(&mut self, name: impl Into<String>) {
        self.card_name = name.into();
    }

    pub fn set_mana_cost(&mut self, mana_cost: ManaCost) {
        self.mana_cost = mana_cost;
    }

    pub fn set_abilities(&mut self, abilities: Vec<String>) {
        self.abilities = abilities;
        self.update_spell_abilities();
        self.refresh_action_specs();
    }

    pub fn set_static_abilities(&mut self, abilities: Vec<StaticAbility>) {
        self.static_abilities = abilities;
    }

    pub fn set_triggers(&mut self, triggers: Vec<Trigger>) {
        self.triggers = triggers;
        self.base_trigger_count = self.triggers.len();
    }

    pub fn set_replacement_effects(&mut self, effects: Vec<ReplacementEffect>) {
        self.replacement_effects = effects;
    }

    pub fn set_renowned(&mut self, renowned: bool) {
        self.is_renowned = renowned;
    }

    pub fn set_monstrous(&mut self, monstrous: bool) {
        self.monstrous = monstrous;
    }

    pub fn clear_granted_keywords(&mut self) {
        self.granted_keywords.clear();
    }

    pub fn clear_pump_keywords(&mut self) {
        self.pump_keywords.clear();
    }

    pub fn increment_pump_trigger_count(&mut self) {
        self.pump_trigger_count += 1;
    }

    pub fn add_remembered_cards<I>(&mut self, cards: I)
    where
        I: IntoIterator<Item = CardId>,
    {
        for card in cards {
            self.add_remembered_card(card);
        }
    }

    pub fn clear_chosen_colors(&mut self) {
        self.chosen_colors.clear();
    }

    pub fn add_chosen_color(&mut self, color: impl Into<String>) {
        self.chosen_colors.push(color.into());
    }

    pub fn add_chosen_card(&mut self, card: CardId) {
        if !self.chosen_cards.contains(&card) {
            self.chosen_cards.push(card);
        }
    }

    pub fn add_pump_keyword(&mut self, keyword: &str) {
        self.pump_keywords.add(keyword);
    }

    pub fn add_granted_keyword(&mut self, keyword: &str) {
        self.granted_keywords.add(keyword);
    }

    pub fn was_turned_face_up_this_turn(&self) -> bool {
        !self.face_down && (self.manifested || self.cloaked)
    }

    pub fn can_transform(&self) -> bool {
        self.other_part.is_some()
    }

    pub fn has_name_overwrite(&self) -> bool {
        false
    }

    pub fn has_non_legendary_creature_names(&self) -> bool {
        false
    }

    pub fn add_changed_name(&mut self, name: &str) {
        if !self.has_s_var("OriginalName") {
            self.set_s_var("OriginalName", self.card_name.clone());
        }
        self.card_name = name.to_string();
    }

    pub fn remove_changed_name(&mut self) {
        if let Some(orig) = self.svars.get("OriginalName").cloned() {
            self.card_name = orig;
        }
    }

    pub fn clear_changed_name(&mut self) {
        self.remove_s_var("OriginalName");
    }

    pub fn add_devoured(&mut self, card_id: CardId) {
        self.add_remembered_card(card_id);
        self.set_s_var("Devoured", "True");
    }
    pub fn add_exploited(&mut self, card_id: CardId) {
        self.add_remembered_card(card_id);
        self.set_s_var("Exploited", "True");
    }
    pub fn add_delved(&mut self, card_id: CardId) {
        self.add_remembered_card(card_id);
        self.set_s_var("Delved", "True");
    }
    pub fn clear_delved(&mut self) {
        self.remove_s_var("Delved");
    }
    pub fn retain_paid_list(&mut self) {
        self.remembered_cards.retain(|_| true);
    }
    pub fn add_stored_rolls(&mut self, roll: i32) {
        self.add_remembered_cmc(roll);
    }
    pub fn replace_stored_roll(&mut self, from: i32, to: i32) {
        for roll in &mut self.remembered_cmc {
            if *roll == from {
                *roll = to;
            }
        }
    }
    pub fn add_flip_result(&mut self, heads: bool) {
        self.set_s_var("FlipResult", if heads { "Heads" } else { "Tails" });
    }
    pub fn clear_flip_result(&mut self) {
        self.remove_s_var("FlipResult");
    }
    pub fn add_blocked_this_turn(&mut self, card_id: CardId) {
        self.add_remembered_card(card_id);
        self.set_s_var("BlockedThisTurn", "True");
    }
    pub fn clear_blocked_this_turn(&mut self) {
        self.remove_s_var("BlockedThisTurn");
    }
    pub fn add_blocked_by_this_turn(&mut self, card_id: CardId) {
        self.add_remembered_card(card_id);
        self.set_s_var("BlockedByThisTurn", "True");
    }
    pub fn clear_blocked_by_this_turn(&mut self) {
        self.remove_s_var("BlockedByThisTurn");
    }

    pub fn add_must_block_card(&mut self, card_id: CardId) {
        if !self.must_block_cards.contains(&card_id) {
            self.must_block_cards.push(card_id);
        }
    }

    pub fn add_must_block_cards(&mut self, cards: impl IntoIterator<Item = CardId>) {
        for c in cards {
            self.add_must_block_card(c);
        }
    }

    pub fn remove_must_block_cards(&mut self, cards: impl IntoIterator<Item = CardId>) {
        let remove: HashSet<CardId> = cards.into_iter().collect();
        self.must_block_cards.retain(|c| !remove.contains(c));
    }

    pub fn clear_must_block_cards(&mut self) {
        self.must_block_cards.clear();
    }

    pub fn has_second_strike(&self) -> bool {
        self.has_double_strike()
    }

    pub fn has_suspend(&self) -> bool {
        self.has_keyword("Suspend")
    }

    pub fn has_converge(&self) -> bool {
        self.has_keyword("Converge")
    }

    pub fn can_receive_counters(&self, _counter: &CounterType) -> bool {
        true
    }

    pub fn can_remove_counters(&self, counter: &CounterType) -> bool {
        self.counter_count(counter) > 0
    }

    pub fn add_counter_internal(&mut self, counter: &CounterType, amount: i32) {
        self.add_counter(counter, amount);
    }

    pub fn create_counter_static(&mut self) {
        self.put_etb_counters();
    }

    pub fn subtract_counter(&mut self, counter: &CounterType, amount: i32) {
        self.remove_counter(counter, amount);
    }

    pub fn clear_counters(&mut self) {
        self.counters.clear();
    }

    pub fn sum_all_counters(&self) -> i32 {
        self.counters.values().sum()
    }

    pub fn put_etb_counters(&mut self) {
        if self.etb_counters_p1p1 > 0 {
            self.add_counter(&CounterType::P1P1, self.etb_counters_p1p1);
            self.etb_counters_p1p1 = 0;
        }
    }

    pub fn copy_changed_s_vars_from(&mut self, other: &Card) {
        self.set_svars_map(other.svars.clone());
    }

    pub fn add_changed_s_vars(&mut self, key: &str, value: &str) {
        self.set_s_var(key, value);
    }

    pub fn remove_changed_s_vars(&mut self, key: &str) {
        self.remove_s_var(key);
    }

    pub fn add_changed_mana_cost(&mut self, mana_cost: &str) {
        if !self.has_s_var("OriginalManaCost") {
            self.set_s_var("OriginalManaCost", self.mana_cost.to_string());
        }
        self.mana_cost = ManaCost::parse(mana_cost);
        self.calculate_perpetual_adjusted_mana_cost();
        self.update_mana_cost_for_view();
    }
    pub fn remove_changed_mana_cost(&mut self, _timestamp: i64, _static_id: i64) -> bool {
        let Some(original) = self.svars.get("OriginalManaCost").cloned() else {
            return false;
        };
        let before = self.mana_cost.clone();
        self.mana_cost = ManaCost::parse(&original);
        self.calculate_perpetual_adjusted_mana_cost();
        self.update_mana_cost_for_view();
        self.remove_s_var("OriginalManaCost");
        self.mana_cost != before
    }

    pub fn cleanup_exiled_with(&mut self) {
        self.exiled_by = None;
    }

    pub fn has_paper_foil(&self) -> bool {
        self.paper_foil
    }

    pub fn has_marked_color(&self) -> bool {
        !self.color.is_colorless()
    }

    pub fn can_produce_color_mana(
        &self,
        game: &GameState,
        colors: &std::collections::HashSet<String>,
    ) -> bool {
        crate::card::card_util::card_can_produce_color_mana(game, self.id, colors)
    }

    pub fn can_produce_same_mana_type_with(&self, game: &GameState, other: &Card) -> bool {
        crate::card::card_util::card_can_produce_same_mana_type_with(game, self.id, other.id)
    }

    pub fn has_remove_intrinsic(&self) -> bool {
        false
    }

    pub fn update_spell_abilities(&mut self) {
        self.activated_abilities.clear();
        for (i, raw) in self.abilities.iter().enumerate() {
            if let Some(parsed) = crate::ability::activated::parse_activated_ability(raw, i) {
                self.activated_abilities.push(parsed);
            }
        }
    }

    pub fn refresh_action_specs(&mut self) {
        let mut spell_specs = Vec::new();
        let mut spell_cost = None;
        let mut ai_phyrexian_payment = None;
        let mut spree_min_mode_cost = None;

        for (ability_index, raw) in self.abilities.iter().enumerate() {
            let parsed = ParsedParams::parse(raw);
            if ai_phyrexian_payment.is_none() {
                ai_phyrexian_payment = parsed.get(keys::AI_PHYREXIAN_PAYMENT).map(str::to_string);
            }
            let Some(sp_kind) = parsed.get(keys::SP) else {
                continue;
            };
            let cost_contains_x = parsed
                .get(keys::COST)
                .is_some_and(|cost| cost.contains('X'));
            if spell_cost.is_none() {
                spell_cost = parsed.get(keys::COST).map(parse_cost);
            }
            spell_specs.push(CardActionSpellSpec {
                ability_index,
                has_valid_tgts: parsed.has(keys::VALID_TGTS),
                cost_contains_x,
                target_chain: self.collect_action_target_chain(raw),
            });

            if sp_kind.eq_ignore_ascii_case("Charm") {
                if let Some(choices) = parsed.get(keys::CHOICES) {
                    let min_mode_cost = choices
                        .split(',')
                        .filter_map(|name| {
                            self.svars.get(name.trim()).and_then(|svar_val| {
                                ParsedParams::parse(svar_val)
                                    .get(keys::MODE_COST)
                                    .map(|cost| forge_foundation::ManaCost::parse(cost).cmc())
                            })
                        })
                        .min();
                    spree_min_mode_cost = spree_min_mode_cost.or(min_mode_cost);
                }
            }
        }

        self.action_spell_specs = spell_specs;
        self.action_spell_cost = spell_cost;
        self.ai_phyrexian_payment = ai_phyrexian_payment;
        self.spree_min_mode_cost = spree_min_mode_cost;
    }

    fn refresh_action_specs_after_svar_change(&mut self) {
        if self.action_spell_specs.is_empty() {
            return;
        }
        self.refresh_action_specs();
    }

    fn collect_action_target_chain(&self, ability_text: &str) -> Vec<CardActionTargetSpec> {
        let mut specs = Vec::new();
        let mut current = Some(ability_text.to_string());

        while let Some(text) = current {
            let parsed = ParsedParams::parse(&text);
            let params = Params::from_parsed(&parsed);
            if let Some(target_restrictions) = TargetRestrictions::new_from_parsed(&parsed, &params)
            {
                specs.push(CardActionTargetSpec {
                    min_targets: parse_literal_target_count(&target_restrictions.min_targets),
                    target_restrictions,
                });
            }

            current = parsed
                .get(keys::SUB_ABILITY)
                .and_then(|name| self.svars.get(name.trim()))
                .cloned();
        }

        specs
    }

    pub fn inc_shield_count(&mut self) {
        self.damage_prevention += 1;
    }

    pub fn dec_shield_count(&mut self) {
        self.damage_prevention = (self.damage_prevention - 1).max(0);
    }

    pub fn reset_shield_count(&mut self) {
        self.damage_prevention = 0;
    }

    pub fn add_regenerated_this_turn(&mut self) {
        self.regeneration_shields += 1;
    }

    pub fn can_be_shielded(&self) -> bool {
        self.is_permanent()
    }

    pub fn add_untap_command(&mut self) {
        self.set_s_var("_cmd_untap", "1");
    }
    pub fn add_unattach_command(&mut self) {
        self.set_s_var("_cmd_unattach", "1");
    }
    pub fn add_faceup_command(&mut self) {
        self.set_s_var("_cmd_faceup", "1");
    }
    pub fn add_facedown_command(&mut self) {
        self.set_s_var("_cmd_facedown", "1");
    }
    pub fn add_change_controller_command(&mut self) {
        self.set_s_var("_cmd_change_controller", "1");
    }
    pub fn add_phase_out_command(&mut self) {
        self.set_s_var("_cmd_phase_out", "1");
    }
    pub fn add_leaves_play_command(&mut self) {
        self.set_s_var("_cmd_leaves_play", "1");
    }
    pub fn add_static_command_list(&mut self) {
        self.set_s_var("_cmd_static", "1");
    }
    pub fn run_leaves_play_commands(&mut self) {
        if self.has_s_var("_cmd_leaves_play") {
            self.cleanup_exiled_with();
            self.remove_s_var("_cmd_leaves_play");
        }
    }
    pub fn run_untap_commands(&mut self) {
        if self.has_s_var("_cmd_untap") {
            self.untap();
            self.remove_s_var("_cmd_untap");
        }
    }
    pub fn run_unattach_commands(&mut self) {
        if self.has_s_var("_cmd_unattach") {
            self.unattach_from_entity();
            self.remove_s_var("_cmd_unattach");
        }
    }
    pub fn run_faceup_commands(&mut self) {
        if self.has_s_var("_cmd_faceup") {
            self.turn_face_up();
            self.remove_s_var("_cmd_faceup");
        }
    }
    pub fn run_facedown_commands(&mut self) {
        if self.has_s_var("_cmd_facedown") {
            self.turn_face_down();
            self.remove_s_var("_cmd_facedown");
        }
    }
    pub fn run_change_controller_commands(&mut self) {
        if self.has_s_var("_cmd_change_controller") {
            self.clear_temp_controllers();
            self.remove_s_var("_cmd_change_controller");
        }
    }
    pub fn run_phase_out_commands(&mut self) {
        if self.has_s_var("_cmd_phase_out") {
            self.phase();
            self.remove_s_var("_cmd_phase_out");
        }
    }

    pub fn has_sickness(&self) -> bool {
        self.summoning_sick
    }

    pub fn has_become_target_this_turn(&self) -> bool {
        self.became_target_this_turn
    }

    pub fn add_target_from_this_turn(&mut self) {
        self.became_target_this_turn = true;
    }

    pub fn has_started_the_turn_untapped(&self) -> bool {
        !self.started_turn_tapped
    }

    pub fn came_under_control_since_last_upkeep(&self) -> bool {
        self.came_under_control_since_last_upkeep
    }

    pub fn add_temp_controller(&mut self, player: PlayerId) {
        self.temp_controllers.push(player);
    }

    pub fn remove_temp_controller(&mut self, player: PlayerId) {
        self.temp_controllers.retain(|&p| p != player);
    }

    pub fn clear_temp_controllers(&mut self) {
        self.temp_controllers.clear();
    }

    pub fn clear_controllers(&mut self) {
        self.controller = self.owner;
        self.clear_temp_controllers();
    }

    pub fn may_player_look(&self, player: PlayerId) -> bool {
        self.may_look_at.contains(&player)
    }

    pub fn add_may_look_face_down_exile(&mut self, player: PlayerId) {
        if !self.may_look_at.contains(&player) {
            self.may_look_at.push(player);
        }
    }

    pub fn add_may_look_at(&mut self, player: PlayerId) {
        if !self.may_look_at.contains(&player) {
            self.may_look_at.push(player);
        }
    }

    pub fn remove_may_look_at(&mut self, player: PlayerId) {
        self.may_look_at.retain(|&p| p != player);
    }

    pub fn add_may_look_temp(&mut self, player: PlayerId) {
        self.add_may_look_at(player);
    }

    pub fn remove_may_look_temp(&mut self, player: PlayerId) {
        self.remove_may_look_at(player);
    }

    pub fn update_may_look(&mut self) {
        let mut seen = HashSet::new();
        self.may_look_at.retain(|p| seen.insert(*p));
    }
    pub fn update_may_play(&mut self) {
        let mut seen = HashSet::new();
        self.may_play.retain(|p| seen.insert(*p));
    }

    pub fn may_play(&self, player: PlayerId) -> bool {
        self.may_play.contains(&player)
    }

    pub fn remove_may_play(&mut self, player: PlayerId) {
        self.may_play.retain(|&p| p != player);
    }

    pub fn reset_may_play_turn(&mut self) {
        self.may_play.clear();
    }

    pub fn remove_attached_to(&mut self) {
        self.attached_to = None;
    }

    pub fn attach_to_entity(&mut self, host: CardId) {
        self.attached_to = Some(host);
    }

    pub fn add_attachment(&mut self, card_id: CardId) {
        if !self.attachments.contains(&card_id) {
            self.attachments.push(card_id);
        }
    }

    pub fn remove_attachment(&mut self, card_id: CardId) {
        self.attachments.retain(|&id| id != card_id);
    }

    pub fn unattach_from_entity(&mut self) {
        self.attached_to = None;
    }

    pub fn clear_intrinsic_keywords(&mut self) {
        self.keywords.clear();
    }

    pub fn clear_all_keyword_sets(&mut self) {
        self.keywords.clear();
        self.pump_keywords.clear();
        self.granted_keywords.clear();
    }

    pub fn clear_subtypes(&mut self) {
        self.type_line.subtypes.clear();
    }

    pub fn clear_changed_card_types(&mut self) {
        self.update_types();
    }
    pub fn clear_changed_card_colors(&mut self) {
        self.color = ColorSet::COLORLESS;
    }
    pub fn add_changed_card_types_by_text(&mut self) {
        self.update_types();
    }
    pub fn remove_changed_card_types_by_text(&mut self) {
        self.update_types();
    }
    pub fn add_changed_card_types(&mut self) {
        self.update_types();
    }
    pub fn remove_changed_card_types(&mut self) {
        self.update_types();
    }
    pub fn update_type_cache(&mut self) {
        self.type_line = CardTypeLine::parse(&self.type_line.to_string());
    }
    pub fn has_changed_card_colors(&self) -> bool {
        !self.color.is_colorless()
    }
    pub fn add_color_by_text(&mut self, color: ColorSet) {
        self.add_color(color);
    }
    pub fn remove_color_by_text(&mut self) {
        self.remove_color();
    }
    pub fn remove_color(&mut self) {
        self.color = ColorSet::COLORLESS;
    }
    pub fn add_clone_state(&mut self) {
        self.set_s_var("CloneState", "True");
    }
    pub fn remove_clone_state(&mut self) {
        self.remove_s_var("CloneState");
    }
    pub fn remove_clone_states(&mut self) {
        self.remove_s_var("CloneState");
    }
    pub fn add_new_pt_by_text(&mut self, p: i32, t: i32) {
        self.base_power = Some(p);
        self.base_toughness = Some(t);
    }
    pub fn remove_new_p_tby_text(&mut self) {
        self.clear_new_pt();
    }
    pub fn add_new_pt(&mut self, p: i32, t: i32) {
        self.base_power = Some(p);
        self.base_toughness = Some(t);
    }
    pub fn remove_new_pt(&mut self) {
        self.clear_new_pt();
    }
    pub fn clear_new_pt(&mut self) {
        self.base_power = None;
        self.base_toughness = None;
    }
    pub fn toughness_assigns_damage(&self) -> bool {
        self.has_keyword("CARDNAME assigns combat damage equal to its toughness")
    }
    pub fn assign_no_combat_damage(&self) -> bool {
        self.has_keyword("CARDNAME assigns no combat damage")
    }
    pub fn add_pt_boost(&mut self, p: i32, t: i32) {
        self.power_modifier += p;
        self.toughness_modifier += t;
    }
    pub fn remove_pt_boost(&mut self, p: i32, t: i32) {
        self.power_modifier -= p;
        self.toughness_modifier -= t;
    }
    pub fn add_draft_action(&mut self) {
        self.set_s_var("DraftAction", "True");
    }
    pub fn add_intensity(&mut self, v: i32) {
        self.intensity += v;
    }
    pub fn has_intensity(&self) -> bool {
        self.intensity > 0
    }
    pub fn has_perpetual(&self) -> bool {
        !self.perpetual.is_empty()
    }
    pub fn get_perpetual(&self) -> &[PerpetualRecord] {
        &self.perpetual
    }
    pub fn add_perpetual(&mut self, p: PerpetualRecord) {
        self.apply_perpetual_record(p, true);
    }
    pub fn remove_perpetual(&mut self, timestamp: i64) -> bool {
        if let Some(idx) = self
            .perpetual
            .iter()
            .position(|p| p.timestamp() == timestamp)
        {
            self.perpetual.remove(idx);
            true
        } else {
            false
        }
    }
    pub fn set_perpetual(&mut self, old_card: &Card, apply_effects: bool) {
        self.perpetual = old_card.perpetual.clone();
        if apply_effects {
            for p in self.perpetual.clone() {
                self.apply_perpetual_record(p, false);
            }
        }
    }
    pub fn set_perpetual_from(&mut self, old_card: &Card) {
        self.set_perpetual(old_card, true);
    }
    pub fn apply_perpetual_record(&mut self, p: PerpetualRecord, remember: bool) {
        if remember {
            self.perpetual.push(p.clone());
        }
        p.apply_effect(self);
    }
    pub fn add_trigger_for_static_ability(&mut self, trig: Trigger) {
        self.add_trigger(trig);
    }
    pub fn visit_keywords(&self) -> Vec<String> {
        self.keywords.as_string_list()
    }
    pub fn update_keywords(&mut self) {
        self.update_keywords_cache();
    }
    pub fn add_changed_card_keywords(&mut self, kw: &str) {
        self.add_intrinsic_keyword(kw);
    }
    pub fn add_keyword_for_static_ability(&mut self, kw: &str) {
        self.granted_keywords.add(kw);
    }
    pub fn add_changed_card_keywords_by_text(&mut self, kw: &str) {
        self.add_intrinsic_keyword(kw);
    }
    pub fn add_changed_card_keywords_internal(&mut self, kw: &str) {
        self.add_intrinsic_keyword(kw);
    }
    pub fn remove_changed_card_keywords(&mut self, kw: &str) {
        self.remove_intrinsic_keyword(kw);
    }
    pub fn remove_changed_card_keywords_by_text(&mut self, kw: &str) {
        self.remove_intrinsic_keyword(kw);
    }
    pub fn clear_changed_card_keywords(&mut self) {
        self.keywords.clear();
    }
    pub fn clear_static_changed_card_keywords(&mut self) {
        self.granted_keywords.clear();
    }
    pub fn add_hidden_extrinsic_keywords(&mut self, kw: &str) {
        self.granted_keywords.add(kw);
    }
    pub fn remove_hidden_extrinsic_keywords(&mut self, kw: &str) {
        self.granted_keywords.remove(kw);
    }
    pub fn remove_hidden_extrinsic_keyword(&mut self, kw: &str) {
        self.granted_keywords.remove(kw);
    }
    pub fn has_start_of_keyword(&self, prefix: &str) -> bool {
        self.keywords.iter_strings().any(|k| k.starts_with(prefix))
    }
    pub fn has_start_of_un_hidden_keyword(&self, prefix: &str) -> bool {
        self.has_start_of_keyword(prefix)
    }
    pub fn has_any_keyword(&self) -> bool {
        !self.keywords.as_string_list().is_empty()
            || !self.granted_keywords.as_string_list().is_empty()
            || !self.pump_keywords.as_string_list().is_empty()
    }
    pub fn add_cant_have_keyword(&mut self, kw: &str) {
        self.cant_have_keywords.insert(kw.to_ascii_lowercase());
    }
    pub fn remove_cant_have_keyword(&mut self, kw: &str) {
        self.cant_have_keywords.remove(&kw.to_ascii_lowercase());
    }
    pub fn add_changed_text_color_word(&mut self, from: &str, to: &str) {
        self.set_s_var(format!("TextColor:{from}"), to);
    }
    pub fn remove_changed_text_color_word(&mut self, from: &str) {
        self.remove_s_var(&format!("TextColor:{from}"));
    }
    pub fn add_changed_text_type_word(&mut self, from: &str, to: &str) {
        self.set_s_var(format!("TextType:{from}"), to);
    }
    pub fn remove_changed_text_type_word(&mut self, from: &str) {
        self.remove_s_var(&format!("TextType:{from}"));
    }
    pub fn copy_changed_text_from(&mut self, other: &Card) {
        for (k, v) in &other.svars {
            if k.starts_with("TextColor:") || k.starts_with("TextType:") {
                self.svars.insert(k.clone(), v.clone());
            }
        }
    }
    pub fn has_playable_land_face(&self) -> bool {
        self.is_land()
            || self
                .other_part
                .as_ref()
                .map(|p| p.type_line.is_land())
                .unwrap_or(false)
    }
    pub fn phase(&mut self) {
        self.phased_out = !self.phased_out;
    }
    pub fn associated_with_color(&self, game: &GameState, color: &str) -> bool {
        let mut colors = HashSet::new();
        colors.insert(color.to_string());
        forge_foundation::Color::from_name(&color.to_ascii_lowercase())
            .map(|parsed| self.color.has_any_color(parsed.mask()))
            .unwrap_or(false)
            || self.can_produce_color_mana(game, &colors)
    }
    pub fn has_no_name(&self) -> bool {
        self.card_name.trim().is_empty()
    }
    pub fn shares_name_with(&self, other: &Card) -> bool {
        self.card_name.eq_ignore_ascii_case(&other.card_name)
    }
    pub fn has_creature_type(&self, creature_type: &str) -> bool {
        if !self.is_creature() && !self.type_line.core_types.contains(&CoreType::Kindred) {
            return false;
        }
        if self.type_line.has_subtype(creature_type) {
            return true;
        }
        self.has_keyword("Changeling") && crate::game::TypeRegistry::is_creature_type(creature_type)
    }
    pub fn has_subtype(&self, subtype: &str) -> bool {
        self.type_line.has_subtype(subtype) || self.has_creature_type(subtype)
    }
    pub fn shares_color_with(&self, other: &Card) -> bool {
        (self.color.has_white() && other.color.has_white())
            || (self.color.has_blue() && other.color.has_blue())
            || (self.color.has_black() && other.color.has_black())
            || (self.color.has_red() && other.color.has_red())
            || (self.color.has_green() && other.color.has_green())
            || (self.color.is_colorless() && other.color.is_colorless())
    }
    pub fn shares_cmc_with(&self, other: &Card) -> bool {
        self.mana_value() == other.mana_value()
    }
    pub fn shares_creature_type_with(&self, other: &Card) -> bool {
        crate::game::TypeRegistry::creature_types()
            .iter()
            .any(|creature_type| {
                self.has_creature_type(creature_type) && other.has_creature_type(creature_type)
            })
    }
    pub fn shares_land_type_with(&self, other: &Card) -> bool {
        self.shares_creature_type_with(other) && self.is_land() && other.is_land()
    }
    pub fn shares_permanent_type_with(&self, other: &Card) -> bool {
        (self.is_creature() && other.is_creature())
            || (self.is_land() && other.is_land())
            || (self.type_line.is_artifact() && other.type_line.is_artifact())
            || (self.type_line.is_enchantment() && other.type_line.is_enchantment())
            || (self.type_line.is_planeswalker() && other.type_line.is_planeswalker())
    }
    pub fn shares_card_type_with(&self, other: &Card) -> bool {
        self.shares_permanent_type_with(other)
    }
    pub fn shares_all_card_types_with(&self, other: &Card) -> bool {
        self.type_line.core_types == other.type_line.core_types
    }
    pub fn shares_controller_with(&self, other: &Card) -> bool {
        self.controller == other.controller
    }
    pub fn has_a_basic_land_type(&self) -> bool {
        self.type_line.has_subtype("Plains")
            || self.type_line.has_subtype("Island")
            || self.type_line.has_subtype("Swamp")
            || self.type_line.has_subtype("Mountain")
            || self.type_line.has_subtype("Forest")
    }
    pub fn has_a_non_basic_land_type(&self) -> bool {
        self.is_land() && !self.has_a_basic_land_type()
    }
    pub fn has_dealt_damage_to_opponent_this_turn(&self) -> bool {
        self.total_damage_done_this_turn > 0
    }
    pub fn has_been_dealt_deathtouch_damage(&self) -> bool {
        self.has_deathtouch_damage
    }
    pub fn has_been_dealt_excess_damage_this_turn(&self) -> bool {
        self.damage > self.toughness()
    }
    pub fn log_excess_damage(&mut self) {
        self.set_s_var("ExcessDamageLogged", "True");
    }
    pub fn add_assigned_damage(&mut self, amount: i32) {
        self.assigned_damage += amount;
    }
    pub fn clear_assigned_damage(&mut self) {
        self.assigned_damage = 0;
    }
    pub fn can_damage_prevented(&self) -> bool {
        !self.has_keyword("Damage can't be prevented")
    }
    pub fn static_replace_damage(&self, amount: i32) -> i32 {
        amount
    }
    pub fn add_damage_after_prevention(&mut self, amount: i32) -> i32 {
        let dealt = if self.can_be_dealt_damage() {
            amount.max(0)
        } else {
            0
        };
        if dealt <= 0 {
            return 0;
        }
        if self.type_line.is_planeswalker() {
            self.remove_counter(&CounterType::Loyalty, dealt);
        }
        if self.type_line.core_types.contains(&CoreType::Battle) {
            self.remove_counter(&CounterType::Named("DEFENSE".to_string()), dealt);
        }
        if self.is_creature() {
            self.damage += dealt;
        }
        dealt
    }
    pub fn border_color(&self) -> &'static str {
        if self.color.is_colorless() {
            "Colorless"
        } else if self.color.has_white() {
            "White"
        } else if self.color.has_blue() {
            "Blue"
        } else if self.color.has_black() {
            "Black"
        } else if self.color.has_red() {
            "Red"
        } else {
            "Green"
        }
    }
    pub fn was_discarded(&self) -> bool {
        self.discarded
    }
    pub fn was_surveilled(&self) -> bool {
        self.surveilled
    }
    pub fn was_milled(&self) -> bool {
        self.milled
    }
    pub fn clear_ring_bearer(&mut self) {
        self.remove_s_var("RingBearer");
    }
    pub fn add_saddled_by_this_turn(&mut self, card: CardId) {
        self.set_s_var("SaddledBy", format!("{}", card.0));
    }
    pub fn reset_saddled(&mut self) {
        self.remove_s_var("SaddledBy");
    }
    pub fn can_specialize(&self) -> bool {
        self.has_keyword("Specialize")
    }
    pub fn can_crew(&self) -> bool {
        self.is_permanent()
    }
    pub fn reset_times_crewed_this_turn(&mut self) {
        self.times_crewed_this_turn = 0;
    }
    pub fn becomes_crewed(&mut self) {
        self.is_crewed = true;
        self.times_crewed_this_turn += 1;
    }
    pub fn reset_crewed(&mut self) {
        self.is_crewed = false;
    }
    pub fn add_crewed_by_this_turn(&mut self, _card: CardId) {
        self.times_crewed_this_turn += 1;
    }
    pub fn visit_attraction(&mut self) {
        self.visited_this_turn = true;
    }
    pub fn was_visited_this_turn(&self) -> bool {
        self.visited_this_turn
    }
    pub fn animate_bestow(&mut self) {
        self.is_bestowed = false;
    }
    pub fn unanimate_bestow(&mut self) {
        self.is_bestowed = true;
    }
    pub fn equals_with_game_timestamp(&self, other: &Card) -> bool {
        self.id == other.id && self.zone_timestamp == other.zone_timestamp
    }
    pub fn update_world_timestamp(&mut self) {
        self.zone_timestamp = self.zone_timestamp.saturating_add(1);
    }
    pub fn can_be_discarded_by(&self, _player: PlayerId) -> bool {
        true
    }
    pub fn can_be_destroyed(&self) -> bool {
        !self.has_indestructible()
    }
    pub fn can_be_targeted_by(&self, _player: PlayerId) -> bool {
        true
    }
    pub fn cant_be_attached_msg(&self) -> Option<String> {
        None
    }
    pub fn can_be_sacrificed_by(&self, _player: PlayerId) -> bool {
        true
    }
    pub fn can_exiled_by(&self, _player: PlayerId) -> bool {
        true
    }
    pub fn update_static_abilities(&mut self) {
        self.recompute_changed_card_traits();
    }
    pub fn update_triggers(&mut self) {
        self.recompute_changed_card_traits();
    }
    pub fn update_replacement_effects(&mut self) {
        self.recompute_changed_card_traits();
    }
    pub fn was_cast(&self) -> bool {
        // Mirrors Java `Card.wasCast()`: true iff `castFrom` was set during
        // cast resolution. Sneak Attack and other "put onto battlefield"
        // effects don't go through the cast pipeline and leave this `None`.
        self.cast_from.is_some()
    }
    pub fn on_end_of_combat(&mut self) {
        self.assigned_damage = 0;
    }
    pub fn on_cleanup_phase(&mut self) {
        self.became_target_this_turn = false;
        self.visited_this_turn = false;
        self.damage_prevention = 0;
    }
    pub fn has_etb_trigger(&self) -> bool {
        self.triggers.iter().any(|t| {
            t.kind == crate::trigger::TriggerType::ChangesZone
                && t.destination_zone() == Some(ZoneType::Battlefield)
        })
    }
    pub fn has_etb_replacement(&self) -> bool {
        self.has_replacement_effect()
    }
    pub fn can_move_to_command_zone(&self) -> bool {
        self.is_commander && self.move_to_command_zone
    }
    pub fn from_paper_card(&mut self) {
        self.is_token = false;
    }
    pub fn cleanup_copied_changes_from(&mut self) {
        self.clear_changed_card_traits();
    }
    pub fn activated_this_turn(&self) -> bool {
        self.ability_activated_this_turn > 0
    }
    pub fn add_ability_activated(&mut self) {
        self.ability_activated_this_turn += 1;
    }
    pub fn add_ability_activated_for(
        &mut self,
        ability: Option<&crate::spellability::SpellAbility>,
    ) {
        self.add_ability_activated_for_with_limit_increase(ability, false);
    }
    pub fn add_ability_activated_for_with_limit_increase(
        &mut self,
        ability: Option<&crate::spellability::SpellAbility>,
        loyalty_limit_increase: bool,
    ) {
        if let Some(ability) = ability {
            self.number_turn_activations.add(ability);
            self.number_game_activations.add(ability);
            if ability.ir.pw_ability {
                self.add_planeswalker_ability_activated(loyalty_limit_increase);
            }
        }
        self.add_ability_activated();
    }
    pub fn add_ability_resolved(&mut self) {
        self.ability_resolved_this_turn += 1;
    }
    pub fn add_ability_resolved_for(
        &mut self,
        ability: Option<&crate::spellability::SpellAbility>,
    ) {
        if let Some(ability) = ability {
            self.number_ability_resolved.add(ability);
        }
        self.add_ability_resolved();
    }
    pub fn get_ability_activated_this_turn(
        &self,
        ability: Option<&crate::spellability::SpellAbility>,
    ) -> u32 {
        ability
            .map(|ability| self.number_turn_activations.get(ability) as u32)
            .unwrap_or(0)
    }
    pub fn get_ability_activated_this_game(
        &self,
        ability: Option<&crate::spellability::SpellAbility>,
    ) -> u32 {
        ability
            .map(|ability| self.number_game_activations.get(ability) as u32)
            .unwrap_or(0)
    }
    pub fn get_ability_resolved_this_turn(
        &self,
        ability: Option<&crate::spellability::SpellAbility>,
    ) -> u32 {
        ability
            .map(|ability| self.number_ability_resolved.get(ability) as u32)
            .unwrap_or(0)
    }
    pub fn get_ability_resolved_this_turn_activators(
        &self,
        ability: Option<&crate::spellability::SpellAbility>,
    ) -> Vec<crate::ids::PlayerId> {
        ability
            .map(|ability| self.number_ability_resolved.get_activators(ability))
            .unwrap_or_default()
    }
    pub fn reset_ability_resolved_this_turn(&mut self) {
        self.ability_resolved_this_turn = 0;
        self.number_ability_resolved.clear();
    }
    pub fn add_chosen_modes(&mut self, modes: Vec<usize>, turn: u32) {
        self.chosen_modes = Some(modes);
        self.chosen_modes_turn = Some(turn);
    }
    pub fn reset_chosen_mode_turn(&mut self) {
        self.chosen_modes_turn = None;
        self.chosen_modes = None;
    }
    pub fn add_planeswalker_ability_activated(&mut self, loyalty_limit_increase: bool) {
        self.planeswalker_abilities_activated += 1;
        if self.planeswalker_abilities_activated == 2 && loyalty_limit_increase {
            self.planeswalker_activation_limit_used = true;
        }
    }
    pub fn planeswalker_activation_limit_used(&self) -> bool {
        self.planeswalker_activation_limit_used
    }
    pub fn reset_activations_per_turn(&mut self) {
        self.ability_activated_this_turn = 0;
        self.number_turn_activations.clear();
        self.planeswalker_abilities_activated = 0;
        self.planeswalker_activation_limit_used = false;
    }
    pub fn add_can_block_additional(&mut self, n: i32) {
        self.can_block_additional += n;
    }
    pub fn remove_can_block_additional(&mut self, n: i32) {
        self.can_block_additional = (self.can_block_additional - n).max(0);
    }
    pub fn can_block_additional(&self) -> i32 {
        self.can_block_additional
    }
    pub fn add_can_block_any(&mut self) {
        self.can_block_any = true;
    }
    pub fn remove_can_block_any(&mut self) {
        self.can_block_any = false;
    }
    pub fn can_block_any(&self) -> bool {
        self.can_block_any
    }
    pub fn ignore_legend_rule(&self) -> bool {
        self.ignore_legend_rule_flag
    }
    pub fn attack_vigilance(&self) -> bool {
        self.has_vigilance()
    }
    pub fn unlock_room(&mut self) {
        self.set_s_var("RoomLocked", "False");
    }
    pub fn lock_room(&mut self) {
        self.set_s_var("RoomLocked", "True");
    }
    pub fn update_rooms(&mut self) {
        if !self.has_s_var("RoomLocked") {
            self.set_s_var("RoomLocked", "False");
        }
    }

    /// Transform this double-faced card to its other face.
    /// Swaps all face-dependent characteristics with `other_part`.
    /// No-op if `other_part` is `None`.
    /// Mirrors Java's `CardUtil.applyState(card, CardStateName.Backside)`.
    /// Mirror of `Card.isModal()`: true only for MDFC (modal) cards, whose back
    /// face may be played from hand. Transform / meld backs are not modal.
    pub fn is_modal(&self) -> bool {
        self.other_part.as_ref().is_some_and(|other| other.is_modal)
    }

    pub fn transform(&mut self) {
        if let Some(other) = self.other_part.as_mut() {
            std::mem::swap(&mut self.card_name, &mut other.name);
            std::mem::swap(&mut self.oracle_text, &mut other.oracle_text);
            std::mem::swap(&mut self.type_line, &mut other.type_line);
            std::mem::swap(&mut self.mana_cost, &mut other.mana_cost);
            std::mem::swap(&mut self.color, &mut other.color);
            std::mem::swap(&mut self.base_power, &mut other.base_power);
            std::mem::swap(&mut self.base_toughness, &mut other.base_toughness);
            std::mem::swap(&mut self.keywords, &mut other.keywords);
            std::mem::swap(&mut self.abilities, &mut other.abilities);
            std::mem::swap(&mut self.triggers, &mut other.triggers);
            std::mem::swap(&mut self.static_abilities, &mut other.static_abilities);
            std::mem::swap(
                &mut self.replacement_effects,
                &mut other.replacement_effects,
            );
            std::mem::swap(&mut self.svars, &mut other.svars);

            // Reset per-face transient state
            self.power_modifier = 0;
            self.toughness_modifier = 0;
            self.damage = 0;
            self.granted_keywords.clear();

            // Re-parse activated abilities from new face's abilities
            self.activated_abilities = self
                .abilities
                .iter()
                .enumerate()
                .filter_map(|(i, raw)| {
                    parse_or_warn(parse_activated_ability(raw, i), "ActivatedAbility", raw)
                })
                .collect();
            self.base_ability_count = self.activated_abilities.len();
            self.base_trigger_count = self.triggers.len();
            self.parsed_svar_cache.clear();
            self.refresh_action_specs();

            // Re-bind replacement hosts so SVar lookups hit the active face.
            let mut res = std::mem::take(&mut self.replacement_effects);
            for re in &mut res {
                re.set_host_card(self);
            }
            self.replacement_effects = res;

            self.is_transformed = !self.is_transformed;

            // Face characteristics changed; reset trait-change baseline and
            // re-apply active trait-change layers against the new face.
            self.reset_changed_card_traits_baseline();
            self.recompute_changed_card_traits();
        }
    }

    fn activated_to_spell_abilities(&self, list: &[ActivatedAbility]) -> Vec<SpellAbility> {
        list.iter()
            .map(|ab| {
                let mut sa = crate::spellability::build_spell_ability_from_host_card(
                    self,
                    &ab.ability_text,
                    self.controller,
                );
                sa.is_activated = true;
                sa
            })
            .collect()
    }

    fn spell_to_activated_abilities(list: &[SpellAbility]) -> Vec<ActivatedAbility> {
        list.iter()
            .enumerate()
            .filter_map(|(i, sa)| parse_activated_ability(&sa.ability_text, i))
            .collect()
    }

    fn capture_changed_card_traits_baseline_if_needed(&mut self) {
        if self.trait_base_activated_abilities.is_none() {
            self.trait_base_activated_abilities = Some(self.activated_abilities.clone());
            self.trait_base_triggers = Some(self.triggers.clone());
            self.trait_base_replacement_effects = Some(self.replacement_effects.clone());
            self.trait_base_static_abilities = Some(self.static_abilities.clone());
            self.trait_base_keywords = Some(self.keywords.clone());
        }
    }

    fn reset_changed_card_traits_baseline(&mut self) {
        self.trait_base_activated_abilities = Some(self.activated_abilities.clone());
        self.trait_base_triggers = Some(self.triggers.clone());
        self.trait_base_replacement_effects = Some(self.replacement_effects.clone());
        self.trait_base_static_abilities = Some(self.static_abilities.clone());
        self.trait_base_keywords = Some(self.keywords.clone());
    }

    pub(crate) fn reset_changed_card_traits_baseline_to_current(&mut self) {
        self.reset_changed_card_traits_baseline();
        self.recompute_changed_card_traits();
    }

    fn recompute_changed_card_traits(&mut self) {
        let Some(base_activated) = self.trait_base_activated_abilities.clone() else {
            return;
        };
        let Some(base_triggers) = self.trait_base_triggers.clone() else {
            return;
        };
        let Some(base_replacements) = self.trait_base_replacement_effects.clone() else {
            return;
        };
        let Some(base_static) = self.trait_base_static_abilities.clone() else {
            return;
        };
        let Some(base_keywords) = self.trait_base_keywords.clone() else {
            return;
        };

        let mut spell_abilities = self.activated_to_spell_abilities(&base_activated);
        let mut triggers = base_triggers;
        let mut replacements = base_replacements;
        let mut static_abilities = base_static;
        let mut keywords = base_keywords;

        for layer in self.changed_card_traits_by_text.values() {
            spell_abilities = crate::card::card_state::apply_spell_ability(layer, spell_abilities);
            triggers = crate::card::card_state::apply_trigger(layer, triggers);
            replacements = crate::card::card_state::apply_replacement_effect(layer, replacements);
            static_abilities =
                crate::card::card_state::apply_static_ability(layer, static_abilities);
            keywords = crate::card::card_state::apply_keywords(layer, keywords);
        }
        for layer in self.changed_card_traits.values() {
            spell_abilities = crate::card::card_state::apply_spell_ability(layer, spell_abilities);
            triggers = crate::card::card_state::apply_trigger(layer, triggers);
            replacements = crate::card::card_state::apply_replacement_effect(layer, replacements);
            static_abilities =
                crate::card::card_state::apply_static_ability(layer, static_abilities);
            keywords = crate::card::card_state::apply_keywords(layer, keywords);
        }

        self.activated_abilities = Self::spell_to_activated_abilities(&spell_abilities);
        self.triggers = triggers;
        self.replacement_effects = replacements;
        self.static_abilities = static_abilities;
        self.keywords = keywords;
    }

    /// Java parity: `addChangedCardTraits`.
    pub fn add_changed_card_traits(
        &mut self,
        layer: card_trait_changes::CardTraitChanges,
        timestamp: i64,
        static_id: i64,
    ) {
        self.capture_changed_card_traits_baseline_if_needed();
        self.changed_card_traits
            .insert((timestamp, static_id), layer);
        self.recompute_changed_card_traits();
    }

    /// Java parity: `addChangedCardTraitsByText`.
    pub fn add_changed_card_traits_by_text(
        &mut self,
        layer: card_trait_changes::CardTraitChanges,
        timestamp: i64,
        static_id: i64,
    ) {
        self.capture_changed_card_traits_baseline_if_needed();
        self.changed_card_traits_by_text
            .insert((timestamp, static_id), layer);
        self.recompute_changed_card_traits();
    }

    /// Java parity: `removeChangedCardTraits`.
    pub fn remove_changed_card_traits(&mut self, timestamp: i64, static_id: i64) -> bool {
        if self
            .changed_card_traits
            .remove(&(timestamp, static_id))
            .is_none()
        {
            return false;
        }
        if self.changed_card_traits.is_empty() && self.changed_card_traits_by_text.is_empty() {
            if let Some(v) = self.trait_base_activated_abilities.take() {
                self.activated_abilities = v;
            }
            if let Some(v) = self.trait_base_triggers.take() {
                self.triggers = v;
            }
            if let Some(v) = self.trait_base_replacement_effects.take() {
                self.replacement_effects = v;
            }
            if let Some(v) = self.trait_base_static_abilities.take() {
                self.static_abilities = v;
            }
            if let Some(v) = self.trait_base_keywords.take() {
                self.keywords = v;
            }
            return true;
        }

        self.recompute_changed_card_traits();
        true
    }

    /// Java parity: `removeChangedCardTraitsByText`.
    pub fn remove_changed_card_traits_by_text(&mut self, timestamp: i64, static_id: i64) -> bool {
        if self
            .changed_card_traits_by_text
            .remove(&(timestamp, static_id))
            .is_none()
        {
            return false;
        }
        if self.changed_card_traits.is_empty() && self.changed_card_traits_by_text.is_empty() {
            if let Some(v) = self.trait_base_activated_abilities.take() {
                self.activated_abilities = v;
            }
            if let Some(v) = self.trait_base_triggers.take() {
                self.triggers = v;
            }
            if let Some(v) = self.trait_base_replacement_effects.take() {
                self.replacement_effects = v;
            }
            if let Some(v) = self.trait_base_static_abilities.take() {
                self.static_abilities = v;
            }
            if let Some(v) = self.trait_base_keywords.take() {
                self.keywords = v;
            }
            return true;
        }

        self.recompute_changed_card_traits();
        true
    }

    /// Java parity: `clearChangedCardTraits`.
    pub fn clear_changed_card_traits(&mut self) {
        self.changed_card_traits.clear();
        self.changed_card_traits_by_text.clear();
        if let Some(v) = self.trait_base_activated_abilities.take() {
            self.activated_abilities = v;
        }
        if let Some(v) = self.trait_base_triggers.take() {
            self.triggers = v;
        }
        if let Some(v) = self.trait_base_replacement_effects.take() {
            self.replacement_effects = v;
        }
        if let Some(v) = self.trait_base_static_abilities.take() {
            self.static_abilities = v;
        }
        if let Some(v) = self.trait_base_keywords.take() {
            self.keywords = v;
        }
    }

    /// Clear continuous static-ability trait changes from the previous layer pass.
    ///
    /// Static layer effects use negative static IDs so they can be recomputed
    /// each pass without disturbing perpetual/card-state trait changes.
    pub fn clear_static_layer_changed_card_traits(&mut self) {
        let before = self.changed_card_traits.len();
        self.changed_card_traits
            .retain(|(_, static_id), _| *static_id >= 0);
        if self.changed_card_traits.len() == before {
            return;
        }
        if self.changed_card_traits.is_empty() && self.changed_card_traits_by_text.is_empty() {
            if let Some(v) = self.trait_base_activated_abilities.take() {
                self.activated_abilities = v;
            }
            if let Some(v) = self.trait_base_triggers.take() {
                self.triggers = v;
            }
            if let Some(v) = self.trait_base_replacement_effects.take() {
                self.replacement_effects = v;
            }
            if let Some(v) = self.trait_base_static_abilities.take() {
                self.static_abilities = v;
            }
            if let Some(v) = self.trait_base_keywords.take() {
                self.keywords = v;
            }
            return;
        }

        self.recompute_changed_card_traits();
    }

    pub fn remove_changed_state(&mut self) {
        self.clear_changed_card_traits();
    }
}

impl HasSVars for Card {
    fn get_svar(&self, name: &str) -> Option<&str> {
        self.get_s_var(name)
    }

    fn set_svar(&mut self, name: String, value: String) {
        self.set_s_var(name, value);
    }

    fn set_svars(&mut self, new_svars: std::collections::HashMap<String, String>) {
        self.set_svars_map(new_svars.into_iter().collect());
    }

    fn get_svars(&self) -> &std::collections::HashMap<String, String> {
        panic!("Card::get_svars is not supported yet; use get_s_var/has_s_var parity accessors");
    }

    fn remove_svar(&mut self, var: &str) {
        self.remove_s_var(var);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;

    use forge_carddb::parse_card_script;
    use forge_foundation::ManaCost;

    #[test]
    fn card_power_toughness() {
        let mut card = Card::new(
            CardId(0),
            "Test".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Bear"),
            ManaCost::parse("1 G"),
            ColorSet::GREEN,
            Some(2),
            Some(2),
            vec![],
            vec![],
        );
        assert_eq!(card.power(), 2);
        assert_eq!(card.toughness(), 2);

        card.add_counter(&CounterType::P1P1, 1);
        assert_eq!(card.power(), 3);
        assert_eq!(card.toughness(), 3);
    }

    #[test]
    fn can_attack() {
        let mut card = Card::new(
            CardId(0),
            "Test".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Bear"),
            ManaCost::parse("1 G"),
            ColorSet::GREEN,
            Some(2),
            Some(2),
            vec![],
            vec![],
        );
        card.zone = ZoneType::Battlefield;
        assert!(!card.can_attack()); // summoning sick

        card.summoning_sick = false;
        assert!(card.can_attack());

        card.tapped = true;
        assert!(!card.can_attack()); // tapped
    }

    #[test]
    fn haste_bypasses_summoning_sickness() {
        let mut card = Card::new(
            CardId(0),
            "Test".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Bear"),
            ManaCost::parse("1 G"),
            ColorSet::GREEN,
            Some(2),
            Some(2),
            vec!["Haste".to_string()],
            vec![],
        );
        card.zone = ZoneType::Battlefield;
        assert!(card.can_attack()); // haste means no summoning sickness check
    }

    #[test]
    fn keyword_helpers() {
        let card = Card::new(
            CardId(0),
            "Test".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Bear"),
            ManaCost::parse("1 G"),
            ColorSet::GREEN,
            Some(2),
            Some(2),
            vec![
                "Hexproof".to_string(),
                "Menace".to_string(),
                "Indestructible".to_string(),
            ],
            vec![],
        );
        assert!(card.has_hexproof());
        assert!(card.has_menace());
        assert!(card.has_indestructible());
        assert!(!card.has_shroud());
        assert!(!card.has_fear());
        assert!(!card.has_shadow());
    }

    #[test]
    fn protection_from_color() {
        let knight = Card::new(
            CardId(0),
            "White Knight".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Knight"),
            ManaCost::parse("W W"),
            ColorSet::WHITE,
            Some(2),
            Some(2),
            vec!["Protection from black".to_string()],
            vec![],
        );
        let black_source = Card::new(
            CardId(1),
            "Doom Blade".to_string(),
            PlayerId(1),
            CardTypeLine::parse("Instant"),
            ManaCost::parse("1 B"),
            ColorSet::BLACK,
            None,
            None,
            vec![],
            vec![],
        );
        let green_source = Card::new(
            CardId(2),
            "Giant Growth".to_string(),
            PlayerId(1),
            CardTypeLine::parse("Instant"),
            ManaCost::parse("G"),
            ColorSet::GREEN,
            None,
            None,
            vec![],
            vec![],
        );
        assert!(knight.is_protected_from(&black_source));
        assert!(!knight.is_protected_from(&green_source));
        assert!(knight.has_protection_from("black"));
        assert!(!knight.has_protection_from("red"));
    }

    #[test]
    fn ward_and_toxic_parsing() {
        let ward_card = Card::new(
            CardId(0),
            "Ward Bear".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Bear"),
            ManaCost::parse("1 U"),
            ColorSet::BLUE,
            Some(2),
            Some(2),
            vec!["Ward:2".to_string()],
            vec![],
        );
        assert_eq!(ward_card.get_ward_cost(), Some("2".to_string()));

        let toxic_card = Card::new(
            CardId(1),
            "Toxic Elf".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Elf"),
            ManaCost::parse("G"),
            ColorSet::GREEN,
            Some(1),
            Some(1),
            vec!["Toxic:1".to_string()],
            vec![],
        );
        assert_eq!(toxic_card.get_toxic_count(), Some(1));

        // No ward/toxic
        let plain = Card::new(
            CardId(2),
            "Bear".to_string(),
            PlayerId(0),
            CardTypeLine::parse("Creature Bear"),
            ManaCost::parse("1 G"),
            ColorSet::GREEN,
            Some(2),
            Some(2),
            vec![],
            vec![],
        );
        assert_eq!(plain.get_ward_cost(), None);
        assert_eq!(plain.get_toxic_count(), None);
    }

    #[test]
    fn from_rules_copies_attraction_lights() {
        let rules = parse_card_script(
            "Name:Balloon Stand\nTypes:Artifact Attraction\nLights: 2 4 6\nOracle:Test.",
        )
        .expect("card script should parse");
        let card = Card::from_rules(&rules, PlayerId(0));
        assert_eq!(card.attraction_lights, vec![2, 4, 6]);
        assert!(card.has_attraction_light(4));
        assert!(!card.has_attraction_light(3));
    }

    #[test]
    fn can_produce_color_mana_uses_mana_abilities_and_reflection() {
        let mut game = GameState::new(&["Alice", "Bob"], 20);
        let p0 = PlayerId(0);

        let white_land = Card::new(
            CardId(0),
            "White Source".to_string(),
            p0,
            CardTypeLine::parse("Land"),
            ManaCost::parse(""),
            ColorSet::COLORLESS,
            None,
            None,
            vec![],
            vec!["AB$ Mana | Cost$ T | Produced$ W | SpellDescription$ Add {W}.".to_string()],
        );
        let reflecting_pool = Card::new(
            CardId(1),
            "Reflecting Pool".to_string(),
            p0,
            CardTypeLine::parse("Land"),
            ManaCost::parse(""),
            ColorSet::COLORLESS,
            None,
            None,
            vec![],
            vec!["AB$ ManaReflected | Cost$ T | Valid$ Land.YouCtrl | ReflectProperty$ Produce | ColorOrType$ Type | Produced$ W | SpellDescription$ Add one mana of any type that a land you control could produce.".to_string()],
        );

        let white_id = game.create_card(white_land);
        let pool_id = game.create_card(reflecting_pool);
        game.move_card(white_id, ZoneType::Battlefield, p0);
        game.move_card(pool_id, ZoneType::Battlefield, p0);

        let mut white = HashSet::new();
        white.insert("white".to_string());
        assert!(game.card(white_id).can_produce_color_mana(&game, &white));
        assert!(game.card(pool_id).can_produce_color_mana(&game, &white));
    }

    #[test]
    fn can_produce_same_mana_type_with_uses_mana_ability_overlap() {
        let mut game = GameState::new(&["Alice", "Bob"], 20);
        let p0 = PlayerId(0);

        let island = Card::new(
            CardId(0),
            "Island Source".to_string(),
            p0,
            CardTypeLine::parse("Land"),
            ManaCost::parse(""),
            ColorSet::COLORLESS,
            None,
            None,
            vec![],
            vec!["AB$ Mana | Cost$ T | Produced$ U | SpellDescription$ Add {U}.".to_string()],
        );
        let prism = Card::new(
            CardId(1),
            "Prism".to_string(),
            p0,
            CardTypeLine::parse("Artifact"),
            ManaCost::parse("2"),
            ColorSet::COLORLESS,
            None,
            None,
            vec![],
            vec!["AB$ Mana | Cost$ T | Produced$ U | SpellDescription$ Add {U}.".to_string()],
        );

        let island_id = game.create_card(island);
        let prism_id = game.create_card(prism);
        game.move_card(island_id, ZoneType::Battlefield, p0);
        game.move_card(prism_id, ZoneType::Battlefield, p0);

        assert!(game
            .card(prism_id)
            .can_produce_same_mana_type_with(&game, game.card(island_id)));
    }
}