- // En passant moves:
- if(move_is_ep(m)) {
-
- // The piece must be a pawn:
- if(type_of_piece(pc) != PAWN)
- return MOVE_NONE;
-
- // The destination square must be the en passant square:
- if(to != pos.ep_square())
- return MOVE_NONE;
-
- assert(pos.square_is_empty(to));
- assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
-
- // The move is pseudo-legal. If it is legal, return it.
- if(pos.move_is_legal(m))
- return m;
- else
- return MOVE_NONE;
- }
-
- // Castling moves:
- else if(move_is_short_castle(m)) {
-
- // The piece must be a king:
- if(type_of_piece(pc) != KING)
- return MOVE_NONE;
-
- // The side to move must still have the right to castle kingside:
- if(!pos.can_castle_kingside(us))
- return MOVE_NONE;
-
- assert(from == pos.king_square(us));
- assert(to == pos.initial_kr_square(us));
- assert(pos.piece_on(to) == rook_of_color(us));
-
- Square g1 = relative_square(us, SQ_G1);
- Square f1 = relative_square(us, SQ_F1);
- Square s;
- bool illegal = false;
-
- for(s = Min(from, g1); s <= Max(from, g1); s++)
- if((s != from && s != to && !pos.square_is_empty(s)) ||
- pos.square_is_attacked(s, them))
- illegal = true;
- for(s = Min(to, f1); s <= Max(to, f1); s++)
- if(s != from && s != to && !pos.square_is_empty(s))
- illegal = true;
-
- if(!illegal)
- return m;
- else
- return MOVE_NONE;
- }
- else if(move_is_long_castle(m)) {
-
- // The piece must be a king:
- if(type_of_piece(pc) != KING)
- return MOVE_NONE;
-
- // The side to move must still have the right to castle kingside:
- if(!pos.can_castle_queenside(us))
- return MOVE_NONE;
-
- assert(from == pos.king_square(us));
- assert(to == pos.initial_qr_square(us));
- assert(pos.piece_on(to) == rook_of_color(us));
-
- Square c1 = relative_square(us, SQ_C1);
- Square d1 = relative_square(us, SQ_D1);
- Square s;
- bool illegal = false;
-
- for(s = Min(from, c1); s <= Max(from, c1); s++)
- if((s != from && s != to && !pos.square_is_empty(s)) ||
- pos.square_is_attacked(s, them))
- illegal = true;
- for(s = Min(to, d1); s <= Max(to, d1); s++)
- if(s != from && s != to && !pos.square_is_empty(s))
- illegal = true;
- if(square_file(to) == FILE_B &&
- (pos.piece_on(to + DELTA_W) == rook_of_color(them) ||
- pos.piece_on(to + DELTA_W) == queen_of_color(them)))
- illegal = true;
-
- if(!illegal)
- return m;
- else
- return MOVE_NONE;
- }
-
- // Normal moves
- else {
-
- // The destination square cannot be occupied by a friendly piece:
- if(pos.color_of_piece_on(to) == us)
- return MOVE_NONE;
-
- // Proceed according to the type of the moving piece.
- switch(type_of_piece(pc)) {
-
- case PAWN:
- // Pawn moves, as usual, are somewhat messy.
- if(us == WHITE) {
- // If the destination square is on the 8th rank, the move must be a
- // promotion.
- if(square_rank(to) == RANK_8 && !move_promotion(m))
- return MOVE_NONE;
-
- // Proceed according to the square delta between the source and
- // destionation squares.
- switch(to - from) {
-
- case DELTA_NW: case DELTA_NE:
- // Capture. The destination square must be occupied by an enemy piece
- // (en passant captures was handled earlier).
- if(pos.color_of_piece_on(to) != them)
- return MOVE_NONE;
- break;
-
- case DELTA_N:
- // Pawn push. The destination square must be empty.
- if(!pos.square_is_empty(to))
- return MOVE_NONE;
- break;
+ Bitboard b, target;
+ Square from, checksq;
+ int checkersCnt = 0;
+ Color us = pos.side_to_move();
+ Square ksq = pos.king_square(us);
+ Bitboard checkers = pos.checkers();
+ Bitboard sliderAttacks = EmptyBoardBB;
+
+ assert(pos.piece_on(ksq) == make_piece(us, KING));
+ assert(checkers);
+
+ // Find squares attacked by slider checkers, we will remove
+ // them from the king evasions set so to early skip known
+ // illegal moves and avoid an useless legality check later.
+ b = checkers;
+ do
+ {
+ checkersCnt++;
+ checksq = pop_1st_bit(&b);
+
+ assert(piece_color(pos.piece_on(checksq)) == opposite_color(us));
+
+ switch (piece_type(pos.piece_on(checksq)))
+ {
+ case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
+ case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
+ case QUEEN:
+ // If queen and king are far we can safely remove all the squares attacked
+ // in the other direction becuase are not reachable by the king anyway.
+ if (squares_between(ksq, checksq) || (RookPseudoAttacks[checksq] & (1ULL << ksq)))
+ sliderAttacks |= QueenPseudoAttacks[checksq];
+
+ // Otherwise, if king and queen are adjacent and on a diagonal line, we need to
+ // use real rook attacks to check if king is safe to move in the other direction.
+ // For example: king in B2, queen in A1 a knight in B1, and we can safely move to C1.
+ else
+ sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);