- // Does either side have an unstoppable passed pawn?
- if(hasUnstoppable[WHITE] && !hasUnstoppable[BLACK])
- ei.egValue += UnstoppablePawnValue - Value(0x40 * movesToGo[WHITE]);
- else if(hasUnstoppable[BLACK] && !hasUnstoppable[WHITE])
- ei.egValue -= UnstoppablePawnValue - Value(0x40 * movesToGo[BLACK]);
- else if(hasUnstoppable[BLACK] && hasUnstoppable[WHITE]) {
- // Both sides have unstoppable pawns! Try to find out who queens
- // first. We begin by transforming 'movesToGo' to the number of
- // plies until the pawn queens for both sides:
- movesToGo[WHITE] *= 2;
- movesToGo[BLACK] *= 2;
- movesToGo[pos.side_to_move()]--;
-
- // If one side queens at least three plies before the other, that
- // side wins:
- if(movesToGo[WHITE] <= movesToGo[BLACK] - 3)
- ei.egValue += UnstoppablePawnValue - Value(0x40 * (movesToGo[WHITE]/2));
- else if(movesToGo[BLACK] <= movesToGo[WHITE] - 3)
- ei.egValue -= UnstoppablePawnValue - Value(0x40 * (movesToGo[BLACK]/2));
-
- // We could also add some rules about the situation when one side
- // queens exactly one ply before the other: Does the first queen
- // check the opponent's king, or attack the opponent's queening square?
- // This is slightly tricky to get right, because it is possible that
- // the opponent's king has moved somewhere before the first pawn queens.
+ // Neither side has an unstoppable passed pawn?
+ if (!(movesToGo[WHITE] | movesToGo[BLACK]))
+ return;
+
+ // Does only one side have an unstoppable passed pawn?
+ if (!movesToGo[WHITE] || !movesToGo[BLACK])
+ {
+ Color winnerSide = movesToGo[WHITE] ? WHITE : BLACK;
+ ei.value += make_score(0, Sign[winnerSide] * (UnstoppablePawnValue - Value(0x40 * movesToGo[winnerSide])));
+ }
+ else
+ { // Both sides have unstoppable pawns! Try to find out who queens
+ // first. We begin by transforming 'movesToGo' to the number of
+ // plies until the pawn queens for both sides.
+ movesToGo[WHITE] *= 2;
+ movesToGo[BLACK] *= 2;
+ movesToGo[pos.side_to_move()]--;
+
+ Color winnerSide = movesToGo[WHITE] < movesToGo[BLACK] ? WHITE : BLACK;
+ Color loserSide = opposite_color(winnerSide);
+
+ // If one side queens at least three plies before the other, that side wins
+ if (movesToGo[winnerSide] <= movesToGo[loserSide] - 3)
+ ei.value += Sign[winnerSide] * make_score(0, UnstoppablePawnValue - Value(0x40 * (movesToGo[winnerSide]/2)));
+
+ // If one side queens one ply before the other and checks the king or attacks
+ // the undefended opponent's queening square, that side wins. To avoid cases
+ // where the opponent's king could move somewhere before first pawn queens we
+ // consider only free paths to queen for both pawns.
+ else if ( !(squares_in_front_of(WHITE, pawnToGo[WHITE]) & pos.occupied_squares())
+ && !(squares_in_front_of(BLACK, pawnToGo[BLACK]) & pos.occupied_squares()))
+ {
+ assert(movesToGo[loserSide] - movesToGo[winnerSide] == 1);
+
+ Square winnerQSq = relative_square(winnerSide, make_square(square_file(pawnToGo[winnerSide]), RANK_8));
+ Square loserQSq = relative_square(loserSide, make_square(square_file(pawnToGo[loserSide]), RANK_8));
+
+ Bitboard b = pos.occupied_squares();
+ clear_bit(&b, pawnToGo[winnerSide]);
+ clear_bit(&b, pawnToGo[loserSide]);
+ b = queen_attacks_bb(winnerQSq, b);
+
+ if ( (b & pos.pieces(KING, loserSide))
+ ||(bit_is_set(b, loserQSq) && !bit_is_set(ei.attacked_by(loserSide), loserQSq)))
+ ei.value += Sign[winnerSide] * make_score(0, UnstoppablePawnValue - Value(0x40 * (movesToGo[winnerSide]/2)));
+ }