]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Move kingSquare[] array to StateInfo
[stockfish] / src / position.cpp
index 770d47f5d17026ef0e0d0f53c7dabfcc502b4354..46daf8f3b1ab407dca20075dac0d46400b8de520 100644 (file)
@@ -479,10 +479,9 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
 
   Color us = side_to_move();
   Square from = move_from(m);
-  Square ksq = king_square(us);
 
   assert(color_of_piece_on(from) == us);
-  assert(piece_on(ksq) == piece_of_color_and_type(us, KING));
+  assert(piece_on(king_square(us)) == piece_of_color_and_type(us, KING));
 
   // En passant captures are a tricky special case.  Because they are
   // rather uncommon, we do it simply by testing whether the king is attacked
@@ -493,6 +492,7 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
       Square to = move_to(m);
       Square capsq = make_square(square_file(to), square_rank(from));
       Bitboard b = occupied_squares();
+      Square ksq = king_square(us);
 
       assert(to == ep_square());
       assert(piece_on(from) == piece_of_color_and_type(us, PAWN));
@@ -509,14 +509,14 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
 
   // If the moving piece is a king, check whether the destination
   // square is attacked by the opponent.
-  if (from == ksq)
+  if (type_of_piece_on(from) == KING)
       return !(square_is_attacked(move_to(m), opposite_color(us)));
 
   // A non-king move is legal if and only if it is not pinned or it
   // is moving along the ray towards or away from the king.
   return (   !pinned
           || !bit_is_set(pinned, from)
-          || (direction_between_squares(from, ksq) == direction_between_squares(move_to(m), ksq)));
+          || (direction_between_squares(from, king_square(us)) == direction_between_squares(move_to(m), king_square(us))));
 }
 
 
@@ -667,7 +667,7 @@ inline void Position::update_checkers(Bitboard* pCheckersBB, Square ksq, Square
 
   else if (   Piece != KING
            && !Slider
-           && bit_is_set(piece_attacks<Piece>(ksq), to))
+           && bit_is_set(Piece == PAWN ? pawn_attacks(opposite_color(sideToMove), ksq) : piece_attacks<Piece>(ksq), to))
       set_bit(pCheckersBB, to);
 
   // Discovery checks
@@ -704,7 +704,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
   struct ReducedStateInfo {
     Key key, pawnKey, materialKey;
     int castleRights, rule50;
-    Square epSquare;
+    Square kingSquare[2], epSquare;
     Value mgValue, egValue;
     Value npMaterial[2];
   };
@@ -786,7 +786,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
 
   // If the moving piece was a king, update the king square
   if (pt == KING)
-      kingSquare[us] = to;
+      st->kingSquare[us] = to;
 
   // Update piece lists, note that index[from] is not updated and
   // becomes stale. This works as long as index[] is accessed just
@@ -920,16 +920,15 @@ void Position::do_capture_move(Bitboard& key, PieceType capture, Color them, Squ
     // Update hash key
     key ^= zobrist[them][capture][capsq];
 
-    // If the captured piece was a pawn, update pawn hash key
-    if (capture == PAWN)
-        st->pawnKey ^= zobrist[them][PAWN][capsq];
-
     // Update incremental scores
     st->mgValue -= pst<MidGame>(them, capture, capsq);
     st->egValue -= pst<EndGame>(them, capture, capsq);
 
-    // Update material
-    if (capture != PAWN)
+    // If the captured piece was a pawn, update pawn hash key,
+    // otherwise update non-pawn material.
+    if (capture == PAWN)
+        st->pawnKey ^= zobrist[them][PAWN][capsq];
+    else
         st->npMaterial[them] -= piece_value_midgame(capture);
 
     // Update material hash key
@@ -1007,7 +1006,7 @@ void Position::do_castle_move(Move m) {
   board[rto] = rook;
 
   // Update king square
-  kingSquare[us] = kto;
+  st->kingSquare[us] = kto;
 
   // Update piece lists
   pieceList[us][KING][index[kfrom]] = kto;
@@ -1120,10 +1119,6 @@ void Position::undo_move(Move m) {
   board[from] = piece_of_color_and_type(us, pt);
   board[to] = EMPTY;
 
-  // If the moving piece was a king, update the king square
-  if (pt == KING)
-      kingSquare[us] = from;
-
   // Update piece list
   index[from] = index[to];
   pieceList[us][pt][index[from]] = from;
@@ -1209,9 +1204,6 @@ void Position::undo_castle_move(Move m) {
   board[rfrom] = piece_of_color_and_type(us, ROOK);
   board[kfrom] = piece_of_color_and_type(us, KING);
 
-  // Update king square
-  kingSquare[us] = kfrom;
-
   // Update piece lists
   pieceList[us][KING][index[kto]] = kfrom;
   pieceList[us][ROOK][index[rto]] = rfrom;
@@ -1529,7 +1521,7 @@ void Position::put_piece(Piece p, Square s) {
   pieceCount[c][pt]++;
 
   if (pt == KING)
-      kingSquare[c] = s;
+      st->kingSquare[c] = s;
 }