- // Enemy queen safe checks
- if ((b1 | b2) & attackedBy[Them][QUEEN] & safe & ~attackedBy[Us][QUEEN])
- kingDanger += QueenSafeCheck;
-
- b1 &= attackedBy[Them][ROOK];
- b2 &= attackedBy[Them][BISHOP];
-
- // Enemy rooks checks
- if (b1 & safe)
- kingDanger += RookSafeCheck;
- else
- unsafeChecks |= b1;
-
- // Enemy bishops checks
- if (b2 & safe)
- kingDanger += BishopSafeCheck;
- else
- unsafeChecks |= b2;
-
- // 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]
- + 102 * kingAdjacentZoneAttacksCount[Them]
- + 191 * popcount(kingRing[Us] & weak)
- + 143 * popcount(pos.pinned_pieces(Us) | unsafeChecks)
- - 848 * !pos.count<QUEEN>(Them)
- - 9 * mg_value(score) / 8
- + 40;
-
- // Transform the kingDanger units into a Score, and substract 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);
- }
- }
-
- // King tropism: firstly, find squares that opponent attacks in our king flank
- File kf = file_of(ksq);
- b = attackedBy[Them][ALL_PIECES] & KingFlank[kf] & Camp;
-
- assert(((Us == WHITE ? b << 4 : b >> 4) & b) == 0);
- assert(popcount(Us == WHITE ? b << 4 : b >> 4) == popcount(b));
+ if (rookChecks)
+ kingDanger += RookSafeCheck;
+ else
+ unsafeChecks |= b1 & attackedBy[Them][ROOK];
+
+ // Enemy queen safe checks: we count them only if they are from squares from
+ // which we can't give a rook check, because rook checks are more valuable.
+ queenChecks = (b1 | b2)
+ & attackedBy[Them][QUEEN]
+ & safe
+ & ~attackedBy[Us][QUEEN]
+ & ~rookChecks;
+
+ if (queenChecks)
+ kingDanger += QueenSafeCheck;
+
+ // Enemy bishops checks: we count them only if they are from squares from
+ // which we can't give a queen check, because queen checks are more valuable.
+ bishopChecks = b2
+ & attackedBy[Them][BISHOP]
+ & safe
+ & ~queenChecks;
+
+ if (bishopChecks)
+ kingDanger += BishopSafeCheck;
+ else
+ unsafeChecks |= b2 & attackedBy[Them][BISHOP];