Skip to content

Commit

Permalink
⚖️ Add opponent king buckets to passed pawns bucketed arrays (#1349)
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio authored Jan 14, 2025
1 parent c7fc722 commit e4163ec
Show file tree
Hide file tree
Showing 3 changed files with 5,818 additions and 5,392 deletions.
16 changes: 9 additions & 7 deletions src/Lynx/Model/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public bool WasProduceByAValidMove()
bitboard.ResetLS1B();

gamePhase += GamePhaseByPiece[pieceIndex];
packedScore += AdditionalPieceEvaluation(whiteBucket, pieceSquareIndex, pieceIndex, (int)Side.White, whiteKing, blackKing, blackPawnAttacks);
packedScore += AdditionalPieceEvaluation(whiteBucket, blackBucket, pieceSquareIndex, pieceIndex, (int)Side.White, whiteKing, blackKing, blackPawnAttacks);
}
}

Expand All @@ -603,7 +603,7 @@ public bool WasProduceByAValidMove()
bitboard.ResetLS1B();

gamePhase += GamePhaseByPiece[pieceIndex];
packedScore -= AdditionalPieceEvaluation(blackBucket, pieceSquareIndex, pieceIndex, (int)Side.Black, blackKing, whiteKing, whitePawnAttacks);
packedScore -= AdditionalPieceEvaluation(blackBucket, whiteBucket, pieceSquareIndex, pieceIndex, (int)Side.Black, blackKing, whiteKing, whitePawnAttacks);
}
}
}
Expand All @@ -629,7 +629,7 @@ public bool WasProduceByAValidMove()

gamePhase += GamePhaseByPiece[pieceIndex];

packedScore += AdditionalPieceEvaluation(whiteBucket, pieceSquareIndex, pieceIndex, (int)Side.White, whiteKing, blackKing, blackPawnAttacks);
packedScore += AdditionalPieceEvaluation(whiteBucket, blackBucket, pieceSquareIndex, pieceIndex, (int)Side.White, whiteKing, blackKing, blackPawnAttacks);
}
}

Expand All @@ -652,7 +652,7 @@ public bool WasProduceByAValidMove()

gamePhase += GamePhaseByPiece[pieceIndex];

packedScore -= AdditionalPieceEvaluation(blackBucket, pieceSquareIndex, pieceIndex, (int)Side.Black, blackKing, whiteKing, whitePawnAttacks);
packedScore -= AdditionalPieceEvaluation(blackBucket, whiteBucket, pieceSquareIndex, pieceIndex, (int)Side.Black, blackKing, whiteKing, whitePawnAttacks);
}
}

Expand Down Expand Up @@ -816,11 +816,11 @@ public static int EvaluateFinalPosition(int ply, bool isInCheck)
/// Doesn't include <see cref="Piece.K"/> and <see cref="Piece.k"/> evaluation
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int AdditionalPieceEvaluation(int bucket, int pieceSquareIndex, int pieceIndex, int pieceSide, int sameSideKingSquare, int oppositeSideKingSquare, BitBoard enemyPawnAttacks)
internal int AdditionalPieceEvaluation(int bucket, int oppositeSideBucket, int pieceSquareIndex, int pieceIndex, int pieceSide, int sameSideKingSquare, int oppositeSideKingSquare, BitBoard enemyPawnAttacks)
{
return pieceIndex switch
{
(int)Piece.P or (int)Piece.p => PawnAdditionalEvaluation(bucket, pieceSquareIndex, pieceIndex, sameSideKingSquare, oppositeSideKingSquare),
(int)Piece.P or (int)Piece.p => PawnAdditionalEvaluation(bucket, oppositeSideBucket, pieceSquareIndex, pieceIndex, sameSideKingSquare, oppositeSideKingSquare),
(int)Piece.R or (int)Piece.r => RookAdditionalEvaluation(pieceSquareIndex, pieceIndex, pieceSide, oppositeSideKingSquare, enemyPawnAttacks),
(int)Piece.B or (int)Piece.b => BishopAdditionalEvaluation(pieceSquareIndex, pieceIndex, pieceSide, oppositeSideKingSquare, enemyPawnAttacks),
(int)Piece.N or (int)Piece.n => KnightAdditionalEvaluation(pieceSquareIndex, pieceSide, oppositeSideKingSquare, enemyPawnAttacks),
Expand All @@ -830,7 +830,7 @@ internal int AdditionalPieceEvaluation(int bucket, int pieceSquareIndex, int pie
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int PawnAdditionalEvaluation(int bucket, int squareIndex, int pieceIndex, int sameSideKingSquare, int oppositeSideKingSquare)
private int PawnAdditionalEvaluation(int bucket, int oppositeSideBucket, int squareIndex, int pieceIndex, int sameSideKingSquare, int oppositeSideKingSquare)
{
int packedBonus = 0;

Expand All @@ -857,6 +857,7 @@ private int PawnAdditionalEvaluation(int bucket, int squareIndex, int pieceIndex
if ((passedPawnsMask & OccupancyBitBoards[oppositeSide]) == 0)
{
packedBonus += PassedPawnBonusNoEnemiesAheadBonus[bucket][rank];
packedBonus += PassedPawnBonusNoEnemiesAheadEnemyBonus[oppositeSideBucket][rank];
}

// King distance to passed pawn
Expand All @@ -866,6 +867,7 @@ private int PawnAdditionalEvaluation(int bucket, int squareIndex, int pieceIndex
var enemyKingDistance = Constants.ChebyshevDistance[squareIndex][oppositeSideKingSquare];

packedBonus += PassedPawnBonus[bucket][rank]
+ PassedPawnEnemyBonus[oppositeSideBucket][rank]
+ FriendlyKingDistanceToPassedPawnBonus[friendlyKingDistance]
+ EnemyKingDistanceToPassedPawnPenalty[enemyKingDistance];
}
Expand Down
Loading

0 comments on commit e4163ec

Please sign in to comment.