Fix in candidate passer calculation
[owl.git] / init.c
blob0a15efa31a728e9b7f720357ea441d8cc3179174
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 #include "common.h"
17 #include "board.h"
18 #include "init.h"
19 #include "init_attacks.h"
21 /* classic mailbox board representation */
22 const int mailbox[120] = {
23 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
24 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25 -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
26 -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
27 -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
28 -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
29 -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
30 -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
31 -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
32 -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
37 const int mailbox64[64] = {
38 21, 22, 23, 24, 25, 26, 27, 28,
39 31, 32, 33, 34, 35, 36, 37, 38,
40 41, 42, 43, 44, 45, 46, 47, 48,
41 51, 52, 53, 54, 55, 56, 57, 58,
42 61, 62, 63, 64, 65, 66, 67, 68,
43 71, 72, 73, 74, 75, 76, 77, 78,
44 81, 82, 83, 84, 85, 86, 87, 88,
45 91, 92, 93, 94, 95, 96, 97, 98 };
47 const int pieces_offsets[7][8] = {
48 { 0, 0, 0, 0, 0, 0, 0, 0 },
49 { 0, 0, 0, 0, 0, 0, 0, 0 },
50 { -21, -19, -12, -8, 8, 12, 19, 21 },
51 { -11, -9, 9, 11, 0, 0, 0, 0 },
52 { -10, -1, 1, 10, 0, 0, 0, 0 },
53 { -11, -10, -9, -1, 1, 9, 10, 11 },
54 { -11, -10, -9, -1, 1, 9, 10, 11 }
57 const int offsets[7] = { 0, 0, 8, 4, 4, 8, 8 };
59 void
60 initialize_bitboards(void)
62 initialize_bitcount();
63 initialize_lzarray();
64 initialize_moves();
65 initialize_forward_ray();
66 initialize_passers();
67 initialize_isolated();
68 initialize_distance();
69 initialize_attacks();
70 initialize_king_shield();
73 void
74 initialize_bitcount(void)
76 int i, j, n;
78 bitcount[0] = 0;
79 bitcount[1] = 1;
80 i = 1;
82 for (n = 2; n <= 16; n++) {
83 i <<= 1;
84 for (j = i; j <= i + (i-1); j++)
85 bitcount[j] = 1 + bitcount[j - i];
89 void
90 initialize_lzarray(void)
92 int i, j, s, n;
94 s = n = 1;
95 for (i = 0; i < 16; i++) {
96 for (j = s; j < s + n; j++)
97 lzArray[j] = 16 - 1 - i;
98 s += n;
99 n += n;
103 void
104 initialize_moves(void)
106 initialize_pawn_moves();
107 initialize_piece_moves();
110 void
111 initialize_pawn_moves(void)
113 int i;
114 int side;
115 int sign_side[2] = { 1, -1 };
117 ZERO_MEM(pawn_moves);
118 for (side = WHITE; side <= BLACK; side++) {
119 for (i = 0; i < 64; i++) {
120 if (_FILE(i) != 7)
121 SET(pawn_moves[side][i], i + sign_side[side] * (9 - side * 2));
122 if (_FILE(i) != 0)
123 SET(pawn_moves[side][i], i + sign_side[side] * (7 + side * 2));
128 void
129 initialize_piece_moves(void)
131 int i, j, x;
132 int piece;
134 ZERO_MEM(piece_moves);
135 for (piece = KNIGHT; piece <= KING; piece++)
136 for (i = 0; i < 64; i++)
137 for (j = 0; j < offsets[piece]; j++) {
138 x = i;
139 while (TRUE) {
140 x = mailbox[mailbox64[x] + pieces_offsets[piece][j]];
141 if (x != -1)
142 SET(piece_moves[piece][i], x);
143 if (x == -1 || piece == KNIGHT || piece == KING)
144 break;
149 void
150 initialize_forward_ray(void)
152 int i, j, x;
153 uint64_t prev;
155 ZERO_MEM(forward_ray);
157 for (i = 0; i < 64; i++) {
158 for (j = WHITE; j <= BLACK; j++) {
159 x = i;
160 prev = 0;
161 while (TRUE) {
162 x = mailbox[mailbox64[x] + 10 * (j ? -1 : 1)];
164 if (x == -1)
165 break;
167 SET(forward_ray[j][i], x);
168 forward_ray[j][i] |= prev;
169 prev = forward_ray[j][i];
175 void
176 initialize_passers(void)
178 int i, side;
180 ZERO_MEM(passer_mask);
182 for (side = 0; side <= 1; side++)
183 for (i = 0; i < 64; i++) {
184 passer_mask[side][i] = forward_ray[side][i];
185 if (_FILE(i) != 0)
186 passer_mask[side][i] |= forward_ray[side][i - 1];
187 if (_FILE(i) != 7)
188 passer_mask[side][i] |= forward_ray[side][i + 1];
192 void
193 initialize_isolated(void)
195 isolated_mask[0] = file_bit[1];
196 isolated_mask[1] = file_bit[0] | file_bit[2];
197 isolated_mask[2] = file_bit[1] | file_bit[3];
198 isolated_mask[3] = file_bit[2] | file_bit[4];
199 isolated_mask[4] = file_bit[3] | file_bit[5];
200 isolated_mask[5] = file_bit[4] | file_bit[6];
201 isolated_mask[6] = file_bit[5] | file_bit[7];
202 isolated_mask[7] = file_bit[6];
205 void
206 initialize_distance(void)
208 int from, to;
209 int file, rank;
211 for (from = 0; from < 64; from++)
212 for (to = 0; to < 64; to++) {
213 file = abs(_FILE(from) - _FILE(to));
214 rank = abs(_RANK(from) - _RANK(to));
215 distance[from][to] = MAX(file, rank);
219 void
220 initialize_king_shield(void)
222 int side, square;
224 ZERO_MEM(king_shield);
225 for (side = 0; side <= 1; side++)
226 for (square = 0; square < 64; square++) {
227 king_shield[side][square] |= file_bit[_FILE(square)];
228 if (_FILE(square) != 0)
229 king_shield[side][square] |= file_bit[_FILE(square - 1)];
230 if (_FILE(square) != 7)
231 king_shield[side][square] |= file_bit[_FILE(square + 1)];