]> git.sesse.net Git - stockfish/blobdiff - src/pawns.cpp
Restrict mobility of pinned pieces
[stockfish] / src / pawns.cpp
index 772410a04b9e7c4c63a79e6c33f39bdf7566298e..91da8a7b0858cf748e7ba57231aabfa93b6f238a 100644 (file)
@@ -51,11 +51,8 @@ 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) }};
 
-  // Pawn chain membership bonus by file
-  const Score ChainMember[FILE_NB] = {
-    S(11,-1), S(13,-1), S(13,-1), S(14,-1),
-    S(14,-1), S(13,-1), S(13,-1), S(11,-1)
-  };
+  // Pawn chain membership bonus by file and rank (initialized by formula)
+  Score ChainMember[FILE_NB][RANK_NB];
 
   // Candidate passed pawn bonus by rank
   const Score CandidatePassed[RANK_NB] = {
@@ -99,7 +96,7 @@ namespace {
     Bitboard ourPawns = pos.pieces(Us, PAWN);
     Bitboard theirPawns = pos.pieces(Them, PAWN);
 
-    e->passedPawns[Us] = 0;
+    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);
@@ -140,16 +137,12 @@ namespace {
             // 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)))
-                b = shift_bb<Up>(b);
+            // 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));
 
-            // The friendly pawn needs to be at least two ranks closer than the
-            // enemy pawn in order to help the potentially backward pawn advance.
+            // 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;
         }
 
@@ -180,10 +173,15 @@ namespace {
             value -= Backward[opposed][f];
 
         if (chain)
-            value += ChainMember[f];
+            value += ChainMember[f][relative_rank(Us, s)];
 
         if (candidate)
+        {
             value += CandidatePassed[relative_rank(Us, s)];
+
+            if (!doubled)
+                e->candidatePawns[Us] |= s;
+        }
     }
 
     return value;
@@ -193,6 +191,22 @@ namespace {
 
 namespace Pawns {
 
+/// init() initializes some tables by formula instead of hard-code their values
+
+void init() {
+
+  const int chainByFile[8] = { 1, 3, 3, 4, 4, 3, 3, 1 };
+  int bonus;
+
+  for (Rank r = RANK_1; r < RANK_8; ++r)
+      for (File f = FILE_A; f <= FILE_H; ++f)
+      {
+          bonus = r * (r-1) * (r-2) + chainByFile[f] * (r/2 + 1);
+          ChainMember[f][r] = make_score(bonus, bonus);
+      }
+}
+
+
 /// probe() takes a position object 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.
@@ -226,14 +240,14 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {
   Rank rkUs, rkThem;
   File kf = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
 
-  for (int f = kf - 1; f <= kf + 1; f++)
+  for (File f = kf - File(1); f <= kf + File(1); ++f)
   {
-      b = ourPawns & FileBB[f];
-      rkUs = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1;
+      b = ourPawns & file_bb(f);
+      rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
       safety -= ShelterWeakness[rkUs];
 
-      b  = theirPawns & FileBB[f];
-      rkThem = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1;
+      b  = theirPawns & file_bb(f);
+      rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
       safety -= StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem];
   }