-Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
- Bitboard theirPawns, PawnInfo* pi) const {
- Bitboard b;
- Square s;
- File f;
- Rank r;
- bool passed, isolated, doubled, opposed, chain, backward, candidate;
- Score value = SCORE_ZERO;
- const BitCountType Max15 = CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
- const Square* ptr = pos.piece_list_begin(Us, PAWN);
-
- // Loop through all pawns of the current color and score each pawn
- while ((s = *ptr++) != SQ_NONE)
- {
- assert(pos.piece_on(s) == piece_of_color_and_type(Us, PAWN));
-
- f = square_file(s);
- r = square_rank(s);
-
- // This file cannot be half open
- pi->halfOpenFiles[Us] &= ~(1 << f);
-
- // Our rank plus previous one. Used for chain detection.
- b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1));
-
- // Passed, isolated, doubled or member of a pawn
- // chain (but not the backward one) ?
- passed = !(theirPawns & passed_pawn_mask(Us, s));
- doubled = ourPawns & squares_in_front_of(Us, s);
- opposed = theirPawns & squares_in_front_of(Us, s);
- isolated = !(ourPawns & neighboring_files_bb(f));
- chain = ourPawns & neighboring_files_bb(f) & b;
-
- // Test for backward pawn
- //
- backward = false;
-
- // If the pawn is passed, isolated, or member of a pawn chain
- // it cannot be backward. If can capture an enemy pawn or if
- // there are friendly pawns behind on neighboring files it cannot
- // be backward either.
- if ( !(passed | isolated | chain)
- && !(ourPawns & attack_span_mask(opposite_color(Us), s))
- && !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
- {
- // We now know that there are no friendly pawns beside or behind this
- // pawn on neighboring files. We now check whether the pawn is
- // backward by looking in the forward direction on the neighboring
- // files, and seeing whether we meet a friendly or an enemy pawn first.
- b = pos.attacks_from<PAWN>(s, Us);
-
- // Note that we are sure to find something because pawn is not passed
- // nor isolated, so loop is potentially infinite, but it isn't.
- while (!(b & (ourPawns | theirPawns)))
- Us == WHITE ? b <<= 8 : b >>= 8;
-
- // The friendly pawn needs to be at least two ranks closer than the enemy
- // pawn in order to help the potentially backward pawn advance.
- backward = (b | (Us == WHITE ? b << 8 : b >> 8)) & theirPawns;
- }
-
- assert(passed | opposed | (attack_span_mask(Us, s) & theirPawns));
-
- // Test for candidate passed pawn
- candidate = !(opposed | passed)
- && (b = attack_span_mask(opposite_color(Us), s + pawn_push(Us)) & ourPawns) != EmptyBoardBB
- && count_1s<Max15>(b) >= count_1s<Max15>(attack_span_mask(Us, s) & theirPawns);
-
- // Mark the pawn as passed. Pawn will be properly scored in evaluation
- // because we need full attack info to evaluate passed pawns. Only the
- // frontmost passed pawn on each file is considered a true passed pawn.
- if (passed && !doubled)
- set_bit(&(pi->passedPawns[Us]), s);
-
- // Score this pawn
- if (isolated)
- value -= IsolatedPawnPenalty[opposed][f];
-
- if (doubled)
- value -= DoubledPawnPenalty[opposed][f];
-
- if (backward)
- value -= BackwardPawnPenalty[opposed][f];
-
- if (chain)
- value += ChainBonus[f];
-
- if (candidate)
- value += CandidateBonus[relative_rank(Us, s)];
- }
- return value;
+Score Entry::do_king_safety(const Position& pos) {
+
+ Square ksq = pos.square<KING>(Us);
+ kingSquares[Us] = ksq;
+ castlingRights[Us] = pos.castling_rights(Us);
+ auto compare = [](Score a, Score b) { return mg_value(a) < mg_value(b); };
+
+ Score shelter = evaluate_shelter<Us>(pos, ksq);
+
+ // If we can castle use the bonus after castling if it is bigger
+
+ if (pos.can_castle(Us & KING_SIDE))
+ shelter = std::max(shelter, evaluate_shelter<Us>(pos, relative_square(Us, SQ_G1)), compare);
+
+ if (pos.can_castle(Us & QUEEN_SIDE))
+ shelter = std::max(shelter, evaluate_shelter<Us>(pos, relative_square(Us, SQ_C1)), compare);
+
+ // In endgame we like to bring our king near our closest pawn
+ Bitboard pawns = pos.pieces(Us, PAWN);
+ int minPawnDist = pawns ? 8 : 0;
+
+ if (pawns & PseudoAttacks[KING][ksq])
+ minPawnDist = 1;
+ else while (pawns)
+ minPawnDist = std::min(minPawnDist, distance(ksq, pop_lsb(&pawns)));
+
+ return shelter - make_score(0, 16 * minPawnDist);