]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
d942d9c290c47abc77f90b770f49f4cc56fc7162
[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))
258           & not_pinned;
259
260       while (b1)
261       {
262           from = pop_1st_bit(&b1);
263           mlist[n++].move = make_move(from, checksq);
264       }
265
266       // Blocking check evasions are possible only if the checking piece is
267       // a slider
268       if (checkers & pos.sliders())
269       {
270           Bitboard blockSquares = squares_between(checksq, ksq);
271
272           assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
273
274           // Pawn moves. Because a blocking evasion can never be a capture, we
275           // only generate pawn pushes.
276           if (us == WHITE)
277               n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n);
278           else
279               n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n);
280
281           // Pieces moves
282           b1 = pos.knights(us) & not_pinned;
283           if (b1)
284               n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n);
285
286           b1 = pos.bishops(us) & not_pinned;
287           if (b1)
288               n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n);
289
290           b1 = pos.rooks(us) & not_pinned;
291           if (b1)
292               n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n);
293
294           b1 = pos.queens(us) & not_pinned;
295           if (b1)
296               n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n);
297     }
298
299     // Finally, the ugly special case of en passant captures. An en passant
300     // capture can only be a check evasion if the check is not a discovered
301     // check. If pos.ep_square() is set, the last move made must have been
302     // a double pawn push. If, furthermore, the checking piece is a pawn,
303     // an en passant check evasion may be possible.
304     if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
305     {
306         to = pos.ep_square();
307         b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
308
309         assert(b1 != EmptyBoardBB);
310   
311         b1 &= not_pinned;
312         while (b1)
313         {
314             from = pop_1st_bit(&b1);
315
316             // Before generating the move, we have to make sure it is legal.
317             // This is somewhat tricky, because the two disappearing pawns may
318             // cause new "discovered checks".  We test this by removing the
319             // two relevant bits from the occupied squares bitboard, and using
320             // the low-level bitboard functions for bishop and rook attacks.
321             b2 = pos.occupied_squares();
322             clear_bit(&b2, from);
323             clear_bit(&b2, checksq);
324             if (!(  (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
325                   ||(rook_attacks_bb(ksq, b2)   & pos.rooks_and_queens(them))))
326                 
327                  mlist[n++].move = make_ep_move(from, to);
328         }
329     }
330   }
331   return n;
332 }
333
334
335 /// generate_legal_moves() computes a complete list of legal moves in the
336 /// current position.  This function is not very fast, and should be used
337 /// only in situations where performance is unimportant.  It wouldn't be
338 /// very hard to write an efficient legal move generator, but for the moment
339 /// we don't need it.
340
341 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
342
343   assert(pos.is_ok());
344
345   if (pos.is_check())
346       return generate_evasions(pos, mlist);
347
348   // Generate pseudo-legal moves:
349   int n = generate_captures(pos, mlist);
350   n += generate_noncaptures(pos, mlist + n);
351
352   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
353
354   // Remove illegal moves from the list:
355   for (int i = 0; i < n; i++)
356       if (!pos.move_is_legal(mlist[i].move, pinned))
357           mlist[i--].move = mlist[--n].move;
358
359   return n;
360 }
361
362
363 /// generate_move_if_legal() takes a position and a (not necessarily
364 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
365 /// whether the move is legal.  If the move is legal, the move itself is
366 /// returned.  If not, the function returns MOVE_NONE.  This function must
367 /// only be used when the side to move is not in check.
368
369 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
370
371   assert(pos.is_ok());
372   assert(!pos.is_check());
373   assert(move_is_ok(m));
374
375   Color us = pos.side_to_move();
376   Color them = opposite_color(us);
377   Square from = move_from(m);
378   Piece pc = pos.piece_on(from);
379
380   // If the from square is not occupied by a piece belonging to the side to
381   // move, the move is obviously not legal.
382   if (color_of_piece(pc) != us)
383       return MOVE_NONE;
384
385   Square to = move_to(m);
386
387   // En passant moves
388   if (move_is_ep(m))
389   {
390       // The piece must be a pawn and destination square must be the
391       // en passant square.
392       if (   type_of_piece(pc) != PAWN
393           || to != pos.ep_square())
394           return MOVE_NONE;
395
396       assert(pos.square_is_empty(to));
397       assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
398
399       // The move is pseudo-legal.  If it is legal, return it.
400       return (pos.move_is_legal(m) ? m : MOVE_NONE);
401   }
402
403   // Castling moves
404   if (move_is_short_castle(m))
405   {
406       // The piece must be a king and side to move must still have
407       // the right to castle kingside.
408       if (   type_of_piece(pc) != KING
409           ||!pos.can_castle_kingside(us))
410           return MOVE_NONE;
411
412       assert(from == pos.king_square(us));
413       assert(to == pos.initial_kr_square(us));
414       assert(pos.piece_on(to) == rook_of_color(us));
415
416       Square g1 = relative_square(us, SQ_G1);
417       Square f1 = relative_square(us, SQ_F1);
418       Square s;
419       bool illegal = false;
420
421       // Check if any of the squares between king and rook
422       // is occupied or under attack.
423       for (s = Min(from, g1); s <= Max(from, g1); s++)
424           if (  (s != from && s != to && !pos.square_is_empty(s))
425               || pos.square_is_attacked(s, them))
426               illegal = true;
427
428       // Check if any of the squares between king and rook
429       // is occupied.
430       for (s = Min(to, f1); s <= Max(to, f1); s++)
431           if (s != from && s != to && !pos.square_is_empty(s))
432               illegal = true;
433
434       return (!illegal ? m : MOVE_NONE);
435   }
436
437   if (move_is_long_castle(m))
438   {
439       // The piece must be a king and side to move must still have
440       // the right to castle kingside.
441       if (   type_of_piece(pc) != KING
442           ||!pos.can_castle_queenside(us))
443           return MOVE_NONE;
444
445       assert(from == pos.king_square(us));
446       assert(to == pos.initial_qr_square(us));
447       assert(pos.piece_on(to) == rook_of_color(us));
448
449       Square c1 = relative_square(us, SQ_C1);
450       Square d1 = relative_square(us, SQ_D1);
451       Square s;
452       bool illegal = false;
453
454       for (s = Min(from, c1); s <= Max(from, c1); s++)
455           if(  (s != from && s != to && !pos.square_is_empty(s))
456              || pos.square_is_attacked(s, them))
457               illegal = true;
458
459       for (s = Min(to, d1); s <= Max(to, d1); s++)
460           if(s != from && s != to && !pos.square_is_empty(s))
461               illegal = true;
462
463       if (   square_file(to) == FILE_B
464           && (   pos.piece_on(to + DELTA_W) == rook_of_color(them)
465               || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
466           illegal = true;
467
468       return (!illegal ? m : MOVE_NONE);
469   }
470
471   // Normal moves
472
473   // The destination square cannot be occupied by a friendly piece
474   if (pos.color_of_piece_on(to) == us)
475       return MOVE_NONE;
476
477   // Proceed according to the type of the moving piece.
478   if (type_of_piece(pc) == PAWN)
479   {  
480       // If the destination square is on the 8/1th rank, the move must
481       // be a promotion.
482       if (   (  (square_rank(to) == RANK_8 && us == WHITE)
483               ||(square_rank(to) == RANK_1 && us != WHITE))
484            && !move_promotion(m))
485           return MOVE_NONE;
486
487       // Proceed according to the square delta between the source and
488       // destionation squares.
489       switch (to - from)
490       {
491       case DELTA_NW:
492       case DELTA_NE:
493       case DELTA_SW:
494       case DELTA_SE:
495       // Capture. The destination square must be occupied by an enemy
496       // piece (en passant captures was handled earlier).
497           if (pos.color_of_piece_on(to) != them)
498               return MOVE_NONE;
499           break;
500
501       case DELTA_N:
502       case DELTA_S:
503       // Pawn push. The destination square must be empty.
504           if (!pos.square_is_empty(to))
505               return MOVE_NONE;
506           break;
507
508       case DELTA_NN:
509       // Double white pawn push. The destination square must be on the fourth
510       // rank, and both the destination square and the square between the
511       // source and destination squares must be empty.
512       if (   square_rank(to) != RANK_4
513           || !pos.square_is_empty(to)
514           || !pos.square_is_empty(from + DELTA_N))
515           return MOVE_NONE;
516           break;
517
518       case DELTA_SS:
519       // Double black pawn push. The destination square must be on the fifth
520       // rank, and both the destination square and the square between the
521       // source and destination squares must be empty.
522           if (   square_rank(to) != RANK_5
523               || !pos.square_is_empty(to)
524               || !pos.square_is_empty(from + DELTA_S))
525               return MOVE_NONE;
526           break;
527
528       default:
529           return MOVE_NONE;
530       }
531       // The move is pseudo-legal.  Return it if it is legal.
532       return (pos.move_is_legal(m) ? m : MOVE_NONE);
533   }
534
535   // Luckly we can handle all the other pieces in one go
536   return (   pos.piece_attacks_square(from, to)
537           && pos.move_is_legal(m)
538           && !move_promotion(m) ? m : MOVE_NONE);
539 }
540
541
542 namespace {
543
544   int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
545
546     Bitboard pawns = pos.pawns(ofs.us);
547     Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
548     Square sq;
549     int n = 0;
550
551     // Captures in the a1-h8 (a8-h1 for black) direction
552     Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
553
554     // Capturing promotions
555     Bitboard b2 = b1 & ofs.Rank8BB;
556     while (b2)
557     {
558         sq = pop_1st_bit(&b2);
559         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
560     }
561
562     // Capturing non-promotions
563     b2 = b1 & ~ofs.Rank8BB;
564     while (b2)
565     {
566         sq = pop_1st_bit(&b2);
567         mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
568     }
569
570     // Captures in the h1-a8 (h8-a1 for black) direction
571     b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
572
573     // Capturing promotions
574     b2 = b1 & ofs.Rank8BB;
575     while (b2)
576     {
577         sq = pop_1st_bit(&b2);
578         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
579     }
580
581     // Capturing non-promotions
582     b2 = b1 & ~ofs.Rank8BB;
583     while (b2)
584     {
585         sq = pop_1st_bit(&b2);
586         mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
587     }
588
589     // Non-capturing promotions
590     b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
591     while (b1)
592     {
593         sq = pop_1st_bit(&b1);
594         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
595     }
596
597     // En passant captures
598     if (pos.ep_square() != SQ_NONE)
599     {
600         assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
601         assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
602
603         b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
604         assert(b1 != EmptyBoardBB);
605
606         while (b1)
607         {
608             sq = pop_1st_bit(&b1);
609             mlist[n++].move = make_ep_move(sq, pos.ep_square());
610         }
611     }
612     return n;
613   }
614
615
616   int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
617
618     Bitboard pawns = pos.pawns(ofs.us);
619     Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
620     Bitboard emptySquares = pos.empty_squares();
621     Bitboard b1, b2;
622     Square sq;
623     int n = 0;
624
625     // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
626     b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
627     while (b1)
628     {
629         sq = pop_1st_bit(&b1);
630         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, ROOK);
631         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, BISHOP);
632         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, KNIGHT);
633     }
634
635     // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
636     b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
637     while (b1)
638     {
639         sq = pop_1st_bit(&b1);
640         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, ROOK);
641         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, BISHOP);
642         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, KNIGHT);
643     }
644
645     // Single pawn pushes
646     b1 = ofs.forward(pawns) & emptySquares;
647     b2 = b1 & ofs.Rank8BB;
648     while (b2)
649     {
650       sq = pop_1st_bit(&b2);
651       mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, ROOK);
652       mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, BISHOP);
653       mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, KNIGHT);
654     }
655     b2 = b1 & ~ofs.Rank8BB;
656     while (b2)
657     {
658         sq = pop_1st_bit(&b2);
659         mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
660     }
661
662     // Double pawn pushes
663     b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
664     while (b2)
665     {
666       sq = pop_1st_bit(&b2);
667       mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
668     }
669     return n;
670   }
671
672
673   int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist, 
674                            Color side, Bitboard target) {
675
676     const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
677     Square from, to;
678     Bitboard b;
679     int n = 0;
680
681     for (int i = 0; i < pos.piece_count(side, piece); i++)
682     {
683         from = pos.piece_list(side, piece, i);
684         b = (pos.*mem_fn)(from) & target;
685         while (b)
686         {
687             to = pop_1st_bit(&b);
688             mlist[n++].move = make_move(from, to);
689         }
690     }
691     return n;
692   }
693
694
695   int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
696
697     int n = 0;
698
699     if (pos.can_castle(us))
700     {
701         Color them = opposite_color(us);
702         Square ksq = pos.king_square(us);        
703
704         assert(pos.piece_on(ksq) == king_of_color(us));
705
706         if (pos.can_castle_kingside(us))
707         {
708             Square rsq = pos.initial_kr_square(us);
709             Square g1 = relative_square(us, SQ_G1);
710             Square f1 = relative_square(us, SQ_F1);
711             Square s;
712             bool illegal = false;
713
714             assert(pos.piece_on(rsq) == rook_of_color(us));
715
716             for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
717                 if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
718                     || pos.square_is_attacked(s, them))
719                     illegal = true;
720
721             for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
722                 if (s != ksq && s != rsq && pos.square_is_occupied(s))
723                     illegal = true;
724
725             if (!illegal)
726                 mlist[n++].move = make_castle_move(ksq, rsq);
727       }
728
729       if (pos.can_castle_queenside(us))
730       {
731           Square rsq = pos.initial_qr_square(us);
732           Square c1 = relative_square(us, SQ_C1);
733           Square d1 = relative_square(us, SQ_D1);
734           Square s;
735           bool illegal = false;        
736
737           assert(pos.piece_on(rsq) == rook_of_color(us));
738
739           for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
740               if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
741                   || pos.square_is_attacked(s, them))
742                   illegal = true;
743
744           for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
745               if (s != ksq && s != rsq && pos.square_is_occupied(s))
746                   illegal = true;
747
748         if (   square_file(rsq) == FILE_B
749             && (   pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
750                 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
751             illegal = true;
752                          
753         if (!illegal)
754             mlist[n++].move = make_castle_move(ksq, rsq);
755       }
756     }
757     return n;
758   }
759
760   int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
761                           Bitboard dc, Square ksq, MoveStack* mlist, int n) {
762
763     const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
764
765     // Discovered checks
766     Bitboard b = target & dc;
767     while (b)
768     {
769         Square from = pop_1st_bit(&b);
770         Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
771         while (bb)
772         {
773             Square to = pop_1st_bit(&bb);
774             mlist[n++].move = make_move(from, to);
775         }
776     }
777
778     // Direct checks
779     b = target & ~dc;
780     Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
781     while (b)
782     {
783         Square from = pop_1st_bit(&b);
784         Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
785         while (bb)
786         {
787             Square to = pop_1st_bit(&bb);
788             mlist[n++].move = make_move(from, to);
789         }
790     }
791     return n;
792   }
793
794   int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
795   {
796     // Pawn moves which give discovered check. This is possible only if the 
797     // pawn is not on the same file as the enemy king, because we don't 
798     // generate captures.
799     Bitboard empty = pos.empty_squares();
800
801     // Find all friendly pawns not on the enemy king's file
802     Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
803
804     // Discovered checks, single pawn pushes
805     b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
806     while (b3)
807     {
808         Square to = pop_1st_bit(&b3);
809         mlist[n++].move = make_move(to - ofs.DELTA_N, to);
810     }
811
812     // Discovered checks, double pawn pushes
813     b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
814     while (b3)
815     {
816         Square to = pop_1st_bit(&b3);
817         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
818     }
819
820     // Direct checks. These are possible only for pawns on neighboring files
821     // of the enemy king
822
823     b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
824
825     // Direct checks, single pawn pushes
826     b2 = (ofs.forward)(b1) & empty;
827     b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
828     while (b3)
829     {
830         Square to = pop_1st_bit(&b3);
831         mlist[n++].move = make_move(to - ofs.DELTA_N, to);
832     }
833
834     // Direct checks, double pawn pushes
835     b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
836     while (b3)
837     {
838         Square to = pop_1st_bit(&b3);
839         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
840     }
841     return n;
842   }
843
844
845    int generate_piece_blocking_evasions(PieceType pce, const Position& pos, Bitboard b,
846                                         Bitboard blockSquares, MoveStack* mlist, int n) {
847
848     const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
849
850     while (b)
851     {
852         Square from = pop_1st_bit(&b);
853         Bitboard bb = (pos.*mem_fn)(from) & blockSquares;
854         while (bb)
855         {
856             Square to = pop_1st_bit(&bb);
857             mlist[n++].move = make_move(from, to);
858         }
859     }
860     return n;
861   }
862
863
864   int generate_pawn_blocking_evasions(const PawnOffsets& ofs, const Position& pos, Bitboard not_pinned,
865                                       Bitboard blockSquares, MoveStack* mlist, int n) {
866     // Find non-pinned pawns
867     Bitboard b1 = pos.pawns(ofs.us) & not_pinned;
868
869     // Single pawn pushes. We don't have to AND with empty squares here,
870     // because the blocking squares will always be empty.
871     Bitboard b2 = (ofs.forward)(b1) & blockSquares;
872     while (b2)
873     {
874         Square to = pop_1st_bit(&b2);
875
876         assert(pos.piece_on(to) == EMPTY);
877
878         if (square_rank(to) == ofs.RANK_8)
879         {
880             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, QUEEN);
881             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, ROOK);
882             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, BISHOP);
883             mlist[n++].move = make_promotion_move(to - ofs.DELTA_N, to, KNIGHT);
884         } else
885             mlist[n++].move = make_move(to - ofs.DELTA_N, to);
886     }
887
888     // Double pawn pushes
889     b2 = (ofs.forward)((ofs.forward)(b1) & pos.empty_squares() & ofs.Rank3BB) & blockSquares;
890     while (b2)
891     {
892         Square to = pop_1st_bit(&b2);
893
894         assert(pos.piece_on(to) == EMPTY);
895         assert(ofs.us != WHITE || square_rank(to) == RANK_4);
896         assert(ofs.us != BLACK || square_rank(to) == RANK_5);
897
898         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
899     }
900     return n;
901   }
902 }