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
35 inline Bitboard forward_white(Bitboard b) { return b << 8; }
36 inline Bitboard forward_right_white(Bitboard b) { return b << 9; }
37 inline Bitboard forward_left_white(Bitboard b) { return b << 7; }
39 inline Bitboard forward_black(Bitboard b) { return b >> 8; }
40 inline Bitboard forward_right_black(Bitboard b) { return b >> 7; }
41 inline Bitboard forward_left_black(Bitboard b) { return b >> 9; }
45 Bitboard Rank3BB, Rank8BB;
47 SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
49 typedef Bitboard (*Shift_fn)(Bitboard b);
50 Shift_fn forward, forward_left, forward_right;
53 const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, RANK_8, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK,
54 &forward_white, forward_left_white, forward_right_white };
56 const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, RANK_1, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE,
57 &forward_black, &forward_left_black, &forward_right_black };
59 int generate_castle_moves(const Position&, MoveStack*, Color);
62 int generate_pawn_captures(const Position&, MoveStack*);
65 int generate_pawn_noncaptures(const Position&, MoveStack*);
68 int generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*, int);
71 int generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
74 int generate_piece_moves(const Position&, MoveStack*, Color, Bitboard);
77 int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
80 int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
83 /// Templates are defined here to avoid lookup issues with specializations
85 template<PieceType Piece>
86 int generate_piece_moves(const Position &pos, MoveStack *mlist,
87 Color side, Bitboard target) {
89 for (int i = 0; i < pos.piece_count(side, Piece); i++)
91 Square from = pos.piece_list(side, Piece, i);
92 Bitboard b = pos.piece_attacks<Piece>(from) & target;
95 Square to = pop_1st_bit(&b);
96 mlist[n++].move = make_move(from, to);
103 template<PieceType Piece>
104 int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
105 Square ksq, MoveStack* mlist, int n) {
107 Bitboard b = target & dc;
110 Square from = pop_1st_bit(&b);
111 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
114 Square to = pop_1st_bit(&bb);
115 mlist[n++].move = make_move(from, to);
120 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
123 Square from = pop_1st_bit(&b);
124 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
127 Square to = pop_1st_bit(&bb);
128 mlist[n++].move = make_move(from, to);
135 template<> // Special case the King
136 int generate_piece_checks<KING>(const Position& pos, Bitboard, Bitboard dc,
137 Square ksq, MoveStack* mlist, int n) {
138 if (bit_is_set(dc, ksq))
140 Bitboard bb = pos.piece_attacks<KING>(ksq)
141 & pos.empty_squares()
142 & ~QueenPseudoAttacks[ksq];
145 Square to = pop_1st_bit(&bb);
146 mlist[n++].move = make_move(ksq, to);
153 template<PieceType Piece>
154 int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
155 Bitboard blockSquares, MoveStack* mlist, int n) {
158 Square from = pop_1st_bit(&b);
159 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
162 Square to = pop_1st_bit(&bb);
163 mlist[n++].move = make_move(from, to);
176 /// generate_captures generates() all pseudo-legal captures and queen
177 /// promotions. The return value is the number of moves generated.
179 int generate_captures(const Position& pos, MoveStack* mlist) {
182 assert(!pos.is_check());
184 Color us = pos.side_to_move();
185 Bitboard target = pos.pieces_of_color(opposite_color(us));
189 n = generate_pawn_captures<WHITE>(pos, mlist);
191 n = generate_pawn_captures<BLACK>(pos, mlist);
193 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
194 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
195 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
196 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
197 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
202 /// generate_noncaptures() generates all pseudo-legal non-captures and
203 /// underpromotions. The return value is the number of moves generated.
205 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
208 assert(!pos.is_check());
210 Color us = pos.side_to_move();
211 Bitboard target = pos.empty_squares();
215 n = generate_pawn_noncaptures<WHITE>(pos, mlist);
217 n = generate_pawn_noncaptures<BLACK>(pos, mlist);
219 n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
220 n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
221 n += generate_piece_moves<ROOK>(pos, mlist+n, us, target);
222 n += generate_piece_moves<QUEEN>(pos, mlist+n, us, target);
223 n += generate_piece_moves<KING>(pos, mlist+n, us, target);
225 n += generate_castle_moves(pos, mlist+n, us);
230 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
231 /// checks, except castling moves (will add this later). It returns the
232 /// number of generated moves.
234 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
237 assert(!pos.is_check());
240 Color us = pos.side_to_move();
241 Square ksq = pos.king_square(opposite_color(us));
243 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
245 dc = pos.discovered_check_candidates(us);
249 n = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist, 0);
251 n = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist, 0);
254 Bitboard b = pos.knights(us);
256 n = generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist, n);
260 n = generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist, n);
264 n = generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist, n);
268 n = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist, n);
270 // Hopefully we always have a king ;-)
271 n = generate_piece_checks<KING>(pos, b, dc, pos.king_square(us), mlist, n);
273 // TODO: Castling moves!
279 /// generate_evasions() generates all check evasions when the side to move is
280 /// in check. Unlike the other move generation functions, this one generates
281 /// only legal moves. It returns the number of generated moves. This
282 /// function is very ugly, and needs cleaning up some time later. FIXME
284 int generate_evasions(const Position& pos, MoveStack* mlist) {
287 assert(pos.is_check());
289 Color us = pos.side_to_move();
290 Color them = opposite_color(us);
291 Square ksq = pos.king_square(us);
295 assert(pos.piece_on(ksq) == king_of_color(us));
297 // Generate evasions for king
298 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
299 Bitboard b2 = pos.occupied_squares();
304 Square to = pop_1st_bit(&b1);
306 // Make sure to is not attacked by the other side. This is a bit ugly,
307 // because we can't use Position::square_is_attacked. Instead we use
308 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
309 // b2 (the occupied squares with the king removed) in order to test whether
310 // the king will remain in check on the destination square.
311 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
312 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
313 || (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
314 || (pos.pawn_attacks(us, to) & pos.pawns(them))
315 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
317 mlist[n++].move = make_move(ksq, to);
320 // Generate evasions for other pieces only if not double check. We use a
321 // simple bit twiddling hack here rather than calling count_1s in order to
322 // save some time (we know that pos.checkers() has at most two nonzero bits).
323 Bitboard checkers = pos.checkers();
325 if (!(checkers & (checkers - 1))) // Only one bit set?
327 Square checksq = first_1(checkers);
329 assert(pos.color_of_piece_on(checksq) == them);
331 // Find pinned pieces
332 Bitboard not_pinned = ~pos.pinned_pieces(us);
334 // Generate captures of the checking piece
337 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
340 from = pop_1st_bit(&b1);
341 if (relative_rank(us, checksq) == RANK_8)
343 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
344 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
345 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
346 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
348 mlist[n++].move = make_move(from, checksq);
352 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
353 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
354 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
358 from = pop_1st_bit(&b1);
359 mlist[n++].move = make_move(from, checksq);
362 // Blocking check evasions are possible only if the checking piece is
364 if (checkers & pos.sliders())
366 Bitboard blockSquares = squares_between(checksq, ksq);
368 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
370 // Pawn moves. Because a blocking evasion can never be a capture, we
371 // only generate pawn pushes.
373 n = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist, n);
375 n = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist, n);
378 b1 = pos.knights(us) & not_pinned;
380 n = generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist, n);
382 b1 = pos.bishops(us) & not_pinned;
384 n = generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist, n);
386 b1 = pos.rooks(us) & not_pinned;
388 n = generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist, n);
390 b1 = pos.queens(us) & not_pinned;
392 n = generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist, n);
395 // Finally, the ugly special case of en passant captures. An en passant
396 // capture can only be a check evasion if the check is not a discovered
397 // check. If pos.ep_square() is set, the last move made must have been
398 // a double pawn push. If, furthermore, the checking piece is a pawn,
399 // an en passant check evasion may be possible.
400 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
402 to = pos.ep_square();
403 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
405 assert(b1 != EmptyBoardBB);
410 from = pop_1st_bit(&b1);
412 // Before generating the move, we have to make sure it is legal.
413 // This is somewhat tricky, because the two disappearing pawns may
414 // cause new "discovered checks". We test this by removing the
415 // two relevant bits from the occupied squares bitboard, and using
416 // the low-level bitboard functions for bishop and rook attacks.
417 b2 = pos.occupied_squares();
418 clear_bit(&b2, from);
419 clear_bit(&b2, checksq);
420 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
421 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
423 mlist[n++].move = make_ep_move(from, to);
431 /// generate_legal_moves() computes a complete list of legal moves in the
432 /// current position. This function is not very fast, and should be used
433 /// only in situations where performance is unimportant. It wouldn't be
434 /// very hard to write an efficient legal move generator, but for the moment
435 /// we don't need it.
437 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
442 return generate_evasions(pos, mlist);
444 // Generate pseudo-legal moves:
445 int n = generate_captures(pos, mlist);
446 n += generate_noncaptures(pos, mlist + n);
448 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
450 // Remove illegal moves from the list:
451 for (int i = 0; i < n; i++)
452 if (!pos.move_is_legal(mlist[i].move, pinned))
453 mlist[i--].move = mlist[--n].move;
459 /// generate_move_if_legal() takes a position and a (not necessarily
460 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
461 /// whether the move is legal. If the move is legal, the move itself is
462 /// returned. If not, the function returns MOVE_NONE. This function must
463 /// only be used when the side to move is not in check.
465 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
468 assert(!pos.is_check());
469 assert(move_is_ok(m));
471 Color us = pos.side_to_move();
472 Color them = opposite_color(us);
473 Square from = move_from(m);
474 Piece pc = pos.piece_on(from);
476 // If the from square is not occupied by a piece belonging to the side to
477 // move, the move is obviously not legal.
478 if (color_of_piece(pc) != us)
481 Square to = move_to(m);
486 // The piece must be a pawn and destination square must be the
487 // en passant square.
488 if ( type_of_piece(pc) != PAWN
489 || to != pos.ep_square())
492 assert(pos.square_is_empty(to));
493 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
495 // The move is pseudo-legal. If it is legal, return it.
496 return (pos.move_is_legal(m) ? m : MOVE_NONE);
500 if (move_is_short_castle(m))
502 // The piece must be a king and side to move must still have
503 // the right to castle kingside.
504 if ( type_of_piece(pc) != KING
505 ||!pos.can_castle_kingside(us))
508 assert(from == pos.king_square(us));
509 assert(to == pos.initial_kr_square(us));
510 assert(pos.piece_on(to) == rook_of_color(us));
512 Square g1 = relative_square(us, SQ_G1);
513 Square f1 = relative_square(us, SQ_F1);
515 bool illegal = false;
517 // Check if any of the squares between king and rook
518 // is occupied or under attack.
519 for (s = Min(from, g1); s <= Max(from, g1); s++)
520 if ( (s != from && s != to && !pos.square_is_empty(s))
521 || pos.square_is_attacked(s, them))
524 // Check if any of the squares between king and rook
526 for (s = Min(to, f1); s <= Max(to, f1); s++)
527 if (s != from && s != to && !pos.square_is_empty(s))
530 return (!illegal ? m : MOVE_NONE);
533 if (move_is_long_castle(m))
535 // The piece must be a king and side to move must still have
536 // the right to castle kingside.
537 if ( type_of_piece(pc) != KING
538 ||!pos.can_castle_queenside(us))
541 assert(from == pos.king_square(us));
542 assert(to == pos.initial_qr_square(us));
543 assert(pos.piece_on(to) == rook_of_color(us));
545 Square c1 = relative_square(us, SQ_C1);
546 Square d1 = relative_square(us, SQ_D1);
548 bool illegal = false;
550 for (s = Min(from, c1); s <= Max(from, c1); s++)
551 if( (s != from && s != to && !pos.square_is_empty(s))
552 || pos.square_is_attacked(s, them))
555 for (s = Min(to, d1); s <= Max(to, d1); s++)
556 if(s != from && s != to && !pos.square_is_empty(s))
559 if ( square_file(to) == FILE_B
560 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
561 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
564 return (!illegal ? m : MOVE_NONE);
569 // The destination square cannot be occupied by a friendly piece
570 if (pos.color_of_piece_on(to) == us)
573 // Proceed according to the type of the moving piece.
574 if (type_of_piece(pc) == PAWN)
576 // If the destination square is on the 8/1th rank, the move must
578 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
579 ||(square_rank(to) == RANK_1 && us != WHITE))
580 && !move_promotion(m))
583 // Proceed according to the square delta between the source and
584 // destionation squares.
591 // Capture. The destination square must be occupied by an enemy
592 // piece (en passant captures was handled earlier).
593 if (pos.color_of_piece_on(to) != them)
599 // Pawn push. The destination square must be empty.
600 if (!pos.square_is_empty(to))
605 // Double white pawn push. The destination square must be on the fourth
606 // rank, and both the destination square and the square between the
607 // source and destination squares must be empty.
608 if ( square_rank(to) != RANK_4
609 || !pos.square_is_empty(to)
610 || !pos.square_is_empty(from + DELTA_N))
615 // Double black pawn push. The destination square must be on the fifth
616 // rank, and both the destination square and the square between the
617 // source and destination squares must be empty.
618 if ( square_rank(to) != RANK_5
619 || !pos.square_is_empty(to)
620 || !pos.square_is_empty(from + DELTA_S))
627 // The move is pseudo-legal. Return it if it is legal.
628 return (pos.move_is_legal(m) ? m : MOVE_NONE);
631 // Luckly we can handle all the other pieces in one go
632 return ( pos.piece_attacks_square(from, to)
633 && pos.move_is_legal(m)
634 && !move_promotion(m) ? m : MOVE_NONE);
641 int generate_pawn_captures(const Position& pos, MoveStack* mlist) {
643 static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
645 Bitboard pawns = pos.pawns(ofs.us);
646 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
650 // Captures in the a1-h8 (a8-h1 for black) direction
651 Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
653 // Capturing promotions
654 Bitboard b2 = b1 & ofs.Rank8BB;
657 sq = pop_1st_bit(&b2);
658 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
661 // Capturing non-promotions
662 b2 = b1 & ~ofs.Rank8BB;
665 sq = pop_1st_bit(&b2);
666 mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
669 // Captures in the h1-a8 (h8-a1 for black) direction
670 b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
672 // Capturing promotions
673 b2 = b1 & ofs.Rank8BB;
676 sq = pop_1st_bit(&b2);
677 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
680 // Capturing non-promotions
681 b2 = b1 & ~ofs.Rank8BB;
684 sq = pop_1st_bit(&b2);
685 mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
688 // Non-capturing promotions
689 b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
692 sq = pop_1st_bit(&b1);
693 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
696 // En passant captures
697 if (pos.ep_square() != SQ_NONE)
699 assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
700 assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
702 b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
703 assert(b1 != EmptyBoardBB);
707 sq = pop_1st_bit(&b1);
708 mlist[n++].move = make_ep_move(sq, pos.ep_square());
716 int generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
718 static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
720 Bitboard pawns = pos.pawns(ofs.us);
721 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
722 Bitboard emptySquares = pos.empty_squares();
727 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
728 b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
731 sq = pop_1st_bit(&b1);
732 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
733 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
734 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
737 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
738 b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
741 sq = pop_1st_bit(&b1);
742 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
743 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
744 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
747 // Single pawn pushes
748 b1 = ofs.forward(pawns) & emptySquares;
749 b2 = b1 & ofs.Rank8BB;
752 sq = pop_1st_bit(&b2);
753 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
754 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
755 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
757 b2 = b1 & ~ofs.Rank8BB;
760 sq = pop_1st_bit(&b2);
761 mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
764 // Double pawn pushes
765 b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
768 sq = pop_1st_bit(&b2);
769 mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
775 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
779 if (pos.can_castle(us))
781 Color them = opposite_color(us);
782 Square ksq = pos.king_square(us);
784 assert(pos.piece_on(ksq) == king_of_color(us));
786 if (pos.can_castle_kingside(us))
788 Square rsq = pos.initial_kr_square(us);
789 Square g1 = relative_square(us, SQ_G1);
790 Square f1 = relative_square(us, SQ_F1);
792 bool illegal = false;
794 assert(pos.piece_on(rsq) == rook_of_color(us));
796 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
797 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
798 || pos.square_is_attacked(s, them))
801 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
802 if (s != ksq && s != rsq && pos.square_is_occupied(s))
806 mlist[n++].move = make_castle_move(ksq, rsq);
809 if (pos.can_castle_queenside(us))
811 Square rsq = pos.initial_qr_square(us);
812 Square c1 = relative_square(us, SQ_C1);
813 Square d1 = relative_square(us, SQ_D1);
815 bool illegal = false;
817 assert(pos.piece_on(rsq) == rook_of_color(us));
819 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
820 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
821 || pos.square_is_attacked(s, them))
824 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
825 if (s != ksq && s != rsq && pos.square_is_occupied(s))
828 if ( square_file(rsq) == FILE_B
829 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
830 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
834 mlist[n++].move = make_castle_move(ksq, rsq);
842 int generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
844 static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
846 // Pawn moves which give discovered check. This is possible only if the
847 // pawn is not on the same file as the enemy king, because we don't
848 // generate captures.
849 Bitboard empty = pos.empty_squares();
851 // Find all friendly pawns not on the enemy king's file
852 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
854 // Discovered checks, single pawn pushes
855 b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
858 Square to = pop_1st_bit(&b3);
859 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
862 // Discovered checks, double pawn pushes
863 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
866 Square to = pop_1st_bit(&b3);
867 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
870 // Direct checks. These are possible only for pawns on neighboring files
873 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
875 // Direct checks, single pawn pushes
876 b2 = (ofs.forward)(b1) & empty;
877 b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
880 Square to = pop_1st_bit(&b3);
881 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
884 // Direct checks, double pawn pushes
885 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
888 Square to = pop_1st_bit(&b3);
889 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
895 int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
896 Bitboard blockSquares, MoveStack* mlist, int n) {
898 static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
900 // Find non-pinned pawns
901 Bitboard b1 = pos.pawns(ofs.us) & not_pinned;
903 // Single pawn pushes. We don't have to AND with empty squares here,
904 // because the blocking squares will always be empty.
905 Bitboard b2 = (ofs.forward)(b1) & blockSquares;
908 Square to = pop_1st_bit(&b2);
910 assert(pos.piece_on(to) == EMPTY);
912 if (square_rank(to) == ofs.RANK_8)
914 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, QUEEN);
915 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, ROOK);
916 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, BISHOP);
917 mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, KNIGHT);
919 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
922 // Double pawn pushes
923 b2 = (ofs.forward)((ofs.forward)(b1) & pos.empty_squares() & ofs.Rank3BB) & blockSquares;
926 Square to = pop_1st_bit(&b2);
928 assert(pos.piece_on(to) == EMPTY);
929 assert(ofs.us != WHITE || square_rank(to) == RANK_4);
930 assert(ofs.us != BLACK || square_rank(to) == RANK_5);
932 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);