]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Generate pseudo-legal moves in generate_evasions()
[stockfish] / src / movegen.cpp
index cab7c243cf3eb414aada89300f6f9456f7574bd7..0af95e2d34f0b5cc7320201a9afe1f456788bb87 100644 (file)
@@ -52,9 +52,6 @@ namespace {
     EVASION
   };
 
-  // Functions
-  bool castling_is_check(const Position&, CastlingSide);
-
   // Helper templates
   template<CastlingSide Side>
   MoveStack* generate_castle_moves(const Position&, MoveStack*);
@@ -190,153 +187,119 @@ MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bi
   mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
   mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
   mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
-  mlist = generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
-
-  // Castling moves that give check. Very rare but nice to have!
-  if (   pos.can_castle_queenside(us)
-      && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
-      && castling_is_check(pos, QUEEN_SIDE))
-      mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
-
-  if (   pos.can_castle_kingside(us)
-      && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
-      && castling_is_check(pos, KING_SIDE))
-      mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
-
-  return mlist;
+  return  generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
 }
 
 
-/// generate_evasions() generates all check evasions when the side to move is
-/// in check. Unlike the other move generation functions, this one generates
-/// only legal moves. Returns a pointer to the end of the move list.
+/// generate_evasions() generates all pseudo-legal check evasions when
+/// the side to move is in check. Returns a pointer to the end of the move list.
 
 MoveStack* generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
 
   assert(pos.is_ok());
   assert(pos.is_check());
 
-  Square from, to;
+  Bitboard b;
+  Square from, to, checksq;
+  int checkersCnt = 0;
   Color us = pos.side_to_move();
   Color them = opposite_color(us);
   Square ksq = pos.king_square(us);
-  Bitboard sliderAttacks = EmptyBoardBB;
   Bitboard checkers = pos.checkers();
+  Bitboard sliderAttacks = EmptyBoardBB;
 
   assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
-
-  // The bitboard of occupied pieces without our king
-  Bitboard b_noKing = pos.occupied_squares();
-  clear_bit(&b_noKing, ksq);
+  assert(checkers);
 
   // Find squares attacked by slider checkers, we will remove
