]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Fix an assert due to a missing parentesis
[stockfish] / src / movegen.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
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.
9   
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.
14   
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/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #include <cassert>
25
26 #include "movegen.h"
27
28
29 ////
30 //// Local definitions
31 ////
32
33 namespace {
34
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; }
38
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; }
42
43   struct PawnOffsets {
44
45       Bitboard Rank3BB, Rank8BB;
46       Rank RANK_8;
47       SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
48       Color us, them;
49       typedef Bitboard (*Shift_fn)(Bitboard b);
50       Shift_fn forward, forward_left, forward_right;
51   };
52
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 };
55
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 };
58   
59   int generate_pawn_captures(const PawnOffsets&, const Position&, MoveStack*);  
60   int generate_pawn_noncaptures(const PawnOffsets&, const Position&, MoveStack*);
61   int generate_pawn_checks(const PawnOffsets&, const Position&, Bitboard, Square, MoveStack*, int);
62   int generate_piece_checks(PieceType, const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
63   int generate_piece_moves(PieceType, const Position&, MoveStack*, Color, Bitboard);
64   int generate_castle_moves(const Position&, MoveStack*, Color);
65   int generate_piece_blocking_evasions(PieceType, const Position&, Bitboard, Bitboard, MoveStack*, int);
66   int generate_pawn_blocking_evasions(const PawnOffsets&, const Position&, Bitboard, Bitboard, MoveStack*, int);
67 }
68
69
70 ////
71 //// Functions
72 ////
73
74
75 /// generate_captures generates() all pseudo-legal captures and queen
76 /// promotions.  The return value is the number of moves generated.
77
78 int generate_captures(const Position& pos, MoveStack* mlist) {
79
80   assert(pos.is_ok());
81   assert(!pos.is_check());
82
83   Color us = pos.side_to_move();
84   Bitboard target = pos.pieces_of_color(opposite_color(us));
85   int n;
86
87   if (us == WHITE)
88       n = generate_pawn_captures(WhitePawnOffsets, pos, mlist);
89   else
90       n = generate_pawn_captures(BlackPawnOffsets, pos, mlist);
91
92   for (PieceType pce = KNIGHT; pce <= KING; pce++)
93       n += generate_piece_moves(pce, pos, mlist+n, us, target);
94
95   return n;
96 }
97
98
99 /// generate_noncaptures() generates all pseudo-legal non-captures and 
100 /// underpromotions.  The return value is the number of moves generated.
101
102 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
103
104   assert(pos.is_ok());
105   assert(!pos.is_check());
106
107   Color us = pos.side_to_move();
108   Bitboard target = pos.empty_squares();
109   int n;
110
111   if (us == WHITE)
112       n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist);
113   else
114       n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist);
115
116   for (PieceType pce = KNIGHT; pce <= KING; pce++)
117       n += generate_piece_moves(pce, pos, mlist+n, us, target);
118
119   n += generate_castle_moves(pos, mlist+n, us);
120   return n;
121 }
122
123
124 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
125 /// checks, except castling moves (will add this later).  It returns the
126 /// number of generated moves.  
127
128 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
129
130   assert(pos.is_ok());
131   assert(!pos.is_check());
132
133   int n;
134   Color us = pos.side_to_move();
135   Square ksq = pos.king_square(opposite_color(us));
136
137   assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
138
139   dc = pos.discovered_check_candidates(us);
140
141   // Pawn moves
142   if (us == WHITE)    
143      n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
144   else
145      n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
146
147   // Pieces moves
148   Bitboard b = pos.knights(us);
149   if (b)
150       n = generate_piece_checks(KNIGHT, pos, b, dc, ksq, mlist, n);
151
152   b = pos.bishops(us);
153   if (b)
154       n = generate_piece_checks(BISHOP, pos, b, dc, ksq, mlist, n);
155
156   b = pos.rooks(us);
157   if (b)
158       n = generate_piece_checks(ROOK, pos, b, dc, ksq, mlist, n);
159
160   b = pos.queens(us);
161   if (b)
162       n = generate_piece_checks(QUEEN, pos, b, dc, ksq, mlist, n);
163
164   // King moves
165   Square from = pos.king_square(us);
166   if (bit_is_set(dc, from))
167   {
168       b = pos.king_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
169       while (b)
170       {
171           Square to = pop_1st_bit(&b);
172           mlist[n++].move = make_move(from, to);
173       }
174   }
175
176   // TODO: Castling moves!
177   
178   return n;
179 }
180
181
182 /// generate_evasions() generates all check evasions when the side to move is
183 /// in check.  Unlike the other move generation functions, this one generates
184 /// only legal moves.  It returns the number of generated moves.  This
185 /// function is very ugly, and needs cleaning up some time later.  FIXME
186
187 int generate_evasions(const Position& pos, MoveStack* mlist) {
188
189   assert(pos.is_ok());
190   assert(pos.is_check());
191
192   Color us = pos.side_to_move();
193   Color them = opposite_color(us);
194   Square ksq = pos.king_square(us);
195   Square from, to;
196   int n = 0;
197
198   assert(pos.piece_on(ksq) == king_of_color(us));
199   
200   // Generate evasions for king
201   Bitboard b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us);
202   Bitboard b2 = pos.occupied_squares();
203   clear_bit(&b2, ksq);
204
205   while (b1)
206   {
207     Square to = pop_1st_bit(&b1);
208
209     // Make sure to is not attacked by the other side. This is a bit ugly,
210     // because we can't use Position::square_is_attacked. Instead we use
211     // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
212     // b2 (the occupied squares with the king removed) in order to test whether
213     // the king will remain in check on the destination square.
214     if (!(   (bishop_attacks_bb(to, b2)  & pos.bishops_and_queens(them))
215           || (rook_attacks_bb(to, b2)    & pos.rooks_and_queens(them))
216           || (pos.knight_attacks(to)     & pos.knights(them))
217           || (pos.pawn_attacks(us, to)   & pos.pawns(them))
218           || (pos.king_attacks(to)       & pos.kings(them))))
219
220         mlist[n++].move = make_move(ksq, to);
221   }
222
223   // Generate evasions for other pieces only if not double check. We use a
224   // simple bit twiddling hack here rather than calling count_1s in order to
225   // save some time (we know that pos.checkers() has at most two nonzero bits).
226   Bitboard checkers = pos.checkers();
227
228   if (!(checkers & (checkers - 1))) // Only one bit set?
229   {
230       Square checksq = first_1(checkers);
231
232       assert(pos.color_of_piece_on(checksq) == them);
233
234       // Find pinned pieces
235       Bitboard not_pinned = ~pos.pinned_pieces(us);
236
237       // Generate captures of the checking piece
238
239       // Pawn captures
240       b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
241       while (b1)
242       {
243           from = pop_1st_bit(&b1);
244           if (relative_rank(us, checksq) == RANK_8)
245           {
246               mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
247               mlist[n++].move = make_promotion_move(from, checksq, ROOK);
248               mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
249               mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
250           } else
251               mlist[n++].move = make_move(from, checksq);
252       }
253
254       // Pieces captures
255       b1 = (  (pos.knight_attacks(checksq) & pos.knights(us))
256             | (pos.bishop_attacks(checksq) & pos.bishops_and_queens(us))
257             | (pos.rook_attacks(checksq)   & pos.rooks_and_queens(us)) ) & not_pinned;
258
259       while (b1)
260       {
261           from = pop_1st_bit(&b1);
262           mlist[n++].move = make_move(from, checksq);
263       }
264
265       // Blocking check evasions are possible only if the checking piece is
266       // a slider
267       if (checkers & pos.sliders())
268       {
269           Bitboard blockSquares = squares_between(checksq, ksq);
270
271           assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
272
273           // Pawn moves. Because a blocking evasion can never be a capture, we
274           // only generate pawn pushes.
275           if (us == WHITE)
276               n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n);
277           else
278               n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n);
279
280           // Pieces moves
281           b1 = pos.knights(us) & not_pinned;
282           if (b1)
283               n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n);
284
285           b1 = pos.bishops(us) & not_pinned;
286           if (b1)
287               n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n);
288
289           b1 = pos.rooks(us) & not_pinned;
290           if (b1)
291               n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n);
292
293           b1 = pos.queens(us) & not_pinned;
294           if (b1)
295               n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n);
296     }
297
298     // Finally, the ugly special case of en passant captures. An en passant
299     // capture can only be a check evasion if the check is not a discovered
300     // check. If pos.ep_square() is set, the last move made must have been
301     // a double pawn push. If, furthermore, the checking piece is a pawn,
302     // an en passant check evasion may be possible.
303     if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
304     {
305         to = pos.ep_square();
306         b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
307
308         assert(b1 != EmptyBoardBB);
309   
310         b1 &= not_pinned;
311         while (b1)
312         {
313             from = pop_1st_bit(&b1);
314
315             // Before generating the move, we have to make sure it is legal.
316             // This is somewhat tricky, because the two disappearing pawns may
317             // cause new "discovered checks".  We test this by removing the
318             // two relevant bits from the occupied squares bitboard, and using
319             // the low-level bitboard functions for bishop and rook attacks.
320             b2 = pos.occupied_squares();
321             clear_bit(&b2, from);
322             clear_bit(&b2, checksq);
323             if (!(  (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
324                   ||(rook_attacks_bb(ksq, b2)   & pos.rooks_and_queens(them))))
325                 
326                  mlist[n++].move = make_ep_move(from, to);
327         }
328     }
329   }
330   return n;
331 }
332
333
334 /// generate_legal_moves() computes a complete list of legal moves in the
335 /// current position.  This function is not very fast, and should be used
336 /// only in situations where performance is unimportant.  It wouldn't be
337 /// very hard to write an efficient legal move generator, but for the moment
338 /// we don't need it.
339
340 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
341
342   assert(pos.is_ok());
343
344   if (pos.is_check())
345       return generate_evasions(pos, mlist);
346
347   // Generate pseudo-legal moves:
348   int n = generate_captures(pos, mlist);
349   n += generate_noncaptures(pos, mlist + n);
350
351   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
352
353   // Remove illegal moves from the list:
354   for (int i = 0; i < n; i++)
355       if (!pos.move_is_legal(mlist[i].move, pinned))
356           mlist[i--].move = mlist[--n].move;
357
358   return n;
359 }
360
361
362 /// generate_move_if_legal() takes a position and a (not necessarily
363 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
364 /// whether the move is legal.  If the move is legal, the move itself is
365 /// returned.  If not, the function returns MOVE_NONE.  This function must
366 /// only be used when the side to move is not in check.
367
368 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
369
370   assert(pos.is_ok());
371   assert(!pos.is_check());
372   assert(move_is_ok(m));
373
374   Color us = pos.side_to_move();
375   Color them = opposite_color(us);
376   Square from = move_from(m);
377   Piece pc = pos.piece_on(from);
378
379   // If the from square is not occupied by a piece belonging to the side to
380   // move, the move is obviously not legal.
381   if (color_of_piece(pc) != us)
382       return MOVE_NONE;
383
384   Square to = move_to(m);
385
386   // En passant moves
387   if (move_is_ep(m))
388   {
389       // The piece must be a pawn and destination square must be the
390       // en passant square.
391       if (   type_of_piece(pc) != PAWN
392           || to != pos.ep_square())
393           return MOVE_NONE;
394
395       assert(pos.square_is_empty(to));
396       assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
397
398       // The move is pseudo-legal.  If it is legal, return it.
399       return (pos.move_is_legal(m) ? m : MOVE_NONE);
400   }
401
402   // Castling moves
403   if (move_is_short_castle(m))
404   {
405       // The piece must be a king and side to move must still have
406       // the right to castle kingside.
407       if (   type_of_piece(pc) != KING
408           ||!pos.can_castle_kingside(us))
409           return MOVE_NONE;
410
411       assert(from == pos.king_square(us));
412       assert(to == pos.initial_kr_square(us));
413       assert(pos.piece_on(to) == rook_of_color(us));
414
415       Square g1 = relative_square(us, SQ_G1);
416       Square f1 = relative_square(us, SQ_F1);
417       Square s;
418       bool illegal = false;
419
420       // Check if any of the squares between king and rook
421       // is occupied or under attack.
422       for (s = Min(from, g1); s <= Max(from, g1); s++)
423           if (  (s != from && s != to && !pos.square_is_empty(s))
424               || pos.square_is_attacked(s, them))
425               illegal = true;
426
427       // Check if any of the squares between king and rook
428       // is occupied.
429       for (s = Min(to, f1); s <= Max(to, f1); s++)
430           if (s != from && s != to && !pos.square_is_empty(s))
431               illegal = true;
432
433       return (!illegal ? m : MOVE_NONE);
434   }
435
436   if (move_is_long_castle(m))
437   {
438       // The piece must be a king and side to move must still have
439       // the right to castle kingside.
440       if (   type_of_piece(pc) != KING
441           ||!pos.can_castle_queenside(us))
442           return MOVE_NONE;
443
444       assert(from == pos.king_square(us));
445       assert(to == pos.initial_qr_square(us));
446       assert(pos.piece_on(to) == rook_of_color(us));
447
448       Square c1 = relative_square(us, SQ_C1);
449       Square d1 = relative_square(us, SQ_D1);
450       Square s;
451       bool illegal = false;
452
453       for (s = Min(from, c1); s <= Max(from, c1); s++)
454           if(  (s != from && s != to && !pos.square_is_empty(s))
455              || pos.square_is_attacked(s, them))
456               illegal = true;
457
458       for (s = Min(to, d1); s <= Max(to, d1); s++)
459           if(s != from && s != to && !pos.square_is_empty(s))
460               illegal = true;
461
462       if (   square_file(to) == FILE_B
463           && (   pos.piece_on(to + DELTA_W) == rook_of_color(them)
464               || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
465           illegal = true;
466
467       return (!illegal ? m : MOVE_NONE);
468   }
469
470   // Normal moves
471
472   // The destination square cannot be occupied by a friendly piece
473   if (pos.color_of_piece_on(to) == us)
474       return MOVE_NONE;
475
476   // Proceed according to the type of the moving piece.
477   if (type_of_piece(pc) == PAWN)
478   {  
479       // If the destination square is on the 8/1th rank, the move must
480       // be a promotion.
481       if (   (  (square_rank(to) == RANK_8 && us == WHITE)
482               ||(square_rank(to) == RANK_1 && us != WHITE))
483            && !move_promotion(m))
484           return MOVE_NONE;
485
486       // Proceed according to the square delta between the source and
487       // destionation squares.
488       switch (to - from)
489       {
490       case DELTA_NW:
491       case DELTA_NE:
492       case DELTA_SW:
493       case DELTA_SE:
494       // Capture. The destination square must be occupied by an enemy
495       // piece (en passant captures was handled earlier).
496           if (pos.color_of_piece_on(to) != them)
497               return MOVE_NONE;
498           break;
499
500       case DELTA_N:
501       case DELTA_S:
502       // Pawn push. The destination square must be empty.
503           if (!pos.square_is_empty(to))
504               return MOVE_NONE;
505           break;
506
507       case DELTA_NN:
508       // Double white pawn push. The destination square must be on the fourth
509       // rank, and both the destination square and the square between the
510       // source and destination squares must be empty.
511       if (   square_rank(to) != RANK_4
512           || !pos.square_is_empty(to)
513           || !pos.square_is_empty(from + DELTA_N))
514           return MOVE_NONE;
515           break;
516
517       case DELTA_SS:
518       // Double black pawn push. The destination square must be on the fifth
519       // rank, and both the destination square and the square between the
520       // source and destination squares must be empty.
521           if (   square_rank(to) != RANK_5
522               || !pos.square_is_empty(to)
523               || !pos.square_is_empty(from + DELTA_S))
524               return MOVE_NONE;
525           break;
526
527       default:
528           return MOVE_NONE;
529       }
530       // The move is pseudo-legal.  Return it if it is legal.
531       return (pos.move_is_legal(m) ? m : MOVE_NONE);
532   }
533
534   // Luckly we can handle all the other pieces in one go
535   return (   pos.piece_attacks_square(from, to)
536           && pos.move_is_legal(m)
537           && !move_promotion(m) ? m : MOVE_NONE);
538 }
539
540
541 namespace {
542
543   int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
544
545     Bitboard pawns = pos.pawns(ofs.us);
546     Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
547     Square sq;
548     int n = 0;
549
550     // Captures in the a1-h8 (a8-h1 for black) direction
551     Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
552
553     // Capturing promotions
554     Bitboard b2 = b1 & ofs.Rank8BB;
555     while (b2)
556     {
557         sq = pop_1st_bit(&b2);
558         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
559     }
560
561     // Capturing non-promotions
562     b2 = b1 & ~ofs.Rank8BB;
563     while (b2)
564     {
565         sq = pop_1st_bit(&b2);
566         mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
567     }
568
569     // Captures in the h1-a8 (h8-a1 for black) direction
570     b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
571
572     // Capturing promotions
573     b2 = b1 & ofs.Rank8BB;
574     while (b2)
575     {
576         sq = pop_1st_bit(&b2);
577         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
578     }
579
580     // Capturing non-promotions
581     b2 = b1 & ~ofs.Rank8BB;
582     while (b2)
583     {
584         sq = pop_1st_bit(&b2);
585         mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
586     }
587
588     // Non-capturing promotions
589     b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
590     while (b1)
591     {
592         sq = pop_1st_bit(&b1);
593         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
594     }
595
596     // En passant captures
597     if (pos.ep_square() != SQ_NONE)
598     {
599         assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
600         assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
601
602         b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
603         assert(b1 != EmptyBoardBB);
604
605         while (b1)
606         {
607             sq = pop_1st_bit(&b1);
608             mlist[n++].move = make_ep_move(sq, pos.ep_square());
609         }
610     }
611     return n;
612   }
613
614
615   int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
616
617     Bitboard pawns = pos.pawns(ofs.us);
618     Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
619     Bitboard emptySquares = pos.empty_squares();
620     Bitboard b1, b2;
621     Square sq;
622     int n = 0;
623
624     // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
625     b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
626     while (b1)
627     {
628         sq = pop_1st_bit(&b1);
629         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
630         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
631         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
632     }
633
634     // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
635     b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
636     while (b1)
637     {
638         sq = pop_1st_bit(&b1);
639         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
640         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
641         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
642     }
643
644     // Single pawn pushes
645     b1 = ofs.forward(pawns) & emptySquares;
646     b2 = b1 & ofs.Rank8BB;
647     while (b2)
648     {
649       sq = pop_1st_bit(&b2);
650       mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
651       mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
652       mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
653     }
654     b2 = b1 & ~ofs.Rank8BB;
655     while (b2)
656     {
657         sq = pop_1st_bit(&b2);
658         mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
659     }
660
661     // Double pawn pushes
662     b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
663     while (b2)
664     {
665       sq = pop_1st_bit(&b2);
666       mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
667     }
668     return n;
669   }
670
671
672   int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist, 
673                            Color side, Bitboard target) {
674
675     const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
676     Square from, to;
677     Bitboard b;
678     int n = 0;
679
680     for (int i = 0; i < pos.piece_count(side, piece); i++)
681     {
682         from = pos.piece_list(side, piece, i);
683         b = (pos.*mem_fn)(from) & target;
684         while (b)
685         {
686             to = pop_1st_bit(&b);
687             mlist[n++].move = make_move(from, to);
688         }
689     }
690     return n;
691   }
692
693
694   int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
695
696     int n = 0;
697
698     if (pos.can_castle(us))
699     {
700         Color them = opposite_color(us);
701         Square ksq = pos.king_square(us);        
702
703         assert(pos.piece_on(ksq) == king_of_color(us));
704
705         if (pos.can_castle_kingside(us))
706         {
707             Square rsq = pos.initial_kr_square(us);
708             Square g1 = relative_square(us, SQ_G1);
709             Square f1 = relative_square(us, SQ_F1);
710             Square s;
711             bool illegal = false;
712
713             assert(pos.piece_on(rsq) == rook_of_color(us));
714
715             for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
716                 if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
717                     || pos.square_is_attacked(s, them))
718                     illegal = true;
719
720             for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
721                 if (s != ksq && s != rsq && pos.square_is_occupied(s))
722                     illegal = true;
723
724             if (!illegal)
725                 mlist[n++].move = make_castle_move(ksq, rsq);
726       }
727
728       if (pos.can_castle_queenside(us))
729       {
730           Square rsq = pos.initial_qr_square(us);
731           Square c1 = relative_square(us, SQ_C1);
732           Square d1 = relative_square(us, SQ_D1);
733           Square s;
734           bool illegal = false;        
735
736           assert(pos.piece_on(rsq) == rook_of_color(us));
737
738           for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
739               if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
740                   || pos.square_is_attacked(s, them))
741                   illegal = true;
742
743           for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
744               if (s != ksq && s != rsq && pos.square_is_occupied(s))
745                   illegal = true;
746
747         if (   square_file(rsq) == FILE_B
748             && (   pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
749                 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
750             illegal = true;
751                          
752         if (!illegal)
753             mlist[n++].move = make_castle_move(ksq, rsq);
754       }
755     }
756     return n;
757   }
758
759   int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
760                           Bitboard dc, Square ksq, MoveStack* mlist, int n) {
761
762     const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
763
764     // Discovered checks
765     Bitboard b = target & dc;
766     while (b)
767     {
768         Square from = pop_1st_bit(&b);
769         Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
770         while (bb)
771         {
772             Square to = pop_1st_bit(&bb);
773             mlist[n++].move = make_move(from, to);
774         }
775     }
776
777     // Direct checks
778     b = target & ~dc;
779     Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
780     while (b)
781     {
782         Square from = pop_1st_bit(&b);
783         Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
784         while (bb)
785         {
786             Square to = pop_1st_bit(&bb);
787             mlist[n++].move = make_move(from, to);
788         }
789     }
790     return n;
791   }
792
793   int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
794   {
795     // Pawn moves which give discovered check. This is possible only if the 
796     // pawn is not on the same file as the enemy king, because we don't 
797     // generate captures.
798     Bitboard empty = pos.empty_squares();
799
800     // Find all friendly pawns not on the enemy king's file
801     Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
802
803     // Discovered checks, single pawn pushes
804     b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
805     while (b3)
806     {
807         Square to = pop_1st_bit(&b3);
808         mlist[n++].move = make_move(to - ofs.DELTA_N, to);
809     }
810
811     // Discovered checks, double pawn pushes
812     b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
813     while (b3)
814     {
815         Square to = pop_1st_bit(&b3);
816         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
817     }
818
819     // Direct checks. These are possible only for pawns on neighboring files
820     // of the enemy king
821
822     b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
823
824     // Direct checks, single pawn pushes
825     b2 = (ofs.forward)(b1) & empty;
826     b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
827     while (b3)
828     {
829         Square to = pop_1st_bit(&b3);
830         mlist[n++].move = make_move(to - ofs.DELTA_N, to);
831     }
832
833     // Direct checks, double pawn pushes
834     b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
835     while (b3)
836     {
837         Square to = pop_1st_bit(&b3);
838         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
839     }
840     return n;
841   }
842
843
844    int generate_piece_blocking_evasions(PieceType pce, const Position& pos, Bitboard b,
845                                         Bitboard blockSquares, MoveStack* mlist, int n) {
846
847     const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
848
849     while (b)
850     {
851         Square from = pop_1st_bit(&b);
852         Bitboard bb = (pos.*mem_fn)(from) & blockSquares;
853         while (bb)
854         {
855             Square to = pop_1st_bit(&bb);
856             mlist[n++].move = make_move(from, to);
857         }
858     }
859     return n;
860   }
861
862
863   int generate_pawn_blocking_evasions(const PawnOffsets& ofs, const Position& pos, Bitboard not_pinned,
864                                       Bitboard blockSquares, MoveStack* mlist, int n) {
865     // Find non-pinned pawns
866     Bitboard b1 = pos.pawns(ofs.us) & not_pinned;
867
868     // Single pawn pushes. We don't have to AND with empty squares here,
869     // because the blocking squares will always be empty.
870     Bitboard b2 = (ofs.forward)(b1) & blockSquares;
871     while (b2)
872     {
873         Square to = pop_1st_bit(&b2);
874
875         assert(pos.piece_on(to) == EMPTY);
876
877         if (square_rank(to) == ofs.RANK_8)
878         {
879             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, QUEEN);
880             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, ROOK);
881             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, BISHOP);
882             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, KNIGHT);
883         } else
884             mlist[n++].move = make_move(to - ofs.DELTA_N, to);
885     }
886
887     // Double pawn pushes
888     b2 = (ofs.forward)((ofs.forward)(b1) & pos.empty_squares() & ofs.Rank3BB) & blockSquares;
889     while (b2)
890     {
891         Square to = pop_1st_bit(&b2);
892
893         assert(pos.piece_on(to) == EMPTY);
894         assert(ofs.us != WHITE || square_rank(to) == RANK_4);
895         assert(ofs.us != BLACK || square_rank(to) == RANK_5);
896
897         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
898     }
899     return n;
900   }
901 }