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