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
|
"use strict"
const data = require("./data.js")
function find_scenario(n) {
let ix = data.scenarios.findIndex(s => s.number === n)
if (ix < 0)
throw new Error("cannot find scenario " + n)
return ix
}
function find_card(s, n) {
let ix = data.cards.findIndex(c => c.scenario === s && c.name === n)
if (ix < 0)
throw new Error("cannot find card " + n)
return ix
}
for (let c of data.cards) for (let a of c.actions) console.log(a.type + ":", a.effect)
// for (let c of data.cards) console.log(c.dice)
// for (let c of data.cards) for (let a of c.actions) { if (a.type === "Counterattack") console.log(c.number, a.type, a.sequence, a.target) }
function check_attack_res(c, a) {
if (a.choice)
return
if (c.rules && c.rules["attack_reserve"])
return
if (c.rules && c.rules["ignore_reserve"])
return
let dead = []
let last = a.target_list[a.target_list.length-1]
for (let tid of a.target_list) {
let t = data.cards[tid]
if (!t.reserve) {
// all good, targetable out of reserve
dead.push(tid)
} else if (Array.isArray(t.reserve)) {
// in reserve
for (let rid of t.reserve)
if (!dead.includes(rid) && tid !== last)
console.log("BLOCK (RES)", c.scenario, c.number, a.target, "(" + t.name + ")", "\n\t" + c.rule_text_1)
dead.push(tid)
} else {
// commanded?
if (tid !== last)
console.log("BLOCK (CMD)", c.scenario, c.number, a.target, "(" + t.name + ")", "\n\t" + c.rule_text_1)
}
}
}
//for (let c of data.cards) for (let a of c.actions) { if (a.type === "Attack") check_attack_res(c, a) }
const P1 = "First"
const P2 = "Second"
var states = {}
var game = null
var view = null
const POOL = -1
const RED = 0
const PINK = 1
const BLUE = 2
const DKBLUE = 3
exports.scenarios = {
"": [ "Random" ],
}
const scenario_roles = {}
for (let s of data.scenarios) {
let id = s.number + " - " + s.name
let x = s.expansion
if (!(x in exports.scenarios)) {
exports.scenarios[""].push("Random - " + x)
exports.scenarios[x] = []
}
exports.scenarios[x].push(id)
scenario_roles[id] = [ s.players[0].name, s.players[1].name ]
}
exports.is_random_scenario = function (scenario) {
return scenario.startsWith("Random")
}
exports.select_random_scenario = function (scenario, seed) {
if (scenario === "Random") {
let info = data.scenarios[seed % data.scenarios.length]
return info.number + " - " + info.name
}
if (scenario.startsWith("Random - ")) {
let list = exports.scenarios[scenario.replace("Random - ", "")]
return list[seed % list.length]
}
return scenario
}
exports.roles = [ P1, P2 ]
exports.action = function (state, player, action, arg) {
game = state
let S = states[game.state]
if (action in S)
S[action](arg, player)
else if (action === "undo" && game.undo && game.undo.length > 0)
pop_undo()
else
throw new Error("Invalid action: " + action)
return game
}
exports.view = function (state, player) {
game = state
view = {
log: game.log,
prompt: null,
scenario: game.scenario,
dice: game.dice,
sticks: game.sticks,
cubes: game.cubes,
morale: game.morale,
tv1: get_tactical_victory_points(0),
tv2: get_tactical_victory_points(1),
front: game.front,
reserve: game.reserve,
selected: game.selected,
target: game.target,
hits: game.hits,
self: game.self,
}
if (game.target2 >= 0 && game.hits2 >= 0) {
view.target2 = game.target2
view.hits2 = game.hits2
}
if (game.shift)
view.shift = game.shift
if (game.state === "game_over") {
view.prompt = game.victory
} else if (player !== game.active) {
let inactive = states[game.state].inactive || game.state
view.prompt = `Waiting for ${player_name(player_index())} to ${inactive}.`
} else {
view.actions = {}
states[game.state].prompt()
if (game.undo && game.undo.length > 0)
view.actions.undo = 1
else
view.actions.undo = 0
}
return view
}
exports.resign = function (state, player) {
game = state
if (game.state !== 'game_over') {
if (player === P1)
return goto_game_over(P2, player_name(0) + " resigned.")
if (player === P2)
return goto_game_over(P1, player_name(1) + " resigned.")
}
}
function goto_game_over(result, victory) {
if (result === P1)
victory = player_name(0) + " won:\n" + victory
else if (result === P2)
victory = player_name(1) + " won:\n" + victory
else
victory = result + ":\n" + victory
game.state = "game_over"
game.active = "None"
game.result = result
game.victory = victory
log("")
log(victory)
return true
}
states.game_over = {
prompt() {
view.prompt = game.victory
},
}
// === SPECIAL RULES - CARD NUMBERS ===
const S2_MARSTON_MOOR = find_scenario(2)
const S2_RUPERTS_LIFEGUARD = find_card(2, "Rupert's Lifeguard")
const S2_NORTHERN_HORSE = find_card(2, "Northern Horse")
const S2_TILLIERS_LEFT = find_card(2, "Tillier's Left")
const S2_TILLIERS_RIGHT = find_card(2, "Tillier's Right")
const S2_BYRON = find_card(2, "Byron")
const S4_BOSWORTH_FIELD = find_scenario(4)
const S4_THE_STANLEYS = find_card(4, "The Stanleys")
const S4_NORTHUMBERLAND = find_card(4, "Northumberland")
const S7_THE_DUNES = find_scenario(7)
const S7_THE_ENGLISH_FLEET = find_card(7, "The English Fleet")
const S7_DON_JUAN_JOSE = find_card(7, "Don Juan Jose")
const S7_SPANISH_RIGHT_CAVALRY = find_card(7, "Spanish Right Cavalry")
const S8_BROOKLYN_HEIGHTS = find_scenario(8)
const S8_CLINTON = find_card(8, "Clinton")
const S8_GRANT = find_card(8, "Grant")
const S8_HESSIANS = find_card(8, "Hessians")
const S36_FOURTH_LINE = find_card(36, "The Fourth Line")
const S3201_GAINES_MILL = find_scenario(3201)
const S3201_JACKSON = find_card(3201, "Jackson")
const S3201_DH_HILL = find_card(3201, "D.H. Hill")
const S3201_AP_HILL = find_card(3201, "A.P. Hill")
const S3201_LONGSTREET = find_card(3201, "Longstreet")
const S9_ST_ALBANS = find_scenario(9)
const S9_HENRY_VI = find_card(9, "Henry VI")
const S9_SHROPSHIRE_LANE = find_card(9, "Shropshire Lane")
const S9_SOPWELL_LANE = find_card(9, "Sopwell Lane")
const S9_ARCHERS = find_card(9, "Archers")
const S11_MORTIMERS_CROSS = find_scenario(11)
const S12_TOWTON = find_scenario(12)
const S13_EDGECOTE_MOOR = find_scenario(13)
const S15_TEWKESBURY = find_scenario(15)
const S15_A_PLUMP_OF_SPEARS = find_card(15, "A Plump of Spears")
const S15_SOMERSET = find_card(15, "Somerset")
const S15_WENLOCK = find_card(15, "Wenlock")
const S14_BARNET = find_scenario(14)
const S14_TREASON = find_card(14, "\"Treason!\"")
const S16_STOKE_FIELD = find_scenario(16)
const S22_GABIENE = find_scenario(22)
const S22_EUMENES_CAMP = find_card(22, "Eumenes's Camp")
const S22_SILVER_SHIELDS = find_card(22, "The Silver Shields")
const S25_WHEATFIELD = find_scenario(25)
const S25_STONY_HILL = find_card(25, "Stony Hill")
const S25_WOFFORD = find_card(25, "Wofford")
const S25_ZOOK = find_card(25, "Zook")
const S25_KELLY = find_card(25, "Kelly")
const S26_PEACH_ORCHARD = find_scenario(26)
const S26_FATAL_BLUNDER = find_card(26, "Fatal Blunder")
const S28_CULPS_HILL = find_scenario(28)
const S28_BREASTWORKS = find_card(28, "Breastworks")
const S28_GEARY = find_card(28, "Geary")
const S29_GETTYS_2ND = find_scenario(29)
const S29_MCLAWS = find_card(29, "McLaws")
const S29_ANDERSON = find_card(29, "Anderson")
const S29_HOOD = find_card(29, "Hood")
const S29_EARLY = find_card(29, "Early")
const S29_JOHNSON = find_card(29, "Johnson")
const S29_MEADE = find_card(29, "Meade")
const S29_LITTLE_ROUND_TOP = find_card(29, "Little Round Top")
const S30_EDGEHILL = find_scenario(30)
const S30_BALFOUR = find_card(30, "Balfour")
const S30_STAPLETON = find_card(30, "Stapleton")
const S30_RUPERT = find_card(30, "Rupert of the Rhine")
const S30_WILMOT = find_card(30, "Wilmot")
const S30_ESSEX = find_card(30, "Charles Essex")
const S31_NEWBURY_1ST = find_scenario(31)
const S31_BYRON = find_card(31, "Byron")
const S31_SKIPPON = find_card(31, "Skippon")
const S31_WENTWORTH = find_card(31, "Wentworth")
const S31_GERARD = find_card(31, "Gerard")
const S34_TULLIBARDINE = find_card(34, "Tullibardine")
const S35_AULDEARN = find_scenario(35)
const S35_MONTROSE = find_card(35, "Montrose")
const S35_GORDON = find_card(35, "Gordon")
const S37_INKERMAN = find_scenario(37)
const S37_PAULOFFS_LEFT = find_card(37, "Pauloff's Left")
const S37_BRITISH_TROOPS = find_card(37, "British Troops")
const S37_FRENCH_TROOPS = find_card(37, "French Troops")
const S37_SOIMONOFF = find_card(37, "Soimonoff")
const S37_THE_FOG = find_card(37, "The Fog")
const S38_FLEURUS = find_scenario(38)
const S38_WALDECK = find_card(38, "Waldeck")
const S38_RETREAT_TO_NIVELLES = find_card(38, "Retreat to Nivelles")
const S38_LUXEMBOURGS_HORSE = find_card(38, "Luxembourg's Horse")
const S38_GOURNAYS_HORSE = find_card(38, "Gournay's Horse")
const S38_DUTCH_LEFT_FOOT = find_card(38, "Dutch Left Foot")
const S38_DUTCH_HORSE = find_card(38, "Dutch Horse")
const S39_MARSAGLIA = find_scenario(39)
const S39_CANNONS = find_card(39, "Cannons")
const S39_EUGENE = find_card(39, "Eugene")
const S39_DUKE_OF_SAVOY = find_card(39, "Duke of Savoy")
const S39_BAYONETS = find_card(39, "Bayonets!")
const S39_CATINAT = find_card(39, "Catinat")
const S39_HOGUETTE = find_card(39, "Hoguette")
const S40_CHIARI = find_scenario(40)
const S40_CASSINES_I = find_card(40, "Cassines I")
const S40_NIGRELLI = find_card(40, "Nigrelli")
const S40_KRIECHBAUM = find_card(40, "Kriechbaum")
const S40_CASSINES_II = find_card(40, "Cassines II")
const S40_MANNSFELDT = find_card(40, "Mannsfeldt")
const S40_GUTTENSTEIN = find_card(40, "Guttenstein")
const S41_BLENHEIM_SCENARIO = find_scenario(41)
const S41_CLERAMBAULT = find_card(41, "Clerambault")
const S41_BLENHEIM_CARD = find_card(41, "Blenheim")
const S41_PRINCE_EUGENE = find_card(41, "Prince Eugene")
const S42_RAMILLIES = find_scenario(42)
const S42_MARLBOROUGH = find_card(42, "Marlborough")
const S42_DUTCH_GUARDS = find_card(42, "Dutch Guards")
const S43_DENAIN = find_scenario(43)
const S43_DUTCH_HORSE = find_card(43, "Dutch Horse")
const S43_VILLARS_LEFT = find_card(43, "Villars's Left")
const S43_BROGLIE = find_card(43, "Broglie")
const S43_PRINCE_DE_TINGRY = find_card(43, "Prince de Tingry")
const S44_HOHENFRIEDBERG = find_scenario(44)
const S44_CHARLES = find_card(44, "Charles")
const S44_FREDERICK_II = find_card(44, "Frederick II")
const S44_BAYREUTH_DRAGOONS = find_card(44, "Bayreuth Dragoons")
const S44_LEOPOLDS_L = find_card(44, "Leopold's Left")
const S44_LEOPOLDS_C = find_card(44, "Leopold's Center")
const S44_LEOPOLDS_R = find_card(44, "Leopold's Right")
const S44_DU_MOULIN = find_card(44, "Du Moulin")
const S44_SAXON_HORSE = find_card(44, "Saxon Horse")
const S45_SOOR = find_scenario(45)
const S45_AUSTRIAN_GUNS = find_card(45, "Austrian Guns")
const S45_CUIRASSIERS = find_card(45, "Cuirassiers")
const S46_ROCOUX = find_scenario(46)
const S46_AUSTRIANS = find_card(46, "Austrians")
const S46_THE_MOUTH_OF_HELL = find_card(46, "The Mouth of Hell")
const S47_PRAGUE = find_scenario(47)
const S47_BROWNE = find_card(47, "Browne")
const S47_SCHWERIN = find_card(47, "Schwerin")
const S47_CHARLES_LORRAINE = find_card(47, "Charles Lorraine")
const S48_BRESLAU = find_scenario(48)
const S48_AUSTRIAN_GUNS = find_card(48, "Austrian Guns")
const S48_PRUSSIAN_GUNS = find_card(48, "Prussian Guns")
const S48_BEVERN = find_card(48, "Bevern")
const S48_GRENZERS = find_card(48, "Grenzers")
const S49_LEUTHEN = find_scenario(49)
const S49_RETZOW = find_card(49, "Retzow")
const S49_COLLOREDO = find_card(49, "Colloredo")
const S49_NADASDY = find_card(49, "Nadasdy")
const S49_BORNE = find_card(49, "Borne")
const S49_FEINT = find_card(49, "Feint")
const S49_POOR_CHARLES = find_card(49, "Poor Charles :-(")
const S49_THE_LEUTHEN_CHORALE = find_card(49, "The Leuthen Chorale")
// === SETUP ===
exports.setup = function (seed, scenario, options) {
scenario = parseInt(scenario)
scenario = data.scenarios.findIndex(s => s.number === scenario)
if (scenario < 0)
throw Error("cannot find scenario: " + scenario)
let info = data.scenarios[scenario]
game = {
seed: seed,
scenario: scenario,
log: [],
undo: [],
active: P1,
state: "roll",
reacted: -1,
// dice value and position
dice: [
0, POOL, 0, POOL, 0, POOL, 0, POOL, 0, POOL, 0, POOL,
0, POOL, 0, POOL, 0, POOL, 0, POOL, 0, POOL, 0, POOL,
],
// sticks (map normal formation -> count)
sticks: [],
// cubes (map special formation -> count)
cubes: [],
morale: [ info.players[0].morale, info.players[1].morale ],
lost: [ 0, 0 ],
front: [ [], [], ],
reserve: [ [], [] ],
// dice value placed on what card
rolled: 0,
placed: [],
summary: 0,
// current action
routed: [ 0, 0 ],
selected: -1,
target: -1,
hits: 0,
self: 0,
// for breastworks etc
self2: 0,
target2: -1,
hits2: 0,
}
// Charles Alexander of Lorraine -- shift special
if (info.number >= 44 && info.number <= 49)
game.shift = []
function setup_formation(front, reserve, c) {
let card = data.cards[c]
if (card.reserve)
set_add(reserve, c)
else
set_add(front, c)
if (card.special) {
if (card_has_rule(c, "start_with_no_cubes"))
add_cubes(c, 0)
else
add_cubes(c, 1)
} else {
set_sticks(c, card.strength)
}
}
for (let p = 0; p < 2; ++p) {
for (let c of info.players[p].cards)
setup_formation(game.front[p], game.reserve[p], c)
}
log(".h1 " + info.name)
log(".h2 " + info.date)
log("")
if (info.players[0].tactical > 0 || info.players[1].tactical > 0) {
if (game.scenario === S38_FLEURUS)
log("Tactical Draw:")
else
log("Tactical Victory:")
if (info.players[0].tactical > 0)
log(">" + player_name(0) + ": " + info.players[0].tactical)
if (info.players[1].tactical > 0)
log(">" + player_name(1) + ": " + info.players[1].tactical)
log("")
}
if (info.lore_text) {
for (let line of info.lore_text.split("<p>"))
log(".lore " + line)
log("")
}
if (info.rule_text) {
for (let line of info.rule_text.split("<p>"))
log(".rule " + line)
log("")
}
if (game.scenario === S37_INKERMAN) {
map_set(game.cubes, S37_THE_FOG, 3)
}
if (game.scenario === S38_FLEURUS) {
map_set(game.cubes, S38_RETREAT_TO_NIVELLES, 3)
}
goto_start_turn()
return game
}
// === GAME STATE ACCESSORS ===
function count_total_cubes() {
let n = game.morale[0] + game.morale[1]
for (let i = 1; i < game.cubes.length; i += 2)
n += game.cubes[i]
return n
}
function is_infantry(c) {
return !!data.cards[c].infantry
}
function is_cavalry(c) {
return !!data.cards[c].cavalry
}
function card_has_rule(c, name) {
let rules = data.cards[c].rules
if (rules)
return rules[name]
return false
}
function card_name(c) {
return data.cards[c].name
}
function player_name(p) {
return data.scenarios[game.scenario].players[p].name
}
function set_opponent_active() {
if (game.active === P1)
game.active = P2
else
game.active = P1
}
function player_index() {
if (game.active === P1)
return 0
return 1
}
function get_cubes(c) {
return map_get(game.cubes, c, 0)
}
function add_cubes(c, n) {
let limit = data.cards[c].special
let old = get_cubes(c)
map_set(game.cubes, c, Math.min(limit, old + n))
}
function remove_cubes(c, n) {
let old = get_cubes(c)
map_set(game.cubes, c, Math.max(0, old - n))
}
function get_sticks(c) {
return map_get(game.sticks, c, 0)
}
function set_sticks(c, n) {
map_set(game.sticks, c, n)
}
function move_sticks(from, to, n) {
// log(`Moved ${n} sticks from C${from} to C${to}.`) // TODO?
set_sticks(from, get_sticks(from) - n)
set_sticks(to, get_sticks(to) + n)
}
function move_all_sticks(from, to) {
move_sticks(from, to, get_sticks(from))
}
function get_shift_sticks(c) {
if (game.shift)
return map_get(game.shift, c, 0)
return 0
}
function set_shift_sticks(c, n) {
if (game.shift) {
if (n)
map_set(game.shift, c, n)
else
map_delete(game.shift, c)
}
}
function remove_sticks(c, n) {
let p = find_card_owner(c)
let old = get_sticks(c)
n = Math.min(n, old)
game.lost[p] += n
set_sticks(c, old - n)
}
function remove_dice(c) {
for (let i = 0; i < 12; ++i) {
if (get_dice_location(i) === c) {
set_dice_location(i, POOL)
set_dice_value(i, 0)
}
}
}
function take_all_dice(from, to) {
log("Dice from C" + from)
for (let i = 0; i < 12; ++i) {
if (get_dice_location(i) === from) {
set_dice_location(i, to)
}
}
}
function take_one_die(from, to) {
log("Die from C" + from)
for (let i = 0; i < 12; ++i) {
if (get_dice_location(i) === from) {
set_dice_location(i, to)
if (to === POOL)
set_dice_value(i, 0)
to = POOL
}
}
}
function take_wild_die(from, to) {
log("Wild die from C" + from)
for (let i = 0; i < 12; ++i) {
if (get_dice_location(i) === from) {
set_dice_location(i, to)
set_dice_value(i, 0)
to = POOL
}
}
}
function eliminate_card(c) {
remove_dice(c)
map_delete(game.cubes, c)
map_delete(game.sticks, c)
set_delete(game.front[0], c)
set_delete(game.front[1], c)
set_delete(game.reserve[0], c)
set_delete(game.reserve[1], c)
}
function rout_card(c) {
let p = find_card_owner(c)
game.lost[p] += get_sticks(c)
game.lost[p] += get_shift_sticks(c)
log("C" + c + " routed.")
eliminate_card(c)
}
function pursue_card(c) {
let p = find_card_owner(c)
game.lost[p] += get_shift_sticks(c) // TODO ?
eliminate_card(c)
}
function retire_card(c) {
let p = find_card_owner(c)
game.lost[p] += get_shift_sticks(c) // TODO ?
eliminate_card(c)
}
function remove_card(c) {
let p = find_card_owner(c)
log("C" + c + " removed.")
game.lost[p] += get_shift_sticks(c) // TODO ?
if (game.scenario === S49_LEUTHEN) {
if (c === S49_BORNE)
game.lost[0] += get_sticks(S49_BORNE)
}
eliminate_card(c)
}
function pay_for_action(c) {
if (game.scenario === S46_ROCOUX) {
if (c === S46_THE_MOUTH_OF_HELL) {
// icky test for second reaction...
if (game.state === "screen") {
remove_cubes(c, 2)
return
}
}
}
if (data.cards[c].special)
remove_cubes(c, 1)
else
remove_dice(c)
}
function get_dice_value(d) {
return game.dice[d * 2]
}
function get_dice_location(i) {
return game.dice[i * 2 + 1]
}
function set_dice_location(d, v) {
game.dice[d * 2 + 1] = v
}
function set_dice_value(d, v) {
game.dice[d * 2] = v
}
// === HOW TO WIN ===
function is_card_in_play(c) {
return (
set_has(game.front[0], c) ||
set_has(game.front[1], c)
)
}
function is_card_in_reserve(c) {
return (
set_has(game.reserve[0], c) ||
set_has(game.reserve[1], c)
)
}
function is_card_in_play_or_reserve(c) {
return (
set_has(game.front[0], c) ||
set_has(game.front[1], c) ||
set_has(game.reserve[0], c) ||
set_has(game.reserve[1], c)
)
}
function is_removed_from_play(c) {
return !is_card_in_play_or_reserve(c)
}
function card_has_active_link(c) {
// Ignores Link.
if (game.scenario === S48_BRESLAU) {
if (game.selected === S48_GRENZERS)
return false
}
let link = data.cards[c].link
if (link) {
for (let t of link)
if (is_card_in_play(t))
return true
}
return false
}
function card_has_attack_with_valid_target(c) {
for (let a of data.cards[c].actions) {
if (a.type === "Attack") {
let attack_reserve = card_has_rule(c, "attack_reserve")
for (let t of a.target_list) {
if (is_card_in_play(t))
return true
if (attack_reserve && is_card_in_reserve(t))
return true
}
}
}
return false
}
function is_impossible_to_attack() {
let p = player_index()
for (let c of game.front[p])
if (card_has_attack_with_valid_target(c))
return false
return true
}
function check_impossible_to_attack_victory() {
if (is_impossible_to_attack()) {
if (player_index() === 0)
return goto_game_over(P2, player_name(0) + " has no more attacks!")
else
return goto_game_over(P1, player_name(1) + " has no more attacks!")
}
return false
}
function get_tactical_victory_points(p) {
let n = game.lost[1-p]
if (game.scenario === S46_ROCOUX) {
if (p === 0)
n += get_sticks(S46_AUSTRIANS)
}
if (game.scenario === S47_PRAGUE) {
if (p === 1)
n += get_sticks(S47_CHARLES_LORRAINE)
}
return n
}
function check_victory() {
let info = data.scenarios[game.scenario]
if (game.scenario === S39_MARSAGLIA) {
if (is_removed_from_play(S39_HOGUETTE) && is_removed_from_play(S39_CATINAT))
return goto_game_over(P1, player_name(1) + " lost both linked formations!")
if (is_removed_from_play(S39_DUKE_OF_SAVOY) && is_removed_from_play(S39_EUGENE))
return goto_game_over(P2, player_name(0) + " lost both linked formations!")
}
if (game.scenario === S43_DENAIN) {
if (is_removed_from_play(S43_PRINCE_DE_TINGRY))
return goto_game_over(P1, "Eugene is able to cross!")
}
if (game.scenario === S49_LEUTHEN) {
if (is_removed_from_play(S49_NADASDY) && is_card_in_reserve(S49_COLLOREDO))
return goto_game_over(P1, "Nadasdy routed before Colloredo entered play!")
}
if (game.morale[0] === 0)
return goto_game_over(P2, player_name(0) + " ran out of morale!")
if (game.morale[1] === 0)
return goto_game_over(P1, player_name(1) + " ran out of morale!")
let tv0 = get_tactical_victory_points(0)
let tv1 = get_tactical_victory_points(1)
if (game.scenario === S38_FLEURUS) {
if (tv1 >= 22)
return goto_game_over("Draw", player_name(1) + " secured a draw!")
}
if (info.players[0].tactical > 0 && tv0 >= info.players[0].tactical)
return goto_game_over(P1, player_name(0) + " won a tactical victory!")
if (info.players[1].tactical > 0 && tv1 >= info.players[1].tactical)
return goto_game_over(P2, player_name(1) + " won a tactical victory!")
return false
}
// === ROLL PHASE ===
function is_pool_die(i, v) {
let p = player_index()
return get_dice_location(p * 6 + i) < 0 && get_dice_value(p * 6 + i) === v
}
function is_pool_die_range(i, lo, hi) {
let p = player_index()
if (get_dice_location(p * 6 + i) < 0) {
let v = get_dice_value(p * 6 + i)
return v >= lo && v <= hi
}
return false
}
function placed_any_dice() {
return game.placed.length > 0
}
function placed_any_dice_on_wing(w) {
for (let i = 0; i < game.placed.length; i += 2) {
let c = game.placed[i]
if (data.cards[c].wing === w)
return true
}
return false
}
function is_straight_4_or_3(_) {
if (game.scenario === S28_CULPS_HILL) {
if (game.rolled >= 5)
return 4
else
return 3
}
if (game.scenario === S31_NEWBURY_1ST) {
if (is_card_in_play(S31_SKIPPON))
return 4
else
return 3
}
if (game.scenario === S48_BRESLAU) {
if (game.rolled >= 5)
return 4
else
return 3
}
throw new Error("Missing rule for Straight 3/4 choice")
}
const place_dice_once = {
"(1)": true,
"(2)": true,
"(3)": true,
"(4)": true,
"(5)": true,
"(6)": true,
"(1)/(2)": true,
"(1-3)": true,
"(1-4)": true,
"(1-5)": true,
"(2)/(3)": true,
"(2-4)": true,
"(2-5)": true,
"(2-6)": true,
"(3)/(4)": true,
"(3-5)": true,
"(3-6)": true,
"(4)/(5)": true,
"(4-6)": true,
"(5)/(6)": true,
}
const place_dice_check = {
"Full House": check_full_house,
"Straight 4/3": check_straight_4_or_3,
"Straight 3": check_straight_3,
"Straight 4": check_straight_4,
"Doubles": check_doubles,
"Triples": check_triples,
"1": (c) => check_single(c, 1),
"2": (c) => check_single(c, 2),
"3": (c) => check_single(c, 3),
"4": (c) => check_single(c, 4),
"5": (c) => check_single(c, 5),
"6": (c) => check_single(c, 6),
"(1)": (c) => check_single(c, 1),
"(2)": (c) => check_single(c, 2),
"(3)": (c) => check_single(c, 3),
"(4)": (c) => check_single(c, 4),
"(5)": (c) => check_single(c, 5),
"(6)": (c) => check_single(c, 6),
"Any": (c) => check_range(c, 1, 6),
"1/2": (c) => check_range(c, 1, 2),
"1-3": (c) => check_range(c, 1, 3),
"1/2/3": (c) => check_range(c, 1, 3),
"1-4": (c) => check_range(c, 1, 4),
"1-5": (c) => check_range(c, 1, 5),
"2/3": (c) => check_range(c, 2, 2),
"2-4": (c) => check_range(c, 2, 4),
"2/3/4": (c) => check_range(c, 2, 4),
"2-5": (c) => check_range(c, 2, 5),
"2-6": (c) => check_range(c, 2, 6),
"3/4": (c) => check_range(c, 3, 4),
"3-5": (c) => check_range(c, 3, 5),
"3-6": (c) => check_range(c, 3, 6),
"4/5": (c) => check_range(c, 4, 5),
"4-6": (c) => check_range(c, 4, 6),
"4/5/6": (c) => check_range(c, 4, 6),
"5/6": (c) => check_range(c, 5, 6),
"(1)/(2)": (c) => check_range(c, 1, 2),
"(1-3)": (c) => check_range(c, 1, 3),
"(1-4)": (c) => check_range(c, 1, 4),
"(1-5)": (c) => check_range(c, 1, 5),
"(2)/(3)": (c) => check_range(c, 2, 2),
"(2-4)": (c) => check_range(c, 2, 4),
"(2-5)": (c) => check_range(c, 2, 5),
"(2-6)": (c) => check_range(c, 2, 6),
"(3)/(4)": (c) => check_range(c, 3, 4),
"(3-5)": (c) => check_range(c, 3, 5),
"(3-6)": (c) => check_range(c, 3, 6),
"(4)/(5)": (c) => check_range(c, 4, 5),
"(4-6)": (c) => check_range(c, 4, 6),
"(5)/(6)": (c) => check_range(c, 5, 6),
}
const place_dice_gen = {
"Full House": gen_full_house,
"Straight 4/3": gen_straight_4_or_3,
"Straight 3": gen_straight_3,
"Straight 4": gen_straight_4,
"Doubles": gen_doubles,
"Triples": gen_triples,
"1": (c) => gen_single(c, 1),
"2": (c) => gen_single(c, 2),
"3": (c) => gen_single(c, 3),
"4": (c) => gen_single(c, 4),
"5": (c) => gen_single(c, 5),
"6": (c) => gen_single(c, 6),
"(1)": (c) => gen_single(c, 1),
"(2)": (c) => gen_single(c, 2),
"(3)": (c) => gen_single(c, 3),
"(4)": (c) => gen_single(c, 4),
"(5)": (c) => gen_single(c, 5),
"(6)": (c) => gen_single(c, 6),
"Any": (c) => gen_range(c, 1, 6),
"1/2": (c) => gen_range(c, 1, 2),
"1-3": (c) => gen_range(c, 1, 3),
"1/2/3": (c) => gen_range(c, 1, 3),
"1-4": (c) => gen_range(c, 1, 4),
"1-5": (c) => gen_range(c, 1, 5),
"2/3": (c) => gen_range(c, 2, 2),
"2-4": (c) => gen_range(c, 2, 4),
"2/3/4": (c) => gen_range(c, 2, 4),
"2-5": (c) => gen_range(c, 2, 5),
"2-6": (c) => gen_range(c, 2, 6),
"3/4": (c) => gen_range(c, 3, 4),
"3-5": (c) => gen_range(c, 3, 5),
"3/4/5": (c) => gen_range(c, 3, 5),
"3-6": (c) => gen_range(c, 3, 6),
"4/5": (c) => gen_range(c, 4, 5),
"4-6": (c) => gen_range(c, 4, 6),
"4/5/6": (c) => gen_range(c, 4, 6),
"5/6": (c) => gen_range(c, 5, 6),
"(1)/(2)": (c) => gen_range(c, 1, 2),
"(1-3)": (c) => gen_range(c, 1, 3),
"(1-4)": (c) => gen_range(c, 1, 4),
"(1-5)": (c) => gen_range(c, 1, 5),
"(2)/(3)": (c) => gen_range(c, 2, 2),
"(2-4)": (c) => gen_range(c, 2, 4),
"(2-5)": (c) => gen_range(c, 2, 5),
"(2-6)": (c) => gen_range(c, 2, 6),
"(3)/(4)": (c) => gen_range(c, 3, 4),
"(3-5)": (c) => gen_range(c, 3, 5),
"(3-6)": (c) => gen_range(c, 3, 6),
"(4)/(5)": (c) => gen_range(c, 4, 5),
"(4-6)": (c) => gen_range(c, 4, 6),
"(5)/(6)": (c) => gen_range(c, 5, 6),
}
const place_dice_take = {
"Full House": take_full_house,
"Straight 4/3": take_straight_4_or_3,
"Straight 3": take_straight_3,
"Straight 4": take_straight_4,
"Doubles": take_doubles,
"Triples": take_triples,
"1": take_single,
"2": take_single,
"3": take_single,
"4": take_single,
"5": take_single,
"6": take_single,
"(1)": take_single,
"(2)": take_single,
"(3)": take_single,
"(4)": take_single,
"(5)": take_single,
"(6)": take_single,
"Any": (c, d) => take_single(c, d),
"1/2": (c, d) => take_single(c, d),
"1-3": (c, d) => take_single(c, d),
"1/2/3": (c, d) => take_single(c, d),
"1-4": (c, d) => take_single(c, d),
"1-5": (c, d) => take_single(c, d),
"2/3": (c, d) => take_single(c, d),
"2-4": (c, d) => take_single(c, d),
"2/3/4": (c, d) => take_single(c, d),
"2-5": (c, d) => take_single(c, d),
"2-6": (c, d) => take_single(c, d),
"3/4": (c, d) => take_single(c, d),
"3-5": (c, d) => take_single(c, d),
"3/4/5": (c, d) => take_single(c, d),
"3-6": (c, d) => take_single(c, d),
"4/5": (c, d) => take_single(c, d),
"4-6": (c, d) => take_single(c, d),
"4/5/6": (c, d) => take_single(c, d),
"5/6": (c, d) => take_single(c, d),
"(1)/(2)": (c, d) => take_single(c, d),
"(1-3)": (c, d) => take_single(c, d),
"(1-4)": (c, d) => take_single(c, d),
"(1-5)": (c, d) => take_single(c, d),
"(2)/(3)": (c, d) => take_single(c, d),
"(2-4)": (c, d) => take_single(c, d),
"(2-5)": (c, d) => take_single(c, d),
"(2-6)": (c, d) => take_single(c, d),
"(3)/(4)": (c, d) => take_single(c, d),
"(3-5)": (c, d) => take_single(c, d),
"(3-6)": (c, d) => take_single(c, d),
"(4)/(5)": (c, d) => take_single(c, d),
"(4-6)": (c, d) => take_single(c, d),
"(5)/(6)": (c, d) => take_single(c, d),
}
function can_place_dice(c) {
let pattern = data.cards[c].dice
if (!pattern)
return false
let pred = place_dice_check[pattern]
if (!pred)
throw Error("bad pattern definition: " + pattern)
// At per card limit?
if (place_dice_once[pattern]) {
if (map_has(game.placed, c))
return false
}
// At cube limit?
if (data.cards[c].special) {
// Max on card
if (get_cubes(c) >= data.cards[c].special)
return false
// Max available
let n_cubes = count_total_cubes()
if (n_cubes >= 10)
return false
if (game.scenario === S12_TOWTON) {
if (n_cubes >= 8)
return false
}
}
// At per wing limit?
let wing = data.cards[c].wing
let n_wing = 0
for (let i = 0; i < game.placed.length; i += 2) {
let x = game.placed[i]
if (x !== c) {
let i_wing = data.cards[x].wing
if (i_wing === wing)
n_wing ++
}
}
if (n_wing >= game.place_max[wing])
return false
if (game.scenario === S8_BROOKLYN_HEIGHTS) {
if (c === S8_CLINTON) {
if (is_removed_from_play(S8_GRANT))
return false
if (is_removed_from_play(S8_HESSIANS))
return false
}
}
if (game.scenario === S3201_GAINES_MILL) {
if (c === S3201_JACKSON) {
if (!has_any_dice_on_card(S3201_DH_HILL))
return false
}
}
if (game.scenario === S29_GETTYS_2ND) {
if (c === S29_MCLAWS) {
if (!has_any_dice_on_card(S29_HOOD))
return false
}
if (c === S29_ANDERSON) {
if (!has_any_dice_on_card(S29_MCLAWS))
return false
}
if (c === S29_EARLY || c === S29_JOHNSON) {
let red = 0
if (has_any_dice_on_card(S29_HOOD)) ++red
if (has_any_dice_on_card(S29_MCLAWS)) ++red
if (has_any_dice_on_card(S29_ANDERSON)) ++red
if (red < 2)
return false
}
}
return pred(c)
}
function can_place_value(c, v) {
let old_v = map_get(game.placed, c, 0)
return old_v === 0 || old_v === v
}
function pool_has_single(v) {
for (let i = 0; i < 6; ++i)
if (is_pool_die(i, v))
return true
return false
}
function check_single_count(c, v, x) {
if (!can_place_value(c, v))
return false
let n = 0
for (let i = 0; i < 6; ++i)
if (is_pool_die(i, v) && ++n >= x)
return true
return false
}
function check_single(c, v) {
if (!can_place_value(c, v))
return false
for (let i = 0; i < 6; ++i)
if (is_pool_die(i, v))
return true
return false
}
function check_range(c, lo, hi) {
let old_v = map_get(game.placed, c, 0)
if (old_v > 0)
return pool_has_single(old_v)
for (let i = 0; i < 6; ++i)
if (is_pool_die_range(i, lo, hi))
return true
return false
}
function check_all_3(c, x, y, z) {
if (!can_place_value(c, x))
return false
return pool_has_single(x) && pool_has_single(y) && pool_has_single(z)
}
function check_all_4(c, x, y, z, w) {
if (!can_place_value(c, x))
return false
return pool_has_single(x) && pool_has_single(y) && pool_has_single(z) && pool_has_single(w)
}
function check_straight_4_or_3(c) {
if (is_straight_4_or_3(c) === 4)
return check_straight_4(c)
else
return check_straight_3(c)
}
function check_straight_3(c) {
return (
check_all_3(c, 1, 2, 3) ||
check_all_3(c, 2, 3, 4) ||
check_all_3(c, 3, 4, 5) ||
check_all_3(c, 4, 5, 6)
)
}
function check_straight_4(c) {
return (
check_all_4(c, 1, 2, 3, 4) ||
check_all_4(c, 2, 3, 4, 5) ||
check_all_4(c, 3, 4, 5, 6)
)
}
function check_doubles(c) {
return (
check_single_count(c, 1, 2) ||
check_single_count(c, 2, 2) ||
check_single_count(c, 3, 2) ||
check_single_count(c, 4, 2) ||
check_single_count(c, 5, 2) ||
check_single_count(c, 6, 2)
)
}
function check_triples(c) {
return (
check_single_count(c, 1, 3) ||
check_single_count(c, 2, 3) ||
check_single_count(c, 3, 3) ||
check_single_count(c, 4, 3) ||
check_single_count(c, 5, 3) ||
check_single_count(c, 6, 3)
)
}
function check_full_house(c) {
for (let x = 1; x <= 6; ++x) {
for (let y = 1; y <= 6; ++y) {
if (x !== y) {
if (check_single_count(c, x, 3) && check_single_count(c, y, 2))
return true
}
}
}
return false
}
function gen_pool_die(v) {
let p = player_index()
for (let i = 0; i < 6; ++i)
if (get_dice_location(p * 6 + i) < 0 && get_dice_value(p * 6 + i) === v)
gen_action_die(p * 6 + i)
}
function gen_single(c, v) {
if (!can_place_value(c, v))
return false
gen_pool_die(v)
}
function gen_range(c, lo, hi) {
for (let v = lo; v <= hi; ++v)
gen_single(c, v)
}
function gen_straight_4_or_3(c) {
if (is_straight_4_or_3(c) === 4)
gen_straight_4(c)
else
gen_straight_3(c)
}
function gen_straight_3(c) {
if (check_all_3(c, 1, 2, 3))
gen_pool_die(1)
if (check_all_3(c, 2, 3, 4))
gen_pool_die(2)
if (check_all_3(c, 3, 4, 5))
gen_pool_die(3)
if (check_all_3(c, 4, 5, 6))
gen_pool_die(4)
}
function gen_straight_4(c) {
if (check_all_4(c, 1, 2, 3, 4))
gen_pool_die(1)
if (check_all_4(c, 2, 3, 4, 5))
gen_pool_die(2)
if (check_all_4(c, 3, 4, 5, 6))
gen_pool_die(3)
}
function gen_doubles(c) {
for (let v = 1; v <= 6; ++v)
if (check_single_count(c, v, 2))
gen_pool_die(v)
}
function gen_triples(c) {
for (let v = 1; v <= 6; ++v)
if (check_single_count(c, v, 3))
gen_pool_die(v)
}
function gen_full_house(c) {
for (let x = 1; x <= 6; ++x) {
for (let y = 1; y <= 6; ++y) {
if (x !== y) {
if (check_single_count(c, x, 3) && check_single_count(c, y, 2))
gen_pool_die(x)
}
}
}
}
function find_and_take_single(c, v) {
let p = player_index()
for (let i = 0; i < 6; ++i) {
let d = p * 6 + i
if (get_dice_location(d) < 0 && get_dice_value(d) === v) {
set_dice_location(d, c)
game.summary |= (1 << d)
return
}
}
throw new Error("cannot find die of value " + v)
}
function take_single(c, d) {
set_dice_location(d, c)
game.summary |= (1 << d)
map_set(game.placed, c, get_dice_value(d))
}
function take_doubles(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v)
}
function take_triples(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v)
find_and_take_single(c, v)
}
function take_full_house(c, d) {
let x = get_dice_value(d)
for (let y = 1; y <= 6; ++y) {
if (x !== y) {
if (check_single_count(c, x, 3) && check_single_count(c, y, 2)) {
find_and_take_single(c, x)
find_and_take_single(c, x)
find_and_take_single(c, x)
find_and_take_single(c, y)
find_and_take_single(c, y)
}
}
}
}
function take_straight_4_or_3(c, d) {
if (is_straight_4_or_3(c) === 4)
take_straight_4(c, d)
else
take_straight_3(c, d)
}
function take_straight_3(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v+1)
find_and_take_single(c, v+2)
}
function take_straight_4(c, d) {
let v = get_dice_value(d)
take_single(c, d)
find_and_take_single(c, v+1)
find_and_take_single(c, v+2)
find_and_take_single(c, v+3)
}
function goto_roll_phase() {
game.selected = -1
game.target = -1
game.target2 = -1
game.action = 0
game.state = "roll"
game.place_max = [ 1, 1, 1, 1 ]
let p = player_index()
for (let c of game.front[p]) {
if (card_has_rule(c, "place_2_blue"))
game.place_max[BLUE] = 2
if (card_has_rule(c, "place_2_red"))
game.place_max[RED] = 2
if (card_has_rule(c, "place_2_red_if_dice") && has_any_dice_on_card(c))
game.place_max[RED] = 2
/*
// NOT USED (YET)
if (card_has_rule(c, "place_2_dkblue")) game.place_max[DKBLUE] = 2
if (card_has_rule(c, "place_2_pink")) game.place_max[PINK] = 2
if (card_has_rule(c, "place_2_blue_if_dice") && has_any_dice_on_card(c)) game.place_max[BLUE] = 2
if (card_has_rule(c, "place_2_dkblue_if_dice") && has_any_dice_on_card(c)) game.place_max[DKBLUE] = 2
if (card_has_rule(c, "place_2_pink_if_dice") && has_any_dice_on_card(c)) game.place_max[PINK] = 2
*/
}
if (game.scenario === S38_FLEURUS) {
if (p === 1) {
if (get_sticks(S38_RETREAT_TO_NIVELLES)) {
if (game.starting_on_your_next_turn++) {
set_sticks(S38_RETREAT_TO_NIVELLES, get_sticks(S38_RETREAT_TO_NIVELLES) - 1)
game.lost[0] += 1
if (check_victory())
return
}
}
}
}
}
states.skip_action = {
inactive: "roll",
prompt() {
view.prompt = "Skipped action phase; roll the dice in your pool."
if (can_shift())
view.actions.shift = 1
view.actions.roll = 1
view.actions.end_turn = 0
},
shift() {
push_undo()
game.state = "shift_from"
},
roll() {
clear_undo()
goto_roll_phase()
roll_dice_in_pool()
},
}
states.roll = {
inactive: "roll",
prompt() {
view.prompt = "Roll the dice in your pool."
view.actions.roll = 1
view.actions.end_turn = 0
},
roll() {
clear_undo()
roll_dice_in_pool()
},
}
function roll_dice_in_pool() {
let rolled = []
if (game.reacted === player_index())
game.reacted = -1
let p = player_index()
for (let i = 0; i < 6; ++i) {
if (get_dice_location(p * 6 + i) < 0) {
let v = random(6) + 1
set_dice_value(p * 6 + i, v)
rolled.push(v)
}
}
log("Roll\n" + rolled.map(d => "D" + d).join(" "))
game.rolled = rolled.length
game.summary = 0
game.state = "place"
}
function gen_place_dice_select_card() {
let p = player_index()
for (let c of game.front[p]) {
if (c === game.selected)
continue
if (can_place_dice(c))
gen_action_card(c)
}
}
states.place = {
inactive: "place dice",
prompt() {
view.prompt = "Place dice on your formations."
gen_place_dice_select_card()
view.actions.end_turn = 1
},
card(c) {
push_undo()
game.selected = c
game.state = "place_on_card"
},
end_turn() {
end_roll_phase()
},
}
states.place_on_card = {
inactive: "place dice",
prompt() {
let card = data.cards[game.selected]
view.prompt = "Place dice on " + card.name + "."
gen_place_dice_select_card()
place_dice_gen[card.dice](game.selected)
view.actions.end_turn = 1
},
card(c) {
if (c === game.selected) {
game.selected = -1
game.state = "place"
} else {
game.selected = c
game.state = "place_on_card"
}
},
die(d) {
push_undo()
place_dice_take[data.cards[game.selected].dice](game.selected, d)
if (!can_place_dice(game.selected)) {
game.selected = -1
game.state = "place"
}
},
end_turn() {
end_roll_phase()
},
}
function end_roll_phase() {
push_undo()
game.selected = -1
if (game.placed.length > 0) {
log("Placed")
for (let i = 0; i < game.placed.length; i += 2) {
let c = game.placed[i]
let s = []
for (let d = 0; d < 12; ++d)
if (game.summary & (1 << d))
if (get_dice_location(d) === c)
s.push("D" + get_dice_value(d))
log(">C" + c + "\n" + s.join(" "))
}
}
// Remove placed dice to add cube on special cards.
for (let c of game.front[player_index()]) {
let s = data.cards[c].special
if (s && has_any_dice_on_card(c)) {
map_set(game.cubes, c, Math.min(s, get_cubes(c) + 1))
remove_dice(c)
}
}
// Blank out unused dice.
let p = player_index()
for (let i = 0; i < 6; ++i)
if (get_dice_location(p * 6 + i) < 0)
set_dice_value(p * 6 + i, 0)
if (game.scenario === S26_PEACH_ORCHARD) {
if (player_index() === 0) {
if (is_card_in_play(S26_FATAL_BLUNDER)) {
if (!placed_any_dice_on_wing(PINK)) {
game.state = "s26_fatal_blunder"
return
}
}
}
}
if (game.scenario === S28_CULPS_HILL) {
if (get_cubes(S28_GEARY) === 5)
return goto_game_over(P2, "Geary's Division arrived!")
}
if (game.scenario === S41_BLENHEIM_SCENARIO) {
if (player_index() === 1) {
if (is_card_in_play(S41_CLERAMBAULT)) {
if (!placed_any_dice_on_wing(DKBLUE)) {
game.state = "s41_clerambault"
return
}
}
}
}
if (game.scenario === S47_PRAGUE) {
if (player_index() === 0) {
if (is_card_in_play(S47_SCHWERIN) && !placed_any_dice()) {
game.target2 = S47_SCHWERIN
game.state = "s47_browne_and_schwerin"
return
}
}
if (player_index() === 1) {
if (is_card_in_play(S47_BROWNE) && !placed_any_dice()) {
game.target2 = S47_BROWNE
game.state = "s47_browne_and_schwerin"
return
}
}
}
if (game.scenario === S48_BRESLAU) {
if (player_index() === 0) {
if (is_card_in_play(S48_AUSTRIAN_GUNS)) {
if (get_cubes(S48_AUSTRIAN_GUNS) === 0 && get_cubes(S48_PRUSSIAN_GUNS) === 2) {
game.target2 = S48_AUSTRIAN_GUNS
game.state = "s48_artillery_duel"
return
}
}
}
if (player_index() === 1) {
if (is_card_in_play(S48_PRUSSIAN_GUNS)) {
if (get_cubes(S48_PRUSSIAN_GUNS) === 0 && get_cubes(S48_AUSTRIAN_GUNS) === 2) {
game.target2 = S48_PRUSSIAN_GUNS
game.state = "s48_artillery_duel"
return
}
}
}
}
end_turn()
}
states.s26_fatal_blunder = {
prompt() {
view.prompt = "Fatal Blunder!"
if (is_card_in_play(S26_FATAL_BLUNDER)) {
gen_action_card(S26_FATAL_BLUNDER)
} else {
let done = true
for (let c of game.front[0]) {
if (data.cards[c].wing === PINK) {
gen_action_card(c)
done = false
}
}
if (done)
view.actions.end_turn = 1
}
},
card(c) {
if (c === S26_FATAL_BLUNDER) {
log("Fatal Blunder!")
remove_card(S26_FATAL_BLUNDER)
game.morale[0] ++
} else {
rout_card(c)
game.morale[0] --
game.morale[1] ++
}
},
end_turn() {
if (check_victory())
return
end_turn()
}
}
states.s41_clerambault = {
prompt() {
view.prompt = "Rout Clerambault and add his sticks to Blenheim!"
if (is_card_in_play(S41_BLENHEIM_CARD) && get_sticks(S41_CLERAMBAULT)) {
view.selected = S41_CLERAMBAULT
gen_action_card(S41_BLENHEIM_CARD)
} else {
gen_action_card(S41_CLERAMBAULT)
}
},
card(c) {
if (c === S41_BLENHEIM_CARD)
move_all_sticks(S41_CLERAMBAULT, S41_BLENHEIM_CARD)
else
set_sticks(S41_CLERAMBAULT, 0)
if (c === S41_CLERAMBAULT) {
rout_card(S41_CLERAMBAULT)
game.morale[0]++
game.morale[1]--
end_turn()
}
},
}
states.s47_browne_and_schwerin = {
prompt() {
view.prompt = "Remove " + card_name(game.target2) + "."
gen_action_card(game.target2)
},
card(_) {
remove_card(game.target2)
game.target2 = -1
end_turn()
},
}
states.s48_artillery_duel = {
prompt() {
view.prompt = "Artillery Duel: Remove " + card_name(game.target2) + "."
gen_action_card(game.target2)
},
card(_) {
remove_card(game.target2)
game.target2 = -1
end_turn()
},
}
function end_turn() {
clear_undo()
map_clear(game.placed)
game.place_max = null
set_opponent_active()
goto_start_turn()
}
// === ACTION PHASE ===
function side_get_wild_die_card(p) {
if (game.scenario === S11_MORTIMERS_CROSS || game.scenario === S12_TOWTON || game.scenario === S16_STOKE_FIELD) {
for (let c of game.front[p])
if (card_has_rule(c, "wild") && has_any_dice_on_card(c))
return c
}
return -1
}
function side_has_wild_die(p) {
return side_get_wild_die_card(p) >= 0
}
function has_any_dice_on_card(c) {
for (let i = 0; i < 12; ++i)
if (get_dice_location(i) === c)
return true
return false
}
function has_any_cubes_on_card(c) {
return get_cubes(c) >= 1
}
function count_dice_on_card(c) {
let n = 0
for (let i = 0; i < 12; ++i)
if (get_dice_location(i) === c)
++n
return n
}
function count_dice_on_card_with_value(c, v) {
let n = 0
for (let i = 0; i < 12; ++i)
if (get_dice_location(i) === c && get_dice_value(i) === v)
++n
return n
}
function require_pair(c) {
for (let v = 1; v <= 6; ++v)
if (count_dice_on_card_with_value(c, v) >= 2)
return true
return false
}
function require_triplet(c) {
for (let v = 1; v <= 6; ++v)
if (count_dice_on_card_with_value(c, v) >= 3)
return true
return false
}
function require_full_house(c) {
let n3 = 0
let n2 = 0
for (let v = 1; v <= 6; ++v) {
let n = count_dice_on_card_with_value(c, v)
if (n >= 3)
++n3
else if (n >= 2)
++n2
}
return (n3 >= 2) || (n3 >= 1 && n2 >= 1)
}
function require_two_pairs(c) {
let n = 0
for (let v = 1; v <= 6; ++v)
if (count_dice_on_card_with_value(c, v) >= 2)
++n
return n >= 2
}
function check_cube_requirement(c, req) {
switch (req) {
case "3 cubes":
case "Three Cubes":
return get_cubes(c) >= 3
case "Two Cubes":
return get_cubes(c) >= 2
case "One Cube*":
case "Voluntary":
case undefined:
return get_cubes(c) >= 1
default:
throw new Error("invalid action requirement: " + req)
}
}
function check_dice_requirement(c, req, wild) {
switch (req) {
case "Three Dice":
return count_dice_on_card(c) >= 3
case "Full House":
return require_full_house(c)
case "Pair":
case "Pair, Voluntary":
// NOTE: Only requirement needed for Wild die scenarios.
if (wild)
return has_any_dice_on_card(c)
return require_pair(c)
case "Triplet":
return require_triplet(c)
case "Two Pairs":
return require_two_pairs(c)
case "Voluntary":
case undefined:
return has_any_dice_on_card(c)
default:
throw new Error("invalid action requirement: " + req)
}
}
function card_has_any_actions(c) {
for (let a of data.cards[c].actions)
if (is_action(c, a))
return true
return false
}
function is_action(c, a) {
return (a.type === "Bombard" || a.type === "Attack" || a.type === "Command")
}
function is_reaction(c, a) {
return (a.type === "Screen" || a.type === "Counterattack" || a.type === "Absorb")
}
function is_mandatory_reaction(c, a) {
if (game.scenario === S44_HOHENFRIEDBERG) {
if (c === S44_DU_MOULIN && game.selected === S44_SAXON_HORSE)
return false
}
return (
a.requirement !== "Voluntary" &&
a.requirement !== "Pair, Voluntary"
)
}
function can_take_action(c, a, ix) {
if (a.type === "Attack") {
if (find_target_of_attack(c, a) < 0)
return false
}
if (a.type === "Command") {
if (find_first_target_of_command(c, a) < 0)
return false
}
if (a.type === "Bombard") {
// cannot Bombard last morale cube
let p = player_index()
if (game.morale[1-p] === 1)
return false
}
if (game.scenario === S8_BROOKLYN_HEIGHTS) {
if (c === S8_CLINTON) {
// Clinton - may only attack if both Grant and Hessians have dice on them.
if (!has_any_dice_on_card(S8_GRANT) || !has_any_dice_on_card(S8_HESSIANS))
return false
}
}
if (game.scenario === S3201_GAINES_MILL) {
if (c === S3201_JACKSON) {
// Jackson - may only attack if D.H. Hill and one other formation have dice
if (!has_any_dice_on_card(S3201_DH_HILL))
return false
if (!has_any_dice_on_card(S3201_AP_HILL) && !has_any_dice_on_card(S3201_LONGSTREET))
return false
}
}
if (game.scenario === S35_AULDEARN) {
if (c === S35_MONTROSE) {
// May only perform the second action after having previously performed the first.
if (ix === 1) {
if (is_card_in_reserve(S35_GORDON))
return false
}
}
}
if (game.scenario === S45_SOOR) {
if (c === S45_AUSTRIAN_GUNS) {
// May only attack Buddenbrock if Cuirassiers still in play
if (ix === 0) {
if (is_removed_from_play(S45_CUIRASSIERS))
return false
}
}
}
if (game.scenario === S49_LEUTHEN) {
if (c === S49_POOR_CHARLES) {
// Cannot command if dice on Feint
if (has_any_dice_on_card(S49_FEINT))
return false
}
}
if (a.type === "Bombard" || a.type === "Attack" || a.type === "Command") {
if (data.cards[c].special)
return check_cube_requirement(c, a.requirement)
else
return check_dice_requirement(c, a.requirement, false)
}
return false
}
function s40_can_take_cassines_action(c, a, b) {
return (get_sticks(c) < 3) && (is_card_in_play(a) || is_card_in_play(b))
}
function can_take_any_action() {
let p = player_index()
for (let c of game.front[p]) {
if (card_has_any_actions(c)) {
if (has_any_dice_on_card(c))
return true
if (has_any_cubes_on_card(c)) // TODO: check requirements!
return true
}
}
if (game.scenario === S40_CHIARI) {
if (player_index() === 1) {
if (s40_can_take_cassines_action(S40_CASSINES_I, S40_NIGRELLI, S40_KRIECHBAUM))
return true
if (s40_can_take_cassines_action(S40_CASSINES_II, S40_MANNSFELDT, S40_GUTTENSTEIN))
return true
}
}
if (game.scenario === S41_BLENHEIM_SCENARIO) {
if (player_index() === 0) {
if (has_any_dice_on_card(S41_PRINCE_EUGENE))
return true
}
}
if (game.scenario === S42_RAMILLIES) {
if (player_index() === 0) {
if (has_any_dice_on_card(S42_MARLBOROUGH))
return true
}
}
if (can_shift())
return true
return false
}
function count_cards_remaining_from_wing(w) {
let n = 0
for (let c of game.front[0])
if (data.cards[c].wing === w)
++n
for (let c of game.front[1])
if (data.cards[c].wing === w)
++n
for (let c of game.reserve[0])
if (data.cards[c].wing === w)
++n
for (let c of game.reserve[1])
if (data.cards[c].wing === w)
++n
return n
}
function goto_start_turn() {
let p = player_index()
log(".p" + (p + 1))
if (check_impossible_to_attack_victory())
return
// TODO: manual step to shift?
if (game.shift) {
for (let c of game.front[p]) {
let n = get_shift_sticks(c)
if (n > 0) {
set_sticks(c, get_sticks(c) + n)
set_shift_sticks(c, 0)
}
}
}
if (game.scenario === S25_WHEATFIELD) {
// Rout Stony Hill at start of Union turn if it is the only Blue card left.
if (p === 1) {
if (is_card_in_play(S25_STONY_HILL)) {
if (count_cards_remaining_from_wing(BLUE) === 1) {
game.state = "s25_stony_hill"
return
}
}
}
}
if (game.scenario === S44_HOHENFRIEDBERG) {
if (p === 1) {
let have_inf_or_cav = false
for (let c of game.front[1])
if (is_infantry(c) || is_cavalry(c))
have_inf_or_cav = true
if (!have_inf_or_cav)
return goto_game_over(P1, "Frederick had no Infantry or Cavalry in play!")
}
}
goto_action_phase()
}
states.s25_stony_hill = {
prompt() {
view.prompt = "Rout Stony Hill!"
gen_action_card(S25_STONY_HILL)
},
card(_) {
rout_card(S25_STONY_HILL)
game.morale[0] --
game.morale[1] ++
if (check_victory())
return
goto_action_phase()
},
}
function goto_action_phase() {
if (game.reacted === player_index()) {
game.state = "skip_action"
} else {
if (can_take_any_action())
game.state = "action"
else
end_action_phase()
}
}
function end_action_phase() {
game.hits = 0
game.self = 0
game.hits2 = 0
game.self2 = 0
game.selected = -1
game.target = -1
game.target2 = -1
goto_routing()
}
states.action = {
inactive: "take an action",
prompt() {
view.prompt = "Take an action."
view.actions.roll = 1
view.actions.end_turn = 0
let p = player_index()
for (let c of game.front[p]) {
let has_dice = has_any_dice_on_card(c)
let has_cube = has_any_cubes_on_card(c)
if (has_dice || has_cube) {
if (data.cards[c].actions.length >= 1) {
if (is_action(c, data.cards[c].actions[0])) {
if (can_take_action(c, data.cards[c].actions[0], 0))
gen_action_action1(c)
else if (has_dice)
gen_action_null1(c)
}
}
if (data.cards[c].actions.length >= 2) {
if (is_action(c, data.cards[c].actions[1])) {
if (can_take_action(c, data.cards[c].actions[1], 1))
gen_action_action2(c)
else if (has_dice)
gen_action_null2(c)
}
}
if (data.cards[c].retire)
gen_action_retire(c)
}
}
if (can_shift())
view.actions.shift = 1
if (game.scenario === S40_CHIARI) {
if (player_index() === 1) {
if (s40_can_take_cassines_action(S40_CASSINES_I, S40_NIGRELLI, S40_KRIECHBAUM))
gen_action_card(S40_CASSINES_I)
if (s40_can_take_cassines_action(S40_CASSINES_II, S40_MANNSFELDT, S40_GUTTENSTEIN))
gen_action_card(S40_CASSINES_II)
}
}
if (game.scenario === S41_BLENHEIM_SCENARIO) {
if (player_index() === 0) {
if (has_any_dice_on_card(S41_PRINCE_EUGENE))
gen_action_card(S41_PRINCE_EUGENE)
}
}
if (game.scenario === S42_RAMILLIES) {
if (player_index() === 0) {
if (has_any_dice_on_card(S42_MARLBOROUGH))
gen_action_card(S42_MARLBOROUGH)
}
}
},
retire(c) {
push_undo()
log("Retire\nC" + c)
if (game.scenario === S38_FLEURUS) {
if (c === S38_LUXEMBOURGS_HORSE) {
if (is_card_in_play(S38_GOURNAYS_HORSE))
move_all_sticks(S38_LUXEMBOURGS_HORSE, S38_GOURNAYS_HORSE)
}
}
retire_card(c)
end_action_phase()
},
a1(c) {
push_undo()
goto_take_action(c, 0)
},
a2(c) {
push_undo()
goto_take_action(c, 1)
},
n1(c) {
push_undo()
goto_null(c)
},
n2(c) {
push_undo()
goto_null(c)
},
roll() {
clear_undo()
goto_roll_phase()
roll_dice_in_pool()
},
shift() {
push_undo()
game.state = "shift_from"
},
card(c) {
push_undo()
game.selected = c
if (game.scenario === S40_CHIARI) {
game.state = "s40_cassines"
return
}
if (game.scenario === S41_BLENHEIM_SCENARIO) {
game.target2 = -1
game.self = Math.min(get_sticks(S41_PRINCE_EUGENE), count_dice_on_card(S41_PRINCE_EUGENE))
game.state = "s41_prince_eugene"
return
}
if (game.scenario === S42_RAMILLIES) {
game.state = "s42_marlborough"
return
}
throw new Error("missing rule for special action: " + card_name(c))
}
}
function can_shift() {
if (!game.shift)
return false
if (game.scenario === S49_LEUTHEN) {
if (player_index() === 1) {
if (has_any_dice_on_card(S49_FEINT))
return false
}
}
return can_shift_any_infantry() || can_shift_any_cavalry()
}
function can_shift_any_infantry() {
let n = 0, m = 0
for (let c of game.front[player_index()]) {
if (is_infantry(c)) {
if (get_sticks(c) > 1)
++m
++n
}
}
return n > 1 && m > 0
}
function can_shift_any_cavalry() {
let n = 0, m = 0
for (let c of game.front[player_index()]) {
if (is_cavalry(c)) {
if (get_sticks(c) > 1)
++m
++n
}
}
return n > 1 && m > 0
}
states.shift_from = {
inactive: "shift sticks",
prompt() {
view.prompt = "Shift sticks from one Formation to another."
let p = player_index()
if (can_shift_any_infantry())
for (let c of game.front[p])
if (is_infantry(c) && get_sticks(c) > 1)
gen_action_card(c)
if (can_shift_any_cavalry())
for (let c of game.front[p])
if (is_cavalry(c) && get_sticks(c) > 1)
gen_action_card(c)
},
card(c) {
game.selected = c
game.target2 = -1
game.state = "shift_to"
},
}
states.shift_to = {
inactive: "shift sticks",
prompt() {
view.prompt = "Shift sticks from " + card_name(game.selected) + "."
let p = player_index()
if (game.target2 < 0) {
if (is_infantry(game.selected))
for (let c of game.front[p])
if (c !== game.selected && is_infantry(c) && c !== S46_AUSTRIANS)
gen_action_card(c)
if (is_cavalry(game.selected))
for (let c of game.front[p])
if (c !== game.selected && is_cavalry(c))
gen_action_card(c)
} else {
gen_action_card(game.target2)
view.actions.next = 1
}
},
card(c) {
game.target2 = c
set_sticks(game.selected, get_sticks(game.selected) - 1)
set_shift_sticks(game.target2, get_shift_sticks(game.target2) + 1)
if (get_sticks(game.selected) === 1)
this.next()
},
next() {
// TODO: skip action phase?
log("Shift " + get_shift_sticks(game.target2) + "\nC" + game.selected + "\nC" + game.target2)
end_action_phase()
},
}
states.s40_cassines = {
prompt() {
view.prompt = "Cassines: Move one unit stick to this card."
if (game.selected === S40_CASSINES_I) {
if (is_card_in_play(S40_NIGRELLI))
gen_action_card(S40_NIGRELLI)
if (is_card_in_play(S40_KRIECHBAUM))
gen_action_card(S40_KRIECHBAUM)
}
if (game.selected === S40_CASSINES_II) {
if (is_card_in_play(S40_MANNSFELDT))
gen_action_card(S40_MANNSFELDT)
if (is_card_in_play(S40_GUTTENSTEIN))
gen_action_card(S40_GUTTENSTEIN)
}
},
card(c) {
log(game.selected + " moved one stick from " + c)
move_sticks(c, game.selected, 1)
end_action_phase()
},
}
states.s41_prince_eugene = {
prompt() {
if (game.target2 < 0) {
view.prompt = `Prince Eugene: Move up to ${game.self} unit sticks to any Red card.`
for (let c of game.front[0])
if (data.cards[c].wing === RED && !data.cards[c].special)
gen_action_card(c)
} else {
view.prompt = `Prince Eugene: Move up to ${game.self} unit sticks to ${card_name(game.target2)}.`
gen_action_card(game.target2)
view.actions.next = 1
}
},
card(c) {
game.target2 = c
log(game.target2 + " moved one stick from " + game.selected)
move_sticks(game.selected, game.target2, 1)
if (--game.self === 0) {
pay_for_action(game.selected)
end_action_phase()
}
},
next() {
pay_for_action(game.selected)
end_action_phase()
},
}
states.s42_marlborough = {
prompt() {
view.prompt = "Marlborough: Move 4 sticks to any red or pink card except the Dutch Guards."
for (let c of game.front[0])
if (c !== S42_MARLBOROUGH && c !== S42_DUTCH_GUARDS)
gen_action_card(c)
},
card(c) {
log(game.selected + " moved 4 sticks to " + c)
move_sticks(S42_MARLBOROUGH, c, 4)
pay_for_action(S42_MARLBOROUGH)
if (get_sticks(S42_MARLBOROUGH) === 0)
remove_card(S42_MARLBOROUGH)
end_action_phase()
},
}
function goto_null(c) {
log("Null\nC" + c)
pay_for_action(c)
end_action_phase()
}
function goto_take_action(c, ix) {
let a = data.cards[c].actions[ix]
game.selected = c
game.action = ix
switch (a.type) {
case "Attack":
if (a.choice)
goto_attack_choose_target()
else
goto_attack(find_target_of_attack(c, a))
break
case "Bombard":
game.state = "bombard"
break
case "Command":
goto_command()
break
}
}
function current_action() {
return data.cards[game.selected].actions[game.action]
}
function find_target_of_attack(c, a) {
if (card_has_rule(c, "attack_reserve")) {
for (let t of a.target_list)
if (is_card_in_play_or_reserve(t))
return t
return -1
}
if (card_has_rule(c, "ignore_reserve")) {
for (let t of a.target_list)
if (is_card_in_play(t))
return t
return -1
}
for (let t of a.target_list) {
if (is_card_in_play(t))
return t
if (is_card_in_reserve(t))
return -1
}
return -1
}
function find_target_of_counterattack(a) {
for (let t of a.target_list) {
if (set_has(game.front[0], t))
return t
if (set_has(game.front[1], t))
return t
}
return -1
}
function find_target_of_absorb(a) {
for (let t of a.target_list) {
if (set_has(game.front[0], t))
return t
if (set_has(game.front[1], t))
return t
}
return -1
}
function find_first_target_of_command(c, a) {
if (game.scenario === S37_INKERMAN) {
if (c === S37_THE_FOG)
return S37_THE_FOG
}
if (game.scenario === S38_FLEURUS) {
if (c === S38_WALDECK)
return S38_RETREAT_TO_NIVELLES
}
if (game.scenario === S44_HOHENFRIEDBERG) {
if (c === S44_CHARLES) {
if (game.reserve[1].length > 0)
return game.reserve[1]
return -1
}
}
if (!a.target_list)
throw new Error("no rule for Command target: " + a.target)
for (let t of a.target_list) {
if (is_card_in_reserve(t))
return t
}
return -1
}
function find_all_targets_of_command(c, a) {
if (game.scenario === S44_HOHENFRIEDBERG) {
if (c === S44_FREDERICK_II) {
for (let t of a.target_list)
if (is_card_in_reserve(t))
return [ t ]
}
}
let list = []
for (let t of a.target_list) {
if (is_card_in_reserve(t))
list.push(t)
}
return list
}
states.bombard = {
inactive: "bombard",
prompt() {
view.prompt = "Bombard."
view.actions.bombard = 1
},
bombard() {
log("Bombard\nC" + game.selected)
let opp = 1 - player_index()
game.morale[opp] --
pay_for_action(game.selected)
end_action_phase()
},
}
function goto_attack_choose_target() {
let a = current_action()
let candidates = []
for (let c of a.target_list) {
if (set_has(game.front[0], c))
candidates.push(c)
if (set_has(game.front[1], c))
candidates.push(c)
}
if (candidates.length > 1)
game.state = "attack_choose_target"
else
goto_attack(candidates[0])
}
states.attack_choose_target = {
inactive: "attack",
prompt() {
view.prompt = "Choose the target of your attack."
let a = current_action()
for (let c of a.target_list) {
if (set_has(game.front[0], c))
gen_action_card(c)
if (set_has(game.front[1], c))
gen_action_card(c)
}
},
card(c) {
goto_attack(c)
},
}
function goto_attack(target) {
let take_from = card_has_rule(game.selected, "take_from")
if (take_from) {
for (let from of take_from)
if (has_any_dice_on_card(from))
take_all_dice(from, game.selected)
}
let take_1_from = card_has_rule(game.selected, "take_1_from")
if (take_1_from) {
for (let from of take_1_from)
if (has_any_dice_on_card(from))
take_one_die(from, game.selected)
}
game.state = "attack"
game.target = target
update_attack1(true)
update_attack2()
}
// Update hits and self hits.
function update_attack1(direct) {
let a = current_action()
let n = count_dice_on_card(game.selected)
switch (a.effect) {
default:
throw new Error("invalid attack effect: " + a.effect)
case "1 hit per die.":
game.self = 0
game.hits = n
break
case "1 hit per die. 1 self.":
game.self = 1
game.hits = n
break
case "1 hit per die. 1 self. If reduced to one stick, no self hits.":
if (get_sticks(game.selected) === 1)
game.self = 0
else
game.self = 1
game.hits = n
break
case "1 hit per pair.":
game.self = 0
game.hits = n / 2
break
case "1 hit per pair. 1 self.":
game.self = 1
game.hits = n / 2
break
case "1 hit plus 1 hit per die.":
game.self = 0
game.hits = 1 + n
break
case "1 hit plus 1 hit per die. 1 self.":
game.self = 1
game.hits = 1 + n
break
case "1 hit. 1 self.":
game.self = 1
game.hits = 1
break
case "2 hits per die.":
game.self = 0
game.hits = 2 * n
break
case "2 hits plus 1 hit per die. 1 self.":
game.self = 1
game.hits = 2 + n
break
case "1 hit.":
game.self = 0
game.hits = 1
break
case "2 hits.":
game.self = 0
game.hits = 2
break
case "5 hits.":
game.self = 0
game.hits = 5
break
case "1 hit per die (2 hits per die versus Blenheim). 1 self.":
game.self = 1
game.hits = n
if (direct && game.target === S41_BLENHEIM_CARD)
game.hits = 2 * n
break
case "1 hit per die (2 hits per die versus Villars's Left). 1 self.":
game.self = 1
game.hits = n
if (direct && game.target === S43_VILLARS_LEFT)
game.hits = 2 * n
break
case "1 hit per die versus Driesen. 2 hits per die versus Retzow.":
case "1 hit per die (2 hits per die versus Retzow).":
game.self = 0
game.hits = n
if (direct && game.target === S49_RETZOW)
game.hits = 2 * n
break
case "1 hit per die. 1 extra hit if Fourth Line is in play.":
game.self = 0
game.hits = n
if (is_card_in_play(S36_FOURTH_LINE))
game.hits += 1
break
case "1 hit per die. 1 self. 1 extra hit if Dutch Horse routed.":
game.self = 1
game.hits = n
if (is_removed_from_play(S38_DUTCH_HORSE))
game.hits += 1
break
case "1 hit per die. 1 self. 1 extra vs Dutch Left Foot.":
game.self = 1
game.hits = n
if (direct && game.target === S38_DUTCH_LEFT_FOOT)
game.hits += 1
break
case "1 hit per die. 1 self. 1 extra vs Essex.":
game.self = 1
game.hits = n
if (game.target === S30_ESSEX)
game.hits += 1
break
case "1 hit per die. 1 self. 1 extra vs Tullibardine.":
game.self = 1
game.hits = n
if (direct && game.target === S34_TULLIBARDINE)
game.hits += 1
break
case "Oxford immediately routs. This attack cannot be screened.":
game.self = 0
game.hits = 8
break
}
if (game.scenario === S2_MARSTON_MOOR) {
if (is_card_in_play(S2_RUPERTS_LIFEGUARD)) {
if (game.target === S2_TILLIERS_LEFT)
game.hits -= 1
if (game.target === S2_TILLIERS_RIGHT)
game.hits -= 1
}
}
if (game.scenario === S9_ST_ALBANS) {
// Defensive Works (negated by Archers)
if (game.target === S9_SHROPSHIRE_LANE || game.target === S9_SOPWELL_LANE) {
if (is_card_in_play(S9_HENRY_VI))
if (!has_any_cubes_on_card(S9_ARCHERS))
game.hits = Math.min(1, game.hits)
}
}
if (game.scenario === S15_TEWKESBURY) {
if (game.target === S15_SOMERSET) {
if (has_any_dice_on_card(S15_A_PLUMP_OF_SPEARS))
game.hits += 1
}
}
if (game.scenario === S22_GABIENE) {
if (game.target === S22_SILVER_SHIELDS) {
if (is_card_in_play(S22_EUMENES_CAMP)) {
game.hits = Math.min(1, game.hits - 1)
}
}
}
if (game.scenario === S31_NEWBURY_1ST) {
if (game.selected === S31_WENTWORTH) {
if (has_any_dice_on_card(S31_BYRON) && is_card_in_play(S31_SKIPPON)) {
game.hits += 1
}
}
if (game.target === S31_WENTWORTH) {
if (has_any_dice_on_card(S31_BYRON) && is_card_in_play(S31_SKIPPON)) {
game.hits -= 1
}
}
}
if (game.scenario === S37_INKERMAN) {
// Until the first Fog Cube is lifted.
if (game.target === S37_SOIMONOFF && get_cubes(S37_THE_FOG) === 3) {
game.hits -= 1
}
}
if (game.scenario === S39_MARSAGLIA) {
if (game.selected === S39_EUGENE) {
if (has_any_dice_on_card(S39_CANNONS)) {
game.self = 0
}
}
if (game.selected === S39_CATINAT && game.target2 === S39_BAYONETS) {
game.self = 0
}
}
if (game.scenario === S44_HOHENFRIEDBERG) {
if (game.target === S44_CHARLES) {
if (game.selected === S44_LEOPOLDS_L || game.selected === S44_LEOPOLDS_C || game.selected === S44_LEOPOLDS_R)
game.self = 0
}
}
// Oblique Attack (CAL expansion rule)
if (is_infantry(game.selected)) {
if (get_sticks(game.selected) >= get_sticks(game.target) + 3)
game.hits += 1
}
let extra = card_has_rule(game.selected, "extra_hit_if_dice_on")
if (extra && has_any_dice_on_card(extra[0]))
game.hits += 1
// Linked Formations (TGA and CAL expansion rule)
if (card_has_active_link(game.target))
game.hits = Math.max(0, game.hits - 1)
if (card_has_rule(game.target, "suffer_1_less"))
game.hits = Math.max(0, game.hits - 1)
if (card_has_rule(game.target, "suffer_1_less_1_max"))
game.hits = Math.max(0, Math.min(1, game.hits - 1))
}
// Update hits and self hits for defensive abilities that redirect or steal hits.
function update_attack2() {
if (game.scenario === S28_CULPS_HILL) {
if (is_card_in_play(S28_BREASTWORKS)) {
if (data.cards[game.target].wing === DKBLUE) {
if (game.hits > 0) {
game.target2 = S28_BREASTWORKS
if (game.hits > 1) {
game.hits2 = game.hits - 1
game.hits = 1
} else {
game.hits2 = 1
game.hits = 0
}
}
}
}
}
}
states.attack = {
inactive: "attack",
prompt() {
view.prompt = "Attack " + card_name(game.target) + "."
gen_action_card(game.target)
let w = side_get_wild_die_card(player_index())
if (w >= 0)
gen_action_dice_on_card(w)
let may_take_from = card_has_rule(game.selected, "may_take_from")
if (may_take_from) {
for (let from of may_take_from)
gen_action_dice_on_card(from)
}
let may_take_from_extra = card_has_rule(game.selected, "may_take_from_extra_self")
if (may_take_from_extra) {
for (let from of may_take_from_extra)
gen_action_dice_on_card(from)
}
if (game.scenario === S39_MARSAGLIA) {
if (game.selected === S39_CATINAT) {
gen_action_dice_on_card(S39_BAYONETS)
}
}
if (game.scenario === S49_LEUTHEN) {
if (player_index() === 0)
gen_action_dice_on_card(S49_THE_LEUTHEN_CHORALE)
}
view.actions.attack = 1
},
attack() {
log("Attack\nC" + game.selected + "\nC" + game.target)
if (can_opponent_react()) {
clear_undo()
set_opponent_active()
game.state = "react"
} else {
resume_attack()
}
},
card(_) {
this.attack()
},
die(d) {
let from = get_dice_location(d)
let w = side_get_wild_die_card(player_index())
if (w === from) {
take_wild_die(w, game.selected)
return
}
let may_take_from = card_has_rule(game.selected, "may_take_from")
if (may_take_from) {
take_all_dice(from, game.selected)
update_attack1(true)
update_attack2()
return
}
let may_take_from_extra = card_has_rule(game.selected, "may_take_from_extra_self")
if (may_take_from_extra) {
take_all_dice(from, game.selected)
game.self2 = 1
update_attack1(true)
update_attack2()
return
}
if (game.scenario === S39_MARSAGLIA) {
if (game.selected === S39_CATINAT && from === S39_BAYONETS) {
take_all_dice(from, game.selected)
game.target2 = S39_BAYONETS
update_attack1(true)
update_attack2()
return
}
}
if (game.scenario === S49_LEUTHEN) {
if (from === S49_THE_LEUTHEN_CHORALE) {
take_all_dice(from, game.selected)
game.target2 = S49_THE_LEUTHEN_CHORALE
update_attack1(true)
update_attack2()
return
}
}
throw new Error("no handler for taking dice from other card")
}
}
function resume_attack() {
pay_for_action(game.selected)
if (game.hits > 0 && game.self > 0) {
log(">" + game.hits + " hits. " + game.self + " self.")
}
else if (game.hits > 0) {
log(">" + game.hits + " hits.")
}
else if (game.self > 0) {
log(">" + game.self + " self.")
}
if (game.hits > 0)
remove_sticks(game.target, game.hits)
if (game.self > 0)
remove_sticks(game.selected, game.self)
if (game.target2 >= 0 && game.hits2 > 0) {
log(">" + game.hits2 + " hits on C" + game.target2)
remove_sticks(game.target2, game.hits2)
}
if (game.scenario === S44_HOHENFRIEDBERG) {
// remove after first attack
if (game.selected === S44_BAYREUTH_DRAGOONS) {
remove_card(S44_BAYREUTH_DRAGOONS)
}
}
if (game.scenario === S49_LEUTHEN) {
// remove after using
if (game.target2 === S49_THE_LEUTHEN_CHORALE) {
remove_card(S49_THE_LEUTHEN_CHORALE)
}
}
end_action_phase()
}
// === COMMAND ===
function goto_command() {
if (game.scenario === S37_INKERMAN && game.selected === S37_THE_FOG) {
log("The Fog Lifts...")
remove_cubes(S37_THE_FOG, 1)
remove_dice(game.selected)
end_action_phase()
return
}
if (game.scenario === S38_FLEURUS && game.selected === S38_WALDECK) {
remove_cubes(S38_RETREAT_TO_NIVELLES, 1)
remove_dice(game.selected)
if (!has_any_cubes_on_card(S38_RETREAT_TO_NIVELLES)) {
game.selected = S38_RETREAT_TO_NIVELLES
game.state = "s38_retreat_to_nivelles_1"
game.starting_on_your_next_turn = 0
return
}
end_action_phase()
return
}
game.state = "command"
}
states.command = {
inactive: "command",
prompt() {
let list = find_all_targets_of_command(game.selected, current_action())
view.prompt = "Bring " + list.map(c => card_name(c)).join(" and ") + " out of reserve."
for (let t of list)
gen_action_card(t)
},
card(c) {
log("Command\nC" + game.selected + "\nC" + c)
let p = player_index()
set_delete(game.reserve[p], c)
set_add(game.front[p], c)
if (game.scenario === S4_BOSWORTH_FIELD) {
if (c === S4_THE_STANLEYS) {
if (is_card_in_play_or_reserve(S4_NORTHUMBERLAND)) {
log("The Stanleys rout Northumberland.")
set_sticks(S4_NORTHUMBERLAND, 0)
}
}
}
if (game.scenario === S37_INKERMAN) {
if (c === S37_PAULOFFS_LEFT) {
log("Morale Cube added to Russian side.")
game.morale[0] += 1
}
}
if (game.scenario === S13_EDGECOTE_MOOR) {
// TODO: pay all 3 cubes? remove cards from play?
if (game.reserve[0].length === 0 && game.reserve[1].length > 0) {
log("Gained a second morale cube.")
game.morale[0] += 1
}
if (game.reserve[1].length === 0 && game.reserve[0].length > 0) {
log("Gained a second morale cube.")
game.morale[1] += 1
}
}
if (game.scenario === S44_HOHENFRIEDBERG) {
// one at a time
if (game.selected === S44_CHARLES || game.selected === S44_FREDERICK_II) {
pay_for_action(game.selected)
end_action_phase()
return
}
}
if (find_first_target_of_command(game.selected, current_action()) < 0) {
pay_for_action(game.selected)
end_action_phase()
}
},
}
states.s38_retreat_to_nivelles_1 = {
prompt() {
view.prompt = "Retreat to Nivelles: Remove all in-play Grand Alliance cards!"
for (let c of game.front[1])
if (c !== S38_RETREAT_TO_NIVELLES)
gen_action_card(c)
},
card(c) {
move_all_sticks(c, S38_RETREAT_TO_NIVELLES)
remove_card(c)
if (game.front[1].length === 1)
game.state = "s38_retreat_to_nivelles_2"
},
}
states.s38_retreat_to_nivelles_2 = {
prompt() {
view.prompt = "Retreat to Nivelles: Bring van Aylva & van Weibnom out of reserve."
for (let c of game.reserve[1])
gen_action_card(c)
},
card(c) {
bring_out_of_reserve(c)
if (game.reserve[1].length === 0)
end_action_phase()
},
}
// === REACTION ===
function can_opponent_react() {
if (game.scenario === S39_MARSAGLIA) {
if (game.selected === S39_CATINAT && game.target2 === S39_BAYONETS)
return false
}
if (game.scenario === S14_BARNET) {
if (game.selected === S14_TREASON)
return false
}
let p = 1 - player_index()
let wild = side_has_wild_die(p)
for (let c of game.front[p])
if (can_card_react(c, wild))
return true
return false
}
function can_card_react(c, wild) {
let has_dice = has_any_dice_on_card(c)
let has_cube = has_any_cubes_on_card(c)
if (has_dice || has_cube) {
if (data.cards[c].actions.length >= 1)
if (is_reaction(c, data.cards[c].actions[0]))
if (can_take_reaction(c, data.cards[c].actions[0], wild))
return true
if (data.cards[c].actions.length >= 2)
if (is_reaction(c, data.cards[c].actions[1]))
if (can_take_reaction(c, data.cards[c].actions[1], wild))
return true
}
return false
}
function can_take_reaction(c, a, wild) {
switch (a.type) {
default:
throw new Error("invalid reaction: " + a.type)
case "Screen":
// if a friendly formation is attacked by a listed enemy formation
// ... or a listed formation is attacked (Wheatfield Road Artillery, etc)
if (!a.target_list.includes(game.selected) && !a.target_list.includes(game.target))
return false
break
case "Counterattack":
// if this formation is attacked
if (game.target !== c)
return false
// ... by one of the listed targets
if (a.choice) {
// if "any" or "or" choice
if (!a.target_list.includes(game.selected))
return false
} else {
// if strict order
if (find_target_of_counterattack(a) !== game.selected)
return false
}
break
case "Absorb":
// if attack target is listed on absorb action
if (a.choice) {
if (!a.target_list.includes(game.target))
return false
} else {
if (find_target_of_absorb(a) !== game.target)
return false
}
break
}
if (game.scenario === S7_THE_DUNES) {
if (has_any_cubes_on_card(S7_THE_ENGLISH_FLEET)) {
if (c === S7_DON_JUAN_JOSE)
return false
if (c === S7_SPANISH_RIGHT_CAVALRY)
return false
}
}
if (game.scenario === S15_TEWKESBURY) {
if (c === S15_WENLOCK) {
if (is_removed_from_play(S15_SOMERSET))
return false
}
}
if (game.scenario === S31_NEWBURY_1ST) {
if (c === S31_GERARD) {
// London Trained Bands enter play when Skippon routs
if (is_removed_from_play(S31_SKIPPON))
return false
}
}
if (game.scenario === S43_DENAIN) {
if (c === S43_DUTCH_HORSE) {
// May only screen Villars's Left if Broglie has routed
if (game.target === S43_VILLARS_LEFT)
if (is_card_in_play(S43_BROGLIE))
return false
}
}
if (data.cards[c].special)
return check_cube_requirement(c, a.requirement)
else
return check_dice_requirement(c, a.requirement, wild)
}
function take_wild_die_if_needed_for_reaction(c, ix) {
let w = side_get_wild_die_card(player_index())
if (w >= 0) {
let a = data.cards[c].actions[ix]
if (!can_take_reaction(c, a, false)) {
take_wild_die(w, c)
}
}
}
states.react = {
inactive: "react",
prompt() {
view.prompt = card_name(game.selected) + " attacks " + card_name(game.target) + "!"
let voluntary = true
let p = player_index()
let wild = side_has_wild_die(p)
for (let c of game.front[p]) {
let has_dice = has_any_dice_on_card(c)
let has_cube = has_any_cubes_on_card(c)
if (has_dice || has_cube) {
for (let i = 0; i < data.cards[c].actions.length; ++i) {
let a = data.cards[c].actions[i]
if (is_reaction(c, a)) {
let must = false
let may = false
if (is_mandatory_reaction(c, a)) {
must = can_take_reaction(c, a, false)
if (!must && wild && can_take_reaction(c, a, true))
may = true
} else {
may = can_take_reaction(c, a, wild)
}
if (must)
voluntary = false
if (must || may) {
if (i === 0) gen_action_action1(c)
if (i === 1) gen_action_action2(c)
}
}
}
}
}
if (voluntary)
view.actions.pass = 1
},
a1(c) {
push_undo()
take_wild_die_if_needed_for_reaction(c, 0)
goto_take_reaction(c, 0)
},
a2(c) {
push_undo()
take_wild_die_if_needed_for_reaction(c, 1)
goto_take_reaction(c, 1)
},
pass() {
set_opponent_active()
resume_attack()
},
}
function goto_take_reaction(c, ix) {
let a = data.cards[c].actions[ix]
switch (a.type) {
case "Screen":
goto_screen(c, a)
break
case "Absorb":
goto_absorb(c, a)
break
case "Counterattack":
goto_counterattack(c, a)
break
}
}
function end_reaction() {
clear_undo()
set_opponent_active()
resume_attack()
}
// === SCREEN ===
function goto_screen(c, a) {
game.reacted = player_index()
game.target = c
update_attack1(false)
switch (a.effect)
{
default:
throw new Error("invalid screen effect: " + a.effect)
case undefined:
game.hits = 0
game.self = 0
break
case "If either Chariot formation is screened, it suffers one hit!":
game.hits = 0
if (card_has_rule(game.selected, "is_chariot"))
game.self = 1
else
game.self = 0
break
}
update_attack2()
game.state = "screen"
}
states.screen = {
inactive: "screen",
prompt() {
view.prompt = "Screen attack from " + card_name(game.selected) + "."
view.actions.screen = 1
gen_action_card(game.selected)
},
screen() {
log("Screen\nC" + game.target)
pay_for_action(game.target)
if (card_has_rule(game.target, "remove_after_screen"))
remove_card(game.target)
end_reaction()
},
card(_) {
this.screen()
},
}
// === ABSORB ===
function goto_absorb(c, a) {
game.reacted = player_index()
if (game.scenario === S29_GETTYS_2ND) {
if (c === S29_MEADE) {
game.state = "s29_meade"
return
}
}
game.target = c
update_attack1(false)
switch (a.effect)
{
default:
throw new Error("invalid absorb effect: " + a.effect)
case "Suffers hits.":
break
case "Suffers 1 hit only.":
game.hits = 1
break
case "Suffers 1 less hit.":
game.hits = Math.max(0, game.hits - 1)
break
case "Suffers 1 less hit per die.":
game.hits = Math.max(0, game.hits - count_dice_on_card(c))
break
}
update_attack2()
game.state = "absorb"
}
states.s29_meade = {
prompt() {
view.prompt = "Choose any friendly Formation except Little Round Top to absorb the hits instead."
let p = player_index()
for (let c of game.front[p]) {
if (c !== S29_MEADE && c !== S29_LITTLE_ROUND_TOP && c !== game.target)
gen_action_card(c)
}
},
card(c) {
remove_dice(S29_MEADE)
game.target = c
update_attack1(false)
update_attack2()
game.state = "absorb"
}
}
states.absorb = {
inactive: "absorb",
prompt() {
view.prompt = "Absorb attack from " + card_name(game.selected) + "."
view.actions.absorb = 1
gen_action_card(game.selected)
},
absorb() {
log("Absorb\nC" + game.target)
pay_for_action(game.target)
end_reaction()
},
card(_) {
this.absorb()
},
}
// === COUNTERATTACK ===
function goto_counterattack(c, a) {
game.reacted = player_index()
update_attack1(false)
switch (a.effect)
{
default:
throw new Error("invalid counterattack effect: " + a.effect)
case "1 hit.":
game.self += 1
break
case "1 hit per die.":
game.self += count_dice_on_card(c)
break
case "1 hit. Suffers 1 hit only.":
game.self += 1
game.hits = 1
break
case "Suffers 1 less hit.":
game.hits = Math.max(0, game.hits - 1)
break
case "1 hit. Suffers 1 less hit.":
game.self += 1
game.hits = Math.max(0, game.hits - 1)
break
case "1 hit. Suffers 1 less hit per die.":
game.self += 1
game.hits = Math.max(0, game.hits - count_dice_on_card(c))
break
case "1 hit. Suffers 1 less hit and never more than 1.":
game.self += 1
game.hits = Math.max(0, Math.min(1, game.hits - 1))
break
case "1 hit. Suffers 2 less hits and never more than 1.":
game.self += 1
game.hits = Math.max(0, Math.min(1, game.hits - 2))
break
}
if (game.scenario === S48_BRESLAU) {
if (player_index() === 1) {
if (has_any_dice_on_card(S48_BEVERN))
game.self += 1
}
}
update_attack2()
game.state = "counterattack"
}
states.counterattack = {
inactive: "counterattack",
prompt() {
view.prompt = "Counterattack " + card_name(game.selected) + "."
view.actions.counterattack = 1
gen_action_card(game.selected)
},
counterattack() {
log("Counterattack\nC" + game.target)
pay_for_action(game.target)
end_reaction()
},
card(_) {
this.counterattack()
},
}
// === ROUTING/PURSUIT/REMOVE/FORCE-RETIRE ===
function find_card_owner(c) {
if (set_has(game.front[0], c) || set_has(game.reserve[0], c))
return 0
if (set_has(game.front[1], c) || set_has(game.reserve[1], c))
return 1
throw new Error("card not found in any player area")
}
function should_rout_card(c) {
if (!data.cards[c].special) {
if (get_sticks(c) === 0)
return true
}
let rout_with = card_has_rule(c, "rout_with")
if (rout_with) {
for (let other of rout_with)
if (!is_removed_from_play(other))
return false
return true
}
return false
}
function should_pursue(c) {
let pursuit = data.cards[c].pursuit
if (pursuit !== undefined)
return !set_has(game.front[0], pursuit) && !set_has(game.front[1], pursuit)
return false
}
function should_remove_card(c) {
let remove_with = card_has_rule(c, "remove_with")
if (remove_with) {
for (let other of remove_with)
if (!is_removed_from_play(other))
return false
return true
}
return false
}
function should_retire_card(c) {
let retire_with = card_has_rule(c, "retire_with")
if (retire_with) {
for (let other of retire_with)
if (!is_removed_from_play(other))
return false
return true
}
if (game.scenario === S25_WHEATFIELD) {
if (c === S25_ZOOK || c === S25_KELLY) {
if (is_card_in_play(S25_WOFFORD))
return true
}
}
return false
}
function goto_routing() {
game.routed = [ 0, 0 ]
if (game.scenario === S2_MARSTON_MOOR) {
// TODO: pause with separate state?
if (is_card_in_play(S2_RUPERTS_LIFEGUARD)) {
if (should_rout_card(S2_NORTHERN_HORSE)) {
log("Rupert's Lifeguard added to Northern Horse.")
move_all_sticks(S2_RUPERTS_LIFEGUARD, S2_NORTHERN_HORSE)
remove_card(S2_RUPERTS_LIFEGUARD)
}
if (should_rout_card(S2_BYRON)) {
log("Rupert's Lifeguard added to Byron.")
move_all_sticks(S2_RUPERTS_LIFEGUARD, S2_BYRON)
remove_card(S2_RUPERTS_LIFEGUARD)
}
}
}
resume_routing()
}
function resume_routing() {
game.state = "routing"
for (let p = 0; p <= 1; ++p) {
for (let c of game.front[p])
if (should_rout_card(c) || should_remove_card(c) || should_retire_card(c) || should_pursue(c))
return
for (let c of game.reserve[p])
if (should_rout_card(c) || should_remove_card(c) || should_retire_card(c))
return
}
end_routing()
}
states.routing = {
inactive: "remove routing and pursuing cards",
prompt() {
view.prompt = "Routing: Remove routing and pursuing cards from play!"
for (let p = 0; p <= 1; ++p) {
for (let c of game.front[p])
if (should_rout_card(c) || should_remove_card(c) || should_retire_card(c) || should_pursue(c))
gen_action_card(c)
for (let c of game.reserve[p])
if (should_rout_card(c) || should_remove_card(c) || should_retire_card(c))
gen_action_card(c)
}
},
card(c) {
if (should_rout_card(c)) {
let p = find_card_owner(c)
game.routed[p] += data.cards[c].morale
rout_card(c)
} else if (should_retire_card(c)) {
log("Retire\nC" + c)
retire_card(c)
} else if (should_pursue(c)) {
log("Pursuit\nC" + c)
pursue_card(c)
} else {
remove_card(c)
}
resume_routing()
},
}
function end_routing() {
// Normal morale loss and gain
if (game.morale[0] > 0 && game.morale[1] > 0) {
if ((game.routed[0] > 0 && !game.routed[1]) || (game.routed[1] > 0 && !game.routed[0])) {
if (game.routed[0]) {
game.routed[0] = Math.min(game.routed[0], game.morale[0])
game.morale[0] -= game.routed[0]
// do not gain for special scenarios
if (game.morale[1] > 0)
game.morale[1] += game.routed[0]
} else {
game.routed[1] = Math.min(game.routed[1], game.morale[1])
game.morale[1] -= game.routed[1]
// do not gain for special scenarios
if (game.morale[0] > 0)
game.morale[0] += game.routed[1]
}
}
if (check_victory())
return
} else {
// SPECIAL: S3 - Plains of Abraham
// SPECIAL: S34 - Tippermuir - Royalists
// SPECIAL: S35 - Auldearn - Royalists
// Instant loss if any card routs for side at 0 morale (S3, S34, S35).
if (game.morale[0] === 0 && game.routed[0])
return goto_game_over(P2, player_name(0) + " routed!")
if (game.morale[1] === 0 && game.routed[1])
return goto_game_over(P1, player_name(1) + " routed!")
// Remove instead of take cubes for side at 0 morale
if (game.routed[0]) {
game.morale[0] -= Math.min(game.routed[0], game.morale[0])
if (game.morale[0] === 0)
return goto_game_over(P2, player_name(0) + " ran out of morale!")
}
if (game.routed[1]) {
game.morale[1] -= Math.min(game.routed[1], game.morale[1])
if (game.morale[1] === 0)
return goto_game_over(P1, player_name(1) + " ran out of morale!")
}
}
game.routed = null
goto_reserve()
}
// === RESERVE ===
function should_enter_reserve(c) {
let reserve = data.cards[c].reserve
if (game.scenario === S37_INKERMAN) {
if (c === S37_BRITISH_TROOPS)
return get_cubes(S37_THE_FOG) === 1
if (c === S37_FRENCH_TROOPS)
return get_cubes(S37_THE_FOG) === 0
}
if (Array.isArray(reserve)) {
for (let t of reserve) {
if (is_removed_from_play(t))
return true
}
}
if (game.scenario === S30_EDGEHILL) {
if (c === S30_BALFOUR || c === S30_STAPLETON) {
return is_removed_from_play(S30_RUPERT) && is_removed_from_play(S30_WILMOT)
}
}
return false
}
function goto_reserve() {
resume_reserve()
}
function resume_reserve() {
game.state = "reserve"
for (let p = 0; p <= 1; ++p)
for (let c of game.reserve[p])
if (should_enter_reserve(c))
return
end_reserve()
}
function end_reserve() {
goto_roll_phase()
}
function bring_out_of_reserve(c) {
log("Reserve\nC" + c)
let p = find_card_owner(c)
set_delete(game.reserve[p], c)
set_add(game.front[p], c)
}
states.reserve = {
inactive: "enter reserves",
prompt() {
view.prompt = "Enter reserves!"
for (let p = 0; p <= 1; ++p)
for (let c of game.reserve[p])
if (should_enter_reserve(c))
gen_action_card(c)
},
card(c) {
bring_out_of_reserve(c)
resume_reserve()
},
}
// === COMMON LIBRARY ===
function gen_action(action, argument) {
if (!(action in view.actions))
view.actions[action] = [ argument ]
else
set_add(view.actions[action], argument)
}
function gen_action_dice_on_card(c) {
for (let d = 0; d < 12; ++d) {
if (get_dice_location(d) === c) {
gen_action_die(d)
return
}
}
}
function gen_action_card(c) {
gen_action("card", c)
}
function gen_action_die(d) {
gen_action("die", d)
}
function gen_action_action1(c) {
gen_action("a1", c)
}
function gen_action_action2(c) {
gen_action("a2", c)
}
function gen_action_null1(c) {
gen_action("n1", c)
}
function gen_action_null2(c) {
gen_action("n2", c)
}
function gen_action_retire(c) {
gen_action("retire", c)
}
function log(msg) {
game.log.push(msg)
}
function clear_undo() {
if (game.undo) {
game.undo.length = 0
}
}
function push_undo() {
if (game.undo) {
let copy = {}
for (let k in game) {
let v = game[k]
if (k === "undo")
continue
else if (k === "log")
v = v.length
else if (typeof v === "object" && v !== null)
v = object_copy(v)
copy[k] = v
}
game.undo.push(copy)
}
}
function pop_undo() {
if (game.undo) {
let save_log = game.log
let save_undo = game.undo
game = save_undo.pop()
save_log.length = game.log
game.log = save_log
game.undo = save_undo
}
}
function random(range) {
// Largest MLCG that will fit its state in a double.
// Uses BigInt for arithmetic, so is an order of magnitude slower.
// https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/S0025-5718-99-00996-5.pdf
// m = 2**53 - 111
return (game.seed = Number(BigInt(game.seed) * 5667072534355537n % 9007199254740881n)) % range
}
// Fast deep copy for objects without cycles
function object_copy(original) {
if (Array.isArray(original)) {
let n = original.length
let copy = new Array(n)
for (let i = 0; i < n; ++i) {
let v = original[i]
if (typeof v === "object" && v !== null)
copy[i] = object_copy(v)
else
copy[i] = v
}
return copy
} else {
let copy = {}
for (let i in original) {
let v = original[i]
if (typeof v === "object" && v !== null)
copy[i] = object_copy(v)
else
copy[i] = v
}
return copy
}
}
// Array remove and insert (faster than splice)
function array_remove(array, index) {
let n = array.length
for (let i = index + 1; i < n; ++i)
array[i - 1] = array[i]
array.length = n - 1
}
function array_insert(array, index, item) {
for (let i = array.length; i > index; --i)
array[i] = array[i - 1]
array[index] = item
}
function array_remove_pair(array, index) {
let n = array.length
for (let i = index + 2; i < n; ++i)
array[i - 2] = array[i]
array.length = n - 2
}
function array_insert_pair(array, index, key, value) {
for (let i = array.length; i > index; i -= 2) {
array[i] = array[i-2]
array[i+1] = array[i-1]
}
array[index] = key
array[index+1] = value
}
// Set as plain sorted array
function set_has(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else
return true
}
return false
}
function set_add(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else
return
}
array_insert(set, a, item)
}
function set_delete(set, item) {
let a = 0
let b = set.length - 1
while (a <= b) {
let m = (a + b) >> 1
let x = set[m]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else {
array_remove(set, m)
return
}
}
}
// Map as plain sorted array of key/value pairs
function map_clear(map) {
map.length = 0
}
function map_has(map, key) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else
return true
}
return false
}
function map_get(map, key, missing) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else
return map[(m<<1)+1]
}
return missing
}
function map_set(map, key, value) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (key < x)
b = m - 1
else if (key > x)
a = m + 1
else {
map[(m<<1)+1] = value
return
}
}
array_insert_pair(map, a<<1, key, value)
}
function map_delete(map, item) {
let a = 0
let b = (map.length >> 1) - 1
while (a <= b) {
let m = (a + b) >> 1
let x = map[m<<1]
if (item < x)
b = m - 1
else if (item > x)
a = m + 1
else {
array_remove_pair(map, m<<1)
return
}
}
}
|