-Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
- Bitboard theirPawns, PawnInfo* pi) {
-
- const Color Them = (Us == WHITE ? BLACK : WHITE);
-
- Bitboard b;
- Square s;
- File f;
- Rank r;
- bool passed, isolated, doubled, opposed, chain, backward, candidate;
- Score value = SCORE_ZERO;
- const Square* pl = pos.piece_list(Us, PAWN);
-
- // Loop through all pawns of the current color and score each pawn
- while ((s = *pl++) != SQ_NONE)
- {
- assert(pos.piece_on(s) == make_piece(Us, PAWN));
-
- f = file_of(s);
- r = rank_of(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));
-
- // Flag the pawn as 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 & adjacent_files_bb(f));
- chain = ourPawns & adjacent_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 there are friendly pawns behind on adjacent files
- // or if can capture an enemy pawn it cannot be backward either.
- if ( !(passed | isolated | chain)
- && !(ourPawns & attack_span_mask(Them, s))
- && !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
- {
- // We now know that there are no friendly pawns beside or behind this
- // pawn on adjacent files. We now check whether the pawn is
- // backward by looking in the forward direction on the adjacent
- // 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(opposed | passed | (attack_span_mask(Us, s) & theirPawns));
-
- // A not passed pawn is a candidate to become passed if it is free to
- // advance and if the number of friendly pawns beside or behind this
- // pawn on adjacent files is higher or equal than the number of
- // enemy pawns in the forward direction on the adjacent files.
- candidate = !(opposed | passed | backward | isolated)
- && (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0
- && popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(Us, s) & theirPawns);
-
- // Passed pawns 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)
- 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;
-}
-
-
-template<Color Us>
-int PawnInfo::shelter_storm(const Position& pos, Square ksq) {
-
- const Color Them = (Us == WHITE ? BLACK : WHITE);
-
- int safety = 0;
- Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, ksq) | RankBB[rank_of(ksq)]);
- Bitboard ourPawns = b & pos.pieces(Us) & ~RankBB[rank_of(ksq)];
+Value Entry::shelter_storm(const Position& pos, Square ksq) {
+
+ constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
+ constexpr Bitboard ShelterMask =
+ Us == WHITE ? make_bitboard(SQ_A2, SQ_B3, SQ_C2, SQ_F2, SQ_G3, SQ_H2)
+ : make_bitboard(SQ_A7, SQ_B6, SQ_C7, SQ_F7, SQ_G6, SQ_H7);
+ constexpr Bitboard StormMask =
+ Us == WHITE ? make_bitboard(SQ_A3, SQ_C3, SQ_F3, SQ_H3)
+ : make_bitboard(SQ_A6, SQ_C6, SQ_F6, SQ_H6);
+
+ enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked };
+
+ File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
+ Bitboard b = pos.pieces(PAWN)
+ & (forward_ranks_bb(Us, ksq) | rank_bb(ksq))
+ & (adjacent_files_bb(center) | file_bb(center));
+ Bitboard ourPawns = b & pos.pieces(Us);