+ // Threshold for lazy and space evaluation
+ const Value LazyThreshold = Value(1500);
+ const Value SpaceThreshold = Value(12222);
+
+ // KingAttackWeights[PieceType] contains king attack weights by piece type
+ const int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 78, 56, 45, 11 };
+
+ // Penalties for enemy's safe checks
+ const int QueenSafeCheck = 780;
+ const int RookSafeCheck = 880;
+ const int BishopSafeCheck = 435;
+ const int KnightSafeCheck = 790;
+
+#define S(mg, eg) make_score(mg, eg)
+
+ // MobilityBonus[PieceType-2][attacked] contains bonuses for middle and end game,
+ // indexed by piece type and number of attacked squares in the mobility area.
+ const Score MobilityBonus[][32] = {
+ { S(-75,-76), S(-57,-54), S( -9,-28), S( -2,-10), S( 6, 5), S( 14, 12), // Knights
+ S( 22, 26), S( 29, 29), S( 36, 29) },
+ { S(-48,-59), S(-20,-23), S( 16, -3), S( 26, 13), S( 38, 24), S( 51, 42), // Bishops
+ S( 55, 54), S( 63, 57), S( 63, 65), S( 68, 73), S( 81, 78), S( 81, 86),
+ S( 91, 88), S( 98, 97) },
+ { S(-58,-76), S(-27,-18), S(-15, 28), S(-10, 55), S( -5, 69), S( -2, 82), // Rooks
+ S( 9,112), S( 16,118), S( 30,132), S( 29,142), S( 32,155), S( 38,165),
+ S( 46,166), S( 48,169), S( 58,171) },
+ { S(-39,-36), S(-21,-15), S( 3, 8), S( 3, 18), S( 14, 34), S( 22, 54), // Queens
+ S( 28, 61), S( 41, 73), S( 43, 79), S( 48, 92), S( 56, 94), S( 60,104),
+ S( 60,113), S( 66,120), S( 67,123), S( 70,126), S( 71,133), S( 73,136),
+ S( 79,140), S( 88,143), S( 88,148), S( 99,166), S(102,170), S(102,175),
+ S(106,184), S(109,191), S(113,206), S(116,212) }
+ };
+
+ // Outpost[knight/bishop][supported by pawn] contains bonuses for minor
+ // pieces if they occupy or can reach an outpost square, bigger if that
+ // square is supported by a pawn.
+ const Score Outpost[][2] = {
+ { S(22, 6), S(36,12) }, // Knight
+ { S( 9, 2), S(15, 5) } // Bishop
+ };
+
+ // RookOnFile[semiopen/open] contains bonuses for each rook when there is
+ // no (friendly) pawn on the rook file.
+ const Score RookOnFile[] = { S(20, 7), S(45, 20) };
+
+ // ThreatByMinor/ByRook[attacked PieceType] contains bonuses according to
+ // which piece type attacks which one. Attacks on lesser pieces which are
+ // pawn-defended are not considered.
+ const Score ThreatByMinor[PIECE_TYPE_NB] = {
+ S(0, 0), S(0, 31), S(39, 42), S(57, 44), S(68, 112), S(47, 120)
+ };
+
+ const Score ThreatByRook[PIECE_TYPE_NB] = {
+ S(0, 0), S(0, 24), S(38, 71), S(38, 61), S(0, 38), S(36, 38)
+ };
+
+ // ThreatByKing[on one/on many] contains bonuses for king attacks on
+ // pawns or pieces which are not pawn-defended.
+ const Score ThreatByKing[] = { S(3, 65), S(9, 145) };
+
+ // PassedRank[Rank] contains a bonus according to the rank of a passed pawn
+ const Score PassedRank[RANK_NB] = {
+ S(0, 0), S(5, 7), S(5, 13), S(32, 42), S(70, 70), S(172, 170), S(217, 269)
+ };
+
+ // PassedFile[File] contains a bonus according to the file of a passed pawn
+ const Score PassedFile[FILE_NB] = {
+ S( 9, 10), S(2, 10), S(1, -8), S(-20,-12),
+ S(-20,-12), S(1, -8), S(2, 10), S( 9, 10)
+ };
+
+ // PassedDanger[Rank] contains a term to weight the passed score
+ const int PassedDanger[RANK_NB] = { 0, 0, 0, 2, 7, 12, 19 };
+
+ // KingProtector[PieceType-2] contains a penalty according to distance from king
+ const Score KingProtector[] = { S(3, 5), S(4, 3), S(3, 0), S(1, -1) };
+
+ // Assorted bonuses and penalties
+ const Score BishopPawns = S( 8, 12);
+ const Score CloseEnemies = S( 7, 0);
+ const Score Hanging = S( 52, 30);
+ const Score HinderPassedPawn = S( 8, 1);
+ const Score LongRangedBishop = S( 22, 0);
+ const Score MinorBehindPawn = S( 16, 0);
+ const Score PawnlessFlank = S( 20, 80);
+ const Score RookOnPawn = S( 8, 24);
+ const Score ThreatByPawnPush = S( 47, 26);
+ const Score ThreatByRank = S( 16, 3);
+ const Score ThreatBySafePawn = S(175,168);
+ const Score ThreatOnQueen = S( 42, 21);
+ const Score TrappedBishopA1H1 = S( 50, 50);
+ const Score TrappedRook = S( 92, 0);
+ const Score WeakQueen = S( 50, 10);
+ const Score WeakUnopposedPawn = S( 5, 25);
+
+#undef S
+
+ // Evaluation class computes and stores attacks tables and other working data
+ template<Tracing T>
+ class Evaluation {
+
+ public:
+ Evaluation() = delete;
+ explicit Evaluation(const Position& p) : pos(p) {}
+ Evaluation& operator=(const Evaluation&) = delete;
+ Value value();
+
+ private:
+ template<Color Us> void initialize();
+ template<Color Us, PieceType Pt> Score pieces();
+ template<Color Us> Score king() const;
+ template<Color Us> Score threats() const;
+ template<Color Us> Score passed() const;
+ template<Color Us> Score space() const;
+ ScaleFactor scale_factor(Value eg) const;
+ Score initiative(Value eg) const;
+
+ const Position& pos;
+ Material::Entry* me;
+ Pawns::Entry* pe;
+ Bitboard mobilityArea[COLOR_NB];
+ Score mobility[COLOR_NB] = { SCORE_ZERO, SCORE_ZERO };