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;
46 SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
48 typedef Bitboard (*Shift_fn)(Bitboard b);
49 Shift_fn forward, forward_left, forward_right;
52 const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK,
53 &forward_white, forward_left_white, forward_right_white };
55 const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE,
56 &forward_black, &forward_left_black, &forward_right_black };
58 int generate_pawn_captures(const PawnOffsets&, const Position&, MoveStack*);
59 int generate_pawn_noncaptures(const PawnOffsets&, const Position&, MoveStack*);
60 int generate_pawn_checks(const PawnOffsets&, const Position&, Bitboard, Square, MoveStack*, int);
61 int generate_piece_checks(PieceType, const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
62 int generate_piece_moves(PieceType, const Position&, MoveStack*, Color, Bitboard);
63 int generate_castle_moves(const Position&, MoveStack*, Color);
64 int generate_piece_blocking_evasions(PieceType, const Position&, Bitboard, Bitboard, MoveStack*, int);
73 /// generate_captures generates() all pseudo-legal captures and queen
74 /// promotions. The return value is the number of moves generated.
76 int generate_captures(const Position& pos, MoveStack* mlist) {
79 assert(!pos.is_check());
81 Color us = pos.side_to_move();
82 Bitboard target = pos.pieces_of_color(opposite_color(us));
86 n = generate_pawn_captures(WhitePawnOffsets, pos, mlist);
88 n = generate_pawn_captures(BlackPawnOffsets, pos, mlist);
90 for (PieceType pce = KNIGHT; pce <= KING; pce++)
91 n += generate_piece_moves(pce, pos, mlist+n, us, target);
97 /// generate_noncaptures() generates all pseudo-legal non-captures and
98 /// underpromotions. The return value is the number of moves generated.
100 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
103 assert(!pos.is_check());
105 Color us = pos.side_to_move();
106 Bitboard target = pos.empty_squares();
110 n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist);
112 n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist);
114 for (PieceType pce = KNIGHT; pce <= KING; pce++)
115 n += generate_piece_moves(pce, pos, mlist+n, us, target);
117 n += generate_castle_moves(pos, mlist+n, us);
122 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
123 /// checks, except castling moves (will add this later). It returns the
124 /// number of generated moves.
126 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
129 assert(!pos.is_check());
132 Color us = pos.side_to_move();
133 Square ksq = pos.king_square(opposite_color(us));
135 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
137 dc = pos.discovered_check_candidates(us);
141 n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
143 n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
146 Bitboard b = pos.knights(us);
148 n = generate_piece_checks(KNIGHT, pos, b, dc, ksq, mlist, n);
152 n = generate_piece_checks(BISHOP, pos, b, dc, ksq, mlist, n);
156 n = generate_piece_checks(ROOK, pos, b, dc, ksq, mlist, n);
160 n = generate_piece_checks(QUEEN, pos, b, dc, ksq, mlist, n);
163 Square from = pos.king_square(us);
164 if (bit_is_set(dc, from))
166 b = pos.king_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
169 Square to = pop_1st_bit(&b);
170 mlist[n++].move = make_move(from, to);
174 // TODO: Castling moves!
180 /// generate_evasions() generates all check evasions when the side to move is
181 /// in check. Unlike the other move generation functions, this one generates
182 /// only legal moves. It returns the number of generated moves. This
183 /// function is very ugly, and needs cleaning up some time later. FIXME
185 int generate_evasions(const Position& pos, MoveStack* mlist) {
188 assert(pos.is_check());
190 Color us = pos.side_to_move();
191 Color them = opposite_color(us);
192 Square ksq = pos.king_square(us);
196 assert(pos.piece_on(ksq) == king_of_color(us));
198 // Generate evasions for king
199 Bitboard b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us);
200 Bitboard b2 = pos.occupied_squares();
205 Square to = pop_1st_bit(&b1);
207 // Make sure to is not attacked by the other side. This is a bit ugly,
208 // because we can't use Position::square_is_attacked. Instead we use
209 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
210 // b2 (the occupied squares with the king removed) in order to test whether
211 // the king will remain in check on the destination square.
212 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
213 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
214 || (pos.knight_attacks(to) & pos.knights(them))
215 || (pos.pawn_attacks(us, to) & pos.pawns(them))
216 || (pos.king_attacks(to) & pos.kings(them))))
218 mlist[n++].move = make_move(ksq, to);
221 // Generate evasions for other pieces only if not double check. We use a
222 // simple bit twiddling hack here rather than calling count_1s in order to
223 // save some time (we know that pos.checkers() has at most two nonzero bits).
224 Bitboard checkers = pos.checkers();
226 if (!(checkers & (checkers - 1))) // Only one bit set?
228 Square checksq = first_1(checkers);
230 assert(pos.color_of_piece_on(checksq) == them);
232 // Find pinned pieces
233 Bitboard not_pinned = ~pos.pinned_pieces(us);
235 // Generate captures of the checking piece
238 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
241 from = pop_1st_bit(&b1);
242 if (relative_rank(us, checksq) == RANK_8)
244 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
245 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
246 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
247 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
249 mlist[n++].move = make_move(from, checksq);
253 b1 = (pos.knight_attacks(checksq) & pos.knights(us))
254 | (pos.bishop_attacks(checksq) & pos.bishops_and_queens(us))
255 | (pos.rook_attacks(checksq) & pos.rooks_and_queens(us))
260 from = pop_1st_bit(&b1);
261 mlist[n++].move = make_move(from, checksq);
264 // Blocking check evasions are possible only if the checking piece is
266 if (checkers & pos.sliders())
268 Bitboard blockSquares = squares_between(checksq, ksq);
270 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
272 // Pawn moves. Because a blocking evasion can never be a capture, we
273 // only generate pawn pushes. As so often, the code for pawns is a bit
274 // ugly, and uses separate clauses for white and black pawns. :-(
277 // Find non-pinned pawns
278 b1 = pos.pawns(WHITE) & not_pinned;
280 // Single pawn pushes. We don't have to AND with empty squares here,
281 // because the blocking squares will always be empty.
282 b2 = (b1 << 8) & blockSquares;
285 to = pop_1st_bit(&b2);
287 assert(pos.piece_on(to) == EMPTY);
289 if (square_rank(to) == RANK_8)
291 mlist[n++].move = make_promotion_move(to - DELTA_N, to, QUEEN);
292 mlist[n++].move = make_promotion_move(to - DELTA_N, to, ROOK);
293 mlist[n++].move = make_promotion_move(to - DELTA_N, to, BISHOP);
294 mlist[n++].move = make_promotion_move(to - DELTA_N, to, KNIGHT);
296 mlist[n++].move = make_move(to - DELTA_N, to);
299 // Double pawn pushes
300 b2 = (((b1 << 8) & pos.empty_squares() & Rank3BB) << 8) & blockSquares;
303 to = pop_1st_bit(&b2);
305 assert(pos.piece_on(to) == EMPTY);
306 assert(square_rank(to) == RANK_4);
308 mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
310 } else { // (us == BLACK)
312 // Find non-pinned pawns
313 b1 = pos.pawns(BLACK) & not_pinned;
315 // Single pawn pushes. We don't have to AND with empty squares here,
316 // because the blocking squares will always be empty.
317 b2 = (b1 >> 8) & blockSquares;
320 to = pop_1st_bit(&b2);
322 assert(pos.piece_on(to) == EMPTY);
324 if (square_rank(to) == RANK_1)
326 mlist[n++].move = make_promotion_move(to - DELTA_S, to, QUEEN);
327 mlist[n++].move = make_promotion_move(to - DELTA_S, to, ROOK);
328 mlist[n++].move = make_promotion_move(to - DELTA_S, to, BISHOP);
329 mlist[n++].move = make_promotion_move(to - DELTA_S, to, KNIGHT);
331 mlist[n++].move = make_move(to - DELTA_S, to);
334 // Double pawn pushes
335 b2 = (((b1 >> 8) & pos.empty_squares() & Rank6BB) >> 8) & blockSquares;
338 to = pop_1st_bit(&b2);
340 assert(pos.piece_on(to) == EMPTY);
341 assert(square_rank(to) == RANK_5);
343 mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
348 b1 = pos.knights(us) & not_pinned;
350 n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n);
352 b1 = pos.bishops(us) & not_pinned;
354 n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n);
357 b1 = pos.rooks(us) & not_pinned;
359 n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n);
362 b1 = pos.queens(us) & not_pinned;
364 n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n);
367 // Finally, the ugly special case of en passant captures. An en passant
368 // capture can only be a check evasion if the check is not a discovered
369 // check. If pos.ep_square() is set, the last move made must have been
370 // a double pawn push. If, furthermore, the checking piece is a pawn,
371 // an en passant check evasion may be possible.
372 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
374 to = pos.ep_square();
375 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
377 assert(b1 != EmptyBoardBB);
382 from = pop_1st_bit(&b1);
384 // Before generating the move, we have to make sure it is legal.
385 // This is somewhat tricky, because the two disappearing pawns may
386 // cause new "discovered checks". We test this by removing the
387 // two relevant bits from the occupied squares bitboard, and using
388 // the low-level bitboard functions for bishop and rook attacks.
389 b2 = pos.occupied_squares();
390 clear_bit(&b2, from);
391 clear_bit(&b2, checksq);
392 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
393 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
395 mlist[n++].move = make_ep_move(from, to);
403 /// generate_legal_moves() computes a complete list of legal moves in the
404 /// current position. This function is not very fast, and should be used
405 /// only in situations where performance is unimportant. It wouldn't be
406 /// very hard to write an efficient legal move generator, but for the moment
407 /// we don't need it.
409 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
414 return generate_evasions(pos, mlist);
416 // Generate pseudo-legal moves:
417 int n = generate_captures(pos, mlist);
418 n += generate_noncaptures(pos, mlist + n);
420 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
422 // Remove illegal moves from the list:
423 for (int i = 0; i < n; i++)
424 if (!pos.move_is_legal(mlist[i].move, pinned))
425 mlist[i--].move = mlist[--n].move;
431 /// generate_move_if_legal() takes a position and a (not necessarily
432 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
433 /// whether the move is legal. If the move is legal, the move itself is
434 /// returned. If not, the function returns MOVE_NONE. This function must
435 /// only be used when the side to move is not in check.
437 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
440 assert(!pos.is_check());
441 assert(move_is_ok(m));
443 Color us = pos.side_to_move();
444 Color them = opposite_color(us);
445 Square from = move_from(m);
446 Piece pc = pos.piece_on(from);
448 // If the from square is not occupied by a piece belonging to the side to
449 // move, the move is obviously not legal.
450 if (color_of_piece(pc) != us)
453 Square to = move_to(m);
458 // The piece must be a pawn and destination square must be the
459 // en passant square.
460 if ( type_of_piece(pc) != PAWN
461 || to != pos.ep_square())
464 assert(pos.square_is_empty(to));
465 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
467 // The move is pseudo-legal. If it is legal, return it.
468 return (pos.move_is_legal(m) ? m : MOVE_NONE);
472 if (move_is_short_castle(m))
474 // The piece must be a king and side to move must still have
475 // the right to castle kingside.
476 if ( type_of_piece(pc) != KING
477 ||!pos.can_castle_kingside(us))
480 assert(from == pos.king_square(us));
481 assert(to == pos.initial_kr_square(us));
482 assert(pos.piece_on(to) == rook_of_color(us));
484 Square g1 = relative_square(us, SQ_G1);
485 Square f1 = relative_square(us, SQ_F1);
487 bool illegal = false;
489 // Check if any of the squares between king and rook
490 // is occupied or under attack.
491 for (s = Min(from, g1); s <= Max(from, g1); s++)
492 if ( (s != from && s != to && !pos.square_is_empty(s))
493 || pos.square_is_attacked(s, them))
496 // Check if any of the squares between king and rook
498 for (s = Min(to, f1); s <= Max(to, f1); s++)
499 if (s != from && s != to && !pos.square_is_empty(s))
502 return (!illegal ? m : MOVE_NONE);
505 if (move_is_long_castle(m))
507 // The piece must be a king and side to move must still have
508 // the right to castle kingside.
509 if ( type_of_piece(pc) != KING
510 ||!pos.can_castle_queenside(us))
513 assert(from == pos.king_square(us));
514 assert(to == pos.initial_qr_square(us));
515 assert(pos.piece_on(to) == rook_of_color(us));
517 Square c1 = relative_square(us, SQ_C1);
518 Square d1 = relative_square(us, SQ_D1);
520 bool illegal = false;
522 for (s = Min(from, c1); s <= Max(from, c1); s++)
523 if( (s != from && s != to && !pos.square_is_empty(s))
524 || pos.square_is_attacked(s, them))
527 for (s = Min(to, d1); s <= Max(to, d1); s++)
528 if(s != from && s != to && !pos.square_is_empty(s))
531 if ( square_file(to) == FILE_B
532 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
533 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
536 return (!illegal ? m : MOVE_NONE);
541 // The destination square cannot be occupied by a friendly piece
542 if (pos.color_of_piece_on(to) == us)
545 // Proceed according to the type of the moving piece.
546 if (type_of_piece(pc) == PAWN)
548 // If the destination square is on the 8/1th rank, the move must
550 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
551 ||(square_rank(to) == RANK_1 && us != WHITE))
552 && !move_promotion(m))
555 // Proceed according to the square delta between the source and
556 // destionation squares.
563 // Capture. The destination square must be occupied by an enemy
564 // piece (en passant captures was handled earlier).
565 if (pos.color_of_piece_on(to) != them)
571 // Pawn push. The destination square must be empty.
572 if (!pos.square_is_empty(to))
577 // Double white pawn push. The destination square must be on the fourth
578 // rank, and both the destination square and the square between the
579 // source and destination squares must be empty.
580 if ( square_rank(to) != RANK_4
581 || !pos.square_is_empty(to)
582 || !pos.square_is_empty(from + DELTA_N))
587 // Double black pawn push. The destination square must be on the fifth
588 // rank, and both the destination square and the square between the
589 // source and destination squares must be empty.
590 if ( square_rank(to) != RANK_5
591 || !pos.square_is_empty(to)
592 || !pos.square_is_empty(from + DELTA_S))
599 // The move is pseudo-legal. Return it if it is legal.
600 return (pos.move_is_legal(m) ? m : MOVE_NONE);
603 // Luckly we can handle all the other pieces in one go
604 return ( pos.piece_attacks_square(from, to)
605 && pos.move_is_legal(m)
606 && !move_promotion(m) ? m : MOVE_NONE);
612 int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
614 Bitboard pawns = pos.pawns(ofs.us);
615 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
619 // Captures in the a1-h8 (a8-h1 for black) direction
620 Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
622 // Capturing promotions
623 Bitboard b2 = b1 & ofs.Rank8BB;
626 sq = pop_1st_bit(&b2);
627 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
630 // Capturing non-promotions
631 b2 = b1 & ~ofs.Rank8BB;
634 sq = pop_1st_bit(&b2);
635 mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
638 // Captures in the h1-a8 (h8-a1 for black) direction
639 b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
641 // Capturing promotions
642 b2 = b1 & ofs.Rank8BB;
645 sq = pop_1st_bit(&b2);
646 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
649 // Capturing non-promotions
650 b2 = b1 & ~ofs.Rank8BB;
653 sq = pop_1st_bit(&b2);
654 mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
657 // Non-capturing promotions
658 b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
661 sq = pop_1st_bit(&b1);
662 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
665 // En passant captures
666 if (pos.ep_square() != SQ_NONE)
668 assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
669 assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
671 b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
672 assert(b1 != EmptyBoardBB);
676 sq = pop_1st_bit(&b1);
677 mlist[n++].move = make_ep_move(sq, pos.ep_square());
684 int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
686 Bitboard pawns = pos.pawns(ofs.us);
687 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
688 Bitboard emptySquares = pos.empty_squares();
693 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
694 b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
697 sq = pop_1st_bit(&b1);
698 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
699 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
700 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
703 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
704 b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
707 sq = pop_1st_bit(&b1);
708 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
709 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
710 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
713 // Single pawn pushes
714 b1 = ofs.forward(pawns) & emptySquares;
715 b2 = b1 & ofs.Rank8BB;
718 sq = pop_1st_bit(&b2);
719 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
720 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
721 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
723 b2 = b1 & ~ofs.Rank8BB;
726 sq = pop_1st_bit(&b2);
727 mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
730 // Double pawn pushes
731 b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
734 sq = pop_1st_bit(&b2);
735 mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
741 int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
742 Color side, Bitboard target) {
744 const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
749 for (int i = 0; i < pos.piece_count(side, piece); i++)
751 from = pos.piece_list(side, piece, i);
752 b = (pos.*mem_fn)(from) & target;
755 to = pop_1st_bit(&b);
756 mlist[n++].move = make_move(from, to);
763 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
767 if (pos.can_castle(us))
769 Color them = opposite_color(us);
770 Square ksq = pos.king_square(us);
772 assert(pos.piece_on(ksq) == king_of_color(us));
774 if (pos.can_castle_kingside(us))
776 Square rsq = pos.initial_kr_square(us);
777 Square g1 = relative_square(us, SQ_G1);
778 Square f1 = relative_square(us, SQ_F1);
780 bool illegal = false;
782 assert(pos.piece_on(rsq) == rook_of_color(us));
784 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
785 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
786 || pos.square_is_attacked(s, them))
789 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
790 if (s != ksq && s != rsq && pos.square_is_occupied(s))
794 mlist[n++].move = make_castle_move(ksq, rsq);
797 if (pos.can_castle_queenside(us))
799 Square rsq = pos.initial_qr_square(us);
800 Square c1 = relative_square(us, SQ_C1);
801 Square d1 = relative_square(us, SQ_D1);
803 bool illegal = false;
805 assert(pos.piece_on(rsq) == rook_of_color(us));
807 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
808 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
809 || pos.square_is_attacked(s, them))
812 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
813 if (s != ksq && s != rsq && pos.square_is_occupied(s))
816 if ( square_file(rsq) == FILE_B
817 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
818 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
822 mlist[n++].move = make_castle_move(ksq, rsq);
828 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
829 Bitboard dc, Square ksq, MoveStack* mlist, int n) {
831 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
834 Bitboard b = target & dc;
837 Square from = pop_1st_bit(&b);
838 Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
841 Square to = pop_1st_bit(&bb);
842 mlist[n++].move = make_move(from, to);
848 Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
851 Square from = pop_1st_bit(&b);
852 Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
855 Square to = pop_1st_bit(&bb);
856 mlist[n++].move = make_move(from, to);
862 int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
864 // Pawn moves which give discovered check. This is possible only if the
865 // pawn is not on the same file as the enemy king, because we don't
866 // generate captures.
867 Bitboard empty = pos.empty_squares();
869 // Find all friendly pawns not on the enemy king's file
870 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
872 // Discovered checks, single pawn pushes
873 b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
876 Square to = pop_1st_bit(&b3);
877 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
880 // Discovered checks, double pawn pushes
881 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
884 Square to = pop_1st_bit(&b3);
885 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
888 // Direct checks. These are possible only for pawns on neighboring files
891 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
893 // Direct checks, single pawn pushes
894 b2 = (ofs.forward)(b1) & empty;
895 b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
898 Square to = pop_1st_bit(&b3);
899 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
902 // Direct checks, double pawn pushes
903 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
906 Square to = pop_1st_bit(&b3);
907 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
913 int generate_piece_blocking_evasions(PieceType pce, const Position& pos, Bitboard b,
914 Bitboard blockSquares, MoveStack* mlist, int n) {
916 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
920 Square from = pop_1st_bit(&b);
921 Bitboard bb = (pos.*mem_fn)(from) & blockSquares;
924 Square to = pop_1st_bit(&bb);
925 mlist[n++].move = make_move(from, to);