- Square ksq = pos.king_square(us);
- Bitboard sliderAttacks = 0;
- Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
-
- // Find all the squares attacked by slider checkers. We will remove them from
- // the king evasions in order to skip known illegal moves, which avoids any
- // useless legality checks later on.
- while (sliders)
- {
- Square checksq = pop_lsb(&sliders);
- sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
- }
-
- // Generate evasions for king, capture and non capture moves
- Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
- while (b)
- (mlist++)->move = make_move(ksq, pop_lsb(&b));
-
- if (more_than_one(pos.checkers()))
- return mlist; // Double check, only a king move can save the day
-
- // Generate blocking evasions or captures of the checking piece
- Square checksq = lsb(pos.checkers());
- Bitboard target = between_bb(checksq, ksq) | checksq;
-
- return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, mlist, target)
- : generate_all<BLACK, EVASIONS>(pos, mlist, target);
-}
-
-
-/// generate<LEGAL> generates all the legal moves in the given position
-
-template<>
-ExtMove* generate<LEGAL>(const Position& pos, ExtMove* mlist) {
-
- ExtMove *end, *cur = mlist;
- Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
- Square ksq = pos.king_square(pos.side_to_move());
-
- end = pos.checkers() ? generate<EVASIONS>(pos, mlist)
- : generate<NON_EVASIONS>(pos, mlist);
- while (cur != end)
- if ( (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
- && !pos.legal(cur->move, pinned))
- cur->move = (--end)->move;
+ Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
+ Square ksq = pos.square<KING>(us);
+ ExtMove* cur = moveList;
+
+ moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
+ : generate<NON_EVASIONS>(pos, moveList);
+ while (cur != moveList)
+ if ( ((pinned & from_sq(*cur)) || from_sq(*cur) == ksq || type_of(*cur) == EN_PASSANT)
+ && !pos.legal(*cur))
+ *cur = (--moveList)->move;