@@ -97,6 +97,7 @@ namespace {
9797 template <Color Us> void initialize ();
9898 template <Color Us> Score evaluate_king ();
9999 template <Color Us> Score evaluate_threats ();
100+ int king_distance (Color c, Square s);
100101 template <Color Us> Score evaluate_passed_pawns ();
101102 template <Color Us> Score evaluate_space ();
102103 template <Color Us, PieceType Pt> Score evaluate_pieces ();
@@ -198,8 +199,8 @@ namespace {
198199 // Passed[mg/eg][Rank] contains midgame and endgame bonuses for passed pawns.
199200 // We don't use a Score because we process the two components independently.
200201 const Value Passed[][RANK_NB] = {
201- { V (5 ), V ( 5 ), V (31 ), V (73 ), V (166 ), V (252 ) },
202- { V (7 ), V (14 ), V (38 ), V (73 ), V (166 ), V (252 ) }
202+ { V (0 ), V ( 5 ), V ( 5 ), V (31 ), V (73 ), V (166 ), V (252 ) },
203+ { V (0 ), V ( 7 ), V (14 ), V (38 ), V (73 ), V (166 ), V (252 ) }
203204 };
204205
205206 // PassedFile[File] contains a bonus according to the file of a passed pawn
@@ -208,6 +209,9 @@ namespace {
208209 S (-20 ,-12 ), S ( 1 , -8 ), S ( 2 , 10 ), S ( 9 , 10 )
209210 };
210211
212+ // Rank factor applied on some bonus for passed pawn on rank 4 or beyond
213+ const int RankFactor[RANK_NB] = {0 , 0 , 0 , 2 , 6 , 11 , 16 };
214+
211215 // KingProtector[PieceType-2] contains a bonus according to distance from king
212216 const Score KingProtector[] = { S (-3 , -5 ), S (-4 , -3 ), S (-3 , 0 ), S (-1 , 1 ) };
213217
@@ -486,7 +490,7 @@ namespace {
486490 - 9 * mg_value (score) / 8
487491 + 40 ;
488492
489- // Transform the kingDanger units into a Score, and substract it from the evaluation
493+ // Transform the kingDanger units into a Score, and subtract it from the evaluation
490494 if (kingDanger > 0 )
491495 {
492496 int mobilityDanger = mg_value (mobility[Them] - mobility[Us]);
@@ -622,6 +626,11 @@ namespace {
622626 return score;
623627 }
624628
629+ // helper used by evaluate_passed_pawns to cap the distance
630+ template <Tracing T>
631+ int Evaluation<T>::king_distance(Color c, Square s) {
632+ return std::min (distance (pos.square <KING>(c), s), 5 );
633+ }
625634
626635 // evaluate_passed_pawns() evaluates the passed pawns and candidate passed
627636 // pawns of the given color.
@@ -646,8 +655,8 @@ namespace {
646655 bb = forward_file_bb (Us, s) & (attackedBy[Them][ALL_PIECES] | pos.pieces (Them));
647656 score -= HinderPassedPawn * popcount (bb);
648657
649- int r = relative_rank (Us, s) - RANK_2 ;
650- int rr = r * (r - 1 ) ;
658+ int r = relative_rank (Us, s);
659+ int rr = RankFactor[r] ;
651660
652661 Value mbonus = Passed[MG][r], ebonus = Passed[EG][r];
653662
@@ -656,12 +665,11 @@ namespace {
656665 Square blockSq = s + Up;
657666
658667 // Adjust bonus based on the king's proximity
659- ebonus += distance (pos.square <KING>(Them), blockSq) * 5 * rr
660- - distance (pos.square <KING>( Us), blockSq) * 2 * rr;
668+ ebonus += (king_distance (Them, blockSq) * 5 - king_distance (Us, blockSq) * 2 ) * rr;
661669
662670 // If blockSq is not the queening square then consider also a second push
663- if (relative_rank (Us, blockSq) != RANK_8 )
664- ebonus -= distance (pos. square <KING>(Us) , blockSq + Up) * rr;
671+ if (r != RANK_7 )
672+ ebonus -= king_distance (Us , blockSq + Up) * rr;
665673
666674 // If the pawn is free to advance, then increase the bonus
667675 if (pos.empty (blockSq))
0 commit comments