- // Put the piece back at the source square
- Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
- byTypeBB[ALL_PIECES] ^= from_to_bb;
- byTypeBB[pt] ^= from_to_bb;
- byColorBB[us] ^= from_to_bb;
-
- board[from] = board[to];
- board[to] = NO_PIECE;
-
- // Update piece lists, index[to] is not updated and becomes stale. This
- // works as long as index[] is accessed just by known occupied squares.
- index[from] = index[to];
- pieceList[us][pt][index[from]] = from;
+ if (type_of(m) == CASTLE)
+ {
+ bool kingSide = to > from;
+ Square rfrom = to; // Castle is encoded as "king captures friendly rook"
+ Square rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
+ to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
+ capture = NO_PIECE_TYPE;
+ pt = KING;
+ do_castle(to, from, rto, rfrom);
+ }
+ else
+ {
+ // Put the piece back at the source square
+ Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
+ byTypeBB[ALL_PIECES] ^= from_to_bb;
+ byTypeBB[pt] ^= from_to_bb;
+ byColorBB[us] ^= from_to_bb;
+
+ board[to] = NO_PIECE;
+ board[from] = make_piece(us, pt);
+
+ // Update piece lists, index[to] is not updated and becomes stale. This
+ // works as long as index[] is accessed just by known occupied squares.
+ index[from] = index[to];
+ pieceList[us][pt][index[from]] = from;
+ }