1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Rename pawn_rank() in relative_rank()

It is more clear, at last for me.

Also cleanup evaluate_rook() and evaluate_queen()

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2008-09-24 16:45:19 +02:00
parent 3263ee8557
commit f56af8e84d
8 changed files with 82 additions and 79 deletions

View file

@ -464,7 +464,7 @@ ScaleFactor KBPKScalingFunction::apply(const Position &pos) {
// If the defending king has distance 1 to the promotion square or // If the defending king has distance 1 to the promotion square or
// is placed somewhere in front of the pawn, it's a draw. // is placed somewhere in front of the pawn, it's a draw.
if(square_distance(kingSq, queeningSq) <= 1 || if(square_distance(kingSq, queeningSq) <= 1 ||
pawn_rank(strongerSide, kingSq) >= rank) relative_rank(strongerSide, kingSq) >= rank)
return ScaleFactor(0); return ScaleFactor(0);
} }
} }
@ -485,8 +485,8 @@ ScaleFactor KQKRPScalingFunction::apply(const Position &pos) {
assert(pos.pawn_count(weakerSide) >= 1); assert(pos.pawn_count(weakerSide) >= 1);
Square kingSq = pos.king_square(weakerSide); Square kingSq = pos.king_square(weakerSide);
if(pawn_rank(weakerSide, kingSq) <= RANK_2 && if(relative_rank(weakerSide, kingSq) <= RANK_2 &&
pawn_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4 && relative_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4 &&
(pos.rooks(weakerSide) & relative_rank_bb(weakerSide, RANK_3)) && (pos.rooks(weakerSide) & relative_rank_bb(weakerSide, RANK_3)) &&
(pos.pawns(weakerSide) & relative_rank_bb(weakerSide, RANK_2)) && (pos.pawns(weakerSide) & relative_rank_bb(weakerSide, RANK_2)) &&
(pos.king_attacks(kingSq) & pos.pawns(weakerSide))) { (pos.king_attacks(kingSq) & pos.pawns(weakerSide))) {
@ -626,10 +626,10 @@ ScaleFactor KRPPKRPScalingFunction::apply(const Position &pos) {
pos.pawn_is_passed(strongerSide, wpsq2)) pos.pawn_is_passed(strongerSide, wpsq2))
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;
Rank r = Max(pawn_rank(strongerSide, wpsq1), pawn_rank(strongerSide, wpsq2)); Rank r = Max(relative_rank(strongerSide, wpsq1), relative_rank(strongerSide, wpsq2));
if(file_distance(bksq, wpsq1) <= 1 && file_distance(bksq, wpsq2) <= 1 if(file_distance(bksq, wpsq1) <= 1 && file_distance(bksq, wpsq2) <= 1
&& pawn_rank(strongerSide, bksq) > r) { && relative_rank(strongerSide, bksq) > r) {
switch(r) { switch(r) {
case RANK_2: return ScaleFactor(10); case RANK_2: return ScaleFactor(10);
@ -707,9 +707,9 @@ ScaleFactor KBPKBScalingFunction::apply(const Position &pos) {
// Case 1: Defending king blocks the pawn, and cannot be driven away. // Case 1: Defending king blocks the pawn, and cannot be driven away.
if(square_file(weakerKingSq) == square_file(pawnSq) if(square_file(weakerKingSq) == square_file(pawnSq)
&& pawn_rank(strongerSide, pawnSq) < pawn_rank(strongerSide, weakerKingSq) && relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
&& (square_color(weakerKingSq) != square_color(strongerBishopSq) && (square_color(weakerKingSq) != square_color(strongerBishopSq)
|| pawn_rank(strongerSide, weakerKingSq) <= RANK_6)) || relative_rank(strongerSide, weakerKingSq) <= RANK_6))
return ScaleFactor(0); return ScaleFactor(0);
// Case 2: Opposite colored bishops. // Case 2: Opposite colored bishops.
@ -725,7 +725,7 @@ ScaleFactor KBPKBScalingFunction::apply(const Position &pos) {
// These rules are probably not perfect, but in practice they work // These rules are probably not perfect, but in practice they work
// reasonably well. // reasonably well.
if(pawn_rank(strongerSide, pawnSq) <= RANK_5) if(relative_rank(strongerSide, pawnSq) <= RANK_5)
return ScaleFactor(0); return ScaleFactor(0);
else { else {
Bitboard ray = Bitboard ray =
@ -759,9 +759,9 @@ ScaleFactor KBPKNScalingFunction::apply(const Position &pos) {
Square weakerKingSq = pos.king_square(weakerSide); Square weakerKingSq = pos.king_square(weakerSide);
if(square_file(weakerKingSq) == square_file(pawnSq) if(square_file(weakerKingSq) == square_file(pawnSq)
&& pawn_rank(strongerSide, pawnSq) < pawn_rank(strongerSide, weakerKingSq) && relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
&& (square_color(weakerKingSq) != square_color(strongerBishopSq) && (square_color(weakerKingSq) != square_color(strongerBishopSq)
|| pawn_rank(strongerSide, weakerKingSq) <= RANK_6)) || relative_rank(strongerSide, weakerKingSq) <= RANK_6))
return ScaleFactor(0); return ScaleFactor(0);
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;

View file

@ -650,28 +650,6 @@ namespace {
void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei) { 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 = 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; ei.attackedBy[us][ROOK] |= b;
@ -680,31 +658,55 @@ namespace {
int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus, int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus,
EndgameRookMobilityBonus); EndgameRookMobilityBonus);
// Penalize rooks which are trapped inside a king which has lost the // Rook on 7th rank
// right to castle: Color them = opposite_color(us);
if(mob <= 6 && !ei.pi->file_is_half_open(us, f)) {
Square ksq = p.king_square(us); if ( relative_rank(us, s) == RANK_7
if(square_file(ksq) >= FILE_E && square_file(s) > square_file(ksq) && && relative_rank(us, p.king_square(them)) == RANK_8)
(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 ei.mgValue += Sign[us] * MidgameRookOn7thBonus;
// board? ei.egValue += Sign[us] * EndgameRookOn7thBonus;
if(!(ei.pi->has_open_file_to_right(us, square_file(ksq)))) { }
ei.mgValue -= p.can_castle(us)?
Sign[us] * ((TrappedRookPenalty - mob * 16) / 2) : // Open and half-open files
Sign[us] * (TrappedRookPenalty - mob * 16); 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
else if(square_file(ksq) <= FILE_D && square_file(s) < square_file(ksq) {
&& (pawn_rank(us, ksq) == RANK_1 || ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
square_rank(ksq) == square_rank(s))) { ei.egValue += Sign[us] * RookHalfOpenFileBonus;
// 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);
} }
} }
// 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);
} }
} }
@ -714,21 +716,22 @@ namespace {
void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei) { 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); Bitboard b = p.queen_attacks(s);
ei.attackedBy[us][QUEEN] |= b; ei.attackedBy[us][QUEEN] |= b;
// King attack and mobility // King attack and mobility
evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus, evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus,
EndgameQueenMobilityBonus); EndgameQueenMobilityBonus);
// 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;
}
} }
@ -740,7 +743,7 @@ namespace {
int shelter = 0, sign = Sign[us]; int shelter = 0, sign = Sign[us];
// King shelter. // 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); Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s);
Rank r = square_rank(s); Rank r = square_rank(s);
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
@ -912,7 +915,7 @@ namespace {
assert(pos.piece_on(s) == pawn_of_color(us)); assert(pos.piece_on(s) == pawn_of_color(us));
assert(pos.pawn_is_passed(us, s)); 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)); int tr = Max(0, r * (r-1));
Square blockSq = s + pawn_push(us); Square blockSq = s + pawn_push(us);
@ -977,7 +980,7 @@ namespace {
+ ((us == pos.side_to_move())? 0 : 1); + ((us == pos.side_to_move())? 0 : 1);
if(d < 0) { if(d < 0) {
int mtg = RANK_8 - pawn_rank(us, s); int mtg = RANK_8 - relative_rank(us, s);
int blockerCount = int blockerCount =
count_1s_max_15(squares_in_front_of(us,s)&pos.occupied_squares()); count_1s_max_15(squares_in_front_of(us,s)&pos.occupied_squares());
mtg += blockerCount; mtg += blockerCount;

View file

@ -81,9 +81,9 @@ Move move_from_string(const Position &pos, const std::string &str) {
SquareDelta delta = (to > from)? DELTA_E : DELTA_W; SquareDelta delta = (to > from)? DELTA_E : DELTA_W;
Square s; Square s;
for(s = from + delta; for(s = from + delta;
pawn_rank(us, s) == RANK_1 && pos.piece_on(s) != rook_of_color(us); relative_rank(us, s) == RANK_1 && pos.piece_on(s) != rook_of_color(us);
s += delta); s += delta);
if(pawn_rank(us, s) == RANK_1 && pos.piece_on(s) == rook_of_color(us)) if(relative_rank(us, s) == RANK_1 && pos.piece_on(s) == rook_of_color(us))
return make_castle_move(from, s); return make_castle_move(from, s);
} }
} }

View file

@ -391,7 +391,7 @@ int generate_evasions(const Position &pos, MoveStack *mlist) {
b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned; b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
while(b1) { while(b1) {
from = pop_1st_bit(&b1); from = pop_1st_bit(&b1);
if(pawn_rank(us, checksq) == RANK_8) { if(relative_rank(us, checksq) == RANK_8) {
mlist[n++].move = make_promotion_move(from, checksq, QUEEN); mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
mlist[n++].move = make_promotion_move(from, checksq, ROOK); mlist[n++].move = make_promotion_move(from, checksq, ROOK);
mlist[n++].move = make_promotion_move(from, checksq, BISHOP); mlist[n++].move = make_promotion_move(from, checksq, BISHOP);

View file

@ -364,8 +364,8 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
ev += ChainEndgameBonus[f]; ev += ChainEndgameBonus[f];
} }
if(candidate) { if(candidate) {
mv += CandidateMidgameBonus[pawn_rank(us, s)]; mv += CandidateMidgameBonus[relative_rank(us, s)];
ev += CandidateEndgameBonus[pawn_rank(us, s)]; ev += CandidateEndgameBonus[relative_rank(us, s)];
} }
mgValue[us] += mv; mgValue[us] += mv;

View file

@ -1053,7 +1053,7 @@ void Position::do_promotion_move(Move m, UndoInfo &u) {
from = move_from(m); from = move_from(m);
to = move_to(m); to = move_to(m);
assert(pawn_rank(us, to) == RANK_8); assert(relative_rank(us, to) == RANK_8);
assert(this->piece_on(from) == pawn_of_color(us)); assert(this->piece_on(from) == pawn_of_color(us));
assert(this->color_of_piece_on(to) == them || this->square_is_empty(to)); assert(this->color_of_piece_on(to) == them || this->square_is_empty(to));
@ -1179,7 +1179,7 @@ void Position::do_ep_move(Move m) {
capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S); capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
assert(to == epSquare); assert(to == epSquare);
assert(pawn_rank(us, to) == RANK_6); assert(relative_rank(us, to) == RANK_6);
assert(this->piece_on(to) == EMPTY); assert(this->piece_on(to) == EMPTY);
assert(this->piece_on(from) == pawn_of_color(us)); assert(this->piece_on(from) == pawn_of_color(us));
assert(this->piece_on(capsq) == pawn_of_color(them)); assert(this->piece_on(capsq) == pawn_of_color(them));
@ -1418,7 +1418,7 @@ void Position::undo_promotion_move(Move m, const UndoInfo &u) {
from = move_from(m); from = move_from(m);
to = move_to(m); to = move_to(m);
assert(pawn_rank(us, to) == RANK_8); assert(relative_rank(us, to) == RANK_8);
assert(this->piece_on(from) == EMPTY); assert(this->piece_on(from) == EMPTY);
// Remove promoted piece: // Remove promoted piece:
@ -1500,7 +1500,7 @@ void Position::undo_ep_move(Move m) {
capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S); capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
assert(to == this->ep_square()); assert(to == this->ep_square());
assert(pawn_rank(us, to) == RANK_6); assert(relative_rank(us, to) == RANK_6);
assert(this->piece_on(to) == pawn_of_color(us)); assert(this->piece_on(to) == pawn_of_color(us));
assert(this->piece_on(from) == EMPTY); assert(this->piece_on(from) == EMPTY);
assert(this->piece_on(capsq) == EMPTY); assert(this->piece_on(capsq) == EMPTY);
@ -2182,7 +2182,7 @@ bool Position::is_ok() const {
if(this->ep_square() != SQ_NONE) { if(this->ep_square() != SQ_NONE) {
// The en passant square must be on rank 6, from the point of view of the // The en passant square must be on rank 6, from the point of view of the
// side to move. // side to move.
if(pawn_rank(this->side_to_move(), this->ep_square()) != RANK_6) if(relative_rank(this->side_to_move(), this->ep_square()) != RANK_6)
return false; return false;
} }

View file

@ -718,7 +718,7 @@ inline bool Position::move_is_pawn_push_to_7th(Move m) const {
Color c = this->side_to_move(); Color c = this->side_to_move();
return return
this->piece_on(move_from(m)) == pawn_of_color(c) && this->piece_on(move_from(m)) == pawn_of_color(c) &&
pawn_rank(c, move_to(m)) == RANK_7; relative_rank(c, move_to(m)) == RANK_7;
} }
inline bool Position::move_is_passed_pawn_push(Move m) const { inline bool Position::move_is_passed_pawn_push(Move m) const {

View file

@ -129,7 +129,7 @@ inline Square relative_square(Color c, Square s) {
return Square(int(s) ^ (int(c) * FlipMask)); return Square(int(s) ^ (int(c) * FlipMask));
} }
inline Rank pawn_rank(Color c, Square s) { inline Rank relative_rank(Color c, Square s) {
return square_rank(relative_square(c, s)); return square_rank(relative_square(c, s));
} }