#include "position.h" namespace rigby { namespace { void add_targets(MoveList& list, int from, u64 targets, u64 theirs) { while (targets) { const int to = pop_lsb(targets); list.add(Move(from, to, (theirs & bb(to)) ? Move::CAPTURE : Move::QUIET)); } } void add_pawn_move(MoveList& list, int from, int to, bool capture, bool promotion) { if (promotion) { const int base = capture ? Move::PROMO_CAPTURE : Move::PROMO; // Queen first: it is by far the most common promotion. for (int k = 3; k >= 0; k--) list.add(Move(from, to, base + k)); } else { list.add(Move(from, to, capture ? Move::CAPTURE : Move::QUIET)); } } } // namespace // Fully legal generation using check and pin masks. void generate_legal(const Position& pos, MoveList& list) { list.clear(); const int us = pos.stm; const int them = us ^ 1; const u64 occ = pos.occupancy(); const u64 ours = pos.colors[us]; const u64 theirs = pos.colors[them]; const int ksq = pos.king_square(us); const u64 checkers = pos.attackers_to(ksq, occ) & theirs; const int num_checkers = popcount(checkers); // King moves: the king itself must not block a slider ray on its destination. { const u64 occ_no_king = occ ^ bb(ksq); u64 targets = attacks::king[ksq] & ~ours; while (targets) { const int to = pop_lsb(targets); if (!pos.square_attacked(to, them, occ_no_king)) list.add(Move(ksq, to, (theirs & bb(to)) ? Move::CAPTURE : Move::QUIET)); } } if (num_checkers > 1) return; const u64 check_mask = num_checkers ? (attacks::between[ksq][lsb(checkers)] | checkers) : ~0ULL; const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN]; const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN]; u64 pinned = 0; { u64 snipers = ((attacks::rook(ksq, 0) & orth_sliders) | (attacks::bishop(ksq, 0) & diag_sliders)) & theirs; while (snipers) { const int s = pop_lsb(snipers); const u64 blockers = attacks::between[ksq][s] & occ; if (blockers && !(blockers & (blockers - 1)) && (blockers & ours)) pinned |= blockers; } } // A pinned knight can never move. u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned; while (knights) { const int from = pop_lsb(knights); add_targets(list, from, attacks::knight[from] & ~ours & check_mask, theirs); } u64 diag = diag_sliders & ours; while (diag) { const int from = pop_lsb(diag); u64 targets = attacks::bishop(from, occ) & ~ours & check_mask; if (pinned & bb(from)) targets &= attacks::line[ksq][from]; add_targets(list, from, targets, theirs); } u64 orth = orth_sliders & ours; while (orth) { const int from = pop_lsb(orth); u64 targets = attacks::rook(from, occ) & ~ours & check_mask; if (pinned & bb(from)) targets &= attacks::line[ksq][from]; add_targets(list, from, targets, theirs); } const int up = us == WHITE ? 8 : -8; const u64 start_rank = us == WHITE ? (RANK_1_BB << 8) : (RANK_8_BB >> 8); const u64 promo_rank = us == WHITE ? RANK_8_BB : RANK_1_BB; u64 pawns = pos.pieces_of(us, PAWN); while (pawns) { const int from = pop_lsb(pawns); u64 allowed = check_mask; if (pinned & bb(from)) allowed &= attacks::line[ksq][from]; const int to = from + up; if (!(occ & bb(to))) { if (allowed & bb(to)) add_pawn_move(list, from, to, false, promo_rank & bb(to)); const int to2 = to + up; if ((start_rank & bb(from)) && !(occ & bb(to2)) && (allowed & bb(to2))) list.add(Move(from, to2, Move::DOUBLE_PUSH)); } u64 captures = attacks::pawn[us][from] & theirs & allowed; while (captures) { const int cap = pop_lsb(captures); add_pawn_move(list, from, cap, true, promo_rank & bb(cap)); } if (pos.ep != NO_SQUARE && (attacks::pawn[us][from] & bb(pos.ep))) { // Rare enough to verify by brute force: rebuild the occupancy and look for any attacker. const int captured_sq = pos.ep - up; const u64 occ_after = (occ ^ bb(from) ^ bb(captured_sq)) | bb(pos.ep); const u64 their_pawns = pos.pieces_of(them, PAWN) & ~bb(captured_sq); const u64 attackers = (attacks::rook(ksq, occ_after) & orth_sliders & theirs) | (attacks::bishop(ksq, occ_after) & diag_sliders & theirs) | (attacks::knight[ksq] & pos.pieces_of(them, KNIGHT)) | (attacks::pawn[us][ksq] & their_pawns); if (!attackers) list.add(Move(from, pos.ep, Move::EN_PASSANT)); } } if (num_checkers) return; const int rank_base = us == WHITE ? 0 : 56; const int oo = us == WHITE ? WHITE_OO : BLACK_OO; const int ooo = us == WHITE ? WHITE_OOO : BLACK_OOO; const u64 our_rooks = pos.pieces_of(us, ROOK); if ((pos.castling & oo) && (our_rooks & bb(rank_base + 7)) && !(occ & (bb(rank_base + 5) | bb(rank_base + 6))) && !pos.square_attacked(rank_base + 5, them, occ) && !pos.square_attacked(rank_base + 6, them, occ)) list.add(Move(ksq, rank_base + 6, Move::KING_CASTLE)); if ((pos.castling & ooo) && (our_rooks & bb(rank_base + 0)) && !(occ & (bb(rank_base + 1) | bb(rank_base + 2) | bb(rank_base + 3))) && !pos.square_attacked(rank_base + 3, them, occ) && !pos.square_attacked(rank_base + 2, them, occ)) list.add(Move(ksq, rank_base + 2, Move::QUEEN_CASTLE)); } } // namespace rigby