X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fmovegen.cpp;h=6f932cd05de6befd53b89737c5e0aadae1cdb516;hp=b130247bea5f151cd810ec3e73ddd8b1a1204a83;hb=ee0afea1e57e4c39140c838b9f9f2d5c7c061e6b;hpb=57b3ca916fdf12ad4ad3dfdcd215379e908b12be diff --git a/src/movegen.cpp b/src/movegen.cpp index b130247b..6f932cd0 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -26,6 +26,7 @@ #include "bitcount.h" #include "movegen.h" +#include "types.h" // Simple macro to wrap a very common while loop, no facny, no flexibility, // hardcoded list name 'mlist' and from square 'from'. @@ -46,7 +47,7 @@ namespace { }; template - MoveStack* generate_castle_moves(const Position&, MoveStack*); + MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us); template MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square); @@ -99,14 +100,14 @@ namespace { } template<> - inline MoveStack* generate_direct_checks(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) { + FORCE_INLINE MoveStack* generate_direct_checks(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) { return (us == WHITE ? generate_pawn_moves(p, m, dc, ksq) : generate_pawn_moves(p, m, dc, ksq)); } template - inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) { + FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) { assert(Piece == PAWN); assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION); @@ -116,7 +117,7 @@ namespace { } template - inline MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { + FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { Bitboard b; Square from; @@ -134,7 +135,7 @@ namespace { } template<> - inline MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { + FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { Bitboard b; Square from = pos.king_square(us); @@ -160,7 +161,7 @@ namespace { /// generate generates all pseudo-legal captures and /// non-captures. Returns a pointer to the end of the move list. -template +template MoveStack* generate(const Position& pos, MoveStack* mlist) { assert(pos.is_ok()); @@ -169,21 +170,21 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) { Color us = pos.side_to_move(); Bitboard target; - if (T == MV_CAPTURE || T == MV_NON_EVASION) + if (Type == MV_CAPTURE || Type == MV_NON_EVASION) target = pos.pieces_of_color(opposite_color(us)); - else if (T == MV_NON_CAPTURE) + else if (Type == MV_NON_CAPTURE) target = pos.empty_squares(); else assert(false); - if (T == MV_NON_EVASION) + if (Type == MV_NON_EVASION) { mlist = generate_piece_moves(pos, mlist, us, target); mlist = generate_piece_moves(pos, mlist, us, pos.empty_squares()); target |= pos.empty_squares(); } else - mlist = generate_piece_moves(pos, mlist, us, target); + mlist = generate_piece_moves(pos, mlist, us, target); mlist = generate_piece_moves(pos, mlist, us, target); mlist = generate_piece_moves(pos, mlist, us, target); @@ -191,10 +192,13 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) { mlist = generate_piece_moves(pos, mlist, us, target); mlist = generate_piece_moves(pos, mlist, us, target); - if (T != MV_CAPTURE) + if (Type != MV_CAPTURE) { - mlist = generate_castle_moves(pos, mlist); - mlist = generate_castle_moves(pos, mlist); + if (pos.can_castle_kingside(us)) + mlist = generate_castle_moves(pos, mlist, us); + + if (pos.can_castle_queenside(us)) + mlist = generate_castle_moves(pos, mlist, us); } return mlist; @@ -622,45 +626,41 @@ namespace { } template - MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) { + MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) { - Color us = pos.side_to_move(); + Color them = opposite_color(us); + Square ksq = pos.king_square(us); - 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)); - 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; - 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)); - 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.attackers_to(s) & pos.pieces_of_color(them))) + illegal = true; - // 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.attackers_to(s) & pos.pieces_of_color(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; - 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 ( 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); - if (!illegal) - (*mlist++).move = make_castle_move(ksq, rsq); - } return mlist; } -} + +} // namespace