#define MOVES_INTERNAL #include "engine/moves.h" static bool pawn_attacks_square(position_t position, square_t square, u8 by_color) { bitboard_t enemy_pawns; if (by_color == WHITE_TURN) { enemy_pawns = position.bitboards[WHITE_PAWN]; if (square < 8) return false; bitboard_t nw = (square % 8 == 7) ? 0 : (enemy_pawns >> (square - 7)); bitboard_t ne = (square % 8 == 0) ? 0 : (enemy_pawns >> (square - 9)); return (nw | ne) & 1; } else { enemy_pawns = position.bitboards[BLACK_PAWN]; if (square > 54) return false; bitboard_t se = (square % 8 == 0) ? 0 : (enemy_pawns >> (square + 7)); bitboard_t sw = (square % 8 == 7) ? 0 : (enemy_pawns >> (square + 9)); return (se | sw) & 1; } } static bool knight_attacks_square(position_t position, square_t square, u8 by_color) { bitboard_t enemy_knights = (by_color == WHITE_TURN) ? position.bitboards[WHITE_KNIGHT] : position.bitboards[BLACK_KNIGHT]; return knight_moves[square] & enemy_knights; } static bool king_attacks_square(position_t position, square_t square, u8 by_color) { bitboard_t enemy_king = (by_color == WHITE_TURN) ? position.bitboards[WHITE_KING] : position.bitboards[BLACK_KING]; bitboard_t movement; if (square >= 10) { movement = 920078ULL << (square - 10); } else { movement = 920078ULL >> -(square - 10); } if ((bitboard_t)1 << square & BITMASK_FILE_A) { movement &= BITMASK_FILE_A | BITMASK_FILE_B; } else if ((bitboard_t)1 << square & BITMASK_FILE_H) { movement &= BITMASK_FILE_G | BITMASK_FILE_H; } return movement & enemy_king; } static bool sliding_attacks_square( position_t position, square_t square, u8 by_color, bool check_rook, bool check_bishop ) { bitboard_t all_occupied = whites(position) | blacks(position); if (check_rook) { bitboard_t rook_like = (by_color == WHITE_TURN) ? (position.bitboards[WHITE_ROOK] | position.bitboards[WHITE_QUEEN]) : (position.bitboards[BLACK_ROOK] | position.bitboards[BLACK_QUEEN]); if (get_rook_attacks(square, all_occupied) & rook_like) return true; } if (check_bishop) { bitboard_t bishop_like = (by_color == WHITE_TURN) ? (position.bitboards[WHITE_BISHOP] | position.bitboards[WHITE_QUEEN]) : (position.bitboards[BLACK_BISHOP] | position.bitboards[BLACK_QUEEN]); if (get_bishop_attacks(square, all_occupied) & bishop_like) return true; } return false; } bool square_attacked(position_t position, square_t square, u8 by_color) { if (pawn_attacks_square(position, square, by_color)) return true; if (knight_attacks_square(position, square, by_color)) return true; if (king_attacks_square(position, square, by_color)) return true; if (sliding_attacks_square(position, square, by_color, true, true)) return true; return false; }