- attackers = attackers_to(to, occupied);
-
- // If the opponent has no attackers we are finished
- stm = ~color_of(piece_on(from));
- stmAttackers = attackers & pieces(stm);
- if (!stmAttackers)
- return PieceValue[Mg][captured];
-
- // 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.
- swapList[0] = PieceValue[Mg][captured];
- 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 the least valuable attacker for the side to move. The loop
- // below looks like it is potentially infinite, but it isn't. We know
- // that the side to move still has at least one attacker left.
- for (captured = PAWN; (b = stmAttackers & pieces(captured)) == 0; captured++)
- assert(captured < KING);
-
- // Remove the attacker we just found from the 'occupied' bitboard,
- // and scan for new X-ray attacks behind the attacker.
- occupied ^= (b & (~b + 1));
- attackers |= (attacks_bb<ROOK>(to, occupied) & pieces(ROOK, QUEEN))
- | (attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN));
-
- attackers &= occupied; // Cut out pieces we've already done
- stm = ~stm;
- stmAttackers = attackers & pieces(stm);
-
- // Stop before processing a king capture
- if (captured == KING && stmAttackers)
- {
- assert(slIndex < 32);
- swapList[slIndex++] = QueenValueMg * 16;
- break;
- }