- 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);
-
- Piece piece = piece_on(from);
- PieceType pt = type_of_piece(piece);
-
- assert(color_of_piece_on(from) == us);
- assert(color_of_piece_on(to) == them || square_is_empty(to));
- assert(!(ep || pm) || piece == piece_of_color_and_type(us, PAWN));
- assert(!pm || relative_rank(us, to) == RANK_8);
-
- st->capture = ep ? PAWN : type_of_piece_on(to);
-
- if (st->capture)
- do_capture_move(st->capture, them, to, ep);
-
- // Update hash key
- st->key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
-
- // Reset en passant square
- if (st->epSquare != SQ_NONE)
- {
- st->key ^= zobEp[st->epSquare];
- st->epSquare = SQ_NONE;
- }
-
- // Update castle rights, try to shortcut a common case
- if ((castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
- {
- st->key ^= zobCastle[st->castleRights];
- st->castleRights &= castleRightsMask[from];
- st->castleRights &= castleRightsMask[to];
- st->key ^= zobCastle[st->castleRights];
- }
-
- // Prefetch TT access as soon as we know key is updated
- TT.prefetch(st->key);
-
- // Move the piece
- Bitboard move_bb = make_move_bb(from, to);
- do_move_bb(&(byColorBB[us]), move_bb);
- do_move_bb(&(byTypeBB[pt]), move_bb);
- do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
-
- board[to] = board[from];
- board[from] = EMPTY;
-
- // If the moving piece was a pawn do some special extra work
- if (pt == PAWN)
- {
- // Reset rule 50 draw counter
- st->rule50 = 0;
-
- // Update pawn hash key
- st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
-
- // Set en passant square, only if moved pawn can be captured
- if (abs(int(to) - int(from)) == 16)
- {
- if ( (us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
- || (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
- {
- st->epSquare = Square((int(from) + int(to)) / 2);
- st->key ^= zobEp[st->epSquare];
- }
- }
- }
-
- // Update incremental scores
- st->mgValue += pst_delta<MidGame>(piece, from, to);
- st->egValue += pst_delta<EndGame>(piece, from, to);
-
- // If the moving piece was a king, update the king square
- if (pt == KING)
- kingSquare[us] = to;
-
- // Update piece lists
- pieceList[us][pt][index[from]] = to;
- index[to] = index[from];
-
- if (pm)
- {
- PieceType promotion = move_promotion_piece(m);
-
- assert(promotion >= KNIGHT && promotion <= QUEEN);
-
- // Insert promoted piece instead of pawn
- clear_bit(&(byTypeBB[PAWN]), to);
- set_bit(&(byTypeBB[promotion]), to);
- board[to] = piece_of_color_and_type(us, promotion);
-
- // Partially revert hash keys update
- st->key ^= zobrist[us][PAWN][to] ^ zobrist[us][promotion][to];
- st->pawnKey ^= zobrist[us][PAWN][to];
-
- // Update material key
- st->materialKey ^= zobMaterial[us][PAWN][pieceCount[us][PAWN]];
- st->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;
-
- // Partially revert and update incremental scores
- st->mgValue -= pst<MidGame>(us, PAWN, to);
- st->mgValue += pst<MidGame>(us, promotion, to);
- st->egValue -= pst<EndGame>(us, PAWN, to);
- st->egValue += pst<EndGame>(us, promotion, to);
-
- // Update material
- st->npMaterial[us] += piece_value_midgame(promotion);
- }
-
- // Update checkers bitboard, piece must be already moved
- if (ep || pm)
- st->checkersBB = attacks_to(king_square(them), us);
- else
- {
- st->checkersBB = EmptyBoardBB; // FIXME EP ?
- Square ksq = king_square(them);
- switch (pt)
- {
- case PAWN: update_checkers<PAWN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case KNIGHT: update_checkers<KNIGHT>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case BISHOP: update_checkers<BISHOP>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case ROOK: update_checkers<ROOK>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case QUEEN: update_checkers<QUEEN>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- case KING: update_checkers<KING>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
- default: assert(false); break;
- }
- }