-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.cpp
186 lines (161 loc) · 3.63 KB
/
book.cpp
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
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#include <endian.h>
#include "book.h"
struct polygonEntry
{
uint64_t key;
uint16_t move;
uint16_t weight;
uint32_t learn;
} __attribute__((packed));
void endianConvertEntry(polygonEntry &ent)
{
ent.key = be64toh(ent.key);
ent.move = be16toh(ent.move);
ent.weight = be16toh(ent.weight);
ent.learn = be16toh(ent.learn);
}
using namespace std;
vector<pair<uint64_t, int> > index;
FILE *bookfile = NULL;
int bookfile_entries = 0;
void initBook()
{
if (bookfile != NULL)
fclose(bookfile);
bookfile = fopen(OPENINGBOOK, "rb");
if (bookfile == NULL)
exit(1);
// generate index
fseek(bookfile, 0, SEEK_END);
int bookfile_len = ftell(bookfile);
bookfile_entries = bookfile_len/16;
index.clear();
index.resize((1 << CACHE_DEPTH)-1);
int blockSize = bookfile_entries >> CACHE_DEPTH;
for (int i=1; i<(1<<CACHE_DEPTH); i++)
{
fseek(bookfile, i*blockSize*sizeof(polygonEntry), SEEK_SET);
polygonEntry ent;
if (fread(&ent, sizeof(polygonEntry), 1, bookfile) != 1)
exit(1);
endianConvertEntry(ent);
uint64_t ckey = ent.key;
index[i-1] = make_pair(ckey, i*blockSize);
}
}
bool queryBook(board b, move &m)
{
if (bookfile == NULL)
initBook();
uint64_t ourKey = b.hash();
// Search index
int low = 0, high = (1 << CACHE_DEPTH);
while (high-low > 1)
{
int mid = (high+low)/2;
if (ourKey < index[mid-1].first)
high = mid;
else
low = mid;
}
if (low != 0)
low = index[low-1].second;
if (high == (1 << CACHE_DEPTH))
high = bookfile_entries;
else
high = index[high-1].second;
// Search book itself
while (high - low > 1)
{
int mid = (high + low)/2;
polygonEntry ent;
fseek(bookfile, mid*sizeof(polygonEntry), SEEK_SET);
if (fread(&ent, sizeof(polygonEntry), 1, bookfile) != 1)
return false;
endianConvertEntry(ent);
if (ourKey < ent.key)
high = mid;
else
low = mid;
}
// go to low end of range for this key
while (low != 0)
{
polygonEntry ent;
fseek(bookfile, (low-1)*sizeof(polygonEntry), SEEK_SET);
if (fread(&ent, sizeof(polygonEntry), 1, bookfile) != 1)
return false;
endianConvertEntry(ent);
if (ent.key == ourKey)
low--;
else
break;
}
// read all entries for this key
vector<polygonEntry> ents;
uint32_t weight_sum = 0;
while (low != bookfile_entries)
{
polygonEntry ent;
fseek(bookfile, low*sizeof(polygonEntry), SEEK_SET);
if (fread(&ent, sizeof(polygonEntry), 1, bookfile)!= 1)
return false;
endianConvertEntry(ent);
if (ent.key == ourKey)
{
weight_sum += ent.weight;
ents.push_back(ent);
}
else
{
break;
}
low++;
}
if (ents.size() == 0 || weight_sum == 0)
return false;
uint32_t choice = rand() % weight_sum;
uint32_t partWS = 0;
for (int i=0; i<ents.size(); i++)
{
partWS += ents[i].weight;
if (partWS > choice)
{
int from = 0, to = 0, piece = 0;
to = ents[i].move & 0x003F;
from = (ents[i].move & 0x0FC0) >> 6;
piece = ents[i].move & 0xF000;
switch(piece)
{
case 0x1000:
piece = PIECE_KNIGHT;
break;
case 0x2000:
piece = PIECE_BISHOP;
break;
case 0x3000:
piece = PIECE_ROOK;
break;
case 0x4000:
piece = PIECE_QUEEN;
break;
}
if (from == 4 && to == 0 && b.squares[4] == (PIECE_KING | PIECE_WHITE))
to = 2;
if (from == 4 && to == 7 && b.squares[4] == (PIECE_KING | PIECE_WHITE))
to = 6;
if (from == 60 && to == 56 && b.squares[60] == (PIECE_KING | PIECE_BLACK))
to = 58;
if (from == 60 && to == 63 && b.squares[60] == (PIECE_KING | PIECE_BLACK))
to = 62;
m = move(from, to, piece);
return true;
}
}
return false;
}