Bitboard FileBB[FILE_NB];
Bitboard RankBB[RANK_NB];
Bitboard AdjacentFilesBB[FILE_NB];
-Bitboard InFrontBB[COLOR_NB][RANK_NB];
+Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
Bitboard DistanceRingBB[SQUARE_NB][8];
-Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
+Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
for (Rank r = RANK_1; r < RANK_8; ++r)
- InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
+ ForwardRanksBB[WHITE][r] = ~(ForwardRanksBB[BLACK][r + 1] = ForwardRanksBB[BLACK][r] | RankBB[r]);
for (Color c = WHITE; c <= BLACK; ++c)
for (Square s = SQ_A1; s <= SQ_H8; ++s)
{
- ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
- PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
- PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
+ ForwardFileBB [c][s] = ForwardRanksBB[c][rank_of(s)] & FileBB[file_of(s)];
+ PawnAttackSpan[c][s] = ForwardRanksBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
+ PassedPawnMask[c][s] = ForwardFileBB [c][s] | PawnAttackSpan[c][s];
}
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
extern Bitboard FileBB[FILE_NB];
extern Bitboard RankBB[RANK_NB];
extern Bitboard AdjacentFilesBB[FILE_NB];
-extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
+extern Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
extern Bitboard DistanceRingBB[SQUARE_NB][8];
-extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
+extern Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
}
-/// in_front_bb() returns a bitboard representing all the squares on all the ranks
+/// forward_ranks_bb() returns a bitboard representing all the squares on all the ranks
/// in front of the given one, from the point of view of the given color. For
-/// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2.
+/// instance, forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
-inline Bitboard in_front_bb(Color c, Rank r) {
- return InFrontBB[c][r];
+inline Bitboard forward_ranks_bb(Color c, Square s) {
+ return ForwardRanksBB[c][rank_of(s)];
}
-/// forward_bb() returns a bitboard representing all the squares along the line
+/// forward_file_bb() returns a bitboard representing all the squares along the line
/// in front of the given one, from the point of view of the given color:
-/// ForwardBB[c][s] = in_front_bb(c, rank_of(s)) & file_bb(s)
+/// ForwardFileBB[c][s] = forward_ranks_bb(c, s) & file_bb(s)
-inline Bitboard forward_bb(Color c, Square s) {
- return ForwardBB[c][s];
+inline Bitboard forward_file_bb(Color c, Square s) {
+ return ForwardFileBB[c][s];
}
/// pawn_attack_span() returns a bitboard representing all the squares that can be
/// attacked by a pawn of the given color when it moves along its file, starting
/// from the given square:
-/// PawnAttackSpan[c][s] = in_front_bb(c, rank_of(s)) & adjacent_files_bb(file_of(s));
+/// PawnAttackSpan[c][s] = forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s));
inline Bitboard pawn_attack_span(Color c, Square s) {
return PawnAttackSpan[c][s];
/// passed_pawn_mask() returns a bitboard mask which can be used to test if a
/// pawn of the given color and on the given square is a passed pawn:
-/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
+/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_file_bb(c, s)
inline Bitboard passed_pawn_mask(Color c, Square s) {
return PassedPawnMask[c][s];
// If all pawns are ahead of the king, on a single rook file and
// the king is within one file of the pawns, it's a draw.
- if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq)))
+ if ( !(pawns & ~forward_ranks_bb(weakSide, ksq))
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
&& distance<File>(ksq, lsb(pawns)) <= 1)
return SCALE_FACTOR_DRAW;
if (relative_rank(strongSide, pawnSq) <= RANK_5)
return SCALE_FACTOR_DRAW;
- Bitboard path = forward_bb(strongSide, pawnSq);
+ Bitboard path = forward_file_bb(strongSide, pawnSq);
if (path & pos.pieces(weakSide, KING))
return SCALE_FACTOR_DRAW;
// King needs to get close to promoting pawn to prevent knight from blocking.
// Rules for this are very tricky, so just approximate.
- if (forward_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
+ if (forward_file_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
return ScaleFactor(distance(weakKingSq, pawnSq));
return SCALE_FACTOR_NONE;
template<Color Us> void initialize();
template<Color Us> Score evaluate_king();
template<Color Us> Score evaluate_threats();
- template<Color Us> Score evaluate_passer_pawns();
+ template<Color Us> Score evaluate_passed_pawns();
template<Color Us> Score evaluate_space();
template<Color Us, PieceType Pt> Score evaluate_pieces();
ScaleFactor evaluate_scale_factor(Value eg);
: ~Bitboard(0) ^ Rank1BB ^ Rank2BB ^ Rank3BB);
const Square ksq = pos.square<KING>(Us);
- Bitboard undefended, b, b1, b2, safe, other;
+ Bitboard kingOnlyDefended, b, b1, b2, safe, other;
int kingDanger;
// King shelter and enemy pawns storm
if (kingAttackersCount[Them] > (1 - pos.count<QUEEN>(Them)))
{
// Find the attacked squares which are defended only by our king...
- undefended = attackedBy[Them][ALL_PIECES]
- & attackedBy[Us][KING]
- & ~attackedBy2[Us];
+ kingOnlyDefended = attackedBy[Them][ALL_PIECES]
+ & attackedBy[Us][KING]
+ & ~attackedBy2[Us];
// ... and those which are not defended at all in the larger king ring
b = attackedBy[Them][ALL_PIECES] & ~attackedBy[Us][ALL_PIECES]
// Initialize the 'kingDanger' variable, which will be transformed
// later into a king danger score. The initial value is based on the
// number and types of the enemy's attacking pieces, the number of
- // attacked and undefended squares around our king and the quality of
- // the pawn shelter (current 'score' value).
+ // attacked and weak squares around our king, the absence of queen and
+ // the quality of the pawn shelter (current 'score' value).
kingDanger = kingAttackersCount[Them] * kingAttackersWeight[Them]
+ 102 * kingAdjacentZoneAttacksCount[Them]
- + 201 * popcount(undefended)
+ + 201 * popcount(kingOnlyDefended)
+ 143 * (popcount(b) + !!pos.pinned_pieces(Us))
- 848 * !pos.count<QUEEN>(Them)
- 9 * mg_value(score) / 8
// Analyse the safe enemy's checks which are possible on next move
safe = ~pos.pieces(Them);
- safe &= ~attackedBy[Us][ALL_PIECES] | (undefended & attackedBy2[Them]);
+ safe &= ~attackedBy[Us][ALL_PIECES] | (kingOnlyDefended & attackedBy2[Them]);
b1 = pos.attacks_from< ROOK>(ksq);
b2 = pos.attacks_from<BISHOP>(ksq);
const Square Up = (Us == WHITE ? NORTH : SOUTH);
const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
- const Bitboard TRank2BB = (Us == WHITE ? Rank2BB : Rank7BB);
- const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
+ const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
Bitboard b, weak, defended, stronglyProtected, safeThreats;
Score score = SCORE_ZERO;
score += ThreatByKing[more_than_one(b)];
}
- // Bonus if some pawns can safely push and attack an enemy piece
- b = pos.pieces(Us, PAWN) & ~TRank7BB;
- b = shift<Up>(b | (shift<Up>(b & TRank2BB) & ~pos.pieces()));
+ // Find squares where our pawns can push on the next move
+ b = shift<Up>(pos.pieces(Us, PAWN)) & ~pos.pieces();
+ b |= shift<Up>(b & TRank3BB) & ~pos.pieces();
- b &= ~pos.pieces()
- & ~attackedBy[Them][PAWN]
+ // Keep only the squares which are not completely unsafe
+ b &= ~attackedBy[Them][PAWN]
& (attackedBy[Us][ALL_PIECES] | ~attackedBy[Them][ALL_PIECES]);
+ // Add a bonus for each new pawn threats from those squares
b = (shift<Left>(b) | shift<Right>(b))
& pos.pieces(Them)
& ~attackedBy[Us][PAWN];
}
- // evaluate_passer_pawns() evaluates the passed pawns and candidate passed
+ // evaluate_passed_pawns() evaluates the passed pawns and candidate passed
// pawns of the given color.
template<Tracing T> template<Color Us>
- Score Evaluation<T>::evaluate_passer_pawns() {
+ Score Evaluation<T>::evaluate_passed_pawns() {
const Color Them = (Us == WHITE ? BLACK : WHITE);
+ const Square Up = (Us == WHITE ? NORTH : SOUTH);
Bitboard b, bb, squaresToQueen, defendedSquares, unsafeSquares;
Score score = SCORE_ZERO;
{
Square s = pop_lsb(&b);
- assert(!(pos.pieces(Them, PAWN) & forward_bb(Us, s + pawn_push(Us))));
+ assert(!(pos.pieces(Them, PAWN) & forward_file_bb(Us, s + Up)));
- bb = forward_bb(Us, s) & (attackedBy[Them][ALL_PIECES] | pos.pieces(Them));
+ bb = forward_file_bb(Us, s) & (attackedBy[Them][ALL_PIECES] | pos.pieces(Them));
score -= HinderPassedPawn * popcount(bb);
int r = relative_rank(Us, s) - RANK_2;
if (rr)
{
- Square blockSq = s + pawn_push(Us);
+ Square blockSq = s + Up;
// Adjust bonus based on the king's proximity
ebonus += distance(pos.square<KING>(Them), blockSq) * 5 * rr
// If blockSq is not the queening square then consider also a second push
if (relative_rank(Us, blockSq) != RANK_8)
- ebonus -= distance(pos.square<KING>(Us), blockSq + pawn_push(Us)) * rr;
+ ebonus -= distance(pos.square<KING>(Us), blockSq + Up) * rr;
// If the pawn is free to advance, then increase the bonus
if (pos.empty(blockSq))
// If there is a rook or queen attacking/defending the pawn from behind,
// consider all the squaresToQueen. Otherwise consider only the squares
// in the pawn's path attacked or occupied by the enemy.
- defendedSquares = unsafeSquares = squaresToQueen = forward_bb(Us, s);
+ defendedSquares = unsafeSquares = squaresToQueen = forward_file_bb(Us, s);
- bb = forward_bb(Them, s) & pos.pieces(ROOK, QUEEN) & pos.attacks_from<ROOK>(s);
+ bb = forward_file_bb(Them, s) & pos.pieces(ROOK, QUEEN) & pos.attacks_from<ROOK>(s);
if (!(pos.pieces(Us) & bb))
defendedSquares &= attackedBy[Us][ALL_PIECES];
// Scale down bonus for candidate passers which need more than one
// pawn push to become passed or have a pawn in front of them.
- if (!pos.pawn_passed(Us, s + pawn_push(Us)) || (pos.pieces(PAWN) & forward_bb(Us, s)))
+ if (!pos.pawn_passed(Us, s + Up) || (pos.pieces(PAWN) & forward_file_bb(Us, s)))
mbonus /= 2, ebonus /= 2;
score += make_score(mbonus, ebonus) + PassedFile[file_of(s)];
score += evaluate_threats<WHITE>()
- evaluate_threats<BLACK>();
- score += evaluate_passer_pawns<WHITE>()
- - evaluate_passer_pawns<BLACK>();
+ score += evaluate_passed_pawns<WHITE>()
+ - evaluate_passed_pawns<BLACK>();
if (pos.non_pawn_material() >= SpaceThreshold)
score += evaluate_space<WHITE>()
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
// Flag the pawn
- opposed = theirPawns & forward_bb(Us, s);
+ opposed = theirPawns & forward_file_bb(Us, s);
stoppers = theirPawns & passed_pawn_mask(Us, s);
lever = theirPawns & PawnAttacks[Us][s];
leverPush = theirPawns & PawnAttacks[Us][s + Up];
// which could become passed after one or two pawn pushes when are
// not attacked more times than defended.
if ( !(stoppers ^ lever ^ leverPush)
- && !(ourPawns & forward_bb(Us, s))
+ && !(ourPawns & forward_file_bb(Us, s))
&& popcount(supported) >= popcount(lever)
&& popcount(phalanx) >= popcount(leverPush))
e->passedPawns[Us] |= s;
enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked };
- Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
+ Bitboard b = pos.pieces(PAWN) & (forward_ranks_bb(Us, ksq) | rank_bb(ksq));
Bitboard ourPawns = b & pos.pieces(Us);
Bitboard theirPawns = b & pos.pieces(Them);
Value safety = MaxSafetyBonus;
b = ourPawns & file_bb(f);
Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
- b = theirPawns & file_bb(f);
+ b = theirPawns & file_bb(f);
Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
int d = std::min(f, FILE_H - f);