- S(34,68), S(83,166), S(0, 0), S( 0, 0)
- };
-
- // UnpairedPawnsTable[] gives a score according to the number
- // of panws that do not have an enemy pawn in front of them.
- const int UnpairedPawnsTable[8] = { 32, 48, 64, 80, 96, 112, 128, 128 };
-
- // Pawn storm tables for positions with opposite castling
- const int QStormTable[64] = {
- 0, 0, 0, 0, 0, 0, 0, 0,
- -22,-22,-22,-14,-6, 0, 0, 0,
- -6,-10,-10,-10,-6, 0, 0, 0,
- 4, 12, 16, 12, 4, 0, 0, 0,
- 16, 23, 23, 16, 0, 0, 0, 0,
- 23, 31, 31, 23, 0, 0, 0, 0,
- 23, 31, 31, 23, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0
- };
-
- const int KStormTable[64] = {
- 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0,-10,-19,-28,-33,-33,
- 0, 0, 0,-10,-15,-19,-24,-24,
- 0, 0, 0, 0, 1, 1, 1, 1,
- 0, 0, 0, 0, 1, 10, 19, 19,
- 0, 0, 0, 0, 1, 19, 31, 27,
- 0, 0, 0, 0, 0, 22, 31, 22,
- 0, 0, 0, 0, 0, 0, 0, 0
- };
-
- // Pawn storm open file bonuses by file
- const int16_t KStormOpenFileBonus[8] = { 31, 31, 18, 0, 0, 0, 0, 0 };
- const int16_t QStormOpenFileBonus[8] = { 0, 0, 0, 0, 0, 26, 42, 26 };
-
- // Pawn storm lever bonuses by file
- const int StormLeverBonus[8] = { -8, -8, -13, 0, 0, -13, -8, -8 };
+ S(34,68), S(83,166), S(0, 0), S( 0, 0) };
+
+ // Weakness of our pawn shelter in front of the king indexed by [rank]
+ const Value ShelterWeakness[RANK_NB] =
+ { V(100), V(0), V(27), V(73), V(92), V(101), V(101) };
+
+ // Danger of enemy pawns moving toward our king indexed by
+ // [no friendly pawn | pawn unblocked | pawn blocked][rank of enemy pawn]
+ const Value StormDanger[3][RANK_NB] = {
+ { V( 0), V(64), V(128), V(51), V(26) },
+ { V(26), V(32), V( 96), V(38), V(20) },
+ { V( 0), V( 0), V( 64), V(25), V(13) } };
+
+ // Max bonus for king safety. Corresponds to start position with all the pawns
+ // in front of the king and no enemy pawn on the horizon.
+ const Value MaxSafetyBonus = V(263);
+
+ #undef S
+ #undef V
+
+ template<Color Us>
+ Score evaluate(const Position& pos, Pawns::Entry* e) {
+
+ const Color Them = (Us == WHITE ? BLACK : WHITE);
+ const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
+ const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
+ const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
+
+ Bitboard b;
+ Square s;
+ File f;
+ bool passed, isolated, doubled, opposed, chain, backward, candidate;
+ Score value = SCORE_ZERO;
+ const Square* pl = pos.list<PAWN>(Us);
+
+ Bitboard ourPawns = pos.pieces(Us, PAWN);
+ Bitboard theirPawns = pos.pieces(Them, PAWN);
+
+ e->passedPawns[Us] = e->candidatePawns[Us] = 0;
+ e->kingSquares[Us] = SQ_NONE;
+ e->semiopenFiles[Us] = 0xFF;
+ e->pawnAttacks[Us] = shift_bb<Right>(ourPawns) | shift_bb<Left>(ourPawns);
+ e->pawnsOnSquares[Us][BLACK] = popcount<Max15>(ourPawns & DarkSquares);
+ e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
+
+ // 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);
+
+ // This file cannot be semi-open
+ e->semiopenFiles[Us] &= ~(1 << f);
+
+ // Our rank plus previous one. Used for chain detection
+ b = rank_bb(s) | rank_bb(s - pawn_push(Us));
+
+ // Flag the pawn as passed, isolated, doubled or member of a pawn
+ // chain (but not the backward one).
+ chain = ourPawns & adjacent_files_bb(f) & b;
+ isolated = !(ourPawns & adjacent_files_bb(f));
+ doubled = ourPawns & forward_bb(Us, s);
+ opposed = theirPawns & forward_bb(Us, s);
+ passed = !(theirPawns & passed_pawn_mask(Us, s));
+
+ // Test for backward pawn.
+ // 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 & pawn_attack_span(Them, s))
+ || (pos.attacks_from<PAWN>(s, Us) & theirPawns))
+ backward = false;
+ else
+ {
+ // 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 picking the closest pawn there.
+ b = pawn_attack_span(Us, s) & (ourPawns | theirPawns);
+ b = pawn_attack_span(Us, s) & rank_bb(backmost_sq(Us, b));
+
+ // If we have an enemy pawn in the same or next rank, the pawn is
+ // backward because it cannot advance without being captured.
+ backward = (b | shift_bb<Up>(b)) & theirPawns;
+ }
+
+ assert(opposed | passed | (pawn_attack_span(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 = pawn_attack_span(Them, s + pawn_push(Us)) & ourPawns) != 0
+ && popcount<Max15>(b) >= popcount<Max15>(pawn_attack_span(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)
+ e->passedPawns[Us] |= s;
+
+ // Score this pawn
+ if (isolated)
+ value -= Isolated[opposed][f];
+
+ if (doubled)
+ value -= Doubled[f];
+
+ if (backward)
+ value -= Backward[opposed][f];
+
+ if (chain)
+ value += ChainMember[f][relative_rank(Us, s)];
+
+ if (candidate)
+ {
+ value += CandidatePassed[relative_rank(Us, s)];
+
+ if (!doubled)
+ e->candidatePawns[Us] |= s;
+ }
+ }
+
+ return value;
+ }