diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-20 20:11:19 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-20 20:11:19 +0200 |
| commit | 058fecaefaf07fc3d84f395313eb6ad0f10e81a7 (patch) | |
| tree | b1e59e6ddba198c14114ee7fb89232a3669e1bb9 | |
| parent | 65e4550c637a839267b5a911a2b48ec53e488570 (diff) | |
perf(board): making is square attacked helper use modern bitboard aproach
| -rw-r--r-- | src/moves.cpp | 60 |
1 files changed, 17 insertions, 43 deletions
diff --git a/src/moves.cpp b/src/moves.cpp index 44e78b2..531d8a9 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -553,64 +553,38 @@ static void GenerateKingMoves(const Game &g, const uint8_t &from, bool IsSquareAttacked(const Game &g, Position square, bool byColor) { int target = PositionToIndex(square); - // Pawn attacks: a pawn of byColor attacks diagonally "forward" from its - // own perspective. White pawns (rank increases toward rank 7) attack from - // one rank below the target; Black pawns attack from one rank above. - int pawnRank = square.rank + (byColor ? -1 : 1); - - if (pawnRank >= 0 && pawnRank < 8) { - for (int fileOffset : {-1, 1}) { - int file = square.file + fileOffset; - if (file < 0 || file >= 8) - continue; - - Piece p = g.pieces[pawnRank * 8 + file]; - if (p.type == PAWN && p.color == byColor) { - return true; - } - } + uint64_t pawns_attacks = + PAWN_ATTACKS[static_cast<size_t>(!byColor)][static_cast<size_t>(target)]; + pawns_attacks &= g.PieceBitboards[static_cast<size_t>(byColor)][PAWN]; + if (pawns_attacks > 0) { + return true; } // Knight attacks - constexpr std::array<int, 8> knightOffsets{-17, -15, -10, -6, 6, 10, 15, 17}; - for (int offset : knightOffsets) { - int from = target + offset; - if (from < 0 || from >= 64) - continue; - - int fileDiff = std::abs((from % 8) - (target % 8)); - if (fileDiff != 1 && fileDiff != 2) - continue; - - Piece p = g.pieces[from]; - if (p.type == KNIGHT && p.color == byColor) - return true; + uint64_t knightAttacks = KNIGHT_ATTACKS[static_cast<size_t>(target)]; + knightAttacks &= g.PieceBitboards[static_cast<size_t>(byColor)][KNIGHT]; + if (knightAttacks > 0) { + return true; } // King attacks - constexpr std::array<int, 8> kingOffsets{-9, -8, -7, -1, 1, 7, 8, 9}; - for (int offset : kingOffsets) { - int from = target + offset; - if (from < 0 || from >= 64) - continue; - if (std::abs((from % 8) - (target % 8)) > 1) - continue; - - Piece p = g.pieces[from]; - if (p.type == KING && p.color == byColor) - return true; + uint64_t king_attacks = KING_ATTACKS[static_cast<size_t>(target)]; + king_attacks &= g.PieceBitboards[static_cast<size_t>(byColor)][KING]; + if (king_attacks > 0) { + return true; } - // Rooks + queens (orthogonal rays) constexpr std::array<int, 4> rookDirs{-8, 8, -1, 1}; for (int dir : rookDirs) { int pos = target; while (true) { int next = pos + dir; - if (next < 0 || next >= 64) + if (next < 0 || next >= 64) { break; - if ((dir == 1 || dir == -1) && next / 8 != pos / 8) + } + if ((dir == 1 || dir == -1) && next / 8 != pos / 8) { break; // horizontal wrap + } Piece p = g.pieces[next]; if (p.type != NONEPIECE) { |
