- to = move_to(m);
-
- // 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;
-
- case DELTA_NN:
- // Double pawn push. The destination square must be on the fourth
- // rank, and both the destination square and the square between the
- // source and destination squares must be empty.
- if(square_rank(to) != RANK_4 || !pos.square_is_empty(to) ||
- !pos.square_is_empty(from + DELTA_N))
- return MOVE_NONE;
- break;
-
- default:
- return MOVE_NONE;
- }
- }
- else { // (us == BLACK)
- // If the destination square is on the 1st rank, the move must be a
- // promotion.
- if(square_rank(to) == RANK_1 && !move_promotion(m))
- return MOVE_NONE;
-
- // Proceed according to the square delta between the source and
- // destionation squares.
- switch(to - from) {
-
- case DELTA_SW: case DELTA_SE:
- // 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_S:
- // Pawn push. The destination square must be empty.
- if(!pos.square_is_empty(to))
- return MOVE_NONE;
- break;
-
- case DELTA_SS:
- // Double pawn push. The destination square must be on the fifth
- // rank, and both the destination square and the square between the
- // source and destination squares must be empty.
- if(square_rank(to) != RANK_5 || !pos.square_is_empty(to) ||
- !pos.square_is_empty(from + DELTA_S))
- return MOVE_NONE;
- break;
-
- default:
- return MOVE_NONE;