]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Generate pseudo-legal moves in generate_evasions()
[stockfish] / src / movegen.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2009 Marco Costalba
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26
27 #include "bitcount.h"
28 #include "movegen.h"
29
30 // Simple macro to wrap a very common while loop, no facny, no flexibility,
31 // hardcoded list name 'mlist' and from square 'from'.
32 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
33
34 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
35 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
36
37 ////
38 //// Local definitions
39 ////
40
41 namespace {
42
43   enum CastlingSide {
44     KING_SIDE,
45     QUEEN_SIDE
46   };
47
48   enum MoveType {
49     CAPTURE,
50     NON_CAPTURE,
51     CHECK,
52     EVASION
53   };
54
55   // Helper templates
56   template<CastlingSide Side>
57   MoveStack* generate_castle_moves(const Position&, MoveStack*);
58
59   template<Color Us, MoveType Type>
60   MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard = EmptyBoardBB,
61                                  Square = SQ_NONE, Bitboard = EmptyBoardBB);
62
63   // Template generate_piece_moves (captures and non-captures) with specializations and overloads
64   template<PieceType>
65   MoveStack* generate_piece_moves(const Position&, MoveStack*, Color, Bitboard);
66
67   template<>
68   MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
69
70   template<PieceType Piece, MoveType Type>
71   inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
72
73     assert(Piece == PAWN);
74     assert(Type == CAPTURE || Type == NON_CAPTURE);
75
76     return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m)
77                         : generate_pawn_moves<BLACK, Type>(p, m));
78   }
79
80   // Templates for non-capture checks generation
81
82   template<PieceType Piece>
83   MoveStack* generate_discovered_checks(const Position& pos, Square from, MoveStack* mlist);
84
85   template<PieceType>
86   MoveStack* generate_direct_checks(const Position&, MoveStack*, Color, Bitboard, Square);
87
88   template<>
89   inline MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
90
91     return (us == WHITE ? generate_pawn_moves<WHITE, CHECK>(p, m, dc, ksq)
92                         : generate_pawn_moves<BLACK, CHECK>(p, m, dc, ksq));
93   }
94
95   // Template generate_piece_evasions with specializations
96   template<PieceType>
97   MoveStack* generate_piece_evasions(const Position&, MoveStack*, Color, Bitboard, Bitboard);
98
99   template<>
100   inline MoveStack* generate_piece_evasions<PAWN>(const Position& p, MoveStack* m,
101                                                   Color us, Bitboard t, Bitboard pnd) {
102
103     return (us == WHITE ? generate_pawn_moves<WHITE, EVASION>(p, m, pnd, SQ_NONE, t)
104                         : generate_pawn_moves<BLACK, EVASION>(p, m, pnd, SQ_NONE, t));
105   }
106 }
107
108
109 ////
110 //// Functions
111 ////
112
113
114 /// generate_captures() generates all pseudo-legal captures and queen
115 /// promotions. Returns a pointer to the end of the move list.
116
117 MoveStack* generate_captures(const Position& pos, MoveStack* mlist) {
118
119   assert(pos.is_ok());
120   assert(!pos.is_check());
121
122   Color us = pos.side_to_move();
123   Bitboard target = pos.pieces_of_color(opposite_color(us));
124
125   mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
126   mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
127   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
128   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
129   mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
130   return  generate_piece_moves<KING>(pos, mlist, us, target);
131 }
132
133
134 /// generate_noncaptures() generates all pseudo-legal non-captures and
135 /// underpromotions. Returns a pointer to the end of the move list.
136
137 MoveStack* generate_noncaptures(const Position& pos, MoveStack* mlist) {
138
139   assert(pos.is_ok());
140   assert(!pos.is_check());
141
142   Color us = pos.side_to_move();
143   Bitboard target = pos.empty_squares();
144
145   mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
146   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
147   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
148   mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
149   mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
150   mlist = generate_piece_moves<KING>(pos, mlist, us, target);
151   mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
152   return  generate_castle_moves<QUEEN_SIDE>(pos, mlist);
153 }
154
155
156 /// generate_non_capture_checks() generates all pseudo-legal non-captures and
157 /// underpromotions that give check. Returns a pointer to the end of the move list.
158
159 MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
160
161   assert(pos.is_ok());
162   assert(!pos.is_check());
163
164   Color us = pos.side_to_move();
165   Square ksq = pos.king_square(opposite_color(us));
166
167   assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
168
169   // Discovered non-capture checks
170   Bitboard b = dc;
171   while (b)
172   {
173      Square from = pop_1st_bit(&b);
174      switch (pos.type_of_piece_on(from))
175      {
176       case PAWN:   /* Will be generated togheter with pawns direct checks */     break;
177       case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, from, mlist); break;
178       case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, from, mlist); break;
179       case ROOK:   mlist = generate_discovered_checks<ROOK>(pos, from, mlist);   break;
180       case KING:   mlist = generate_discovered_checks<KING>(pos, from, mlist);   break;
181       default: assert(false); break;
182      }
183   }
184
185   // Direct non-capture checks
186   mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
187   mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
188   mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
189   mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
190   return  generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
191 }
192
193
194 /// generate_evasions() generates all pseudo-legal check evasions when
195 /// the side to move is in check. Returns a pointer to the end of the move list.
196
197 MoveStack* generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
198
199   assert(pos.is_ok());
200   assert(pos.is_check());
201
202   Bitboard b;
203   Square from, to, checksq;
204   int checkersCnt = 0;
205   Color us = pos.side_to_move();
206   Color them = opposite_color(us);
207   Square ksq = pos.king_square(us);
208   Bitboard checkers = pos.checkers();
209   Bitboard sliderAttacks = EmptyBoardBB;
210
211   assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
212   assert(checkers);
213
214   // Find squares attacked by slider checkers, we will remove
215   // them from the king evasions set so to early skip known
216   // illegal moves and avoid an useless legality check later.
217   b = checkers;
218   do
219   {
220       checkersCnt++;
221       checksq = pop_1st_bit(&b);
222
223       assert(pos.color_of_piece_on(checksq) == them);
224
225       switch (pos.type_of_piece_on(checksq))
226       {
227       case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
228       case ROOK:   sliderAttacks |= RookPseudoAttacks[checksq];   break;
229       case QUEEN:
230           // In case of a queen remove also squares attacked in the other direction to
231           // avoid possible illegal moves when queen and king are on adjacent squares.
232           if (direction_is_straight(checksq, ksq))
233               sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
234           else
235               sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
236       default:
237           break;
238       }
239   } while (b);
240
241   // Generate evasions for king, capture and non capture moves
242   b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
243   from = ksq;
244   SERIALIZE_MOVES(b);
245
246   // Generate evasions for other pieces only if not double check
247   if (checkersCnt > 1)
248       return mlist;
249
250   Bitboard target = squares_between(checksq, ksq);
251
252   // Pawn captures
253   b = pos.attacks_from<PAWN>(checksq, them) & pos.pieces(PAWN, us) & ~pinned;
254   while (b)
255   {
256       from = pop_1st_bit(&b);
257       if (relative_rank(us, checksq) == RANK_8)
258       {
259           (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
260           (*mlist++).move = make_promotion_move(from, checksq, ROOK);
261           (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
262           (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
263       } else
264           (*mlist++).move = make_move(from, checksq);
265   }
266
267   // Pawn blocking evasions (possible only if the checking piece is a slider)
268   if (sliderAttacks)
269       mlist = generate_piece_evasions<PAWN>(pos, mlist, us, target, pinned);
270
271   // Add the checking piece to the target squares
272   target |= checkers;
273
274   // Captures and blocking evasions for the other pieces
275   mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
276   mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
277   mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
278   mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
279
280   // Finally, the special case of en passant captures. An en passant
281   // capture can only be a check evasion if the check is not a discovered
282   // check. If pos.ep_square() is set, the last move made must have been
283   // a double pawn push. If, furthermore, the checking piece is a pawn,
284   // an en passant check evasion may be possible.
285   if (pos.ep_square() != SQ_NONE && (checkers & pos.pieces(PAWN, them)))
286   {
287       to = pos.ep_square();
288       b = pos.attacks_from<PAWN>(to, them) & pos.pieces(PAWN, us);
289
290       // The checking pawn cannot be a discovered (bishop) check candidate
291       // otherwise we were in check also before last double push move.
292       assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
293       assert(count_1s(b) == 1 || count_1s(b) == 2);
294
295       b &= ~pinned;
296       while (b)
297       {
298           from = pop_1st_bit(&b);
299           // Move is always legal because checking pawn is not a discovered
300           // check candidate and our capturing pawn has been already tested
301           // against pinned pieces.
302           (*mlist++).move = make_ep_move(from, to);
303       }
304   }
305   return mlist;
306 }
307
308
309 /// generate_moves() computes a complete list of legal or pseudo-legal moves in
310 /// the current position. This function is not very fast, and should be used
311 /// only in non time-critical paths.
312
313 MoveStack* generate_moves(const Position& pos, MoveStack* mlist, bool pseudoLegal) {
314
315   assert(pos.is_ok());
316
317   MoveStack* last;
318   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
319
320   // Generate pseudo-legal moves
321   if (pos.is_check())
322       last = generate_evasions(pos, mlist, pinned);
323   else {
324       last = generate_captures(pos, mlist);
325       last = generate_noncaptures(pos, last);
326   }
327   if (pseudoLegal)
328       return last;
329
330   // Remove illegal moves from the list
331   for (MoveStack* cur = mlist; cur != last; cur++)
332       if (!pos.pl_move_is_legal(cur->move, pinned))
333       {
334           cur->move = (--last)->move;
335           cur--;
336       }
337   return last;
338 }
339
340
341 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
342 /// move and tests whether the move is legal. This version is not very fast
343 /// and should be used only in non time-critical paths.
344
345 bool move_is_legal(const Position& pos, const Move m) {
346
347   MoveStack mlist[256];
348   MoveStack* last = generate_moves(pos, mlist, true);
349   for (MoveStack* cur = mlist; cur != last; cur++)
350       if (cur->move == m)
351           return pos.pl_move_is_legal(m);
352
353   return false;
354 }
355
356
357 /// Fast version of move_is_legal() that takes a position a move and a
358 /// bitboard of pinned pieces as input, and tests whether the move is legal.
359 /// This version must only be used when the side to move is not in check.
360
361 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
362
363   assert(pos.is_ok());
364   assert(!pos.is_check());
365   assert(move_is_ok(m));
366   assert(pinned == pos.pinned_pieces(pos.side_to_move()));
367
368   // Use a slower but simpler function for uncommon cases
369   if (move_is_ep(m) || move_is_castle(m))
370       return move_is_legal(pos, m);
371
372   Color us = pos.side_to_move();
373   Color them = opposite_color(us);
374   Square from = move_from(m);
375   Square to = move_to(m);
376   Piece pc = pos.piece_on(from);
377
378   // If the from square is not occupied by a piece belonging to the side to
379   // move, the move is obviously not legal.
380   if (color_of_piece(pc) != us)
381       return false;
382
383   // The destination square cannot be occupied by a friendly piece
384   if (pos.color_of_piece_on(to) == us)
385       return false;
386
387   // Handle the special case of a pawn move
388   if (type_of_piece(pc) == PAWN)
389   {
390       // Move direction must be compatible with pawn color
391       int direction = to - from;
392       if ((us == WHITE) != (direction > 0))
393           return false;
394
395       // A pawn move is a promotion iff the destination square is
396       // on the 8/1th rank.
397       if ((  (square_rank(to) == RANK_8 && us == WHITE)
398            ||(square_rank(to) == RANK_1 && us != WHITE)) != bool(move_is_promotion(m)))
399           return false;
400
401       // Proceed according to the square delta between the origin and
402       // destination squares.
403       switch (direction)
404       {
405       case DELTA_NW:
406       case DELTA_NE:
407       case DELTA_SW:
408       case DELTA_SE:
409       // Capture. The destination square must be occupied by an enemy
410       // piece (en passant captures was handled earlier).
411           if (pos.color_of_piece_on(to) != them)
412               return false;
413           break;
414
415       case DELTA_N:
416       case DELTA_S:
417       // Pawn push. The destination square must be empty.
418           if (!pos.square_is_empty(to))
419               return false;
420           break;
421
422       case DELTA_NN:
423       // Double white pawn push. The destination square must be on the fourth
424       // rank, and both the destination square and the square between the
425       // source and destination squares must be empty.
426       if (   square_rank(to) != RANK_4
427           || !pos.square_is_empty(to)
428           || !pos.square_is_empty(from + DELTA_N))
429           return false;
430           break;
431
432       case DELTA_SS:
433       // Double black pawn push. The destination square must be on the fifth
434       // rank, and both the destination square and the square between the
435       // source and destination squares must be empty.
436           if (   square_rank(to) != RANK_5
437               || !pos.square_is_empty(to)
438               || !pos.square_is_empty(from + DELTA_S))
439               return false;
440           break;
441
442       default:
443           return false;
444       }
445       // The move is pseudo-legal, check if it is also legal
446       return pos.pl_move_is_legal(m, pinned);
447   }
448
449   // Luckly we can handle all the other pieces in one go
450   return (   bit_is_set(pos.attacks_from(pc, from), to)
451           && pos.pl_move_is_legal(m, pinned)
452           && !move_is_promotion(m));
453 }
454
455
456 namespace {
457
458   template<PieceType Piece>
459   MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
460
461     Square from;
462     Bitboard b;
463     const Square* ptr = pos.piece_list_begin(us, Piece);
464
465     while ((from = *ptr++) != SQ_NONE)
466     {
467         b = pos.attacks_from<Piece>(from) & target;
468         SERIALIZE_MOVES(b);
469     }
470     return mlist;
471   }
472
473   template<>
474   MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
475
476     Bitboard b;
477     Square from = pos.king_square(us);
478
479     b = pos.attacks_from<KING>(from) & target;
480     SERIALIZE_MOVES(b);
481     return mlist;
482   }
483
484   template<PieceType Piece>
485   MoveStack* generate_piece_evasions(const Position& pos, MoveStack* mlist,
486                                      Color us, Bitboard target, Bitboard pinned) {
487     Square from;
488     Bitboard b;
489     const Square* ptr = pos.piece_list_begin(us, Piece);
490
491     while ((from = *ptr++) != SQ_NONE)
492     {
493         if (pinned && bit_is_set(pinned, from))
494             continue;
495
496         b = pos.attacks_from<Piece>(from) & target;
497         SERIALIZE_MOVES(b);
498     }
499     return mlist;
500   }
501
502   template<Color Us, SquareDelta Direction>
503   inline Bitboard move_pawns(Bitboard p) {
504
505     if (Direction == DELTA_N)
506         return Us == WHITE ? p << 8 : p >> 8;
507     else if (Direction == DELTA_NE)
508         return Us == WHITE ? p << 9 : p >> 7;
509     else if (Direction == DELTA_NW)
510         return Us == WHITE ? p << 7 : p >> 9;
511     else
512         return p;
513   }
514
515   template<Color Us, SquareDelta Diagonal>
516   MoveStack* generate_pawn_diagonal_captures(MoveStack* mlist, Bitboard pawns, Bitboard enemyPieces, bool promotion) {
517
518     // Calculate our parametrized parameters at compile time
519     const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
520     const Bitboard TFileABB = (Diagonal == DELTA_NE ? FileABB : FileHBB);
521     const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
522     const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
523     const SquareDelta TTDELTA_NE = (Diagonal == DELTA_NE ? TDELTA_NE : TDELTA_NW);
524
525     Square to;
526
527     // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
528     Bitboard b1 = move_pawns<Us, Diagonal>(pawns) & ~TFileABB & enemyPieces;
529
530     // Capturing promotions
531     if (promotion)
532     {
533         Bitboard b2 = b1 & TRank8BB;
534         b1 &= ~TRank8BB;
535         while (b2)
536         {
537             to = pop_1st_bit(&b2);
538             (*mlist++).move = make_promotion_move(to - TTDELTA_NE, to, QUEEN);
539         }
540     }
541
542     // Capturing non-promotions
543     SERIALIZE_MOVES_D(b1, -TTDELTA_NE);
544     return mlist;
545   }
546
547   template<Color Us, MoveType Type>
548   MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard dcp,
549                                  Square ksq, Bitboard blockSquares) {
550
551     // Calculate our parametrized parameters at compile time
552     const Color Them = (Us == WHITE ? BLACK : WHITE);
553     const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
554     const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
555     const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
556     const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
557     const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
558     const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
559
560     Bitboard b1, b2, dcPawns1, dcPawns2;
561     Square to;
562     Bitboard pawns = (Type == EVASION ? pos.pieces(PAWN, Us) & ~dcp : pos.pieces(PAWN, Us));
563     bool possiblePromotion = pawns & TRank7BB;
564
565     if (Type == CAPTURE)
566     {
567         // Standard captures and capturing promotions in both directions
568         Bitboard enemyPieces = pos.pieces_of_color(opposite_color(Us));
569         mlist = generate_pawn_diagonal_captures<Us, DELTA_NE>(mlist, pawns, enemyPieces, possiblePromotion);
570         mlist = generate_pawn_diagonal_captures<Us, DELTA_NW>(mlist, pawns, enemyPieces, possiblePromotion);
571     }
572
573     if (possiblePromotion)
574     {
575         // When generating checks consider under-promotion moves (both captures
576         // and non captures) only if can give a discovery check. Note that dcp
577         // is dc bitboard or pinned bitboard when Type == EVASION.
578         Bitboard pp = (Type == CHECK ? pawns & dcp : pawns);
579
580         if (Type != EVASION && Type != CAPTURE)
581         {
582             Bitboard enemyPieces = pos.pieces_of_color(opposite_color(Us));
583
584             // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
585             b1 = move_pawns<Us, DELTA_NE>(pp) & ~FileABB & enemyPieces & TRank8BB;
586             while (b1)
587             {
588                 to = pop_1st_bit(&b1);
589                 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
590                 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
591                 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
592             }
593
594             // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
595             b1 = move_pawns<Us, DELTA_NW>(pp) & ~FileHBB & enemyPieces & TRank8BB;
596             while (b1)
597             {
598                 to = pop_1st_bit(&b1);
599                 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
600                 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
601                 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
602             }
603         }
604
605         // Underpromotion pawn pushes. Also queen promotions for evasions and captures.
606         b1 = move_pawns<Us, DELTA_N>(pp) & TRank8BB;
607         b1 &= (Type == EVASION ? blockSquares : pos.empty_squares());
608
609         while (b1)
610         {
611             to = pop_1st_bit(&b1);
612             if (Type == EVASION || Type == CAPTURE)
613                 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
614
615             if (Type != CAPTURE)
616             {
617                 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
618                 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
619                 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
620             }
621         }
622     }
623
624     if (Type != CAPTURE)
625     {
626         Bitboard emptySquares = pos.empty_squares();
627         dcPawns1 = dcPawns2 = EmptyBoardBB;
628         if (Type == CHECK && (pawns & dcp))
629         {
630             // Pawn moves which gives discovered check. This is possible only if the
631             // pawn is not on the same file as the enemy king, because we don't
632             // generate captures.
633             dcPawns1 = move_pawns<Us, DELTA_N>(pawns & dcp & ~file_bb(ksq)) & emptySquares & ~TRank8BB;
634             dcPawns2 = move_pawns<Us, DELTA_N>(dcPawns1 & TRank3BB) & emptySquares;
635         }
636
637         // Single pawn pushes
638         b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares & ~TRank8BB;
639         b2 = (Type == CHECK ? (b1 & pos.attacks_from<PAWN>(ksq, Them)) | dcPawns1 :
640              (Type == EVASION ? b1 & blockSquares : b1));
641         SERIALIZE_MOVES_D(b2, -TDELTA_N);
642
643         // Double pawn pushes
644         b1 = move_pawns<Us, DELTA_N>(b1 & TRank3BB) & emptySquares;
645         b2 = (Type == CHECK ? (b1 & pos.attacks_from<PAWN>(ksq, Them)) | dcPawns2 :
646              (Type == EVASION ? b1 & blockSquares : b1));
647         SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
648     }
649     else if (pos.ep_square() != SQ_NONE) // En passant captures
650     {
651         assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
652         assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
653
654         b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
655         assert(b1 != EmptyBoardBB);
656
657         while (b1)
658         {
659             to = pop_1st_bit(&b1);
660             (*mlist++).move = make_ep_move(to, pos.ep_square());
661         }
662     }
663     return mlist;
664   }
665
666   template<PieceType Piece>
667   MoveStack* generate_discovered_checks(const Position& pos, Square from, MoveStack* mlist) {
668
669     assert(Piece != QUEEN);
670
671     Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
672     if (Piece == KING)
673     {
674         Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
675         b &= ~QueenPseudoAttacks[ksq];
676     }
677     SERIALIZE_MOVES(b);
678     return mlist;
679   }
680
681   template<PieceType Piece>
682   MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
683                                    Bitboard dc, Square ksq) {
684     assert(Piece != KING);
685
686     Square from;
687     Bitboard checkSqs;
688     const Square* ptr = pos.piece_list_begin(us, Piece);
689
690     if ((from = *ptr++) == SQ_NONE)
691         return mlist;
692
693     checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
694
695     do
696     {
697         if (   (Piece == QUEEN  && !(QueenPseudoAttacks[from]  & checkSqs))
698             || (Piece == ROOK   && !(RookPseudoAttacks[from]   & checkSqs))
699             || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
700             continue;
701
702         if (dc && bit_is_set(dc, from))
703             continue;
704
705         Bitboard bb = pos.attacks_from<Piece>(from) & checkSqs;
706         SERIALIZE_MOVES(bb);
707
708     } while ((from = *ptr++) != SQ_NONE);
709
710     return mlist;
711   }
712
713   template<CastlingSide Side>
714   MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
715
716     Color us = pos.side_to_move();
717
718     if (  (Side == KING_SIDE && pos.can_castle_kingside(us))
719         ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
720     {
721         Color them = opposite_color(us);
722         Square ksq = pos.king_square(us);
723
724         assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
725
726         Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
727         Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
728         Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
729         Square s;
730         bool illegal = false;
731
732         assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
733
734         // It is a bit complicated to correctly handle Chess960
735         for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
736             if (  (s != ksq && s != rsq && pos.square_is_occupied(s))
737                 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
738                 illegal = true;
739
740         for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
741             if (s != ksq && s != rsq && pos.square_is_occupied(s))
742                 illegal = true;
743
744         if (   Side == QUEEN_SIDE
745             && square_file(rsq) == FILE_B
746             && (   pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
747                 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
748             illegal = true;
749
750         if (!illegal)
751             (*mlist++).move = make_castle_move(ksq, rsq);
752     }
753     return mlist;
754   }
755 }