mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 08:43:09 +00:00
Group common evaluate code
This removes code redundancy but perhaps impact performance due to uninlining. Testing for regression is needed. For now aim to best code readibility. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
7dd0c39714
commit
853ce65f17
1 changed files with 35 additions and 48 deletions
|
@ -571,6 +571,29 @@ void read_weights(Color sideToMove) {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
// evaluate_common() computes terms common to all pieces attack
|
||||||
|
|
||||||
|
int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei,
|
||||||
|
int AttackWeight, const Value* mgBonus, const Value* egBonus) {
|
||||||
|
|
||||||
|
Color them = opposite_color(us);
|
||||||
|
|
||||||
|
// King attack
|
||||||
|
if(b & ei.attackZone[us]) {
|
||||||
|
ei.attackCount[us]++;
|
||||||
|
ei.attackWeight[us] += AttackWeight;
|
||||||
|
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] * mgBonus[mob];
|
||||||
|
ei.egMobility += Sign[us] * egBonus[mob];
|
||||||
|
|
||||||
|
return mob;
|
||||||
|
}
|
||||||
|
|
||||||
// evaluate_knight() assigns bonuses and penalties to a knight of a given
|
// evaluate_knight() assigns bonuses and penalties to a knight of a given
|
||||||
// color on a given square.
|
// color on a given square.
|
||||||
|
|
||||||
|
@ -580,18 +603,9 @@ namespace {
|
||||||
Bitboard b = p.knight_attacks(s);
|
Bitboard b = p.knight_attacks(s);
|
||||||
ei.attackedBy[us][KNIGHT] |= b;
|
ei.attackedBy[us][KNIGHT] |= b;
|
||||||
|
|
||||||
// King attack
|
// King attack and mobility
|
||||||
if(b & ei.attackZone[us]) {
|
evaluate_common(p, b, us, ei, KnightAttackWeight,
|
||||||
ei.attackCount[us]++;
|
MidgameKnightMobilityBonus, EndgameKnightMobilityBonus);
|
||||||
ei.attackWeight[us] += KnightAttackWeight;
|
|
||||||
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] * MidgameKnightMobilityBonus[mob];
|
|
||||||
ei.egMobility += Sign[us] * EndgameKnightMobilityBonus[mob];
|
|
||||||
|
|
||||||
// Knight outposts:
|
// Knight outposts:
|
||||||
if(p.square_is_weak(s, them)) {
|
if(p.square_is_weak(s, them)) {
|
||||||
|
@ -628,18 +642,9 @@ namespace {
|
||||||
|
|
||||||
ei.attackedBy[us][BISHOP] |= b;
|
ei.attackedBy[us][BISHOP] |= b;
|
||||||
|
|
||||||
// King attack
|
// King attack and mobility
|
||||||
if(b & ei.attackZone[us]) {
|
evaluate_common(p, b, us, ei, BishopAttackWeight,
|
||||||
ei.attackCount[us]++;
|
MidgameBishopMobilityBonus, EndgameBishopMobilityBonus);
|
||||||
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:
|
// Bishop outposts:
|
||||||
if(p.square_is_weak(s, them)) {
|
if(p.square_is_weak(s, them)) {
|
||||||
|
@ -697,18 +702,9 @@ namespace {
|
||||||
rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
|
rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
|
||||||
ei.attackedBy[us][ROOK] |= b;
|
ei.attackedBy[us][ROOK] |= b;
|
||||||
|
|
||||||
// King attack
|
// King attack and mobility
|
||||||
if(b & ei.attackZone[us]) {
|
int mob = evaluate_common(p, b, us, ei, RookAttackWeight,
|
||||||
ei.attackCount[us]++;
|
MidgameRookMobilityBonus, EndgameRookMobilityBonus);
|
||||||
ei.attackWeight[us] += RookAttackWeight;
|
|
||||||
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] * MidgameRookMobilityBonus[mob];
|
|
||||||
ei.egMobility += Sign[us] * EndgameRookMobilityBonus[mob];
|
|
||||||
|
|
||||||
// Penalize rooks which are trapped inside a king which has lost the
|
// Penalize rooks which are trapped inside a king which has lost the
|
||||||
// right to castle:
|
// right to castle:
|
||||||
|
@ -756,18 +752,9 @@ namespace {
|
||||||
Bitboard b = p.queen_attacks(s);
|
Bitboard b = p.queen_attacks(s);
|
||||||
ei.attackedBy[us][QUEEN] |= b;
|
ei.attackedBy[us][QUEEN] |= b;
|
||||||
|
|
||||||
// King attack
|
// King attack and mobility
|
||||||
if(b & ei.attackZone[us]) {
|
evaluate_common(p, b, us, ei, QueenAttackWeight,
|
||||||
ei.attackCount[us]++;
|
MidgameQueenMobilityBonus, EndgameQueenMobilityBonus);
|
||||||
ei.attackWeight[us] += QueenAttackWeight;
|
|
||||||
Bitboard bb = (b & ei.attackedBy[them][KING]);
|
|
||||||
if(bb) ei.attacked[us] += count_1s_max_15(bb);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mobility
|
|
||||||
int mob = count_1s(b & ~p.pieces_of_color(us));
|
|
||||||
ei.mgMobility += Sign[us] * MidgameQueenMobilityBonus[mob];
|
|
||||||
ei.egMobility += Sign[us] * EndgameQueenMobilityBonus[mob];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue