]> git.sesse.net Git - stockfish/blobdiff - src/pawns.cpp
Halve StormDanger bonus for blocked pawn on A/H file
[stockfish] / src / pawns.cpp
index 4693e1584f5b2161573fd42ff19643791feeef20..d6ea1479de80c407449863709208db76fa1b13c3 100644 (file)
@@ -49,17 +49,14 @@ namespace {
   { S(20, 28), S(29, 31), S(33, 31), S(33, 31),
     S(33, 31), S(33, 31), S(29, 31), S(20, 28) } };
 
-  // Connected bonus by rank
-  const int Connected[RANK_NB] = {0, 6, 15, 10, 57, 75, 135, 258};
+  // Connected pawn bonus by opposed, phalanx flags and rank
+  Score Connected[2][2][RANK_NB];
 
   // Levers bonus by rank
   const Score Lever[RANK_NB] = {
     S( 0, 0), S( 0, 0), S(0, 0), S(0, 0),
     S(20,20), S(40,40), S(0, 0), S(0, 0) };
 
-  // Bonus for file distance of the two outermost pawns
-  const Score PawnsFileSpan = S(0, 15);
-
   // Unsupported pawn penalty
   const Score UnsupportedPawnPenalty = S(20, 10);
 
@@ -68,11 +65,14 @@ namespace {
   { 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[][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(160), V(25), V(13) } };
+  // [edge files][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(160), V(25), V(13) } },
+  { { 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( 80), V(13), V( 7) } } };
 
   // 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.
@@ -89,9 +89,9 @@ namespace {
     const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
     const Square Left  = (Us == WHITE ? DELTA_NW : DELTA_SE);
 
-    Bitboard b, p, doubled;
+    Bitboard b, p, doubled, connected;
     Square s;
-    bool passed, isolated, opposed, connected, backward, unsupported, lever;
+    bool passed, isolated, opposed, phalanx, backward, unsupported, lever;
     Score value = SCORE_ZERO;
     const Square* pl = pos.list<PAWN>(Us);
     const Bitboard* pawnAttacksBB = StepAttacksBB[make_piece(Us, PAWN)];
@@ -111,7 +111,6 @@ namespace {
     {
         assert(pos.piece_on(s) == make_piece(Us, PAWN));
 
-        Rank r = rank_of(s), rr = relative_rank(Us, s);
         File f = file_of(s);
 
         // This file cannot be semi-open
@@ -120,12 +119,10 @@ namespace {
         // Previous rank
         p = rank_bb(s - pawn_push(Us));
 
-        // Our rank plus previous one
-        b = rank_bb(s) | p;
-
         // Flag the pawn as passed, isolated, doubled,
         // unsupported or connected (but not the backward one).
-        connected   =   ourPawns   & adjacent_files_bb(f) & b;
+        connected   =   ourPawns   & adjacent_files_bb(f) & (rank_bb(s) | p);
+        phalanx     =   connected  & rank_bb(s);
         unsupported = !(ourPawns   & adjacent_files_bb(f) & p);
         isolated    = !(ourPawns   & adjacent_files_bb(f));
         doubled     =   ourPawns   & forward_bb(Us, s);
@@ -171,29 +168,21 @@ namespace {
             value -= UnsupportedPawnPenalty;
 
         if (doubled)
-            value -= Doubled[f] / rank_distance(s, lsb(doubled));
+            value -= Doubled[f] / distance<Rank>(s, frontmost_sq(Us, doubled));
 
         if (backward)
             value -= Backward[opposed][f];
 
-        if (connected) {
-            int bonus = Connected[rr];
-            if (ourPawns & adjacent_files_bb(f) & rank_bb(r))
-                bonus += (Connected[rr+1] - Connected[rr]) / 2;
-            value += make_score(bonus / 2, bonus >> opposed);
-        }
+        if (connected)
+            value += Connected[opposed][phalanx][relative_rank(Us, s)];
 
         if (lever)
-            value += Lever[rr];
+            value += Lever[relative_rank(Us, s)];
     }
 
     b = e->semiopenFiles[Us] ^ 0xFF;
     e->pawnSpan[Us] = b ? int(msb(b) - lsb(b)) : 0;
 
-    // In endgame it's better to have pawns on both wings. So give a bonus according
-    // to file distance between left and right outermost pawns.
-    value += PawnsFileSpan * e->pawnSpan[Us];
-
     return value;
   }
 
@@ -201,6 +190,24 @@ namespace {
 
 namespace Pawns {
 
+/// init() initializes some tables used by evaluation. Instead of hard-coded
+/// tables, when makes sense, we prefer to calculate them with a formula to
+/// reduce independent parameters and to allow easier tuning and better insight.
+
+void init()
+{
+  static const int Seed[RANK_NB] = { 0, 6, 15, 10, 57, 75, 135, 258 };
+
+  for (int opposed = 0; opposed <= 1; ++opposed)
+      for (int phalanx = 0; phalanx <= 1; ++phalanx)
+          for (Rank r = RANK_2; r < RANK_8; ++r)
+          {
+              int bonus = Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0);
+              Connected[opposed][phalanx][r] = make_score(bonus / 2, bonus >> opposed);
+          }
+}
+
+
 /// probe() takes a position as input, computes a Entry object, and returns a
 /// pointer to it. The result is also stored in a hash table, so we don't have
 /// to recompute everything when the same pawn structure occurs again.
@@ -248,7 +255,8 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {
           safety += 200;
       else
           safety -=  ShelterWeakness[rkUs]
-                   + StormDanger[rkUs   == RANK_1   ? 0 :
+                   + StormDanger[f == FILE_A || f == FILE_H]
+                                [rkUs   == RANK_1   ? 0 :
                                  rkThem != rkUs + 1 ? 1 : 2][rkThem];
   }
 
@@ -264,14 +272,14 @@ Score Entry::do_king_safety(const Position& pos, Square ksq) {
 
   kingSquares[Us] = ksq;
   castlingRights[Us] = pos.can_castle(Us);
-  minKPdistance[Us] = 0;
+  minKingPawnDistance[Us] = 0;
 
   Bitboard pawns = pos.pieces(Us, PAWN);
   if (pawns)
-      while (!(DistanceRingsBB[ksq][minKPdistance[Us]++] & pawns)) {}
+      while (!(DistanceRingsBB[ksq][minKingPawnDistance[Us]++] & pawns)) {}
 
   if (relative_rank(Us, ksq) > RANK_4)
-      return make_score(0, -16 * minKPdistance[Us]);
+      return make_score(0, -16 * minKingPawnDistance[Us]);
 
   Value bonus = shelter_storm<Us>(pos, ksq);
 
@@ -282,7 +290,7 @@ Score Entry::do_king_safety(const Position& pos, Square ksq) {
   if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
       bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
 
-  return make_score(bonus, -16 * minKPdistance[Us]);
+  return make_score(bonus, -16 * minKingPawnDistance[Us]);
 }
 
 // Explicit template instantiation