X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=e6125ec7c2c756816bc098014d132fa04900ae03;hp=cc0f42e9c9ac1b004938a29968b554001e75c9f6;hb=9b6b9e67fe767a83837ff4cbf3954a8cf70eee59;hpb=243fa483d7b329ad8512c86eb152cca7a0982674 diff --git a/src/position.cpp b/src/position.cpp index cc0f42e9..e6125ec7 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -207,7 +207,7 @@ void Position::from_fen(const std::string& fen) { castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO; find_checkers(); - find_pinned(); + find_hidden_checks(); st->key = compute_key(); st->pawnKey = compute_pawn_key(); @@ -446,23 +446,29 @@ void Position::find_checkers() { st->checkersBB = attacks_to(king_square(us), opposite_color(us)); } +/// Position:find_hidden_checks() computes the pinned, pinners and dcCandidates +/// bitboards. There are two versions of this function. One takes a color and +/// computes bitboards relative to that color only, the other computes both +/// colors. Bitboard checkersBB must be already updated. -/// Position:find_pinned() computes the pinned, pinners and dcCandidates -/// bitboards for both colors. Bitboard checkersBB must be already updated. - -void Position::find_pinned() { +void Position::find_hidden_checks(Color us, unsigned int types) { Bitboard p1, p2; - Square ksq; - - for (Color c = WHITE; c <= BLACK; c++) + Color them = opposite_color(us); + Square ksq = king_square(them); + if (types & Pinned) { - ksq = king_square(c); - st->pinned[c] = hidden_checks(c, ksq, p1) | hidden_checks(c, ksq, p2); - st->pinners[c] = p1 | p2; - ksq = king_square(opposite_color(c)); - st->dcCandidates[c] = hidden_checks(c, ksq, p1) | hidden_checks(c, ksq, p2); + st->pinned[them] = hidden_checks(them, ksq, p1) | hidden_checks(them, ksq, p2); + st->pinners[them] = p1 | p2; } + if (types & DcCandidates) + st->dcCandidates[us] = hidden_checks(us, ksq, p1) | hidden_checks(us, ksq, p2); +} + +void Position::find_hidden_checks() { + + for (Color c = WHITE; c <= BLACK; c++) + find_hidden_checks(c, Pinned | DcCandidates); } @@ -657,7 +663,8 @@ bool Position::move_is_capture(Move m) const { } -/// Position::update_checkers() is a private method to udpate chekers info +/// Position::update_checkers() udpates chekers info given the move. It is called +/// in do_move() and is faster then find_checkers(). template inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square from, @@ -677,22 +684,72 @@ inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square } -/// Position::init_new_state() copies from the current state the fields -/// that will be updated incrementally, skips the fields, like bitboards -/// that will be recalculated form scratch anyway. +/// Position::update_hidden_checks() udpates pinned, pinners and dcCandidates +/// bitboards incrementally, given the move. It is called in do_move and is +/// faster then find_hidden_checks(). -void Position::init_new_state(StateInfo& newSt) { +void Position::update_hidden_checks(Square from, Square to) { + + Color us = sideToMove; + Color them = opposite_color(us); + Square ksq = king_square(opposite_color(us)); - newSt.key = st->key; - newSt.pawnKey = st->pawnKey; - newSt.materialKey = st->materialKey; - newSt.castleRights = st->castleRights; - newSt.rule50 = st->rule50; - newSt.epSquare = st->epSquare; - newSt.mgValue = st->mgValue; - newSt.egValue = st->egValue; - newSt.capture = NO_PIECE_TYPE; - newSt.previous = st; + Bitboard moveSquares = EmptyBoardBB; + set_bit(&moveSquares, from); + set_bit(&moveSquares, to); + + // Our moving piece could have been a possible pinner or hidden checker behind a dcCandidates? + bool checkerMoved = (st->dcCandidates[us] || bit_is_set(st->pinners[them], from)) && (moveSquares & sliders()); + + // If we are moving from/to an opponent king attack direction and we was a possible hidden checker + // or there exsist some possible hidden checker on that line then recalculate the position + // otherwise skip because our dcCandidates and opponent pinned pieces are not changed. + if ( (moveSquares & RookPseudoAttacks[ksq]) && (checkerMoved || (rooks_and_queens(us) & RookPseudoAttacks[ksq])) + || (moveSquares & BishopPseudoAttacks[ksq]) && (checkerMoved || (bishops_and_queens(us) & BishopPseudoAttacks[ksq]))) + { + // If the move gives direct check and we don't have pinners/dc cadidates + // then we can be sure that we won't have them also after the move if + // we are not moving from a possible king attack direction. + bool outsideChecker = false; + + if ( bit_is_set(st->checkersBB, to) + && !(bit_is_set(RookPseudoAttacks[ksq], from) && (checkerMoved || (rooks_and_queens(us) & RookPseudoAttacks[ksq]))) + && !(bit_is_set(BishopPseudoAttacks[ksq], from) && (checkerMoved || (bishops_and_queens(us) & BishopPseudoAttacks[ksq])))) + outsideChecker = true; + + if (!outsideChecker || st->pinned[them]) + find_hidden_checks(us, Pinned); + + if (!outsideChecker || st->dcCandidates[us] || bit_is_set(st->pinned[them], to)) + find_hidden_checks(us, DcCandidates); + } + + ksq = king_square(us); + + if (ksq == to) + { + find_hidden_checks(them, Pinned | DcCandidates); + return; + } + + // It is possible that we have captured an opponent hidden checker? + Bitboard checkerCaptured = st->capture && (st->dcCandidates[them] || bit_is_set(st->pinners[us], to)); + + // If we are moving from/to an our king attack direction and there was/is some possible + // opponent hidden checker then calculate the position otherwise skip because opponent + // dcCandidates and our pinned pieces are not changed. + if ( (moveSquares & RookPseudoAttacks[ksq]) && (checkerCaptured || (rooks_and_queens(them) & RookPseudoAttacks[ksq])) + || (moveSquares & BishopPseudoAttacks[ksq]) && (checkerCaptured || (bishops_and_queens(them) & BishopPseudoAttacks[ksq]))) + { + find_hidden_checks(them, Pinned); + + // If we don't have opponent dc candidates and we are moving in the + // attack line then won't be any dc candidates also after the move. + if ( st->dcCandidates[them] + || (bit_is_set(RookPseudoAttacks[ksq], from) && (rooks_and_queens(them) & RookPseudoAttacks[ksq])) + || (bit_is_set(BishopPseudoAttacks[ksq], from) && (bishops_and_queens(them) & BishopPseudoAttacks[ksq]))) + find_hidden_checks(them, DcCandidates); + } } @@ -712,7 +769,9 @@ void Position::do_move(Move m, StateInfo& newSt) { // Copy some fields of old state to our new StateInfo object (except the // captured piece, which is taken care of later) and switch state pointer // to point to the new, ready to be updated, state. - init_new_state(newSt); + newSt = *st; + newSt.capture = NO_PIECE_TYPE; + newSt.previous = st; st = &newSt; // Save the current key to the history[] array, in order to be able to @@ -820,10 +879,11 @@ void Position::do_move(Move m, StateInfo& newSt) { case KING: update_checkers(&st->checkersBB, ksq, from, to, oldDcCandidates); break; default: assert(false); break; } + + update_hidden_checks(from, to); } // Finish - find_pinned(); st->key ^= zobSideToMove; sideToMove = opposite_color(sideToMove); gamePly++; @@ -972,6 +1032,9 @@ void Position::do_castle_move(Move m) { // Update checkers BB st->checkersBB = attacks_to(king_square(them), us); + + // Update hidden checks + find_hidden_checks(); } @@ -1062,6 +1125,9 @@ void Position::do_promotion_move(Move m) { // Update checkers BB st->checkersBB = attacks_to(king_square(them), us); + + // Update hidden checks + find_hidden_checks(); } @@ -1144,6 +1210,9 @@ void Position::do_ep_move(Move m) { // Update checkers BB st->checkersBB = attacks_to(king_square(them), us); + + // Update hidden checks + find_hidden_checks(); } @@ -1491,7 +1560,7 @@ void Position::undo_null_move() { /// Position::see() is a static exchange evaluator: It tries to estimate the -/// material gain or loss resulting from a move. There are three versions of +/// material gain or loss resulting from a move. There are three versions of /// this function: One which takes a destination square as input, one takes a /// move, and one which takes a 'from' and a 'to' square. The function does /// not yet understand promotions captures. @@ -1528,6 +1597,11 @@ int Position::see(Square from, Square to) const { Color us = (from != SQ_NONE ? color_of_piece_on(from) : opposite_color(color_of_piece_on(to))); Color them = opposite_color(us); + // Initialize pinned and pinners bitboards + Bitboard pinned[2], pinners[2]; + pinned[us] = pinned_pieces(us, pinners[us]); + pinned[them] = pinned_pieces(them, pinners[them]); + // Initialize pieces Piece piece = piece_on(from); Piece capture = piece_on(to); @@ -1560,6 +1634,17 @@ int Position::see(Square from, Square to) const { | (pawn_attacks(WHITE, to) & pawns(BLACK)) | (pawn_attacks(BLACK, to) & pawns(WHITE)); + // Remove our pinned pieces from attacks if the captured piece is not + // a pinner, otherwise we could remove a valid "capture the pinner" attack. + if (pinned[us] != EmptyBoardBB && !bit_is_set(pinners[us], to)) + attackers &= ~pinned[us]; + + // Remove opponent pinned pieces from attacks if the moving piece is not + // a pinner, otherwise we could remove a piece that is no more pinned + // due to our pinner piece is moving away. + if (pinned[them] != EmptyBoardBB && !bit_is_set(pinners[them], from)) + attackers &= ~pinned[them]; + if (from != SQ_NONE) break; @@ -1597,7 +1682,7 @@ int Position::see(Square from, Square to) const { swapList[0] = seeValues[capture]; do { - // Locate the least valuable attacker for the side to move. The loop + // Locate the least valuable attacker for the side to move. The loop // below looks like it is potentially infinite, but it isn't. We know // that the side to move still has at least one attacker left. for (pt = PAWN; !(attackers & pieces_of_color_and_type(c, pt)); pt++) @@ -1622,6 +1707,12 @@ int Position::see(Square from, Square to) const { lastCapturingPieceValue = seeValues[pt]; c = opposite_color(c); + // Remove pinned pieces from attackers + if ( pinned[c] != EmptyBoardBB + && !bit_is_set(pinners[c], to) + && !(pinners[c] & attackers)) + attackers &= ~pinned[c]; + // Stop after a king capture if (pt == KING && (attackers & pieces_of_color(c))) { @@ -1640,6 +1731,18 @@ int Position::see(Square from, Square to) const { } +/// Position::setStartState() copies the content of the argument +/// inside startState and makes st point to it. This is needed +/// when the st pointee could become stale, as example because +/// the caller is about to going out of scope. + +void Position::setStartState(const StateInfo& s) { + + startState = s; + st = &startState; +} + + /// Position::clear() erases the position object to a pristine state, with an /// empty board, white to move, and no castling rights.