// Pointer to pawn hash table entry
PawnInfo* pi;
+ // updateKingTables[color] is set to true if we have enough material
+ // to trigger the opponent's king safety calculation. When is false we
+ // skip the time consuming update of the king attackers tables.
+ bool updateKingTables[2];
+
// attackedBy[color][piece type] is a bitboard representing all squares
// attacked by a given color and piece type, attackedBy[color][0] contains
// all squares attacked by the given color.
const int KingAttackWeights[] = { 0, 0, 2, 2, 3, 5 };
// Bonuses for enemy's safe checks
- const int QueenContactCheckBonus = 3;
- const int QueenCheckBonus = 2;
- const int RookCheckBonus = 1;
+ const int QueenContactCheckBonus = 6;
+ const int RookContactCheckBonus = 4;
+ const int QueenCheckBonus = 3;
+ const int RookCheckBonus = 2;
const int BishopCheckBonus = 1;
const int KnightCheckBonus = 1;
Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.king_square(Them));
ei.kingZone[Us] = (b | (Us == WHITE ? b >> 8 : b << 8));
ei.attackedBy[Us][PAWN] = ei.pi->pawn_attacks(Us);
- b &= ei.attackedBy[Us][PAWN];
- ei.kingAttackersCount[Us] = b ? count_1s_max_15<HasPopCnt>(b) / 2 : EmptyBoardBB;
- ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = EmptyBoardBB;
+ ei.updateKingTables[Us] = pos.piece_count(Us, QUEEN) && pos.non_pawn_material(Us) >= QueenValueMidgame + RookValueMidgame;
+ if (ei.updateKingTables[Us])
+ {
+ b &= ei.attackedBy[Us][PAWN];
+ ei.kingAttackersCount[Us] = b ? count_1s_max_15<HasPopCnt>(b) / 2 : EmptyBoardBB;
+ ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = EmptyBoardBB;
+ }
}
ei.attackedBy[Us][Piece] |= b;
// King attacks
- if (b & ei.kingZone[Us])
+ if (ei.updateKingTables[Us] && (b & ei.kingZone[Us]))
{
ei.kingAttackersCount[Us]++;
ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
// King safety. This is quite complicated, and is almost certainly far
// from optimally tuned.
- if ( ei.kingAttackersCount[Them] >= 2
- && pos.non_pawn_material(Them) >= QueenValueMidgame + RookValueMidgame
- && pos.piece_count(Them, QUEEN) >= 1
+ if ( ei.updateKingTables[Them]
+ && ei.kingAttackersCount[Them] >= 2
&& ei.kingAdjacentZoneAttacksCount[Them])
{
// Find the attacked squares around the king which has no defenders
* (Them == pos.side_to_move() ? 2 : 1);
}
+ // Analyse enemy's safe rook contact checks. First find undefended
+ // squares around the king attacked by enemy rooks...
+ b = undefended & ei.attackedBy[Them][ROOK] & ~pos.pieces_of_color(Them);
+
+ // Consider only squares where the enemy rook gives check
+ b &= RookPseudoAttacks[ksq];
+
+ if (b)
+ {
+ // ...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][QUEEN]);
+ if (b)
+ attackUnits += RookContactCheckBonus
+ * count_1s_max_15<HasPopCnt>(b)
+ * (Them == pos.side_to_move() ? 2 : 1);
+ }
+
// Analyse enemy's safe distance checks for sliders and knights
safe = ~(pos.pieces_of_color(Them) | ei.attackedBy[Us][0]);