-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrftg.c
2794 lines (2308 loc) · 61.8 KB
/
rftg.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
/*
* Race for the Galaxy AI
*
* Copyright (C) 2009-2011 Keldon Jones
*
* Source file modified by B. Nordli, August 2014.
*
* 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 "rftg.h"
/*
* Reasons to restart main loop.
*/
#define RESTART_NEW 1
#define RESTART_NONE 2
#define RESTART_LOAD 3
#define RESTART_RESTORE 4
#define RESTART_UNDO 5
#define RESTART_UNDO_ROUND 6
#define RESTART_UNDO_GAME 7
#define RESTART_REDO 8
#define RESTART_REDO_ROUND 9
#define RESTART_REDO_GAME 10
#define RESTART_REPLAY 11
#define RESTART_CURRENT 12
/* API for sending game info to JS */
#define MSG_CARDINFO 0
#define MSG_GAMESTATE 1
#define MSG_LOGLINE 2
#define MSG_CHOICE 3
#define MSG_GAMEOVER 4
#define STATUS_DATA_SIZE 100000
#define STATUS_DATA_STR_SIZE 500000
static int *status_data, status_data_ptr, status_data_str_ptr;
static char *status_data_str;
static void reset_status_data(void) {
status_data_ptr = 0;
status_data_str_ptr = 0;
}
static void add_data(int v) {
status_data[status_data_ptr++] = v;
}
static void add_data_str(char *v) {
char *dst = status_data_str + status_data_str_ptr;
status_data[status_data_ptr++] = (int)dst;
status_data_str_ptr += strlen(v) + 1;
strcpy(dst, v);
}
int *get_status_data(void) {
printf("buffers: %d %d\n", status_data_ptr, status_data_str_ptr);
return status_data;
}
int get_status_data_size(void) {
return status_data_ptr;
}
int last_message_send = 0, message_count = 0;
void add_message(char *tag, char *msg) {
if (message_count++ < last_message_send) return;
last_message_send++;
add_data(MSG_LOGLINE);
add_data_str(tag);
add_data_str(msg);
}
/* API for getting the user input from JS */
static int *selection_result_ptr;
static int *selection_result_len_ptr;
int *selection_result(int len) {
*selection_result_len_ptr += len;
return selection_result_ptr;
}
/*
* User options.
*/
typedef struct options
{
/* Number of players */
int num_players;
/* Expansion level */
int expanded;
/* Player name */
char *player_name;
/* Two-player advanced game */
int advanced;
/* Disable goals */
int disable_goal;
/* Disable takeovers */
int disable_takeover;
/* Customize seed */
int customize_seed;
/* Seed value */
unsigned int seed;
/* Campaign name */
char *campaign_name;
/* Display the VP value for cards in hand */
int vp_in_hand;
/* Autosave */
int auto_save;
} options;
extern void reset_status(game *g, int who);
#define FALSE 0
#define TRUE 1
/*
* Our default options.
*/
static options opt =
{
2, // num_players
};
/*
* Current (real) game state.
*/
static game real_game;
/*
* Current undo position.
*/
static int num_undo;
/*
* Total number of undo positions saved.
*/
static int max_undo;
/*
* Whether the game is replaying or not.
*/
static int game_replaying;
/*
* Choice logs for each player.
*/
static int *orig_log[MAX_PLAYER];
/*
* Original log sizes for each player.
*/
static int orig_log_size[MAX_PLAYER];
/*
* Games started (used for random sampling)
*/
static int games_started;
/*
* Player we're playing as.
*/
static int player_us;
/*
* We have restarted the main game loop.
*/
static int restart_loop;
static char *goal_description[MAX_GOAL] =
{
"First to have five VP chips",
"First to have worlds of all four kinds",
"First to have three Alien cards",
"First to discard at end of round",
"First to have powers in all phases, plus Trade",
"First to place a 6-cost development giving ? VPs",
"First to have three Uplift cards",
"First to have four goods",
"First to have eight cards",
"First to have negative Military and two worlds\n"
" or a takeover attack power and two Military worlds",
"First to have two prestige chips and three VP chips",
"First to have three Imperium cards\n"
" or four Military worlds",
"Most total military",
"Most Novelty and/or Rare worlds",
"Most developments",
"Most production worlds",
"Most cards with Explore powers",
"Most Rebel Military worlds",
"Most prestige chips",
"Most cards with Consume powers",
};
/*
* Player names.
*/
static char *player_names[MAX_PLAYER] = { "[P0]", "[P1]", "[P2]", "[P3]", "[P4]", "[P5]" };
typedef struct discounts
{
/* The base discount */
int base;
/* The current temporary discount */
int bonus;
/* Additional specific discount */
int specific[6];
/* May discard to place at zero count */
int zero;
/* Additional discount when paying for military */
int pay_discount;
/* May pay for military with 0 discount (Rebel Cantina) */
int non_alien_mil_0;
/* May pay for military with 1 discount (Contact Specialist) */
int non_alien_mil_1;
/* May pay for rebel worlds with 2 discount (Rebel Alliance) */
int rebel_mil_2;
/* May pay for chromosome worlds (Ravaged Uplift World) */
int chromo_mil;
/* May pay for alien worlds (Alien Research Team) */
int alien_mil;
/* May discard to conquer with 0 discount (Imperium Invasion Fleet) */
int conquer_settle_0;
/* May discard to conquer with 2 discount (Imperium Cloaking Tech) */
int conquer_settle_2;
/* Any value is set */
int has_data;
} discounts;
typedef struct mil_strength
{
/* Base military */
int base;
/* Current temporary military */
int bonus;
/* Maximum additional temporary military */
int max_bonus;
/* Additional military against rebel worlds */
int rebel;
/* Additional specific military */
int specific[6];
/* Additional extra defense during takeovers */
int defense;
/* Additional military when using attack imperium TO power */
int attack_imperium;
/* Name of attack imperium TO power */
char imp_card[64];
/* Imperium world played */
int imperium;
/* Rebel military world played */
int military_rebel;
/* Any value is set */
int has_data;
} mil_strength;
/*
* Check whether a log position marks a round boundary.
*/
int is_round_boundary(int advanced, int *p)
{
/* Only start and action choices are boundary */
if (*p != CHOICE_START && *p != CHOICE_ACTION) return FALSE;
/* Second choice of Psi-Crystal is not a boundary */
/* XXX This only works in newer save games */
if (advanced && *(p + 1) == 2) return FALSE;
/* Everything else is */
return TRUE;
}
/*
* Add text to the message buffer.
*/
void message_add(game *g, char *msg)
{
add_message("", msg);
}
/*
* Add formatted text to the message buffer.
*/
void message_add_formatted(game *g, char *msg, char *tag)
{
add_message(tag, msg);
}
/*
* Add a private message to the message buffer.
*/
void message_add_private(game *g, int who, char *msg, char *tag)
{
/* Verify we are the correct player */
if (who == player_us)
{
/* Add message */
message_add_formatted(g, msg, tag);
}
}
/*
* Handle an error dialog with a message.
*/
void display_error(char *msg)
{
fprintf(stderr, "ERROR: %s\n",msg);
}
/*
* Use simple random number generator.
*/
int game_rand(game *g)
{
/* Call simple random number generator */
return simple_rand(&g->random_seed);
}
/*
* Compute the military/cost needed for a military world.
*/
static void military_world_payment(game *g, int who, int which,
int mil_only, int mil_bonus, discounts *d_ptr,
int *military, int *cost, char **cost_card)
{
card *c_ptr;
int strength, pay_for_mil;
/* Get card */
c_ptr = &g->deck[which];
/* Get current strength */
strength = strength_against(g, who, which, -1, 0) + mil_bonus;
/* Compute extra military needed */
*military = c_ptr->d_ptr->cost - strength;
/* Do not reduce below 0 */
if (*military <= 0) *military = 0;
/* Reset cost and pay-for-military */
pay_for_mil = *cost = -1;
/* Check for no pay-for-military available */
if (mil_only) return;
/* Check for Rebel Alliance */
if (d_ptr->rebel_mil_2 && (c_ptr->d_ptr->flags & FLAG_REBEL))
{
/* Set reduction to 2 */
pay_for_mil = 2;
/* Save card name */
*cost_card = "Rebel Alliance";
}
/* Check for Contact Specialist */
else if (d_ptr->non_alien_mil_1 &&
c_ptr->d_ptr->good_type != GOOD_ALIEN)
{
/* Set reduction to 1 */
pay_for_mil = 1;
/* Save card name */
*cost_card = "Contact Specialist";
}
/* Check for Rebel Cantina */
else if (d_ptr->non_alien_mil_0 &&
c_ptr->d_ptr->good_type != GOOD_ALIEN)
{
/* Set reduction to 0 */
pay_for_mil = 0;
/* Save card name */
*cost_card = "Rebel Cantina";
}
/* Check for Alien Research Team */
else if (d_ptr->alien_mil &&
c_ptr->d_ptr->good_type == GOOD_ALIEN)
{
/* Set reduction to 0 */
pay_for_mil = 0;
/* Save card name */
*cost_card = "Alien Research Team";
}
/* Check for Ravaged Uplift World */
else if (d_ptr->chromo_mil && c_ptr->d_ptr->flags & FLAG_CHROMO)
{
/* Set reduction to 0 */
pay_for_mil = 0;
/* Save card name */
*cost_card = "Ravaged Uplift World";
}
/* Check for any pay-for-military power */
if (pay_for_mil >= 0)
{
/* Compute cost */
*cost = c_ptr->d_ptr->cost - d_ptr->base - d_ptr->bonus -
d_ptr->specific[c_ptr->d_ptr->good_type] -
pay_for_mil - d_ptr->pay_discount;
/* Do not reduce below 0 */
if (*cost < 0) *cost = 0;
}
}
/*
* Compute the cost/military needed for a non-military world.
*/
static void peaceful_world_payment(game *g, int who, int which,
int mil_only, discounts *d_ptr,
int *cost, int *ict_mil, int *iif_mil)
{
card *c_ptr;
int strength;
/* Get card */
c_ptr = &g->deck[which];
/* Check for no normal payment available */
if (mil_only)
{
/* Disable payment */
*cost = -1;
}
else
{
/* Compute cost */
*cost = c_ptr->d_ptr->cost - d_ptr->base - d_ptr->bonus -
d_ptr->specific[c_ptr->d_ptr->good_type];
/* Do not reduce below 0 */
if (*cost < 0) *cost = 0;
}
/* Compute strength */
strength = strength_against(g, who, which, -1, 0);
/* Reset ICT/IIF military */
*ict_mil = *iif_mil = -1;
/* Check for Imperium Cloaking Technology */
if (d_ptr->conquer_settle_2)
{
/* Compute extra military needed */
*ict_mil = c_ptr->d_ptr->cost - strength - 2;
/* Do not reduce below 0 */
if (*ict_mil < 0) *ict_mil = 0;
}
/* Check for Imperium Invasion Fleet */
if (d_ptr->conquer_settle_0)
{
/* Compute extra military needed */
*iif_mil = c_ptr->d_ptr->cost - strength;
/* Do not reduce below 0 */
if (*iif_mil < 0) *iif_mil = 0;
}
}
int callbuffer[4096];
int *get_callbuffer(void) {
return callbuffer;
}
/*
* Function to determine whether selected cards meet payment.
*/
int action_check_payment(int which, int n, int ns, int mil, int bonus)
{
game sim;
int i;
int *list = callbuffer, *special = callbuffer + n;
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
sim.game_over = 0;
/* Loop over players */
for (i = 0; i < sim.num_players; i++)
{
/* Have AI make any pending decisions for this player */
sim.p[i].control = &ai_func;
}
/* Try to make payment */
return payment_callback(&sim, player_us, which,
list, n, special, ns, mil,
bonus);
}
/*
* Function to determine whether selected goods can be consumed.
*/
int action_check_goods(int cidx, int oidx, int n)
{
game sim;
int *list = callbuffer;
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
/* Try to make payment */
return good_chosen(&sim, player_us, cidx, oidx, list, n);
}
/*
* Function to determine whether selected card can be taken over.
*/
int action_check_takeover(int target, int special)
{
game sim;
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
sim.game_over = 0;
/* Check takeover legality */
return takeover_callback(&sim, special, target);
}
/*
* Function to determine whether selected cards are a legal defense.
*/
int action_check_defend(int n, int ns)
{
game sim;
int *list = callbuffer, *special = callbuffer + n;
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
sim.game_over = 0;
/* Try to defend (we don't care about win/lose, just legality */
return defend_callback(&sim, player_us, 0, list, n, special, ns);
}
/*
* Function to determine whether selected cards are a legal world upgrade.
*/
int action_check_upgrade(int upgrade, int upgraded)
{
game sim;
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
sim.game_over = 0;
/* Try to upgrade */
return upgrade_chosen(&sim, player_us, upgrade, upgraded);
}
/*
* Function to determine whether selected cards are legal to consume.
*/
int action_check_consume(int cidx, int oidx, int n)
{
game sim;
int *list = callbuffer;
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
/* Try to consume */
return consume_hand_chosen(&sim, player_us, cidx, oidx, list, n);
}
/*
* Return whether the selected world and hand is a valid start.
*/
int action_check_start(int n, int ns)
{
game sim;
int *list = callbuffer, *special = callbuffer + n;
/* Check for exactly 1 world selected */
if (ns != 1) return 0;
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
/* Try to start */
return start_callback(&sim, player_us, list, n, special, ns);
}
/*
* Find if there are any forced choices for payment.
*/
int compute_forced_choice(int which, int num, int num_special, int mil_only, int mil_bonus)
{
int *list = callbuffer, *special = callbuffer + num;
game sim;
int i, j, num_choice;
int special_choice[10];
int special_set;
/* Clear forced variables */
int special_mask = ~0;
int forced_hand = 1;
if (num > 20 || num_special > 10) return 0;
/* Loop over all reasonable hand payments */
for (i = 0; i <= num; ++i)
{
/* Loop over all sets of special cards */
for (special_set = 0; special_set < (1 << num_special); ++special_set)
{
/* Clear number of special cards used */
num_choice = 0;
/* Loop over special cards */
for (j = 0; j < num_special; ++j)
{
/* Check for this special card selected */
if (special_set & (1 << j))
{
/* Add card */
special_choice[num_choice++] = special[j];
}
}
/* Copy game */
sim = real_game;
/* Set simulation flag */
sim.simulation = 1;
/* Loop over players */
for (j = 0; j < sim.num_players; j++)
{
/* Have AI make any pending decisions for this player */
sim.p[j].control = &ai_func;
}
/* Try to make payment */
if (payment_callback(&sim, player_us, which,
list, i, special_choice, num_choice,
mil_only, mil_bonus))
{
/* Check for legal without all hand cards */
if (i != num) forced_hand = 0;
/* Update mask */
special_mask &= special_set;
}
/* Optimization */
if (!special_mask && !forced_hand) return 0;
}
}
return (special_mask << 1) | forced_hand;
}
/*
* Compute settle discounts for a player.
*/
static void compute_discounts(game *g, int who, discounts *d_ptr)
{
power_where w_list[100];
power *o_ptr;
int i, n;
/* Clear discounts */
memset(d_ptr, 0, sizeof(discounts));
/* Set bonus discounts */
d_ptr->bonus = g->p[who].bonus_reduce;
/* Check for prestige settle */
if ((g->cur_action == ACT_SETTLE || g->cur_action == ACT_SETTLE2) &&
player_chose(g, who, ACT_PRESTIGE | g->cur_action))
{
/* Add prestige bonus */
d_ptr->bonus += 3;
}
/* Get settle phase powers */
n = get_powers(g, who, PHASE_SETTLE, w_list);
/* Loop over powers */
for (i = 0; i < n; i++)
{
/* Get power pointer */
o_ptr = w_list[i].o_ptr;
/* Check discard for 0 */
if (o_ptr->code == (P3_DISCARD | P3_REDUCE_ZERO))
d_ptr->zero += 1;
/* Check for reduce power */
if (o_ptr->code & P3_REDUCE)
{
/* Check for general discount */
if (o_ptr->code == P3_REDUCE)
d_ptr->base += o_ptr->value;
/* Check for discount against Novelty worlds */
if (o_ptr->code & P3_NOVELTY)
d_ptr->specific[GOOD_NOVELTY] += o_ptr->value;
/* Check for discount against Rare worlds */
if (o_ptr->code & P3_RARE)
d_ptr->specific[GOOD_RARE] += o_ptr->value;
/* Check for discount against Genes worlds */
if (o_ptr->code & P3_GENE)
d_ptr->specific[GOOD_GENE] += o_ptr->value;
/* Check for discount against Alien worlds */
if (o_ptr->code & P3_ALIEN)
d_ptr->specific[GOOD_ALIEN] += o_ptr->value;
}
/* Check for pay-for-military powers */
if (o_ptr->code & P3_PAY_MILITARY)
{
/* Check for non-alien power without discount */
if (o_ptr->code == P3_PAY_MILITARY && o_ptr->value == 0)
d_ptr->non_alien_mil_0 = TRUE;
/* Check for non-alien power with discount */
if (o_ptr->code == P3_PAY_MILITARY && o_ptr->value == 1)
d_ptr->non_alien_mil_1 = TRUE;
/* Check for rebel flag */
if (o_ptr->code & P3_AGAINST_REBEL)
d_ptr->rebel_mil_2 = TRUE;
/* Check for chromo flag */
if (o_ptr->code & P3_AGAINST_CHROMO)
d_ptr->chromo_mil = TRUE;
/* Check for alien flag */
if (o_ptr->code & P3_ALIEN)
d_ptr->alien_mil = TRUE;
}
/* Check for pay-for-military discount */
if (o_ptr->code & P3_PAY_DISCOUNT)
d_ptr->pay_discount += o_ptr->value;
/* Check for conquer settle without discount */
if ((o_ptr->code & P3_CONQUER_SETTLE) && o_ptr->value == 0)
d_ptr->conquer_settle_0 = TRUE;
/* Check for conquer settle with discount */
if ((o_ptr->code & P3_CONQUER_SETTLE) && o_ptr->value == 2)
d_ptr->conquer_settle_2 = TRUE;
}
/* Check for any modifiers */
d_ptr->has_data = d_ptr->base || d_ptr->bonus ||
d_ptr->specific[GOOD_NOVELTY] || d_ptr->specific[GOOD_RARE] ||
d_ptr->specific[GOOD_GENE] || d_ptr->specific[GOOD_ALIEN] ||
d_ptr->zero || d_ptr->pay_discount ||
d_ptr->non_alien_mil_0 || d_ptr->non_alien_mil_1 ||
d_ptr->rebel_mil_2 || d_ptr->chromo_mil || d_ptr->alien_mil ||
d_ptr->conquer_settle_0 || d_ptr->conquer_settle_2;
}
/*
* Compute military strength for a player.
*/
static void compute_military(game *g, int who, mil_strength *m_ptr)
{
card *c_ptr;
power *o_ptr;
int x, i, hand_size, hand_military = 0, rare_goods;
/* Start strengths at 0 */
memset(m_ptr, 0, sizeof(mil_strength));
/* Begin with base military strength */
m_ptr->base = total_military(g, who);
/* Set bonus military */
m_ptr->bonus = g->p[who].bonus_military;
/* Get first active card */
x = g->p[who].start_head[WHERE_ACTIVE];
/* Count number of rare goods */
rare_goods = get_goods(g, who, NULL, GOOD_RARE);
/* Loop over cards */
for ( ; x != -1; x = g->deck[x].start_next)
{
/* Get card pointer */
c_ptr = &g->deck[x];
/* Loop over card's powers */
for (i = 0; i < c_ptr->d_ptr->num_power; i++)
{
/* Get power pointer */
o_ptr = &c_ptr->d_ptr->powers[i];
/* Skip incorrect phase */
if (o_ptr->phase != PHASE_SETTLE) continue;
/* Check for discard power */
if ((o_ptr->code & P3_DISCARD) && c_ptr->where == WHERE_DISCARD)
continue;
/* Check for defense power */
if (o_ptr->code & P3_TAKEOVER_DEFENSE && takeovers_enabled(g))
{
/* Add defense for military worlds */
m_ptr->defense +=
count_active_flags(g, who, FLAG_MILITARY);
/* Add extra defense for Rebel military worlds */
m_ptr->defense +=
count_active_flags(g, who, FLAG_REBEL | FLAG_MILITARY);
}
/* Check for takeover imperium power */
if (o_ptr->code & P3_TAKEOVER_IMPERIUM && takeovers_enabled(g))
{
/* Set imperium attack */
m_ptr->attack_imperium =
2 * count_active_flags(g, who, FLAG_REBEL | FLAG_MILITARY);
/* Check if card name already set */
if (strlen(m_ptr->imp_card))
{
/* XXX Use name of both cards */
strcpy(m_ptr->imp_card, "Rebel Alliance/Rebel Sneak Attack");
}
else
{
/* Remember name of card */
strcpy(m_ptr->imp_card, c_ptr->d_ptr->name);
}
}
/* Skip used powers */
if (c_ptr->misc & (1 << (MISC_USED_SHIFT + i))) continue;
/* Check for military from hand */
if (o_ptr->code & P3_MILITARY_HAND)
hand_military += o_ptr->value;
/* Skip non-military powers */
if (!(o_ptr->code & P3_EXTRA_MILITARY)) continue;
/* Check for discard for military */
if (o_ptr->code & P3_DISCARD)
m_ptr->max_bonus += o_ptr->value;
/* Check for prestige for military */
if ((o_ptr->code & P3_CONSUME_PRESTIGE) && g->p[who].prestige)
m_ptr->max_bonus += o_ptr->value;
/* Check for good for military */
if ((o_ptr->code & P3_CONSUME_RARE) && rare_goods)
{
m_ptr->max_bonus += o_ptr->value;
--rare_goods;
}
/* Check for strength against rebels */
if (o_ptr->code & P3_AGAINST_REBEL)
m_ptr->rebel += o_ptr->value;
/* Check for strength against Novelty worlds */
if (o_ptr->code & P3_NOVELTY)
m_ptr->specific[GOOD_NOVELTY] += o_ptr->value;
/* Check for strength against Rare worlds */
if (o_ptr->code & P3_RARE)
m_ptr->specific[GOOD_RARE] += o_ptr->value;
/* Check for strength against Genes worlds */
if (o_ptr->code & P3_GENE)
m_ptr->specific[GOOD_GENE] += o_ptr->value;
/* Check for strength against Alien worlds */
if (o_ptr->code & P3_ALIEN)
m_ptr->specific[GOOD_ALIEN] += o_ptr->value;
}
}
/* Get player hand size */
hand_size = count_player_area(g, who, WHERE_HAND);
/* Reduce maximum military from hand */
if (hand_size < hand_military) hand_military = hand_size;
/* Add military from hand to max temporary military */
m_ptr->max_bonus += hand_military;
/* Check for takeovers enabled and imperium card played */
m_ptr->imperium = takeovers_enabled(g) &&
count_active_flags(g, who, FLAG_IMPERIUM);
/* Check for takeovers enabled and rebel military world played */
m_ptr->military_rebel = takeovers_enabled(g) &&
count_active_flags(g, who, FLAG_MILITARY | FLAG_REBEL);
/* Check for any modifiers */
m_ptr->has_data = m_ptr->base || m_ptr->bonus || m_ptr->rebel ||
m_ptr->specific[GOOD_NOVELTY] || m_ptr->specific[GOOD_RARE] ||
m_ptr->specific[GOOD_GENE] || m_ptr->specific[GOOD_ALIEN] ||
m_ptr->defense || m_ptr->attack_imperium || m_ptr->imperium ||
m_ptr->military_rebel || m_ptr->max_bonus;
}
/*
* Return a "score" for sorting consume powers.
*/
static int score_consume(power *o_ptr)
{
int vp = 0, card = 0, prestige = 0, goods = 1;
int vp_mult = 1, score = 0;
/* Check for discard form hand */
if (o_ptr->code & P4_DISCARD_HAND)
{
/* Always discard from hand last */
score -= 1000;
/* Check for VP awarded */
if (o_ptr->code & P4_GET_VP) vp += o_ptr->value;
/* Check for card awarded */
if (o_ptr->code & P4_GET_CARD) card += o_ptr->value;
/* Check for prestige awarded */
if (o_ptr->code & P4_GET_PRESTIGE) prestige += o_ptr->value;