- do {
- from = *ptr;
- b = pos.attacks_from<Piece>(from) & target;
- SERIALIZE_MOVES(b);
- } while (*++ptr != SQ_NONE);
- }
- return mlist;
- }
-
- template<>
- inline MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
-
- Bitboard b;
- Square from = pos.king_square(us);
-
- b = pos.attacks_from<KING>(from) & target;
- SERIALIZE_MOVES(b);
- return mlist;
- }
-
-}
-
-////
-//// Functions
-////
-
-
-/// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
-/// promotions. Returns a pointer to the end of the move list.
-///
-/// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
-/// underpromotions. Returns a pointer to the end of the move list.
-///
-/// 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>
-MoveStack* generate(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
- assert(!pos.is_check());
-
- Color us = pos.side_to_move();
- Bitboard target;
-
- if (T == MV_CAPTURE || T == MV_NON_EVASION)
- target = pos.pieces_of_color(opposite_color(us));
- else if (T == MV_NON_CAPTURE)
- target = pos.empty_squares();
- else
- assert(false);
-
- if (T == 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<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);
- mlist = generate_piece_moves<KING>(pos, mlist, us, target);
-
- if (T != MV_CAPTURE)
- {
- 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;
-}
-
-// Explicit template instantiation
-template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
-template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
-template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
-
-
-/// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
-/// underpromotions that give check. Returns a pointer to the end of the move list.
-template<>
-MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
- assert(!pos.is_check());
-
- Bitboard b, dc;
- Square from;
- 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));
-
- // Discovered non-capture checks
- b = dc = pos.discovered_check_candidates(us);
-
- while (b)
- {
- from = pop_1st_bit(&b);
- switch (pos.type_of_piece_on(from))
- {
- case PAWN: /* Will be generated togheter with pawns direct checks */ break;
- case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
- case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
- case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
- case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
- default: assert(false); break;
- }
- }
-
- // Direct non-capture checks
- mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
- 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);
- return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
-}
-
-
-/// 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.
-template<>
-MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
- assert(pos.is_check());
-
- Bitboard b, target;
- Square from, checksq;
- int checkersCnt = 0;
- Color us = pos.side_to_move();
- Square ksq = pos.king_square(us);
- Bitboard checkers = pos.checkers();
- Bitboard sliderAttacks = EmptyBoardBB;
-
- assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
- assert(checkers);
-
- // Find squares attacked by slider checkers, we will remove
- // them from the king evasions set so to early skip known
- // illegal moves and avoid an useless legality check later.
- b = checkers;
- do
- {
- checkersCnt++;
- checksq = pop_1st_bit(&b);
-
- assert(pos.color_of_piece_on(checksq) == opposite_color(us));
-
- switch (pos.type_of_piece_on(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 (RookPseudoAttacks[checksq] & (1ULL << 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);
-
- // Generate evasions for other pieces only if not double check
- if (checkersCnt > 1)
- return mlist;
-
- // Find squares where a blocking evasion or a capture of the
- // checker piece is possible.
- target = squares_between(checksq, ksq) | checkers;
-
- mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
- 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);
- return generate_piece_moves<QUEEN>(pos, mlist, us, target);
-}
-
-
-/// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
-/// or pseudo-legal moves in the current position.
-template<>
-inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
-
- return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
- : generate<MV_NON_EVASION>(pos, mlist);
-}
-
-template<>
-MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
-
- MoveStack *last, *cur = mlist;
- Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
-
- last = generate<MV_PSEUDO_LEGAL>(pos, mlist);