]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Use CheckInfo to compute dcCandidates
[stockfish] / src / position.cpp
index c43cde790a4cfd225b6ebcb3990bdf6aed81adcb..8ab712b46d1f81a0fe6cd25865f39a1ea57e3c93 100644 (file)
@@ -56,12 +56,25 @@ Score Position::PieceSquareTable[16][64];
 
 static bool RequestPending = false;
 
-////
-//// Functions
-////
 
 /// Constructors
 
+CheckInfo::CheckInfo(const Position& pos) {
+
+  Color us = pos.side_to_move();
+  Color them = opposite_color(us);
+
+  ksq = pos.king_square(them);
+  dc = pos.discovered_check_candidates(us);
+
+  checkSq[PAWN] = pos.attacks_from<PAWN>(ksq, them);
+  checkSq[KNIGHT] = pos.attacks_from<KNIGHT>(ksq);
+  checkSq[BISHOP] = pos.attacks_from<BISHOP>(ksq);
+  checkSq[ROOK] = pos.attacks_from<ROOK>(ksq);
+  checkSq[QUEEN] = checkSq[BISHOP] | checkSq[ROOK];
+  checkSq[KING] = EmptyBoardBB;
+}
+
 Position::Position(const Position& pos) {
   copy(pos);
 }
@@ -718,10 +731,10 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
   // ones which are recalculated from scratch anyway, then switch our state
   // pointer to point to the new, ready to be updated, state.
   struct ReducedStateInfo {
-    Key key, pawnKey, materialKey;
+    Key pawnKey, materialKey;
     int castleRights, rule50, pliesFromNull;
     Square epSquare;
-    Value mgValue, egValue;
+    Value value;
     Value npMaterial[2];
   };
 
@@ -758,16 +771,15 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
 
   Piece piece = piece_on(from);
   PieceType pt = type_of_piece(piece);
+  PieceType capture = ep ? PAWN : type_of_piece_on(to);
 
   assert(color_of_piece_on(from) == us);
   assert(color_of_piece_on(to) == them || square_is_empty(to));
   assert(!(ep || pm) || piece == piece_of_color_and_type(us, PAWN));
   assert(!pm || relative_rank(us, to) == RANK_8);
 
-  st->capture = ep ? PAWN : type_of_piece_on(to);
-
-  if (st->capture)
-      do_capture_move(key, st->capture, them, to, ep);
+  if (capture)
+      do_capture_move(key, capture, them, to, ep);
 
   // Update hash key
   key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
@@ -817,7 +829,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
       st->pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
 
       // Set en passant square, only if moved pawn can be captured
-      if (abs(int(to) - int(from)) == 16)
+      if ((to ^ from) == 16)
       {
           if (attacks_from<PAWN>(from + (us == WHITE ? DELTA_N : DELTA_S), us) & pieces(PAWN, them))
           {
@@ -830,6 +842,9 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
   // Update incremental scores
   st->value += pst_delta(piece, from, to);
 
+  // Set capture piece
+  st->capture = capture;
+
   if (pm) // promotion ?
   {
       PieceType promotion = move_promotion_piece(m);
@@ -894,7 +909,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
 
   // Finish
   sideToMove = opposite_color(sideToMove);
-  st->value += (sideToMove == WHITE) ?  TempoValue : -TempoValue;
+  st->value += (sideToMove == WHITE ?  TempoValue : -TempoValue);
 
   assert(is_ok());
 }
@@ -1054,7 +1069,7 @@ void Position::do_castle_move(Move m) {
 
   // Finish
   sideToMove = opposite_color(sideToMove);
-  st->value += (sideToMove == WHITE) ?  TempoValue : -TempoValue;
+  st->value += (sideToMove == WHITE ?  TempoValue : -TempoValue);
 
   assert(is_ok());
 }
@@ -1631,7 +1646,7 @@ Key Position::compute_material_key() const {
 /// updated by do_move and undo_move when the program is running in debug mode.
 Score Position::compute_value() const {
 
-  Score result(0, 0);
+  Score result = make_score(0, 0);
   Bitboard b;
   Square s;
 
@@ -1647,7 +1662,7 @@ Score Position::compute_value() const {
           }
       }
 
-  result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2;
+  result += (side_to_move() == WHITE ? TempoValue / 2 : -TempoValue / 2);
   return result;
 }
 
@@ -1796,7 +1811,7 @@ void Position::init_piece_square_tables() {
       for (Piece p = WP; p <= WK; p++)
       {
           i = (r == 0)? 0 : (genrand_int32() % (r*2) - r);
-          PieceSquareTable[p][s] = Score(MgPST[p][s] + i, EgPST[p][s] + i);
+          PieceSquareTable[p][s] = make_score(MgPST[p][s] + i, EgPST[p][s] + i);
       }
 
   for (Square s = SQ_A1; s <= SQ_H8; s++)