]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Simplify legality check in generate_evasions()
[stockfish] / src / movegen.cpp
index 78533a9e75acf3f8fe4421e3bb4d49db99716311..134127eb387837acd51e38a197ab4807c2b41fc2 100644 (file)
@@ -223,7 +223,8 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
 
   // Find squares attacked by slider checkers, we will
   // remove them from king evasions set so to avoid a couple
-  // of cycles in the slow king evasions legality check loop.
+  // of cycles in the slow king evasions legality check loop
+  // and to be able to use square_is_attacked().
   Bitboard checkers = pos.checkers();
   Bitboard checkersAttacks = EmptyBoardBB;
   Bitboard b = checkers & (pos.queens() | pos.bishops());
@@ -245,17 +246,9 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
   while (b1)
   {
       to = pop_1st_bit(&b1);
-
-      // Make sure 'to' is not attacked by the other side. This is a bit ugly,
-      // because we can't use Position::square_is_attacked. Instead we use
-      // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
-      // b2 (the occupied squares with the king removed) in order to test whether
-      // the king will remain in check on the destination square.
-      if (!(   (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
-            || (pos.pawn_attacks(us, to)      & pos.pawns(them))
-            || (bishop_attacks_bb(to, b2)     & pos.bishops_and_queens(them))
-            || (rook_attacks_bb(to, b2)       & pos.rooks_and_queens(them))
-            || (pos.piece_attacks<KING>(to)   & pos.kings(them))))
+      // Note that we can use square_is_attacked() only because we
+      // have already removed sliders checkers.
+      if (!pos.square_is_attacked(to, them))
           (*mlist++).move = make_move(ksq, to);
   }
 
@@ -322,25 +315,19 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
         to = pos.ep_square();
         b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
 
-        assert(b1 != EmptyBoardBB);
+        // 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);
-
-            // Before generating the move, we have to make sure it is legal.
-            // This is somewhat tricky, because the two disappearing pawns may
-            // cause new "discovered checks".  We test this by removing the
-            // two relevant bits from the occupied squares bitboard, and using
-            // the low-level bitboard functions for bishop and rook attacks.
-            b2 = pos.occupied_squares();
-            clear_bit(&b2, from);
-            clear_bit(&b2, checksq);
-            if (!(  (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
-                  ||(rook_attacks_bb(ksq, b2)   & pos.rooks_and_queens(them))))
-
-                 (*mlist++).move = make_ep_move(from, to);
+            // 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);
         }
     }
   }