]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Retire move_ambiguity() altogether
[stockfish] / src / movegen.cpp
index b130247bea5f151cd810ec3e73ddd8b1a1204a83..0056ce384dfff017f45d164d442d746a62649943 100644 (file)
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-
-////
-//// Includes
-////
-
 #include <cassert>
 
 #include "bitcount.h"
 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
 
-////
-//// Local definitions
-////
-
 namespace {
 
   enum CastlingSide {
@@ -46,7 +37,7 @@ namespace {
   };
 
   template<CastlingSide Side>
-  MoveStack* generate_castle_moves(const Position&, MoveStack*);
+  MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
 
   template<Color Us, MoveType Type>
   MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
@@ -99,14 +90,14 @@ namespace {
   }
 
   template<>
-  inline MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
+  FORCE_INLINE MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
 
     return (us == WHITE ? generate_pawn_moves<WHITE, MV_CHECK>(p, m, dc, ksq)
                         : generate_pawn_moves<BLACK, MV_CHECK>(p, m, dc, ksq));
   }
 
   template<PieceType Piece, MoveType Type>
-  inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
+  FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
 
     assert(Piece == PAWN);
     assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION);
@@ -116,7 +107,7 @@ namespace {
   }
 
   template<PieceType Piece>
-  inline MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
+  FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
 
     Bitboard b;
     Square from;
@@ -134,7 +125,7 @@ namespace {
   }
 
   template<>
-  inline MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
+  FORCE_INLINE MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
 
     Bitboard b;
     Square from = pos.king_square(us);
@@ -160,7 +151,7 @@ namespace {
 /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
 /// non-captures. Returns a pointer to the end of the move list.
 
-template<MoveType T>
+template<MoveType Type>
 MoveStack* generate(const Position& pos, MoveStack* mlist) {
 
   assert(pos.is_ok());
@@ -169,21 +160,21 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) {
   Color us = pos.side_to_move();
   Bitboard target;
 
-  if (T == MV_CAPTURE || T == MV_NON_EVASION)
+  if (Type == MV_CAPTURE || Type == MV_NON_EVASION)
       target = pos.pieces_of_color(opposite_color(us));
-  else if (T == MV_NON_CAPTURE)
+  else if (Type == MV_NON_CAPTURE)
       target = pos.empty_squares();
   else
       assert(false);
 
-  if (T == MV_NON_EVASION)
+  if (Type == MV_NON_EVASION)
   {
       mlist = generate_piece_moves<PAWN, MV_CAPTURE>(pos, mlist, us, target);
       mlist = generate_piece_moves<PAWN, MV_NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
       target |= pos.empty_squares();
   }
   else
-      mlist = generate_piece_moves<PAWN, T>(pos, mlist, us, target);
+      mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target);
 
   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
@@ -191,10 +182,13 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) {
   mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
   mlist = generate_piece_moves<KING>(pos, mlist, us, target);
 
-  if (T != MV_CAPTURE)
+  if (Type != MV_CAPTURE)
   {
-      mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
-      mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
+      if (pos.can_castle_kingside(us))
+          mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
+
+      if (pos.can_castle_queenside(us))
+          mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
   }
 
   return mlist;
@@ -219,7 +213,7 @@ MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist)
   Color us = pos.side_to_move();
   Square ksq = pos.king_square(opposite_color(us));
 
-  assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
+  assert(pos.piece_on(ksq) == make_piece(opposite_color(us), KING));
 
   // Discovered non-capture checks
   b = dc = pos.discovered_check_candidates(us);
@@ -263,7 +257,7 @@ MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
   Bitboard checkers = pos.checkers();
   Bitboard sliderAttacks = EmptyBoardBB;
 
-  assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
+  assert(pos.piece_on(ksq) == make_piece(us, KING));
   assert(checkers);
 
   // Find squares attacked by slider checkers, we will remove
@@ -458,7 +452,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
 
 namespace {
 
-  template<SquareDelta Delta>
+  template<Square Delta>
   inline Bitboard move_pawns(Bitboard p) {
 
     return Delta == DELTA_N  ? p << 8 : Delta == DELTA_S  ? p >> 8 :
@@ -466,7 +460,7 @@ namespace {
            Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
   }
 
-  template<MoveType Type, SquareDelta Delta>
+  template<MoveType Type, Square Delta>
   inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
 
     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
@@ -480,7 +474,7 @@ namespace {
     return mlist;
   }
 
-  template<Color Us, MoveType Type, SquareDelta Delta>
+  template<Color Us, MoveType Type, Square Delta>
   inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
 
     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
@@ -523,12 +517,12 @@ namespace {
 
     // Calculate our parametrized parameters at compile time, named
     // according to the point of view of white side.
-    const Color       Them      = (Us == WHITE ? BLACK    : WHITE);
-    const Bitboard    TRank7BB  = (Us == WHITE ? Rank7BB  : Rank2BB);
-    const Bitboard    TRank3BB  = (Us == WHITE ? Rank3BB  : Rank6BB);
-    const SquareDelta TDELTA_N  = (Us == WHITE ? DELTA_N  : DELTA_S);
-    const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
-    const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
+    const Color    Them      = (Us == WHITE ? BLACK    : WHITE);
+    const Bitboard TRank7BB  = (Us == WHITE ? Rank7BB  : Rank2BB);
+    const Bitboard TRank3BB  = (Us == WHITE ? Rank3BB  : Rank6BB);
+    const Square   TDELTA_N  = (Us == WHITE ? DELTA_N  : DELTA_S);
+    const Square   TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
+    const Square   TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
 
     Square to;
     Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
@@ -622,45 +616,41 @@ namespace {
   }
 
   template<CastlingSide Side>
-  MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
+  MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
 
-    Color us = pos.side_to_move();
+    Color them = opposite_color(us);
+    Square ksq = pos.king_square(us);
 
-    if (  (Side == KING_SIDE  && pos.can_castle_kingside(us))
-        ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
-    {
-        Color them = opposite_color(us);
-        Square ksq = pos.king_square(us);
+    assert(pos.piece_on(ksq) == make_piece(us, KING));
 
-        assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
+    Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
+    Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
+    Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
+    Square s;
+    bool illegal = false;
 
-        Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
-        Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
-        Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
-        Square s;
-        bool illegal = false;
+    assert(pos.piece_on(rsq) == make_piece(us, ROOK));
 
-        assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
+    // It is a bit complicated to correctly handle Chess960
+    for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
+        if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
+            ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
+            illegal = true;
 
-        // It is a bit complicated to correctly handle Chess960
-        for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
-            if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
-                ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
-                illegal = true;
+    for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
+        if (s != ksq && s != rsq && pos.square_is_occupied(s))
+            illegal = true;
 
-        for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
-            if (s != ksq && s != rsq && pos.square_is_occupied(s))
-                illegal = true;
+    if (   Side == QUEEN_SIDE
+        && square_file(rsq) == FILE_B
+        && (   pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, ROOK)
+            || pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, QUEEN)))
+        illegal = true;
 
-        if (   Side == QUEEN_SIDE
-            && square_file(rsq) == FILE_B
-            && (   pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
-                || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
-            illegal = true;
+    if (!illegal)
+        (*mlist++).move = make_castle_move(ksq, rsq);
 
-        if (!illegal)
-            (*mlist++).move = make_castle_move(ksq, rsq);
-    }
     return mlist;
   }
-}
+
+} // namespace