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 dc, Square ksq, MoveStack*, int n);
61 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target, Bitboard dc, Square ksq, MoveStack* mlist, int n);
62 int generate_piece_moves(PieceType, const Position&, MoveStack*, Color side, Bitboard t);
63 int generate_castle_moves(const Position&, MoveStack*, Color us);
72 /// generate_captures generates() all pseudo-legal captures and queen
73 /// promotions. The return value is the number of moves generated.
75 int generate_captures(const Position& pos, MoveStack* mlist) {
78 assert(!pos.is_check());
80 Color us = pos.side_to_move();
81 Bitboard target = pos.pieces_of_color(opposite_color(us));
85 n = generate_pawn_captures(WhitePawnOffsets, pos, mlist);
87 n = generate_pawn_captures(BlackPawnOffsets, pos, mlist);
89 for (PieceType pce = KNIGHT; pce <= KING; pce++)
90 n += generate_piece_moves(pce, pos, mlist+n, us, target);
96 /// generate_noncaptures() generates all pseudo-legal non-captures and
97 /// underpromotions. The return value is the number of moves generated.
99 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
102 assert(!pos.is_check());
104 Color us = pos.side_to_move();
105 Bitboard target = pos.empty_squares();
109 n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist);
111 n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist);
113 for (PieceType pce = KNIGHT; pce <= KING; pce++)
114 n += generate_piece_moves(pce, pos, mlist+n, us, target);
116 n += generate_castle_moves(pos, mlist+n, us);
121 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
122 /// checks, except castling moves (will add this later). It returns the
123 /// number of generated moves.
125 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
128 assert(!pos.is_check());
131 Color us = pos.side_to_move();
132 Square ksq = pos.king_square(opposite_color(us));
134 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
136 dc = pos.discovered_check_candidates(us);
140 n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
142 n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
145 Bitboard b = pos.knights(us);
147 n = generate_piece_checks(KNIGHT, pos, b, dc, ksq, mlist, n);
151 n = generate_piece_checks(BISHOP, pos, b, dc, ksq, mlist, n);
155 n = generate_piece_checks(ROOK, pos, b, dc, ksq, mlist, n);
159 n = generate_piece_checks(QUEEN, pos, b, dc, ksq, mlist, n);
162 Square from = pos.king_square(us);
163 if (bit_is_set(dc, from))
165 b = pos.king_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
168 Square to = pop_1st_bit(&b);
169 mlist[n++].move = make_move(from, to);
173 // TODO: Castling moves!
179 /// generate_evasions() generates all check evasions when the side to move is
180 /// in check. Unlike the other move generation functions, this one generates
181 /// only legal moves. It returns the number of generated moves. This
182 /// function is very ugly, and needs cleaning up some time later. FIXME
184 int generate_evasions(const Position& pos, MoveStack* mlist) {
187 assert(pos.is_check());
189 Color us = pos.side_to_move();
190 Color them = opposite_color(us);
191 Square ksq = pos.king_square(us);
195 assert(pos.piece_on(ksq) == king_of_color(us));
197 // Generate evasions for king
198 Bitboard b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us);
199 Bitboard b2 = pos.occupied_squares();
204 Square to = pop_1st_bit(&b1);
206 // Make sure to is not attacked by the other side. This is a bit ugly,
207 // because we can't use Position::square_is_attacked. Instead we use
208 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
209 // b2 (the occupied squares with the king removed) in order to test whether
210 // the king will remain in check on the destination square.
211 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
212 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
213 || (pos.knight_attacks(to) & pos.knights(them))
214 || (pos.pawn_attacks(us, to) & pos.pawns(them))
215 || (pos.king_attacks(to) & pos.kings(them))))
217 mlist[n++].move = make_move(ksq, to);
220 // Generate evasions for other pieces only if not double check. We use a
221 // simple bit twiddling hack here rather than calling count_1s in order to
222 // save some time (we know that pos.checkers() has at most two nonzero bits).
223 Bitboard checkers = pos.checkers();
225 if (!(checkers & (checkers - 1))) // Only one bit set?
227 Square checksq = first_1(checkers);
229 assert(pos.color_of_piece_on(checksq) == them);
231 // Find pinned pieces
232 Bitboard not_pinned = ~pos.pinned_pieces(us);
234 // Generate captures of the checking piece
237 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
240 from = pop_1st_bit(&b1);
241 if (relative_rank(us, checksq) == RANK_8)
243 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
244 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
245 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
246 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
248 mlist[n++].move = make_move(from, checksq);
252 b1 = pos.knight_attacks(checksq) & pos.knights(us)
253 & pos.bishop_attacks(checksq) & pos.bishops_and_queens(us)
254 & pos.rook_attacks(checksq) & pos.rooks_and_queens(us)
259 from = pop_1st_bit(&b1);
260 mlist[n++].move = make_move(from, checksq);
263 // Blocking check evasions are possible only if the checking piece is
265 if (checkers & pos.sliders())
267 Bitboard blockSquares = squares_between(checksq, ksq);
269 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
271 // Pawn moves. Because a blocking evasion can never be a capture, we
272 // only generate pawn pushes. As so often, the code for pawns is a bit
273 // ugly, and uses separate clauses for white and black pawns. :-(
276 // Find non-pinned pawns
277 b1 = pos.pawns(WHITE) & not_pinned;
279 // Single pawn pushes. We don't have to AND with empty squares here,
280 // because the blocking squares will always be empty.
281 b2 = (b1 << 8) & blockSquares;
284 to = pop_1st_bit(&b2);
286 assert(pos.piece_on(to) == EMPTY);
288 if (square_rank(to) == RANK_8)
290 mlist[n++].move = make_promotion_move(to - DELTA_N, to, QUEEN);
291 mlist[n++].move = make_promotion_move(to - DELTA_N, to, ROOK);
292 mlist[n++].move = make_promotion_move(to - DELTA_N, to, BISHOP);
293 mlist[n++].move = make_promotion_move(to - DELTA_N, to, KNIGHT);
295 mlist[n++].move = make_move(to - DELTA_N, to);
298 // Double pawn pushes
299 b2 = (((b1 << 8) & pos.empty_squares() & Rank3BB) << 8) & blockSquares;
302 to = pop_1st_bit(&b2);
304 assert(pos.piece_on(to) == EMPTY);
305 assert(square_rank(to) == RANK_4);
307 mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
309 } else { // (us == BLACK)
311 // Find non-pinned pawns
312 b1 = pos.pawns(BLACK) & not_pinned;
314 // Single pawn pushes. We don't have to AND with empty squares here,
315 // because the blocking squares will always be empty.
316 b2 = (b1 >> 8) & blockSquares;
319 to = pop_1st_bit(&b2);
321 assert(pos.piece_on(to) == EMPTY);
323 if (square_rank(to) == RANK_1)
325 mlist[n++].move = make_promotion_move(to - DELTA_S, to, QUEEN);
326 mlist[n++].move = make_promotion_move(to - DELTA_S, to, ROOK);
327 mlist[n++].move = make_promotion_move(to - DELTA_S, to, BISHOP);
328 mlist[n++].move = make_promotion_move(to - DELTA_S, to, KNIGHT);
330 mlist[n++].move = make_move(to - DELTA_S, to);
333 // Double pawn pushes
334 b2 = (((b1 >> 8) & pos.empty_squares() & Rank6BB) >> 8) & blockSquares;
337 to = pop_1st_bit(&b2);
339 assert(pos.piece_on(to) == EMPTY);
340 assert(square_rank(to) == RANK_5);
342 mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
347 b1 = pos.knights(us) & not_pinned;
350 from = pop_1st_bit(&b1);
351 b2 = pos.knight_attacks(from) & blockSquares;
354 to = pop_1st_bit(&b2);
355 mlist[n++].move = make_move(from, to);
360 b1 = pos.bishops(us) & not_pinned;
363 from = pop_1st_bit(&b1);
364 b2 = pos.bishop_attacks(from) & blockSquares;
367 to = pop_1st_bit(&b2);
368 mlist[n++].move = make_move(from, to);
373 b1 = pos.rooks(us) & not_pinned;
376 from = pop_1st_bit(&b1);
377 b2 = pos.rook_attacks(from) & blockSquares;
380 to = pop_1st_bit(&b2);
381 mlist[n++].move = make_move(from, to);
386 b1 = pos.queens(us) & not_pinned;
389 from = pop_1st_bit(&b1);
390 b2 = pos.queen_attacks(from) & blockSquares;
393 to = pop_1st_bit(&b2);
394 mlist[n++].move = make_move(from, to);
399 // Finally, the ugly special case of en passant captures. An en passant
400 // capture can only be a check evasion if the check is not a discovered
401 // check. If pos.ep_square() is set, the last move made must have been
402 // a double pawn push. If, furthermore, the checking piece is a pawn,
403 // an en passant check evasion may be possible.
404 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
406 to = pos.ep_square();
407 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
409 assert(b1 != EmptyBoardBB);
414 from = pop_1st_bit(&b1);
416 // Before generating the move, we have to make sure it is legal.
417 // This is somewhat tricky, because the two disappearing pawns may
418 // cause new "discovered checks". We test this by removing the
419 // two relevant bits from the occupied squares bitboard, and using
420 // the low-level bitboard functions for bishop and rook attacks.
421 b2 = pos.occupied_squares();
422 clear_bit(&b2, from);
423 clear_bit(&b2, checksq);
424 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
425 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
427 mlist[n++].move = make_ep_move(from, to);
435 /// generate_legal_moves() computes a complete list of legal moves in the
436 /// current position. This function is not very fast, and should be used
437 /// only in situations where performance is unimportant. It wouldn't be
438 /// very hard to write an efficient legal move generator, but for the moment
439 /// we don't need it.
441 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
446 return generate_evasions(pos, mlist);
448 // Generate pseudo-legal moves:
449 int n = generate_captures(pos, mlist);
450 n += generate_noncaptures(pos, mlist + n);
452 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
454 // Remove illegal moves from the list:
455 for (int i = 0; i < n; i++)
456 if (!pos.move_is_legal(mlist[i].move, pinned))
457 mlist[i--].move = mlist[--n].move;
463 /// generate_move_if_legal() takes a position and a (not necessarily
464 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
465 /// whether the move is legal. If the move is legal, the move itself is
466 /// returned. If not, the function returns MOVE_NONE. This function must
467 /// only be used when the side to move is not in check.
469 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
472 assert(!pos.is_check());
473 assert(move_is_ok(m));
475 Color us = pos.side_to_move();
476 Color them = opposite_color(us);
477 Square from = move_from(m);
478 Piece pc = pos.piece_on(from);
480 // If the from square is not occupied by a piece belonging to the side to
481 // move, the move is obviously not legal.
482 if (color_of_piece(pc) != us)
485 Square to = move_to(m);
490 // The piece must be a pawn and destination square must be the
491 // en passant square.
492 if ( type_of_piece(pc) != PAWN
493 || to != pos.ep_square())
496 assert(pos.square_is_empty(to));
497 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
499 // The move is pseudo-legal. If it is legal, return it.
500 return (pos.move_is_legal(m) ? m : MOVE_NONE);
504 if (move_is_short_castle(m))
506 // The piece must be a king and side to move must still have
507 // the right to castle kingside.
508 if ( type_of_piece(pc) != KING
509 ||!pos.can_castle_kingside(us))
512 assert(from == pos.king_square(us));
513 assert(to == pos.initial_kr_square(us));
514 assert(pos.piece_on(to) == rook_of_color(us));
516 Square g1 = relative_square(us, SQ_G1);
517 Square f1 = relative_square(us, SQ_F1);
519 bool illegal = false;
521 // Check if any of the squares between king and rook
522 // is occupied or under attack.
523 for (s = Min(from, g1); s <= Max(from, g1); s++)
524 if ( (s != from && s != to && !pos.square_is_empty(s))
525 || pos.square_is_attacked(s, them))
528 // Check if any of the squares between king and rook
530 for (s = Min(to, f1); s <= Max(to, f1); s++)
531 if (s != from && s != to && !pos.square_is_empty(s))
534 return (!illegal ? m : MOVE_NONE);
537 if (move_is_long_castle(m))
539 // The piece must be a king and side to move must still have
540 // the right to castle kingside.
541 if ( type_of_piece(pc) != KING
542 ||!pos.can_castle_queenside(us))
545 assert(from == pos.king_square(us));
546 assert(to == pos.initial_qr_square(us));
547 assert(pos.piece_on(to) == rook_of_color(us));
549 Square c1 = relative_square(us, SQ_C1);
550 Square d1 = relative_square(us, SQ_D1);
552 bool illegal = false;
554 for (s = Min(from, c1); s <= Max(from, c1); s++)
555 if( (s != from && s != to && !pos.square_is_empty(s))
556 || pos.square_is_attacked(s, them))
559 for (s = Min(to, d1); s <= Max(to, d1); s++)
560 if(s != from && s != to && !pos.square_is_empty(s))
563 if ( square_file(to) == FILE_B
564 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
565 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
568 return (!illegal ? m : MOVE_NONE);
573 // The destination square cannot be occupied by a friendly piece
574 if (pos.color_of_piece_on(to) == us)
577 // Proceed according to the type of the moving piece.
578 if (type_of_piece(pc) == PAWN)
580 // If the destination square is on the 8/1th rank, the move must
582 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
583 ||(square_rank(to) == RANK_1 && us != WHITE))
584 && !move_promotion(m))
587 // Proceed according to the square delta between the source and
588 // destionation squares.
595 // Capture. The destination square must be occupied by an enemy
596 // piece (en passant captures was handled earlier).
597 if (pos.color_of_piece_on(to) != them)
603 // Pawn push. The destination square must be empty.
604 if (!pos.square_is_empty(to))
609 // Double white pawn push. The destination square must be on the fourth
610 // rank, and both the destination square and the square between the
611 // source and destination squares must be empty.
612 if ( square_rank(to) != RANK_4
613 || !pos.square_is_empty(to)
614 || !pos.square_is_empty(from + DELTA_N))
619 // Double black pawn push. The destination square must be on the fifth
620 // rank, and both the destination square and the square between the
621 // source and destination squares must be empty.
622 if ( square_rank(to) != RANK_5
623 || !pos.square_is_empty(to)
624 || !pos.square_is_empty(from + DELTA_S))
631 // The move is pseudo-legal. Return it if it is legal.
632 return (pos.move_is_legal(m) ? m : MOVE_NONE);
635 // Luckly we can handle all the other pieces in one go
636 return ( pos.piece_attacks_square(from, to)
637 && pos.move_is_legal(m)
638 && !move_promotion(m) ? m : MOVE_NONE);
644 int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
646 Bitboard pawns = pos.pawns(ofs.us);
647 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
651 // Captures in the a1-h8 (a8-h1 for black) direction
652 Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
654 // Capturing promotions
655 Bitboard b2 = b1 & ofs.Rank8BB;
658 sq = pop_1st_bit(&b2);
659 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
662 // Capturing non-promotions
663 b2 = b1 & ~ofs.Rank8BB;
666 sq = pop_1st_bit(&b2);
667 mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
670 // Captures in the h1-a8 (h8-a1 for black) direction
671 b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
673 // Capturing promotions
674 b2 = b1 & ofs.Rank8BB;
677 sq = pop_1st_bit(&b2);
678 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
681 // Capturing non-promotions
682 b2 = b1 & ~ofs.Rank8BB;
685 sq = pop_1st_bit(&b2);
686 mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
689 // Non-capturing promotions
690 b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
693 sq = pop_1st_bit(&b1);
694 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
697 // En passant captures
698 if (pos.ep_square() != SQ_NONE)
700 assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
701 assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
703 b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
704 assert(b1 != EmptyBoardBB);
708 sq = pop_1st_bit(&b1);
709 mlist[n++].move = make_ep_move(sq, pos.ep_square());
716 int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
718 Bitboard pawns = pos.pawns(ofs.us);
719 Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
720 Bitboard emptySquares = pos.empty_squares();
725 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
726 b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
729 sq = pop_1st_bit(&b1);
730 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
731 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
732 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
735 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
736 b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
739 sq = pop_1st_bit(&b1);
740 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
741 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
742 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
745 // Single pawn pushes
746 b1 = ofs.forward(pawns) & emptySquares;
747 b2 = b1 & ofs.Rank8BB;
750 sq = pop_1st_bit(&b2);
751 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
752 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
753 mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
755 b2 = b1 & ~ofs.Rank8BB;
758 sq = pop_1st_bit(&b2);
759 mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
762 // Double pawn pushes
763 b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
766 sq = pop_1st_bit(&b2);
767 mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
773 int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist,
774 Color side, Bitboard target) {
776 const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
781 for (int i = 0; i < pos.piece_count(side, piece); i++)
783 from = pos.piece_list(side, piece, i);
784 b = (pos.*mem_fn)(from) & target;
787 to = pop_1st_bit(&b);
788 mlist[n++].move = make_move(from, to);
795 int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
799 if (pos.can_castle(us))
801 Color them = opposite_color(us);
802 Square ksq = pos.king_square(us);
804 assert(pos.piece_on(ksq) == king_of_color(us));
806 if (pos.can_castle_kingside(us))
808 Square rsq = pos.initial_kr_square(us);
809 Square g1 = relative_square(us, SQ_G1);
810 Square f1 = relative_square(us, SQ_F1);
812 bool illegal = false;
814 assert(pos.piece_on(rsq) == rook_of_color(us));
816 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
817 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
818 || pos.square_is_attacked(s, them))
821 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
822 if (s != ksq && s != rsq && pos.square_is_occupied(s))
826 mlist[n++].move = make_castle_move(ksq, rsq);
829 if (pos.can_castle_queenside(us))
831 Square rsq = pos.initial_qr_square(us);
832 Square c1 = relative_square(us, SQ_C1);
833 Square d1 = relative_square(us, SQ_D1);
835 bool illegal = false;
837 assert(pos.piece_on(rsq) == rook_of_color(us));
839 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
840 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
841 || pos.square_is_attacked(s, them))
844 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
845 if (s != ksq && s != rsq && pos.square_is_occupied(s))
848 if ( square_file(rsq) == FILE_B
849 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
850 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
854 mlist[n++].move = make_castle_move(ksq, rsq);
860 int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
861 Bitboard dc, Square ksq, MoveStack* mlist, int n) {
863 const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
866 Bitboard b = target & dc;
869 Square from = pop_1st_bit(&b);
870 Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
873 Square to = pop_1st_bit(&bb);
874 mlist[n++].move = make_move(from, to);
880 Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
883 Square from = pop_1st_bit(&b);
884 Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
887 Square to = pop_1st_bit(&bb);
888 mlist[n++].move = make_move(from, to);
894 int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
896 // Pawn moves which give discovered check. This is possible only if the
897 // pawn is not on the same file as the enemy king, because we don't
898 // generate captures.
899 Bitboard empty = pos.empty_squares();
901 // Find all friendly pawns not on the enemy king's file
902 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
904 // Discovered checks, single pawn pushes
905 b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
908 Square to = pop_1st_bit(&b3);
909 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
912 // Discovered checks, double pawn pushes
913 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
916 Square to = pop_1st_bit(&b3);
917 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
920 // Direct checks. These are possible only for pawns on neighboring files
923 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
925 // Direct checks, single pawn pushes
926 b2 = (ofs.forward)(b1) & empty;
927 b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
930 Square to = pop_1st_bit(&b3);
931 mlist[n++].move = make_move(to - ofs.DELTA_N, to);
934 // Direct checks, double pawn pushes
935 b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
938 Square to = pop_1st_bit(&b3);
939 mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);