Fix SEE bugs and ensure getKingAttacks works even if the king is removed from the board

This commit is contained in:
Mattia Giambirtone 2024-05-13 14:47:36 +02:00
parent 2c840f2f3c
commit 7fcdf25dbb
Signed by: nocturn9x
GPG Key ID: 37B83AB6C3BE6514
2 changed files with 10 additions and 5 deletions

View File

@ -113,14 +113,18 @@ func getPawnAttacks*(self: Position, square: Square, attacker: PieceColor): Bitb
return self.getBitboard(Pawn, attacker) and getPawnAttacks(attacker, square)
func getKingAttacks*(self: Position, square: Square, attacker: PieceColor): Bitboard {.inline.} =
proc getKingAttacks*(self: Position, square: Square, attacker: PieceColor): Bitboard {.inline.} =
## Returns the location of the king if it is attacking the given square
result = Bitboard(0)
let king = self.getBitboard(King, attacker)
if king == 0:
# The king was removed (probably by SEE or some
# other internal machinery). This should never
# occur during normal movegen!
return
if (getKingAttacks(king.toSquare()) and square.toBitboard()) != 0:
return king
func getKnightAttacks*(self: Position, square: Square, attacker: PieceColor): Bitboard =
## Returns the locations of the knights attacking the given square
let

View File

@ -101,9 +101,10 @@ proc see(position: Position, square: Square): int =
let
epTarget = position.enPassantSquare.toBitboard()
epPawn = epTarget.backwardRelativeTo(sideToMove).toSquare()
victimPiece = position.getPiece(epPawn)
victim = victimPiece.getStaticPieceScore()
position.removePiece(epPawn)
if position.getPiece(epPawn) != nullPiece():
victimPiece = position.getPiece(epPawn)
victim = victimPiece.getStaticPieceScore()
position.removePiece(epPawn)
# Capture with promotion
if attackerPiece.kind == Pawn and getRankMask(rankFromSquare(square)) == attackerPiece.color.getLastRank():