]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Fix a small bug in king safety
[stockfish] / src / evaluate.cpp
index 9368aa2cb38e25f1f5c952037400568a3e958646..c29fd5e651a46bd5a46af414b256f2923f6a029d 100644 (file)
@@ -58,15 +58,17 @@ namespace {
   // weights read from UCI parameters.  The purpose is to be able to change
   // the evaluation weights while keeping the default values of the UCI
   // parameters at 100, which looks prettier.
-  const int WeightMobilityMidgameInternal      = 0x100;
-  const int WeightMobilityEndgameInternal      = 0x100;
-  const int WeightPawnStructureMidgameInternal = 0x100;
-  const int WeightPawnStructureEndgameInternal = 0x100;
-  const int WeightPassedPawnsMidgameInternal   = 0x100;
-  const int WeightPassedPawnsEndgameInternal   = 0x100;
-  const int WeightKingSafetyInternal           = 0x110;
-  const int WeightKingOppSafetyInternal        = 0x110;
-  const int WeightSpaceInternal                = 0x30;
+  //
+  // Values modified by Joona Kiiski
+  const int WeightMobilityMidgameInternal      = 0x0FA;
+  const int WeightMobilityEndgameInternal      = 0x10A;
+  const int WeightPawnStructureMidgameInternal = 0x0EC;
+  const int WeightPawnStructureEndgameInternal = 0x0CD;
+  const int WeightPassedPawnsMidgameInternal   = 0x108;
+  const int WeightPassedPawnsEndgameInternal   = 0x109;
+  const int WeightKingSafetyInternal           = 0x0F7;
+  const int WeightKingOppSafetyInternal        = 0x101;
+  const int WeightSpaceInternal                = 0x02F;
 
   // Visually better to define tables constants
   typedef Value V;
@@ -167,18 +169,19 @@ namespace {
     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0)  // 8
   };
 
-  // Bonus for unstoppable passed pawns:
+  // Bonus for unstoppable passed pawns
   const Value UnstoppablePawnValue = Value(0x500);
 
-  // Rooks and queens on the 7th rank:
-  const Value MidgameRookOn7thBonus  = Value(50);
-  const Value EndgameRookOn7thBonus  = Value(100);
-  const Value MidgameQueenOn7thBonus = Value(25);
-  const Value EndgameQueenOn7thBonus = Value(50);
+  // Rooks and queens on the 7th rank
+  const Value MidgameRookOn7thBonus  = Value(47);
+  const Value EndgameRookOn7thBonus  = Value(98);
+  const Value MidgameQueenOn7thBonus = Value(27);
+  const Value EndgameQueenOn7thBonus = Value(54);
 
-  // Rooks on open files:
-  const Value RookOpenFileBonus     = Value(40);
-  const Value RookHalfOpenFileBonus = Value(20);
+
+  // Rooks on open files
+  const Value RookOpenFileBonus = Value(43);
+  const Value RookHalfOpenFileBonus = Value(19);
 
   // Penalty for rooks trapped inside a friendly king which has lost the
   // right to castle:
@@ -487,7 +490,10 @@ void init_eval(int threads) {
   }
 
   for (Bitboard b = 0ULL; b < 256ULL; b++)
-      BitCount8Bit[b] = count_1s(b);
+  {
+      assert(count_1s(b) == int(uint8_t(count_1s(b))));
+      BitCount8Bit[b] = (uint8_t)count_1s(b);
+  }
 }
 
 
