- // Enemy knights checks
- b = pos.attacks_from<KNIGHT>(ksq) & attackedBy[Them][KNIGHT];
- if (b & safe)
- kingDanger += KnightSafeCheck;
- else
- unsafeChecks |= b;
-
- // Unsafe or occupied checking squares will also be considered, as long as
- // the square is in the attacker's mobility area.
- unsafeChecks &= mobilityArea[Them];
-
- kingDanger += kingAttackersCount[Them] * kingAttackersWeight[Them]
- + 69 * kingAttacksCount[Them]
- + 185 * popcount(kingRing[Us] & weak)
- + 150 * popcount(pos.blockers_for_king(Us) | unsafeChecks)
- + 4 * tropism
- - 873 * !pos.count<QUEEN>(Them)
- - 6 * mg_value(score) / 8
- - 30;
-
- // Transform the kingDanger units into a Score, and subtract it from the evaluation
- if (kingDanger > 0)
- {
- int mobilityDanger = mg_value(mobility[Them] - mobility[Us]);
- kingDanger = std::max(0, kingDanger + mobilityDanger);
- score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16);
- }
- }
+ if (knightChecks & safe)
+ kingDanger += KnightSafeCheck;
+ else
+ unsafeChecks |= knightChecks;
+
+ // Find the squares that opponent attacks in our king flank, the squares
+ // which they attack twice in that flank, and the squares that we defend.
+ b1 = attackedBy[Them][ALL_PIECES] & KingFlank[file_of(ksq)] & Camp;
+ b2 = b1 & attackedBy2[Them];
+ b3 = attackedBy[Us][ALL_PIECES] & KingFlank[file_of(ksq)] & Camp;
+
+ int kingFlankAttack = popcount(b1) + popcount(b2);
+ int kingFlankDefense = popcount(b3);
+
+ kingDanger += kingAttackersCount[Them] * kingAttackersWeight[Them]
+ + 185 * popcount(kingRing[Us] & weak)
+ + 148 * popcount(unsafeChecks)
+ + 98 * popcount(pos.blockers_for_king(Us))
+ + 69 * kingAttacksCount[Them]
+ + 4 * (kingFlankAttack - kingFlankDefense)
+ + 3 * kingFlankAttack * kingFlankAttack / 8
+ + mg_value(mobility[Them] - mobility[Us])
+ - 873 * !pos.count<QUEEN>(Them)
+ - 100 * bool(attackedBy[Us][KNIGHT] & attackedBy[Us][KING])
+ - 35 * bool(attackedBy[Us][BISHOP] & attackedBy[Us][KING])
+ - 6 * mg_value(score) / 8
+ - 7;
+
+ // Transform the kingDanger units into a Score, and subtract it from the evaluation
+ if (kingDanger > 100)
+ score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16);