// attacks which one.
const Value MidgameThreatBonus[8][8] = {
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
// attacks which one.
const Value MidgameThreatBonus[8][8] = {
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
- { V(0),V(30), V(0),V(50), V(70), V(70), V(0), V(0) }, // KNIGHT attacks
- { V(0),V(30),V(50), V(0), V(70), V(70), V(0), V(0) }, // BISHOP attacks
- { V(0),V(20),V(40),V(40), V(0), V(50), V(0), V(0) }, // ROOK attacks
- { V(0),V(40),V(40),V(40), V(40), V(0), V(0), V(0) }, // QUEEN attacks
+ { V(0),V(18), V(0),V(37), V(55), V(55), V(0), V(0) }, // KNIGHT attacks
+ { V(0),V(18),V(37), V(0), V(55), V(55), V(0), V(0) }, // BISHOP attacks
+ { V(0), V(9),V(27),V(27), V(0), V(37), V(0), V(0) }, // ROOK attacks
+ { V(0),V(27),V(27),V(27), V(27), V(0), V(0), V(0) }, // QUEEN attacks
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) } // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) } // not used
const Value EndgameThreatBonus[8][8] = {
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
const Value EndgameThreatBonus[8][8] = {
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
- { V(0),V(40), V(0),V(50),V(100),V(100), V(0), V(0) }, // KNIGHT attacks
- { V(0),V(40),V(50), V(0),V(100),V(100), V(0), V(0) }, // BISHOP attacks
- { V(0),V(30),V(50),V(50), V(0), V(50), V(0), V(0) }, // ROOK attacks
- { V(0),V(40),V(40),V(40), V(40), V(0), V(0), V(0) }, // QUEEN attacks
+ { V(0),V(37), V(0),V(47), V(97), V(97), V(0), V(0) }, // KNIGHT attacks
+ { V(0),V(37),V(47), V(0), V(97), V(97), V(0), V(0) }, // BISHOP attacks
+ { V(0),V(27),V(47),V(47), V(0), V(47), V(0), V(0) }, // ROOK attacks
+ { V(0),V(37),V(37),V(37), V(37), V(0), V(0), V(0) }, // QUEEN attacks
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) } // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) }, // not used
{ V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0) } // not used
// ThreatedByPawnPenalty[] contains a penalty according to which piece
// type is attacked by an enemy pawn.
const Value MidgameThreatedByPawnPenalty[8] = {
// ThreatedByPawnPenalty[] contains a penalty according to which piece
// type is attacked by an enemy pawn.
const Value MidgameThreatedByPawnPenalty[8] = {
- V(0), V(0), V(50), V(50), V(70), V(80), V(0), V(0)
+ V(0), V(0), V(56), V(56), V(76), V(86), V(0), V(0)
- V(0), V(0), V(70), V(70), V(100), V(120), V(0), V(0)
+ V(0), V(0), V(70), V(70), V(99), V(118), V(0), V(0)
void evaluate_trapped_bishop_a7h7(const Position& pos, Square s, Color us, EvalInfo& ei);
void evaluate_trapped_bishop_a1h1(const Position& pos, Square s, Color us, EvalInfo& ei);
inline Value apply_weight(Value v, int w);
void evaluate_trapped_bishop_a7h7(const Position& pos, Square s, Color us, EvalInfo& ei);
void evaluate_trapped_bishop_a1h1(const Position& pos, Square s, Color us, EvalInfo& ei);
inline Value apply_weight(Value v, int w);
- Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]);
+ inline Score apply_weight(Score v, int wmg, int weg);
+ Value scale_by_game_phase(const Score& v, Phase ph, const ScaleFactor sf[]);
// Initialize by reading the incrementally updated scores included in the
// position object (material + piece square tables)
// Initialize by reading the incrementally updated scores included in the
// position object (material + piece square tables)
// If we have a specialized evaluation function for the current material
// configuration, call it and return
// If we have a specialized evaluation function for the current material
// configuration, call it and return
- ei.mgValue += apply_weight(ei.pi->mg_value(), WeightPawnStructureMidgame);
- ei.egValue += apply_weight(ei.pi->eg_value(), WeightPawnStructureEndgame);
+ ei.value += apply_weight(ei.pi->value(), WeightPawnStructureMidgame, WeightPawnStructureEndgame);
// Initialize king attack bitboards and king attack zones for both sides
ei.attackedBy[WHITE][KING] = pos.attacks_from<KING>(pos.king_square(WHITE));
// Initialize king attack bitboards and king attack zones for both sides
ei.attackedBy[WHITE][KING] = pos.attacks_from<KING>(pos.king_square(WHITE));
else if ( square_file(pos.king_square(WHITE)) <= FILE_D
&& square_file(pos.king_square(BLACK)) >= FILE_E)
else if ( square_file(pos.king_square(WHITE)) <= FILE_D
&& square_file(pos.king_square(BLACK)) >= FILE_E)
- ei.mgValue += apply_weight(ei.mgMobility, WeightMobilityMidgame);
- ei.egValue += apply_weight(ei.egMobility, WeightMobilityEndgame);
+ ei.value += apply_weight(Score(ei.mgMobility, ei.egMobility), WeightMobilityMidgame, WeightMobilityEndgame);
// If we don't already have an unusual scale factor, check for opposite
// colored bishop endgames, and use a lower scale for those
if ( phase < PHASE_MIDGAME
&& pos.opposite_colored_bishops()
// If we don't already have an unusual scale factor, check for opposite
// colored bishop endgames, and use a lower scale for those
if ( phase < PHASE_MIDGAME
&& pos.opposite_colored_bishops()
- && ( (factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0))
- || (factor[BLACK] == SCALE_FACTOR_NORMAL && ei.egValue < Value(0))))
+ && ( (factor[WHITE] == SCALE_FACTOR_NORMAL && ei.value.eg() > Value(0))
+ || (factor[BLACK] == SCALE_FACTOR_NORMAL && ei.value.eg() < Value(0))))
- Value v = Sign[stm] * scale_by_game_phase(ei.mgValue, ei.egValue, phase, factor);
+ Value v = Sign[stm] * scale_by_game_phase(ei.value, phase, factor);
- return Sign[stm] * scale_by_game_phase(mgv, egv, ph, sf);
+ return Sign[stm] * scale_by_game_phase(pos.value(), ph, sf);
// Decrease score if we are attacked by an enemy pawn. Remaining part
// of threat evaluation must be done later when we have full attack info.
if (bit_is_set(ei.attackedBy[Them][PAWN], s))
// Decrease score if we are attacked by an enemy pawn. Remaining part
// of threat evaluation must be done later when we have full attack info.
if (bit_is_set(ei.attackedBy[Them][PAWN], s))
// Bishop and knight outposts squares
if ((Piece == BISHOP || Piece == KNIGHT) && pos.square_is_weak(s, Them))
// Bishop and knight outposts squares
if ((Piece == BISHOP || Piece == KNIGHT) && pos.square_is_weak(s, Them))
if ( relative_rank(Us, s) == RANK_7
&& relative_rank(Us, pos.king_square(Them)) == RANK_8)
{
if ( relative_rank(Us, s) == RANK_7
&& relative_rank(Us, pos.king_square(Them)) == RANK_8)
{
- ei.mgValue += Sign[Us] * (Piece == ROOK ? MidgameRookOn7thBonus : MidgameQueenOn7thBonus);
- ei.egValue += Sign[Us] * (Piece == ROOK ? EndgameRookOn7thBonus : EndgameQueenOn7thBonus);
+ ei.value += Sign[Us] * (Piece == ROOK ? Score(MidgameRookOn7thBonus, EndgameRookOn7thBonus)
+ : Score(MidgameQueenOn7thBonus, EndgameQueenOn7thBonus));
if (ei.pi->file_is_half_open(Us, f))
{
if (ei.pi->file_is_half_open(Them, f))
if (ei.pi->file_is_half_open(Us, f))
{
if (ei.pi->file_is_half_open(Them, f))
{
// 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)))
{
// 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 -= pos.can_castle(Us)? Sign[Us] * ((TrappedRookPenalty - mob * 16) / 2)
- : Sign[Us] * (TrappedRookPenalty - mob * 16);
+ ei.value -= Sign[Us] * Score(pos.can_castle(Us) ? (TrappedRookPenalty - mob * 16) / 2
+ : (TrappedRookPenalty - mob * 16), 0);
{
// 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)))
{
// 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 -= pos.can_castle(Us)? Sign[Us] * ((TrappedRookPenalty - mob * 16) / 2)
- : Sign[Us] * (TrappedRookPenalty - mob * 16);
+ ei.value -= Sign[Us] * Score(pos.can_castle(Us) ? (TrappedRookPenalty - mob * 16) / 2
+ : (TrappedRookPenalty - mob * 16), 0);
// Enemy pieces not defended by a pawn and under our attack
Bitboard weakEnemies = pos.pieces_of_color(Them)
// Enemy pieces not defended by a pawn and under our attack
Bitboard weakEnemies = pos.pieces_of_color(Them)
if (relative_rank(Us, s) <= RANK_4)
{
shelter = ei.pi->get_king_shelter(pos, Us, s);
if (relative_rank(Us, s) <= RANK_4)
{
shelter = ei.pi->get_king_shelter(pos, Us, s);
// change far bigger than the value of the captured piece.
Value v = apply_weight(SafetyTable[attackUnits], WeightKingSafety[Us]);
// change far bigger than the value of the captured piece.
Value v = apply_weight(SafetyTable[attackUnits], WeightKingSafety[Us]);
- ei.mgValue += apply_weight(Sign[Us] * mbonus, WeightPassedPawnsMidgame);
- ei.egValue += apply_weight(Sign[Us] * ebonus, WeightPassedPawnsEndgame);
+ ei.value += Score(apply_weight(Sign[Us] * mbonus, WeightPassedPawnsMidgame),
+ apply_weight(Sign[Us] * ebonus, WeightPassedPawnsEndgame));
// If one side queens at least three plies before the other, that side wins
if (movesToGo[winnerSide] <= movesToGo[loserSide] - 3)
// If one side queens at least three plies before the other, that side wins
if (movesToGo[winnerSide] <= movesToGo[loserSide] - 3)
// If one side queens one ply before the other and checks the king or attacks
// the undefended opponent's queening square, that side wins. To avoid cases
// If one side queens one ply before the other and checks the king or attacks
// the undefended opponent's queening square, that side wins. To avoid cases
if ( (b & pos.pieces(KING, loserSide))
||(bit_is_set(b, loserQSq) && !bit_is_set(ei.attacked_by(loserSide), loserQSq)))
if ( (b & pos.pieces(KING, loserSide))
||(bit_is_set(b, loserQSq) && !bit_is_set(ei.attacked_by(loserSide), loserQSq)))
int space = count_1s_max_15<HasPopCnt>(safeSquares)
+ count_1s_max_15<HasPopCnt>(behindFriendlyPawns & safeSquares);
int space = count_1s_max_15<HasPopCnt>(safeSquares)
+ count_1s_max_15<HasPopCnt>(behindFriendlyPawns & safeSquares);
// scale_by_game_phase() interpolates between a middle game and an endgame
// score, based on game phase. It also scales the return value by a
// ScaleFactor array.
// scale_by_game_phase() interpolates between a middle game and an endgame
// score, based on game phase. It also scales the return value by a
// ScaleFactor array.
- Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]) {
+ Value scale_by_game_phase(const Score& v, Phase ph, const ScaleFactor sf[]) {
- assert(mv > -VALUE_INFINITE && mv < VALUE_INFINITE);
- assert(ev > -VALUE_INFINITE && ev < VALUE_INFINITE);
+ assert(v.mg() > -VALUE_INFINITE && v.mg() < VALUE_INFINITE);
+ assert(v.eg() > -VALUE_INFINITE && v.eg() < VALUE_INFINITE);