Book: replace Write* macros with functions.
[gnushogi.git] / gnushogi / book.c
1 /*
2  * FILE: book.c
3  *
4  * ----------------------------------------------------------------------
5  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7  * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
8  *
9  * GNU SHOGI is based on GNU CHESS
10  *
11  * Copyright (c) 1988, 1989, 1990 John Stanback
12  * Copyright (c) 1992 Free Software Foundation
13  *
14  * This file is part of GNU SHOGI.
15  *
16  * GNU Shogi is free software; you can redistribute it and/or modify it
17  * under the terms of the GNU General Public License as published by the
18  * Free Software Foundation; either version 3 of the License,
19  * or (at your option) any later version.
20  *
21  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with GNU Shogi; see the file COPYING. If not, see
28  * <http://www.gnu.org/licenses/>.
29  * ----------------------------------------------------------------------
30  *
31  */
32
33 #include "gnushogi.h"
34
35 #define O_BINARY 0
36
37 #if HAVE_UNISTD_H
38 /* Declarations of read(), write(), close(), and lseek(). */
39 #include <unistd.h>
40 #endif
41
42 #if HAVE_FCNTL_H
43 #include <fcntl.h>
44 #endif
45
46 #include "book.h"
47
48 unsigned booksize = BOOKSIZE;
49 unsigned short bookmaxply = BOOKMAXPLY;
50 unsigned bookcount = 0;
51
52 #ifdef BOOK
53 char *bookfile = BOOK;
54 #else
55 char *bookfile = NULL;
56 #endif
57
58 #ifdef BINBOOK
59 char *binbookfile = BINBOOK;
60 #else
61 char *binbookfile = NULL;
62 #endif
63
64 static char bmvstr[3][7];
65
66 static ULONG bhashbd;
67 static ULONG bhashkey;
68
69
70 /*
71  * Balgbr(f, t, flag)
72  *
73  * Generate move strings in different formats.
74  */
75
76 static void
77 Balgbr(short f, short t, short flag)
78 {
79     short promoted = false;
80
81     if ((f & 0x80) != 0)
82     {
83         f &= 0x7f;
84         promoted = true;
85     }
86
87     if (f > NO_SQUARES)
88     {
89         short piece;
90         piece = f - NO_SQUARES;
91
92         if (f > (NO_SQUARES + NO_PIECES))
93             piece -= NO_PIECES;
94
95         flag = (dropmask | piece);
96     }
97
98     if ((t & 0x80) != 0)
99     {
100         flag |= promote;
101         t &= 0x7f;
102     }
103
104     if ((f == t) && ((f != 0) || (t != 0)))
105     {
106         /*
107          * error in algbr: FROM=TO=t
108          */
109
110         bmvstr[0][0] = bmvstr[1][0] = bmvstr[2][0] = '\0';
111     }
112     else
113     {
114         if ((flag & dropmask) != 0)
115         {
116             /* bmvstr[0]: P*3c bmvstr[1]: P'3c */
117             short piece = flag & pmask;
118             bmvstr[0][0] = pxx[piece];
119             bmvstr[0][1] = '*';
120             bmvstr[0][2] = COL_NAME(column(t));
121             bmvstr[0][3] = ROW_NAME(row(t));
122             bmvstr[0][4] = bmvstr[2][0] = '\0';
123             strcpy(bmvstr[1], bmvstr[0]);
124             bmvstr[1][1] = '\'';
125         }
126         else
127         {
128             if ((f != 0) || (t != 0))
129             {
130                 /* algebraic notation */
131                 /* bmvstr[0]: 7g7f bmvstr[1]:
132                  * (+)P7g7f(+) bmvstr[2]: (+)P7f(+) */
133                 bmvstr[0][0] = COL_NAME(column(f));
134                 bmvstr[0][1] = ROW_NAME(row(f));
135                 bmvstr[0][2] = COL_NAME(column(t));
136                 bmvstr[0][3] = ROW_NAME(row(t));
137                 bmvstr[0][4] = '\0';
138
139                 if (promoted)
140                 {
141                     bmvstr[1][0] = bmvstr[2][0] = '+';
142                     bmvstr[1][1] = bmvstr[2][1] = pxx[board[f]];
143                     strcpy(&bmvstr[1][2], &bmvstr[0][0]);
144                     strcpy(&bmvstr[2][2], &bmvstr[0][2]);
145                 }
146                 else
147                 {
148                     bmvstr[1][0] = bmvstr[2][0] = pxx[board[f]];
149                     strcpy(&bmvstr[1][1], &bmvstr[0][0]);
150                     strcpy(&bmvstr[2][1], &bmvstr[0][2]);
151                 }
152
153                 if (flag & promote)
154                 {
155                     strcat(bmvstr[0], "+");
156                     strcat(bmvstr[1], "+");
157                     strcat(bmvstr[2], "+");
158                 }
159             }
160             else
161             {
162                 bmvstr[0][0] = bmvstr[1][0] = bmvstr[2][0] = '\0';
163             }
164         }
165     }
166 }
167
168
169 #ifndef QUIETBOOKGEN
170 static void
171 bkdisplay(char *s, int cnt, int moveno)
172 {
173     static short pnt;
174     struct leaf  *node;
175     int r, c, l;
176
177     pnt = TrPnt[2];
178     printf("matches = %d\n", cnt);
179     printf("inout move is :%s: move number %d side %s\n",
180             s, moveno / 2 + 1, (moveno & 1) ? "white" : "black");
181
182 #ifndef SEMIQUIETBOOKGEN
183     printf("legal moves are \n");
184
185     while (pnt < TrPnt[3])
186     {
187         node = &Tree[pnt++];
188
189         if (is_promoted[board[node->f]] )
190             Balgbr(node->f | 0x80, node->t, (short) node->flags);
191         else
192             Balgbr(node->f, node->t, (short) node->flags);
193
194         printf("%s %s %s\n",
195                bmvstr[0], bmvstr[1], bmvstr[2]);
196     }
197
198     printf("\n current board is\n");
199
200     for (r = (NO_ROWS - 1); r >= 0; r--)
201     {
202         for (c = 0; c <= (NO_COLS - 1); c++)
203         {
204             char pc;
205
206             l = locn(r, c);
207             pc = (is_promoted[board[l]] ? '+' : ' ');
208
209             if (color[l] == neutral)
210                 printf(" -");
211             else if (color[l] == black)
212                 printf("%c%c", pc, qxx[board[l]]);
213             else
214                 printf("%c%c", pc, pxx[board[l]]);
215         }
216
217         printf("\n");
218     }
219
220     printf("\n");
221     {
222         short color;
223
224         for (color = black; color <= white; color++)
225         {
226             short piece, c;
227
228             printf((color == black) ? "black " : "white ");
229
230             for (piece = pawn; piece <= king; piece++)
231             {
232                 if ((c = Captured[color][piece]))
233                     printf("%i%c ", c, pxx[piece]);
234             }
235
236             printf("\n");
237         };
238     }
239 #endif /* SEMIQUIETBOOKGEN */
240 }
241 #endif /* QUIETBOOKGEN */
242
243
244 /*
245  * BVerifyMove(s, mv, moveno)
246  *
247  * Compare the string 's' to the list of legal moves available for the
248  * opponent. If a match is found, make the move on the board.
249  */
250
251 static int
252 BVerifyMove(char *s, unsigned short *mv, int moveno)
253 {
254     static short pnt, tempb, tempc, tempsf, tempst, cnt;
255     static struct leaf xnode;
256     struct leaf  *node;
257
258     *mv = 0;
259     cnt = 0;
260     MoveList(opponent, 2, -2, true);
261     pnt = TrPnt[2];
262
263     while (pnt < TrPnt[3])
264     {
265         node = &Tree[pnt++];
266
267         if (is_promoted[board[node->f]] )
268             Balgbr(node->f | 0x80, node->t, (short) node->flags);
269         else
270             Balgbr(node->f, node->t, (short) node->flags);
271
272         if (strcmp(s, bmvstr[0]) == 0 || strcmp(s, bmvstr[1]) == 0 ||
273             strcmp(s, bmvstr[2]) == 0)
274         {
275             cnt++;
276             xnode = *node;
277         }
278     }
279
280     if (cnt == 1)
281     {
282         short blockable;
283
284         MakeMove(opponent, &xnode, &tempb,
285                  &tempc, &tempsf, &tempst, &INCscore);
286
287         if (SqAttacked(PieceList[opponent][0], computer, &blockable))
288         {
289             UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
290             /* Illegal move in check */
291 #if !defined QUIETBOOKGEN
292             puts("Illegal move (in check): %s");
293             bkdisplay(s, cnt, moveno);
294 #endif
295             return false;
296         }
297         else
298         {
299             *mv = (xnode.f << 8) | xnode.t;
300
301             if (is_promoted[board[xnode.t]] )
302                 Balgbr(xnode.f | 0x80, xnode.t, false);
303             else
304                 Balgbr(xnode.f, xnode.t, false);
305
306             return true;
307         }
308     }
309
310     /* Illegal move */
311 #if !defined QUIETBOOKGEN
312     printf("Illegal move (no match): %s\n", s);
313     bkdisplay(s, cnt, moveno);
314 #endif
315     return false;
316 }
317
318
319 /*
320  * RESET()
321  *
322  * Reset the board and other variables to start a new game.
323  *
324  */
325
326 static void
327 RESET(void)
328 {
329     short l;
330
331     flag.illegal = flag.mate = flag.quit
332         = flag.reverse = flag.bothsides = flag.onemove = flag.force
333         = false;
334
335     flag.post &= xboard; /* [HGM] xboard: do not clear in XBoard mode */
336
337     flag.material = flag.coords = flag.hash = flag.easy
338         = flag.beep = flag.rcptr
339         = true;
340
341     flag.stars = flag.shade = flag.back = flag.musttimeout = false;
342     flag.gamein = false;
343     GenCnt   = 0;
344     GameCnt  = 0;
345     CptrFlag[0] = TesujiFlag[0] = false;
346     opponent = black;
347     computer = white;
348
349     for (l = 0; l < NO_SQUARES; l++)
350     {
351         board[l]   = Stboard[l];
352         color[l]   = Stcolor[l];
353         Mvboard[l] = 0;
354     }
355
356     ClearCaptured();
357     InitializeStats();
358     hashbd = hashkey = 0;
359 }
360
361
362 static int
363 Vparse (FILE * fd, USHORT *mv, USHORT *flags, int moveno)
364 {
365     int c, i;
366     char s[255];
367
368     *flags = 0;
369
370     while (true)
371     {
372         while (((c = getc(fd)) == ' ')
373                || (c == '!') || (c == '/') || (c == '\n'));
374
375         if (c == '(')
376         {
377             /* amount of time spent for the last move */
378             while (((c = getc(fd)) != ')') && (c != EOF));
379
380             if (c == ')')
381             {
382                 while (((c = getc(fd)) == ' ') || (c == '\n'));
383             }
384         }
385
386         if (c == '[')
387         {
388             /* comment for the actual game */
389             while (((c = getc(fd))) != ']' && (c != EOF));
390
391             if (c == ']')
392             {
393                 while (((c = getc(fd))) == ' ' || (c == '\n'));
394             }
395         }
396
397         if (c == '\r')
398             continue;
399
400         if (c == '#')
401         {
402             /* comment */
403             do
404             {
405                 c = getc(fd);
406
407                 if (c == '\r')
408                     continue;
409                 /* goes to end of line */
410
411                 if (c == '\n')
412                     return 0;
413
414                 if (c == EOF)
415                     return -1;
416             }
417             while (true);
418         }
419
420         s[i = 0] = (char) c;
421
422         while ((c >= '0') && (c <= '9'))
423         {
424             c = getc(fd);
425             s[++i] = (char) c;
426         }
427
428         if (c == '.')
429         {
430             while (((c = getc(fd)) == ' ') || (c == '.') || (c == '\n'));
431             s[i = 0] = (char) c;
432         }
433
434         while (((c = getc(fd)) != '?') && (c != '!') && (c != ' ')
435                && (c != '(') && (c != '\n') && (c != '\t') && (c != EOF))
436         {
437             if (c == '\r')
438                 continue;
439
440             if ((c != 'x') && (c != '-') && (c != ',')
441                 && (c != ';') && (c != '='))
442             {
443                 s[++i] = (char) c;
444             }
445         }
446
447         s[++i] = '\0';
448
449         if (c == '(')
450         {
451             while (((c = getc(fd)) != ')') && (c != EOF));
452
453             if (c == ')')
454                 c = getc(fd);
455         }
456
457         if (c == EOF)
458             return -1;
459
460         if (s[0] == '#')
461         {
462             while ((c != '\n') && (c != EOF))
463                 c = getc(fd);
464
465             if (c == EOF)
466                 return -1;
467             else
468                 return 0;
469         }
470
471         if (strcmp(s, "draw") == 0)
472             continue;
473         else if (strcmp(s, "1-0") == 0)
474             continue;
475         else if (strcmp(s, "0-1") == 0)
476             continue;
477         else if (strcmp(s, "Resigns") == 0)
478             continue;
479         else if (strcmp(s, "Resigns.") == 0)
480             continue;
481         else if (strcmp(s, "Sennichite") == 0)
482             continue;
483         else if (strcmp(s, "Sennichite.") == 0)
484             continue;
485         else if (strcmp(s, "Jishogi") == 0)
486             continue;
487         else if (strcmp(s, "Jishogi.") == 0)
488             continue;
489
490         bhashkey = hashkey;
491         bhashbd  = hashbd;
492
493         i = BVerifyMove(s, mv, moveno);
494
495         if (c == '?')
496         {
497             /* Bad move, not for the program to play */
498             *flags |= BADMOVE;  /* Flag it ! */
499             while (((c = getc(fd)) == '?') || (c == '!') || (c == '/'));
500         }
501 #ifdef EASY_OPENINGS
502         else if (c == '~')
503         {
504             /* Do not use by computer */
505             *flags |= BADMOVE;  /* Flag it ! */
506
507             while (((c = getc(fd)) == '?') || (c == '!') || (c == '/'));
508         }
509 #endif
510         else if (c == '!')
511         {
512             /* Good move */
513             *flags |= GOODMOVE; /* Flag it ! */
514
515             while (((c = getc(fd)) == '?') || (c == '!') || (c == '/'));
516         }
517         else if (c == '\r')
518         {
519             c = getc(fd);
520         }
521
522         if (c == '(' )
523             while (((c = getc(fd)) != ')') && (c != EOF));
524
525         if (!i)
526         {
527             /* flush to start of next */
528             while (((c = getc(fd)) != '#') && (c != EOF));
529
530             if (c == EOF)
531             {
532                 return -1;
533             }
534             else
535             {
536                 ungetc(c, fd);
537                 return i;
538             }
539         }
540
541         return i;
542     }
543 }
544
545
546 static struct gdxadmin ADMIN;
547 struct gdxadmin B;
548 static struct gdxdata DATA;
549
550 /* lts(l) returns most significant 16 bits of l */
551
552 #if SIZEOF_LONG == 8  /* 64-bit long i.e. 8 bytes */
553 #  define lts(x) (USHORT)(((x >> 48) & 0xfffe) | side)
554 #else
555 #  if defined USE_LTSIMP
556 static USHORT ltsimp(long x)
557 {
558     USHORT n;
559     n = (((x >> 16) & 0xfffe));
560     return n;
561 }
562 #    define lts(x) (USHORT)(ltsimp(x) | side)
563 #  else
564 #    define lts(x) (USHORT)(((x >> 16)&0xfffe) | side)
565 #  endif
566 #endif
567
568
569 /* #define HashValue(l) lts(l) */
570 #define HashValue(l) (USHORT)(l & 0xffff)
571
572
573 static int gfd;
574 static ULONG currentoffset;
575
576
577 #define MAXOFFSET(B) ((B.booksize - 1) * sizeof_gdxdata + sizeof_gdxadmin)
578
579 static void HashOffset(ULONG hashkey, struct gdxadmin *B)
580 {
581     currentoffset = (hashkey % B->booksize) * sizeof_gdxdata + sizeof_gdxadmin;
582 }
583
584
585 static void NextOffset(struct gdxadmin *B)
586 {
587     currentoffset += sizeof_gdxdata;
588     if (currentoffset > B->maxoffset)
589         currentoffset = sizeof_gdxadmin;
590 }
591
592
593 static void WriteAdmin(void)
594 {
595     lseek(gfd, 0, SEEK_SET);
596     write(gfd, (char *)&ADMIN, sizeof_gdxadmin);
597 }
598
599 static void WriteData(int *mustwrite)
600 {
601     if (*mustwrite)
602     {
603         lseek(gfd, currentoffset, SEEK_SET);
604         write(gfd, (char *)&DATA, sizeof_gdxdata);
605         *mustwrite = false;
606     }
607 }
608
609 static int ReadAdmin(void)
610 {
611     lseek(gfd, 0, SEEK_SET);
612     return (sizeof_gdxadmin == read(gfd, (char *)&ADMIN, sizeof_gdxadmin));
613 }
614
615 static int ReadData(struct gdxdata *DATA)
616 {
617     lseek(gfd, currentoffset, SEEK_SET);
618     return (sizeof_gdxdata == read(gfd, (char *)DATA, sizeof_gdxdata));
619 }
620
621
622 /*
623  * GetOpenings()
624  *
625  * CHECKME: is this still valid esp. wrt gnushogi.book?
626  *
627  * Read in the Opening Book file and parse the algebraic notation for a move
628  * into an unsigned integer format indicating the from and to square. Create
629  * a linked list of opening lines of play, with entry->next pointing to the
630  * next line and entry->move pointing to a chunk of memory containing the
631  * moves. More Opening lines of up to 100 half moves may be added to
632  * gnushogi.book. But now it's a hashed table by position which yields a move
633  * or moves for each position. It no longer knows about openings per se only
634  * positions and recommended moves in those positions.
635  *
636  */
637
638 void
639 GetOpenings(void)
640 {
641     short i;
642     int mustwrite = false, first;
643     unsigned short side;
644     short c;
645     USHORT mv, flags;
646     unsigned int x;
647     unsigned int games = 0;
648     LONG collisions = 0;
649     char msg[80];
650
651     FILE *fd;
652
653     if ((fd = fopen(bookfile, "r")) == NULL)
654         fd = fopen("gnushogi.tbk", "r");
655
656     if (fd != NULL)
657     {
658         /* yes add to book */
659         /* open book as writer */
660         gfd = open(binbookfile, O_RDONLY | O_BINARY);
661
662         if (gfd >= 0)
663         {
664             if (ReadAdmin())
665             {
666                 B.bookcount = ADMIN.bookcount;
667                 B.booksize = ADMIN.booksize;
668                 B.maxoffset = ADMIN.maxoffset;
669
670                 if (B.booksize && !(B.maxoffset == MAXOFFSET(B)))
671                 {
672                     printf("bad format %s\n", binbookfile);
673                     exit(1);
674                 }
675             }
676             else
677             {
678                 printf("bad format %s\n", binbookfile);
679                 exit(1);
680             }
681             close(gfd);
682             gfd = open(binbookfile, O_RDWR | O_BINARY);
683
684         }
685         else
686         {
687             gfd = open(binbookfile, O_RDWR | O_CREAT | O_BINARY, 0644);
688
689             ADMIN.bookcount = B.bookcount = 0;
690             ADMIN.booksize = B.booksize = booksize;
691             B.maxoffset = ADMIN.maxoffset = MAXOFFSET(B);
692             DATA.hashbd = 0;
693             DATA.hashkey = 0;
694             DATA.bmove = 0;
695             DATA.flags = 0;
696             DATA.hint = 0;
697             DATA.count = 0;
698             write(gfd, (char *)&ADMIN, sizeof_gdxadmin);
699             printf("creating bookfile %s %ld %ld\n",
700                     binbookfile, B.maxoffset, B.booksize);
701
702             for (x = 0; x < B.booksize; x++)
703             {
704                 write(gfd, (char *)&DATA, sizeof_gdxdata);
705             }
706         }
707
708         if (gfd >= 0)
709         {
710             /* setvbuf(fd, buffr, _IOFBF, 2048); */
711             side = black;
712             hashbd = hashkey = 0;
713             i = 0;
714
715             while ((c = Vparse(fd, &mv, &flags, i)) >= 0)
716             {
717                 if (c == 1)
718                 {
719                     /*
720                      * If this is not the first move of an opening and
721                      * if it's the first time we have seen it then
722                      * save the next move as a hint.
723                      */
724                     i++;
725
726                     if (i < bookmaxply + 2)
727                     {
728                         if (i > 1 && !(flags & BADMOVE))
729                             DATA.hint = mv;
730
731                         if (i < bookmaxply + 1)
732                         {
733                             /*
734                              * See if this position and move already
735                              * exist from some other opening.
736                              */
737
738                             WriteData(&mustwrite);
739                             HashOffset(bhashkey, &B);
740                             first = true;
741
742                             while (true)
743                             {
744                                 if (!ReadData(&DATA))
745                                     break; /* corrupted binbook file */
746
747                                 if (DATA.bmove == 0)
748                                     break;  /* free entry */
749
750                                 if (DATA.hashkey == HashValue(bhashkey)
751                                     && DATA.hashbd == bhashbd)
752                                 {
753                                     if (DATA.bmove == mv)
754                                     {
755                                         /*
756                                          * Yes, so just bump count - count
757                                          * is used to choose the opening
758                                          * move in proportion to its
759                                          * presence in the book.
760                                          */
761
762                                         DATA.count++;
763                                         DATA.flags |= flags;
764                                         mustwrite = true;
765                                         break;
766                                     }
767                                     else
768                                     {
769                                         if (first)
770                                             collisions++;
771
772                                         if (DATA.flags & LASTMOVE)
773                                         {
774                                             DATA.flags &= (~LASTMOVE);
775                                             mustwrite = true;
776                                             WriteData(&mustwrite);
777                                         }
778                                     }
779                                 }
780
781                                 NextOffset(&B);
782                                 first = false;
783                             }
784
785                             /*
786                              * Doesn't exist so add it to the book.
787                              */
788
789                             if (!mustwrite)
790                             {
791                                 B.bookcount++;
792
793                                 if ((B.bookcount % 1000) == 0)
794                                 {
795                                     /* CHECKME: may want to get rid of this,
796                                      * especially for xshogi. */
797                                     printf("%ld rec %d openings "
798                                            "processed\n",
799                                            B.bookcount, games);
800                                 }
801
802                                 /* initialize a record */
803                                 DATA.hashbd = bhashbd;
804                                 DATA.hashkey = HashValue(bhashkey);
805                                 DATA.bmove = mv;
806                                 DATA.flags = flags | LASTMOVE;
807                                 DATA.count = 1;
808                                 DATA.hint = 0;
809                                 mustwrite = true;
810                             }
811                         }
812                     }
813
814                     computer = opponent;
815                     opponent = computer ^ 1;
816
817                     side = side ^ 1;
818                 }
819                 else if (i > 0)
820                 {
821                     /* reset for next opening */
822                     games++;
823                     WriteData(&mustwrite);
824                     RESET();
825                     i = 0;
826                     side = black;
827                 }
828             }
829
830             WriteData(&mustwrite);
831             fclose(fd);
832             /* write admin rec with counts */
833             ADMIN.bookcount = B.bookcount;
834             WriteAdmin();
835
836             close(gfd);
837         }
838     }
839
840     if (binbookfile != NULL)
841     {
842         /* open book as reader */
843         gfd = open(binbookfile, O_RDONLY | O_BINARY);
844
845         if (gfd >= 0)
846         {
847             if (ReadAdmin() && (!ADMIN.booksize
848                                 || (ADMIN.maxoffset == MAXOFFSET(ADMIN))))
849             {
850                 B.bookcount = ADMIN.bookcount;
851                 B.booksize  = ADMIN.booksize;
852                 B.maxoffset = ADMIN.maxoffset;
853             }
854             else
855             {
856                 printf("bad format %s\n", binbookfile);
857                 exit(1);
858             }
859
860         }
861         else
862         {
863             B.bookcount = 0;
864             B.booksize = booksize;
865
866         }
867
868         sprintf(msg, "Book used %lu(%lu).", B.bookcount, B.booksize);
869         dsp->ShowMessage(msg);
870     }
871
872     /* Set everything back to start the game. */
873     Book = BOOKFAIL;
874     RESET();
875
876     /* Now get ready to play .*/
877     if (!B.bookcount)
878     {
879         dsp->ShowMessage("Can't find book.");
880         Book = 0;
881     }
882 }
883
884
885 /*
886  * OpeningBook(hint)
887  *
888  * Go through each of the opening lines of play and check for a match with
889  * the current game listing. If a match occurs, generate a random
890  * number. If this number is the largest generated so far then the next
891  * move in this line becomes the current "candidate".  After all lines are
892  * checked, the candidate move is put at the top of the Tree[] array and
893  * will be played by the program.  Note that the program does not handle
894  * book transpositions.
895  */
896
897 int
898 OpeningBook(unsigned short *hint)
899 {
900     unsigned short r, m;
901     int possibles = TrPnt[2] - TrPnt[1];
902
903     gsrand((unsigned int) time((long *) 0));
904     m = 0;
905
906     /*
907      * Find all the moves for this position  - count them and get their
908      * total count.
909      */
910
911     {
912         USHORT i, x;
913         USHORT rec = 0;
914         USHORT summ = 0;
915         USHORT h = 0, b = 0;
916         struct gdxdata OBB[128];
917
918         if (B.bookcount == 0)
919         {
920             Book--;
921             return false;
922         }
923
924         x = 0;
925         HashOffset(hashkey, &B);
926 #ifdef BOOKTEST
927         printf("looking for book move, bhashbd = 0x%lx bhashkey = 0x%x\n",
928                (ULONG)hashbd, HashValue(hashkey));
929 #endif
930         while (true)
931         {
932             if (!ReadData(&OBB[x]))
933                 break;
934
935             if (OBB[x].bmove == 0)
936                 break;
937
938 #ifdef BOOKTEST
939             printf("compare with bhashbd = 0x%lx bhashkey = 0x%x\n",
940                    OBB[x].hashbd, OBB[x].hashkey);
941 #endif
942             if ((OBB[x].hashkey == HashValue(hashkey))
943                 && (OBB[x].hashbd == (ULONG)hashbd))
944             {
945                 x++;
946
947                 if (OBB[x-1].flags & LASTMOVE)
948                     break;
949             }
950
951             NextOffset(&B);
952         }
953
954 #ifdef BOOKTEST
955         printf("%d book move(s) found.\n", x);
956 #endif
957
958         if (x == 0)
959         {
960             Book--;
961             return false;
962         }
963
964         for (i = 0; i < x; i++)
965         {
966             if (OBB[i].flags & BADMOVE)
967             {
968                 m = OBB[i].bmove;
969
970                 /* Is the move in the MoveList? */
971                 for (b = TrPnt[1]; b < (unsigned) TrPnt[2]; b++)
972                 {
973                     if (((Tree[b].f << 8) | Tree[b].t) == m)
974                     {
975                         if (--possibles)
976                             Tree[b].score = DONTUSE;
977                         break;
978                     }
979                 }
980             }
981             else
982             {
983 #if defined BOOKTEST
984                 char s[20];
985                 movealgbr(m = OBB[i].bmove, s);
986                 printf("finding book move: %s\n", s);
987 #endif
988                 summ += OBB[i].count;
989             }
990         }
991
992         if (summ == 0)
993         {
994             Book--;
995             return false;
996         }
997
998         r = (urand() % summ);
999
1000         for (i = 0; i < x; i++)
1001         {
1002             if (!(OBB[i].flags & BADMOVE))
1003             {
1004                 if (r < OBB[i].count)
1005                 {
1006                     rec = i;
1007                     break;
1008                 }
1009                 else
1010                 {
1011                     r -= OBB[i].count;
1012                 }
1013             }
1014         }
1015
1016         h = OBB[rec].hint;
1017         m = OBB[rec].bmove;
1018
1019         /* Make sure the move is in the MoveList. */
1020         for (b = TrPnt[1]; b < (unsigned) TrPnt[2]; b++)
1021         {
1022             if (((Tree[b].f << 8) | Tree[b].t) == m)
1023             {
1024                 Tree[b].flags |= book;
1025                 Tree[b].score = 0;
1026                 break;
1027             }
1028         }
1029
1030         /* Make sure it's the best. */
1031
1032         pick(TrPnt[1], TrPnt[2] - 1);
1033
1034         if (Tree[TrPnt[1]].score)
1035         {
1036             /* no! */
1037             Book--;
1038             return false;
1039         }
1040
1041         /* Ok, pick up the hint and go. */
1042         *hint = h;
1043         return true;
1044     }
1045
1046     Book--;
1047     return false;
1048 }