-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.c
1512 lines (1496 loc) · 58.5 KB
/
book.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
#include <math.h>
#include "chess.h"
#include "data.h"
#if defined(UNIX)
# include <unistd.h>
#endif
/* last modified 08/07/05 */
/*
*******************************************************************************
* *
* Book() is used to determine if the current position is in the book data- *
* base. It simply takes the set of moves produced by root_moves() and then *
* tries each position's hash key to see if it can be found in the data- *
* base. If so, such a move represents a "book move." The set of flags is *
* used to decide on a sub-set of moves to be used as the "book move pool" *
* from which a move is chosen randomly. *
* *
* The format of a book position is as follows: *
* *
* 64 bits: hash key for this position. *
* *
* 8 bits: flag bits defined as follows: *
* *
* 0000 0001 ?? flagged move (0x01) *
* 0000 0010 ? flagged move (0x02) *
* 0000 0100 = flagged move (0x04) *
* 0000 1000 ! flagged move (0x08) *
* 0001 0000 !! flagged move (0x10) *
* 0010 0000 black won at least 1 game (0x20) *
* 0100 0000 at least one game was drawn (0x40) *
* 1000 0000 white won at least 1 game (0x80) *
* *
* 24 bits: number of games this move was played. *
* *
* 32 bits: learned value (floating point). *
* *
* (Note: counts are normalized to a max of 255. *
* *
*******************************************************************************
*/
#define BAD_MOVE 0x02
#define GOOD_MOVE 0x08
int Book(TREE * RESTRICT tree, int wtm, int root_list_done) {
static int book_moves[200];
static BOOK_POSITION start_moves[200];
static BITBOARD selected_key[200];
static int selected[200];
static int selected_order_played[200], selected_value[200];
static int selected_status[200], selected_percent[200],
book_development[200];
static int bs_played[200], bs_percent[200];
static int book_status[200], evaluations[200], bs_learn[200];
static float bs_value[200], total_value;
static BITBOARD book_key[200], bs_key[200];
int m1_status, forced = 0, total_percent, play_percentage = 0;
float tempr;
int done, i, j, last_move, temp, which, minlv = 999999, maxlv = -999999;
int maxp = -999999, minev = 999999, maxev = -999999;
int nflagged, im, value, np, book_ponder_move;
int cluster, scluster, test;
unsigned char buf32[4];
BITBOARD temp_hash_key, common, tempk;
int key, nmoves, num_selected, st;
int percent_played, total_played, total_moves, smoves;
int distribution;
int initial_development;
char *kibitz_p;
/*
************************************************************
* *
* If we have been out of book for several moves, return *
* and start the normal tree search. *
* *
************************************************************
*/
if (moves_out_of_book > 6)
return (0);
/*
************************************************************
* *
* Position is known, read the start book file and save *
* each move found. These will be used later to augment *
* the flags in the normal book to offer better control. *
* *
************************************************************
*/
if (!root_list_done)
RootMoveList(wtm);
test = HashKey >> 49;
smoves = 0;
if (books_file) {
fseek(books_file, test * sizeof(int), SEEK_SET);
fread(buf32, 4, 1, books_file);
key = BookIn32(buf32);
if (key > 0) {
fseek(books_file, key, SEEK_SET);
fread(buf32, 4, 1, books_file);
scluster = BookIn32(buf32);
BookClusterIn(books_file, scluster, book_buffer);
for (im = 0; im < n_root_moves; im++) {
common = HashKey & ((BITBOARD) 65535 << 48);
MakeMove(tree, 1, root_moves[im].move, wtm);
if (RepetitionCheckBook(tree, 2, wtm)) {
UnmakeMove(tree, 1, root_moves[im].move, wtm);
return (0);
}
temp_hash_key = (wtm) ? HashKey : ~HashKey;
temp_hash_key = (temp_hash_key & ~((BITBOARD) 65535 << 48)) | common;
for (i = 0; i < scluster; i++)
if (!(temp_hash_key ^ book_buffer[i].position)) {
start_moves[smoves++] = book_buffer[i];
break;
}
UnmakeMove(tree, 1, root_moves[im].move, wtm);
}
}
}
/*
************************************************************
* *
* Position is known, read in the appropriate cluster. *
* Note that this cluster will have all possible book *
* moves from current position in it (as well as others *
* of course.) *
* *
************************************************************
*/
test = HashKey >> 49;
if (book_file) {
fseek(book_file, test * sizeof(int), SEEK_SET);
fread(buf32, 4, 1, book_file);
key = BookIn32(buf32);
if (key > 0) {
book_learn_seekto = key;
fseek(book_file, key, SEEK_SET);
fread(buf32, 4, 1, book_file);
cluster = BookIn32(buf32);
BookClusterIn(book_file, cluster, book_buffer);
} else
cluster = 0;
if (!cluster && !smoves)
return (0);
/*
************************************************************
* *
* Now add any moves from books.bin to the end of the *
* cluster so that they will be played even if not in the *
* regular database of moves. *
* *
************************************************************
*/
for (i = 0; i < smoves; i++) {
for (j = 0; j < cluster; j++)
if (!(book_buffer[j].position ^ start_moves[i].position))
break;
if (j >= cluster) {
book_buffer[cluster] = start_moves[i];
book_buffer[cluster].status_played =
book_buffer[cluster].status_played & 037700000000;
cluster++;
}
}
/*
************************************************************
* *
* First cycle through the root move list, make each *
* move, and see if the resulting hash key is in the book *
* database. *
* *
************************************************************
*/
initial_development = tree->score_mg;
EvaluateDevelopment(tree, 1, wtm);
initial_development = tree->score_mg - initial_development;
total_moves = 0;
nmoves = 0;
for (im = 0; im < n_root_moves; im++) {
common = HashKey & ((BITBOARD) 65535 << 48);
MakeMove(tree, 1, root_moves[im].move, wtm);
if (RepetitionCheckBook(tree, 2, wtm)) {
UnmakeMove(tree, 1, root_moves[im].move, wtm);
return (0);
}
temp_hash_key = (wtm) ? HashKey : ~HashKey;
temp_hash_key = (temp_hash_key & ~((BITBOARD) 65535 << 48)) | common;
for (i = 0; i < cluster; i++) {
if (!(temp_hash_key ^ book_buffer[i].position)) {
book_status[nmoves] = book_buffer[i].status_played >> 24;
bs_played[nmoves] = book_buffer[i].status_played & 077777777;
bs_learn[nmoves] = (int) (book_buffer[i].learn * 100.0);
if (puzzling)
bs_played[nmoves] += 1;
tree->curmv[1] = root_moves[im].move;
if (!Captured(root_moves[im].move)) {
book_development[nmoves] = tree->score_mg;
EvaluateDevelopment(tree, 2, wtm);
book_development[nmoves] =
tree->score_mg - book_development[nmoves];
} else
book_development[nmoves] = 0;
total_moves += bs_played[nmoves];
evaluations[nmoves] = Evaluate(tree, 2, wtm, -99999, 99999);
evaluations[nmoves] -= MaterialSTM(wtm);
bs_percent[nmoves] = 0;
for (j = 0; j < smoves; j++) {
if (!(book_buffer[i].position ^ start_moves[j].position)) {
book_status[nmoves] |= start_moves[j].status_played >> 24;
bs_percent[nmoves] = start_moves[j].status_played & 077777777;
break;
}
}
book_moves[nmoves] = root_moves[im].move;
book_key[nmoves] = temp_hash_key;
nmoves++;
break;
}
}
UnmakeMove(tree, 1, root_moves[im].move, wtm);
}
if (!nmoves)
return (0);
book_learn_nmoves = nmoves;
/*
************************************************************
* *
* If any moves have a very bad or a very good learn *
* value, set the appropriate ? or ! flag so the move *
* be played or avoided as appropriate. *
* *
************************************************************
*/
for (i = 0; i < nmoves; i++)
if (!(book_status[i] & BAD_MOVE))
maxp = Max(maxp, bs_played[i]);
for (i = 0; i < nmoves; i++) {
if (bs_learn[i] <= LEARN_COUNTER_BAD && !bs_percent[i] &&
!(book_status[i] & 0x18))
book_status[i] |= BAD_MOVE;
if (wtm && !(book_status[i] & 0x80) && !bs_percent[i] &&
!(book_status[i] & 0x18))
book_status[i] |= BAD_MOVE;
if (!wtm && !(book_status[i] & 0x20) && !bs_percent[i] &&
!(book_status[i] & 0x18))
book_status[i] |= BAD_MOVE;
if (bs_played[i] < maxp / 10 && !bs_percent[i] && book_random &&
!(book_status[i] & 0x18))
book_status[i] |= BAD_MOVE;
if (bs_learn[i] >= LEARN_COUNTER_GOOD && !(book_status[i] & 0x03))
book_status[i] |= GOOD_MOVE;
if (bs_percent[i])
book_status[i] |= GOOD_MOVE;
}
/*
************************************************************
* *
* We have the book moves, now it's time to decide how *
* they are supposed to be sorted and compute the sort *
* index. *
* *
************************************************************
*/
for (i = 0; i < nmoves; i++) {
if (!(book_status[i] & BAD_MOVE)) {
minlv = Min(minlv, bs_learn[i]);
maxlv = Max(maxlv, bs_learn[i]);
minev = Min(minev, evaluations[i]);
maxev = Max(maxev, evaluations[i]);
maxp = Max(maxp, bs_played[i]);
}
}
maxp++;
for (i = 0; i < nmoves; i++) {
bs_value[i] = 1;
bs_value[i] += bs_played[i] / (float) maxp *1000.0 * book_weight_freq;
if (minlv < maxlv)
bs_value[i] +=
(bs_learn[i] - minlv) / (float) (maxlv -
minlv) * 1000.0 * book_weight_learn;
if (minev < maxev)
bs_value[i] +=
(evaluations[i] - minev) / (float) (Max(maxev - minev,
50)) * 1000.0 * book_weight_eval;
}
total_played = total_moves;
/*
************************************************************
* *
* If there are any ! moves, make their popularity count *
* huge since they have to be considered. *
* *
************************************************************
*/
for (i = 0; i < nmoves; i++)
if (book_status[i] & 0x18)
break;
if (i < nmoves) {
for (i = 0; i < nmoves; i++) {
if (book_status[i] & 0x18)
bs_value[i] += 8000.0;
if (!(book_status[i] & 0x03))
bs_value[i] += 4000.0;
}
}
/*
************************************************************
* *
* Now sort the moves based on the complete sort value. *
* *
************************************************************
*/
if (nmoves)
do {
done = 1;
for (i = 0; i < nmoves - 1; i++) {
if (bs_percent[i] < bs_percent[i + 1] ||
(bs_percent[i] == bs_percent[i + 1]
&& bs_value[i] < bs_value[i + 1])) {
tempr = bs_played[i];
bs_played[i] = bs_played[i + 1];
bs_played[i + 1] = tempr;
tempr = bs_value[i];
bs_value[i] = bs_value[i + 1];
bs_value[i + 1] = tempr;
temp = evaluations[i];
evaluations[i] = evaluations[i + 1];
evaluations[i + 1] = temp;
temp = bs_learn[i];
bs_learn[i] = bs_learn[i + 1];
bs_learn[i + 1] = temp;
temp = book_development[i];
book_development[i] = book_development[i + 1];
book_development[i + 1] = temp;
temp = book_moves[i];
book_moves[i] = book_moves[i + 1];
book_moves[i + 1] = temp;
temp = book_status[i];
book_status[i] = book_status[i + 1];
book_status[i + 1] = temp;
temp = bs_percent[i];
bs_percent[i] = bs_percent[i + 1];
bs_percent[i + 1] = temp;
tempk = book_key[i];
book_key[i] = book_key[i + 1];
book_key[i + 1] = tempk;
done = 0;
}
}
} while (!done);
/*
************************************************************
* *
* Display the book moves, and total counts, etc. if the *
* operator has requested it. *
* *
************************************************************
*/
if (show_book) {
Print(128, " after screening, the following moves can be played\n");
Print(128,
" move played %% score learn " "sortv P%% P\n");
for (i = 0; i < nmoves; i++) {
Print(128, "%6s", OutputMove(tree, book_moves[i], 1, wtm));
st = book_status[i];
if (st & 0x1f) {
if (st & 0x01)
Print(128, "??");
else if (st & 0x02)
Print(128, "? ");
else if (st & 0x04)
Print(128, "= ");
else if (st & 0x08)
Print(128, "! ");
else if (st & 0x10)
Print(128, "!!");
} else
Print(128, " ");
Print(128, " %6d", bs_played[i]);
Print(128, " %3d", 100 * bs_played[i] / Max(total_moves, 1));
Print(128, "%s", DisplayEvaluation(evaluations[i], wtm));
Print(128, "%9.2f", (float) bs_learn[i] / 100.0);
Print(128, " %9.1f", bs_value[i]);
Print(128, " %3d", bs_percent[i]);
if ((book_status[i] & book_accept_mask &&
!(book_status[i] & book_reject_mask)) ||
(!(book_status[i] & book_reject_mask) && (bs_percent[i] ||
book_status[i] & 0x18 || (wtm && book_status[i] & 0x80) ||
(!wtm && book_status[i] & 0x20))))
Print(128, " Y");
else
Print(128, " N");
Print(128, "\n");
}
}
/*
************************************************************
* *
* Check for book moves with the play % value set. if *
* there are any such moves, then exclude all moves that *
* do not have a play % or a !/!! flag set. *
* *
************************************************************
*/
for (i = 0; i < nmoves; i++)
if (bs_percent[i])
play_percentage = 1;
/*
************************************************************
* *
* Delete ? and ?? moves first, which includes those *
* moves with bad learned results. Here is where we also *
* exclude moves with no play % if we find at least one *
* with a non-zero value. *
* *
************************************************************
*/
num_selected = 0;
if (!play_percentage) {
for (i = 0; i < nmoves; i++)
if (!(book_status[i] & 0x03) || bs_percent[i]) {
selected_status[num_selected] = book_status[i];
selected_order_played[num_selected] = bs_played[i];
selected_value[num_selected] = bs_value[i];
selected_percent[num_selected] = bs_percent[i];
selected_key[num_selected] = book_key[i];
selected[num_selected++] = book_moves[i];
}
} else {
for (i = 0; i < nmoves; i++)
if (bs_percent[i]) {
selected_status[num_selected] = book_status[i];
selected_order_played[num_selected] = bs_played[i];
selected_value[num_selected] = bs_value[i];
selected_percent[num_selected] = bs_percent[i];
selected_key[num_selected] = book_key[i];
selected[num_selected++] = book_moves[i];
}
}
for (i = 0; i < num_selected; i++) {
book_status[i] = selected_status[i];
bs_played[i] = selected_order_played[i];
bs_value[i] = selected_value[i];
bs_percent[i] = selected_percent[i];
book_moves[i] = selected[i];
}
nmoves = num_selected;
/*
************************************************************
* *
* If this is a real search (not a puzzling search to *
* find a move by the opponent to ponder) then we need to *
* set up the whisper info for later. *
* *
************************************************************
*/
if (!puzzling)
do {
kibitz_text[0] = '\0';
if (!nmoves)
break;
sprintf(kibitz_text, "book moves (");
kibitz_p = kibitz_text + strlen(kibitz_text);
for (i = 0; i < nmoves; i++) {
sprintf(kibitz_p, "%s %d%%", OutputMove(tree, book_moves[i], 1,
wtm), 100 * bs_played[i] / Max(total_played, 1));
kibitz_p = kibitz_text + strlen(kibitz_text);
if (i < nmoves - 1) {
sprintf(kibitz_p, ", ");
kibitz_p = kibitz_text + strlen(kibitz_text);
}
}
sprintf(kibitz_p, ")\n");
} while (0);
/*
************************************************************
* *
* Now select a move from the set of moves just found. Do *
* this in three distinct passes: (1) look for !! moves; *
* (2) look for ! moves; (3) look for any other moves. *
* Note: book_accept_mask *should* have a bit set for any *
* move that is selected, including !! and ! type moves *
* so that they *can* be excluded if desired. Note also *
* that book_reject_mask should have ?? and ? set (at a *
* minimum) to exclude these types of moves. *
* *
************************************************************
*/
num_selected = 0;
if (!num_selected && !puzzling)
if (book_accept_mask & 16)
for (i = 0; i < nmoves; i++)
if (book_status[i] & 16) {
forced = 1;
selected_status[num_selected] = book_status[i];
selected_order_played[num_selected] = bs_played[i];
selected_value[num_selected] = bs_value[i];
selected_key[num_selected] = book_key[i];
selected[num_selected++] = book_moves[i];
}
if (!num_selected && !puzzling)
if (book_accept_mask & 8)
for (i = 0; i < nmoves; i++)
if (book_status[i] & 8) {
forced = 1;
selected_status[num_selected] = book_status[i];
selected_order_played[num_selected] = bs_played[i];
selected_value[num_selected] = bs_value[i];
selected_key[num_selected] = book_key[i];
selected[num_selected++] = book_moves[i];
}
if (!num_selected && !puzzling)
if (book_accept_mask & 4)
for (i = 0; i < nmoves; i++)
if (book_status[i] & 4) {
selected_status[num_selected] = book_status[i];
selected_order_played[num_selected] = bs_played[i];
selected_value[num_selected] = bs_value[i];
selected_key[num_selected] = book_key[i];
selected[num_selected++] = book_moves[i];
}
if (!num_selected && !puzzling)
for (i = 0; i < nmoves; i++)
if (book_status[i] & book_accept_mask) {
selected_status[num_selected] = book_status[i];
selected_order_played[num_selected] = bs_played[i];
selected_value[num_selected] = bs_value[i];
selected_key[num_selected] = book_key[i];
selected[num_selected++] = book_moves[i];
}
if (!num_selected)
for (i = 0; i < nmoves; i++) {
selected_status[num_selected] = book_status[i];
selected_order_played[num_selected] = bs_played[i];
selected_value[num_selected] = bs_value[i];
selected_key[num_selected] = book_key[i];
selected[num_selected++] = book_moves[i];
}
if (!num_selected)
return (0);
for (i = 0; i < num_selected; i++) {
book_status[i] = selected_status[i];
book_moves[i] = selected[i];
bs_played[i] = selected_order_played[i];
bs_value[i] = selected_value[i];
bs_key[i] = selected_key[i];
}
nmoves = num_selected;
if (nmoves == 0)
return (0);
Print(128, " book moves {");
for (i = 0; i < nmoves; i++) {
Print(128, "%s", OutputMove(tree, book_moves[i], 1, wtm));
if (i < nmoves - 1)
Print(128, ", ");
}
Print(128, "}\n");
nflagged = 0;
for (i = 0; i < nmoves; i++)
if (book_status[i] & 8)
nflagged++;
nmoves = Max(Min(nmoves, book_selection_width), nflagged);
if (show_book) {
Print(128, " moves considered {");
for (i = 0; i < nmoves; i++) {
Print(128, "%s", OutputMove(tree, book_moves[i], 1, wtm));
if (i < nmoves - 1)
Print(128, ", ");
}
Print(128, "}\n");
}
/*
************************************************************
* *
* We have the book moves, if any have specified percents *
* for play, then adjust the bs_value[] to reflect this *
* percentage. *
* *
************************************************************
*/
total_value = 0.0;
total_percent = 0;
for (i = 0; i < nmoves; i++) {
if (!bs_percent[i])
total_value += bs_value[i];
total_percent += bs_percent[i];
}
if (fabs(total_value) < 0.0001)
total_value = 1000.0;
total_percent = (total_percent > 99) ? 99 : total_percent;
for (i = 0; i < nmoves; i++)
if (bs_percent[i])
bs_value[i] =
total_value / (1.0 -
(float) total_percent / 100.0) * (float) bs_percent[i] / 100.0;
/*
************************************************************
* *
* Display the book moves, and total counts, etc. if the *
* operator has requested it. *
* *
************************************************************
*/
if (show_book) {
Print(128, " move played %% score sortv P%% P\n");
for (i = 0; i < nmoves; i++) {
Print(128, "%6s", OutputMove(tree, book_moves[i], 1, wtm));
st = book_status[i];
if (st & 0x1f) {
if (st & 0x01)
Print(128, "??");
else if (st & 0x02)
Print(128, "? ");
else if (st & 0x04)
Print(128, "= ");
else if (st & 0x08)
Print(128, "! ");
else if (st & 0x10)
Print(128, "!!");
} else
Print(128, " ");
Print(128, " %6d", bs_played[i]);
Print(128, " %3d", 100 * bs_played[i] / Max(total_moves, 1));
Print(128, "%s", DisplayEvaluation(evaluations[i], wtm));
Print(128, " %9.1f", bs_value[i]);
Print(128, " %3d", bs_percent[i]);
if ((book_status[i] & book_accept_mask &&
!(book_status[i] & book_reject_mask)) ||
(!(book_status[i] & book_reject_mask) && ((wtm &&
book_status[i] & 0x80) || (!wtm &&
book_status[i] & 0x20))))
Print(128, " Y");
else
Print(128, " N");
Print(128, "\n");
}
}
/*
************************************************************
* *
* If random=0, then we search the set of legal book *
* moves with the normal search engine (but with a short *
* time limit) to choose among them. *
* *
************************************************************
*/
if (nmoves && (!puzzling || mode != tournament_mode)) {
np = bs_played[nmoves - 1];
if (!puzzling && (!book_random || (mode == tournament_mode &&
np < book_search_trigger))) {
if (!forced) {
n_root_moves = nmoves;
for (i = 0; i < n_root_moves; i++) {
root_moves[i].move = book_moves[i];
root_moves[i].nodes = 0;
root_moves[i].status = 0;
}
last_pv.pathd = 0;
booking = 1;
value = Iterate(wtm, booking, 1);
booking = 0;
abort_search = 0;
if (value < -50) {
last_pv.pathd = 0;
return (0);
}
} else {
tree->pv[1].path[1] = book_moves[0];
tree->pv[1].pathl = 1;
tree->pv[1].pathd = 0;
}
return (1);
}
}
/*
************************************************************
* *
* If puzzling, in tournament mode we try to find the *
* best non-book move, because a book move will produce *
* a quick move anyway. We therefore would rather search *
* for a non-book move, just in case the opponent goes *
* out of book here. *
* *
************************************************************
*/
else if (mode == tournament_mode && puzzling) {
RootMoveList(wtm);
for (i = 0; i < n_root_moves; i++)
for (j = 0; j < nmoves; j++)
if (root_moves[i].move == book_moves[j])
root_moves[i].move = 0;
for (i = 0, j = 0; i < n_root_moves; i++)
if (root_moves[i].move != 0)
root_moves[j++] = root_moves[i];
n_root_moves = j;
Print(128, " moves considered {only non-book moves}\n");
nmoves = j;
if (nmoves > 1) {
last_pv.pathd = 0;
booking = 1;
(void) Iterate(wtm, booking, 1);
booking = 0;
} else {
tree->pv[1].path[1] = book_moves[0];
tree->pv[1].pathl = 1;
tree->pv[1].pathd = 0;
}
return (1);
}
last_move = nmoves;
/*
************************************************************
* *
* Compute a random value and use this to generate a *
* book move based on a probability distribution of *
* the number of games won by each book move. *
* *
************************************************************
*/
which = Random32();
j = ReadClock() / 100 % 13;
for (i = 0; i < j; i++)
which = Random32();
total_moves = 0;
for (i = 0; i < last_move; i++) {
if (bs_percent[0])
total_moves += bs_value[i];
else
total_moves += bs_value[i] * bs_value[i];
}
distribution = abs(which) % Max(total_moves, 1);
for (which = 0; which < last_move; which++) {
if (bs_percent[0])
distribution -= bs_value[which];
else
distribution -= bs_value[which] * bs_value[which];
if (distribution < 0)
break;
}
which = Min(which, last_move - 1);
tree->pv[1].path[1] = book_moves[which];
percent_played = 100 * bs_played[which] / Max(total_played, 1);
total_played = bs_played[which];
m1_status = book_status[which];
tree->pv[1].pathl = 1;
tree->pv[1].pathd = 0;
if (mode != tournament_mode) {
MakeMove(tree, 1, book_moves[which], wtm);
if ((book_ponder_move = BookPonderMove(tree, Flip(wtm)))) {
tree->pv[1].path[2] = book_ponder_move;
tree->pv[1].pathl = 2;
}
UnmakeMove(tree, 1, book_moves[which], wtm);
}
book_learn_key = bs_key[which];
Print(128, " book 0.0s %3d%% ", percent_played);
Print(128, " %s", OutputMove(tree, tree->pv[1].path[1], 1, wtm));
st = m1_status & book_accept_mask & (~224);
if (st) {
if (st & 1)
Print(128, "??");
else if (st & 2)
Print(128, "?");
else if (st & 4)
Print(128, "=");
else if (st & 8)
Print(128, "!");
else if (st & 16)
Print(128, "!!");
}
MakeMove(tree, 1, tree->pv[1].path[1], wtm);
if (tree->pv[1].pathl > 1)
Print(128, " %s", OutputMove(tree, tree->pv[1].path[2], 2, Flip(wtm)));
UnmakeMove(tree, 1, tree->pv[1].path[1], wtm);
Print(128, "\n");
return (1);
}
return (0);
}
/* last modified 08/07/05 */
/*
*******************************************************************************
* *
* BookPonderMove() is used to find a move to ponder, to avoid the overhead *
* of a "puzzling" search early in the game (unless there are no book moves *
* found, of course.) The algorithm is much simpler than the normal book *
* move code... just find the move with the largest frequency counter and *
* assume that will be played. *
* *
*******************************************************************************
*/
int BookPonderMove(TREE * RESTRICT tree, int wtm) {
BITBOARD temp_hash_key, common;
static int book_moves[200];
int i, key, *lastm, cluster, n_moves, im, played, tplayed;
int book_ponder_move = 0, test;
unsigned char buf32[4];
/*
************************************************************
* *
* position is known, read in the appropriate cluster. *
* note that this cluster will have all possible book *
* moves from current position in it (as well as others *
* of course.) *
* *
************************************************************
*/
if (book_file) {
test = HashKey >> 49;
fseek(book_file, test * sizeof(int), SEEK_SET);
fread(buf32, 4, 1, book_file);
key = BookIn32(buf32);
if (key > 0) {
fseek(book_file, key, SEEK_SET);
fread(buf32, 4, 1, book_file);
cluster = BookIn32(buf32);
BookClusterIn(book_file, cluster, book_buffer);
} else
cluster = 0;
if (!cluster)
return (0);
lastm = GenerateCaptures(tree, 2, wtm, book_moves);
lastm = GenerateNoncaptures(tree, 2, wtm, lastm);
n_moves = lastm - book_moves;
/*
************************************************************
* *
* First cycle through the root move list, make each *
* move, and see if the resulting hash key is in the book *
* database. *
* *
************************************************************
*/
played = -1;
for (im = 0; im < n_moves; im++) {
common = HashKey & ((BITBOARD) 65535 << 48);
MakeMove(tree, 2, book_moves[im], wtm);
temp_hash_key = (wtm) ? HashKey : ~HashKey;
temp_hash_key = (temp_hash_key & ~((BITBOARD) 65535 << 48)) | common;
for (i = 0; i < cluster; i++) {
if (!(temp_hash_key ^ book_buffer[i].position)) {
tplayed = book_buffer[i].status_played & 077777777;
if (tplayed > played) {
played = tplayed;
book_ponder_move = book_moves[im];
}
break;
}
}
UnmakeMove(tree, 2, book_moves[im], wtm);
}
}
return (book_ponder_move);
}
/* last modified 08/07/05 */
/*
*******************************************************************************
* *
* BookUp() is used to create/add to the opening book file. typing "<file> *
* create" will erase the old book file and start from scratch, *
* *
* The format of the input data is a left bracket ("[") followed by any title*
* information desired, followed by a right bracket ("]") followed by a *
* sequence of moves. The sequence of moves is assumed to start at ply=1, *
* with white-to-move (normal opening position) and can contain as many moves*
* as desired (no limit on the depth of each variation.) The file *must* be *
* terminated with a line that begins with "end", since handling the EOF *
* condition makes portable code difficult. *
* *
* Book moves can either be typed in by hand, directly into book_add(), by *
* using the "book create/add" command. Using the command "book add/create *
* filename" will cause book_add() to read its opening text moves from *
* filename rather than from the keyboard *
* *
* In addition to the normal text for a move (reduced or full algebraic is *
* accepted, ie, e4, ed, exd4, e3d4, etc. are all acceptable) some special *
* characters can be appended to a move. *
* *
* ?? -> Never play this move. since the same book is used for both *
* black and white, you can enter moves in that white might play, *
* but prevent the program from choosing them on its own. *
* ? -> Avoid this move except for non-important games. These openings *
* are historically those that the program doesn't play very well, *
* but which aren't outright losing. *
* = -> Drawish move, only play this move if drawish moves are allowed *
* by the operator. This is used to encourage the program to play *
* drawish openings (Petrov's comes to mind) when the program needs*
* to draw or is facing a formidable opponent (deep thought comes *
* to mind.) *
* ! -> Always play this move, if there isn't a move with the !! flag *
* set also. This is a strong move, but not as strong as a !! *
* move. *
* !! -> Always play this move. This can be used to make the program *
* favor particular lines, or to mark a strong move for certain *
* opening traps. *
* *
* {Play nn%} is used to force this specific book move to be played a specific*
* percentage of the time, and override the frequency of play that *
* comes from the large pgn database. *
* *
*******************************************************************************
*/
void BookUp(TREE * RESTRICT tree, int nargs, char **args) {
BB_POSITION *bbuffer;
BITBOARD temp_hash_key, common;
FILE *book_input;
char fname[128], start, *ch, output_filename[128];
static char schar[2] = { "." };
int result = 0, played, i, mask_word, total_moves;
int move, move_num, wtm, book_positions, major, minor;
int cluster, max_cluster, discarded = 0, discarded_mp = 0, discarded_lose =
0;
int errors, data_read;
int start_elapsed_time, ply, max_ply = 256;
int stat, files = 0, buffered = 0, min_played = 0, games_parsed = 0;
int wins, losses;
BOOK_POSITION current, next;
BB_POSITION temp;
int last, cluster_seek, next_cluster;
int counter, *index, max_search_depth;
double wl_percent = 0.0;
/*
************************************************************
* *
* Open the correct book file for writing/reading *
* *
************************************************************
*/
#if defined(POSITIONS)
FILE *pout = fopen("positions", "w");
#endif
if (!strcmp(args[1], "create")) {
if (nargs < 4) {
Print(4095, "usage: <binfile> create <pgn-filename> ");
Print(4095, "maxply [minplay] [win/lose %]\n");
return;
}
max_ply = atoi(args[3]);
if (nargs >= 5) {
min_played = atoi(args[4]);
}
if (nargs > 5) {
wl_percent = atof(args[5]) / 100.0;
}
strcpy(output_filename, args[0]);
if (!strstr(output_filename, ".bin")) {
strcat(output_filename, ".bin");
}
} else if (!strcmp(args[1], "off")) {
if (book_file)
fclose(book_file);
if (books_file)
fclose(normal_bs_file);
if (computer_bs_file)
fclose(computer_bs_file);
book_file = 0;
books_file = 0;
computer_bs_file = 0;
normal_bs_file = 0;
Print(4095, "book file disabled.\n");
return;
} else if (!strcmp(args[1], "on")) {
if (!book_file) {
sprintf(fname, "%s/book.bin", book_path);
book_file = fopen(fname, "rb+");
sprintf(fname, "%s/books.bin", book_path);
books_file = fopen(fname, "rb+");
Print(4095, "book file enabled.\n");
}
return;
} else if (!strcmp(args[1], "mask")) {
if (nargs < 4) {
Print(4095, "usage: book mask accept|reject value\n");
return;
} else if (!strcmp(args[2], "accept")) {
book_accept_mask = BookMask(args[3]);
book_reject_mask = book_reject_mask & ~book_accept_mask;
return;
} else if (!strcmp(args[2], "reject")) {
book_reject_mask = BookMask(args[3]);
book_accept_mask = book_accept_mask & ~book_reject_mask;
return;
}
} else if (!strcmp(args[1], "random")) {
if (nargs < 3) {
Print(4095, "usage: book random <n>\n");
return;
}
book_random = atoi(args[2]);
switch (book_random) {
case 0:
Print(4095, "play best book line after search.\n");
break;
case 1:
Print(4095, "choose from book moves randomly (using weights.)\n");
break;