- // If one side has only a king, check whether exists any unstoppable passed pawn
- if (!pos.non_pawn_material(WHITE) || !pos.non_pawn_material(BLACK))
- score += evaluate_unstoppable_pawns(pos, ei);
-
- // Evaluate space for both sides, only in middle-game.
- if (ei.mi->space_weight())
- {
- int s = evaluate_space<WHITE>(pos, ei) - evaluate_space<BLACK>(pos, ei);
- score += apply_weight(make_score(s * ei.mi->space_weight(), 0), Weights[Space]);
- }
-
- // Scale winning side if position is more drawish that what it appears
- ScaleFactor sf = eg_value(score) > VALUE_DRAW ? ei.mi->scale_factor(pos, WHITE)
- : ei.mi->scale_factor(pos, BLACK);
-
- // If we don't already have an unusual scale factor, check for opposite
- // colored bishop endgames, and use a lower scale for those.
- if ( ei.mi->game_phase() < PHASE_MIDGAME
- && pos.opposite_bishops()
- && sf == SCALE_FACTOR_NORMAL)
- {
- // Only the two bishops ?
- if ( pos.non_pawn_material(WHITE) == BishopValueMg
- && pos.non_pawn_material(BLACK) == BishopValueMg)
- {
- // Check for KBP vs KB with only a single pawn that is almost
- // certainly a draw or at least two pawns.
- bool one_pawn = (pos.count<PAWN>(WHITE) + pos.count<PAWN>(BLACK) == 1);
- sf = one_pawn ? ScaleFactor(8) : ScaleFactor(32);
- }
- else
- // Endgame with opposite-colored bishops, but also other pieces. Still
- // a bit drawish, but not as drawish as with only the two bishops.
- sf = ScaleFactor(50);
- }
-
- margin = margins[pos.side_to_move()];
- Value v = interpolate(score, ei.mi->game_phase(), sf);
-
- // In case of tracing add all single evaluation contributions for both white and black
- if (Trace)
- {
- Tracing::add(PST, pos.psq_score());
- Tracing::add(IMBALANCE, ei.mi->material_value());
- Tracing::add(PAWN, ei.pi->pawns_value());
- Tracing::add(UNSTOPPABLE, evaluate_unstoppable_pawns(pos, ei));
- Score w = make_score(ei.mi->space_weight() * evaluate_space<WHITE>(pos, ei), 0);
- Score b = make_score(ei.mi->space_weight() * evaluate_space<BLACK>(pos, ei), 0);
- Tracing::add(SPACE, apply_weight(w, Weights[Space]), apply_weight(b, Weights[Space]));
- Tracing::add(TOTAL, score);
- Tracing::stream << "\nUncertainty margin: White: " << to_cp(margins[WHITE])
- << ", Black: " << to_cp(margins[BLACK])
- << "\nScaling: " << std::noshowpos
- << std::setw(6) << 100.0 * ei.mi->game_phase() / 128.0 << "% MG, "
- << std::setw(6) << 100.0 * (1.0 - ei.mi->game_phase() / 128.0) << "% * "
- << std::setw(6) << (100.0 * sf) / SCALE_FACTOR_NORMAL << "% EG.\n"
- << "Total evaluation: " << to_cp(v);
- }
-
- return pos.side_to_move() == WHITE ? v : -v;
-}
-
-
- // init_eval_info() initializes king bitboards for given color adding
- // pawn attacks. To be done at the beginning of the evaluation.
-
- template<Color Us>
- void init_eval_info(const Position& pos, EvalInfo& ei) {
-
- const Color Them = (Us == WHITE ? BLACK : WHITE);
- const Square Down = (Us == WHITE ? DELTA_S : DELTA_N);
-
- Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.king_square(Them));
- ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us);
-
- // Init king safety tables only if we are going to use them
- if (pos.count<QUEEN>(Us) && pos.non_pawn_material(Us) > QueenValueMg + PawnValueMg)
- {
- ei.kingRing[Them] = b | shift_bb<Down>(b);
- b &= ei.attackedBy[Us][PAWN];
- ei.kingAttackersCount[Us] = b ? popcount<Max15>(b) / 2 : 0;
- ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0;
- } else
- ei.kingRing[Them] = ei.kingAttackersCount[Us] = 0;
- }
-
-
- // evaluate_outposts() evaluates bishop and knight outposts squares
-
- template<PieceType Piece, Color Us>
- Score evaluate_outposts(const Position& pos, EvalInfo& ei, Square s) {