-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbot.c
983 lines (813 loc) · 26.4 KB
/
tbot.c
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
#include <assert.h>
#include <stdio.h>
#include "cards.h"
#include "game.h"
/* 4 cards that can be added to the end of the event line */
#define TBOT_EVENT_MAX (8)
#define TBOT_EVENT_ADD_IDX (4)
#define array_size(arr) (sizeof(arr) / sizeof(arr[0]))
#define tbot_log_append(game, ...) \
game->log_ptr += \
snprintf(game->log_ptr, \
TBOT_LOG_LEN - (game->log_ptr - game->tbot_log), \
__VA_ARGS__);
static void activate_ally(struct game_state *game, enum locations location)
{
assert(location < TRIP_ALLIES);
game->t_allies[location] = 3;
}
static void pirate_raid(struct game_state *game, enum locations location);
static void apply_damage(struct game_state *game, enum locations location,
enum battle_type btype, int hits)
{
int idx;
int apply_hits;
/* Bombardment is handled directly since there's no real logic to it */
assert(btype != NAVAL_BOMBARDMENT);
if (hits == 0) {
return;
}
if (has_trip_allies(location)) {
assert(btype == NAVAL_BATTLE);
hits = min(hits, game->t_allies[location]);
game->t_allies[location] -= hits;
return;
}
if (btype == GROUND_BATTLE) {
idx = trip_infantry_idx(location);
hits = min(hits, game->t_infantry[idx]);
game->t_infantry[idx] -= hits;
return;
}
assert(location == TRIPOLI && btype == NAVAL_BATTLE);
if ((game->year >= 1801 && game->year <= 1804) ||
(game->year == 1805 && game->season == WINTER) ||
game->victory_or_death) {
/* First damage frigates before destroying corsairs */
apply_hits = min(hits, game->t_frigates);
hits -= apply_hits;
game->t_frigates -= apply_hits;
game->t_damaged_frigates += apply_hits;
assert(hits >= 0 && game->t_frigates >= 0);
/* Next destroy corsairs */
apply_hits = min(hits, game->t_corsairs_tripoli);
game->t_corsairs_tripoli -= apply_hits;
hits -= apply_hits;
assert(hits >= 0 && game->t_corsairs_tripoli >= 0);
/* Final resort, destroy damaged frigates */
apply_hits = min(hits, game->t_damaged_frigates);
game->t_damaged_frigates -= apply_hits;
assert(game->t_damaged_frigates >= 0);
/* If we're not in the final battle the frigates will stick around until
* they're destroyed
*/
if (!game->victory_or_death) {
if (game->year < END_YEAR) {
game->t_turn_frigates[year_to_frigate_idx(game->year + 1)] +=
game->t_damaged_frigates;
}
game->t_damaged_frigates = 0;
}
} else if (game->year == 1805 || game->year == 1806) {
/* Destroy corsairs first */
apply_hits = min(hits, game->t_corsairs_tripoli);
hits -= apply_hits;
game->t_corsairs_tripoli -= apply_hits;
assert(hits >= 0 && game->t_corsairs_tripoli >= 0);
/* Damage frigates */
apply_hits = min(hits, game->t_frigates);
hits -= apply_hits;
game->t_frigates -= apply_hits;
game->t_damaged_frigates += apply_hits;
assert(hits >= 0 && game->t_frigates >= 0);
/* Destroy frigates when theres no other option */
apply_hits -= min(hits, game->t_damaged_frigates);
game->t_damaged_frigates -= apply_hits;
assert(game->t_damaged_frigates >= 0);
/* Any frigates not destroyed ship em off to get repaired */
if (game->year < END_YEAR) {
game->t_turn_frigates[year_to_frigate_idx(game->year + 1)] +=
game->t_damaged_frigates;
}
} else {
/* Unreachable */
assert(false);
}
}
/* Battle cards, these are used directly when applicable */
struct card books_overboard = {
.name = "US Signal Books Overboard",
.text = "Playable after any Interception Roll that "
"includes an American frigate. Randomly "
"draw one card from the American "
"player’s hand and place the card in the "
"discard pile."
};
struct card uncharted_waters = {
.name = "Uncharted Waters",
.text = "Playable if The Philadelphia Runs "
"Aground is the active event card this turn. "
"Roll two dice instead of one and choose "
"the preferred result."
};
struct card merchant_ship_converted = {
.name = "Merchant Ship Converted",
.text = "Playable if a Tripolitan Pirate Raid has "
"just been successful. Place one Tripolitan "
"corsair in the harbor of Tripoli."
};
struct card happy_hunting = {
.name = "Happy Hunting",
.text = "Playable when making a Pirate Raid "
"with Tripolitan corsairs. Roll three "
"additional dice."
};
struct card the_guns_of_tripoli = {
.name = "The Guns of Tripoli",
.text = "Playable during a naval battle in the "
"harbor of Tripoli. The Tripoli fleet may "
"roll an additional twelve dice. If played "
"during the Assault on Tripoli, only roll "
"the extra dice in the first round of the "
"naval battle."
};
struct card mercenaries_desert = {
.name = "Mercenaries Desert",
.text = "Playable at the start of a land battle. "
"Before the battle starts, roll one die for "
"each Arab infantry unit. For each 6, "
"take an Arab infantry unit and return it "
"to the Supply"
};
static struct card *tbot_battle_cards[] = {
&books_overboard,
&uncharted_waters,
&merchant_ship_converted,
&happy_hunting,
&the_guns_of_tripoli,
&mercenaries_desert
};
static bool tbot_check_play_battle_card(struct game_state *game,
struct card *card)
{
int i;
for (i = 0; i < array_size(tbot_battle_cards); i++) {
if (tbot_battle_cards[i] == card) {
tbot_battle_cards[i] = NULL;
tbot_log_append(game, "T-Bot plays [%s] as a battle card\n",
card->name);
return true;
}
}
return false;
}
static bool yusuf_playable(struct game_state *game)
{
int ally_count = 0;
int i;
for (i = 0; i < TRIP_ALLIES; i++) {
if (game->t_allies[i] > 0) {
ally_count++;
}
}
return (game->year >= 1801 && game->year <= 1804 && ally_count >= 2) ||
(game->year >= 1805 && ally_count >= 1);
}
static const char *play_yusuf(struct game_state *game)
{
int i;
pirate_raid(game, TRIPOLI);
for (i = 0; i < TRIP_ALLIES; i++) {
if (game->t_allies[i] > 0) {
pirate_raid(game, i);
}
}
return NULL;
}
/* Tbot core cards */
struct card yusuf_qaramanli = {
.name = "Yusuf Qaramanli",
.text = "Pirate Raid with the corsairs from the "
"harbor of Tripoli and the corsairs from "
"the harbor of each active ally (Algiers, "
"Tangier, Tunis).",
.playable = yusuf_playable,
.play = play_yusuf
};
static bool break_out_playable(struct game_state *game)
{
return game->t_corsairs_gibraltar > 0 &&
(game->patrol_frigates[GIBRALTAR] == 0 ||
(game->year == 1801 && game->season == WINTER));
}
static const char *play_break_out(struct game_state *game)
{
game_handle_intercept(game, GIBRALTAR);
game->t_corsairs_tripoli += game->t_corsairs_gibraltar;
game->t_corsairs_gibraltar = 0;
return NULL;
}
struct card murad_reis_breaks_out = {
.name = "Murad Reis Breaks Out",
.text = "Move the two Tripolitan corsairs from "
"the harbor of Gibraltar to the harbor of "
"Tripoli. Any American frigates in the "
"naval patrol zone of Gibraltar may first "
"make an Interception Roll.",
.playable = break_out_playable,
.play = play_break_out
};
static bool send_aid_playable(struct game_state *game)
{
return hamets_army_at(game, DERNE);
}
static const char *play_send_aid(struct game_state *game)
{
game->t_frigates++;
game->t_corsairs_tripoli += 2;
game->t_infantry[trip_infantry_idx(TRIPOLI)] += 2;
return NULL;
}
struct card constantinople_sends_aid = {
.name = "Constantinople Sends Aid",
.text = "Playable if Hamet’s Army has captured "
"Derne. Place one Tripolitan frigate and "
"two Tripolitan corsairs in the harbor of "
"Tripoli. Place two Tripolitan infantry "
"units in the city of Tripoli.",
.playable = send_aid_playable,
.play = play_send_aid
};
/* Tbot deck */
static bool supplies_run_low_playable(struct game_state *game)
{
return game->patrol_frigates[TRIPOLI] == 2;
}
static const char *play_supplies_run_low(struct game_state *game)
{
/* XXX: Should the bot choose a zone with the most frigates or just tripoli,
* likely that most frigates will be in tripoli patrol zone but... you never
* know
*/
game->patrol_frigates[TRIPOLI]--;
game->us_frigates[MALTA]++;
return NULL;
}
struct card us_supplies_run_low = {
.name = "US Supplies Run Low",
.text = "Move one American frigate from any "
"naval patrol zone to the harbor of Malta.",
.playable = supplies_run_low_playable,
.play = play_supplies_run_low
};
/* With the corsairs raid cards, the bot will never play them directly and will
* always discard to raid or build instead
*/
static bool corsairs_raid_playable(struct game_state *game)
{
return false;
}
static const char *play_corsairs_raid(struct game_state *game)
{
assert(false);
return NULL;
}
/* 2 copies */
struct card algerine_corsairs_raid = {
.name = "Algerine Corsairs Raid",
.text = "Pirate Raid with all corsairs from the "
"harbor of Algiers.",
.playable = corsairs_raid_playable,
.play = play_corsairs_raid
};
/* 2 copies */
struct card moroccan_corsairs_raid = {
.name = "Moroccan Corsairs Raid",
.text = "Pirate Raid with all corsairs from the "
"harbor of Tangier.",
.playable = corsairs_raid_playable,
.play = play_corsairs_raid
};
/* 2 copies */
struct card tunisian_corsairs_raid = {
.name = "Tunisian Corsairs Raid",
.text = "Pirate Raid with all corsairs from the "
"harbor of Tunis.",
.playable = corsairs_raid_playable,
.play = play_corsairs_raid
};
static bool troops_to_derne_playable(struct game_state *game)
{
return game->t_infantry[trip_infantry_idx(DERNE)] > 0;
}
static const char *play_troops_to_derne(struct game_state *game)
{
game->t_infantry[trip_infantry_idx(DERNE)] += 2;
return NULL;
}
struct card troops_to_derne = {
.name = "Troops to Derne",
.text = "Playable if Hamet’s Army has not "
"captured Derne. Place two Tripolitan "
"infantry in the city of Derne.",
.playable = troops_to_derne_playable,
.play = play_troops_to_derne
};
static bool troops_to_benghazi_playable(struct game_state *game)
{
return game->t_infantry[trip_infantry_idx(BENGHAZI)] > 0;
}
static const char *play_troops_to_benghazi(struct game_state *game)
{
game->t_infantry[trip_infantry_idx(BENGHAZI)] += 2;
return NULL;
}
struct card troops_to_benghazi = {
.name = "Troops to Benghazi",
.text = "Playable if Hamet’s Army has not "
"captured Benghazi. Place two Tripolitan "
"infantry in the city of Benghazi.",
.playable = troops_to_benghazi_playable,
.play = play_troops_to_benghazi
};
static const char *play_troops_to_tripoli(struct game_state *game)
{
game->t_infantry[trip_infantry_idx(TRIPOLI)] += 2;
return NULL;
}
struct card troops_to_tripoli = {
.name = "Troops to Tripoli",
.text = "Place two Tripolitan infantry in the city "
"of Tripoli.",
.playable = always_playable,
.play = play_troops_to_tripoli
};
static bool storms_playable(struct game_state *game)
{
int i;
for (i = 0; i < PATROL_ZONES; i++) {
if (game->patrol_frigates[i] >= 2) {
return true;
}
}
return false;
}
static int storm_score(struct game_state *game, enum locations location)
{
assert(has_patrol_zone(location));
if (game->patrol_frigates[location] < 2) {
return -1;
}
return game->patrol_frigates[location];
}
static const char *play_storms(struct game_state *game)
{
int i;
int scores[PATROL_ZONES];
int max_score = -1;
int score_idx = -1;
int score_count = 0;
int successes = 0;
int rolls;
for (i = 0; i < PATROL_ZONES; i++) {
scores[i] = storm_score(game, i);
if (scores[i] > max_score) {
max_score = scores[i];
score_count = 1;
} else if (scores[i] == max_score) {
score_count++;
}
}
assert(max_score != -1);
score_count = rand() % score_count + 1;
for (i = 0; i < PATROL_ZONES; i++) {
if (scores[i] == max_score) {
if (--score_count == 0) {
score_idx = i;
break;
}
}
}
assert(score_idx != -1);
rolls = game->patrol_frigates[score_idx];
successes = rolld6s(rolls, 6);
if (successes > 0) {
game->patrol_frigates[score_idx]--;
game->destroyed_us_frigates++;
successes--;
}
game->patrol_frigates[score_idx] -= successes;
if (game->year < END_YEAR) {
game->turn_track_frigates[year_to_frigate_idx(game->year + 1)] +=
successes;
}
return NULL;
}
struct card storms = {
.name = "Storms",
.text = "Select a naval patrol zone that contains "
"at least one American frigate. Roll one "
"die for each American frigate. The first "
"6 rolled sinks a frigate. Each additional "
"6 rolled damages a frigate and is placed "
"on the following year of the Year Turn "
"Track.",
.playable = storms_playable,
.play = play_storms
};
static bool tripoli_attacks_playable(struct game_state *game)
{
int trip_dice = game->t_corsairs_tripoli + game->t_frigates * FRIGATE_DICE;
return trip_dice >= 5 && game->patrol_frigates[TRIPOLI] == 1;
}
/* This one is a weird fight since it's at the tripoli patrol zone so we just
* handle it separately
*/
static const char *play_tripoli_attacks(struct game_state *game)
{
bool prebles_boys_played = check_play_battle_card(game, &prebles_boys);
int us_dice = (prebles_boys_played) ? 3 : FRIGATE_DICE; /* only 1 ship in
* the zone */
int trip_dice = game->t_corsairs_tripoli + game->t_frigates * FRIGATE_DICE;
int us_success = rolld6s(us_dice, 6);;
int trip_success = rolld6s(trip_dice, 6);;
if (trip_success >= 1) {
game->patrol_frigates[TRIPOLI]--;
if (trip_success == 1) {
if (game->year < END_YEAR) {
game->turn_track_frigates[year_to_frigate_idx(game->year + 1)]++;
}
} else if (trip_success >= 2) {
game->destroyed_us_frigates++;
}
}
apply_damage(game, TRIPOLI, NAVAL_BATTLE, us_success);
return NULL;
}
struct card tripoli_attacks = {
.name = "Tripoli Attacks",
.text = "Move all Tripolitan frigates and corsairs "
"from the harbor of Tripoli to the naval "
"patrol zone of Tripoli. Resolve the "
"battle against the American frigates in "
"the patrol zone. Any Swedish frigates "
"in the patrol zone do not participate in "
"the battle.",
.playable = tripoli_attacks_playable,
.play = play_tripoli_attacks
};
static bool sweden_pays_tribute_playable(struct game_state *game)
{
return game->year >= 1803 && game->swedish_frigates_active;
}
static const char *play_sweden_pays_tribute(struct game_state *game)
{
game->swedish_frigates_active = false;
game->pirated_gold += 2;
return NULL;
}
struct card sweden_pays_tribute = {
.name = "Sweden Pays Tribute",
.text = "Playable if it is 1803 or later and there "
"are Swedish frigates in the naval patrol "
"zone of Tripoli. Return the Swedish "
"frigates to the Supply and receive two "
"Gold Coins.",
.playable = sweden_pays_tribute_playable,
.play = play_sweden_pays_tribute
};
static bool acquire_corsairs_playable(struct game_state *game)
{
int corsairs = game->t_corsairs_gibraltar + game->t_corsairs_tripoli;
return corsairs <= MAX_TRIPOLI_CORSAIRS - 2;
}
static const char *play_tripoli_acquires_corsairs(struct game_state *game)
{
game->t_corsairs_tripoli += 2;
return NULL;
}
struct card tripoli_acquires_corsairs = {
.name = "Tripoli Acquires Corsairs",
.text = "Place two Tripolitan corsairs in the "
"harbor of Tripoli.",
.playable = acquire_corsairs_playable,
.play = play_tripoli_acquires_corsairs
};
static bool philly_runs_aground_playable(struct game_state *game)
{
return game->patrol_frigates[TRIPOLI] > 0;
}
static const char *play_philly_runs_aground(struct game_state *game)
{
int roll = rolld6();
bool uncharted_waters_played =
tbot_check_play_battle_card(game, &uncharted_waters);
if (uncharted_waters_played) {
roll = max(roll, rolld6());
}
switch (roll) {
case 5:
case 6:
game->t_frigates++;
case 3:
case 4:
game->patrol_frigates[TRIPOLI]--;
game->destroyed_us_frigates++;
}
return NULL;
}
struct card philly_runs_aground = {
.name = "The Philadelphia Runs Aground",
.text = "Playable if there is at least one American "
"frigate in the naval patrol zone of Tripoli. "
"Roll one die and apply the result: "
"1-2: Minor damage. Move an American "
"frigate to the harbor of Malta. "
"3-4: Frigate sunk. "
"5-6: Frigate captured. Take the American "
"frigate as “sunk” and place one Tripolitan "
"frigate in the harbor of Tripoli.",
.playable = philly_runs_aground_playable,
.play = play_philly_runs_aground
};
static const char *play_algiers_declares_war(struct game_state *game)
{
activate_ally(game, ALGIERS);
return NULL;
}
struct card algiers_declares_war = {
.name = "Algiers Declares War",
.text = "Place three Algerine corsairs in the "
"harbor of Algiers.",
.playable = always_playable,
.play = play_algiers_declares_war
};
static const char *play_morocco_declares_war(struct game_state *game)
{
activate_ally(game, TANGIER);
return NULL;
}
struct card morocco_declares_war = {
.name = "Morocco Declares War",
.text = "Place three Moroccan corsairs in the "
"harbor of Tangier.",
.playable = always_playable,
.play = play_morocco_declares_war
};
static const char *play_tunis_declares_war(struct game_state *game)
{
activate_ally(game, TUNIS);
return NULL;
}
struct card tunis_declares_war = {
.name = "Tunis Declares War",
.text = "Place three Tunisian corsairs in the "
"harbor of Tunis.",
.playable = always_playable,
.play = play_tunis_declares_war
};
struct card second_storms = {
.name = "Second Storms",
.text = "Select a naval patrol zone that contains "
"at least one American frigate. Roll one "
"die for each American frigate. The first "
"6 rolled sinks a frigate. Each additional "
"6 rolled damages a frigate and is placed "
"on the following year of the Year Turn "
"Track.",
.playable = storms_playable, /* It's the same card as Storms */
.play = play_storms
};
/* We leave the last 2 slots open for Storms and Second Storms */
static struct card *tbot_event_line[TBOT_EVENT_MAX] = {
&yusuf_qaramanli,
&murad_reis_breaks_out,
&constantinople_sends_aid,
&sweden_pays_tribute
};
static struct card *tbot_deck[] = {
&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, &tripoli_acquires_corsairs,
&philly_runs_aground,
&algiers_declares_war, &morocco_declares_war, &tunis_declares_war,
&second_storms
};
static unsigned int tbot_deck_size = array_size(tbot_deck);
int tbot_resolve_naval_battle(struct game_state *game, enum locations location,
int damage)
{
int hits = 0;
int dice;
assert(has_trip_allies(location) || location == TRIPOLI);
if (has_trip_allies(location)) {
dice = game->t_allies[location];
} else {
dice = game->t_frigates * FRIGATE_DICE +
game->t_damaged_frigates * FRIGATE_DICE +
game->t_corsairs_tripoli;
if ((game->year == 1805 && game->season != WINTER) ||
game->year == 1806 || game->victory_or_death) {
if (tbot_check_play_battle_card(game, &the_guns_of_tripoli)) {
dice += 12;
}
}
}
hits = rolld6s(dice, 6);
apply_damage(game, location, NAVAL_BATTLE, damage);
return hits;
}
int tbot_resolve_ground_combat(struct game_state *game, enum locations location,
int damage)
{
int hits = 0;
int dice = game->t_infantry[trip_infantry_idx(location)];
hits = rolld6s(dice, 6);
apply_damage(game, location, GROUND_BATTLE, damage);
return hits;
}
static inline bool five_corsair_check(struct game_state *game)
{
return game->t_corsairs_tripoli >= 5;
}
static bool check_add_card_to_event_line(struct game_state *game,
struct card *card)
{
int i;
if (!card->playable(game) &&
(card == &storms || card == &second_storms ||
card == &philly_runs_aground || card == &tripoli_acquires_corsairs)) {
tbot_log_append(game, "T-Bot adds [%s] to the event line\n", card->name);
for (i = TBOT_EVENT_ADD_IDX; i < TBOT_EVENT_MAX; i++) {
if (tbot_event_line[i] == NULL) {
tbot_event_line[i] = card;
return true;
}
}
assert(false); /* Should have found a free slot */
}
return false;
}
static bool tbot_draw_play_card(struct game_state *game)
{
int card_idx;
struct card *card;
draw_new_card:
if (tbot_deck_size == 0) {
return false;
}
card_idx = rand() % tbot_deck_size;
card = tbot_deck[card_idx];
/* tbot cards go away forever even if unplayed */
tbot_deck[card_idx] = tbot_deck[tbot_deck_size - 1];
tbot_deck[--tbot_deck_size] = NULL;
assert(card && card->playable && card->play);
if (check_add_card_to_event_line(game, card)) {
goto draw_new_card;
}
if (card->playable(game)) {
tbot_log_append(game, "T-Bot drew and played [%s]\n", card->name);
card->play(game);
return true;
}
tbot_log_append(game, "T-Bot discarded [%s] to Raid or Build\n", card->name);
return false;
}
static bool tbot_process_event_line(struct game_state *game)
{
int i;
struct card *card;
for (i = 0; i < array_size(tbot_event_line); i++) {
card = tbot_event_line[i];
assert(!card || (card->play && card->playable));
if (card && card->playable(game)) {
tbot_log_append(game, "T-Bot plays [%s] from the event line\n",
card->name);
card->play(game);
tbot_event_line[i] = NULL;
return true;
}
}
return false;
}
static void pirate_raid(struct game_state *game, enum locations location)
{
int successes = 0;
int raid_count;
bool intercepted = game_handle_intercept(game, location);
int card_idx;
if (intercepted && (game->year >= 1805 &&
tbot_check_play_battle_card(game, &books_overboard))) {
card_idx = rand() % game->hand_size;
tbot_log_append(game, "T-Bot discards [%s] from US Hand\n",
game->us_hand[card_idx]->name);
discard_from_hand(game, card_idx);
}
raid_count = (location == TRIPOLI) ? game->t_corsairs_tripoli :
game->t_allies[location];
if (tbot_check_play_battle_card(game, &happy_hunting)) {
raid_count += 3;
}
successes = rolld6s(raid_count, 5);
game->pirated_gold += successes;
tbot_log_append(game, "T-Bot raids from %s and pirates %d gold\n",
location_str(location), successes);
if (successes > 0 &&
tbot_check_play_battle_card(game, &merchant_ship_converted)) {
game->t_corsairs_tripoli++;
}
}
static int tbot_raid_score(struct game_state *game, enum locations location)
{
int corsairs;
int frigs;
if (location < TRIP_ALLIES) {
corsairs = game->t_allies[location];
} else {
corsairs = game->t_corsairs_tripoli;
}
frigs = game->patrol_frigates[location];
if (location == TRIPOLI && game->swedish_frigates_active) {
frigs += 2;
}
if (corsairs >= 3 && corsairs > frigs) {
return corsairs - frigs;
}
return -1;
}
static void raid_or_build(struct game_state *game)
{
int i;
int scores[TRIP_ALLIES + 1];
int max_score = -1;
int score_count = 0;
int score_idx = -1;
for (i = 0; i < TRIP_ALLIES; i++) {
scores[i] = tbot_raid_score(game, i);
}
scores[TRIP_ALLIES] = tbot_raid_score(game, TRIPOLI);
for (i = 0; i <= TRIP_ALLIES; i++) {
if (scores[i] > max_score) {
max_score = scores[i];
score_count = 1;
} else if (scores[i] == max_score) {
score_count++;
}
}
if (max_score == -1) {
tbot_log_append(game, "T-Bot builds a corsair in Tripoli\n");
game->t_corsairs_tripoli++;
return;
}
assert(score_count > 0);
score_count = rand() % score_count + 1;
for (i = 0; i <= TRIP_ALLIES; i++) {
if (scores[i] == max_score) {
if (--score_count == 0) {
score_idx = i;
break;
}
}
}
assert(score_idx != -1);
if (score_idx == TRIP_ALLIES) {
pirate_raid(game, TRIPOLI);
} else {
pirate_raid(game, score_idx);
}
}
static inline void tbot_reset_log(struct game_state *game)
{
game->log_ptr = game->tbot_log;
game->log_ptr[0] = 0;
}
void tbot_do_turn(struct game_state *game)
{
tbot_reset_log(game);
if (tbot_process_event_line(game)) {
return;
}
if (five_corsair_check(game)) {
pirate_raid(game, TRIPOLI);
return;
}
if (tbot_draw_play_card(game)) {
return;
}
raid_or_build(game);
}
void tbot_plays_mercenaries_desert(struct game_state *game)
{
int dice;
int idx = us_infantry_idx(DERNE);
int remove = 0;
if (!tbot_check_play_battle_card(game, &mercenaries_desert)) {
return;
}
dice = game->arab_infantry[idx];
remove = rolld6s(dice, 6);
game->arab_infantry[idx] -= remove;
}