+ return mlist;
+ }
+
+ template<CastlingSide Side>
+ MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
+
+ Color us = pos.side_to_move();
+
+ if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
+ ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
+ {
+ Color them = opposite_color(us);
+ Square ksq = pos.king_square(us);
+
+ assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
+
+ Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
+ Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
+ Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
+ Square s;
+ bool illegal = false;
+
+ assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
+
+ // It is a bit complicated to correctly handle Chess960
+ for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
+ if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
+ || pos.square_is_attacked(s, them))
+ illegal = true;
+
+ for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
+ if (s != ksq && s != rsq && pos.square_is_occupied(s))
+ illegal = true;
+
+ if ( Side == QUEEN_SIDE
+ && square_file(rsq) == FILE_B
+ && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
+ || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
+ illegal = true;
+
+ if (!illegal)
+ (*mlist++).move = make_castle_move(ksq, rsq);
+ }
+ return mlist;
+ }
+
+ bool castling_is_check(const Position& pos, CastlingSide side) {
+
+ // After castling opponent king is attacked by the castled rook?
+ File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
+ Color us = pos.side_to_move();
+ Square ksq = pos.king_square(us);
+ Bitboard occ = pos.occupied_squares();
+
+ clear_bit(&occ, ksq); // Remove our king from the board
+ Square rsq = make_square(rookFile, square_rank(ksq));
+ return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));