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