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
|
"use strict";
// Battle Card timing:
// us: before interception roll
// tr: after interception roll
// tr: before pirate raid
// tr: after pirate raid
// us: before naval battle
// tr: before naval battle
// us: before land battle
// tr: before land battle
// us: own reaction: burn the philadelphia
// us: own reaction: launch the intrepid
// us: own reaction: assault on tripoli
// tr: own reaction: philadelphia runs aground
const US = "United States";
const TR = "Tripolitania";
const { SPACES, PIECES, SEASONS } = require('./data');
exports.scenarios = [
"Historical",
];
function get_piece_id(name) {
return PIECES.indexOf(name);
}
function get_space_id(name) {
return SPACES.indexOf(name);
}
function create_piece_list(n, name) {
let list = [];
for (let i = 1; i <= n; ++i)
list.push(get_piece_id(name + i));
return list;
}
const US_FRIGATES = create_piece_list(8, 'us_frigate_');
const TR_FRIGATES = create_piece_list(2, 'tr_frigate_');
const SE_FRIGATES = create_piece_list(2, 'se_frigate_');
const US_GUNBOATS = create_piece_list(3, 'us_gunboat_');
const TR_CORSAIRS = create_piece_list(9, 'tr_corsair_');
const AL_CORSAIRS = create_piece_list(9, 'al_corsair_');
const US_MARINES = create_piece_list(4, 'us_marine_');
const AR_INFANTRY = create_piece_list(10, 'ar_infantry_');
const TR_INFANTRY = create_piece_list(20, 'tr_infantry_');
const ALEXANDRIA_HARBOR = get_space_id("Alexandria Harbor");
const ALGIERS_HARBOR = get_space_id("Algiers Harbor");
const ALGIERS_PATROL_ZONE = get_space_id("Algiers Patrol Zone");
const BENGHAZI_HARBOR = get_space_id("Benghazi Harbor");
const DERNE_HARBOR = get_space_id("Derne Harbor");
const GIBRALTAR_HARBOR = get_space_id("Gibraltar Harbor");
const GIBRALTAR_PATROL_ZONE = get_space_id("Gibraltar Patrol Zone");
const MALTA_HARBOR = get_space_id("Malta Harbor");
const TANGIER_HARBOR = get_space_id("Tangier Harbor");
const TANGIER_PATROL_ZONE = get_space_id("Tangier Patrol Zone");
const TRIPOLI_HARBOR = get_space_id("Tripoli Harbor");
const TRIPOLI_PATROL_ZONE = get_space_id("Tripoli Patrol Zone");
const TUNIS_HARBOR = get_space_id("Tunis Harbor");
const TUNIS_PATROL_ZONE = get_space_id("Tunis Patrol Zone");
const UNITED_STATES_SUPPLY = get_space_id("United States Supply");
const TRIPOLITAN_SUPPLY = get_space_id("Tripolitan Supply");
const TRACK_1801 = get_space_id("1801");
const TRACK_1802 = get_space_id("1802");
const TRACK_1803 = get_space_id("1803");
const TRACK_1804 = get_space_id("1804");
const TRACK_1805 = get_space_id("1805");
const TRACK_1806 = get_space_id("1806");
const TRACK_YEAR = {
1801: TRACK_1801,
1802: TRACK_1802,
1803: TRACK_1803,
1804: TRACK_1804,
1805: TRACK_1805,
1806: TRACK_1806,
};
const FRIGATE_SPACES = [
ALEXANDRIA_HARBOR,
ALGIERS_HARBOR,
ALGIERS_PATROL_ZONE,
BENGHAZI_HARBOR,
DERNE_HARBOR,
GIBRALTAR_HARBOR,
GIBRALTAR_PATROL_ZONE,
MALTA_HARBOR,
TANGIER_HARBOR,
TANGIER_PATROL_ZONE,
TRIPOLI_HARBOR,
TRIPOLI_PATROL_ZONE,
TUNIS_HARBOR,
TUNIS_PATROL_ZONE,
];
const BATTLE_SPACES = [
ALEXANDRIA_HARBOR,
ALGIERS_HARBOR,
BENGHAZI_HARBOR,
DERNE_HARBOR,
TANGIER_HARBOR,
TRIPOLI_HARBOR,
TUNIS_HARBOR,
];
const THOMAS_JEFFERSON = 1;
const SWEDISH_FRIGATES_ARRIVE = 2;
const HAMETS_ARMY_CREATED = 3;
const TREATY_OF_PEACE_AND_AMITY = 4;
const ASSAULT_ON_TRIPOLI = 5;
const NAVAL_MOVEMENT_1 = 6;
const NAVAL_MOVEMENT_2 = 7;
const NAVAL_MOVEMENT_3 = 8;
const NAVAL_MOVEMENT_4 = 9;
const EARLY_DEPLOYMENT = 10;
const A_SHOW_OF_FORCE = 11;
const TRIBUTE_PAID = 12;
const CONSTANTINOPLE_DEMANDS_TRIBUTE = 13;
const HAMET_RECRUITS_BEDOUINS = 14;
const BAINBRIDGE_SUPPLIES_INTEL = 15;
const CONGRESS_AUTHORIZES_ACTION = 16;
const CORSAIRS_CONFISCATED = 17;
const BURN_THE_PHILADELPHIA = 18;
const LAUNCH_THE_INTREPID = 19;
const GENERAL_EATON_ATTACKS_DERNE = 20;
const GENERAL_EATON_ATTACKS_BENGHAZI = 21;
const LIEUTENANT_STERETT_IN_PURSUIT = 22;
const PREBLES_BOYS_TAKE_AIM = 23;
const THE_DARING_STEPHEN_DECATUR = 24;
const SEND_IN_THE_MARINES = 25;
const LIEUTENANT_OBANNON_LEADS_THE_CHARGE = 26;
const MARINE_SHARPSHOOTERS = 27;
const YUSUF_QARAMANLI = 28;
const MURAD_REIS_BREAKS_OUT = 29;
const CONSTANTINOPLE_SENDS_AID = 30;
const US_SUPPLIES_RUN_LOW = 31;
const ALGERINE_CORSAIRS_RAID_1 = 32;
const ALGERINE_CORSAIRS_RAID_2 = 33;
const MOROCCAN_CORSAIRS_RAID_1 = 34;
const MOROCCAN_CORSAIRS_RAID_2 = 35;
const TUNISIAN_CORSAIRS_RAID_1 = 36;
const TUNISIAN_CORSAIRS_RAID_2 = 37;
const TROOPS_TO_DERNE = 38;
const TROOPS_TO_BENGHAZI = 39;
const TROOPS_TO_TRIPOLI = 40;
const STORMS = 41;
const TRIPOLI_ATTACKS = 42;
const SWEDEN_PAYS_TRIBUTE = 43;
const TRIPOLI_ACQUIRES_CORSAIRS = 44;
const THE_PHILADELPHIA_RUNS_AGROUND = 45;
const ALGIERS_DECLARES_WAR = 46;
const MOROCCO_DECLARES_WAR = 47;
const TUNIS_DECLARES_WAR = 48;
const US_SIGNAL_BOOKS_OVERBOARD = 49;
const UNCHARTED_WATERS = 50;
const MERCHANT_SHIP_CONVERTED = 51;
const HAPPY_HUNTING = 52;
const THE_GUNS_OF_TRIPOLI = 53;
const MERCENARIES_DESERT = 54;
const CARD_NAMES = [
// No Card
null,
// United States Cards
"Thomas Jefferson",
"Swedish Frigates Arrive",
"Hamet's Army Created",
"Treaty of Peace and Amity",
"Assault on Tripoli",
"Naval Movement",
"Naval Movement",
"Naval Movement",
"Naval Movement",
"Early Deployment",
"A Show of Force",
"Tribute Paid",
"Constantinople Demands Tribute",
"Hamet Recruits Bedouins",
"Bainbridge Supplies Intel",
"Congress Authorizes Action",
"Corsairs Confiscated",
"Burn the Philadelphia",
"Launch the Intrepid",
"General Eaton Attacks Derne",
"General Eaton Attacks Benghazi",
"Lieutenant Sterett in Pursuit",
"Preble's Boys Take Aim",
"The Daring Stephen Decatur",
"Send in the Marines",
"Lieutenant O'Bannon Leads the Charge",
"Marine Sharpshooters",
// Tripolitan Cards
"Yusuf Qaramanli",
"Murad Reis Breaks Out",
"Constantinople Sends Aid",
"US Supplies Run Low",
"Algerine Corsairs Raid",
"Algerine Corsairs Raid",
"Moroccan Corsairs Raid",
"Moroccan Corsairs Raid",
"Tunisian Corsairs Raid",
"Tunisian Corsairs Raid",
"Troops to Derne",
"Troops to Benghazi",
"Troops to Tripoli",
"Storms",
"Tripoli Attacks",
"Sweden Pays Tribute",
"Tripoli Acquires Corsairs",
"The Philadelphia Runs Aground",
"Algiers Declares War",
"Morocco Declares War",
"Tunis Declares War",
"US Signal Books Overboard",
"Uncharted Waters",
"Merchant Ship Converted",
"Happy Hunting",
"The Guns of Tripoli",
"Mercenaries Desert",
];
function should_remove_after_play(card) {
switch (card) {
case THOMAS_JEFFERSON:
case SWEDISH_FRIGATES_ARRIVE:
case HAMETS_ARMY_CREATED:
case CONGRESS_AUTHORIZES_ACTION:
case CORSAIRS_CONFISCATED:
case BURN_THE_PHILADELPHIA:
case LAUNCH_THE_INTREPID:
case GENERAL_EATON_ATTACKS_DERNE:
case GENERAL_EATON_ATTACKS_BENGHAZI:
case LIEUTENANT_STERETT_IN_PURSUIT:
case PREBLES_BOYS_TAKE_AIM:
case THE_DARING_STEPHEN_DECATUR:
case SEND_IN_THE_MARINES:
case LIEUTENANT_OBANNON_LEADS_THE_CHARGE:
case MARINE_SHARPSHOOTERS:
case YUSUF_QARAMANLI:
case MURAD_REIS_BREAKS_OUT:
case CONSTANTINOPLE_SENDS_AID:
case SWEDEN_PAYS_TRIBUTE:
case TRIPOLI_ACQUIRES_CORSAIRS:
case THE_PHILADELPHIA_RUNS_AGROUND:
case ALGIERS_DECLARES_WAR:
case MOROCCO_DECLARES_WAR:
case TUNIS_DECLARES_WAR:
case US_SIGNAL_BOOKS_OVERBOARD:
case UNCHARTED_WATERS:
case MERCHANT_SHIP_CONVERTED:
case HAPPY_HUNTING:
case THE_GUNS_OF_TRIPOLI:
case MERCENARIES_DESERT:
return true;
}
return false;
}
const states = {};
let game = null;
function log(...args) {
let s = Array.from(args).join("");
game.log.push(s);
}
function logp(...args) {
let s = Array.from(args).join("");
game.log.push(game.active + " " + s);
}
function is_inactive_player(current) {
return current == "Observer" || (game.active != current && game.active != "Both");
}
function remove_from_array(array, item) {
let i = array.indexOf(item);
if (i >= 0)
array.splice(i, 1);
}
function clear_undo() {
if (game.undo)
game.undo.length = 0;
else
game.undo = [];
}
function push_undo() {
game.undo.push(JSON.stringify(game, (k,v) => {
if (k === 'undo') return undefined;
if (k === 'log') return v.length;
return v;
}));
}
function pop_undo() {
let undo = game.undo;
let log = game.log;
Object.assign(game, JSON.parse(undo.pop()));
game.undo = undo;
log.length = game.log;
game.log = log;
}
function gen_action_undo(view) {
if (!view.actions)
view.actions = {}
if (game.undo && game.undo.length > 0)
view.actions.undo = 1;
else
view.actions.undo = 0;
}
function gen_action_pass(view, text) {
if (!view.actions)
view.actions = {}
view.actions['pass'] = text;
}
function gen_action(view, action, argument) {
if (!view.actions)
view.actions = {}
if (argument != undefined) {
if (!(action in view.actions))
view.actions[action] = [ argument ];
else
view.actions[action].push(argument);
} else {
view.actions[action] = 1;
}
}
function roll_d6() {
return Math.floor(Math.random() * 6) + 1;
}
function reset_deck() {
let deck = [];
for (let c = 1; c <= 27; ++c)
deck.push(c);
return deck;
}
function reshuffle_discard(deck, discard) {
while (discard.length > 0)
deck.push(discard.pop());
}
function draw_cards(hand, deck, n) {
for (let i = 0; i < n; ++i) {
let c = Math.floor(Math.random() * deck.length);
hand.push(deck[c]);
deck.splice(c, 1);
}
}
function count_pieces(list, where) {
let n = 0;
for (let p of list)
if (game.location[p] == where)
++n;
return n;
}
function discard_card(player, card, reason = "") {
log("");
// log(game.active + " discards \"" + CARD_NAMES[card] + "\"" + reason + ".");
log(game.active + " discards a card" + reason + ".");
remove_from_array(player.hand, card);
player.discard.push(card);
game.active_card = game.active;
}
function play_card(player, card) {
log("");
log(game.active + " plays \"" + CARD_NAMES[card] + "\".");
remove_from_array(player.hand, card);
if (!should_remove_after_play(card))
player.discard.push(card);
game.active_card = card;
}
function deploy(piece_name, space) {
game.location[get_piece_id(piece_name)] = space;
}
function move_one_piece(list, from, to) {
for (let p of list) {
if (game.location[p] == from) {
game.location[p] = to;
return;
}
}
throw Error("no " + list + " to move from " + from);
}
function move_all_pieces(list, from, to) {
for (let p of list) {
if (game.location[p] == from) {
game.location[p] = to;
}
}
}
function fire(what, n_dice) {
let hits = 0;
for (let i = 0; i < n_dice; ++i) {
let roll = roll_d6();
if (roll == 6)
++hits;
log(what + " fires " + roll + ".");
}
return hits;
}
function intercept(what, n) {
let hits = 0;
for (let i = 0; i < n; ++i) {
let a = roll_d6();
let b = roll_d6();
if (a == 6) ++hits;
if (b == 6) ++hits;
log(what + " intercepts " + a + ", " + b + ".");
}
return hits;
}
function capture(what, n) {
let hits = 0;
for (let i = 0; i < n; ++i) {
let roll = roll_d6();
if (roll >= 5) ++hits;
log(what + " captures " + roll + ".");
}
return hits;
}
function count_swedish_frigates(where) {
return count_pieces(SE_FRIGATES, where);
}
function count_american_frigates(where) {
return count_pieces(US_FRIGATES, where);
}
function count_american_gunboats(where) {
return count_pieces(US_GUNBOATS, where);
}
function count_tripolitan_corsairs(where) {
return count_pieces(TR_CORSAIRS, where);
}
function count_tripolitan_frigates(where) {
return count_pieces(TR_FRIGATES, where);
}
function count_allied_corsairs(where) {
return count_pieces(AL_CORSAIRS, where);
}
function count_tripolitan_infantry(where) {
return count_pieces(TR_INFANTRY, where);
}
function can_play_thomas_jefferson() {
return game.us.core.includes(THOMAS_JEFFERSON);
}
function can_play_swedish_frigates_arrive() {
return game.us.core.includes(SWEDISH_FRIGATES_ARRIVE);
}
function can_play_hamets_army_created() {
return game.us.core.includes(HAMETS_ARMY_CREATED) &&
count_american_frigates(ALEXANDRIA_HARBOR) > 0 &&
game.year >= 1804;
}
function can_play_yusuf_qaramanli() {
let n = count_allied_corsairs(ALGIERS_HARBOR) +
count_allied_corsairs(TRIPOLI_HARBOR) +
count_allied_corsairs(TUNIS_HARBOR);
return game.tr.core.includes(YUSUF_QARAMANLI) && n > 0;
}
function can_play_murad_reis_breaks_out() {
return game.tr.core.includes(MURAD_REIS_BREAKS_OUT);
}
function can_play_constantinople_sends_aid() {
return game.tr.core.includes(CONSTANTINOPLE_SENDS_AID) && game.derne_captured;
}
function can_build_gunboat_in_malta() {
return count_pieces(US_GUNBOATS, UNITED_STATES_SUPPLY) > 0;
}
function can_build_corsair_in_tripoli() {
return count_pieces(TR_CORSAIRS, TRIPOLITAN_SUPPLY) > 0;
}
function can_pirate_raid_from_tripoli() {
return count_pieces(TR_CORSAIRS, TRIPOLI_HARBOR) > 0;
}
function start_of_year() {
log("");
log("Start of " + game.year + ".");
game.season = SPRING;
move_all_pieces(US_FRIGATES, TRACK_YEAR[game.year], GIBRALTAR_HARBOR);
move_all_pieces(TR_FRIGATES, TRACK_YEAR[game.year], TRIPOLI_HARBOR);
if (game.year <= 1804) {
draw_cards(game.us.hand, game.us.deck, 6);
draw_cards(game.tr.hand, game.tr.deck, 6);
}
if (game.year == 1805) {
reshuffle_discard(game.us.deck, game.us.discard);
draw_cards(game.us.hand, game.us.deck, 6);
reshuffle_discard(game.tr.deck, game.tr.discard);
draw_cards(game.tr.hand, game.tr.deck, 6);
}
if (game.year == 1806) {
draw_cards(game.us.hand, game.us.deck, game.us.deck.length);
draw_cards(game.tr.hand, game.tr.deck, game.tr.deck.length);
}
// TODO: hand limit
game.active = US;
game.state = 'american_play';
}
function end_american_play() {
clear_undo();
game.active = TR;
game.state = 'tripolitan_play';
}
function end_tripolitan_play() {
clear_undo();
end_of_season();
}
const SPRING = 0;
const SUMMER = 1;
const FALL = 2;
const WINTER = 3;
function end_of_season() {
if (game.season == WINTER) {
end_of_year();
} else {
++game.season;
game.active = US;
game.state = 'american_play';
}
}
function end_of_year() {
if (game.year == 1806) {
game.result = 'Draw';
game.victory = "The game ends in a draw.";
game.state = 'game_over';
return;
}
++game.year;
start_of_year();
}
function can_play_american_event(c) {
return false;
}
function can_play_tripolitan_event(c) {
return false;
}
states.american_play = {
prompt: function (view, current) {
if (is_inactive_player(current))
return view.prompt = "American play.";
view.prompt = "American play.";
if (can_play_thomas_jefferson())
gen_action(view, 'card_event', THOMAS_JEFFERSON);
if (can_play_swedish_frigates_arrive())
gen_action(view, 'card_event', SWEDISH_FRIGATES_ARRIVE);
if (can_play_hamets_army_created())
gen_action(view, 'card_event', HAMETS_ARMY_CREATED);
let build = can_build_gunboat_in_malta();
for (let c of game.us.hand) {
gen_action(view, 'card_move_frigates', c);
if (build)
gen_action(view, 'card_build_gunboat', c);
if (can_play_american_event(c))
gen_action(view, 'card_event', c);
}
if (game.us.hand.length == 0)
gen_action(view, 'pass');
},
card_build_gunboat: function (c) {
discard_card(game.us, c, " to build a gunboat in Malta");
move_one_piece(US_GUNBOATS, UNITED_STATES_SUPPLY, MALTA_HARBOR);
end_american_play();
},
card_move_frigates: function (c) {
discard_card(game.us, c, " to move up to two frigates");
goto_move_up_to_n_american_frigates(2);
},
pass: function () {
log("");
log(game.active + " passes.");
end_american_play();
}
}
states.tripolitan_play = {
prompt: function (view, current) {
if (is_inactive_player(current))
return view.prompt = "Tripolitan play.";
view.prompt = "Tripolitan play.";
if (can_play_yusuf_qaramanli())
gen_action(view, 'card_event', YUSUF_QARAMANLI);
if (can_play_murad_reis_breaks_out())
gen_action(view, 'card_event', MURAD_REIS_BREAKS_OUT);
if (can_play_constantinople_sends_aid())
gen_action(view, 'card_event', CONSTANTINOPLE_SENDS_AID);
let build = can_build_corsair_in_tripoli();
let raid = can_pirate_raid_from_tripoli();
for (let c of game.tr.hand) {
if (build)
gen_action(view, 'card_build_corsair', c);
if (raid)
gen_action(view, 'card_pirate_raid', c);
if (can_play_tripolitan_event(c))
gen_action(view, 'card_event', c);
}
if (game.tr.hand.length == 0)
gen_action(view, 'pass');
},
card_build_corsair: function (c) {
discard_card(game.tr, c, " to build a Tripolitan corsair in Tripoli");
move_one_piece(TR_CORSAIRS, TRIPOLITAN_SUPPLY, TRIPOLI_HARBOR);
end_tripolitan_play();
},
card_pirate_raid: function (c) {
discard_card(game.tr, c, " to Pirate Raid with the corsairs from Tripoli");
goto_pirate_raid(TRIPOLI_HARBOR, TRIPOLI_PATROL_ZONE);
},
pass: function () {
log("");
log(game.active + " passes.");
end_tripolitan_play();
}
}
function goto_pirate_raid(harbor, patrol_zone) {
game.where = patrol_zone;
interception_roll(harbor, patrol_zone);
capture_roll(harbor);
game.where = null;
end_tripolitan_play();
}
function interception_roll(harbor, patrol_zone) {
let n_se = count_swedish_frigates(patrol_zone);
let n_us = count_american_frigates(patrol_zone);
let n_tr = count_tripolitan_corsairs(harbor);
let n_al = count_allied_corsairs(harbor);
let hits = 0;
hits += intercept("Swedish frigate", n_se);
hits += intercept("US frigate", n_us);
if (hits > n_tr + n_al)
hits = n_tr + n_al;
log(hits + " corsairs sink.");
if (n_tr > 0)
for (let i = 0; i < hits; ++i)
move_one_piece(TR_CORSAIRS, harbor, TRIPOLITAN_SUPPLY);
else
for (let i = 0; i < hits; ++i)
move_one_piece(AL_CORSAIRS, harbor, TRIPOLITAN_SUPPLY);
}
function capture_roll(harbor) {
let hits = 0;
hits += capture("Tripolitan corsair", count_tripolitan_corsairs(harbor));
hits += capture("Allied corsair", count_allied_corsairs(harbor));
log(hits + " merchant ships captured.");
game.tr.gold += hits;
if (game.tr.gold > 12)
game.tr.gold = 12;
// TODO: check victory
}
function goto_move_up_to_n_american_frigates(n) {
game.moves = n;
game.active = US;
game.state = 'move_us_frigate_from';
push_undo();
}
function is_naval_battle_location(space) {
let n_us = count_american_frigates(space);
let n_tr = count_tripolitan_corsairs(space) + count_allied_corsairs(space);
return (n_us > 0 && n_tr > 0);
}
function is_naval_bombardment_location(space) {
let n_us = count_american_frigates(space);
let n_tr = count_tripolitan_infantry(space);
return (n_us > 0 && n_tr > 0);
}
function is_naval_battle_or_bombardment_location(space) {
let n_us = count_american_frigates(space);
let n_tr_ships = count_tripolitan_corsairs(space) + count_allied_corsairs(space);
let n_tr_infantry = count_tripolitan_infantry(space);
return (n_us > 0 && (n_tr_ships > 0 || n_tr_infantry > 0));
}
function count_naval_battle_or_bombardment_locations() {
let n = 0;
for (let space of BATTLE_SPACES)
if (is_naval_battle_or_bombardment_location(space))
++n;
return n;
}
// TODO: click 'from' location to go back to selecting source?
states.move_us_frigate_from = {
prompt: function (view, current) {
if (is_inactive_player(current))
return view.prompt = "United States: Move up to " + game.moves + " frigates.";
view.prompt = "United States: Move up to " + game.moves + " frigates."
if (game.moves > 0) {
view.prompt += " Select a frigate to move.";
for (let space of FRIGATE_SPACES) {
if (count_american_frigates(space) > 0)
gen_action(view, 'space', space);
}
}
gen_action(view, 'next');
gen_action_undo(view);
},
space: function (space) {
push_undo();
game.where = space;
game.state = 'move_us_frigate_to'
},
next: function () {
if (count_naval_battle_or_bombardment_locations() > 0)
goto_allocate_gunboats();
else
end_american_play();
},
undo: pop_undo
}
states.move_us_frigate_to = {
prompt: function (view, current) {
if (is_inactive_player(current))
return view.prompt = "United States: Move up to " + game.moves + " frigates.";
view.prompt = "United States: Move up to " + game.moves + " frigates. Select a destination.";
for (let space of FRIGATE_SPACES)
if (space != game.where)
gen_action(view, 'space', space);
gen_action_undo(view);
},
space: function (space) {
log(game.active + " moves a frigate from " + SPACES[game.where] + " to " + SPACES[space] + ".");
move_one_piece(US_FRIGATES, game.where, space);
--game.moves;
game.where = null;
game.state = 'move_us_frigate_from'
},
undo: pop_undo
}
function goto_allocate_gunboats() {
if (count_american_gunboats(MALTA_HARBOR) == 0)
return goto_select_combat();
game.where = MALTA_HARBOR;
game.state = 'allocate_gunboats';
}
states.allocate_gunboats = {
prompt: function (view, current) {
view.prompt = "United States: Allocate gunboats to battle locations.";
if (is_inactive_player(current))
return view.prompt;
if (count_american_gunboats(MALTA_HARBOR) > 0) {
for (let space of BATTLE_SPACES)
if (is_naval_battle_or_bombardment_location(space))
gen_action(view, 'space', space);
}
gen_action(view, 'next');
gen_action_undo(view);
},
space: function (space) {
push_undo();
log(game.active + " moves a gunboat to " + SPACES[space] + ".");
move_one_piece(US_GUNBOATS, MALTA_HARBOR, space);
},
next: function () {
game.where = null;
goto_select_combat();
},
undo: pop_undo
}
function goto_select_combat() {
clear_undo();
// TODO: auto-select if only one location?
if (count_naval_battle_or_bombardment_locations() > 0)
game.state = 'select_combat';
else
end_american_play();
}
states.select_combat = {
prompt: function (view, current) {
view.prompt = "United States: Pick the next naval combat or bombardment."
if (is_inactive_player(current))
return view.prompt;
for (let space of BATTLE_SPACES)
if (is_naval_battle_or_bombardment_location(space))
gen_action(view, 'space', space);
},
space: function (space) {
game.where = space;
if (is_naval_battle_location(space))
goto_naval_battle();
else
goto_naval_bombardment();
},
}
function goto_naval_battle() {
log("Naval battle in " + SPACES[game.where] + ".");
// TODO: battle cards
naval_battle_round();
}
function naval_battle_round() {
let n_us_frigates = count_american_frigates(game.where);
let n_us_gunboats = count_american_gunboats(game.where);
let us_hitpoints = n_us_frigates * 2 + n_us_gunboats;
let n_tr_frigates = count_tripolitan_frigates(game.where);
let n_tr_corsairs = count_tripolitan_corsairs(game.where);
let n_al_corsairs = count_allied_corsairs(game.where);
let tr_hitpoints = n_tr_frigates * 2 + n_tr_corsairs + n_al_corsairs;
game.n_tr_hits = 0;
game.n_tr_hits += fire("US frigate", 2 * n_us_frigates);
game.n_tr_hits += fire("US gunboat", 1 * n_us_gunboats);
if (game.n_tr_hits > tr_hitpoints)
game.n_tr_hits = tr_hitpoints;
game.n_us_hits = 0;
game.n_us_hits += fire("Tripolitan frigate", 2 * n_tr_frigates);
game.n_us_hits += fire("Tripolitan corsair", 1 * n_tr_corsairs);
game.n_us_hits += fire("Allied corsair", 1 * n_al_corsairs);
if (game.n_us_hits > us_hitpoints)
game.n_us_hits = us_hitpoints;
if (game.active == US)
goto_allocate_american_hits();
else
goto_allocate_tripolitan_hits();
}
function goto_allocate_american_hits() {
if (game.n_us_hits > 0) {
game.active = US;
game.state = 'allocate_us_hits';
} else if (game.n_tr_hits > 0) {
game.active = TR;
game.state = 'allocate_tr_hits';
} else {
end_naval_battle();
}
}
function goto_allocate_tripolitan_hits() {
if (game.n_tr_hits > 0) {
game.active = TR;
game.state = 'allocate_tr_hits';
} else if (game.n_us_hits > 0) {
game.active = US;
game.state = 'allocate_us_hits';
} else {
end_naval_battle();
}
}
states.allocate_us_hits = {
prompt: function (view, current) {
view.prompt = "United States: Allocate " + game.n_us_hits + " hits in " + SPACES[game.where] + ".";
if (is_inactive_player(current))
return view.prompt;
gen_action_undo(view);
if (game.n_us_hits > 0) {
for (let p of US_FRIGATES)
if (game.location[p] == game.where)
gen_action(view, 'piece', p);
for (let p of US_GUNBOATS)
if (game.location[p] == game.where)
gen_action(view, 'piece', p);
} else {
gen_action(view, 'next');
}
},
piece: function (p) {
push_undo();
--game.n_us_hits;
if (US_FRIGATES.includes(p)) {
if (game.damaged.includes(p)) {
log("US frigate sinks!");
game.location[p] = TRIPOLITAN_SUPPLY;
remove_from_array(game.damaged, p);
// TODO: check victory
} else {
log("US frigate is damaged.");
game.damaged.push(p);
}
}
if (US_GUNBOATS.includes(p)) {
log("US gunboat sinks.");
move_one_piece(US_GUNBOATS, game.where, UNITED_STATES_SUPPLY);
}
},
next: function () {
clear_undo();
if (game.n_tr_hits > 0) {
game.active = TR;
game.state = 'allocate_tr_hits';
} else {
end_naval_battle();
}
},
undo: pop_undo
}
states.allocate_tr_hits = {
prompt: function (view, current) {
view.prompt = "Tripolitania: Allocate " + game.n_tr_hits + " hits in " + SPACES[game.where] + ".";
if (is_inactive_player(current))
return view.prompt;
gen_action_undo(view);
if (game.n_tr_hits > 0) {
for (let p of TR_FRIGATES)
if (game.location[p] == game.where)
gen_action(view, 'piece', p);
for (let p of TR_CORSAIRS)
if (game.location[p] == game.where)
gen_action(view, 'piece', p);
for (let p of AL_CORSAIRS)
if (game.location[p] == game.where)
gen_action(view, 'piece', p);
} else {
gen_action(view, 'next');
}
},
piece: function (p) {
push_undo();
--game.n_tr_hits;
if (TR_FRIGATES.includes(p)) {
if (game.damaged.includes(p)) {
log("Tripolitan frigate sinks!");
game.location[p] = TRIPOLITAN_SUPPLY;
remove_from_array(game.damaged, p);
} else {
log("Tripolitan frigate is damaged.");
game.damaged.push(p);
}
}
if (TR_CORSAIRS.includes(p)) {
log("Tripolitan corsair sinks.");
move_one_piece(TR_CORSAIRS, game.where, TRIPOLITAN_SUPPLY);
}
if (AL_CORSAIRS.includes(p)) {
log("Allied corsair sinks.");
move_one_piece(TR_CORSAIRS, game.where, TRIPOLITAN_SUPPLY);
}
},
next: function () {
clear_undo();
if (game.n_us_hits > 0) {
game.active = US;
game.state = 'allocate_us_hits';
} else {
end_naval_battle();
}
},
undo: pop_undo
}
function move_damaged_frigate_to_year_track(p, supply) {
if (game.year == 1806)
game.location[p] = supply;
else
game.location[p] = TRACK_YEAR[game.year + 1];
remove_from_array(game.damaged, p);
}
function remove_damaged_frigates() {
for (let p of US_FRIGATES)
if (game.damaged.includes(p))
move_damaged_frigate_to_year_track(p, UNITED_STATES_SUPPLY);
for (let p of TR_FRIGATES)
if (game.damaged.includes(p))
move_damaged_frigate_to_year_track(p, TRIPOLITAN_SUPPLY);
}
function end_naval_battle() {
remove_damaged_frigates()
move_all_pieces(US_FRIGATES, game.where, MALTA_HARBOR);
move_all_pieces(US_GUNBOATS, game.where, MALTA_HARBOR);
if (game.where == TRIPOLI_PATROL_ZONE) {
move_all_pieces(TR_FRIGATES, game.where, TRIPOLI_HARBOR);
move_all_pieces(TR_CORSAIRS, game.where, TRIPOLI_HARBOR);
}
game.where = null;
goto_select_combat();
}
function goto_naval_bombardment() {
log("Naval bombardment in " + SPACES[game.where] + ".");
naval_bombardment_round();
end_naval_bombardment();
}
function naval_bombardment_round() {
let n_frigates = count_american_frigates(game.where);
let n_gunboats = count_american_gunboats(game.where);
let n_infantry = count_tripolitan_infantry(game.where);
let n_hits = 0;
n_hits += fire("US frigate", 2 * n_frigates);
n_hits += fire("US gunboat", 1 * n_gunboats);
if (n_hits > n_infantry)
n_hits = n_infantry;
log(n_hits + " Tripolitan infantry eliminated.");
for (let i = 0; i < n_hits; ++i)
move_one_piece(TR_INFANTRY, game.where, TRIPOLITAN_SUPPLY);
}
function end_naval_bombardment() {
move_all_pieces(US_FRIGATES, game.where, MALTA_HARBOR);
move_all_pieces(US_GUNBOATS, game.where, MALTA_HARBOR);
game.where = null;
goto_select_combat();
}
states.game_over = {
prompt: function (view, current) {
return view.prompt = game.victory;
},
}
exports.setup = function (scenario, players) {
if (players.length != 2)
throw new Error("Invalid player count: " + players.length);
game = {
state: null,
year: 1801,
season: 0,
log: [],
location: [],
damaged: [],
us: {
core: [],
hand: [],
deck: [],
discard: [],
},
tr: {
core: [],
hand: [],
deck: [],
discard: [],
gold: 0,
},
derne_captured: 0,
where: null,
undo: [],
};
game.tr.core.push(YUSUF_QARAMANLI);
game.tr.core.push(MURAD_REIS_BREAKS_OUT);
game.tr.core.push(CONSTANTINOPLE_SENDS_AID);
game.us.core.push(THOMAS_JEFFERSON);
game.us.core.push(SWEDISH_FRIGATES_ARRIVE);
game.us.core.push(HAMETS_ARMY_CREATED);
for (let i = 4; i <= 27; ++i) {
game.us.deck.push(i);
game.tr.deck.push(i+27);
}
deploy("us_frigate_1", GIBRALTAR_HARBOR);
deploy("us_frigate_2", GIBRALTAR_HARBOR);
deploy("us_frigate_3", GIBRALTAR_HARBOR);
deploy("us_frigate_4", TRACK_1802);
deploy("us_frigate_5", TRACK_1803);
deploy("us_frigate_6", TRACK_1804);
deploy("us_frigate_7", UNITED_STATES_SUPPLY);
deploy("us_frigate_8", UNITED_STATES_SUPPLY);
deploy("us_gunboat_1", UNITED_STATES_SUPPLY);
deploy("us_gunboat_2", UNITED_STATES_SUPPLY);
deploy("us_gunboat_3", UNITED_STATES_SUPPLY);
deploy("se_frigate_1", UNITED_STATES_SUPPLY);
deploy("se_frigate_2", UNITED_STATES_SUPPLY);
deploy("us_marine_1", UNITED_STATES_SUPPLY);
deploy("us_marine_2", UNITED_STATES_SUPPLY);
deploy("us_marine_3", UNITED_STATES_SUPPLY);
deploy("us_marine_4", UNITED_STATES_SUPPLY);
for (let i = 1; i <= 10; ++i)
deploy("ar_infantry_" + i, UNITED_STATES_SUPPLY);
deploy("tr_frigate_1", TRIPOLITAN_SUPPLY);
deploy("tr_frigate_2", TRIPOLITAN_SUPPLY);
deploy("tr_corsair_1", GIBRALTAR_HARBOR);
deploy("tr_corsair_2", GIBRALTAR_HARBOR);
deploy("tr_corsair_3", TRIPOLI_HARBOR);
deploy("tr_corsair_4", TRIPOLI_HARBOR);
deploy("tr_corsair_5", TRIPOLI_HARBOR);
deploy("tr_corsair_6", TRIPOLI_HARBOR);
deploy("tr_corsair_7", TRIPOLITAN_SUPPLY);
deploy("tr_corsair_8", TRIPOLITAN_SUPPLY);
deploy("tr_corsair_9", TRIPOLITAN_SUPPLY);
for (let i = 1; i <= 9; ++i)
deploy("al_corsair_" + i, TRIPOLITAN_SUPPLY);
deploy("tr_infantry_1", TRIPOLI_HARBOR);
deploy("tr_infantry_2", TRIPOLI_HARBOR);
deploy("tr_infantry_3", TRIPOLI_HARBOR);
deploy("tr_infantry_4", TRIPOLI_HARBOR);
deploy("tr_infantry_5", BENGHAZI_HARBOR);
deploy("tr_infantry_6", BENGHAZI_HARBOR);
deploy("tr_infantry_7", DERNE_HARBOR);
deploy("tr_infantry_8", DERNE_HARBOR);
for (let i = 9; i <= 20; ++i)
deploy("tr_infantry_" + i, TRIPOLITAN_SUPPLY);
start_of_year();
return game;
}
exports.action = function (state, current, action, arg) {
game = state;
// TODO: check against action list
if (true) {
let S = states[game.state];
if (action in S) {
S[action](arg, current);
} else {
throw new Error("Invalid action: " + action);
}
}
return state;
}
exports.resign = function (state, current) {
game = state;
if (game.state != 'game_over') {
log("");
log(current + " resigned.");
count_vp();
game.active = null;
game.state = 'game_over';
game.result = enemy(current);
game.victory = current + " resigned."
}
}
exports.view = function(state, current) {
game = state;
let view = {
log: game.log,
year: game.year,
season: game.season,
location: game.location,
damaged: game.damaged,
active: game.active,
prompt: null,
actions: null,
tr: {
core: game.tr.core,
deck: game.tr.deck.length,
discard: game.tr.discard.length,
hand: game.tr.hand.length,
gold: game.tr.gold,
},
us: {
core: game.us.core,
deck: game.us.deck.length,
discard: game.us.discard.length,
hand: game.us.hand.length,
},
card: game.active_card,
where: game.where,
};
states[game.state].prompt(view, current);
if (current == TR)
view.hand = game.tr.hand;
else if (current == US)
view.hand = game.us.hand;
else
view.hand = [];
return view;
}
|