X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fevaluate.cpp;h=53a54006a7a6cf1afabd0d8a9989c3a7b0b5131b;hp=333d04aca277d0b456800017980887967d14e09d;hb=69dc5568b3bb9a02c71bdd5bb0e87135e892ea19;hpb=14c4a40dc7c4d6ddec6fb2447c2dac82aa590de2 diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 333d04ac..53a54006 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -258,25 +258,20 @@ namespace { attackedBy[Us][ALL_PIECES] = attackedBy[Us][KING] | attackedBy[Us][PAWN]; attackedBy2[Us] = attackedBy[Us][KING] & attackedBy[Us][PAWN]; - kingRing[Us] = kingAttackersCount[Them] = 0; + // Init our king safety tables + kingRing[Us] = attackedBy[Us][KING]; + if (relative_rank(Us, pos.square(Us)) == RANK_1) + kingRing[Us] |= shift(kingRing[Us]); - // Init our king safety tables only if we are going to use them - if (pos.non_pawn_material(Them) >= RookValueMg + KnightValueMg) - { - kingRing[Us] = attackedBy[Us][KING]; - if (relative_rank(Us, pos.square(Us)) == RANK_1) - kingRing[Us] |= shift(kingRing[Us]); - - if (file_of(pos.square(Us)) == FILE_H) - kingRing[Us] |= shift(kingRing[Us]); + if (file_of(pos.square(Us)) == FILE_H) + kingRing[Us] |= shift(kingRing[Us]); - else if (file_of(pos.square(Us)) == FILE_A) - kingRing[Us] |= shift(kingRing[Us]); + else if (file_of(pos.square(Us)) == FILE_A) + kingRing[Us] |= shift(kingRing[Us]); - kingAttackersCount[Them] = popcount(kingRing[Us] & pe->pawn_attacks(Them)); - kingRing[Us] &= ~double_pawn_attacks_bb(pos.pieces(Us, PAWN)); - kingAttacksCount[Them] = kingAttackersWeight[Them] = 0; - } + kingAttackersCount[Them] = popcount(kingRing[Us] & pe->pawn_attacks(Them)); + kingRing[Us] &= ~double_pawn_attacks_bb(pos.pieces(Us, PAWN)); + kingAttacksCount[Them] = kingAttackersWeight[Them] = 0; } @@ -424,67 +419,64 @@ namespace { int tropism = popcount(b1) + popcount(b2); // Main king safety evaluation - if (kingAttackersCount[Them] > 1 - pos.count(Them)) - { - int kingDanger = 0; - unsafeChecks = 0; + int kingDanger = 0; + unsafeChecks = 0; - // Attacked squares defended at most once by our queen or king - weak = attackedBy[Them][ALL_PIECES] - & ~attackedBy2[Us] - & (~attackedBy[Us][ALL_PIECES] | attackedBy[Us][KING] | attackedBy[Us][QUEEN]); + // Attacked squares defended at most once by our queen or king + weak = attackedBy[Them][ALL_PIECES] + & ~attackedBy2[Us] + & (~attackedBy[Us][ALL_PIECES] | attackedBy[Us][KING] | attackedBy[Us][QUEEN]); - // Analyse the safe enemy's checks which are possible on next move - safe = ~pos.pieces(Them); - safe &= ~attackedBy[Us][ALL_PIECES] | (weak & attackedBy2[Them]); + // Analyse the safe enemy's checks which are possible on next move + safe = ~pos.pieces(Them); + safe &= ~attackedBy[Us][ALL_PIECES] | (weak & attackedBy2[Them]); - b1 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); - b2 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); + b1 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); + b2 = attacks_bb(ksq, pos.pieces() ^ pos.pieces(Us, QUEEN)); - // Enemy queen safe checks - if ((b1 | b2) & attackedBy[Them][QUEEN] & safe & ~attackedBy[Us][QUEEN]) - kingDanger += QueenSafeCheck; + // Enemy queen safe checks + if ((b1 | b2) & attackedBy[Them][QUEEN] & safe & ~attackedBy[Us][QUEEN]) + kingDanger += QueenSafeCheck; - b1 &= attackedBy[Them][ROOK]; - b2 &= attackedBy[Them][BISHOP]; + b1 &= attackedBy[Them][ROOK]; + b2 &= attackedBy[Them][BISHOP]; - // Enemy rooks checks - if (b1 & safe) - kingDanger += RookSafeCheck; - else - unsafeChecks |= b1; + // Enemy rooks checks + if (b1 & safe) + kingDanger += RookSafeCheck; + else + unsafeChecks |= b1; - // Enemy bishops checks - if (b2 & safe) - kingDanger += BishopSafeCheck; - else - unsafeChecks |= b2; + // Enemy bishops checks + if (b2 & safe) + kingDanger += BishopSafeCheck; + else + unsafeChecks |= b2; - // Enemy knights checks - b = pos.attacks_from(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) - + tropism * tropism / 4 - - 873 * !pos.count(Them) - - 6 * mg_value(score) / 8 - + mg_value(mobility[Them] - mobility[Us]) - - 30; - - // Transform the kingDanger units into a Score, and subtract it from the evaluation - if (kingDanger > 0) - score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16); - } + // Enemy knights checks + b = pos.attacks_from(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) + + tropism * tropism / 4 + - 873 * !pos.count(Them) + - 6 * mg_value(score) / 8 + + mg_value(mobility[Them] - mobility[Us]) + - 30; + + // Transform the kingDanger units into a Score, and subtract it from the evaluation + if (kingDanger > 0) + score -= make_score(kingDanger * kingDanger / 4096, kingDanger / 16); // Penalty when our king is on a pawnless flank if (!(pos.pieces(PAWN) & kingFlank))