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