- // and the defending king cannot gain tempi by threatening the attacking
- // rook.
- if(r == RANK_7 && f != FILE_A && square_file(wrsq) == f
- && wrsq != queeningSq
- && (square_distance(wksq, queeningSq) <
- square_distance(bksq, queeningSq) - 2 + tempo)
- && (square_distance(wksq, queeningSq) <
- square_distance(bksq, wrsq) + tempo))
- return ScaleFactor(SCALE_FACTOR_MAX
- - 2 * square_distance(wksq, queeningSq));
-
- // Similar to the above, but with the pawn further back:
- if(f != FILE_A && square_file(wrsq) == f && wrsq < wpsq
- && (square_distance(wksq, queeningSq) <
- square_distance(bksq, queeningSq) - 2 + tempo)
- && (square_distance(wksq, wpsq + DELTA_N) <
- square_distance(bksq, wpsq + DELTA_N) - 2 + tempo)
- && (square_distance(bksq, wrsq) + tempo >= 3
- || (square_distance(wksq, queeningSq) <
- square_distance(bksq, wrsq) + tempo
- && (square_distance(wksq, wpsq + DELTA_N) <
- square_distance(bksq, wrsq) + tempo))))
- return
- ScaleFactor(SCALE_FACTOR_MAX
- - (8 * square_distance(wpsq, queeningSq) +
- 2 * square_distance(wksq, queeningSq)));
-
+ // and the defending king cannot gain tempi by threatening the attacking rook.
+ if ( r == RANK_7
+ && f != FILE_A
+ && square_file(wrsq) == f
+ && wrsq != queeningSq
+ && (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo)
+ && (square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo))
+ return ScaleFactor(SCALE_FACTOR_MAX - 2 * square_distance(wksq, queeningSq));
+
+ // Similar to the above, but with the pawn further back
+ if ( f != FILE_A
+ && square_file(wrsq) == f
+ && wrsq < wpsq
+ && (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo)
+ && (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wpsq + DELTA_N) - 2 + tempo)
+ && ( square_distance(bksq, wrsq) + tempo >= 3
+ || ( square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo
+ && (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wrsq) + tempo))))
+ return ScaleFactor( SCALE_FACTOR_MAX
+ - (8 * square_distance(wpsq, queeningSq)
+ + 2 * square_distance(wksq, queeningSq)));
+
+ // If the pawn is not far advanced, and the defending king is somewhere in
+ // the pawn's path, it's probably a draw.
+ if (r <= RANK_4 && bksq > wpsq)
+ {
+ if (square_file(bksq) == square_file(wpsq))
+ return ScaleFactor(10);
+ if ( abs(square_file(bksq) - square_file(wpsq)) == 1
+ && square_distance(wksq, bksq) > 2)
+ return ScaleFactor(24 - 2 * square_distance(wksq, bksq));
+ }