namespace {
- // evaluate_knight() assigns bonuses and penalties to a knight of a given
- // color on a given square.
+ // evaluate_common() computes terms common to all pieces attack
- void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) {
+ int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei,
+ int AttackWeight, const Value* mgBonus, const Value* egBonus,
+ Square s = SQ_NONE, const Value* OutpostBonus = NULL) {
Color them = opposite_color(us);
- Bitboard b = p.knight_attacks(s);
- ei.attackedBy[us][KNIGHT] |= b;
// King attack
- if(b & ei.attackZone[us]) {
+ if (b & ei.attackZone[us])
+ {
ei.attackCount[us]++;
- ei.attackWeight[us] += KnightAttackWeight;
+ ei.attackWeight[us] += AttackWeight;
Bitboard bb = (b & ei.attackedBy[them][KING]);
- if(bb) ei.attacked[us] += count_1s_max_15(bb);
+ if (bb)
+ ei.attacked[us] += count_1s_max_15(bb);
}
// Mobility
int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
- ei.mgMobility += Sign[us] * MidgameKnightMobilityBonus[mob];
- ei.egMobility += Sign[us] * EndgameKnightMobilityBonus[mob];
-
- // Knight outposts:
- if(p.square_is_weak(s, them)) {
- Value v, bonus;
-
- // Initial bonus based on square:
- v = bonus = KnightOutpostBonus[relative_square(us, s)];
-
- // Increase bonus if supported by pawn, especially if the opponent has
- // no minor piece which can exchange the outpost piece:
- if(v && p.pawn_attacks(them, s) & p.pawns(us)) {
- bonus += v/2;
- if(p.knight_count(them) == 0 &&
- (SquaresByColorBB[square_color(s)] &
- p.bishops(them)) == EmptyBoardBB) {
- bonus += v;
- }
- }
+ ei.mgMobility += Sign[us] * mgBonus[mob];
+ ei.egMobility += Sign[us] * egBonus[mob];
+
+ // Bishop and Knight outposts
+ if (!OutpostBonus || !p.square_is_weak(s, them))
+ return mob;
+
+ // Initial bonus based on square
+ Value v, bonus;
+ v = bonus = OutpostBonus[relative_square(us, s)];
- ei.mgValue += Sign[us] * bonus;
- ei.egValue += Sign[us] * bonus;
+ // Increase bonus if supported by pawn, especially if the opponent has
+ // no minor piece which can exchange the outpost piece
+ if (v && (p.pawn_attacks(them, s) & p.pawns(us)))
+ {
+ bonus += v / 2;
+ if ( p.knight_count(them) == 0
+ && (SquaresByColorBB[square_color(s)] & p.bishops(them)) == EmptyBoardBB)
+ bonus += v;
}
+ ei.mgValue += Sign[us] * bonus;
+ ei.egValue += Sign[us] * bonus;
+ return mob;
}
- // evaluate_bishop() assigns bonuses and penalties to a bishop of a given
+ // evaluate_knight() assigns bonuses and penalties to a knight of a given
// color on a given square.
- void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei) {
+ void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) {
- Color them = opposite_color(us);
- Bitboard b =
- bishop_attacks_bb(s, p.occupied_squares() & ~p.queens(us));
+ Bitboard b = p.knight_attacks(s);
+ ei.attackedBy[us][KNIGHT] |= b;
- ei.attackedBy[us][BISHOP] |= b;
+ // King attack, mobility and outposts
+ evaluate_common(p, b, us, ei, KnightAttackWeight, MidgameKnightMobilityBonus,
+ EndgameKnightMobilityBonus, s, KnightOutpostBonus);
+ }
- // King attack
- if(b & ei.attackZone[us]) {
- ei.attackCount[us]++;
- ei.attackWeight[us] += BishopAttackWeight;
- Bitboard bb = (b & ei.attackedBy[them][KING]);
- if(bb) ei.attacked[us] += count_1s_max_15(bb);
- }
- // Mobility:
- int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
- ei.mgMobility += Sign[us] * MidgameBishopMobilityBonus[mob];
- ei.egMobility += Sign[us] * EndgameBishopMobilityBonus[mob];
-
- // Bishop outposts:
- if(p.square_is_weak(s, them)) {
- Value v, bonus;
-
- // Initial bonus based on square:
- v = bonus = BishopOutpostBonus[relative_square(us, s)];
-
- // Increase bonus if supported by pawn, especially if the opponent has
- // no minor piece which can exchange the outpost piece:
- if(v && p.pawn_attacks(them, s) & p.pawns(us)) {
- bonus += v/2;
- if(p.knight_count(them) == 0 &&
- (SquaresByColorBB[square_color(s)] &
- p.bishops(them)) == EmptyBoardBB) {
- bonus += v;
- }
- }
+ // evaluate_bishop() assigns bonuses and penalties to a bishop of a given
+ // color on a given square.
- ei.mgValue += Sign[us] * bonus;
- ei.egValue += Sign[us] * bonus;
- }
+ void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei) {
+
+ Bitboard b = bishop_attacks_bb(s, p.occupied_squares() & ~p.queens(us));
+ ei.attackedBy[us][BISHOP] |= b;
+
+ // King attack, mobility and outposts
+ evaluate_common(p, b, us, ei, BishopAttackWeight, MidgameBishopMobilityBonus,
+ EndgameBishopMobilityBonus, s, BishopOutpostBonus);
}
void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei) {
- Color them = opposite_color(us);
-
- // Open and half-open files:
- File f = square_file(s);
- if(ei.pi->file_is_half_open(us, f)) {
- if(ei.pi->file_is_half_open(them, f)) {
- ei.mgValue += Sign[us] * RookOpenFileBonus;
- ei.egValue += Sign[us] * RookOpenFileBonus;
- }
- else {
- ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
- ei.egValue += Sign[us] * RookHalfOpenFileBonus;
- }
- }
-
- // Rook on 7th rank:
- if(pawn_rank(us, s) == RANK_7 &&
- pawn_rank(us, p.king_square(them)) == RANK_8) {
- ei.mgValue += Sign[us] * MidgameRookOn7thBonus;
- ei.egValue += Sign[us] * EndgameRookOn7thBonus;
- }
-
//Bitboard b = p.rook_attacks(s);
- Bitboard b =
- rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
+ Bitboard b = rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
ei.attackedBy[us][ROOK] |= b;
- // King attack
- if(b & ei.attackZone[us]) {
- ei.attackCount[us]++;
- ei.attackWeight[us] += RookAttackWeight;
- Bitboard bb = (b & ei.attackedBy[them][KING]);
- if(bb) ei.attacked[us] += count_1s_max_15(bb);
+ // King attack and mobility
+ int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus,
+ EndgameRookMobilityBonus);
+
+ // Rook on 7th rank
+ Color them = opposite_color(us);
+
+ if ( relative_rank(us, s) == RANK_7
+ && relative_rank(us, p.king_square(them)) == RANK_8)
+ {
+ ei.mgValue += Sign[us] * MidgameRookOn7thBonus;
+ ei.egValue += Sign[us] * EndgameRookOn7thBonus;
}
- // Mobility
- int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
- ei.mgMobility += Sign[us] * MidgameRookMobilityBonus[mob];
- ei.egMobility += Sign[us] * EndgameRookMobilityBonus[mob];
-
- // Penalize rooks which are trapped inside a king which has lost the
- // right to castle:
- if(mob <= 6 && !ei.pi->file_is_half_open(us, f)) {
- Square ksq = p.king_square(us);
- if(square_file(ksq) >= FILE_E && square_file(s) > square_file(ksq) &&
- (pawn_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s))) {
- // Is there a half-open file between the king and the edge of the
- // board?
- if(!(ei.pi->has_open_file_to_right(us, square_file(ksq)))) {
- ei.mgValue -= p.can_castle(us)?
- Sign[us] * ((TrappedRookPenalty - mob * 16) / 2) :
- Sign[us] * (TrappedRookPenalty - mob * 16);
+ // Open and half-open files
+ File f = square_file(s);
+ if (ei.pi->file_is_half_open(us, f))
+ {
+ if (ei.pi->file_is_half_open(them, f))
+ {
+ ei.mgValue += Sign[us] * RookOpenFileBonus;
+ ei.egValue += Sign[us] * RookOpenFileBonus;
}
- }
- else if(square_file(ksq) <= FILE_D && square_file(s) < square_file(ksq)
- && (pawn_rank(us, ksq) == RANK_1 ||
- square_rank(ksq) == square_rank(s))) {
- // Is there a half-open file between the king and the edge of the
- // board?
- if(!(ei.pi->has_open_file_to_left(us, square_file(ksq)))) {
- ei.mgValue -= p.can_castle(us)?
- Sign[us] * ((TrappedRookPenalty - mob * 16) / 2) :
- Sign[us] * (TrappedRookPenalty - mob * 16);
+ else
+ {
+ ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
+ ei.egValue += Sign[us] * RookHalfOpenFileBonus;
}
- }
+ }
+
+ // Penalize rooks which are trapped inside a king. Penalize more if
+ // king has lost right to castle
+ if (mob > 6 || ei.pi->file_is_half_open(us, f))
+ return;
+
+ Square ksq = p.king_square(us);
+ if ( square_file(ksq) >= FILE_E
+ && square_file(s) > square_file(ksq)
+ && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
+ {
+ // Is there a half-open file between the king and the edge of the board?
+ if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
+ ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
+ : Sign[us] * (TrappedRookPenalty - mob * 16);
+ }
+ else if ( square_file(ksq) <= FILE_D
+ && square_file(s) < square_file(ksq)
+ && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
+ {
+ // Is there a half-open file between the king and the edge of the board?
+ if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
+ ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
+ : Sign[us] * (TrappedRookPenalty - mob * 16);
}
}
void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei) {
- Color them = opposite_color(us);
-
- // Queen on 7th rank:
- if(pawn_rank(us, s) == RANK_7 &&
- pawn_rank(us, p.king_square(them)) == RANK_8) {
- ei.mgValue += Sign[us] * MidgameQueenOn7thBonus;
- ei.egValue += Sign[us] * EndgameQueenOn7thBonus;
- }
-
Bitboard b = p.queen_attacks(s);
ei.attackedBy[us][QUEEN] |= b;
- // King attack
- if(b & ei.attackZone[us]) {
- ei.attackCount[us]++;
- ei.attackWeight[us] += QueenAttackWeight;
- Bitboard bb = (b & ei.attackedBy[them][KING]);
- if(bb) ei.attacked[us] += count_1s_max_15(bb);
- }
+ // King attack and mobility
+ evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus,
+ EndgameQueenMobilityBonus);
- // Mobility
- int mob = count_1s(b & ~p.pieces_of_color(us));
- ei.mgMobility += Sign[us] * MidgameQueenMobilityBonus[mob];
- ei.egMobility += Sign[us] * EndgameQueenMobilityBonus[mob];
+ // Queen on 7th rank
+ Color them = opposite_color(us);
+
+ if ( relative_rank(us, s) == RANK_7
+ && relative_rank(us, p.king_square(them)) == RANK_8)
+ {
+ ei.mgValue += Sign[us] * MidgameQueenOn7thBonus;
+ ei.egValue += Sign[us] * EndgameQueenOn7thBonus;
+ }
}
int shelter = 0, sign = Sign[us];
// King shelter.
- if(pawn_rank(us, s) <= RANK_4) {
+ if(relative_rank(us, s) <= RANK_4) {
Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s);
Rank r = square_rank(s);
for(int i = 0; i < 3; i++)
assert(pos.piece_on(s) == pawn_of_color(us));
assert(pos.pawn_is_passed(us, s));
- int r = int(pawn_rank(us, s) - RANK_2);
+ int r = int(relative_rank(us, s) - RANK_2);
int tr = Max(0, r * (r-1));
Square blockSq = s + pawn_push(us);
+ ((us == pos.side_to_move())? 0 : 1);
if(d < 0) {
- int mtg = RANK_8 - pawn_rank(us, s);
+ int mtg = RANK_8 - relative_rank(us, s);
int blockerCount =
count_1s_max_15(squares_in_front_of(us,s)&pos.occupied_squares());
mtg += blockerCount;