diff --git a/src/eval.cpp b/src/eval.cpp index e8837f4..9db1c43 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -47,6 +47,64 @@ int victim_type(const Position& pos, Move m) { return m.is_en_passant() ? PAWN : } // namespace +bool see_ge(const Position& pos, Move m, int threshold) { + // Castling, en passant and promotions are taken as an even trade. + if (m.is_castle() || m.is_en_passant() || m.is_promotion()) return threshold <= 0; + + const int from = m.from(); + const int to = m.to(); + + // `swap` is what the side to move is ahead of the threshold if the exchange + // stops now, seen from the side that just captured. + int swap = (pos.piece_on(to) == NO_PIECE ? 0 : SEE_VALUE[piece_type(pos.piece_on(to))]) - threshold; + if (swap < 0) return false; + swap = SEE_VALUE[piece_type(pos.piece_on(from))] - swap; + if (swap <= 0) return true; + + u64 occ = pos.occupancy() ^ bb(from) ^ bb(to); + u64 attackers = pos.attackers_to(to, occ); + const u64 diagonal = pos.pieces[BISHOP] | pos.pieces[QUEEN]; + const u64 straight = pos.pieces[ROOK] | pos.pieces[QUEEN]; + int stm = pos.stm; + int result = 1; + + for (;;) { + stm ^= 1; + attackers &= occ; + const u64 ours = attackers & pos.colors[stm]; + if (!ours) break; + result ^= 1; + + // Recapture with the least valuable piece; taking it off the board may + // open a line for a slider behind it. + u64 b; + if ((b = ours & pos.pieces[PAWN])) { + if ((swap = SEE_VALUE[PAWN] - swap) < result) break; + occ ^= bb(lsb(b)); + attackers |= attacks::bishop(to, occ) & diagonal; + } else if ((b = ours & pos.pieces[KNIGHT])) { + if ((swap = SEE_VALUE[KNIGHT] - swap) < result) break; + occ ^= bb(lsb(b)); + } else if ((b = ours & pos.pieces[BISHOP])) { + if ((swap = SEE_VALUE[BISHOP] - swap) < result) break; + occ ^= bb(lsb(b)); + attackers |= attacks::bishop(to, occ) & diagonal; + } else if ((b = ours & pos.pieces[ROOK])) { + if ((swap = SEE_VALUE[ROOK] - swap) < result) break; + occ ^= bb(lsb(b)); + attackers |= attacks::rook(to, occ) & straight; + } else if ((b = ours & pos.pieces[QUEEN])) { + if ((swap = SEE_VALUE[QUEEN] - swap) < result) break; + occ ^= bb(lsb(b)); + attackers |= (attacks::bishop(to, occ) & diagonal) | (attacks::rook(to, occ) & straight); + } else { + // Only the king is left: it may recapture only if nothing defends the square. + return (attackers & ~pos.colors[stm]) ? result ^ 1 : result; + } + } + return result; +} + void init() { for (int type = PAWN; type <= KING; type++) { for (int sq = 0; sq < 64; sq++) { @@ -86,6 +144,8 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) { const int victim = victim_type(pos, m); // Delta pruning: even winning the piece for free cannot reach alpha. if (!in_check && !m.is_promotion() && stand_pat + SEE_VALUE[victim] + 200 <= alpha) continue; + // Captures that lose material in the exchange on the square. + if (!in_check && !see_ge(pos, m, 0)) continue; score = SEE_VALUE[victim] * 16 - piece_type(pos.piece_on(m.from())); } if (m.is_promotion()) score += SEE_VALUE[m.promo_type()] * 16; diff --git a/src/eval.h b/src/eval.h index de8d5b7..27972df 100644 --- a/src/eval.h +++ b/src/eval.h @@ -12,6 +12,10 @@ void init(); // Static evaluation (the value network) in centipawns, side to move's view. int static_eval(const Position& pos); +// Static exchange evaluation: true when the exchange `m` starts on its target +// square gains at least `threshold` centipawns for the side to move (pins ignored). +bool see_ge(const Position& pos, Move m, int threshold); + // Capture-only search on top of the static evaluation, side to move's view. int quiescence(const Position& pos, int alpha, int beta, int ply);