/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
- Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "movegen.h"
#include "position.h"
-/// Simple macro to wrap a very common while loop, no facny, no flexibility,
+/// Simple macro to wrap a very common while loop, no fancy, no flexibility,
/// hardcoded names 'mlist' and 'from'.
#define SERIALIZE(b) while (b) (mlist++)->move = make_move(from, pop_lsb(&b))
(mlist++)->move = make_move(to - (d), to); }
namespace {
- template<CastlingSide Side, bool Checks, bool Chess960>
- ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us) {
+ template<CastlingFlag Cf, bool Checks, bool Chess960>
+ ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {
- if (pos.castling_impeded(us, Side) || !pos.can_castle(make_castling_flag(us, Side)))
+ static const bool KingSide = (Cf == WHITE_OO || Cf == BLACK_OO);
+
+ if (pos.castling_impeded(Cf) || !pos.can_castle(Cf))
return mlist;
// After castling, the rook and king final positions are the same in Chess960
// as they would be in standard chess.
Square kfrom = pos.king_square(us);
- Square rfrom = pos.castling_rook_square(us, Side);
- Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
+ Square rfrom = pos.castling_rook_square(Cf);
+ Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
Bitboard enemies = pos.pieces(~us);
assert(!pos.checkers());
- const int K = Chess960 ? kto > kfrom ? -1 : 1
- : Side == KING_SIDE ? -1 : 1;
+ const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
+ : KingSide ? DELTA_W : DELTA_E;
- for (Square s = kto; s != kfrom; s += (Square)K)
+ for (Square s = kto; s != kfrom; s += K)
if (pos.attackers_to(s) & enemies)
return mlist;
if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
return mlist;
- (mlist++)->move = make<CASTLING>(kfrom, rfrom);
+ Move m = make<CASTLING>(kfrom, rfrom);
+
+ if (Checks && !pos.gives_check(m, *ci))
+ return mlist;
- if (Checks && !pos.gives_check((mlist - 1)->move, CheckInfo(pos)))
- --mlist;
+ (mlist++)->move = m;
return mlist;
}
{
if (pos.is_chess960())
{
- mlist = generate_castling< KING_SIDE, Checks, true>(pos, mlist, Us);
- mlist = generate_castling<QUEEN_SIDE, Checks, true>(pos, mlist, Us);
+ mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
+ mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
}
else
{
- mlist = generate_castling< KING_SIDE, Checks, false>(pos, mlist, Us);
- mlist = generate_castling<QUEEN_SIDE, Checks, false>(pos, mlist, Us);
+ mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
+ mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
}
}
PieceType pt = type_of(pos.piece_on(from));
if (pt == PAWN)
- continue; // Will be generated togheter with direct checks
+ continue; // Will be generated together with direct checks
Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();