]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Introduce generate_piece_blocking_evasions()
[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       SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
47       Color us, them;
48       typedef Bitboard (*Shift_fn)(Bitboard b);
49       Shift_fn forward, forward_left, forward_right;
50   };
51
52   const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK,
53                                          &forward_white, forward_left_white, forward_right_white };
54
55   const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE,
56                                          &forward_black, &forward_left_black, &forward_right_black };
57   
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);
65 }
66
67
68 ////
69 //// Functions
70 ////
71
72
73 /// generate_captures generates() all pseudo-legal captures and queen
74 /// promotions.  The return value is the number of moves generated.
75
76 int generate_captures(const Position& pos, MoveStack* mlist) {
77
78   assert(pos.is_ok());
79   assert(!pos.is_check());
80
81   Color us = pos.side_to_move();
82   Bitboard target = pos.pieces_of_color(opposite_color(us));
83   int n;
84
85   if (us == WHITE)
86       n = generate_pawn_captures(WhitePawnOffsets, pos, mlist);
87   else
88       n = generate_pawn_captures(BlackPawnOffsets, pos, mlist);
89
90   for (PieceType pce = KNIGHT; pce <= KING; pce++)
91       n += generate_piece_moves(pce, pos, mlist+n, us, target);
92
93   return n;
94 }
95
96
97 /// generate_noncaptures() generates all pseudo-legal non-captures and 
98 /// underpromotions.  The return value is the number of moves generated.
99
100 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
101
102   assert(pos.is_ok());
103   assert(!pos.is_check());
104
105   Color us = pos.side_to_move();
106   Bitboard target = pos.empty_squares();
107   int n;
108
109   if (us == WHITE)
110       n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist);
111   else
112       n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist);
113
114   for (PieceType pce = KNIGHT; pce <= KING; pce++)
115       n += generate_piece_moves(pce, pos, mlist+n, us, target);
116
117   n += generate_castle_moves(pos, mlist+n, us);
118   return n;
119 }
120
121
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.  
125
126 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
127
128   assert(pos.is_ok());
129   assert(!pos.is_check());
130
131   int n;
132   Color us = pos.side_to_move();
133   Square ksq = pos.king_square(opposite_color(us));
134
135   assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
136
137   dc = pos.discovered_check_candidates(us);
138
139   // Pawn moves
140   if (us == WHITE)    
141      n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
142   else
143      n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
144
145   // Pieces moves
146   Bitboard b = pos.knights(us);
147   if (b)
148       n = generate_piece_checks(KNIGHT, pos, b, dc, ksq, mlist, n);
149
150   b = pos.bishops(us);
151   if (b)
152       n = generate_piece_checks(BISHOP, pos, b, dc, ksq, mlist, n);
153
154   b = pos.rooks(us);
155   if (b)
156       n = generate_piece_checks(ROOK, pos, b, dc, ksq, mlist, n);
157
158   b = pos.queens(us);
159   if (b)
160       n = generate_piece_checks(QUEEN, pos, b, dc, ksq, mlist, n);
161
162   // King moves
163   Square from = pos.king_square(us);
164   if (bit_is_set(dc, from))
165   {
166       b = pos.king_attacks(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
167       while (b)
168       {
169           Square to = pop_1st_bit(&b);
170           mlist[n++].move = make_move(from, to);
171       }
172   }
173
174   // TODO: Castling moves!
175   
176   return n;
177 }
178
179
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
184
185 int generate_evasions(const Position& pos, MoveStack* mlist) {
186
187   assert(pos.is_ok());
188   assert(pos.is_check());
189
190   Color us = pos.side_to_move();
191   Color them = opposite_color(us);
192   Square ksq = pos.king_square(us);
193   Square from, to;
194   int n = 0;
195
196   assert(pos.piece_on(ksq) == king_of_color(us));
197   
198   // Generate evasions for king
199   Bitboard b1 = pos.king_attacks(ksq) & ~pos.pieces_of_color(us);
200   Bitboard b2 = pos.occupied_squares();
201   clear_bit(&b2, ksq);
202
203   while (b1)
204   {
205     Square to = pop_1st_bit(&b1);
206
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))))
217
218         mlist[n++].move = make_move(ksq, to);
219   }
220
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();
225
226   if (!(checkers & (checkers - 1))) // Only one bit set?
227   {
228       Square checksq = first_1(checkers);
229
230       assert(pos.color_of_piece_on(checksq) == them);
231
232       // Find pinned pieces
233       Bitboard not_pinned = ~pos.pinned_pieces(us);
234
235       // Generate captures of the checking piece
236
237       // Pawn captures
238       b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
239       while (b1)
240       {
241           from = pop_1st_bit(&b1);
242           if (relative_rank(us, checksq) == RANK_8)
243           {
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);
248           } else
249               mlist[n++].move = make_move(from, checksq);
250       }
251
252       // Pieces captures
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))
256           & not_pinned;
257
258       while (b1)
259       {
260           from = pop_1st_bit(&b1);
261           mlist[n++].move = make_move(from, checksq);
262       }
263
264       // Blocking check evasions are possible only if the checking piece is
265       // a slider
266       if (checkers & pos.sliders())
267       {
268           Bitboard blockSquares = squares_between(checksq, ksq);
269
270           assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
271
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. :-(
275           if (us == WHITE)
276           {
277               // Find non-pinned pawns
278               b1 = pos.pawns(WHITE) & not_pinned;
279
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;
283               while(b2)
284               {
285                   to = pop_1st_bit(&b2);
286
287                   assert(pos.piece_on(to) == EMPTY);
288
289                   if (square_rank(to) == RANK_8)
290                   {
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);
295                   } else
296                       mlist[n++].move = make_move(to - DELTA_N, to);
297               }
298
299               // Double pawn pushes
300               b2 = (((b1 << 8) & pos.empty_squares() & Rank3BB) << 8) & blockSquares;
301               while (b2)
302               {
303                   to = pop_1st_bit(&b2);
304                 
305                   assert(pos.piece_on(to) == EMPTY);
306                   assert(square_rank(to) == RANK_4);
307
308                   mlist[n++].move = make_move(to - DELTA_N - DELTA_N, to);
309               }
310           } else { // (us == BLACK)
311
312               // Find non-pinned pawns
313               b1 = pos.pawns(BLACK) & not_pinned;
314
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;
318               while (b2)
319               {
320                   to = pop_1st_bit(&b2);
321
322                   assert(pos.piece_on(to) == EMPTY);
323         
324                   if (square_rank(to) == RANK_1)
325                   {
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);
330                   } else
331                       mlist[n++].move = make_move(to - DELTA_S, to);
332               }
333
334               // Double pawn pushes
335               b2 = (((b1 >> 8) & pos.empty_squares() & Rank6BB) >> 8) & blockSquares;
336               while (b2)
337               {
338                   to = pop_1st_bit(&b2);
339
340                   assert(pos.piece_on(to) == EMPTY);
341                   assert(square_rank(to) == RANK_5);
342
343                   mlist[n++].move = make_move(to - DELTA_S - DELTA_S, to);
344               }
345           }
346
347           // Pieces moves
348           b1 = pos.knights(us) & not_pinned;
349           if (b1)
350               n = generate_piece_blocking_evasions(KNIGHT, pos, b1, blockSquares, mlist, n);
351
352           b1 = pos.bishops(us) & not_pinned;
353           if (b1)
354               n = generate_piece_blocking_evasions(BISHOP, pos, b1, blockSquares, mlist, n);
355           
356           // Rook moves
357           b1 = pos.rooks(us) & not_pinned;
358           if (b1)
359               n = generate_piece_blocking_evasions(ROOK, pos, b1, blockSquares, mlist, n);
360           
361           // Queen moves
362           b1 = pos.queens(us) & not_pinned;
363           if (b1)
364               n = generate_piece_blocking_evasions(QUEEN, pos, b1, blockSquares, mlist, n);
365     }
366
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)))
373     {
374         to = pos.ep_square();
375         b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
376
377         assert(b1 != EmptyBoardBB);
378   
379         b1 &= not_pinned;
380         while (b1)
381         {
382             from = pop_1st_bit(&b1);
383
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))))
394                 
395                  mlist[n++].move = make_ep_move(from, to);
396         }
397     }
398   }
399   return n;
400 }
401
402
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.
408
409 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
410
411   assert(pos.is_ok());
412
413   if (pos.is_check())
414       return generate_evasions(pos, mlist);
415
416   // Generate pseudo-legal moves:
417   int n = generate_captures(pos, mlist);
418   n += generate_noncaptures(pos, mlist + n);
419
420   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
421
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;
426
427   return n;
428 }
429
430
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.
436
437 Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
438
439   assert(pos.is_ok());
440   assert(!pos.is_check());
441   assert(move_is_ok(m));
442
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);
447
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)
451       return MOVE_NONE;
452
453   Square to = move_to(m);
454
455   // En passant moves
456   if (move_is_ep(m))
457   {
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())
462           return MOVE_NONE;
463
464       assert(pos.square_is_empty(to));
465       assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
466
467       // The move is pseudo-legal.  If it is legal, return it.
468       return (pos.move_is_legal(m) ? m : MOVE_NONE);
469   }
470
471   // Castling moves
472   if (move_is_short_castle(m))
473   {
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))
478           return MOVE_NONE;
479
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));
483
484       Square g1 = relative_square(us, SQ_G1);
485       Square f1 = relative_square(us, SQ_F1);
486       Square s;
487       bool illegal = false;
488
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))
494               illegal = true;
495
496       // Check if any of the squares between king and rook
497       // is occupied.
498       for (s = Min(to, f1); s <= Max(to, f1); s++)
499           if (s != from && s != to && !pos.square_is_empty(s))
500               illegal = true;
501
502       return (!illegal ? m : MOVE_NONE);
503   }
504
505   if (move_is_long_castle(m))
506   {
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))
511           return MOVE_NONE;
512
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));
516
517       Square c1 = relative_square(us, SQ_C1);
518       Square d1 = relative_square(us, SQ_D1);
519       Square s;
520       bool illegal = false;
521
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))
525               illegal = true;
526
527       for (s = Min(to, d1); s <= Max(to, d1); s++)
528           if(s != from && s != to && !pos.square_is_empty(s))
529               illegal = true;
530
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)))
534           illegal = true;
535
536       return (!illegal ? m : MOVE_NONE);
537   }
538
539   // Normal moves
540
541   // The destination square cannot be occupied by a friendly piece
542   if (pos.color_of_piece_on(to) == us)
543       return MOVE_NONE;
544
545   // Proceed according to the type of the moving piece.
546   if (type_of_piece(pc) == PAWN)
547   {  
548       // If the destination square is on the 8/1th rank, the move must
549       // be a promotion.
550       if (   (  (square_rank(to) == RANK_8 && us == WHITE)
551               ||(square_rank(to) == RANK_1 && us != WHITE))
552            && !move_promotion(m))
553           return MOVE_NONE;
554
555       // Proceed according to the square delta between the source and
556       // destionation squares.
557       switch (to - from)
558       {
559       case DELTA_NW:
560       case DELTA_NE:
561       case DELTA_SW:
562       case DELTA_SE:
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)
566               return MOVE_NONE;
567           break;
568
569       case DELTA_N:
570       case DELTA_S:
571       // Pawn push. The destination square must be empty.
572           if (!pos.square_is_empty(to))
573               return MOVE_NONE;
574           break;
575
576       case DELTA_NN:
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))
583           return MOVE_NONE;
584           break;
585
586       case DELTA_SS:
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))
593               return MOVE_NONE;
594           break;
595
596       default:
597           return MOVE_NONE;
598       }
599       // The move is pseudo-legal.  Return it if it is legal.
600       return (pos.move_is_legal(m) ? m : MOVE_NONE);
601   }
602
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);
607 }
608
609
610 namespace {
611
612   int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
613
614     Bitboard pawns = pos.pawns(ofs.us);
615     Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
616     Square sq;
617     int n = 0;
618
619     // Captures in the a1-h8 (a8-h1 for black) direction
620     Bitboard b1 = (ofs.forward_right)(pawns) & ~FileABB & enemyPieces;
621
622     // Capturing promotions
623     Bitboard b2 = b1 & ofs.Rank8BB;
624     while (b2)
625     {
626         sq = pop_1st_bit(&b2);
627         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NE, sq, QUEEN);
628     }
629
630     // Capturing non-promotions
631     b2 = b1 & ~ofs.Rank8BB;
632     while (b2)
633     {
634         sq = pop_1st_bit(&b2);
635         mlist[n++].move = make_move(sq - ofs.DELTA_NE, sq);
636     }
637
638     // Captures in the h1-a8 (h8-a1 for black) direction
639     b1 = (ofs.forward_left)(pawns) & ~FileHBB & enemyPieces;
640
641     // Capturing promotions
642     b2 = b1 & ofs.Rank8BB;
643     while (b2)
644     {
645         sq = pop_1st_bit(&b2);
646         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_NW, sq, QUEEN);
647     }
648
649     // Capturing non-promotions
650     b2 = b1 & ~ofs.Rank8BB;
651     while (b2)
652     {
653         sq = pop_1st_bit(&b2);
654         mlist[n++].move = make_move(sq - ofs.DELTA_NW, sq);
655     }
656
657     // Non-capturing promotions
658     b1 = (ofs.forward)(pawns) & pos.empty_squares() & Rank8BB;
659     while (b1)
660     {
661         sq = pop_1st_bit(&b1);
662         mlist[n++].move = make_promotion_move(sq - ofs.DELTA_N, sq, QUEEN);
663     }
664
665     // En passant captures
666     if (pos.ep_square() != SQ_NONE)
667     {
668         assert(ofs.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
669         assert(ofs.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
670
671         b1 = pawns & pos.pawn_attacks(ofs.them, pos.ep_square());
672         assert(b1 != EmptyBoardBB);
673
674         while (b1)
675         {
676             sq = pop_1st_bit(&b1);
677             mlist[n++].move = make_ep_move(sq, pos.ep_square());
678         }
679     }
680     return n;
681   }
682
683
684   int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
685
686     Bitboard pawns = pos.pawns(ofs.us);
687     Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
688     Bitboard emptySquares = pos.empty_squares();
689     Bitboard b1, b2;
690     Square sq;
691     int n = 0;
692
693     // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
694     b1 = ofs.forward_right(pawns) & ~FileABB & enemyPieces & ofs.Rank8BB;
695     while (b1)
696     {
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);
701     }
702
703     // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
704     b1 = ofs.forward_left(pawns) & ~FileHBB & enemyPieces & ofs.Rank8BB;
705     while (b1)
706     {
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);
711     }
712
713     // Single pawn pushes
714     b1 = ofs.forward(pawns) & emptySquares;
715     b2 = b1 & ofs.Rank8BB;
716     while (b2)
717     {
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);
722     }
723     b2 = b1 & ~ofs.Rank8BB;
724     while (b2)
725     {
726         sq = pop_1st_bit(&b2);
727         mlist[n++].move = make_move(sq - ofs.DELTA_N, sq);
728     }
729
730     // Double pawn pushes
731     b2 = (ofs.forward(b1 & ofs.Rank3BB)) & emptySquares;
732     while (b2)
733     {
734       sq = pop_1st_bit(&b2);
735       mlist[n++].move = make_move(sq - ofs.DELTA_N - ofs.DELTA_N, sq);
736     }
737     return n;
738   }
739
740
741   int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist, 
742                            Color side, Bitboard target) {
743
744     const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
745     Square from, to;
746     Bitboard b;
747     int n = 0;
748
749     for (int i = 0; i < pos.piece_count(side, piece); i++)
750     {
751         from = pos.piece_list(side, piece, i);
752         b = (pos.*mem_fn)(from) & target;
753         while (b)
754         {
755             to = pop_1st_bit(&b);
756             mlist[n++].move = make_move(from, to);
757         }
758     }
759     return n;
760   }
761
762
763   int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
764
765     int n = 0;
766
767     if (pos.can_castle(us))
768     {
769         Color them = opposite_color(us);
770         Square ksq = pos.king_square(us);        
771
772         assert(pos.piece_on(ksq) == king_of_color(us));
773
774         if (pos.can_castle_kingside(us))
775         {
776             Square rsq = pos.initial_kr_square(us);
777             Square g1 = relative_square(us, SQ_G1);
778             Square f1 = relative_square(us, SQ_F1);
779             Square s;
780             bool illegal = false;
781
782             assert(pos.piece_on(rsq) == rook_of_color(us));
783
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))
787                     illegal = true;
788
789             for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
790                 if (s != ksq && s != rsq && pos.square_is_occupied(s))
791                     illegal = true;
792
793             if (!illegal)
794                 mlist[n++].move = make_castle_move(ksq, rsq);
795       }
796
797       if (pos.can_castle_queenside(us))
798       {
799           Square rsq = pos.initial_qr_square(us);
800           Square c1 = relative_square(us, SQ_C1);
801           Square d1 = relative_square(us, SQ_D1);
802           Square s;
803           bool illegal = false;        
804
805           assert(pos.piece_on(rsq) == rook_of_color(us));
806
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))
810                   illegal = true;
811
812           for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
813               if (s != ksq && s != rsq && pos.square_is_occupied(s))
814                   illegal = true;
815
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)))
819             illegal = true;
820                          
821         if (!illegal)
822             mlist[n++].move = make_castle_move(ksq, rsq);
823       }
824     }
825     return n;
826   }
827
828   int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
829                           Bitboard dc, Square ksq, MoveStack* mlist, int n) {
830
831     const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
832
833     // Discovered checks
834     Bitboard b = target & dc;
835     while (b)
836     {
837         Square from = pop_1st_bit(&b);
838         Bitboard bb = (pos.*mem_fn)(from) & pos.empty_squares();
839         while (bb)
840         {
841             Square to = pop_1st_bit(&bb);
842             mlist[n++].move = make_move(from, to);
843         }
844     }
845
846     // Direct checks
847     b = target & ~dc;
848     Bitboard checkSqs = (pos.*mem_fn)(ksq) & pos.empty_squares();
849     while (b)
850     {
851         Square from = pop_1st_bit(&b);
852         Bitboard bb = (pos.*mem_fn)(from) & checkSqs;
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   int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
863   {
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();
868
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;
871
872     // Discovered checks, single pawn pushes
873     b2 = b3 = (ofs.forward)(b1 & dc) & ~ofs.Rank8BB & empty;
874     while (b3)
875     {
876         Square to = pop_1st_bit(&b3);
877         mlist[n++].move = make_move(to - ofs.DELTA_N, to);
878     }
879
880     // Discovered checks, double pawn pushes
881     b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty;
882     while (b3)
883     {
884         Square to = pop_1st_bit(&b3);
885         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
886     }
887
888     // Direct checks. These are possible only for pawns on neighboring files
889     // of the enemy king
890
891     b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
892
893     // Direct checks, single pawn pushes
894     b2 = (ofs.forward)(b1) & empty;
895     b3 = b2 & pos.pawn_attacks(ofs.them, ksq);
896     while (b3)
897     {
898         Square to = pop_1st_bit(&b3);
899         mlist[n++].move = make_move(to - ofs.DELTA_N, to);
900     }
901
902     // Direct checks, double pawn pushes
903     b3 = (ofs.forward)(b2 & ofs.Rank3BB) & empty & pos.pawn_attacks(ofs.them, ksq);
904     while (b3)
905     {
906         Square to = pop_1st_bit(&b3);
907         mlist[n++].move = make_move(to - ofs.DELTA_N - ofs.DELTA_N, to);
908     }
909     return n;
910   }
911
912
913    int generate_piece_blocking_evasions(PieceType pce, const Position& pos, Bitboard b,
914                                         Bitboard blockSquares, MoveStack* mlist, int n) {
915
916     const Piece_attacks_fn mem_fn = piece_attacks_fn[pce];
917
918     while (b)
919     {
920         Square from = pop_1st_bit(&b);
921         Bitboard bb = (pos.*mem_fn)(from) & blockSquares;
922         while (bb)
923         {
924             Square to = pop_1st_bit(&bb);
925             mlist[n++].move = make_move(from, to);
926         }
927     }
928     return n;
929   }
930 }