X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fpawns.cpp;h=41d2e6f8148234d46d86a729c08a4468a737b597;hb=5e18b81e870dd44dd8fadab1a4e428d79e6b09d9;hp=d000209fec7ccdbdb3fc9929f0016b43ec5ed535;hpb=3ec94abcdb5f4ad81cc8829aece2d7fa64bb21db;p=stockfish diff --git a/src/pawns.cpp b/src/pawns.cpp index d000209f..41d2e6f8 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -65,6 +65,22 @@ namespace { #undef S + typedef Value V; + + // Weakness of our pawn shelter in front of the king indexed by [king pawn][rank] + const Value ShelterWeakness[2][8] = + { { V(141), V(0), V(38), V(102), V(128), V(141), V(141), V(141) }, + { V( 61), V(0), V(16), V( 44), V( 56), V( 61), V( 61), V( 61) } }; + + // Danger of enemy pawns moving toward our king indexed by [pawn blocked][rank] + const Value StormDanger[2][8] = + { { V(26), V(0), V(128), V(51), V(26), V(0), V(0), V(0) }, + { V(13), V(0), V( 64), V(25), V(13), V(0), V(0), V(0) } }; + + // Max bonus for king safety. Corresponds to start position with all the pawns + // in front of the king and no enemy pawns on the horizont. + const Value MaxSafetyBonus = V(263); + inline Score apply_weight(Score v, Score w) { return make_score((int(mg_value(v)) * mg_value(w)) / 0x100, (int(eg_value(v)) * eg_value(w)) / 0x100); @@ -188,7 +204,7 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns, // full attack info to evaluate passed pawns. Only the frontmost passed // pawn on each file is considered a true passed pawn. if (passed && !doubled) - set_bit(&(pi->passedPawns[Us]), s); + pi->passedPawns[Us] |= s; // Score this pawn if (isolated) @@ -210,31 +226,66 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns, } -/// PawnInfo::updateShelter() calculates and caches king shelter. It is called -/// only when king square changes, about 20% of total king_shelter() calls. template -Score PawnInfo::updateShelter(const Position& pos, Square ksq) { +int PawnInfo::shelter_storm(const Position& pos, Square ksq) { + + const Color Them = (Us == WHITE ? BLACK : WHITE); + + int safety = 0; + Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, ksq) | RankBB[rank_of(ksq)]); + Bitboard ourPawns = b & pos.pieces(Us) & ~RankBB[rank_of(ksq)]; + Bitboard theirPawns = b & pos.pieces(Them); + Rank rkUs, rkThem; + File kf = file_of(ksq); + + // Compute shelter and storm values for the file the king is on, as well as + // the two adjacent files. Computation is done from the white point of view. + kf = (kf == FILE_A) ? kf++ : (kf == FILE_H) ? kf-- : kf; + + for (int f = kf - 1; f <= kf + 1; f++) + { + // Shelter penalty is higher for the pawn in front of the king + b = ourPawns & FileBB[f]; + rkUs = b ? rank_of(Us == WHITE ? first_1(b) : ~last_1(b)) : RANK_1; + safety -= ShelterWeakness[f != kf][rkUs]; + + // Storm danger is smaller if enemy pawn is blocked + b = theirPawns & FileBB[f]; + rkThem = b ? rank_of(Us == WHITE ? first_1(b) : ~last_1(b)) : RANK_1; + safety -= StormDanger[rkThem == rkUs + 1][rkThem]; + } - const int Shift = (Us == WHITE ? 8 : -8); + return safety; +} + + +/// PawnInfo::update_safety() calculates and caches a bonus for king safety. It is +/// called only when king square changes, about 20% of total king_safety() calls. - Bitboard pawns; - int r, shelter = 0; +template +Score PawnInfo::update_safety(const Position& pos, Square ksq) { + + int bonus = 0; if (relative_rank(Us, ksq) <= RANK_4) { - pawns = pos.pieces(PAWN, Us) & this_and_adjacent_files_bb(file_of(ksq)); - r = ksq & (7 << 3); - for (int i = 0; i < 3; i++) - { - r += Shift; - shelter += BitCount8Bit[(pawns >> r) & 0xFF] << (6 - i); - } + bonus = shelter_storm(pos, ksq); + + // If we can castle use the bonus after the castle if is bigger + if (pos.can_castle(Us == WHITE ? WHITE_OO : BLACK_OO)) + bonus = std::max(bonus, shelter_storm(pos, relative_square(Us, SQ_G1))); + + if (pos.can_castle(Us == WHITE ? WHITE_OOO : BLACK_OOO)) + bonus = std::max(bonus, shelter_storm(pos, relative_square(Us, SQ_C1))); + + bonus += MaxSafetyBonus; // Offset to be sure bonus is always positive } + kingSquares[Us] = ksq; - kingShelters[Us] = make_score(shelter, 0); + kingShelters[Us] = make_score(bonus, 0); return kingShelters[Us]; } // Explicit template instantiation -template Score PawnInfo::updateShelter(const Position& pos, Square ksq); -template Score PawnInfo::updateShelter(const Position& pos, Square ksq); +template Score PawnInfo::update_safety(const Position& pos, Square ksq); +template Score PawnInfo::update_safety(const Position& pos, Square ksq);