]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Retire generate_king_moves()
[stockfish] / src / movegen.cpp
index 0b46298b9c80f9ca770e88766d4b6d74d20f1fa3..e5824d547d0273d82b65da905bdd0ed49dbb7b9a 100644 (file)
@@ -31,7 +31,7 @@
                                          (*mlist++).move = make_move(to - (d), to); }
 namespace {
 
-  template<CastlingSide Side, bool Checks>
+  template<CastlingSide Side, bool Checks, bool Chess960>
   MoveStack* generate_castle(const Position& pos, MoveStack* mlist, Color us) {
 
     if (pos.castle_impeded(us, Side) || !pos.can_castle(make_castle_right(us, Side)))
@@ -44,18 +44,20 @@ namespace {
     Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
     Bitboard enemies = pos.pieces(~us);
 
-    assert(!pos.in_check());
+    assert(!pos.checkers());
 
-    for (Square s = kto; s != kfrom; s += (Square)(Side == KING_SIDE ? -1 : 1))
+    const int K = Chess960 ? kto > kfrom ? -1 : 1
+                           : Side == KING_SIDE ? -1 : 1;
+
+    for (Square s = kto; s != kfrom; s += (Square)K)
         if (pos.attackers_to(s) & enemies)
             return mlist;
 
     // Because we generate only legal castling moves we need to verify that
     // when moving the castling rook we do not discover some hidden checker.
     // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
-    if (    pos.is_chess960()
-        && (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
-            return mlist;
+    if (Chess960 && (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
+        return mlist;
 
     (*mlist++).move = make<CASTLE>(kfrom, rfrom);
 
@@ -247,34 +249,39 @@ namespace {
   }
 
 
-  FORCE_INLINE MoveStack* generate_king_moves(const Position& pos, MoveStack* mlist,
-                                              Color us, Bitboard target) {
-    Square from = pos.king_square(us);
-    Bitboard b = pos.attacks_from<KING>(from) & target;
-    SERIALIZE(b);
-    return mlist;
-  }
-
-
   template<GenType Type> FORCE_INLINE
-  MoveStack* generate_all_moves(const Position& pos, MoveStack* mlist, Color us,
-                                Bitboard target, const CheckInfo* ci = NULL) {
+  MoveStack* generate_all(const Position& pos, MoveStack* mlist, Color us,
+                          Bitboard target, const CheckInfo* ci = NULL) {
+
+    const bool Checks = Type == QUIET_CHECKS;
 
     mlist = (us == WHITE ? generate_pawn_moves<WHITE, Type>(pos, mlist, target, ci)
                          : generate_pawn_moves<BLACK, Type>(pos, mlist, target, ci));
 
-    mlist = generate_moves<KNIGHT, Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
-    mlist = generate_moves<BISHOP, Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
-    mlist = generate_moves<ROOK,   Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
-    mlist = generate_moves<QUEEN,  Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
+    mlist = generate_moves<KNIGHT, Checks>(pos, mlist, us, target, ci);
+    mlist = generate_moves<BISHOP, Checks>(pos, mlist, us, target, ci);
+    mlist = generate_moves<ROOK,   Checks>(pos, mlist, us, target, ci);
+    mlist = generate_moves<QUEEN,  Checks>(pos, mlist, us, target, ci);
 
     if (Type != QUIET_CHECKS && Type != EVASIONS)
-        mlist = generate_king_moves(pos, mlist, us, target);
+    {
+        Square from = pos.king_square(us);
+        Bitboard b = pos.attacks_from<KING>(from) & target;
+        SERIALIZE(b);
+    }
 
     if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(us))
     {
-        mlist = generate_castle<KING_SIDE,  Type == QUIET_CHECKS>(pos, mlist, us);
-        mlist = generate_castle<QUEEN_SIDE, Type == QUIET_CHECKS>(pos, mlist, us);
+        if (pos.is_chess960())
+        {
+            mlist = generate_castle<KING_SIDE,  Checks, true>(pos, mlist, us);
+            mlist = generate_castle<QUEEN_SIDE, Checks, true>(pos, mlist, us);
+        }
+        else
+        {
+            mlist = generate_castle<KING_SIDE,  Checks, false>(pos, mlist, us);
+            mlist = generate_castle<QUEEN_SIDE, Checks, false>(pos, mlist, us);
+        }
     }
 
     return mlist;
@@ -297,21 +304,15 @@ template<GenType Type>
 MoveStack* generate(const Position& pos, MoveStack* mlist) {
 
   assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
-  assert(!pos.in_check());
+  assert(!pos.checkers());
 
   Color us = pos.side_to_move();
-  Bitboard target;
-
-  if (Type == CAPTURES)
-      target = pos.pieces(~us);
-
-  else if (Type == QUIETS)
-      target = ~pos.pieces();
 
-  else if (Type == NON_EVASIONS)
-      target = ~pos.pieces(us);
+  Bitboard target = Type == CAPTURES     ?  pos.pieces(~us)
+                  : Type == QUIETS       ? ~pos.pieces()
+                  : Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
 
-  return generate_all_moves<Type>(pos, mlist, us, target);
+  return generate_all<Type>(pos, mlist, us, target);
 }
 
 // Explicit template instantiations
@@ -325,9 +326,8 @@ template MoveStack* generate<NON_EVASIONS>(const Position&, MoveStack*);
 template<>
 MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
 
-  assert(!pos.in_check());
+  assert(!pos.checkers());
 
-  Color us = pos.side_to_move();
   CheckInfo ci(pos);
   Bitboard dc = ci.dcCandidates;
 
@@ -347,7 +347,7 @@ MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
      SERIALIZE(b);
   }
 
-  return generate_all_moves<QUIET_CHECKS>(pos, mlist, us, ~pos.pieces(), &ci);
+  return generate_all<QUIET_CHECKS>(pos, mlist, pos.side_to_move(), ~pos.pieces(), &ci);
 }
 
 
@@ -356,7 +356,7 @@ MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
 template<>
 MoveStack* generate<EVASIONS>(const Position& pos, MoveStack* mlist) {
 
-  assert(pos.in_check());
+  assert(pos.checkers());
 
   Square from, checksq;
   int checkersCnt = 0;
@@ -409,7 +409,7 @@ MoveStack* generate<EVASIONS>(const Position& pos, MoveStack* mlist) {
   // Generate blocking evasions or captures of the checking piece
   Bitboard target = between_bb(checksq, ksq) | pos.checkers();
 
-  return generate_all_moves<EVASIONS>(pos, mlist, us, target);
+  return generate_all<EVASIONS>(pos, mlist, us, target);
 }
 
 
@@ -420,11 +420,13 @@ MoveStack* generate<LEGAL>(const Position& pos, MoveStack* mlist) {
 
   MoveStack *end, *cur = mlist;
   Bitboard pinned = pos.pinned_pieces();
+  Square ksq = pos.king_square(pos.side_to_move());
 
-  end = pos.in_check() ? generate<EVASIONS>(pos, mlist)
+  end = pos.checkers() ? generate<EVASIONS>(pos, mlist)
                        : generate<NON_EVASIONS>(pos, mlist);
   while (cur != end)
-      if (!pos.pl_move_is_legal(cur->move, pinned))
+      if (   (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
+          && !pos.pl_move_is_legal(cur->move, pinned))
           cur->move = (--end)->move;
       else
           cur++;