+
+ return *this;
+}
+
+
+/// Position::set_castling_right() is a helper function used to set castling
+/// rights given the corresponding color and the rook starting square.
+
+void Position::set_castling_right(Color c, Square rfrom) {
+
+ Square kfrom = square<KING>(c);
+ CastlingRights cr = c & (kfrom < rfrom ? KING_SIDE: QUEEN_SIDE);
+
+ st->castlingRights |= cr;
+ castlingRightsMask[kfrom] |= cr;
+ castlingRightsMask[rfrom] |= cr;
+ castlingRookSquare[cr] = rfrom;
+
+ Square kto = relative_square(c, cr & KING_SIDE ? SQ_G1 : SQ_C1);
+ Square rto = relative_square(c, cr & KING_SIDE ? SQ_F1 : SQ_D1);
+
+ castlingPath[cr] = (between_bb(rfrom, rto) | between_bb(kfrom, kto) | rto | kto)
+ & ~(square_bb(kfrom) | rfrom);
+}
+
+
+/// Position::set_check_info() sets king attacks to detect if a move gives check
+
+void Position::set_check_info(StateInfo* si) const {
+
+ si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square<KING>(WHITE), si->pinners[BLACK]);
+ si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square<KING>(BLACK), si->pinners[WHITE]);
+
+ Square ksq = square<KING>(~sideToMove);
+
+ si->checkSquares[PAWN] = attacks_from<PAWN>(ksq, ~sideToMove);
+ si->checkSquares[KNIGHT] = attacks_from<KNIGHT>(ksq);
+ si->checkSquares[BISHOP] = attacks_from<BISHOP>(ksq);
+ si->checkSquares[ROOK] = attacks_from<ROOK>(ksq);
+ si->checkSquares[QUEEN] = si->checkSquares[BISHOP] | si->checkSquares[ROOK];
+ si->checkSquares[KING] = 0;
+}
+
+
+/// Position::set_state() computes the hash keys of the position, and other
+/// data that once computed is updated incrementally as moves are made.
+/// The function is only used when a new position is set up, and to verify
+/// the correctness of the StateInfo data when running in debug mode.
+
+void Position::set_state(StateInfo* si) const {
+
+ si->key = si->materialKey = 0;
+ si->pawnKey = Zobrist::noPawns;
+ si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO;
+ si->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);
+
+ set_check_info(si);
+
+ for (Bitboard b = pieces(); b; )
+ {
+ Square s = pop_lsb(&b);
+ Piece pc = piece_on(s);
+ si->key ^= Zobrist::psq[pc][s];
+
+ if (type_of(pc) == PAWN)
+ si->pawnKey ^= Zobrist::psq[pc][s];
+
+ else if (type_of(pc) != KING)
+ si->nonPawnMaterial[color_of(pc)] += PieceValue[MG][pc];
+ }
+
+ if (si->epSquare != SQ_NONE)
+ si->key ^= Zobrist::enpassant[file_of(si->epSquare)];
+
+ if (sideToMove == BLACK)
+ si->key ^= Zobrist::side;
+
+ si->key ^= Zobrist::castling[si->castlingRights];
+
+ for (Piece pc : Pieces)
+ for (int cnt = 0; cnt < pieceCount[pc]; ++cnt)
+ si->materialKey ^= Zobrist::psq[pc][cnt];