forked from fairy-stockfish/Fairy-Stockfish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovegen.cpp
More file actions
633 lines (532 loc) · 24.7 KB
/
movegen.cpp
File metadata and controls
633 lines (532 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include "movegen.h"
#include "position.h"
namespace Stockfish {
namespace {
template<MoveType T>
ExtMove* make_move_and_gating(const Position& pos, ExtMove* moveList, Color us, Square from, Square to, PieceType pt = NO_PIECE_TYPE) {
// Wall placing moves
//if it's "wall or move", and they chose non-null move, skip even generating wall move
if (pos.walling() && !(pos.wall_or_move() && (from!=to)))
{
Bitboard b = pos.board_bb() & ~((pos.pieces() ^ from) | to);
if (T == CASTLING)
{
Square kto = make_square(to > from ? pos.castling_kingside_file() : pos.castling_queenside_file(), pos.castling_rank(us));
Direction step = kto > from ? EAST : WEST;
Square rto = kto - step;
b ^= square_bb(to) ^ kto ^ rto;
}
if (T == EN_PASSANT)
b ^= pos.capture_square(to);
if (pos.walling_rule() == ARROW)
b &= moves_bb(us, type_of(pos.piece_on(from)), to, pos.pieces() ^ from);
//Any current or future wall variant must follow the walling region rule if set:
b &= pos.walling_region(us);
if (pos.walling_rule() == PAST)
b &= square_bb(from);
if (pos.walling_rule() == EDGE)
{
Bitboard wallsquares = pos.state()->wallSquares;
b &= (FileABB | file_bb(pos.max_file()) | Rank1BB | rank_bb(pos.max_rank())) |
( shift<NORTH >(wallsquares) | shift<SOUTH >(wallsquares)
| shift<EAST >(wallsquares) | shift<WEST >(wallsquares));
}
while (b)
*moveList++ = make_gating<T>(from, to, pt, pop_lsb(b));
return moveList;
}
*moveList++ = make<T>(from, to, pt);
if (pos.spell_chess())
{
PieceType freezePt = pos.freeze_potion_type();
if ( freezePt != NO_PIECE_TYPE
&& pos.freeze_cd(us) == 0
&& pos.count_in_hand(us, freezePt) > 0)
{
Bitboard origins = square_bb(from);
for (Bitboard centers = pos.board_bb(); centers; )
{
Square center = pop_lsb(centers);
Bitboard zone = (attacks_bb<KING>(center) | square_bb(center)) & pos.board_bb();
if (zone & origins)
continue;
*moveList++ = make_gating<T>(from, to, freezePt, center);
}
}
}
// Gating moves
if (pos.seirawan_gating() && (pos.gates(us) & from))
for (PieceSet ps = pos.piece_types(); ps;)
{
PieceType pt_gating = pop_lsb(ps);
if (pos.can_drop(us, pt_gating) && (pos.drop_region(us, pt_gating) & from))
*moveList++ = make_gating<T>(from, to, pt_gating, from);
}
if (pos.seirawan_gating() && T == CASTLING && (pos.gates(us) & to))
for (PieceSet ps = pos.piece_types(); ps;)
{
PieceType pt_gating = pop_lsb(ps);
if (pos.can_drop(us, pt_gating) && (pos.drop_region(us, pt_gating) & to))
*moveList++ = make_gating<T>(from, to, pt_gating, to);
}
return moveList;
}
template<Color c, GenType Type, Direction D>
ExtMove* make_promotions(const Position& pos, ExtMove* moveList, Square to) {
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
{
for (PieceSet promotions = pos.promotion_piece_types(c); promotions;)
{
PieceType pt = pop_msb(promotions);
if (!pos.promotion_limit(pt) || pos.promotion_limit(pt) > pos.count(c, pt))
moveList = make_move_and_gating<PROMOTION>(pos, moveList, pos.side_to_move(), to - D, to, pt);
}
PieceType pt = pos.promoted_piece_type(PAWN);
if (pt && !(pos.piece_promotion_on_capture() && pos.empty(to)))
moveList = make_move_and_gating<PIECE_PROMOTION>(pos, moveList, pos.side_to_move(), to - D, to);
}
return moveList;
}
template<Color Us, GenType Type>
ExtMove* generate_drops(const Position& pos, ExtMove* moveList, PieceType pt, Bitboard b) {
assert(Type != CAPTURES);
// Do not generate virtual drops for perft and at root
if (pos.can_drop(Us, pt) || (Type != NON_EVASIONS && pos.two_boards() && pos.allow_virtual_drop(Us, pt)))
{
// Restrict to valid target
b &= pos.drop_region(Us, pt);
// Add to move list
if (pos.drop_promoted() && pos.promoted_piece_type(pt))
{
Bitboard b2 = b;
if (Type == QUIET_CHECKS)
b2 &= pos.check_squares(pos.promoted_piece_type(pt));
while (b2)
*moveList++ = make_drop(pop_lsb(b2), pt, pos.promoted_piece_type(pt));
}
if (Type == QUIET_CHECKS || !pos.can_drop(Us, pt))
b &= pos.check_squares(pt);
while (b)
*moveList++ = make_drop(pop_lsb(b), pt, pt);
}
return moveList;
}
template<Color Us, GenType Type>
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
if (!pos.pieces(Us, PAWN))
return moveList;
constexpr Color Them = ~Us;
constexpr Direction Up = pawn_push(Us);
constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
const Bitboard promotionZone = pos.promotion_zone(Us);
const Bitboard standardPromotionZone = pos.sittuyin_promotion() ? Bitboard(0) : promotionZone;
const Bitboard doubleStepRegion = pos.double_step_region(Us);
const Bitboard tripleStepRegion = pos.triple_step_region(Us);
const Bitboard pawns = pos.pieces(Us, PAWN) & ~pos.freeze_zone(Us);
const Bitboard movable = pos.board_bb(Us, PAWN) & ~pos.pieces();
const Bitboard capturable = pos.board_bb(Us, PAWN) & pos.pieces(Them);
target = Type == EVASIONS ? target : AllSquares;
// Define single and double push, left and right capture, as well as respective promotion moves
Bitboard b1 = shift<Up>(pawns) & movable & target;
Bitboard b2 = shift<Up>(shift<Up>(pawns & doubleStepRegion) & movable) & movable & target;
Bitboard b3 = shift<Up>(shift<Up>(shift<Up>(pawns & tripleStepRegion) & movable) & movable) & movable & target;
Bitboard brc = shift<UpRight>(pawns) & capturable & target;
Bitboard blc = shift<UpLeft >(pawns) & capturable & target;
Bitboard b1p = b1 & standardPromotionZone;
Bitboard b2p = b2 & standardPromotionZone;
Bitboard b3p = b3 & standardPromotionZone;
Bitboard brcp = brc & standardPromotionZone;
Bitboard blcp = blc & standardPromotionZone;
// Restrict regions based on rules and move generation type
if (pos.mandatory_pawn_promotion())
{
b1 &= ~standardPromotionZone;
b2 &= ~standardPromotionZone;
b3 &= ~standardPromotionZone;
brc &= ~standardPromotionZone;
blc &= ~standardPromotionZone;
}
if (Type == QUIET_CHECKS && pos.count<KING>(Them))
{
// To make a quiet check, you either make a direct check by pushing a pawn
// or push a blocker pawn that is not on the same file as the enemy king.
// Discovered check promotion has been already generated amongst the captures.
Square ksq = pos.square<KING>(Them);
Bitboard dcCandidatePawns = pos.blockers_for_king(Them) & ~file_bb(ksq);
b1 &= pawn_attacks_bb(Them, ksq) | shift< Up>(dcCandidatePawns);
b2 &= pawn_attacks_bb(Them, ksq) | shift<Up+Up>(dcCandidatePawns);
}
// Single and double pawn pushes, no promotions
if (Type != CAPTURES)
{
while (b1)
{
Square to = pop_lsb(b1);
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, to - Up, to);
}
while (b2)
{
Square to = pop_lsb(b2);
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, to - Up - Up, to);
}
while (b3)
{
Square to = pop_lsb(b3);
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, to - Up - Up - Up, to);
}
}
// Promotions and underpromotions
while (brcp)
moveList = make_promotions<Us, Type, UpRight>(pos, moveList, pop_lsb(brcp));
while (blcp)
moveList = make_promotions<Us, Type, UpLeft >(pos, moveList, pop_lsb(blcp));
while (b1p)
moveList = make_promotions<Us, Type, Up >(pos, moveList, pop_lsb(b1p));
while (b2p)
moveList = make_promotions<Us, Type, Up+Up >(pos, moveList, pop_lsb(b2p));
while (b3p)
moveList = make_promotions<Us, Type, Up+Up+Up>(pos, moveList, pop_lsb(b3p));
// Sittuyin promotions
if (pos.sittuyin_promotion() && (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS))
{
// Pawns need to be in promotion zone if there is more than one pawn
Bitboard promotionPawns = pos.count<PAWN>(Us) > 1 ? pawns & promotionZone : pawns;
while (promotionPawns)
{
Square from = pop_lsb(promotionPawns);
for (PieceSet ps = pos.promotion_piece_types(Us); ps;)
{
PieceType pt = pop_msb(ps);
if (pos.promotion_limit(pt) && pos.promotion_limit(pt) <= pos.count(Us, pt))
continue;
Bitboard b = ((pos.attacks_from(Us, pt, from) & ~pos.pieces()) | from) & target;
while (b)
{
Square to = pop_lsb(b);
if (!(attacks_bb(Us, pt, to, pos.pieces() ^ from) & pos.pieces(Them)))
*moveList++ = make<PROMOTION>(from, to, pt);
}
}
}
}
// Standard and en passant captures
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
{
while (brc)
{
Square to = pop_lsb(brc);
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, to - UpRight, to);
}
while (blc)
{
Square to = pop_lsb(blc);
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, to - UpLeft, to);
}
for (Bitboard epSquares = pos.ep_squares() & ~pos.pieces(); epSquares; )
{
Square epSquare = pop_lsb(epSquares);
// An en passant capture cannot resolve a discovered check (unless there non-sliding riders)
if (Type == EVASIONS && (target & (epSquare + Up)) && !pos.non_sliding_riders())
return moveList;
Bitboard b = pawns & pawn_attacks_bb(Them, epSquare);
// En passant square is already disabled for non-fairy variants if there is no attacker
assert(b || !pos.fast_attacks());
while (b)
moveList = make_move_and_gating<EN_PASSANT>(pos, moveList, Us, pop_lsb(b), epSquare);
}
}
return moveList;
}
template<Color Us, GenType Type>
ExtMove* generate_moves(const Position& pos, ExtMove* moveList, PieceType Pt, Bitboard target) {
assert(Pt != KING && Pt != PAWN);
Bitboard bb = pos.pieces(Us, Pt) & ~pos.freeze_zone(Us);
Bitboard boardMask = pos.board_bb();
Bitboard occupiedAll = pos.pieces();
Bitboard ourPieces = pos.pieces(Us);
auto advance = [&](Square sq, Direction dir) -> Square {
Square next = Square(int(sq) + int(dir));
if (!is_ok(next) || !(boardMask & square_bb(next)))
return SQ_NONE;
int fileDelta = file_of(next) - file_of(sq);
int rankDelta = rank_of(next) - rank_of(sq);
switch (dir)
{
case NORTH: if (fileDelta != 0 || rankDelta != 1) return SQ_NONE; break;
case SOUTH: if (fileDelta != 0 || rankDelta != -1) return SQ_NONE; break;
case EAST: if (rankDelta != 0 || fileDelta != 1) return SQ_NONE; break;
case WEST: if (rankDelta != 0 || fileDelta != -1) return SQ_NONE; break;
case NORTH_EAST: if (fileDelta != 1 || rankDelta != 1) return SQ_NONE; break;
case NORTH_WEST: if (fileDelta != -1 || rankDelta != 1) return SQ_NONE; break;
case SOUTH_EAST: if (fileDelta != 1 || rankDelta != -1) return SQ_NONE; break;
case SOUTH_WEST: if (fileDelta != -1 || rankDelta != -1) return SQ_NONE; break;
default: break;
}
return next;
};
while (bb)
{
Square from = pop_lsb(bb);
Bitboard attacks = pos.attacks_from(Us, Pt, from);
Bitboard quiets = pos.moves_from(Us, Pt, from);
Bitboard b = ( (attacks & pos.pieces())
| (quiets & ~pos.pieces()));
Bitboard b1 = b & target;
Bitboard promotion_zone = pos.promotion_zone(Us);
PieceType promPt = pos.promoted_piece_type(Pt);
Bitboard b2 = promPt && (!pos.promotion_limit(promPt) || pos.promotion_limit(promPt) > pos.count(Us, promPt)) ? b1 : Bitboard(0);
Bitboard b3 = pos.piece_demotion() && pos.is_promoted(from) ? b1 : Bitboard(0);
Bitboard pawnPromotions = (pos.promotion_pawn_types(Us) & Pt) ? (b & (Type == EVASIONS ? target : ~pos.pieces(Us)) & promotion_zone) : Bitboard(0);
Bitboard epSquares = (pos.en_passant_types(Us) & Pt) ? (attacks & ~quiets & pos.ep_squares() & ~pos.pieces()) : Bitboard(0);
// target squares considering pawn promotions
if (pawnPromotions && pos.mandatory_pawn_promotion())
b1 &= ~pawnPromotions;
// Restrict target squares considering promotion zone
if (b2 | b3)
{
if (pos.mandatory_piece_promotion())
b1 &= (promotion_zone & from ? Bitboard(0) : ~promotion_zone) | (pos.piece_promotion_on_capture() ? ~pos.pieces() : Bitboard(0));
// Exclude quiet promotions/demotions
if (pos.piece_promotion_on_capture())
{
b2 &= pos.pieces();
b3 &= pos.pieces();
}
// Consider promotions/demotions into promotion zone
if (!(promotion_zone & from))
{
b2 &= promotion_zone;
b3 &= promotion_zone;
}
}
if (Type == QUIET_CHECKS)
{
b1 &= pos.check_squares(Pt);
if (b2)
b2 &= pos.check_squares(pos.promoted_piece_type(Pt));
if (b3)
b3 &= pos.check_squares(type_of(pos.unpromoted_piece_on(from)));
}
while (b1)
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, from, pop_lsb(b1));
// Shogi-style piece promotions
while (b2)
*moveList++ = make<PIECE_PROMOTION>(from, pop_lsb(b2));
// Piece demotions
while (b3)
*moveList++ = make<PIECE_DEMOTION>(from, pop_lsb(b3));
// Pawn-style promotions
if ((Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS) && pawnPromotions)
for (PieceSet ps = pos.promotion_piece_types(Us); ps;)
{
PieceType ptP = pop_msb(ps);
if (!pos.promotion_limit(ptP) || pos.promotion_limit(ptP) > pos.count(Us, ptP))
for (Bitboard promotions = pawnPromotions; promotions; )
moveList = make_move_and_gating<PROMOTION>(pos, moveList, pos.side_to_move(), from, pop_lsb(promotions), ptP);
}
// En passant captures
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
while (epSquares)
moveList = make_move_and_gating<EN_PASSANT>(pos, moveList, Us, from, pop_lsb(epSquares));
if ( pos.spell_chess()
&& pos.jump_potion_type() != NO_PIECE_TYPE
&& pos.jump_cd(Us) == 0
&& pos.count_in_hand(Us, pos.jump_potion_type()) > 0
&& (Pt == ROOK || Pt == BISHOP || Pt == QUEEN))
{
PieceType jumpPt = pos.jump_potion_type();
Bitboard captureTargets = target;
if constexpr (Type != QUIETS && Type != QUIET_CHECKS)
captureTargets |= pos.pieces(~Us);
auto generate_jump = [&](Direction dir) {
Square sq = advance(from, dir);
Square blocker = SQ_NONE;
while (sq != SQ_NONE)
{
Bitboard sqBB = square_bb(sq);
if (occupiedAll & sqBB)
{
if (blocker == SQ_NONE)
{
blocker = sq;
sq = advance(sq, dir);
continue;
}
if (!(ourPieces & sqBB) && (captureTargets & sqBB))
*moveList++ = make_gating<NORMAL>(from, sq, jumpPt, blocker);
break;
}
if (blocker != SQ_NONE && (target & sqBB))
*moveList++ = make_gating<NORMAL>(from, sq, jumpPt, blocker);
sq = advance(sq, dir);
}
};
if (Pt == ROOK || Pt == QUEEN)
{
generate_jump(NORTH);
generate_jump(SOUTH);
generate_jump(EAST);
generate_jump(WEST);
}
if (Pt == BISHOP || Pt == QUEEN)
{
generate_jump(NORTH_EAST);
generate_jump(NORTH_WEST);
generate_jump(SOUTH_EAST);
generate_jump(SOUTH_WEST);
}
}
}
return moveList;
}
template<Color Us, GenType Type>
ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
static_assert(Type != LEGAL, "Unsupported type in generate_all()");
constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantiations
const Square ksq = pos.count<KING>(Us) ? pos.square<KING>(Us) : SQ_NONE;
Bitboard target;
// Skip generating non-king moves when in double check
if (Type != EVASIONS || !more_than_one(pos.checkers() & ~pos.non_sliding_riders()))
{
target = Type == EVASIONS ? between_bb(ksq, lsb(pos.checkers()))
: Type == NON_EVASIONS ? ~pos.pieces( Us)
: Type == CAPTURES ? pos.pieces(~Us)
: ~pos.pieces( ); // QUIETS || QUIET_CHECKS
if (Type == EVASIONS)
{
if (pos.checkers() & pos.non_sliding_riders())
target = ~pos.pieces(Us);
// Leaper attacks can not be blocked
Square checksq = lsb(pos.checkers());
if (LeaperAttacks[~Us][type_of(pos.piece_on(checksq))][checksq] & pos.square<KING>(Us))
target = pos.checkers();
}
// Remove inaccessible squares (outside board + wall squares)
target &= pos.board_bb();
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
for (PieceSet ps = pos.piece_types() & ~(piece_set(PAWN) | KING); ps;)
moveList = generate_moves<Us, Type>(pos, moveList, pop_lsb(ps), target);
// generate drops
if (pos.piece_drops() && Type != CAPTURES && (pos.can_drop(Us, ALL_PIECES) || pos.two_boards()))
for (PieceSet ps = pos.piece_types(); ps;)
moveList = generate_drops<Us, Type>(pos, moveList, pop_lsb(ps), target & ~pos.pieces(~Us));
// Castling with non-king piece
if (!pos.count<KING>(Us) && Type != CAPTURES && pos.can_castle(Us & ANY_CASTLING))
{
Square from = pos.castling_king_square(Us);
for(CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
if (!pos.castling_impeded(cr) && pos.can_castle(cr))
{
Square rookFrom = pos.castling_rook_square(cr);
if (pos.freeze_zone(Us) & (square_bb(from) | square_bb(rookFrom)))
continue;
moveList = make_move_and_gating<CASTLING>(pos, moveList, Us, from, rookFrom);
}
}
// Special moves
if (pos.cambodian_moves() && pos.gates(Us) && Type != CAPTURES)
{
if (Type != EVASIONS && (pos.pieces(Us, KING) & pos.gates(Us)))
{
Square from = pos.square<KING>(Us);
Bitboard b = PseudoAttacks[WHITE][KNIGHT][from] & rank_bb(rank_of(from + (Us == WHITE ? NORTH : SOUTH)))
& target & ~pos.pieces();
while (b)
moveList = make_move_and_gating<SPECIAL>(pos, moveList, Us, from, pop_lsb(b));
}
Bitboard b = pos.pieces(Us, FERS) & pos.gates(Us);
while (b)
{
Square from = pop_lsb(b);
Square to = from + 2 * (Us == WHITE ? NORTH : SOUTH);
if (is_ok(to) && (target & to & ~pos.pieces()))
moveList = make_move_and_gating<SPECIAL>(pos, moveList, Us, from, to);
}
}
// Workaround for passing: Execute a non-move with any piece
if (pos.pass(Us) && !pos.count<KING>(Us) && pos.pieces(Us))
*moveList++ = make<SPECIAL>(lsb(pos.pieces(Us)), lsb(pos.pieces(Us)));
//if "wall or move", generate walling action with null move
if (pos.wall_or_move())
{
moveList = make_move_and_gating<SPECIAL>(pos, moveList, Us, lsb(pos.pieces(Us)), lsb(pos.pieces(Us)));
}
}
// King moves
if (pos.count<KING>(Us)
&& !(pos.freeze_zone(Us) & square_bb(ksq))
&& (!Checks || pos.blockers_for_king(~Us) & ksq))
{
Bitboard b = ( (pos.attacks_from(Us, KING, ksq) & pos.pieces())
| (pos.moves_from(Us, KING, ksq) & ~pos.pieces())) & (Type == EVASIONS ? ~pos.pieces(Us) : target);
while (b)
moveList = make_move_and_gating<NORMAL>(pos, moveList, Us, ksq, pop_lsb(b));
// Passing move by king
if (pos.pass(Us))
*moveList++ = make<SPECIAL>(ksq, ksq);
if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
for (CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
if (!pos.castling_impeded(cr) && pos.can_castle(cr))
{
Square rookFrom = pos.castling_rook_square(cr);
if (pos.freeze_zone(Us) & (square_bb(ksq) | square_bb(rookFrom)))
continue;
moveList = make_move_and_gating<CASTLING>(pos, moveList, Us,ksq, rookFrom);
}
}
return moveList;
}
} // namespace
/// <CAPTURES> Generates all pseudo-legal captures plus queen promotions
/// <QUIETS> Generates all pseudo-legal non-captures and underpromotions
/// <EVASIONS> Generates all pseudo-legal check evasions when the side to move is in check
/// <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check, except castling and promotions
/// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
///
/// Returns a pointer to the end of the move list.
template<GenType Type>
ExtMove* generate(const Position& pos, ExtMove* moveList) {
static_assert(Type != LEGAL, "Unsupported type in generate()");
assert((Type == EVASIONS) == (bool)pos.checkers());
Color us = pos.side_to_move();
return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
: generate_all<BLACK, Type>(pos, moveList);
}
// Explicit template instantiations
template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
template ExtMove* generate<EVASIONS>(const Position&, ExtMove*);
template ExtMove* generate<QUIET_CHECKS>(const Position&, ExtMove*);
template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
/// generate<LEGAL> generates all the legal moves in the given position
template<>
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
if (pos.is_immediate_game_end())
return moveList;
ExtMove* cur = moveList;
moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
: generate<NON_EVASIONS>(pos, moveList);
while (cur != moveList)
if (!pos.legal(*cur) || pos.virtual_drop(*cur))
*cur = (--moveList)->move;
else
++cur;
return moveList;
}
} // namespace Stockfish