Merge branch 'master' into libmap
[pachi.git] / fbook.c
blob4041ff4ec1c15a92ee1ff51c98033862f431771f
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #define DEBUG
8 #include "board.h"
9 #include "debug.h"
10 #include "fbook.h"
11 #include "random.h"
14 static coord_t
15 coord_transform(struct board *b, coord_t coord, int i)
17 #define HASH_VMIRROR 1
18 #define HASH_HMIRROR 2
19 #define HASH_XYFLIP 4
20 if (i & HASH_VMIRROR) {
21 coord = coord_xy(b, coord_x(coord, b), board_size(b) - 1 - coord_y(coord, b));
23 if (i & HASH_HMIRROR) {
24 coord = coord_xy(b, board_size(b) - 1 - coord_x(coord, b), coord_y(coord, b));
26 if (i & HASH_XYFLIP) {
27 coord = coord_xy(b, coord_y(coord, b), coord_x(coord, b));
29 return coord;
32 /* Check if we can make a move along the fbook right away.
33 * Otherwise return pass. */
34 coord_t
35 fbook_check(struct board *board)
37 if (!board->fbook) return pass;
39 hash_t hi = board->hash;
40 coord_t cf = pass;
41 while (!is_pass(board->fbook->moves[hi & fbook_hash_mask])) {
42 if (board->fbook->hashes[hi & fbook_hash_mask] == board->hash) {
43 cf = board->fbook->moves[hi & fbook_hash_mask];
44 break;
46 hi++;
48 if (!is_pass(cf)) {
49 if (DEBUGL(1))
50 fprintf(stderr, "fbook match %"PRIhash":%"PRIhash"\n", board->hash, board->hash & fbook_hash_mask);
51 } else {
52 /* No match, also prevent further fbook usage
53 * until the next clear_board. */
54 if (DEBUGL(4))
55 fprintf(stderr, "fbook out %"PRIhash":%"PRIhash"\n", board->hash, board->hash & fbook_hash_mask);
56 fbook_done(board->fbook);
57 board->fbook = NULL;
59 return cf;
62 static struct fbook *fbcache;
64 struct fbook *
65 fbook_init(char *filename, struct board *b)
67 if (fbcache && fbcache->bsize == board_size(b)
68 && fbcache->handicap == b->handicap)
69 return fbcache;
71 FILE *f = fopen(filename, "r");
72 if (!f) {
73 perror(filename);
74 return NULL;
77 struct fbook *fbook = calloc(1, sizeof(*fbook));
78 fbook->bsize = board_size(b);
79 fbook->handicap = b->handicap;
80 /* We do not set handicap=1 in case of too low komi on purpose;
81 * we want to go with the no-handicap fbook for now. */
82 for (int i = 0; i < 1<<fbook_hash_bits; i++)
83 fbook->moves[i] = pass;
85 if (DEBUGL(1))
86 fprintf(stderr, "Loading opening fbook %s...\n", filename);
88 /* Scratch board where we lay out the sequence;
89 * one for each transposition. */
90 struct board *bs[8];
91 for (int i = 0; i < 8; i++) {
92 bs[i] = board_init(NULL);
93 board_resize(bs[i], fbook->bsize - 2);
96 char linebuf[1024];
97 while (fgets(linebuf, sizeof(linebuf), f)) {
98 char *line = linebuf;
99 linebuf[strlen(linebuf) - 1] = 0; // chop
101 /* Format of line is:
102 * BSIZE COORD COORD COORD... | COORD
103 * BSIZE/HANDI COORD COORD COORD... | COORD */
104 int bsize = strtol(line, &line, 10);
105 if (bsize != fbook->bsize - 2)
106 continue;
107 int handi = 0;
108 if (*line == '/') {
109 line++;
110 handi = strtol(line, &line, 10);
112 if (handi != fbook->handicap)
113 continue;
114 while (isspace(*line)) line++;
116 for (int i = 0; i < 8; i++) {
117 board_clear(bs[i]);
118 bs[i]->last_move.color = S_WHITE;
121 while (*line != '|') {
122 coord_t *c = str2coord(line, fbook->bsize);
124 for (int i = 0; i < 8; i++) {
125 coord_t coord = coord_transform(b, *c, i);
126 struct move m = { .coord = coord, .color = stone_other(bs[i]->last_move.color) };
127 int ret = board_play(bs[i], &m);
128 assert(ret >= 0);
131 coord_done(c);
132 while (!isspace(*line)) line++;
133 while (isspace(*line)) line++;
136 line++;
137 while (isspace(*line)) line++;
139 /* In case of multiple candidates, pick one with
140 * exponentially decreasing likelihood. */
141 while (strchr(line, ' ') && fast_random(2)) {
142 line = strchr(line, ' ');
143 while (isspace(*line)) line++;
144 // fprintf(stderr, "<%s> skip to %s\n", linebuf, line);
147 coord_t *c = str2coord(line, fbook->bsize);
148 for (int i = 0; i < 8; i++) {
149 coord_t coord = coord_transform(b, *c, i);
150 #if 0
151 char conflict = is_pass(fbook->moves[bs[i]->hash & fbook_hash_mask]) ? '+' : 'C';
152 if (conflict == 'C')
153 for (int j = 0; j < i; j++)
154 if (bs[i]->hash == bs[j]->hash)
155 conflict = '+';
156 if (conflict == 'C') {
157 hash_t hi = bs[i]->hash;
158 while (!is_pass(fbook->moves[hi & fbook_hash_mask]) && fbook->hashes[hi & fbook_hash_mask] != bs[i]->hash)
159 hi++;
160 if (fbook->hashes[hi & fbook_hash_mask] == bs[i]->hash)
161 hi = 'c';
163 fprintf(stderr, "%c %"PRIhash":%"PRIhash" (<%d> %s)\n", conflict,
164 bs[i]->hash & fbook_hash_mask, bs[i]->hash, i, linebuf);
165 #endif
166 hash_t hi = bs[i]->hash;
167 while (!is_pass(fbook->moves[hi & fbook_hash_mask]) && fbook->hashes[hi & fbook_hash_mask] != bs[i]->hash)
168 hi++;
169 fbook->moves[hi & fbook_hash_mask] = coord;
170 fbook->hashes[hi & fbook_hash_mask] = bs[i]->hash;
171 fbook->movecnt++;
173 coord_done(c);
176 for (int i = 0; i < 8; i++) {
177 board_done(bs[i]);
180 fclose(f);
182 if (!fbook->movecnt) {
183 /* Empty book is not worth the hassle. */
184 fbook_done(fbook);
185 return NULL;
188 struct fbook *fbold = fbcache;
189 fbcache = fbook;
190 if (fbold)
191 fbook_done(fbold);
193 return fbook;
196 void fbook_done(struct fbook *fbook)
198 if (fbook != fbcache)
199 free(fbook);