From f56af8e84db25c0d26fe762fbe171ec5518177bb Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Wed, 24 Sep 2008 16:45:19 +0200 Subject: [PATCH] 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 --- src/endgame.cpp | 20 ++++---- src/evaluate.cpp | 117 ++++++++++++++++++++++++----------------------- src/move.cpp | 4 +- src/movegen.cpp | 2 +- src/pawns.cpp | 4 +- src/position.cpp | 10 ++-- src/position.h | 2 +- src/square.h | 2 +- 8 files changed, 82 insertions(+), 79 deletions(-) diff --git a/src/endgame.cpp b/src/endgame.cpp index c320ba30..7a47a3e1 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -464,7 +464,7 @@ ScaleFactor KBPKScalingFunction::apply(const Position &pos) { // If the defending king has distance 1 to the promotion square or // is placed somewhere in front of the pawn, it's a draw. if(square_distance(kingSq, queeningSq) <= 1 || - pawn_rank(strongerSide, kingSq) >= rank) + relative_rank(strongerSide, kingSq) >= rank) return ScaleFactor(0); } } @@ -485,8 +485,8 @@ ScaleFactor KQKRPScalingFunction::apply(const Position &pos) { assert(pos.pawn_count(weakerSide) >= 1); Square kingSq = pos.king_square(weakerSide); - if(pawn_rank(weakerSide, kingSq) <= RANK_2 && - pawn_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4 && + if(relative_rank(weakerSide, kingSq) <= RANK_2 && + relative_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4 && (pos.rooks(weakerSide) & relative_rank_bb(weakerSide, RANK_3)) && (pos.pawns(weakerSide) & relative_rank_bb(weakerSide, RANK_2)) && (pos.king_attacks(kingSq) & pos.pawns(weakerSide))) { @@ -626,10 +626,10 @@ ScaleFactor KRPPKRPScalingFunction::apply(const Position &pos) { pos.pawn_is_passed(strongerSide, wpsq2)) 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 - && pawn_rank(strongerSide, bksq) > r) { + && relative_rank(strongerSide, bksq) > r) { switch(r) { 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. 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) - || pawn_rank(strongerSide, weakerKingSq) <= RANK_6)) + || relative_rank(strongerSide, weakerKingSq) <= RANK_6)) return ScaleFactor(0); // 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 // reasonably well. - if(pawn_rank(strongerSide, pawnSq) <= RANK_5) + if(relative_rank(strongerSide, pawnSq) <= RANK_5) return ScaleFactor(0); else { Bitboard ray = @@ -759,9 +759,9 @@ ScaleFactor KBPKNScalingFunction::apply(const Position &pos) { Square weakerKingSq = pos.king_square(weakerSide); 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) - || pawn_rank(strongerSide, weakerKingSq) <= RANK_6)) + || relative_rank(strongerSide, weakerKingSq) <= RANK_6)) return ScaleFactor(0); return SCALE_FACTOR_NONE; diff --git a/src/evaluate.cpp b/src/evaluate.cpp index d5cabaeb..f4b7fb75 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -650,28 +650,6 @@ namespace { 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)); ei.attackedBy[us][ROOK] |= b; @@ -680,31 +658,55 @@ namespace { int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus, EndgameRookMobilityBonus); - // 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); + // 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; + } + + // 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); } } @@ -714,21 +716,22 @@ namespace { 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 and mobility evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus, 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]; // 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++) @@ -912,7 +915,7 @@ namespace { 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); @@ -977,7 +980,7 @@ namespace { + ((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; diff --git a/src/move.cpp b/src/move.cpp index 2de102df..d09c3052 100644 --- a/src/move.cpp +++ b/src/move.cpp @@ -81,9 +81,9 @@ Move move_from_string(const Position &pos, const std::string &str) { SquareDelta delta = (to > from)? DELTA_E : DELTA_W; Square s; 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); - 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); } } diff --git a/src/movegen.cpp b/src/movegen.cpp index ecdcd8fb..abcafc00 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -391,7 +391,7 @@ int generate_evasions(const Position &pos, MoveStack *mlist) { b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned; while(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, ROOK); mlist[n++].move = make_promotion_move(from, checksq, BISHOP); diff --git a/src/pawns.cpp b/src/pawns.cpp index 19a37ee3..e8b509cf 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -364,8 +364,8 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) { ev += ChainEndgameBonus[f]; } if(candidate) { - mv += CandidateMidgameBonus[pawn_rank(us, s)]; - ev += CandidateEndgameBonus[pawn_rank(us, s)]; + mv += CandidateMidgameBonus[relative_rank(us, s)]; + ev += CandidateEndgameBonus[relative_rank(us, s)]; } mgValue[us] += mv; diff --git a/src/position.cpp b/src/position.cpp index a9bf1f12..e73e1e5d 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1053,7 +1053,7 @@ void Position::do_promotion_move(Move m, UndoInfo &u) { from = move_from(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->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); 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(from) == pawn_of_color(us)); 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); to = move_to(m); - assert(pawn_rank(us, to) == RANK_8); + assert(relative_rank(us, to) == RANK_8); assert(this->piece_on(from) == EMPTY); // Remove promoted piece: @@ -1500,7 +1500,7 @@ void Position::undo_ep_move(Move m) { capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S); 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(from) == EMPTY); assert(this->piece_on(capsq) == EMPTY); @@ -2182,7 +2182,7 @@ bool Position::is_ok() const { if(this->ep_square() != SQ_NONE) { // The en passant square must be on rank 6, from the point of view of the // 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; } diff --git a/src/position.h b/src/position.h index ab2b21de..80d23602 100644 --- a/src/position.h +++ b/src/position.h @@ -718,7 +718,7 @@ inline bool Position::move_is_pawn_push_to_7th(Move m) const { Color c = this->side_to_move(); return 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 { diff --git a/src/square.h b/src/square.h index 3afa127a..c158c89e 100644 --- a/src/square.h +++ b/src/square.h @@ -129,7 +129,7 @@ inline Square relative_square(Color c, Square s) { 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)); } -- 2.39.2