]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Revert dynamic contempt
[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-2014 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 "movegen.h"
23 #include "position.h"
24
25 namespace {
26
27   template<CastlingRight Cr, bool Checks, bool Chess960>
28   ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {
29
30     static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
31
32     if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
33         return mlist;
34
35     // After castling, the rook and king final positions are the same in Chess960
36     // as they would be in standard chess.
37     Square kfrom = pos.king_square(us);
38     Square rfrom = pos.castling_rook_square(Cr);
39     Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
40     Bitboard enemies = pos.pieces(~us);
41
42     assert(!pos.checkers());
43
44     const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
45                               : KingSide    ? DELTA_W : DELTA_E;
46
47     for (Square s = kto; s != kfrom; s += K)
48         if (pos.attackers_to(s) & enemies)
49             return mlist;
50
51     // Because we generate only legal castling moves we need to verify that
52     // when moving the castling rook we do not discover some hidden checker.
53     // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
54     if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
55         return mlist;
56
57     Move m = make<CASTLING>(kfrom, rfrom);
58
59     if (Checks && !pos.gives_check(m, *ci))
60         return mlist;
61
62     (mlist++)->move = m;
63
64     return mlist;
65   }
66
67
68   template<GenType Type, Square Delta>
69   inline ExtMove* generate_promotions(ExtMove* mlist, Bitboard pawnsOn7,
70                                       Bitboard target, const CheckInfo* ci) {
71
72     Bitboard b = shift_bb<Delta>(pawnsOn7) & target;
73
74     while (b)
75     {
76         Square to = pop_lsb(&b);
77
78         if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
79             (mlist++)->move = make<PROMOTION>(to - Delta, to, QUEEN);
80
81         if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
82         {
83             (mlist++)->move = make<PROMOTION>(to - Delta, to, ROOK);
84             (mlist++)->move = make<PROMOTION>(to - Delta, to, BISHOP);
85             (mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
86         }
87
88         // Knight promotion is the only promotion that can give a direct check
89         // that's not already included in the queen promotion.
90         if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ci->ksq))
91             (mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
92         else
93             (void)ci; // Silence a warning under MSVC
94     }
95
96     return mlist;
97   }
98
99
100   template<Color Us, GenType Type>
101   ExtMove* generate_pawn_moves(const Position& pos, ExtMove* mlist,
102                                Bitboard target, const CheckInfo* ci) {
103
104     // Compute our parametrized parameters at compile time, named according to
105     // the point of view of white side.
106     const Color    Them     = (Us == WHITE ? BLACK    : WHITE);
107     const Bitboard TRank8BB = (Us == WHITE ? Rank8BB  : Rank1BB);
108     const Bitboard TRank7BB = (Us == WHITE ? Rank7BB  : Rank2BB);
109     const Bitboard TRank3BB = (Us == WHITE ? Rank3BB  : Rank6BB);
110     const Square   Up       = (Us == WHITE ? DELTA_N  : DELTA_S);
111     const Square   Right    = (Us == WHITE ? DELTA_NE : DELTA_SW);
112     const Square   Left     = (Us == WHITE ? DELTA_NW : DELTA_SE);
113
114     Bitboard b1, b2, dc1, dc2, emptySquares;
115
116     Bitboard pawnsOn7    = pos.pieces(Us, PAWN) &  TRank7BB;
117     Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
118
119     Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
120                         Type == CAPTURES ? target : pos.pieces(Them));
121
122     // Single and double pawn pushes, no promotions
123     if (Type != CAPTURES)
124     {
125         emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
126
127         b1 = shift_bb<Up>(pawnsNotOn7)   & emptySquares;
128         b2 = shift_bb<Up>(b1 & TRank3BB) & emptySquares;
129
130         if (Type == EVASIONS) // Consider only blocking squares
131         {
132             b1 &= target;
133             b2 &= target;
134         }
135
136         if (Type == QUIET_CHECKS)
137         {
138             b1 &= pos.attacks_from<PAWN>(ci->ksq, Them);
139             b2 &= pos.attacks_from<PAWN>(ci->ksq, Them);
140
141             // Add pawn pushes which give discovered check. This is possible only
142             // if the pawn is not on the same file as the enemy king, because we
143             // don't generate captures. Note that a possible discovery check
144             // promotion has been already generated amongst the captures.
145             if (pawnsNotOn7 & ci->dcCandidates)
146             {
147                 dc1 = shift_bb<Up>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
148                 dc2 = shift_bb<Up>(dc1 & TRank3BB) & emptySquares;
149
150                 b1 |= dc1;
151                 b2 |= dc2;
152             }
153         }
154
155         while (b1)
156         {
157             Square to = pop_lsb(&b1);
158             (mlist++)->move = make_move(to - Up, to);
159         }
160
161         while (b2)
162         {
163             Square to = pop_lsb(&b2);
164             (mlist++)->move = make_move(to - Up - Up, to);
165         }
166     }
167
168     // Promotions and underpromotions
169     if (pawnsOn7 && (Type != EVASIONS || (target & TRank8BB)))
170     {
171         if (Type == CAPTURES)
172             emptySquares = ~pos.pieces();
173
174         if (Type == EVASIONS)
175             emptySquares &= target;
176
177         mlist = generate_promotions<Type, Right>(mlist, pawnsOn7, enemies, ci);
178         mlist = generate_promotions<Type, Left >(mlist, pawnsOn7, enemies, ci);
179         mlist = generate_promotions<Type, Up>(mlist, pawnsOn7, emptySquares, ci);
180     }
181
182     // Standard and en-passant captures
183     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
184     {
185         b1 = shift_bb<Right>(pawnsNotOn7) & enemies;
186         b2 = shift_bb<Left >(pawnsNotOn7) & enemies;
187
188         while (b1)
189         {
190             Square to = pop_lsb(&b1);
191             (mlist++)->move = make_move(to - Right, to);
192         }
193
194         while (b2)
195         {
196             Square to = pop_lsb(&b2);
197             (mlist++)->move = make_move(to - Left, to);
198         }
199
200         if (pos.ep_square() != SQ_NONE)
201         {
202             assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
203
204             // An en passant capture can be an evasion only if the checking piece
205             // is the double pushed pawn and so is in the target. Otherwise this
206             // is a discovery check and we are forced to do otherwise.
207             if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
208                 return mlist;
209
210             b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
211
212             assert(b1);
213
214             while (b1)
215                 (mlist++)->move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
216         }
217     }
218
219     return mlist;
220   }
221
222
223   template<PieceType Pt, bool Checks> FORCE_INLINE
224   ExtMove* generate_moves(const Position& pos, ExtMove* mlist, Color us,
225                           Bitboard target, const CheckInfo* ci) {
226
227     assert(Pt != KING && Pt != PAWN);
228
229     const Square* pl = pos.list<Pt>(us);
230
231     for (Square from = *pl; from != SQ_NONE; from = *++pl)
232     {
233         if (Checks)
234         {
235             if (    (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
236                 && !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
237                 continue;
238
239             if (unlikely(ci->dcCandidates) && (ci->dcCandidates & from))
240                 continue;
241         }
242
243         Bitboard b = pos.attacks_from<Pt>(from) & target;
244
245         if (Checks)
246             b &= ci->checkSq[Pt];
247
248         while (b)
249             (mlist++)->move = make_move(from, pop_lsb(&b));
250     }
251
252     return mlist;
253   }
254
255
256   template<Color Us, GenType Type> FORCE_INLINE
257   ExtMove* generate_all(const Position& pos, ExtMove* mlist, Bitboard target,
258                         const CheckInfo* ci = NULL) {
259
260     const bool Checks = Type == QUIET_CHECKS;
261
262     mlist = generate_pawn_moves<Us, Type>(pos, mlist, target, ci);
263     mlist = generate_moves<KNIGHT, Checks>(pos, mlist, Us, target, ci);
264     mlist = generate_moves<BISHOP, Checks>(pos, mlist, Us, target, ci);
265     mlist = generate_moves<  ROOK, Checks>(pos, mlist, Us, target, ci);
266     mlist = generate_moves< QUEEN, Checks>(pos, mlist, Us, target, ci);
267
268     if (Type != QUIET_CHECKS && Type != EVASIONS)
269     {
270         Square ksq = pos.king_square(Us);
271         Bitboard b = pos.attacks_from<KING>(ksq) & target;
272         while (b)
273             (mlist++)->move = make_move(ksq, pop_lsb(&b));
274     }
275
276     if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
277     {
278         if (pos.is_chess960())
279         {
280             mlist = generate_castling<MakeCastling<Us,  KING_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
281             mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
282         }
283         else
284         {
285             mlist = generate_castling<MakeCastling<Us,  KING_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
286             mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
287         }
288     }
289
290     return mlist;
291   }
292
293
294 } // namespace
295
296
297 /// generate<CAPTURES> generates all pseudo-legal captures and queen
298 /// promotions. Returns a pointer to the end of the move list.
299 ///
300 /// generate<QUIETS> generates all pseudo-legal non-captures and
301 /// underpromotions. Returns a pointer to the end of the move list.
302 ///
303 /// generate<NON_EVASIONS> generates all pseudo-legal captures and
304 /// non-captures. Returns a pointer to the end of the move list.
305
306 template<GenType Type>
307 ExtMove* generate(const Position& pos, ExtMove* mlist) {
308
309   assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
310   assert(!pos.checkers());
311
312   Color us = pos.side_to_move();
313
314   Bitboard target = Type == CAPTURES     ?  pos.pieces(~us)
315                   : Type == QUIETS       ? ~pos.pieces()
316                   : Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
317
318   return us == WHITE ? generate_all<WHITE, Type>(pos, mlist, target)
319                      : generate_all<BLACK, Type>(pos, mlist, target);
320 }
321
322 // Explicit template instantiations
323 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
324 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
325 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
326
327
328 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
329 /// underpromotions that give check. Returns a pointer to the end of the move list.
330 template<>
331 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* mlist) {
332
333   assert(!pos.checkers());
334
335   Color us = pos.side_to_move();
336   CheckInfo ci(pos);
337   Bitboard dc = ci.dcCandidates;
338
339   while (dc)
340   {
341      Square from = pop_lsb(&dc);
342      PieceType pt = type_of(pos.piece_on(from));
343
344      if (pt == PAWN)
345          continue; // Will be generated together with direct checks
346
347      Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
348
349      if (pt == KING)
350          b &= ~PseudoAttacks[QUEEN][ci.ksq];
351
352      while (b)
353          (mlist++)->move = make_move(from, pop_lsb(&b));
354   }
355
356   return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci)
357                      : generate_all<BLACK, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci);
358 }
359
360
361 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
362 /// to move is in check. Returns a pointer to the end of the move list.
363 template<>
364 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* mlist) {
365
366   assert(pos.checkers());
367
368   Color us = pos.side_to_move();
369   Square ksq = pos.king_square(us);
370   Bitboard sliderAttacks = 0;
371   Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
372
373   // Find all the squares attacked by slider checkers. We will remove them from
374   // the king evasions in order to skip known illegal moves, which avoids any
375   // useless legality checks later on.
376   while (sliders)
377   {
378       Square checksq = pop_lsb(&sliders);
379       sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
380   }
381
382   // Generate evasions for king, capture and non capture moves
383   Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
384   while (b)
385       (mlist++)->move = make_move(ksq, pop_lsb(&b));
386
387   if (more_than_one(pos.checkers()))
388       return mlist; // Double check, only a king move can save the day
389
390   // Generate blocking evasions or captures of the checking piece
391   Square checksq = lsb(pos.checkers());
392   Bitboard target = between_bb(checksq, ksq) | checksq;
393
394   return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, mlist, target)
395                      : generate_all<BLACK, EVASIONS>(pos, mlist, target);
396 }
397
398
399 /// generate<LEGAL> generates all the legal moves in the given position
400
401 template<>
402 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* mlist) {
403
404   ExtMove *end, *cur = mlist;
405   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
406   Square ksq = pos.king_square(pos.side_to_move());
407
408   end = pos.checkers() ? generate<EVASIONS>(pos, mlist)
409                        : generate<NON_EVASIONS>(pos, mlist);
410   while (cur != end)
411       if (   (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
412           && !pos.legal(cur->move, pinned))
413           cur->move = (--end)->move;
414       else
415           ++cur;
416
417   return end;
418 }