search: fixed a bug in NULL move pruning
[owl.git] / see.c
blob9a10e18927bed0a73aa1da92ebc1775d9b92084c
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 "attacks.h"
18 #include "board.h"
19 #include "move.h"
21 int SEE(int move)
23 int count;
24 int square;
25 int from, to, type;
26 int piece;
27 int att_side, def_side;
28 int last_val;
29 uint64_t bb;
30 uint64_t occ;
31 uint64_t atts, defs;
32 int swap_list[32];
34 occ = complete;
35 from = GET_FROM(move);
36 to = GET_TO(move);
37 type = GET_TYPE(move);
39 att_side = (BIT(from) & side_boards[WHITE]) ? WHITE : BLACK;
40 def_side = 1 ^ att_side;
42 CLEAR(occ, from);
43 atts = ATTACK_TO(to, att_side, occ);
44 defs = ATTACK_TO(to, def_side, occ);
45 CLEAR(atts, from);
47 swap_list[0] = 0;
48 last_val = 0;
50 if (type & TYPE_PROMOTE) {
51 swap_list[0] = piece_value[GET_PROMOTE(move)] - piece_value[PAWN];
52 last_val = -piece_value[GET_PROMOTE(move)];
54 swap_list[0] += (GET_TYPE(move) & TYPE_ENPASSANT) ? piece_value[PAWN] : piece_value[square64[to]];
55 last_val += -piece_value[square64[from]];
57 count = 1;
58 while (defs) {
59 for (piece = PAWN; piece <= KING; piece++) {
60 bb = defs & piece_boards[def_side][piece];
61 if (bb) {
62 square = bit_scan(bb);
63 defs &= defs - 1;
65 swap_list[count] = swap_list[count - 1] + last_val;
66 last_val = piece_value[piece];
67 count++;
69 break;
72 if (!atts) {
73 break;
75 for (piece = PAWN; piece <= KING; piece++) {
76 bb = atts & piece_boards[att_side][piece];
77 if (bb) {
78 square = bit_scan(bb);
79 atts &= atts - 1;
81 swap_list[count] = swap_list[count - 1] + last_val;
82 last_val = -piece_value[piece];
83 count++;
85 break;
91 count--;
93 while (count) {
94 if (count & 1) {
95 if (swap_list[count] <= swap_list[count - 1])
96 swap_list[count - 1] = swap_list[count];
97 } else {
98 if (swap_list[count] >= swap_list[count - 1])
99 swap_list[count - 1] = swap_list[count];
101 count--;
104 return swap_list[0];