-void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveIsCheck) {
-
- assert(is_ok(m));
- assert(&newSt != st);
-
- ++nodes;
- Key k = st->key;
-
- // Copy some fields of the old state to our new StateInfo object except the
- // ones which are going to be recalculated from scratch anyway and then switch
- // our state pointer to point to the new (ready to be updated) state.
- std::memcpy(&newSt, st, StateCopySize64 * sizeof(uint64_t));
-
- newSt.previous = st;
- st = &newSt;
-
- // Update side to move
- k ^= Zobrist::side;
-
- // Increment ply counters. In particular, rule50 will be reset to zero later on
- // in case of a capture or a pawn move.
- ++gamePly;
- ++st->rule50;
- ++st->pliesFromNull;
-
- Color us = sideToMove;
- Color them = ~us;
- Square from = from_sq(m);
- Square to = to_sq(m);
- Piece pc = piece_on(from);
- PieceType pt = type_of(pc);
- PieceType captured = type_of(m) == ENPASSANT ? PAWN : type_of(piece_on(to));
-
- assert(color_of(pc) == us);
- assert(piece_on(to) == NO_PIECE || color_of(piece_on(to)) == them || type_of(m) == CASTLING);
- assert(captured != KING);
-
- if (type_of(m) == CASTLING)
- {
- assert(pc == make_piece(us, KING));
-
- bool kingSide = to > from;
- Square rfrom = to; // Castling 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);
- captured = NO_PIECE_TYPE;
-
- do_castling(from, to, rfrom, rto);
-
- st->psq += psq[us][ROOK][rto] - psq[us][ROOK][rfrom];
- k ^= Zobrist::psq[us][ROOK][rfrom] ^ Zobrist::psq[us][ROOK][rto];
- }
-
- if (captured)
- {
- Square capsq = to;
-
- // If the captured piece is a pawn, update pawn hash key, otherwise
- // update non-pawn material.
- if (captured == PAWN)
- {
- if (type_of(m) == ENPASSANT)
- {
- capsq += pawn_push(them);
-
- assert(pt == PAWN);
- assert(to == st->epSquare);
- assert(relative_rank(us, to) == RANK_6);
- assert(piece_on(to) == NO_PIECE);
- assert(piece_on(capsq) == make_piece(them, PAWN));
-
- board[capsq] = NO_PIECE;
- }
-
- st->pawnKey ^= Zobrist::psq[them][PAWN][capsq];
- }
- else
- st->npMaterial[them] -= PieceValue[MG][captured];
-
- // Update board and piece lists
- remove_piece(capsq, them, captured);
-
- // Update material hash key and prefetch access to materialTable
- k ^= Zobrist::psq[them][captured][capsq];
- st->materialKey ^= Zobrist::psq[them][captured][pieceCount[them][captured]];
- prefetch((char*)thisThread->materialTable[st->materialKey]);
-
- // Update incremental scores
- st->psq -= psq[them][captured][capsq];
-
- // Reset rule 50 counter
- st->rule50 = 0;
- }
-
- // Update hash key
- k ^= Zobrist::psq[us][pt][from] ^ Zobrist::psq[us][pt][to];
-
- // Reset en passant square
- if (st->epSquare != SQ_NONE)
- {
- k ^= Zobrist::enpassant[file_of(st->epSquare)];
- st->epSquare = SQ_NONE;
- }
-
- // Update castling rights if needed
- if (st->castlingRights && (castlingRightsMask[from] | castlingRightsMask[to]))
- {
- int cr = castlingRightsMask[from] | castlingRightsMask[to];
- k ^= Zobrist::castling[st->castlingRights & cr];
- st->castlingRights &= ~cr;
- }
-
- // Prefetch TT access as soon as we know the new hash key
- prefetch((char*)TT.first_entry(k));
-
- // Move the piece. The tricky Chess960 castling is handled earlier
- if (type_of(m) != CASTLING)
- move_piece(from, to, us, pt);
-
- // If the moving piece is a pawn do some special extra work
- if (pt == PAWN)
- {
- // Set en-passant square if the moved pawn can be captured
- if ( (int(to) ^ int(from)) == 16
- && (attacks_from<PAWN>(from + pawn_push(us), us) & pieces(them, PAWN)))
- {
- st->epSquare = Square((from + to) / 2);
- k ^= Zobrist::enpassant[file_of(st->epSquare)];
- }
-
- if (type_of(m) == PROMOTION)
- {
- PieceType promotion = promotion_type(m);
-
- assert(relative_rank(us, to) == RANK_8);
- assert(promotion >= KNIGHT && promotion <= QUEEN);
-
- remove_piece(to, us, PAWN);
- put_piece(to, us, promotion);
-
- // Update hash keys
- k ^= Zobrist::psq[us][PAWN][to] ^ Zobrist::psq[us][promotion][to];
- st->pawnKey ^= Zobrist::psq[us][PAWN][to];
- st->materialKey ^= Zobrist::psq[us][promotion][pieceCount[us][promotion]-1]
- ^ Zobrist::psq[us][PAWN][pieceCount[us][PAWN]];
-
- // Update incremental score
- st->psq += psq[us][promotion][to] - psq[us][PAWN][to];
-
- // Update material
- st->npMaterial[us] += PieceValue[MG][promotion];
- }
-
- // Update pawn hash key and prefetch access to pawnsTable
- st->pawnKey ^= Zobrist::psq[us][PAWN][from] ^ Zobrist::psq[us][PAWN][to];
- prefetch((char*)thisThread->pawnsTable[st->pawnKey]);
-
- // Reset rule 50 draw counter
- st->rule50 = 0;
- }
-
- // Update incremental scores
- st->psq += psq[us][pt][to] - psq[us][pt][from];
-
- // Set capture piece
- st->capturedType = captured;
-
- // Update the key with the final value
- st->key = k;
-
- // Update checkers bitboard: piece must be already moved
- st->checkersBB = 0;
-
- if (moveIsCheck)
- {
- if (type_of(m) != NORMAL)
- st->checkersBB = attackers_to(king_square(them)) & pieces(us);
- else
- {
- // Direct checks
- if (ci.checkSq[pt] & to)
- st->checkersBB |= to;
-
- // Discovered checks
- if (ci.dcCandidates && (ci.dcCandidates & from))
- {
- if (pt != ROOK)
- st->checkersBB |= attacks_from<ROOK>(king_square(them)) & pieces(us, QUEEN, ROOK);
-
- if (pt != BISHOP)
- st->checkersBB |= attacks_from<BISHOP>(king_square(them)) & pieces(us, QUEEN, BISHOP);
- }
- }
- }
-
- sideToMove = ~sideToMove;
-
- assert(pos_is_ok());