X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fevaluate.cpp;h=8651ab278db8e333c41ba613ca4d6093e7c16b0a;hp=d6af118bf36a7c6fd094a6ecf2dfd6677e59fed0;hb=805afcbf3d5db39c85b759232cfb99ab0a250311;hpb=190d2ea4bc433a0c047ede87f5d9cc241de2b1e2 diff --git a/src/evaluate.cpp b/src/evaluate.cpp index d6af118b..8651ab27 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -77,6 +77,11 @@ namespace { // attacked by a given color and piece type (can be also ALL_PIECES). Bitboard attackedBy[COLOR_NB][PIECE_TYPE_NB]; + // attackedBy2[color] are the squares attacked by 2 pieces of a given color, + // possibly via x-ray or by one pawn and one piece. Diagonal x-ray through + // pawn or squares attacked by 2 pawns are not explicitly added. + Bitboard attackedBy2[COLOR_NB]; + // kingRing[color] is the zone around the king which is considered // by the king safety evaluation. This consists of the squares directly // adjacent to the king, and the three (or two, for a king on an edge file) @@ -227,9 +232,10 @@ namespace { const Square Down = (Us == WHITE ? DELTA_S : DELTA_N); ei.pinnedPieces[Us] = pos.pinned_pieces(Us); - Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from(pos.square(Them)); + Bitboard b = ei.attackedBy[Them][KING]; ei.attackedBy[Them][ALL_PIECES] |= b; ei.attackedBy[Us][ALL_PIECES] |= ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us); + ei.attackedBy2[Us] = ei.attackedBy[Us][PAWN] & ei.attackedBy[Us][KING]; // Init king safety tables only if we are going to use them if (pos.non_pawn_material(Us) >= QueenValueMg) @@ -272,6 +278,7 @@ namespace { if (ei.pinnedPieces[Us] & s) b &= LineBB[pos.square(Us)][s]; + ei.attackedBy2[Us] |= ei.attackedBy[Us][ALL_PIECES] & b; ei.attackedBy[Us][ALL_PIECES] |= ei.attackedBy[Us][Pt] |= b; if (b & ei.kingRing[Them]) @@ -352,7 +359,7 @@ namespace { if (Pt == QUEEN) { // Penalty if any relative pin or discovered attack against the queen - if (pos.slider_blockers(pos.pieces(), pos.pieces(Them, ROOK, BISHOP), s)) + if (pos.slider_blockers(pos.pieces(Them, ROOK, BISHOP), s)) score -= WeakQueen; } } @@ -389,11 +396,9 @@ namespace { if (ei.kingAttackersCount[Them]) { // Find the attacked squares which are defended only by the king... - 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]); + undefended = ei.attackedBy[Them][ALL_PIECES] + & ei.attackedBy[Us][KING] + & ~ei.attackedBy2[Us]; // ... and those which are not defended at all in the larger king ring b = ei.attackedBy[Them][ALL_PIECES] & ~ei.attackedBy[Us][ALL_PIECES] @@ -414,15 +419,9 @@ namespace { // 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]; - attackUnits += QueenContactCheck * popcount(b); - } + // ...and keep squares supported by another enemy piece + attackUnits += QueenContactCheck * popcount(b & ei.attackedBy2[Them]); // Analyse the safe enemy's checks which are possible on next move... safe = ~(ei.attackedBy[Us][ALL_PIECES] | pos.pieces(Them)); @@ -439,6 +438,12 @@ namespace { if ((b1 | b2) & ei.attackedBy[Them][QUEEN] & safe) attackUnits += QueenCheck, score -= SafeCheck; + // For other pieces, also consider the square safe if attacked twice, + // and only defended by a queen. + safe |= ei.attackedBy2[Them] + & ~(ei.attackedBy2[Us] | pos.pieces(Them)) + & ei.attackedBy[Us][QUEEN]; + // Enemy rooks safe and other checks if (b1 & ei.attackedBy[Them][ROOK] & safe) attackUnits += RookCheck, score -= SafeCheck; @@ -476,6 +481,19 @@ namespace { // evaluate_threats() assigns bonuses according to the types of the attacking // and the attacked pieces. + const Bitboard WhiteCamp = Rank1BB | Rank2BB | Rank3BB | Rank4BB | Rank5BB; + const Bitboard BlackCamp = Rank8BB | Rank7BB | Rank6BB | Rank5BB | Rank4BB; + const Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB; + const Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB; + const Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB; + + const Bitboard KingFlank[COLOR_NB][FILE_NB] = { + { QueenSide & WhiteCamp, QueenSide & WhiteCamp, QueenSide & WhiteCamp, CenterFiles & WhiteCamp, + CenterFiles & WhiteCamp, KingSide & WhiteCamp, KingSide & WhiteCamp, KingSide & WhiteCamp }, + { QueenSide & BlackCamp, QueenSide & BlackCamp, QueenSide & BlackCamp, CenterFiles & BlackCamp, + CenterFiles & BlackCamp, KingSide & BlackCamp, KingSide & BlackCamp, KingSide & BlackCamp }, + }; + template Score evaluate_threats(const Position& pos, const EvalInfo& ei) { @@ -553,6 +571,18 @@ namespace { score += ThreatByPawnPush * popcount(b); + // King tropism: firstly, find squares that we attack in the enemy king flank + b = ei.attackedBy[Us][ALL_PIECES] & KingFlank[Them][file_of(pos.square(Them))]; + + // Secondly, add to the bitboard the squares which we attack twice in that flank + // but which are not protected by a enemy pawn. Note the trick to shift away the + // previous attack bits to the empty part of the bitboard. + b = (b & ei.attackedBy2[Us] & ~ei.attackedBy[Them][PAWN]) + | (Us == WHITE ? b >> 4 : b << 4); + + // Count all these squares with a single popcount + score += make_score(7 * popcount(b), 0); + if (DoTrace) Trace::add(THREAT, Us, score); @@ -673,10 +703,10 @@ namespace { // ...count safe + (behind & safe) with a single popcount int bonus = popcount((Us == WHITE ? safe << 32 : safe >> 32) | (behind & safe)); - int weight = pos.count(Us) + pos.count(Us) - + pos.count(Them) + pos.count(Them); + bonus = std::min(16, bonus); + int weight = pos.count(Us); - return make_score(bonus * weight * weight * 2 / 11, 0); + return make_score(bonus * weight * weight / 22, 0); } @@ -728,9 +758,9 @@ namespace { // Endings where weaker side can place his king in front of the opponent's // pawns are drawish. else if ( abs(eg) <= BishopValueEg - && ei.pi->pawn_span(strongSide) <= 1 + && pos.count(strongSide) <= 2 && !pos.pawn_passed(~strongSide, pos.square(~strongSide))) - sf = ei.pi->pawn_span(strongSide) ? ScaleFactor(51) : ScaleFactor(37); + sf = ScaleFactor(37 + 7 * pos.count(strongSide)); } return sf; @@ -770,6 +800,8 @@ Value Eval::evaluate(const Position& pos) { // Initialize attack and king safety bitboards ei.attackedBy[WHITE][ALL_PIECES] = ei.attackedBy[BLACK][ALL_PIECES] = 0; + ei.attackedBy[WHITE][KING] = pos.attacks_from(pos.square(WHITE)); + ei.attackedBy[BLACK][KING] = pos.attacks_from(pos.square(BLACK)); eval_init(pos, ei); eval_init(pos, ei);