-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai.c
6719 lines (5501 loc) · 140 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
/*
* Race for the Galaxy AI
*
* Copyright (C) 2009-2011 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 "rftg.h"
#include "net.h"
/* #define DEBUG */
/*
* Track number of times neural net is computed.
*/
static int num_computes;
/*
* A neural net for evaluating hand and active cards.
*/
static net eval;
/*
* A neural net for predicting role choices.
*/
static net role;
/*
* Counters for tracking usefulness of role prediction.
*/
static int role_hit, role_miss;
static double role_avg;
/*
* Size of evaluator neural net.
*/
#define EVAL_MISC 77
#define EVAL_PLAYER 143
#define EVAL_HIDDEN 50
/*
* Size of role predictor neural net.
*/
#define ROLE_MISC 76
#define ROLE_PLAYER 114
#define ROLE_HIDDEN 50
/*
* Number of outputs for role predictor network (basic and advanced).
*/
#define ROLE_OUT 7
#define ROLE_OUT_ADV 23
#define ROLE_OUT_EXP3 15
#define ROLE_OUT_ADV_EXP3 76
/*
* Amount of current turn to complete simulation of.
*/
#define COMPLETE_ROUND 0
#define COMPLETE_PHASE 1
#define COMPLETE_DEVSET 2
/*
* Forward declaration.
*/
static void initial_training(game *g);
static void setup_nets(game *g);
static void fill_adv_combo(void);
/*
* Initialize AI.
*/
static void ai_initialize(game *g, int who, double factor)
{
char fname[1024];
static int loaded_p, loaded_e, loaded_a;
/* Create table of advanced action combinations */
fill_adv_combo();
/* Do nothing if correct networks already loaded */
if (loaded_p == g->num_players && loaded_e == g->expanded &&
loaded_a == g->advanced) return;
/* Free old networks if some already loaded */
if (loaded_p > 0)
{
/* Free old networks */
free_net(&eval);
free_net(&role);
}
/* Compute size and input names of networks */
setup_nets(g);
/* Set learning rate */
eval.alpha = 0.0001 * factor;
#ifdef DEBUG
eval.alpha = 0.0;
#endif
/* Create evaluator filename */
sprintf(fname, RFTGDIR "/network/rftg.eval.%d.%d%s.net", g->expanded,
g->num_players, g->advanced ? "a" : "");
/* Attempt to load network weights from disk */
if (load_net(&eval, fname))
{
/* Try looking under current directory */
sprintf(fname, "network/rftg.eval.%d.%d%s.net", g->expanded,
g->num_players, g->advanced ? "a" : "");
/* Attempt to load again */
if (load_net(&eval, fname))
{
/* Print warning */
printf("Warning: Couldn't open %s\n", fname);
/* Perform initial training on new network */
initial_training(g);
}
}
/* Set learning rate */
role.alpha = 0.0005 * factor;
#ifdef DEBUG
role.alpha = 0.0;
#endif
/* Create predictor filename */
sprintf(fname, RFTGDIR "/network/rftg.role.%d.%d%s.net", g->expanded,
g->num_players, g->advanced ? "a" : "");
/* Attempt to load network weights from disk */
if (load_net(&role, fname))
{
/* Try looking under current directory */
sprintf(fname, "network/rftg.role.%d.%d%s.net", g->expanded,
g->num_players, g->advanced ? "a" : "");
/* Attempt to load again */
if (load_net(&role, fname))
{
/* Print warning */
printf("Warning: Couldn't open %s\n", fname);
}
}
/* Mark network as loaded */
loaded_p = g->num_players;
loaded_e = g->expanded;
loaded_a = g->advanced;
}
/*
* Called when player spots have been rotated.
*
* We need to do nothing.
*/
static void ai_notify_rotation(game *g, int who)
{
}
/*
* Copy the game state to a temporary copy so that we can simulate the future.
*/
static void simulate_game(game *sim, game *orig, int who)
{
int i;
/* Copy game */
memcpy(sim, orig, sizeof(game));
/* Loop over players */
for (i = 0; i < sim->num_players; i++)
{
/* Move choice log position to end */
sim->p[i].choice_pos = sim->p[i].choice_size;
}
/* Check for first-level simulation */
if (!sim->simulation)
{
/* Set simulation flag */
sim->simulation = 1;
/* Lose real random seed */
sim->random_seed = 1;
/* Remember whose point-of-view to use */
sim->sim_who = who;
/* Loop over players */
for (i = 0; i < sim->num_players; i++)
{
/* Set action functions to AI */
sim->p[i].control = &ai_func;
}
}
}
/*
* Complete the current turn.
*
* We call this before evaluating a game state, to attempt to foresee
* consequences of chosen actions.
*/
static void complete_turn(game *g, int partial)
{
player *p_ptr;
int i, target;
/* Ensure game is simulated */
if (!g->simulation)
{
/* Error */
printf("complete_turn() called with real game!\n");
exit(1);
}
/* Do nothing in aborted games */
if (g->game_over) return;
/* Finish current phase */
for (i = g->turn + 1; i < g->num_players; i++)
{
/* Get player pointer */
p_ptr = &g->p[i];
/* Set turn */
g->turn = i;
/* Check for consume phase */
if (g->cur_action == ACT_CONSUME_TRADE)
{
/* Check for consume-trade chosen */
if (player_chose(g, i, ACT_CONSUME_TRADE))
{
/* Trade a good */
trade_action(g, i, 0, 1);
}
/* Use consume powers until none are available */
while (consume_action(g, i));
}
/* Check for produce phase */
if (g->cur_action == ACT_PRODUCE)
{
/* Use produce phase powers */
while (produce_action(g, i));
}
}
/* Resolve any pending takeovers */
if (g->cur_action == ACT_SETTLE || g->cur_action == ACT_SETTLE2)
resolve_takeovers(g);
/* Use final produce powers */
if (g->cur_action == ACT_PRODUCE) phase_produce_end(g);
/* Check for partial completion request */
if (partial == COMPLETE_PHASE) return;
/* Clear temp flags from just-finished phase */
clear_temp(g);
/* Check goals from just-finished phase */
check_goals(g);
/* Check prestige from just-finished phase */
check_prestige(g);
/* Loop over remaining phases */
for (i = g->cur_action + 1; i <= ACT_PRODUCE; i++)
{
/* Check for stop after develop/settle */
if (partial == COMPLETE_DEVSET && i >= ACT_CONSUME_TRADE)
{
/* Stop simulation */
return;
}
/* Set game phase */
g->cur_action = i;
/* Skip unselected actions */
if (!g->action_selected[i]) continue;
/* Handle phase */
switch (i)
{
case ACT_SEARCH: phase_search(g); break;
case ACT_EXPLORE_5_0: phase_explore(g); break;
case ACT_DEVELOP:
case ACT_DEVELOP2: phase_develop(g); break;
case ACT_SETTLE:
case ACT_SETTLE2: phase_settle(g); break;
case ACT_CONSUME_TRADE: phase_consume(g); break;
case ACT_PRODUCE: phase_produce(g); break;
}
}
/* Clear current action */
g->cur_action = -1;
/* Handle discard phase */
phase_discard(g);
/* Check intermediate goals */
check_goals(g);
/* Check for game end */
if (g->vp_pool <= 0) g->game_over = 1;
/* Check for full table */
for (i = 0; i < g->num_players; i++)
{
/* Get player pointer */
p_ptr = &g->p[i];
/* Assume player needs 12 cards to end game */
target = 12;
/* Check for "game ends at 14" flag */
if (count_active_flags(g, i, FLAG_GAME_END_14)) target = 14;
/* Check for enough cards */
if (count_player_area(g, i, WHERE_ACTIVE) >= target)
{
/* Game over */
g->game_over = 1;
}
/* Check for enough prestige to end game */
if (p_ptr->prestige >= 15) g->game_over = 1;
}
/* Handle beginning of next round if possible */
if (!g->game_over)
{
/* Award prestige bonuses */
start_prestige(g);
}
}
/*
* List of all advanced game action combinations.
*/
static int adv_combo[ROLE_OUT_ADV_EXP3][2] =
{
{ ACT_EXPLORE_5_0, ACT_EXPLORE_1_1 },
{ ACT_EXPLORE_5_0, ACT_DEVELOP },
{ ACT_EXPLORE_5_0, ACT_SETTLE },
{ ACT_EXPLORE_5_0, ACT_CONSUME_TRADE },
{ ACT_EXPLORE_5_0, ACT_CONSUME_X2 },
{ ACT_EXPLORE_5_0, ACT_PRODUCE },
{ ACT_EXPLORE_1_1, ACT_DEVELOP },
{ ACT_EXPLORE_1_1, ACT_SETTLE },
{ ACT_EXPLORE_1_1, ACT_CONSUME_TRADE },
{ ACT_EXPLORE_1_1, ACT_CONSUME_X2 },
{ ACT_EXPLORE_1_1, ACT_PRODUCE },
{ ACT_DEVELOP, ACT_DEVELOP2 },
{ ACT_DEVELOP, ACT_SETTLE },
{ ACT_DEVELOP, ACT_CONSUME_TRADE },
{ ACT_DEVELOP, ACT_CONSUME_X2 },
{ ACT_DEVELOP, ACT_PRODUCE },
{ ACT_SETTLE, ACT_SETTLE2 },
{ ACT_SETTLE, ACT_CONSUME_TRADE },
{ ACT_SETTLE, ACT_CONSUME_X2 },
{ ACT_SETTLE, ACT_PRODUCE },
{ ACT_CONSUME_TRADE, ACT_CONSUME_X2 },
{ ACT_CONSUME_TRADE, ACT_PRODUCE },
{ ACT_CONSUME_X2, ACT_PRODUCE }
};
/*
* Mapping of role outputs in non-advanced game to actions.
*/
static int role_out[ROLE_OUT_EXP3] =
{
ACT_EXPLORE_5_0,
ACT_EXPLORE_1_1,
ACT_DEVELOP,
ACT_SETTLE,
ACT_CONSUME_TRADE,
ACT_CONSUME_X2,
ACT_PRODUCE,
ACT_SEARCH,
ACT_PRESTIGE | ACT_EXPLORE_5_0,
ACT_PRESTIGE | ACT_EXPLORE_1_1,
ACT_PRESTIGE | ACT_DEVELOP,
ACT_PRESTIGE | ACT_SETTLE,
ACT_PRESTIGE | ACT_CONSUME_TRADE,
ACT_PRESTIGE | ACT_CONSUME_X2,
ACT_PRESTIGE | ACT_PRODUCE
};
/*
* Mapping from card indices to neural network inputs.
*/
static int card_input[MAX_DESIGN], num_c_input;
static int good_input[MAX_DESIGN], num_g_input;
/*
* Setup mappings of card indices to neural net inputs.
*
* Also create network input names.
*/
static void setup_nets(game *g)
{
design *d_ptr;
int i, j, k, n;
int inputs, outputs;
char buf[1024], name[1024];
/* Reset input numbers */
num_c_input = num_g_input = 0;
/* Loop over card designs */
for (i = 0; i < MAX_DESIGN; i++)
{
/* Clear input mapping */
card_input[i] = good_input[i] = -1;
/* Get design pointer */
d_ptr = &library[i];
/* Skip cards that have no copies in this game */
if (d_ptr->expand[g->expanded] == 0) continue;
/* Add mapping of this card design */
card_input[i] = num_c_input++;
/* Skip cards that cannot hold goods */
if (d_ptr->good_type == 0) continue;
/* Add mapping of this good-holding card */
good_input[i] = num_g_input++;
}
/* Compute number of eval inputs */
inputs = num_c_input + EVAL_MISC +
(num_c_input + num_g_input + EVAL_PLAYER) * g->num_players;
/* Create evaluator network */
make_learner(&eval, inputs, EVAL_HIDDEN, g->num_players);
/* Start at first input */
n = 0;
/* Set input names */
eval.input_name[n++] = strdup("Game over");
for (i = 0; i < 12; i++)
{
sprintf(buf, "VP Pool %d", i);
eval.input_name[n++] = strdup(buf);
}
for (i = 0; i < 12; i++)
{
sprintf(buf, "Max active %d", i);
eval.input_name[n++] = strdup(buf);
}
for (i = 0; i < 12; i++)
{
sprintf(buf, "Clock %d", i);
eval.input_name[n++] = strdup(buf);
}
for (i = 0; i < MAX_GOAL; i++)
{
sprintf(buf, "Goal active %d", i);
eval.input_name[n++] = strdup(buf);
}
for (i = 0; i < MAX_GOAL; i++)
{
sprintf(buf, "Goal available %d", i);
eval.input_name[n++] = strdup(buf);
}
for (i = 0; i < num_c_input; i++)
{
for (j = 0; j < MAX_DESIGN; j++)
{
if (card_input[j] == i) break;
}
sprintf(buf, "%s in hand", library[j].name);
eval.input_name[n++] = strdup(buf);
}
for (i = 0; i < g->num_players; i++)
{
if (i == 0) strcpy(name, "Us");
else
{
sprintf(name, "Opponent %d", i);
}
for (j = 0; j < num_c_input; j++)
{
for (k = 0; k < MAX_DESIGN; k++)
{
if (card_input[k] == j) break;
}
sprintf(buf, "%s active %s", name, library[k].name);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < num_g_input; j++)
{
for (k = 0; k < MAX_DESIGN; k++)
{
if (good_input[k] == j) break;
}
sprintf(buf, "%s good %s", name, library[k].name);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < 12; j++)
{
sprintf(buf, "%s %d goods", name, j);
eval.input_name[n++] = strdup(buf);
}
sprintf(buf, "%s Novelty good", name);
eval.input_name[n++] = strdup(buf);
sprintf(buf, "%s Rare good", name);
eval.input_name[n++] = strdup(buf);
sprintf(buf, "%s Gene good", name);
eval.input_name[n++] = strdup(buf);
sprintf(buf, "%s Alien good", name);
eval.input_name[n++] = strdup(buf);
for (j = 0; j < 12; j++)
{
sprintf(buf, "%s %d cards", name, j);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < 15; j++)
{
sprintf(buf, "%s %d developments", name, j);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < 17; j++)
{
sprintf(buf, "%s %d worlds", name, j);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < 5; j++)
{
sprintf(buf, "%s %d built behind", name, j);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < 15; j++)
{
sprintf(buf, "%s %d military", name, j);
eval.input_name[n++] = strdup(buf);
}
sprintf(buf, "%s explore mix", name);
eval.input_name[n++] = strdup(buf);
for (j = 0; j < MAX_GOAL; j++)
{
sprintf(buf, "%s goal %d claimed", name, j);
eval.input_name[n++] = strdup(buf);
}
sprintf(buf, "%s prestige action used", name);
eval.input_name[n++] = strdup(buf);
for (j = 0; j < 15; j++)
{
sprintf(buf, "%s %d prestige", name, j);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < 5; j++)
{
sprintf(buf, "%s %d prestige behind", name, j);
eval.input_name[n++] = strdup(buf);
}
for (j = 0; j < 20; j++)
{
sprintf(buf, "%s %d points behind", name, j);
eval.input_name[n++] = strdup(buf);
}
sprintf(buf, "%s winner", name);
eval.input_name[n++] = strdup(buf);
}
if (n != inputs)
{
printf("Bad role setup %d %d!\n", n, inputs);
exit(1);
}
/* Compute number of role inputs */
inputs = ROLE_MISC +
(num_c_input + num_g_input + ROLE_PLAYER) * g->num_players;
/* Check for third expansion */
if (g->expanded >= 3)
{
/* Use third expansion number of outputs */
outputs = g->advanced ? ROLE_OUT_ADV_EXP3 : ROLE_OUT_EXP3;
}
else
{
/* Use simpler number of outputs */
outputs = g->advanced ? ROLE_OUT_ADV : ROLE_OUT;
}
/* Add inputs for raw action scorse */
inputs += outputs;
/* Create role predictor network */
make_learner(&role, inputs, ROLE_HIDDEN, outputs);
/* Start at first input */
n = 0;
/* Set input names */
for (i = 0; i < g->num_players; i++)
{
if (i == 0) strcpy(name, "Us");
else
{
sprintf(name, "Opponent %d", i);
}
for (j = 0; j < num_c_input; j++)
{
for (k = 0; k < MAX_DESIGN; k++)
{
if (card_input[k] == j) break;
}
sprintf(buf, "%s active %s", name, library[k].name);
role.input_name[n++] = strdup(buf);
}
for (j = 0; j < 12; j++)
{
sprintf(buf, "%s %d developments", name, j);
role.input_name[n++] = strdup(buf);
}
for (j = 0; j < 12; j++)
{
sprintf(buf, "%s %d worlds", name, j);
role.input_name[n++] = strdup(buf);
}
for (j = 0; j < num_g_input; j++)
{
for (k = 0; k < MAX_DESIGN; k++)
{
if (good_input[k] == j) break;
}
sprintf(buf, "%s good %s", name, library[k].name);
role.input_name[n++] = strdup(buf);
}
for (j = 0; j < 12; j++)
{
sprintf(buf, "%s %d goods", name, j);
role.input_name[n++] = strdup(buf);
}
sprintf(buf, "%s Novelty good", name);
role.input_name[n++] = strdup(buf);
sprintf(buf, "%s Rare good", name);
role.input_name[n++] = strdup(buf);
sprintf(buf, "%s Gene good", name);
role.input_name[n++] = strdup(buf);
sprintf(buf, "%s Alien good", name);
role.input_name[n++] = strdup(buf);
for (j = 0; j < 12; j++)
{
sprintf(buf, "%s %d cards", name, j);
role.input_name[n++] = strdup(buf);
}
for (j = 0; j < 15; j++)
{
sprintf(buf, "%s %d military", name, j);
role.input_name[n++] = strdup(buf);
}
sprintf(buf, "%s explore mix", name);
role.input_name[n++] = strdup(buf);
for (j = 0; j < MAX_GOAL; j++)
{
sprintf(buf, "%s goal %d claimed", name, j);
role.input_name[n++] = strdup(buf);
}
sprintf(buf, "%s prestige action used", name);
role.input_name[n++] = strdup(buf);
for (j = 0; j < 15; j++)
{
sprintf(buf, "%s %d prestige", name, j);
role.input_name[n++] = strdup(buf);
}
for (j = 0; j < MAX_ACTION; j++)
{
sprintf(buf, "%s prev %s", name, action_name(j));
role.input_name[n++] = strdup(buf);
}
}
for (i = 0; i < 12; i++)
{
sprintf(buf, "VP Pool %d", i);
role.input_name[n++] = strdup(buf);
}
for (i = 0; i < 12; i++)
{
sprintf(buf, "Max active %d", i);
role.input_name[n++] = strdup(buf);
}
for (i = 0; i < 12; i++)
{
sprintf(buf, "Clock %d", i);
role.input_name[n++] = strdup(buf);
}
for (i = 0; i < MAX_GOAL; i++)
{
sprintf(buf, "Goal active %d", i);
role.input_name[n++] = strdup(buf);
}
for (i = 0; i < MAX_GOAL; i++)
{
sprintf(buf, "Goal available %d", i);
role.input_name[n++] = strdup(buf);
}
for (i = 0; i < outputs; i++)
{
if (g->advanced)
{
sprintf(buf, "Raw %s/%s", action_name(adv_combo[i][0]),
action_name(adv_combo[i][1]));
}
else
{
sprintf(buf, "Raw %s", action_name(role_out[i]));
}
role.input_name[n++] = strdup(buf);
}
if (n != inputs)
{
printf("Bad role setup %d %d!\n", n, inputs);
exit(1);
}
}
/*
* Cached result from eval_game.
*/
typedef struct eval_cache
{
/* Hash value of game state */
uint64_t key;
/* Score to return */
double score;
/* Next cache entry in chain */
struct eval_cache *next;
} eval_cache;
/*
* Hash table for cached evaluation results.
*/
static eval_cache *eval_hash[65536];
/*
* Generic hash mixer.
*/
#define mix(a,b,c) \
{ \
a = a - b; a = a - c; a = a ^ (c >> 43); \
b = b - c; b = b - a; b = b ^ (a << 9); \
c = c - a; c = c - b; c = c ^ (b >> 8); \
a = a - b; a = a - c; a = a ^ (c >> 38); \
b = b - c; b = b - a; b = b ^ (a << 23); \
c = c - a; c = c - b; c = c ^ (b >> 5); \
a = a - b; a = a - c; a = a ^ (c >> 35); \
b = b - c; b = b - a; b = b ^ (a << 49); \
c = c - a; c = c - b; c = c ^ (b >> 11); \
a = a - b; a = a - c; a = a ^ (c >> 12); \
b = b - c; b = b - a; b = b ^ (a << 18); \
c = c - a; c = c - b; c = c ^ (b >> 22); \
}
/*
* Generic hash function.
*/
static uint64_t gen_hash(unsigned char *k, int length)
{
uint64_t a, b, c, len;
/* Internal state */
len = length;
a = b = 0x9e3779b97f4a7c13LL;
c = 0;
/* Most of key */
while (len > 23)
{
a += *(uint64_t *)(&k[0]);
b += *(uint64_t *)(&k[8]);
c += *(uint64_t *)(&k[16]);
mix(a, b, c);
k += 24;
len -= 24;
}
/* Rest of key */
c += length;
switch (len)
{
case 23: c += (uint64_t)k[22] << 56;
case 22: c += (uint64_t)k[21] << 48;
case 21: c += (uint64_t)k[20] << 40;
case 20: c += (uint64_t)k[19] << 32;
case 19: c += (uint64_t)k[18] << 24;
case 18: c += (uint64_t)k[17] << 16;
case 17: c += (uint64_t)k[16] << 8;
case 16: b += (uint64_t)k[15] << 56;
case 15: b += (uint64_t)k[14] << 48;
case 14: b += (uint64_t)k[13] << 40;
case 13: b += (uint64_t)k[12] << 32;
case 12: b += (uint64_t)k[11] << 24;
case 11: b += (uint64_t)k[10] << 16;
case 10: b += (uint64_t)k[9] << 8;
case 9: b += (uint64_t)k[8];
case 8: a += (uint64_t)k[7] << 56;
case 7: a += (uint64_t)k[6] << 48;
case 6: a += (uint64_t)k[5] << 40;
case 5: a += (uint64_t)k[4] << 32;
case 4: a += (uint64_t)k[3] << 24;
case 3: a += (uint64_t)k[2] << 16;
case 2: a += (uint64_t)k[1] << 8;
case 1: a += (uint64_t)k[0];
}
mix(a, b, c);
/* Return result */
return c;
}
/*
* Look up a game state in the result cache.
*/
static eval_cache *lookup_eval(game *g, int who)
{
player *p_ptr;
card *c_ptr;
eval_cache *e_ptr;
uint64_t key;
unsigned char value[498];
int len = 0;
int i;
/* Loop over cards */
for (i = 0; i < g->deck_size; i++)
{
/* Get card pointer */
c_ptr = &g->deck[i];
/* Add owner and location to value */
value[len++] = (unsigned char)c_ptr->owner;
value[len++] = (unsigned char)c_ptr->where;
}
/* Loop over players */
for (i = 0; i < g->num_players; i++)
{
/* Get player pointer */
p_ptr = &g->p[i];
/* Add victory points and fake card counts to value */
value[len++] = (unsigned char)p_ptr->vp;
value[len++] = (unsigned char)p_ptr->prestige;
value[len++] = (unsigned char)p_ptr->total_fake;
value[len++] = (unsigned char)p_ptr->fake_hand;
value[len++] = (unsigned char)p_ptr->fake_discards;
}
/* Add evaluating player to value */
value[len++] = (unsigned char)who;
/* Get key for value */
key = gen_hash(value, len);
/* Look for key in hash table */
for (e_ptr = eval_hash[key & 0xffff]; e_ptr; e_ptr = e_ptr->next)
{
/* Check for match */
if (e_ptr->key == key) break;
}
/* Check for no match */
if (!e_ptr)
{
/* Make new entry */
e_ptr = (eval_cache *)malloc(sizeof(eval_cache));
/* Set key of new entry */
e_ptr->key = key;
/* Clear score */
e_ptr->score = -1;
/* Insert into hash table */
e_ptr->next = eval_hash[key & 0xffff];
eval_hash[key & 0xffff] = e_ptr;
}
/* Return pointer */
return e_ptr;
}
/*
* Delete the entries in the evaluation cache.
*/
static void clear_eval_cache(void)
{
eval_cache *e_ptr;
int i;
/* Loop over each row in hash table */
for (i = 0; i < 65536; i++)
{
/* Delete entries until clear */
while (eval_hash[i])
{
/* Get pointer to first entry */
e_ptr = eval_hash[i];
/* Move row to next entry */
eval_hash[i] = e_ptr->next;
/* Delete entry */
free(e_ptr);
}
}
}
/*
* Set inputs of the evaluation network for the given player.
*
* We only use public knowledge in this function.
*/
static int eval_game_player(game *g, int who, int n, int max_vp,
int max_prestige, int max_build)
{
player *p_ptr;
card *c_ptr;
power *o_ptr;
int i, x, count, good[6], explore_mix = 0, num_build = 0;
/* Clear good type array */
for (i = 0; i <= GOOD_ALIEN; i++) good[i] = 0;
/* Set player pointer */
p_ptr = &g->p[who];
/* Start at first active card */
x = p_ptr->head[WHERE_ACTIVE];
/* Loop over our active cards */
for ( ; x != -1; x = g->deck[x].next)