@@ -593,10 +599,14 @@ namespace {
   void evaluate_pieces(const Position& pos, Color us, EvalInfo& ei) {
 
     Bitboard b;
+    Square s, ksq;
+    Color them;
+    int mob;
+    File f;
 
-    for (int i = 0; i < pos.piece_count(us, Piece); i++)
+    for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
     {
-        Square s = pos.piece_list(us, Piece, i);
+        s = pos.piece_list(us, Piece, i);
 
         if (Piece == KNIGHT || Piece == QUEEN)
             b = pos.piece_attacks<Piece>(s);
@@ -606,7 +616,7 @@ namespace {
             b = rook_attacks_bb(s, pos.occupied_squares() & ~pos.rooks_and_queens(us));
 
         // Attacks, mobility and outposts
-        int mob = evaluate_common<Piece>(pos, b, us, ei, s);
+        mob = evaluate_common<Piece>(pos, b, us, ei, s);
 
         // Special patterns: trapped bishops on a7/h7/a2/h2
         // and trapped bishops on a1/h1/a8/h8 in Chess960.
@@ -619,63 +629,63 @@ namespace {
                 evaluate_trapped_bishop_a1h1(pos, s, us, ei);
         }
 
-        if (Piece != ROOK && Piece != QUEEN)
-            continue;
-
-        // Queen or rook on 7th rank
-        Color them = opposite_color(us);
-
-        if (   relative_rank(us, s) == RANK_7
-            && relative_rank(us, pos.king_square(them)) == RANK_8)
+        if (Piece == ROOK || Piece == QUEEN)
         {
-            ei.mgValue += Sign[us] * (Piece == ROOK ? MidgameRookOn7thBonus : MidgameQueenOn7thBonus);
-            ei.egValue += Sign[us] * (Piece == ROOK ? EndgameRookOn7thBonus : EndgameQueenOn7thBonus);
+            // Queen or rook on 7th rank
+            them = opposite_color(us);
+
+            if (   relative_rank(us, s) == RANK_7
+                && relative_rank(us, pos.king_square(them)) == RANK_8)
+            {
+                ei.mgValue += Sign[us] * (Piece == ROOK ? MidgameRookOn7thBonus : MidgameQueenOn7thBonus);
+                ei.egValue += Sign[us] * (Piece == ROOK ? EndgameRookOn7thBonus : EndgameQueenOn7thBonus);
+            }
         }
 
         // Special extra evaluation for rooks
-        if (Piece != ROOK)
-            continue;
-
-        // Open and half-open files
-        File f = square_file(s);
-        if (ei.pi->file_is_half_open(us, f))
+        if (Piece == ROOK)
         {
-            if (ei.pi->file_is_half_open(them, f))
-            {
-                ei.mgValue += Sign[us] * RookOpenFileBonus;
-                ei.egValue += Sign[us] * RookOpenFileBonus;
-            }
-            else
+            // Open and half-open files
+            f = square_file(s);
+            if (ei.pi->file_is_half_open(us, f))
             {
-                ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
-                ei.egValue += Sign[us] * RookHalfOpenFileBonus;
+                if (ei.pi->file_is_half_open(them, f))
+                {
+                    ei.mgValue += Sign[us] * RookOpenFileBonus;
+                    ei.egValue += Sign[us] * RookOpenFileBonus;
+                }
+                else
+                {
+                    ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
+                    ei.egValue += Sign[us] * RookHalfOpenFileBonus;
+                }
             }
-        }
 
-        // Penalize rooks which are trapped inside a king. Penalize more if
-        // king has lost right to castle.
-        if (mob > 6 || ei.pi->file_is_half_open(us, f))
-            continue;
+            // Penalize rooks which are trapped inside a king. Penalize more if
+            // king has lost right to castle.
+            if (mob > 6 || ei.pi->file_is_half_open(us, f))
+                continue;
 
-        Square ksq = pos.king_square(us);
+            ksq = pos.king_square(us);
 
-        if (    square_file(ksq) >= FILE_E
-            &&  square_file(s) > square_file(ksq)
-            && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
-        {
-            // Is there a half-open file between the king and the edge of the board?
-            if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
-                ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
-                                                : Sign[us] *  (TrappedRookPenalty - mob * 16);
-        }
-        else if (    square_file(ksq) <= FILE_D
-                 &&  square_file(s) < square_file(ksq)
-                 && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
-        {
-            // Is there a half-open file between the king and the edge of the board?
-            if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
-                ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
-                                                : Sign[us] * (TrappedRookPenalty - mob * 16);
+            if (    square_file(ksq) >= FILE_E
+                &&  square_file(s) > square_file(ksq)
+                && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
+            {
+                // Is there a half-open file between the king and the edge of the board?
+                if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
+                    ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
+                                                    : Sign[us] *  (TrappedRookPenalty - mob * 16);
+            }
+            else if (    square_file(ksq) <= FILE_D
+                    &&  square_file(s) < square_file(ksq)
+                    && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
+            {
+                // Is there a half-open file between the king and the edge of the board?
+                if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
+                    ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
+                                                    : Sign[us] * (TrappedRookPenalty - mob * 16);
+            }
         }
     }
   }
@@ -772,7 +782,7 @@ namespace {
                         if (    bit_is_set(p.piece_attacks<QUEEN>(from), to)
                             && !bit_is_set(p.pinned_pieces(them), from)
                             && !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us))
-                            && !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us)))
+                            && !(bishop_attacks_bb(to, occ & ClearMaskBB[from]) & p.bishops_and_queens(us)))
 
                             ei.mateThreat[them] = make_move(from, to);
                     }