]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Retire broken SendSearchedNodes
[stockfish] / src / position.cpp
index f4f0db77547ba7bd7ea9d85c9450ed908e4c1ebb..922a96fd8bdee8e23c4df7e1382ca486985a961c 100644 (file)
@@ -30,7 +30,6 @@
 #include "rkiss.h"
 #include "thread.h"
 #include "tt.h"
-#include "ucioption.h"
 
 using std::string;
 using std::cout;
@@ -100,7 +99,6 @@ CheckInfo::CheckInfo(const Position& pos) {
 Position::Position(const Position& pos, int th) {
 
   memcpy(this, &pos, sizeof(Position));
-  detach(); // Always detach() in copy c'tor to avoid surprises
   threadID = th;
   nodes = 0;
 
@@ -114,18 +112,6 @@ Position::Position(const string& fen, bool isChess960, int th) {
 }
 
 
-/// Position::detach() copies the content of the current state and castling
-/// masks inside the position itself. This is needed when the st pointee could
-/// become stale, as example because the caller is about to going out of scope.
-
-void Position::detach() {
-
-  startState = *st;
-  st = &startState;
-  st->previous = NULL; // As a safe guard
-}
-
-
 /// Position::from_fen() initializes the position object with the given FEN
 /// string. This function is not very robust - make sure that input FENs are
 /// correct (this is assumed to be the responsibility of the GUI).
@@ -204,7 +190,11 @@ void Position::from_fen(const string& fenStr, bool isChess960) {
   }
 
   // 5-6. Halfmove clock and fullmove number
-  fen >> std::skipws >> st->rule50 >> fullMoves;
+  fen >> std::skipws >> st->rule50 >> startPosPly;
+
+  // Convert from fullmove starting from 1 to ply starting from 0,
+  // handle also common incorrect FEN with fullmove = 0.
+  startPosPly = Max(2 * (startPosPly - 1), 0) + int(sideToMove == BLACK);
 
   // Various initialisations
   chess960 = isChess960;
@@ -324,7 +314,7 @@ const string Position::to_fen() const {
       fen << '-';
 
   fen << (ep_square() == SQ_NONE ? " -" : " " + square_to_string(ep_square()))
-      << " " << st->rule50 << " " << fullMoves;
+      << " " << st->rule50 << " " << 1 + (startPosPly - int(sideToMove == BLACK)) / 2;
 
   return fen.str();
 }
@@ -773,32 +763,6 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const {
 }
 
 
-/// Position::do_setup_move() makes a permanent move on the board. It should
-/// be used when setting up a position on board. You can't undo the move.
-
-void Position::do_setup_move(Move m) {
-
-  StateInfo newSt;
-
-  // Update the number of full moves after black's move
-  if (sideToMove == BLACK)
-      fullMoves++;
-
-  do_move(m, newSt);
-
-  // Reset "game ply" in case we made a non-reversible move.
-  // "game ply" is used for repetition detection.
-  if (st->rule50 == 0)
-      st->gamePly = 0;
-
-  // Our StateInfo newSt is about going out of scope so copy
-  // its content before it disappears.
-  detach();
-
-  assert(is_ok());
-}
-
-
 /// 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.
@@ -822,10 +786,10 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
   // pointer to point to the new, ready to be updated, state.
   struct ReducedStateInfo {
     Key pawnKey, materialKey;
-    int castleRights, rule50, gamePly, pliesFromNull;
-    Square epSquare;
-    Score value;
     Value npMaterial[2];
+    int castleRights, rule50, pliesFromNull;
+    Score value;
+    Square epSquare;
   };
 
   memcpy(&newSt, st, sizeof(ReducedStateInfo));
@@ -833,10 +797,6 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
   newSt.previous = st;
   st = &newSt;
 
-  // Save the current key to the history[] array, in order to be able to
-  // detect repetition draws.
-  history[st->gamePly++] = key;
-
   // Update side to move
   key ^= zobSideToMove;
 
@@ -1368,10 +1328,6 @@ void Position::do_null_move(StateInfo& backupSt) {
   backupSt.pliesFromNull = st->pliesFromNull;
   st->previous = &backupSt;
 
-  // Save the current key to the history[] array, in order to be able to
-  // detect repetition draws.
-  history[st->gamePly++] = st->key;
-
   // Update the necessary information
   if (st->epSquare != SQ_NONE)
       st->key ^= zobEp[st->epSquare];
@@ -1406,7 +1362,6 @@ void Position::undo_null_move() {
   // Update the necessary information
   sideToMove = opposite_color(sideToMove);
   st->rule50--;
-  st->gamePly--;
 
   assert(is_ok());
 }
@@ -1557,7 +1512,6 @@ void Position::clear() {
       castleRightsMask[sq] = ALL_CASTLES;
   }
   sideToMove = WHITE;
-  fullMoves = 1;
   nodes = 0;
 }
 
@@ -1698,9 +1652,24 @@ bool Position::is_draw() const {
 
   // Draw by repetition?
   if (!SkipRepetition)
-      for (int i = 4, e = Min(Min(st->gamePly, st->rule50), st->pliesFromNull); i <= e; i += 2)
-          if (history[st->gamePly - i] == st->key)
-              return true;
+  {
+      int i = 4, e = Min(st->rule50, st->pliesFromNull);
+
+      if (i <= e)
+      {
+          StateInfo* stp = st->previous->previous;
+
+          do {
+              stp = stp->previous->previous;
+
+              if (stp->key == st->key)
+                  return true;
+
+              i +=2;
+
+          } while (i <= e);
+      }
+  }
 
   return false;
 }