+ || relative_rank(weakerSide, kingSq) <= relative_rank(weakerSide, pawnSq))
+ return SCALE_FACTOR_DRAW;
+ }
+ }
+
+ // All pawns on same B or G file? Then potential draw
+ if ( (pawnFile == FILE_B || pawnFile == FILE_G)
+ && !(pos.pieces(PAWN) & ~file_bb(pawnFile))
+ && pos.non_pawn_material(weakerSide) == 0
+ && pos.count<PAWN>(weakerSide) >= 1)
+ {
+ // Get weakerSide pawn that is closest to home rank
+ Square weakerPawnSq = backmost_sq(weakerSide, pos.pieces(weakerSide, PAWN));
+
+ Square strongerKingSq = pos.king_square(strongerSide);
+ Square weakerKingSq = pos.king_square(weakerSide);
+ Square bishopSq = pos.list<BISHOP>(strongerSide)[0];
+
+ // Potential for a draw if our pawn is blocked on the 7th rank
+ // the bishop cannot attack it or they only have one pawn left
+ if ( relative_rank(strongerSide, weakerPawnSq) == RANK_7
+ && (pos.pieces(strongerSide, PAWN) & (weakerPawnSq + pawn_push(weakerSide)))
+ && (opposite_colors(bishopSq, weakerPawnSq) || pos.count<PAWN>(strongerSide) == 1))
+ {
+ int strongerKingDist = square_distance(weakerPawnSq, strongerKingSq);
+ int weakerKingDist = square_distance(weakerPawnSq, weakerKingSq);
+
+ // Draw if the weak king is on it's back two ranks, within 2
+ // squares of the blocking pawn and the strong king is not
+ // closer. (I think this rule only fails in practically
+ // unreachable positions such as 5k1K/6p1/6P1/8/8/3B4/8/8 w
+ // and positions where qsearch will immediately correct the
+ // problem such as 8/4k1p1/6P1/1K6/3B4/8/8/8 w)
+ if ( relative_rank(strongerSide, weakerKingSq) >= RANK_7
+ && weakerKingDist <= 2
+ && weakerKingDist <= strongerKingDist)
+ return SCALE_FACTOR_DRAW;