- {}, {},
- { S(-75,-76), S(-56,-54), S(- 9,-26), S( -2,-10), S( 6, 5), S( 15, 11), // Knights
- S( 22, 26), S( 30, 28), S( 36, 29) },
- { S(-48,-58), S(-21,-19), S( 16, -2), S( 26, 12), S( 37, 22), S( 51, 42), // Bishops
- S( 54, 54), S( 63, 58), S( 65, 63), S( 71, 70), S( 79, 74), S( 81, 86),
- S( 92, 90), S( 97, 94) },
- { S(-56,-78), S(-25,-18), S(-11, 26), S( -5, 55), S( -4, 70), S( -1, 81), // Rooks
- S( 8,109), S( 14,120), S( 21,128), S( 23,143), S( 31,154), S( 32,160),
- S( 43,165), S( 49,168), S( 59,169) },
- { S(-40,-35), S(-25,-12), S( 2, 7), S( 4, 19), S( 14, 37), S( 24, 55), // Queens
- S( 25, 62), S( 40, 76), S( 43, 79), S( 47, 87), S( 54, 94), S( 56,102),
- S( 60,111), S( 70,116), S( 72,118), S( 73,122), S( 75,128), S( 77,130),
- S( 85,133), S( 94,136), S( 99,140), S(108,157), S(112,158), S(113,161),
- S(118,174), S(119,177), S(123,191), S(128,199) }
+ { 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) }
- const Score RookOnFile[2] = { S(19, 10), S(43, 21) };
-
- // ThreatBySafePawn[PieceType] contains bonuses according to which piece
- // type is attacked by a pawn which is protected or is not attacked.
- const Score ThreatBySafePawn[PIECE_TYPE_NB] = {
- S(0, 0), S(0, 0), S(176, 139), S(131, 127), S(217, 218), S(203, 215) };
-
- // Threat[by minor/by rook][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 Threat[][PIECE_TYPE_NB] = {
- { S(0, 0), S(0, 33), S(45, 43), S(46, 47), S(72,107), S(48,118) }, // by Minor
- { S(0, 0), S(0, 25), S(40, 62), S(40, 59), S( 0, 34), S(35, 48) } // by Rook
+ 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, 33), S(45, 43), S(46, 47), S(72, 107), S(48, 118)
+ };
+
+ const Score ThreatByRook[PIECE_TYPE_NB] = {
+ S(0, 0), S(0, 25), S(40, 62), S(40, 59), S(0, 34), S(35, 48)
- const Score MinorBehindPawn = S(16, 0);
- const Score BishopPawns = S( 8, 12);
- const Score RookOnPawn = S( 7, 27);
- const Score TrappedRook = S(92, 0);
- const Score Checked = S(20, 20);
- const Score ThreatByHangingPawn = S(70, 63);
- const Score Hanging = S(48, 28);
- const Score ThreatByPawnPush = S(31, 19);
- const Score Unstoppable = S( 0, 20);
+ const Score MinorBehindPawn = S( 16, 0);
+ const Score BishopPawns = S( 8, 12);
+ const Score RookOnPawn = S( 8, 24);
+ const Score TrappedRook = S( 92, 0);
+ const Score WeakQueen = S( 50, 10);
+ const Score OtherCheck = S( 10, 10);
+ const Score CloseEnemies = S( 7, 0);
+ const Score PawnlessFlank = S( 20, 80);
+ const Score ThreatByHangingPawn = S( 71, 61);
+ const Score ThreatBySafePawn = S(182,175);
+ const Score ThreatByRank = S( 16, 3);
+ const Score Hanging = S( 48, 27);
+ const Score ThreatByPawnPush = S( 38, 22);
+ const Score HinderPassedPawn = S( 7, 0);
- // Find the attacked squares around the king which have no defenders
- // apart from the king itself.
- undefended = ei.attackedBy[Them][ALL_PIECES]
- & ei.attackedBy[Us][KING]
- & ~( ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
- | ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
- | ei.attackedBy[Us][QUEEN]);
-
- // Initialize the 'attackUnits' variable, which is used later on as an
- // index into the KingDanger[] array. The initial value is based on the
- // number and types of the enemy's attacking pieces, the number of
- // attacked and undefended squares around our king and the quality of
- // the pawn shelter (current 'score' value).
- attackUnits = std::min(72, ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them])
- + 9 * ei.kingAdjacentZoneAttacksCount[Them]
- + 27 * popcount<Max15>(undefended)
- + 11 * !!ei.pinnedPieces[Us]
- - 64 * !pos.count<QUEEN>(Them)
- - mg_value(score) / 8;
-
- // Analyse the enemy's safe queen contact checks. Firstly, find the
- // undefended squares around the king reachable by the enemy queen...
- b = undefended & ei.attackedBy[Them][QUEEN] & ~pos.pieces(Them);
- if (b)
- {
- // ...and then remove squares not supported by another enemy piece
- b &= ei.attackedBy[Them][PAWN] | ei.attackedBy[Them][KNIGHT]
- | ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]
- | ei.attackedBy[Them][KING];
-
- if (b)
- attackUnits += QueenContactCheck * popcount<Max15>(b);
- }
+ // Find the attacked squares which are defended only by our king...
+ kingOnlyDefended = attackedBy[Them][ALL_PIECES]
+ & attackedBy[Us][KING]
+ & ~attackedBy2[Us];
- b = (b1 | b2) & ei.attackedBy[Them][QUEEN];
- if (b)
- {
- attackUnits += QueenCheck * popcount<Max15>(b);
- score -= Checked;
- }
+ if ((b1 | b2) & attackedBy[Them][QUEEN] & safe)
+ kingDanger += QueenCheck;
+
+ // For minors and rooks, also consider the square safe if attacked twice,
+ // and only defended by our queen.
+ safe |= attackedBy2[Them]
+ & ~(attackedBy2[Us] | pos.pieces(Them))
+ & attackedBy[Us][QUEEN];
+
+ // Some other potential checks are also analysed, even from squares
+ // currently occupied by the opponent own pieces, as long as the square
+ // is not attacked by our pawns, and is not occupied by a blocked pawn.
+ other = ~( attackedBy[Us][PAWN]
+ | (pos.pieces(Them, PAWN) & shift<Up>(pos.pieces(PAWN))));
+
+ // Enemy rooks safe and other checks
+ if (b1 & attackedBy[Them][ROOK] & safe)
+ kingDanger += RookCheck;
+
+ else if (b1 & attackedBy[Them][ROOK] & other)
+ score -= OtherCheck;
+
+ // Enemy bishops safe and other checks
+ if (b2 & attackedBy[Them][BISHOP] & safe)
+ kingDanger += BishopCheck;
+
+ else if (b2 & attackedBy[Them][BISHOP] & other)
+ score -= OtherCheck;
+
+ // Enemy knights safe and other checks
+ b = pos.attacks_from<KNIGHT>(ksq) & attackedBy[Them][KNIGHT];
+ if (b & safe)
+ kingDanger += KnightCheck;
+
+ else if (b & other)
+ score -= OtherCheck;
+
+ // Transform the kingDanger units into a Score, and substract it from the evaluation
+ if (kingDanger > 0)
+ score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16);
+ }
- // Evaluate space for both sides, only during opening
- if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >= 12222)
- score += evaluate_space<WHITE>(pos, ei)
- - evaluate_space<BLACK>(pos, ei);
-
- // Evaluate position potential for the winning side
- score += evaluate_initiative(pos, ei.pi->pawn_asymmetry(), eg_value(score));
-
- // Evaluate scale factor for the winning side
- ScaleFactor sf = evaluate_scale_factor(pos, ei, eg_value(score));
-
- // Interpolate between a middlegame and a (scaled by 'sf') endgame score
- Value v = mg_value(score) * int(ei.me->game_phase())
- + eg_value(score) * int(PHASE_MIDGAME - ei.me->game_phase()) * sf / SCALE_FACTOR_NORMAL;
-
- v /= int(PHASE_MIDGAME);
-
- // In case of tracing add all remaining individual evaluation terms
- if (DoTrace)
- {
- Trace::add(MATERIAL, pos.psq_score());
- Trace::add(IMBALANCE, ei.me->imbalance());
- Trace::add(PAWN, ei.pi->pawns_score());
- Trace::add(MOBILITY, mobility[WHITE], mobility[BLACK]);
- Trace::add(SPACE, evaluate_space<WHITE>(pos, ei)
- , evaluate_space<BLACK>(pos, ei));
- Trace::add(TOTAL, score);
+ return (pos.side_to_move() == WHITE ? v : -v) + Eval::Tempo; // Side to move point of view