2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 //// Local definitions
37 Bitboard Rank3BB, Rank8BB;
39 SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
42 const PawnParams WhitePawnParams = { Rank3BB, Rank8BB, RANK_8, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK };
43 const PawnParams BlackPawnParams = { Rank6BB, Rank1BB, RANK_1, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE };
45 int generate_castle_moves(const Position&, MoveStack*, Color);
48 int generate_pawn_captures(const Position&, MoveStack*);
51 int generate_pawn_noncaptures(const Position&, MoveStack*);
54 int generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*, int);
57 int generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
60 int generate_piece_moves(const Position&, MoveStack*, Color, Bitboard);
63 int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
66 int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
69 /// Templates are defined here to avoid lookup issues with specializations
71 template<PieceType Piece>
72 int generate_piece_moves(const Position &pos, MoveStack *mlist,
73 Color side, Bitboard target) {
75 for (int i = 0; i < pos.piece_count(side, Piece); i++)
77 Square from = pos.piece_list(side, Piece, i);
78 Bitboard b = pos.piece_attacks<Piece>(from) & target;
81 Square to = pop_1st_bit(&b);
82 mlist[n++].move = make_move(from, to);
89 template<PieceType Piece>
90 int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
91 Square ksq, MoveStack* mlist, int n) {
93 Bitboard b = target & dc;
96 Square from = pop_1st_bit(&b);
97 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
100 Square to = pop_1st_bit(&bb);
101 mlist[n++].move = make_move(from, to);
106 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
109 Square from = pop_1st_bit(&b);
110 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
113 Square to = pop_1st_bit(&bb);
114 mlist[n++].move = make_move(from, to);
121 template<> // Special case the King
122 int generate_piece_checks<KING>(const Position& pos, Bitboard, Bitboard dc,
123 Square ksq, MoveStack* mlist, int n) {
124 if (bit_is_set(dc, ksq))
126 Bitboard bb = pos.piece_attacks<KING>(ksq)
127 & pos.empty_squares()
128 & ~QueenPseudoAttacks[ksq];
131 Square to = pop_1st_bit(&bb);
132 mlist[n++].move = make_move(ksq, to);
139 template<PieceType Piece>
140 int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
141 Bitboard blockSquares, MoveStack* mlist, int n) {
144 Square from = pop_1st_bit(&b);
145 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
148 Square to = pop_1st_bit(&bb);
149 mlist[n++].move = make_move(from, to);
162 /// generate_captures generates() all pseudo-legal captures and queen
163 /// promotions. The return value is the number of moves generated.
165 int generate_captures(const Position& pos, MoveStack* mlist) {
168 assert(!pos.is_check());
170 Color us = pos.side_to_move();
171 Bitboard target = pos.pieces_of_color(opposite_color(us));
175 n = generate_pawn_captures<WHITE>(pos, mlist);
177 n = generate_pawn_captures<BLACK>(pos, mlist);
179 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
180 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
181 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
182 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
183 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
188 /// generate_noncaptures() generates all pseudo-legal non-captures and
189 /// underpromotions. The return value is the number of moves generated.
191 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
194 assert(!pos.is_check());
196 Color us = pos.side_to_move();
197 Bitboard target = pos.empty_squares();
201 n = generate_pawn_noncaptures<WHITE>(pos, mlist);
203 n = generate_pawn_noncaptures<BLACK>(pos, mlist);
205 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
206 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
207 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
208 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
209 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
211 n += generate_castle_moves(pos, mlist+n, us);
216 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
217 /// checks, except castling moves (will add this later). It returns the
218 /// number of generated moves.
220 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
223 assert(!pos.is_check());
226 Color us = pos.side_to_move();
227 Square ksq = pos.king_square(opposite_color(us));
229 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
231 dc = pos.discovered_check_candidates(us);
235 n = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist, 0);
237 n = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist, 0);
240 Bitboard b = pos.knights(us);
242 n = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist, n);
246 n = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist, n);
250 n = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist, n);
254 n = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist, n);
256 // Hopefully we always have a king ;-)
257 n = generate_piece_checks<KING>(pos, b, dc, pos.king_square(us), mlist, n);
259 // TODO: Castling moves!
265 /// generate_evasions() generates all check evasions when the side to move is
266 /// in check. Unlike the other move generation functions, this one generates
267 /// only legal moves. It returns the number of generated moves. This
268 /// function is very ugly, and needs cleaning up some time later. FIXME
270 int generate_evasions(const Position& pos, MoveStack* mlist) {
273 assert(pos.is_check());
275 Color us = pos.side_to_move();
276 Color them = opposite_color(us);
277 Square ksq = pos.king_square(us);
281 assert(pos.piece_on(ksq) == king_of_color(us));
283 // Generate evasions for king
284 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
285 Bitboard b2 = pos.occupied_squares();
290 Square to = pop_1st_bit(&b1);
292 // Make sure to is not attacked by the other side. This is a bit ugly,
293 // because we can't use Position::square_is_attacked. Instead we use
294 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
295 // b2 (the occupied squares with the king removed) in order to test whether
296 // the king will remain in check on the destination square.
297 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
298 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
299 || (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
300 || (pos.pawn_attacks(us, to) & pos.pawns(them))
301 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
303 mlist[n++].move = make_move(ksq, to);
306 // Generate evasions for other pieces only if not double check. We use a
307 // simple bit twiddling hack here rather than calling count_1s in order to
308 // save some time (we know that pos.checkers() has at most two nonzero bits).
309 Bitboard checkers = pos.checkers();
311 if (!(checkers & (checkers - 1))) // Only one bit set?
313 Square checksq = first_1(checkers);
315 assert(pos.color_of_piece_on(checksq) == them);
317 // Find pinned pieces
318 Bitboard not_pinned = ~pos.pinned_pieces(us);
320 // Generate captures of the checking piece
323 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
326 from = pop_1st_bit(&b1);
327 if (relative_rank(us, checksq) == RANK_8)
329 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
330 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
331 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
332 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
334 mlist[n++].move = make_move(from, checksq);
338 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
339 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
340 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
344 from = pop_1st_bit(&b1);
345 mlist[n++].move = make_move(from, checksq);
348 // Blocking check evasions are possible only if the checking piece is
350 if (checkers & pos.sliders())
352 Bitboard blockSquares = squares_between(checksq, ksq);
354 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
356 // Pawn moves. Because a blocking evasion can never be a capture, we
357 // only generate pawn pushes.
359 n = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist, n);
361 n = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist, n);
364 b1 = pos.knights(us) & not_pinned;
366 n = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist, n);
368 b1 = pos.bishops(us) & not_pinned;
370 n = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist, n);
372 b1 = pos.rooks(us) & not_pinned;
374 n = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist, n);
376 b1 = pos.queens(us) & not_pinned;
378 n = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist, n);
381 // Finally, the ugly special case of en passant captures. An en passant
382 // capture can only be a check evasion if the check is not a discovered
383 // check. If pos.ep_square() is set, the last move made must have been
384 // a double pawn push. If, furthermore, the checking piece is a pawn,
385 // an en passant check evasion may be possible.
386 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
388 to = pos.ep_square();
389 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
391 assert(b1 != EmptyBoardBB);
396 from = pop_1st_bit(&b1);
398 // Before generating the move, we have to make sure it is legal.
399 // This is somewhat tricky, because the two disappearing pawns may
400 // cause new "discovered checks". We test this by removing the
401 // two relevant bits from the occupied squares bitboard, and using
402 // the low-level bitboard functions for bishop and rook attacks.
403 b2 = pos.occupied_squares();
404 clear_bit(&b2, from);
405 clear_bit(&b2, checksq);
406 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
407 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
409 mlist[n++].move = make_ep_move(from, to);
417 /// generate_legal_moves() computes a complete list of legal moves in the
418 /// current position. This function is not very fast, and should be used
419 /// only in situations where performance is unimportant. It wouldn't be
420 /// very hard to write an efficient legal move generator, but for the moment
421 /// we don't need it.
423 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
428 return generate_evasions(pos, mlist);
430 // Generate pseudo-legal moves:
431 int n = generate_captures(pos, mlist);
432 n += generate_noncaptures(pos, mlist + n);
434 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
436 // Remove illegal moves from the list:
437 for (int i = 0; i < n; i++)
438 if (!pos.move_is_legal(mlist[i].move, pinned))
439 mlist[i--].move = mlist[--n].move;
445 /// generate_move_if_legal() takes a position and a (not necessarily
446 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
447 /// whether the move is legal. If the move is legal, the move itself is
448 /// returned. If not, the function returns MOVE_NONE. This function must
449 /// only be used when the side to move is not in check.
451 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
454 assert(!pos.is_check());
455 assert(move_is_ok(m));
457 Color us = pos.side_to_move();
458 Color them = opposite_color(us);
459 Square from = move_from(m);
460 Piece pc = pos.piece_on(from);
462 // If the from square is not occupied by a piece belonging to the side to
463 // move, the move is obviously not legal.
464 if (color_of_piece(pc) != us)
467 Square to = move_to(m);
472 // The piece must be a pawn and destination square must be the
473 // en passant square.
474 if ( type_of_piece(pc) != PAWN
475 || to != pos.ep_square())
478 assert(pos.square_is_empty(to));
479 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
481 // The move is pseudo-legal. If it is legal, return it.
482 return (pos.move_is_legal(m) ? m : MOVE_NONE);
486 if (move_is_short_castle(m))
488 // The piece must be a king and side to move must still have
489 // the right to castle kingside.
490 if ( type_of_piece(pc) != KING
491 ||!pos.can_castle_kingside(us))
494 assert(from == pos.king_square(us));
495 assert(to == pos.initial_kr_square(us));
496 assert(pos.piece_on(to) == rook_of_color(us));
498 Square g1 = relative_square(us, SQ_G1);
499 Square f1 = relative_square(us, SQ_F1);
501 bool illegal = false;
503 // Check if any of the squares between king and rook
504 // is occupied or under attack.
505 for (s = Min(from, g1); s <= Max(from, g1); s++)
506 if ( (s != from && s != to && !pos.square_is_empty(s))
507 || pos.square_is_attacked(s, them))
510 // Check if any of the squares between king and rook
512 for (s = Min(to, f1); s <= Max(to, f1); s++)
513 if (s != from && s != to && !pos.square_is_empty(s))
516 return (!illegal ? m : MOVE_NONE);
519 if (move_is_long_castle(m))
521 // The piece must be a king and side to move must still have
522 // the right to castle kingside.
523 if ( type_of_piece(pc) != KING
524 ||!pos.can_castle_queenside(us))
527 assert(from == pos.king_square(us));
528 assert(to == pos.initial_qr_square(us));
529 assert(pos.piece_on(to) == rook_of_color(us));
531 Square c1 = relative_square(us, SQ_C1);
532 Square d1 = relative_square(us, SQ_D1);
534 bool illegal = false;
536 for (s = Min(from, c1); s <= Max(from, c1); s++)
537 if( (s != from && s != to && !pos.square_is_empty(s))
538 || pos.square_is_attacked(s, them))
541 for (s = Min(to, d1); s <= Max(to, d1); s++)
542 if(s != from && s != to && !pos.square_is_empty(s))
545 if ( square_file(to) == FILE_B
546 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
547 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
550 return (!illegal ? m : MOVE_NONE);
555 // The destination square cannot be occupied by a friendly piece
556 if (pos.color_of_piece_on(to) == us)
559 // Proceed according to the type of the moving piece.
560 if (type_of_piece(pc) == PAWN)
562 // If the destination square is on the 8/1th rank, the move must
564 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
565 ||(square_rank(to) == RANK_1 && us != WHITE))
566 && !move_promotion(m))
569 // Proceed according to the square delta between the source and
570 // destionation squares.
577 // Capture. The destination square must be occupied by an enemy
578 // piece (en passant captures was handled earlier).
579 if (pos.color_of_piece_on(to) != them)
585 // Pawn push. The destination square must be empty.
586 if (!pos.square_is_empty(to))
591 // Double white pawn push. The destination square must be on the fourth
592 // rank, and both the destination square and the square between the
593 // source and destination squares must be empty.
594 if ( square_rank(to) != RANK_4
595 || !pos.square_is_empty(to)
596 || !pos.square_is_empty(from + DELTA_N))
601 // Double black pawn push. The destination square must be on the fifth
602 // rank, and both the destination square and the square between the
603 // source and destination squares must be empty.
604 if ( square_rank(to) != RANK_5
605 || !pos.square_is_empty(to)
606 || !pos.square_is_empty(from + DELTA_S))
613 // The move is pseudo-legal. Return it if it is legal.
614 return (pos.move_is_legal(m) ? m : MOVE_NONE);
617 // Luckly we can handle all the other pieces in one go
618 return ( pos.piece_attacks_square(from, to)
619 && pos.move_is_legal(m)
620 && !move_promotion(m) ? m : MOVE_NONE);
627 int generate_pawn_captures(const Position& pos, MoveStack* mlist) {
629 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
631 Bitboard pawns = pos.pawns(PP.us);
632 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
636 // Captures in the a1-h8 (a8-h1 for black) direction
637 Bitboard b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
639 // Capturing promotions
640 Bitboard b2 = b1 & PP.Rank8BB;
643 sq = pop_1st_bit(&b2);
644 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, QUEEN);
647 // Capturing non-promotions
648 b2 = b1 & ~PP.Rank8BB;
651 sq = pop_1st_bit(&b2);
652 mlist[n++].move = make_move(sq - PP.DELTA_NE, sq);
655 // Captures in the h1-a8 (h8-a1 for black) direction
656 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
658 // Capturing promotions
659 b2 = b1 & PP.Rank8BB;
662 sq = pop_1st_bit(&b2);
663 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, QUEEN);
666 // Capturing non-promotions
667 b2 = b1 & ~PP.Rank8BB;
670 sq = pop_1st_bit(&b2);
671 mlist[n++].move = make_move(sq - PP.DELTA_NW, sq);
674 // Non-capturing promotions
675 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & Rank8BB;
678 sq = pop_1st_bit(&b1);
679 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, QUEEN);
682 // En passant captures
683 if (pos.ep_square() != SQ_NONE)
685 assert(PP.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
686 assert(PP.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
688 b1 = pawns & pos.pawn_attacks(PP.them, pos.ep_square());
689 assert(b1 != EmptyBoardBB);
693 sq = pop_1st_bit(&b1);
694 mlist[n++].move = make_ep_move(sq, pos.ep_square());
702 int generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
704 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
706 Bitboard pawns = pos.pawns(PP.us);
707 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
708 Bitboard emptySquares = pos.empty_squares();
713 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
714 b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & PP.Rank8BB;
717 sq = pop_1st_bit(&b1);
718 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, ROOK);
719 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, BISHOP);
720 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, KNIGHT);
723 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
724 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & PP.Rank8BB;
727 sq = pop_1st_bit(&b1);
728 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, ROOK);
729 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, BISHOP);
730 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, KNIGHT);
733 // Single pawn pushes
734 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
735 b2 = b1 & PP.Rank8BB;
738 sq = pop_1st_bit(&b2);
739 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, ROOK);
740 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, BISHOP);
741 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, KNIGHT);
743 b2 = b1 & ~PP.Rank8BB;
746 sq = pop_1st_bit(&b2);
747 mlist[n++].move = make_move(sq - PP.DELTA_N, sq);
750 // Double pawn pushes
751 b2 = (C == WHITE ? (b1 & PP.Rank3BB) << 8 : (b1 & PP.Rank3BB) >> 8) & emptySquares;
754 sq = pop_1st_bit(&b2);
755 mlist[n++].move = make_move(sq - PP.DELTA_N - PP.DELTA_N, sq);
762 int generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
764 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
766 // Pawn moves which give discovered check. This is possible only if the
767 // pawn is not on the same file as the enemy king, because we don't
768 // generate captures.
769 Bitboard empty = pos.empty_squares();
771 // Find all friendly pawns not on the enemy king's file
772 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
774 // Discovered checks, single pawn pushes
775 b2 = b3 = (C == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~PP.Rank8BB & empty;
778 Square to = pop_1st_bit(&b3);
779 mlist[n++].move = make_move(to - PP.DELTA_N, to);
782 // Discovered checks, double pawn pushes
783 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8) & empty;
786 Square to = pop_1st_bit(&b3);
787 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
790 // Direct checks. These are possible only for pawns on neighboring files
793 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
795 // Direct checks, single pawn pushes
796 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & empty;
797 b3 = b2 & pos.pawn_attacks(PP.them, ksq);
800 Square to = pop_1st_bit(&b3);
801 mlist[n++].move = make_move(to - PP.DELTA_N, to);
804 // Direct checks, double pawn pushes
805 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8)
807 & pos.pawn_attacks(PP.them, ksq);
811 Square to = pop_1st_bit(&b3);
812 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
818 int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
819 Bitboard blockSquares, MoveStack* mlist, int n) {
821 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
823 // Find non-pinned pawns
824 Bitboard b1 = pos.pawns(PP.us) & not_pinned;
826 // Single pawn pushes. We don't have to AND with empty squares here,
827 // because the blocking squares will always be empty.
828 Bitboard b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
831 Square to = pop_1st_bit(&b2);
833 assert(pos.piece_on(to) == EMPTY);
835 if (square_rank(to) == PP.RANK_8)
837 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, QUEEN);
838 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, ROOK);
839 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, BISHOP);
840 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, KNIGHT);
842 mlist[n++].move = make_move(to - PP.DELTA_N, to);
845 // Double pawn pushes
846 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & PP.Rank3BB;
847 b2 = (C == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
850 Square to = pop_1st_bit(&b2);
852 assert(pos.piece_on(to) == EMPTY);
853 assert(PP.us != WHITE || square_rank(to) == RANK_4);
854 assert(PP.us != BLACK || square_rank(to) == RANK_5);
856 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
862 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
866 if (pos.can_castle(us))
868 Color them = opposite_color(us);
869 Square ksq = pos.king_square(us);
871 assert(pos.piece_on(ksq) == king_of_color(us));
873 if (pos.can_castle_kingside(us))
875 Square rsq = pos.initial_kr_square(us);
876 Square g1 = relative_square(us, SQ_G1);
877 Square f1 = relative_square(us, SQ_F1);
879 bool illegal = false;
881 assert(pos.piece_on(rsq) == rook_of_color(us));
883 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
884 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
885 || pos.square_is_attacked(s, them))
888 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
889 if (s != ksq && s != rsq && pos.square_is_occupied(s))
893 mlist[n++].move = make_castle_move(ksq, rsq);
896 if (pos.can_castle_queenside(us))
898 Square rsq = pos.initial_qr_square(us);
899 Square c1 = relative_square(us, SQ_C1);
900 Square d1 = relative_square(us, SQ_D1);
902 bool illegal = false;
904 assert(pos.piece_on(rsq) == rook_of_color(us));
906 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
907 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
908 || pos.square_is_attacked(s, them))
911 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
912 if (s != ksq && s != rsq && pos.square_is_occupied(s))
915 if ( square_file(rsq) == FILE_B
916 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
917 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
921 mlist[n++].move = make_castle_move(ksq, rsq);