]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Split calculation of pinners from dc candidates
[stockfish] / src / position.cpp
index 91fbd103a739ccfd84b5fec8717ec90b0e1b4858..ec54da9e9e3161b382120acfaadfe7c9866129fd 100644 (file)
@@ -23,8 +23,9 @@
 ////
 
 #include <cassert>
-#include <iostream>
+#include <cstring>
 #include <fstream>
+#include <iostream>
 
 #include "mersenne.h"
 #include "movegen.h"
@@ -206,6 +207,7 @@ void Position::from_fen(const std::string& fen) {
   castleRightsMask[make_square(initialQRFile, RANK_8)] ^= BLACK_OOO;
 
   find_checkers();
+  find_hidden_checks();
 
   st->key = compute_key();
   st->pawnKey = compute_pawn_key();
@@ -319,44 +321,11 @@ void Position::copy(const Position &pos) {
 }
 
 
-/// Position:pinned_pieces() returns a bitboard of all pinned (against the
-/// king) pieces for the given color.
-Bitboard Position::pinned_pieces(Color c) const {
-
-  if (st->pinned[c] != ~EmptyBoardBB)
-      return st->pinned[c];
-
-  Bitboard p1, p2;
-  Square ksq = king_square(c);
-  st->pinned[c] = hidden_checks<ROOK, true>(c, ksq, p1) | hidden_checks<BISHOP, true>(c, ksq, p2);
-  st->pinners[c] = p1 | p2;
-  return st->pinned[c];
-}
-
-Bitboard Position::pinned_pieces(Color c, Bitboard& p) const {
-
-  if (st->pinned[c] == ~EmptyBoardBB)
-      pinned_pieces(c);
-
-  p = st->pinners[c];
-  return st->pinned[c];
-}
-
-Bitboard Position::discovered_check_candidates(Color c) const {
-
-  if (st->dcCandidates[c] != ~EmptyBoardBB)
-      return st->dcCandidates[c];
-
-  Bitboard dummy;
-  Square ksq = king_square(opposite_color(c));
-  st->dcCandidates[c] = hidden_checks<ROOK, false>(c, ksq, dummy) | hidden_checks<BISHOP, false>(c, ksq, dummy);
-  return st->dcCandidates[c];
-}
-
 /// Position:hidden_checks<>() returns a bitboard of all pinned (against the
 /// king) pieces for the given color and for the given pinner type. Or, when
 /// template parameter FindPinned is false, the pinned pieces of opposite color
 /// that are, indeed, the pieces candidate for a discovery check.
+/// Note that checkersBB bitboard must be already updated.
 template<PieceType Piece, bool FindPinned>
 Bitboard Position::hidden_checks(Color c, Square ksq, Bitboard& pinners) const {
 
@@ -466,7 +435,7 @@ bool Position::move_attacks_square(Move m, Square s) const {
 
 
 /// Position::find_checkers() computes the checkersBB bitboard, which
-/// contains a nonzero bit for each checking piece (0, 1 or 2).  It
+/// contains a nonzero bit for each checking piece (0, 1 or 2). It
 /// currently works by calling Position::attacks_to, which is probably
 /// inefficient. Consider rewriting this function to use the last move
 /// played, like in non-bitboard versions of Glaurung.
@@ -477,6 +446,31 @@ 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.
+
+void Position::find_hidden_checks(Color us, unsigned int types) {
+
+  Bitboard p1, p2;
+  Color them = opposite_color(us);
+  Square ksq = king_square(them);
+  if (types & Pinned)
+  {
+      st->pinned[them] = hidden_checks<ROOK, true>(them, ksq, p1) | hidden_checks<BISHOP, true>(them, ksq, p2);
+      st->pinners[them] = p1 | p2;
+  }
+  if (types & DcCandidates)
+      st->dcCandidates[us] = hidden_checks<ROOK, false>(us, ksq, p1) | hidden_checks<BISHOP, false>(us, ksq, p2);
+}
+
+void Position::find_hidden_checks() {
+
+  for (Color c = WHITE; c <= BLACK; c++)
+      find_hidden_checks(c, Pinned | DcCandidates);
+}
+
 
 /// Position::pl_move_is_legal() tests whether a pseudo-legal move is legal
 
@@ -669,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<PieceType Piece>
 inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square from,
@@ -689,6 +684,61 @@ inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square
 }
 
 
+/// 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::update_hidden_checks(Square from, Square to) {
+
+  Color us = sideToMove;
+  Color them = opposite_color(us);
+  Square ksq = king_square(opposite_color(us));
+
+  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] | st->pinners[them]) && (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])))
+      find_hidden_checks(us, Pinned | 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])))
+  {
+
+      // If we don't have opponent dc candidates and we are moving in the
+      // attack line then won't be dc candidates also after the move.
+      if (   st->dcCandidates[them]
+          || bit_is_set(RookPseudoAttacks[ksq], from)
+          || bit_is_set(BishopPseudoAttacks[ksq], from))
+
+          find_hidden_checks(them, Pinned | DcCandidates);
+      else
+          find_hidden_checks(them, Pinned);
+  }
+}
+
+
 /// Position::do_move() makes a move, and saves all information necessary
 /// to a StateInfo object. The move is assumed to be legal.
 /// Pseudo-legal moves should be filtered out before this function is called.
