/
ai.c
2517 lines (2008 loc) · 48.9 KB
/
ai.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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Bluemoon AI
*
* Copyright (C) 2007-2008 Keldon Jones
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "bluemoon.h"
#include "net.h"
extern int verbose;
/* #define DEBUG */
/*
* Action types.
*/
#define ACT_NONE 0
#define ACT_RETREAT 1
#define ACT_RETRIEVE 2
#define ACT_PLAY 3
#define ACT_PLAY_NO 4
#define ACT_ANN_FIRE 5
#define ACT_ANN_EARTH 6
#define ACT_USE 7
#define ACT_SATISFY 8
#define ACT_CHOOSE 9
#define ACT_LAND 10
#define ACT_LOAD 11
#define ACT_BLUFF 12
#define ACT_REVEAL 13
/*
* An action to take.
*/
typedef struct action
{
/* Action code */
int act;
/* Action argument (card to play, usually) */
design *arg;
/* Target argument (ship to be loaded on) */
design *target;
/* Cards chosen (when act is ACT_CHOOSE) */
int chosen;
/* Index of card used (to reduce AI tree searching) */
int index;
} action;
/*
* Maximum action path length.
*/
#define MAX_ACTION 50
/*
* Current best path.
*/
static action best_path[MAX_ACTION];
#ifdef DEBUG
/* Actions in current evaluated path */
static action cur_path[MAX_ACTION];
#endif
/*
* Current best path position.
*/
static int best_path_pos;
/*
* Current best path score.
*/
static double best_path_score;
/*
* Information about choice to make.
*/
typedef struct node
{
/* Callback */
choose_result callback;
/* Legal combinations */
int legal[5000];
/* Number of combinations */
int num_legal;
/* Card choices */
design *choices[DECK_SIZE];
/* Whose cards are chosen */
int who;
/* Data to pass to callback */
void *data;
} node;
/*
* Choices to make.
*/
static node nodes[10];
/*
* Current choice.
*/
static int node_pos;
/*
* Number of upcoming choices.
*/
static int node_len;
/*
* Prevent recursive chooses.
*/
static int inside_choose;
/*
* String used for AI assist purposes.
*/
static char *assist_str;
/*
* Flag used when checking for no option but retreat.
*/
static int must_retreat;
static int checking_retreat;
/*
* Flag used when checking for opponent's response to declined fight.
*/
static int checking_decline;
/*
* A neural net for each player.
*/
net learner[2];
/* Neural net inputs */
#define NET_INPUT 443
/* Number of hidden nodes */
#define HIDDEN_NODES 50
/*
* Set an input value of the neural net.
*/
#define SET_INPUT(l, n, x) (l)->input_value[(n)] = (x)
/*
* Copy a game structure and set the "simulation" flag.
*
* If a game is a simulation, certain actions like drawing cards are
* faked in such a way as to not give the AI unfair information.
*/
static void simulate_game(game *sim, game *orig)
{
/* Copy game */
memcpy(sim, orig, sizeof(game));
/* Check for original game */
if (!sim->simulation)
{
/* Set simulation flag */
sim->simulation = 1;
/* Remember who initiated simulation */
sim->sim_turn = sim->turn;
/* Set both players control interfaces to AI */
sim->p[0].control = &ai_func;
sim->p[1].control = &ai_func;
/* Reset random seed */
sim->random_seed = 0;
}
}
/*
* Evaluate the current game state.
*/
static double eval_game(game *g, int who)
{
player *p;
card *c;
int n = 0, i, j;
int power, stack, bluff, bad_bluff;
net *l;
/* Get player's network */
l = &learner[who];
/* Check for no learner loaded */
if (!l->num_inputs) return 0.5;
/* Loop over each player */
for (i = 0; i < 2; i++)
{
/* Get player pointer */
p = &g->p[i];
/* Loop over cards in deck */
for (j = 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
c = &p->deck[j];
/* Set input for active cards (except leadership) */
SET_INPUT(l, n++, c->active && !c->random_fake &&
c->d_ptr->type != TYPE_LEADERSHIP &&
(who == i || c->loc_known));
}
/* Loop over cards in deck */
for (j = 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
c = &p->deck[j];
/* Set input for cards in hand (if known) */
SET_INPUT(l, n++, (who == i || c->loc_known) &&
c->where == LOC_HAND &&
!c->random_fake);
}
/* Loop over cards in deck */
for (j = 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
c = &p->deck[j];
/* Set input for used cards */
SET_INPUT(l, n++, (who == i || c->loc_known) &&
!c->random_fake &&
(c->where == LOC_DISCARD ||
c->where == LOC_LEADERSHIP ||
(c->where == LOC_COMBAT &&
!c->active)));
}
/* Loop over cards in deck */
for (j = 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
c = &p->deck[j];
/* Set input for cards loaded on ship */
SET_INPUT(l, n++, (c->ship != NULL));
}
}
/* Get evaluating player */
p = &g->p[who];
/* Loop over cards in deck */
for (i = 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Set input for "special" card */
SET_INPUT(l, n++, c->text_boosted || c->on_bottom || c->bluff);
}
/* Assume no bad bluff */
bad_bluff = 0;
/* Loop over cards in deck */
for (i = 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip non-bluff cards */
if (!c->bluff) continue;
/* Check for bad bluff */
if ((!g->fight_element && !(c->icons & ICON_BLUFF_F)) ||
(g->fight_element && !(c->icons & ICON_BLUFF_E)))
{
/* Bluff is bad */
bad_bluff = 1;
}
}
/* Set input for a bad bluff */
SET_INPUT(l, n++, bad_bluff);
/* Set input for game over */
SET_INPUT(l, n++, g->game_over);
/* Set input for fight started */
SET_INPUT(l, n++, g->fight_started);
/* Set inputs for fight element (only if started) */
SET_INPUT(l, n++, g->fight_element && g->fight_started);
SET_INPUT(l, n++, !g->fight_element && g->fight_started);
/* Loop over players */
for (i = 0; i < 2; i++)
{
/* Get player pointer */
p = &g->p[i];
/* Set input if it is this player's turn */
SET_INPUT(l, n++, g->turn == i && !g->game_over);
/* Check for fight started */
if (g->fight_started)
{
/* Compute power level */
power = compute_power(g, i);
}
else
{
/* Assume no power */
power = 0;
}
/* Loop over possible power values */
for (j = 0; j < 15; j++)
{
/* Set input if player has this much power */
SET_INPUT(l, n++, power > j);
}
/* Count active cards */
stack = p->stack[LOC_COMBAT] + p->stack[LOC_SUPPORT];
/* Assume no bluff cards */
bluff = 0;
/* Loop over cards */
for (j = 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
c = &p->deck[j];
/* Check for bluff card */
if (c->bluff)
{
/* Bluff cards do not count for dragons */
stack--;
/* Count bluffs */
bluff++;
}
}
/* Loop over stack sizes */
for (j = 0; j < 8; j++)
{
/* Set input if player has this many cards played */
SET_INPUT(l, n++, stack > j);
}
/* Loop over bluff counts */
for (j = 0; j < 4; j++)
{
/* Set input if player has this many bluffs */
SET_INPUT(l, n++, bluff > j);
}
/* Count cards in hand */
stack = p->stack[LOC_HAND];
/* Loop over hand sizes */
for (j = 0; j < 10; j++)
{
/* Set input if player has this many cards */
SET_INPUT(l, n++, stack > j);
}
/* Count cards in draw deck and hand */
stack = p->stack[LOC_DRAW] + p->stack[LOC_HAND];
/* Loop over deck sizes */
for (j = 0; j < 30; j++)
{
/* Set input if player has this many cards */
SET_INPUT(l, n++, stack > j);
}
/* Assume no characters */
stack = 0;
/* Loop over deck */
for (j = 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
c = &p->deck[j];
/* Skip non-characters */
if (c->d_ptr->type != TYPE_CHARACTER) continue;
/* Skip cards not in hand */
if (c->where == LOC_HAND &&
(who == i || c->loc_known) &&
!c->random_fake)
{
/* Add more character */
stack++;
}
}
/* Loop over character counts */
for (j = 0; j < 5; j++)
{
/* Set input if player has this many characters */
SET_INPUT(l, n++, stack > j);
}
/* Assume no undisclosed cards */
stack = 0;
/* Loop over deck */
for (j = 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
c = &p->deck[j];
/* Skip cards not in hand */
if (c->where != LOC_HAND) continue;
/* Skip disclosed cards */
if (c->disclosed) continue;
/* Count undisclosed cards */
stack++;
}
/* Loop over disclose counts */
for (j = 0; j < 6; j++)
{
/* Set input if player has this cards disclosed */
SET_INPUT(l, n++, stack > j);
}
/* Set input if player is first to run out of cards */
SET_INPUT(l, n++, p->no_cards);
/* Loop over dragon counts */
for (j = 0; j < 3; j++)
{
/* Set input if player has this many dragons */
SET_INPUT(l, n++, p->dragons > j);
}
/* Set input if player has instant victory */
SET_INPUT(l, n++, p->instant_win);
}
/* Compute network value */
compute_net(l);
#ifdef DEBUG
/* Print score and path to get here */
if (verbose && !checking_retreat && best_path_pos > 0)
{
printf("%.12lf: ", l->win_prob[who]);
for (i = 0; i <= best_path_pos; i++)
{
action a;
a = cur_path[i];
switch (a.act)
{
case ACT_NONE: break;
case ACT_RETREAT: printf("Retreat "); break;
case ACT_RETRIEVE: printf("Retrieve %s ", a.arg->name); break;
case ACT_PLAY: printf("Play %s ", a.arg->name); break;
case ACT_PLAY_NO: printf("Play (no) %s ", a.arg->name); break;
case ACT_ANN_FIRE: printf("Announce fire "); break;
case ACT_ANN_EARTH: printf("Announce earth "); break;
case ACT_USE: printf("Use %s ", a.arg->name); break;
case ACT_SATISFY: printf("Satsify %s ", a.arg->name); break;
case ACT_CHOOSE: printf("Choose %d ", a.chosen); break;
case ACT_LAND: printf("Land %s ", a.arg->name); break;
case ACT_LOAD: printf("Load %s on %s ", a.arg->name, a.target->name); break;
case ACT_BLUFF: printf("Bluff %s ", a.arg->name); break;
case ACT_REVEAL: printf("Reveal %s ", a.arg->name); break;
}
}
if (must_retreat) printf("Force retreat");
if (checking_decline)
{
printf("Responding %s", g->fight_element ? "earth" : "fire");
}
printf("\n");
}
#endif
/* Return output */
return l->win_prob[who];
}
/*
* Perform a training iteration.
*
* The "desired" array is passed only at the end of the game. Otherwise
* we train the network to give results (using past inputs) to be like our
* results using current inputs.
*/
static void perform_training(game *g, int who, double *desired)
{
double target[2];
double lambda = 1.0;
int i;
net *l;
/* Get correct network to train */
l = &learner[who];
/* Check for uninitialized network */
if (!l->num_inputs) return;
/* Get current state */
eval_game(g, who);
/* Store current inputs */
store_net(l);
/* Check for passed in results */
if (desired)
{
/* Copy results */
target[0] = desired[0];
target[1] = desired[1];
/* Train current inputs with desired outputs */
train_net(l, lambda, target);
}
else
{
/* Copy results */
target[0] = l->win_prob[0];
target[1] = l->win_prob[1];
}
/* Loop over past inputs (starting with most recent) */
for (i = l->num_past - 2; i >= 0; i--)
{
/* Copy past inputs to network */
memcpy(l->input_value, l->past_input[i],
sizeof(int) * (l->num_inputs + 1));
/* Compute net */
compute_net(l);
/* Train using this */
train_net(l, lambda, target);
/* Reduce training amount for less recent results */
lambda *= 0.9;
}
}
/*
* Initialize AI.
*/
static void ai_initialize(game *g, int who)
{
char fname[1024], buf[1024];
/* Create neural net */
make_learner(&learner[who], NET_INPUT, HIDDEN_NODES, 2);
/* Set learning rate */
learner[who].alpha = 0.0001;
/* learner[who].alpha = 0.0; printf("WARNING: alpha is 0\n"); */
/* Create network filename */
sprintf(fname, DATADIR "/networks/bluemoon.net.%s.%s",
g->p[who].p_ptr->name,
g->p[!who].p_ptr->name);
/* Attempt to load net weights from disk */
if (load_net(&learner[who], fname))
{
/* Create warning message */
sprintf(buf,
_("WARNING: Couldn't open %s, expect random play!\n"),
fname);
/* Send message */
message_add(buf);
}
/* Evaluate starting position */
eval_game(g, who);
/* Message */
if (verbose >= 1)
{
/* Print win probabilities of starting state */
printf("%s Start prob: %f %f\n", g->p[who].p_ptr->name,
learner[who].win_prob[0],
learner[who].win_prob[1]);
}
}
/*
* Return how many more bluff cards we can play before it is obvious that
* it is bad.
*
* Return a negative number if we are already at or past that point.
*/
static int check_auto_bluff(game *g, int future)
{
player *p;
card *c;
int i;
int bluff = 0, unknown_f = 0, unknown_e = 0;
/* Get player pointer */
p = &g->p[g->turn];
/* Check for uncallable bluff */
for (i = 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip cards without category 3 special text */
if (c->d_ptr->special_cat != 3) continue;
/* Check for "you may not call bluff" text */
if (c->d_ptr->special_effect !=(S3_YOU_MAY_NOT | S3_CALL_BLUFF))
{
continue;
}
if (c->active && !c->text_ignored) return 99;
if (c->where == LOC_HAND && future) return 99;
}
/* Loop over cards */
for (i = 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Check for fire bluff icon */
if (c->d_ptr->icons & ICON_BLUFF_F)
{
/* Count cards with unknown locations */
if (!c->loc_known) unknown_f++;
}
/* Check for earth bluff icon */
if (c->d_ptr->icons & ICON_BLUFF_E)
{
/* Count cards with unknown locations */
if (!c->loc_known) unknown_e++;
}
/* Count bluff cards */
if (c->bluff) bluff++;
}
/* Check for fight started */
if (g->fight_started)
{
/* Return number of excess unknown bluff cards */
return g->fight_element ? unknown_e - bluff : unknown_f - bluff;
}
/* Return number of excess unknown bluff cards in lesser element */
return unknown_e < unknown_f ? unknown_e - bluff : unknown_f - bluff;
}
/*
* Add a list of legal support phase actions to the given list.
*
* This includes loading ships and playing bluff cards.
*/
static int legal_support(game *g, action *legal, int n)
{
player *p;
card *c, *d;
int i, j;
/* Get player pointer */
p = &g->p[g->turn];
/* Check for no support actions allowed */
if (!support_allowed(g)) return n;
/* Look for played ships */
for (i = 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Don't load ships when checking forced retreat */
if (checking_retreat) break;
/* Skip inactive cards */
if (!c->active) continue;
/* Skip non-ship cards */
if (!c->d_ptr->capacity) continue;
/* Loop over other cards */
for (j = p->last_played + 1; j < DECK_SIZE; j++)
{
/* Get card pointer */
d = &p->deck[j];
/* Skip ineligible cards */
if (!card_eligible(g, d->d_ptr)) continue;
/* Skip cards that aren't character/support/booster */
if (d->d_ptr->type != TYPE_CHARACTER &&
d->d_ptr->type != TYPE_BOOSTER &&
d->d_ptr->type != TYPE_SUPPORT) continue;
/* Check for legal loading */
if (load_allowed(g, c->d_ptr))
{
/* Add load action */
legal[n].act = ACT_LOAD;
legal[n].index = j;
legal[n].arg = d->d_ptr;
legal[n++].target = c->d_ptr;
}
}
/* XXX Stop after first ship with space */
break;
}
/* Check for bluffs illegal */
if (!bluff_legal(g, g->turn)) return n;
/* Check for no more unknown bluff cards available */
if (check_auto_bluff(g, 1) < 1) return n;
/* Look for bluff cards */
for (i = p->last_played + 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip ineligible cards */
if (!card_eligible(g, c->d_ptr)) continue;
/* Skip cards without bluff icons */
if (!(c->icons & ICON_BLUFF_MASK)) continue;
/* Add bluff action */
legal[n].act = ACT_BLUFF;
legal[n].index = i;
legal[n++].arg = c->d_ptr;
/* Any bluff is as good as another when checking retreat */
if (checking_retreat) break;
}
/* Return length of list */
return n;
}
/*
* Return a list of legal actions from the given game state.
*
* "ACT_NONE" means to take no action and advance the phase counter.
*/
static int legal_act(game *g, action *legal)
{
player *p, *opp;
card *c;
int power;
int i, n = 0;
/* Get player pointer */
p = &g->p[g->turn];
/* Get opponent pointer */
opp = &g->p[!g->turn];
/* Switch on phase */
switch (p->phase)
{
/* Beginning of turn */
case PHASE_BEGIN:
{
/* Loop over cards */
for (i = p->last_played + 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip inactive cards */
if (!c->active) continue;
/* Skip cards that aren't ships */
if (!c->d_ptr->capacity) continue;
/* Skip cards that are already landed */
if (c->landed) continue;
/* Add "land" action */
legal[n].act = ACT_LAND;
legal[n].index = i;
legal[n++].arg = c->d_ptr;
}
/* Loop over cards */
for (i = p->last_played + 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip non-bluff cards */
if (!c->bluff) continue;
/* Don't reveal when checking retreat */
if (!checking_retreat)
{
/* Add "reveal" action */
legal[n].act = ACT_REVEAL;
legal[n].index = i;
legal[n++].arg = c->d_ptr;
}
}
/* Check for active cards that can be retrieved */
for (i = p->last_played + 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip unretrieveable */
if (!retrieve_legal(g, c)) continue;
/* Add retrieve action */
legal[n].act = ACT_RETRIEVE;
legal[n].index = i;
legal[n++].arg = c->d_ptr;
}
/* No action is always allowed */
legal[n++].act = ACT_NONE;
/* Done */
return n;
}
/* Play a card */
case PHASE_LEADER:
case PHASE_CHAR:
case PHASE_SUPPORT:
{
if (check_auto_bluff(g, 1) < 0) return 0;
/* Loop over active cards */
for (i = 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip inactive cards */
if (!c->active) continue;
/* Skip cards with no special effect */
if (!c->d_ptr->special_cat) continue;
/* Skip cards with ignored text */
if (c->text_ignored) continue;
/* Skip cards already used */
if (c->used) continue;
/* Skip cards that can't be used anytime */
if (c->d_ptr->special_time != TIME_MYTURN)
continue;
/* Skip cards with no useful effect */
if (!special_possible(g, c->d_ptr)) continue;
/* Add action to use card power */
legal[n].act = ACT_USE;
legal[n++].arg = c->d_ptr;
}
/* Always use special text first if possible */
if (n) return n;
/* Look for cards to play */
for (i = p->last_played + 1; i < DECK_SIZE; i++)
{
/* Get card pointer */
c = &p->deck[i];
/* Skip randomly chosen cards */
if (c->random_fake) continue;
/* Skip ineligible cards */
if (!card_eligible(g, c->d_ptr)) continue;
/* Check for illegal leadership card */
if (c->d_ptr->type == TYPE_LEADERSHIP &&
p->phase != PHASE_LEADER) continue;
/* Check for illegal character card */
if (c->d_ptr->type == TYPE_CHARACTER &&
p->phase != PHASE_CHAR) continue;
/* Check for illegal booster card */
if (c->d_ptr->type == TYPE_BOOSTER &&
p->phase != PHASE_SUPPORT) continue;
/* Check for illegal support card */
if (c->d_ptr->type == TYPE_SUPPORT &&
p->phase != PHASE_SUPPORT) continue;
/* Check card legality */
if (!card_allowed(g, c->d_ptr)) continue;
/* Check for optional special effect */
if (((c->d_ptr->special_cat == 4 &&
c->d_ptr->special_effect & S4_OPTIONAL) ||
(c->d_ptr->special_cat == 8 &&
c->d_ptr->special_effect & S8_OPTIONAL))&&
!checking_retreat)
{
/* Playing card without effect */
legal[n].act = ACT_PLAY_NO;
legal[n].index = i;
legal[n++].arg = c->d_ptr;
}
/* Playing card is allowed */
legal[n].act = ACT_PLAY;
legal[n].index = i;
legal[n++].arg = c->d_ptr;
/* XXX Always play characters on ship */
if (p->phase == PHASE_CHAR && c->ship) return n;
}
/* Check for support phase */
if (p->phase == PHASE_SUPPORT)
{
/* Add support actions */
n = legal_support(g, legal, n);
}
/* Check every action when checking retreat */
if (checking_retreat && n > 0) break;
/* Advance phase allowed unless character is unplayed */
if (p->phase != PHASE_CHAR || p->char_played)
{
/* Add no action */
legal[n++].act = ACT_NONE;
}
/* Done, add special actions */
break;
}
/* Retreat phase */
case PHASE_RETREAT:
{
/* Retreat is always allowed */
legal[n++].act = ACT_RETREAT;
/* Not retreating is allowed as well */
legal[n++].act = ACT_NONE;