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