- // Remove pawn:
- clear_bit(&(byColorBB[us]), from);
- clear_bit(&(byTypeBB[PAWN]), from);
- clear_bit(&(byTypeBB[0]), from); // HACK: byTypeBB[0] == occupied squares
- board[from] = EMPTY;
-
- // Insert promoted piece:
- promotion = move_promotion(m);
- assert(promotion >= KNIGHT && promotion <= QUEEN);
- set_bit(&(byColorBB[us]), to);
- set_bit(&(byTypeBB[promotion]), to);
- set_bit(&(byTypeBB[0]), to); // HACK: byTypeBB[0] == occupied squares
- board[to] = piece_of_color_and_type(us, promotion);
-
- // Update hash key:
- key ^= zobrist[us][PAWN][from] ^ zobrist[us][promotion][to];
-
- // Update pawn hash key:
- pawnKey ^= zobrist[us][PAWN][from];
-
- // Update material key:
- materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
- materialKey ^= zobMaterial[us][promotion][pieceCount[us][promotion]+1];
-
- // Update piece counts:
- pieceCount[us][PAWN]--;
- pieceCount[us][promotion]++;
-
- // Update piece lists:
- pieceList[us][PAWN][index[from]] =
- pieceList[us][PAWN][pieceCount[us][PAWN]];
- index[pieceList[us][PAWN][index[from]]] = index[from];
- pieceList[us][promotion][pieceCount[us][promotion] - 1] = to;
- index[to] = pieceCount[us][promotion] - 1;
-
- // Update incremental scores:
- mgValue -= this->mg_pst(us, PAWN, from);
- mgValue += this->mg_pst(us, promotion, to);
- egValue -= this->eg_pst(us, PAWN, from);
- egValue += this->eg_pst(us, promotion, to);
-
- // Update material:
- npMaterial[us] += piece_value_midgame(promotion);
-
- // Clear the en passant square:
- if(epSquare != SQ_NONE) {
- key ^= zobEp[epSquare];
- epSquare = SQ_NONE;
+ Color us = side_to_move();
+ Color them = opposite_color(us);
+ Square from = move_from(m);
+ Square to = move_to(m);
+ bool ep = move_is_ep(m);
+ bool pm = move_is_promotion(m);
+
+ PieceType pt = type_of_piece_on(to);
+
+ assert(square_is_empty(from));
+ assert(color_of_piece_on(to) == us);
+ assert(!pm || relative_rank(us, to) == RANK_8);
+ assert(!ep || to == st->previous->epSquare);
+ assert(!ep || relative_rank(us, to) == RANK_6);
+ assert(!ep || piece_on(to) == piece_of_color_and_type(us, PAWN));
+
+ if (pm) // promotion ?
+ {
+ PieceType promotion = move_promotion_piece(m);
+ pt = PAWN;
+
+ assert(promotion >= KNIGHT && promotion <= QUEEN);
+ assert(piece_on(to) == piece_of_color_and_type(us, promotion));
+
+ // Replace promoted piece with a pawn
+ clear_bit(&(byTypeBB[promotion]), to);
+ set_bit(&(byTypeBB[PAWN]), to);
+
+ // Update piece counts
+ pieceCount[us][promotion]--;
+ pieceCount[us][PAWN]++;
+
+ // Update piece list replacing promotion piece with a pawn
+ Square lastPromotionSquare = pieceList[us][promotion][pieceCount[us][promotion]];
+ index[lastPromotionSquare] = index[to];
+ pieceList[us][promotion][index[lastPromotionSquare]] = lastPromotionSquare;
+ index[to] = pieceCount[us][PAWN] - 1;
+ pieceList[us][PAWN][index[to]] = to;