MoveStack* generate(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok());
- assert(!pos.is_check());
+ assert(!pos.in_check());
Color us = pos.side_to_move();
Bitboard target;
MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok());
- assert(!pos.is_check());
+ assert(!pos.in_check());
Bitboard b, dc;
Square from;
while (b)
{
from = pop_1st_bit(&b);
- switch (pos.type_of_piece_on(from))
+ switch (type_of_piece(pos.piece_on(from)))
{
case PAWN: /* Will be generated togheter with pawns direct checks */ break;
case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok());
- assert(pos.is_check());
+ assert(pos.in_check());
Bitboard b, target;
Square from, checksq;
assert(pos.color_of_piece_on(checksq) == opposite_color(us));
- switch (pos.type_of_piece_on(checksq))
+ switch (type_of_piece(pos.piece_on(checksq)))
{
case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
case QUEEN:
- // In case of a queen remove also squares attacked in the other direction to
- // avoid possible illegal moves when queen and king are on adjacent squares.
- if (RookPseudoAttacks[checksq] & (1ULL << ksq))
- sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
+ // If queen and king are far we can safely remove all the squares attacked
+ // in the other direction becuase are not reachable by the king anyway.
+ if (squares_between(ksq, checksq) || (RookPseudoAttacks[checksq] & (1ULL << ksq)))
+ sliderAttacks |= QueenPseudoAttacks[checksq];
+
+ // Otherwise, if king and queen are adjacent and on a diagonal line, we need to
+ // use real rook attacks to check if king is safe to move in the other direction.
+ // For example: king in B2, queen in A1 a knight in B1, and we can safely move to C1.
else
sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
+
default:
break;
}
}
-/// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
-/// or pseudo-legal moves in the current position.
-template<>
-MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
-
- assert(pos.is_ok());
-
- return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
- : generate<MV_NON_EVASION>(pos, mlist);
-}
+/// generate<MV_LEGAL> computes a complete list of legal moves in the current position
template<>
MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
MoveStack *last, *cur = mlist;
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
- last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
+ last = pos.in_check() ? generate<MV_EVASION>(pos, mlist)
+ : generate<MV_NON_EVASION>(pos, mlist);
// Remove illegal moves from the list
while (cur != last)
// Single and double pawn pushes
if (Type != MV_CAPTURE)
{
- b1 = pawnPushes & emptySquares;
+ b1 = (Type != MV_EVASION ? pawnPushes : pawnPushes & emptySquares);
b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
if (Type == MV_CHECK)