- // Early return if SEE cannot be negative because captured piece value
- // is not less then capturing one. Note that king moves always return
- // here because king midgame value is set to 0.
- if (PieceValue[MG][piece_moved(m)] <= PieceValue[MG][piece_on(to_sq(m))])
- return 1;
-
- return see(m);
-}
-
-int Position::see(Move m, int asymmThreshold) const {
-
- Square from, to;
- Bitboard occupied, attackers, stmAttackers;
- int swapList[32], slIndex = 1;
- PieceType captured;
- Color stm;
-
- assert(is_ok(m));
-
- from = from_sq(m);
- to = to_sq(m);
- swapList[0] = PieceValue[MG][type_of(piece_on(to))];
- stm = color_of(piece_on(from));
- occupied = pieces() ^ from;
-
- // Castle moves are implemented as king capturing the rook so cannot be
- // handled correctly. Simply return 0 that is always the correct value
- // unless in the rare case the rook ends up under attack.
- if (type_of(m) == CASTLE)
- return 0;
-
- if (type_of(m) == ENPASSANT)
- {
- occupied ^= to - pawn_push(stm); // Remove the captured pawn
- swapList[0] = PieceValue[MG][PAWN];
- }
-
- // Find all attackers to the destination square, with the moving piece
- // removed, but possibly an X-ray attacker added behind it.
- attackers = attackers_to(to, occupied) & occupied;
-
- // If the opponent has no attackers we are finished
- stm = ~stm;
- stmAttackers = attackers & pieces(stm);
- if (!stmAttackers)
- return swapList[0];
-
- // The destination square is defended, which makes things rather more
- // difficult to compute. We proceed by building up a "swap list" containing
- // the material gain or loss at each stop in a sequence of captures to the
- // destination square, where the sides alternately capture, and always
- // capture with the least valuable piece. After each capture, we look for
- // new X-ray attacks from behind the capturing piece.
- captured = type_of(piece_on(from));
-
- do {
- assert(slIndex < 32);
-
- // Add the new entry to the swap list
- swapList[slIndex] = -swapList[slIndex - 1] + PieceValue[MG][captured];
- slIndex++;
-
- // Locate and remove the next least valuable attacker
- captured = min_attacker<PAWN>(byTypeBB, to, stmAttackers, occupied, attackers);
- stm = ~stm;
- stmAttackers = attackers & pieces(stm);
-
- // Stop before processing a king capture
- if (captured == KING && stmAttackers)
- {
- swapList[slIndex++] = QueenValueMg * 16;
- break;
- }
-
- } while (stmAttackers);
-
- // If we are doing asymmetric SEE evaluation and the same side does the first
- // and the last capture, he loses a tempo and gain must be at least worth
- // 'asymmThreshold', otherwise we replace the score with a very low value,
- // before negamaxing.
- if (asymmThreshold)
- for (int i = 0; i < slIndex; i += 2)
- if (swapList[i] < asymmThreshold)
- swapList[i] = - QueenValueMg * 16;