]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Small bonus to favor thorn pawns
[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-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cassert>
22
23 #include "movegen.h"
24 #include "position.h"
25
26 namespace {
27
28   template<GenType Type, Direction D>
29   ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
30
31     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
32         *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
33
34     if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
35     {
36         *moveList++ = make<PROMOTION>(to - D, to, ROOK);
37         *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
38         *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
39     }
40
41     // Knight promotion is the only promotion that can give a direct check
42     // that's not already included in the queen promotion.
43     if (Type == QUIET_CHECKS && (attacks_bb<KNIGHT>(to) & ksq))
44         *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
45     else
46         (void)ksq; // Silence a warning under MSVC
47
48     return moveList;
49   }
50
51
52   template<Color Us, GenType Type>
53   ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
54
55     constexpr Color     Them     = ~Us;
56     constexpr Bitboard  TRank7BB = (Us == WHITE ? Rank7BB    : Rank2BB);
57     constexpr Bitboard  TRank3BB = (Us == WHITE ? Rank3BB    : Rank6BB);
58     constexpr Direction Up       = pawn_push(Us);
59     constexpr Direction UpRight  = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
60     constexpr Direction UpLeft   = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
61
62     const Square ksq = pos.square<KING>(Them);
63     Bitboard emptySquares;
64
65     Bitboard pawnsOn7    = pos.pieces(Us, PAWN) &  TRank7BB;
66     Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
67
68     Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
69                         Type == CAPTURES ? target : pos.pieces(Them));
70
71     // Single and double pawn pushes, no promotions
72     if (Type != CAPTURES)
73     {
74         emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
75
76         Bitboard b1 = shift<Up>(pawnsNotOn7)   & emptySquares;
77         Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
78
79         if (Type == EVASIONS) // Consider only blocking squares
80         {
81             b1 &= target;
82             b2 &= target;
83         }
84
85         if (Type == QUIET_CHECKS)
86         {
87             b1 &= pawn_attacks_bb(Them, ksq);
88             b2 &= pawn_attacks_bb(Them, ksq);
89
90             // Add pawn pushes which give discovered check. This is possible only
91             // if the pawn is not on the same file as the enemy king, because we
92             // don't generate captures. Note that a possible discovery check
93             // promotion has been already generated amongst the captures.
94             Bitboard dcCandidateQuiets = pos.blockers_for_king(Them) & pawnsNotOn7;
95             if (dcCandidateQuiets)
96             {
97                 Bitboard dc1 = shift<Up>(dcCandidateQuiets) & emptySquares & ~file_bb(ksq);
98                 Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
99
100                 b1 |= dc1;
101                 b2 |= dc2;
102             }
103         }
104
105         while (b1)
106         {
107             Square to = pop_lsb(&b1);
108             *moveList++ = make_move(to - Up, to);
109         }
110
111         while (b2)
112         {
113             Square to = pop_lsb(&b2);
114             *moveList++ = make_move(to - Up - Up, to);
115         }
116     }
117
118     // Promotions and underpromotions
119     if (pawnsOn7)
120     {
121         if (Type == CAPTURES)
122             emptySquares = ~pos.pieces();
123
124         if (Type == EVASIONS)
125             emptySquares &= target;
126
127         Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
128         Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
129         Bitboard b3 = shift<Up     >(pawnsOn7) & emptySquares;
130
131         while (b1)
132             moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(&b1), ksq);
133
134         while (b2)
135             moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(&b2), ksq);
136
137         while (b3)
138             moveList = make_promotions<Type, Up     >(moveList, pop_lsb(&b3), ksq);
139     }
140
141     // Standard and en-passant captures
142     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
143     {
144         Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
145         Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
146
147         while (b1)
148         {
149             Square to = pop_lsb(&b1);
150             *moveList++ = make_move(to - UpRight, to);
151         }
152
153         while (b2)
154         {
155             Square to = pop_lsb(&b2);
156             *moveList++ = make_move(to - UpLeft, to);
157         }
158
159         if (pos.ep_square() != SQ_NONE)
160         {
161             assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
162
163             // An en passant capture can be an evasion only if the checking piece
164             // is the double pushed pawn and so is in the target. Otherwise this
165             // is a discovery check and we are forced to do otherwise.
166             if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
167                 return moveList;
168
169             b1 = pawnsNotOn7 & pawn_attacks_bb(Them, pos.ep_square());
170
171             assert(b1);
172
173             while (b1)
174                 *moveList++ = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
175         }
176     }
177
178     return moveList;
179   }
180
181
182   template<Color Us, PieceType Pt, bool Checks>
183   ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
184
185     static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
186
187     const Square* pl = pos.squares<Pt>(Us);
188
189     for (Square from = *pl; from != SQ_NONE; from = *++pl)
190     {
191         if (Checks)
192         {
193             if (    (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
194                 && !(attacks_bb<Pt>(from) & target & pos.check_squares(Pt)))
195                 continue;
196
197             if (pos.blockers_for_king(~Us) & from)
198                 continue;
199         }
200
201         Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
202
203         if (Checks)
204             b &= pos.check_squares(Pt);
205
206         while (b)
207             *moveList++ = make_move(from, pop_lsb(&b));
208     }
209
210     return moveList;
211   }
212
213
214   template<Color Us, GenType Type>
215   ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
216     constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantations
217     Bitboard target;
218
219     switch (Type)
220     {
221         case CAPTURES:
222             target =  pos.pieces(~Us);
223             break;
224         case QUIETS:
225         case QUIET_CHECKS:
226             target = ~pos.pieces();
227             break;
228         case EVASIONS:
229         {
230             Square checksq = lsb(pos.checkers());
231             target = between_bb(pos.square<KING>(Us), checksq) | checksq;
232             break;
233         }
234         case NON_EVASIONS:
235             target = ~pos.pieces(Us);
236             break;
237         default:
238             static_assert(true, "Unsupported type in generate_all()");
239     }
240
241     moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
242     moveList = generate_moves<Us, KNIGHT, Checks>(pos, moveList, target);
243     moveList = generate_moves<Us, BISHOP, Checks>(pos, moveList, target);
244     moveList = generate_moves<Us,   ROOK, Checks>(pos, moveList, target);
245     moveList = generate_moves<Us,  QUEEN, Checks>(pos, moveList, target);
246
247     if (Type != QUIET_CHECKS && Type != EVASIONS)
248     {
249         Square ksq = pos.square<KING>(Us);
250         Bitboard b = attacks_bb<KING>(ksq) & target;
251         while (b)
252             *moveList++ = make_move(ksq, pop_lsb(&b));
253
254         if ((Type != CAPTURES) && pos.can_castle(Us & ANY_CASTLING))
255             for(CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
256                 if (!pos.castling_impeded(cr) && pos.can_castle(cr))
257                     *moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
258     }
259
260     return moveList;
261   }
262
263 } // namespace
264
265
266 /// <CAPTURES>     Generates all pseudo-legal captures and queen promotions
267 /// <QUIETS>       Generates all pseudo-legal non-captures and underpromotions
268 /// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
269 ///
270 /// Returns a pointer to the end of the move list.
271
272 template<GenType Type>
273 ExtMove* generate(const Position& pos, ExtMove* moveList) {
274
275   static_assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS, "Unsupported type in generate()");
276   assert(!pos.checkers());
277
278   Color us = pos.side_to_move();
279
280   return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
281                      : generate_all<BLACK, Type>(pos, moveList);
282 }
283
284 // Explicit template instantiations
285 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
286 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
287 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
288
289
290 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
291 /// underpromotions that give check. Returns a pointer to the end of the move list.
292 template<>
293 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
294
295   assert(!pos.checkers());
296
297   Color us = pos.side_to_move();
298   Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us) & ~pos.pieces(PAWN);
299
300   while (dc)
301   {
302      Square from = pop_lsb(&dc);
303      PieceType pt = type_of(pos.piece_on(from));
304
305      Bitboard b = attacks_bb(pt, from, pos.pieces()) & ~pos.pieces();
306
307      if (pt == KING)
308          b &= ~attacks_bb<QUEEN>(pos.square<KING>(~us));
309
310      while (b)
311          *moveList++ = make_move(from, pop_lsb(&b));
312   }
313
314   return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList)
315                      : generate_all<BLACK, QUIET_CHECKS>(pos, moveList);
316 }
317
318
319 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
320 /// to move is in check. Returns a pointer to the end of the move list.
321 template<>
322 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
323
324   assert(pos.checkers());
325
326   Color us = pos.side_to_move();
327   Square ksq = pos.square<KING>(us);
328   Bitboard sliderAttacks = 0;
329   Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
330
331   // Find all the squares attacked by slider checkers. We will remove them from
332   // the king evasions in order to skip known illegal moves, which avoids any
333   // useless legality checks later on.
334   while (sliders)
335       sliderAttacks |= line_bb(ksq, pop_lsb(&sliders)) & ~pos.checkers();
336
337   // Generate evasions for king, capture and non capture moves
338   Bitboard b = attacks_bb<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
339   while (b)
340       *moveList++ = make_move(ksq, pop_lsb(&b));
341
342   if (more_than_one(pos.checkers()))
343       return moveList; // Double check, only a king move can save the day
344
345   // Generate blocking evasions or captures of the checking piece
346   return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList)
347                      : generate_all<BLACK, EVASIONS>(pos, moveList);
348 }
349
350
351 /// generate<LEGAL> generates all the legal moves in the given position
352
353 template<>
354 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
355
356   Color us = pos.side_to_move();
357   Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
358   Square ksq = pos.square<KING>(us);
359   ExtMove* cur = moveList;
360
361   moveList = pos.checkers() ? generate<EVASIONS    >(pos, moveList)
362                             : generate<NON_EVASIONS>(pos, moveList);
363   while (cur != moveList)
364       if (   (pinned || from_sq(*cur) == ksq || type_of(*cur) == ENPASSANT)
365           && !pos.legal(*cur))
366           *cur = (--moveList)->move;
367       else
368           ++cur;
369
370   return moveList;
371 }