summaryrefslogtreecommitdiff
path: root/src/engine/moves/attack.c
blob: 2786ad3eefcbeb40a7b1d317e97b3018063fc37d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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;
}