@@ -698,14 +748,13 @@ void Position::do_move(Move m, StateInfo& newSt) {
   assert(is_ok());
   assert(move_is_ok(m));
 
-  // Get now the current (pre-move) dc candidates that we will use
+  // Get now the current (before to move) dc candidates that we will use
   // in update_checkers().
   Bitboard oldDcCandidates = discovered_check_candidates(side_to_move());
 
-  // Copy the old state to our new StateInfo object (except the
-  // captured piece, which is taken care of later.
-  // TODO do not copy pinners and checkersBB because are recalculated
-  // anyway.
+  // 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.
   newSt = *st;
   newSt.capture = NO_PIECE_TYPE;
   newSt.previous = st;
@@ -719,10 +768,6 @@ void Position::do_move(Move m, StateInfo& newSt) {
   // case of non-reversible moves is taken care of later.
   st->rule50++;
 
-  // Reset pinned bitboard and its friends
-  for (Color c = WHITE; c <= BLACK; c++)
-      st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB;
-
   if (move_is_castle(m))
       do_castle_move(m);
   else if (move_promotion(m))
@@ -820,6 +865,8 @@ void Position::do_move(Move m, StateInfo& newSt) {
     case KING:   update_checkers<KING>(&st->checkersBB, ksq, from, to, oldDcCandidates);   break;
     default: assert(false); break;
     }
+
+    update_hidden_checks(from, to);
   }
 
   // Finish
@@ -971,6 +1018,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();
 }
 
 
@@ -1061,6 +1111,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();
 }
 
 
@@ -1143,6 +1196,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();
 }
 
 
@@ -1490,7 +1546,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.
@@ -1527,6 +1583,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);
@@ -1559,6 +1620,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;
 
@@ -1596,7 +1668,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++)
@@ -1621,6 +1693,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)))
       {
@@ -1639,22 +1717,32 @@ 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.
 
 void Position::clear() {
 
   st = &startState;
-  st->previous = NULL; // We should never dereference this
+  memset(st, 0, sizeof(StateInfo));
+  st->epSquare = SQ_NONE;
+
+  memset(index, 0, sizeof(int) * 64);
+  memset(byColorBB, 0, sizeof(Bitboard) * 2);
 
   for (int i = 0; i < 64; i++)
-  {
       board[i] = EMPTY;
-      index[i] = 0;
-  }
-
-  for (int i = 0; i < 2; i++)
-      byColorBB[i] = EmptyBoardBB;
 
   for (int i = 0; i < 7; i++)
   {
@@ -1664,21 +1752,11 @@ void Position::clear() {
           pieceList[0][i][j] = pieceList[1][i][j] = SQ_NONE;
   }
 
-  st->checkersBB = EmptyBoardBB;
-  for (Color c = WHITE; c <= BLACK; c++)
-      st->pinners[c] = st->pinned[c] = st->dcCandidates[c] = ~EmptyBoardBB;
-
   sideToMove = WHITE;
   gamePly = 0;
   initialKFile = FILE_E;
   initialKRFile = FILE_H;
   initialQRFile = FILE_A;
-
-  st->lastMove = MOVE_NONE;
-  st->castleRights = NO_CASTLES;
-  st->epSquare = SQ_NONE;
-  st->rule50 = 0;
-  st->previous = NULL;
 }