-  // them from the king evasions set so to avoid a couple
-  // of cycles in the slow king evasions legality check loop
-  // and to be able to use attackers_to().
-  Bitboard b = checkers & pos.pieces(BISHOP, QUEEN);
-  while (b)
+  // them from the king evasions set so to early skip known
+  // illegal moves and avoid an useless legality check later.
+  b = checkers;
+  do
   {
-      from = pop_1st_bit(&b);
-      sliderAttacks |= bishop_attacks_bb(from, b_noKing);
-  }
-
-  b = checkers & pos.pieces(ROOK, QUEEN);
-  while (b)
-  {
-      from = pop_1st_bit(&b);
-      sliderAttacks |= rook_attacks_bb(from, b_noKing);
-  }
-
-  // Generate evasions for king, capture and non capture moves
-  Bitboard enemy = pos.pieces_of_color(them);
-  Bitboard b1 = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
-  while (b1)
-  {
-      // Note that we can use attackers_to() only because we have already
-      // removed from b1 the squares attacked by slider checkers.
-      to = pop_1st_bit(&b1);
-      if (!(pos.attackers_to(to) & enemy))
-          (*mlist++).move = make_move(ksq, to);
-  }
-
-  // Generate evasions for other pieces only if not double check. We use a
-  // simple bit twiddling hack here rather than calling count_1s in order to
-  // save some time (we know that pos.checkers() has at most two nonzero bits).
-  if (!(checkers & (checkers - 1))) // Only one bit set?
-  {
-      Square checksq = first_1(checkers);
+      checkersCnt++;
+      checksq = pop_1st_bit(&b);
 
       assert(pos.color_of_piece_on(checksq) == them);
 
-      // Generate captures of the checking piece
-
-      // Pawn captures
-      b1 = pos.attacks_from<PAWN>(checksq, them) & pos.pieces(PAWN, us) & ~pinned;
-      while (b1)
+      switch (pos.type_of_piece_on(checksq))
       {
-          from = pop_1st_bit(&b1);
-          if (relative_rank(us, checksq) == RANK_8)
-          {
-              (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
-              (*mlist++).move = make_promotion_move(from, checksq, ROOK);
-              (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
-              (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
-          } else
-              (*mlist++).move = make_move(from, checksq);
+      case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
+      case ROOK:   sliderAttacks |= RookPseudoAttacks[checksq];   break;
+      case QUEEN:
+          // In case of a queen remove also squares attacked in the other direction to
+          // avoid possible illegal moves when queen and king are on adjacent squares.
+          if (direction_is_straight(checksq, ksq))
+              sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
+          else
+              sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
+      default:
+          break;
       }
+  } while (b);
+
+  // Generate evasions for king, capture and non capture moves
+  b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
+  from = ksq;
+  SERIALIZE_MOVES(b);
 
-      // Pieces captures
-      b1 = (  (pos.attacks_from<KNIGHT>(checksq) & pos.pieces(KNIGHT, us))
-            | (pos.attacks_from<BISHOP>(checksq) & pos.pieces(BISHOP, QUEEN, us))
-            | (pos.attacks_from<ROOK>(checksq)   & pos.pieces(ROOK, QUEEN, us)) ) & ~pinned;
+  // Generate evasions for other pieces only if not double check
+  if (checkersCnt > 1)
+      return mlist;
 
-      while (b1)
+  Bitboard target = squares_between(checksq, ksq);
+
+  // Pawn captures
+  b = pos.attacks_from<PAWN>(checksq, them) & pos.pieces(PAWN, us) & ~pinned;
+  while (b)
+  {
+      from = pop_1st_bit(&b);
+      if (relative_rank(us, checksq) == RANK_8)
       {
-          from = pop_1st_bit(&b1);
+          (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
+          (*mlist++).move = make_promotion_move(from, checksq, ROOK);
+          (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
+          (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
+      } else
           (*mlist++).move = make_move(from, checksq);
-      }
+  }
 
-      // Blocking check evasions are possible only if the checking piece is a slider
-      if (sliderAttacks)
-      {
-          Bitboard blockSquares = squares_between(checksq, ksq);
-
-          assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
-
-          if (blockSquares)
-          {
-              mlist = generate_piece_evasions<PAWN>(pos, mlist, us, blockSquares, pinned);
-              mlist = generate_piece_evasions<KNIGHT>(pos, mlist, us, blockSquares, pinned);
-              mlist = generate_piece_evasions<BISHOP>(pos, mlist, us, blockSquares, pinned);
-              mlist = generate_piece_evasions<ROOK>(pos, mlist, us, blockSquares, pinned);
-              mlist = generate_piece_evasions<QUEEN>(pos, mlist, us, blockSquares, pinned);
-          }
-      }
+  // Pawn blocking evasions (possible only if the checking piece is a slider)
+  if (sliderAttacks)
+      mlist = generate_piece_evasions<PAWN>(pos, mlist, us, target, pinned);
+
+  // Add the checking piece to the target squares
+  target |= checkers;
+
+  // Captures and blocking evasions for the other pieces
+  mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
+  mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
+  mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
+  mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
+
+  // Finally, the special case of en passant captures. An en passant
+  // capture can only be a check evasion if the check is not a discovered
+  // check. If pos.ep_square() is set, the last move made must have been
+  // a double pawn push. If, furthermore, the checking piece is a pawn,
+  // an en passant check evasion may be possible.
+  if (pos.ep_square() != SQ_NONE && (checkers & pos.pieces(PAWN, them)))
+  {
+      to = pos.ep_square();
+      b = pos.attacks_from<PAWN>(to, them) & pos.pieces(PAWN, us);
+
+      // The checking pawn cannot be a discovered (bishop) check candidate
+      // otherwise we were in check also before last double push move.
+      assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
+      assert(count_1s(b) == 1 || count_1s(b) == 2);
 
-      // Finally, the special case of en passant captures. An en passant
-      // capture can only be a check evasion if the check is not a discovered
-      // check. If pos.ep_square() is set, the last move made must have been
-      // a double pawn push. If, furthermore, the checking piece is a pawn,
-      // an en passant check evasion may be possible.
-      if (pos.ep_square() != SQ_NONE && (checkers & pos.pieces(PAWN, them)))
+      b &= ~pinned;
+      while (b)
       {
-          to = pos.ep_square();
-          b1 = pos.attacks_from<PAWN>(to, them) & pos.pieces(PAWN, us);
-
-          // The checking pawn cannot be a discovered (bishop) check candidate
-          // otherwise we were in check also before last double push move.
-          assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
-          assert(count_1s(b1) == 1 || count_1s(b1) == 2);
-
-          b1 &= ~pinned;
-          while (b1)
-          {
-              from = pop_1st_bit(&b1);
-              // Move is always legal because checking pawn is not a discovered
-              // check candidate and our capturing pawn has been already tested
-              // against pinned pieces.
-              (*mlist++).move = make_ep_move(from, to);
-          }
+          from = pop_1st_bit(&b);
+          // Move is always legal because checking pawn is not a discovered
+          // check candidate and our capturing pawn has been already tested
+          // against pinned pieces.
+          (*mlist++).move = make_ep_move(from, to);
       }
   }
   return mlist;
@@ -351,14 +314,16 @@ MoveStack* generate_moves(const Position& pos, MoveStack* mlist, bool pseudoLega
 
   assert(pos.is_ok());
 
+  MoveStack* last;
   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
 
-  if (pos.is_check())
-      return generate_evasions(pos, mlist, pinned);
-
   // Generate pseudo-legal moves
-  MoveStack* last = generate_captures(pos, mlist);
-  last = generate_noncaptures(pos, last);
+  if (pos.is_check())
+      last = generate_evasions(pos, mlist, pinned);
+  else {
+      last = generate_captures(pos, mlist);
+      last = generate_noncaptures(pos, last);
+  }
   if (pseudoLegal)
       return last;
 
@@ -787,17 +752,4 @@ namespace {
     }
     return mlist;
   }
-
-  bool castling_is_check(const Position& pos, CastlingSide side) {
-
-    // After castling opponent king is attacked by the castled rook?
-    File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
-    Color us = pos.side_to_move();
-    Square ksq = pos.king_square(us);
-    Bitboard occ = pos.occupied_squares();
-
-    clear_bit(&occ, ksq); // Remove our king from the board
-    Square rsq = make_square(rookFile, square_rank(ksq));
-    return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));
-  }
 }