Original work by Lucas.
No functional change.
result = UNKNOWN;
// Check if two pieces are on the same square or if a king can be captured
result = UNKNOWN;
// Check if two pieces are on the same square or if a king can be captured
- if ( square_distance(wksq, bksq) <= 1
+ if ( distance(wksq, bksq) <= 1
|| wksq == psq
|| bksq == psq
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
|| wksq == psq
|| bksq == psq
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
// Immediate win if a pawn can be promoted without getting captured
if ( rank_of(psq) == RANK_7
&& wksq != psq + DELTA_N
// Immediate win if a pawn can be promoted without getting captured
if ( rank_of(psq) == RANK_7
&& wksq != psq + DELTA_N
- && ( square_distance(bksq, psq + DELTA_N) > 1
+ && ( distance(bksq, psq + DELTA_N) > 1
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
result = WIN;
}
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
result = WIN;
}
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
if (s1 != s2)
{
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
if (s1 != s2)
{
- SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(s1, s2));
+ SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= s2;
}
DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= s2;
}
{
Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
{
Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
- if (is_ok(to) && square_distance(s, to) < 3)
+ if (is_ok(to) && distance(s, to) < 3)
StepAttacksBB[make_piece(c, pt)][s] |= to;
}
StepAttacksBB[make_piece(c, pt)][s] |= to;
}
for (int i = 0; i < 4; ++i)
for (Square s = sq + deltas[i];
for (int i = 0; i < 4; ++i)
for (Square s = sq + deltas[i];
- is_ok(s) && square_distance(s, s - deltas[i]) == 1;
+ is_ok(s) && distance(s, s - deltas[i]) == 1;
s += deltas[i])
{
attack |= s;
s += deltas[i])
{
attack |= s;
-inline int square_distance(Square s1, Square s2) {
- return SquareDistance[s1][s2];
-}
-
-inline int file_distance(Square s1, Square s2) {
- return dist(file_of(s1), file_of(s2));
-}
+template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
+template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
-inline int rank_distance(Square s1, Square s2) {
- return dist(rank_of(s1), rank_of(s2));
-}
+template<typename T1, typename T2> inline int distance(T2 x, T2 y);
+template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
+template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
Value result = pos.non_pawn_material(strongSide)
+ pos.count<PAWN>(strongSide) * PawnValueEg
+ PushToEdges[loserKSq]
Value result = pos.non_pawn_material(strongSide)
+ pos.count<PAWN>(strongSide) * PawnValueEg
+ PushToEdges[loserKSq]
- + PushClose[square_distance(winnerKSq, loserKSq)];
+ + PushClose[distance(winnerKSq, loserKSq)];
if ( pos.count<QUEEN>(strongSide)
|| pos.count<ROOK>(strongSide)
if ( pos.count<QUEEN>(strongSide)
|| pos.count<ROOK>(strongSide)
}
Value result = VALUE_KNOWN_WIN
}
Value result = VALUE_KNOWN_WIN
- + PushClose[square_distance(winnerKSq, loserKSq)]
+ + PushClose[distance(winnerKSq, loserKSq)]
+ PushToCorners[loserKSq];
return strongSide == pos.side_to_move() ? result : -result;
+ PushToCorners[loserKSq];
return strongSide == pos.side_to_move() ? result : -result;
// If the stronger side's king is in front of the pawn, it's a win
if (wksq < psq && file_of(wksq) == file_of(psq))
// If the stronger side's king is in front of the pawn, it's a win
if (wksq < psq && file_of(wksq) == file_of(psq))
- result = RookValueEg - square_distance(wksq, psq);
+ result = RookValueEg - distance(wksq, psq);
// If the weaker side's king is too far from the pawn and the rook,
// it's a win.
// If the weaker side's king is too far from the pawn and the rook,
// it's a win.
- else if ( square_distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide)
- && square_distance(bksq, rsq) >= 3)
- result = RookValueEg - square_distance(wksq, psq);
+ else if ( distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide)
+ && distance(bksq, rsq) >= 3)
+ result = RookValueEg - distance(wksq, psq);
// If the pawn is far advanced and supported by the defending king,
// the position is drawish
else if ( rank_of(bksq) <= RANK_3
// If the pawn is far advanced and supported by the defending king,
// the position is drawish
else if ( rank_of(bksq) <= RANK_3
- && square_distance(bksq, psq) == 1
+ && distance(bksq, psq) == 1
&& rank_of(wksq) >= RANK_4
&& rank_of(wksq) >= RANK_4
- && square_distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide))
- result = Value(80) - 8 * square_distance(wksq, psq);
+ && distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide))
+ result = Value(80) - 8 * distance(wksq, psq);
- result = Value(200) - 8 * ( square_distance(wksq, psq + DELTA_S)
- - square_distance(bksq, psq + DELTA_S)
- - square_distance(psq, queeningSq));
+ result = Value(200) - 8 * ( distance(wksq, psq + DELTA_S)
+ - distance(bksq, psq + DELTA_S)
+ - distance(psq, queeningSq));
return strongSide == pos.side_to_move() ? result : -result;
}
return strongSide == pos.side_to_move() ? result : -result;
}
Square bksq = pos.king_square(weakSide);
Square bnsq = pos.list<KNIGHT>(weakSide)[0];
Square bksq = pos.king_square(weakSide);
Square bnsq = pos.list<KNIGHT>(weakSide)[0];
- Value result = Value(PushToEdges[bksq] + PushAway[square_distance(bksq, bnsq)]);
+ Value result = Value(PushToEdges[bksq] + PushAway[distance(bksq, bnsq)]);
return strongSide == pos.side_to_move() ? result : -result;
}
return strongSide == pos.side_to_move() ? result : -result;
}
Square loserKSq = pos.king_square(weakSide);
Square pawnSq = pos.list<PAWN>(weakSide)[0];
Square loserKSq = pos.king_square(weakSide);
Square pawnSq = pos.list<PAWN>(weakSide)[0];
- Value result = Value(PushClose[square_distance(winnerKSq, loserKSq)]);
+ Value result = Value(PushClose[distance(winnerKSq, loserKSq)]);
if ( relative_rank(weakSide, pawnSq) != RANK_7
if ( relative_rank(weakSide, pawnSq) != RANK_7
- || square_distance(loserKSq, pawnSq) != 1
+ || distance(loserKSq, pawnSq) != 1
|| !((FileABB | FileCBB | FileFBB | FileHBB) & pawnSq))
result += QueenValueEg - PawnValueEg;
|| !((FileABB | FileCBB | FileFBB | FileHBB) & pawnSq))
result += QueenValueEg - PawnValueEg;
Value result = QueenValueEg
- RookValueEg
+ PushToEdges[loserKSq]
Value result = QueenValueEg
- RookValueEg
+ PushToEdges[loserKSq]
- + PushClose[square_distance(winnerKSq, loserKSq)];
+ + PushClose[distance(winnerKSq, loserKSq)];
return strongSide == pos.side_to_move() ? result : -result;
}
return strongSide == pos.side_to_move() ? result : -result;
}
Square kingSq = pos.king_square(weakSide);
if ( opposite_colors(queeningSq, bishopSq)
Square kingSq = pos.king_square(weakSide);
if ( opposite_colors(queeningSq, bishopSq)
- && square_distance(queeningSq, kingSq) <= 1)
+ && distance(queeningSq, kingSq) <= 1)
return SCALE_FACTOR_DRAW;
}
return SCALE_FACTOR_DRAW;
}
&& (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide)))
&& (opposite_colors(bishopSq, weakPawnSq) || pos.count<PAWN>(strongSide) == 1))
{
&& (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide)))
&& (opposite_colors(bishopSq, weakPawnSq) || pos.count<PAWN>(strongSide) == 1))
{
- int strongKingDist = square_distance(weakPawnSq, strongKingSq);
- int weakKingDist = square_distance(weakPawnSq, weakKingSq);
+ int strongKingDist = distance(weakPawnSq, strongKingSq);
+ int weakKingDist = distance(weakPawnSq, weakKingSq);
// It's a draw if the weak king is on its back two ranks, within 2
// squares of the blocking pawn and the strong king is not
// It's a draw if the weak king is on its back two ranks, within 2
// squares of the blocking pawn and the strong king is not
// If the pawn is not too far advanced and the defending king defends the
// queening square, use the third-rank defence.
if ( r <= RANK_5
// If the pawn is not too far advanced and the defending king defends the
// queening square, use the third-rank defence.
if ( r <= RANK_5
- && square_distance(bksq, queeningSq) <= 1
+ && distance(bksq, queeningSq) <= 1
&& wksq <= SQ_H5
&& (rank_of(brsq) == RANK_6 || (r <= RANK_3 && rank_of(wrsq) != RANK_6)))
return SCALE_FACTOR_DRAW;
&& wksq <= SQ_H5
&& (rank_of(brsq) == RANK_6 || (r <= RANK_3 && rank_of(wrsq) != RANK_6)))
return SCALE_FACTOR_DRAW;
// The defending side saves a draw by checking from behind in case the pawn
// has advanced to the 6th rank with the king behind.
if ( r == RANK_6
// The defending side saves a draw by checking from behind in case the pawn
// has advanced to the 6th rank with the king behind.
if ( r == RANK_6
- && square_distance(bksq, queeningSq) <= 1
+ && distance(bksq, queeningSq) <= 1
&& rank_of(wksq) + tempo <= RANK_6
&& rank_of(wksq) + tempo <= RANK_6
- && (rank_of(brsq) == RANK_1 || (!tempo && dist(file_of(brsq), f) >= 3)))
+ && (rank_of(brsq) == RANK_1 || (!tempo && distance(file_of(brsq), f) >= 3)))
return SCALE_FACTOR_DRAW;
if ( r >= RANK_6
&& bksq == queeningSq
&& rank_of(brsq) == RANK_1
return SCALE_FACTOR_DRAW;
if ( r >= RANK_6
&& bksq == queeningSq
&& rank_of(brsq) == RANK_1
- && (!tempo || square_distance(wksq, wpsq) >= 2))
+ && (!tempo || distance(wksq, wpsq) >= 2))
return SCALE_FACTOR_DRAW;
// White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7
return SCALE_FACTOR_DRAW;
// White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7
// away, it's a draw.
if ( r <= RANK_5
&& bksq == wpsq + DELTA_N
// away, it's a draw.
if ( r <= RANK_5
&& bksq == wpsq + DELTA_N
- && square_distance(wksq, wpsq) - tempo >= 2
- && square_distance(wksq, brsq) - tempo >= 2)
+ && distance(wksq, wpsq) - tempo >= 2
+ && distance(wksq, brsq) - tempo >= 2)
return SCALE_FACTOR_DRAW;
// Pawn on the 7th rank supported by the rook from behind usually wins if the
return SCALE_FACTOR_DRAW;
// Pawn on the 7th rank supported by the rook from behind usually wins if the
&& f != FILE_A
&& file_of(wrsq) == f
&& wrsq != queeningSq
&& f != FILE_A
&& file_of(wrsq) == f
&& wrsq != queeningSq
- && (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo)
- && (square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo))
- return ScaleFactor(SCALE_FACTOR_MAX - 2 * square_distance(wksq, queeningSq));
+ && (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo)
+ && (distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo))
+ return ScaleFactor(SCALE_FACTOR_MAX - 2 * distance(wksq, queeningSq));
// Similar to the above, but with the pawn further back
if ( f != FILE_A
&& file_of(wrsq) == f
&& wrsq < wpsq
// Similar to the above, but with the pawn further back
if ( f != FILE_A
&& file_of(wrsq) == f
&& wrsq < wpsq
- && (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo)
- && (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wpsq + DELTA_N) - 2 + tempo)
- && ( square_distance(bksq, wrsq) + tempo >= 3
- || ( square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo
- && (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wrsq) + tempo))))
+ && (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo)
+ && (distance(wksq, wpsq + DELTA_N) < distance(bksq, wpsq + DELTA_N) - 2 + tempo)
+ && ( distance(bksq, wrsq) + tempo >= 3
+ || ( distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo
+ && (distance(wksq, wpsq + DELTA_N) < distance(bksq, wrsq) + tempo))))
return ScaleFactor( SCALE_FACTOR_MAX
return ScaleFactor( SCALE_FACTOR_MAX
- - 8 * square_distance(wpsq, queeningSq)
- - 2 * square_distance(wksq, queeningSq));
+ - 8 * distance(wpsq, queeningSq)
+ - 2 * distance(wksq, queeningSq));
// If the pawn is not far advanced and the defending king is somewhere in
// the pawn's path, it's probably a draw.
// If the pawn is not far advanced and the defending king is somewhere in
// the pawn's path, it's probably a draw.
{
if (file_of(bksq) == file_of(wpsq))
return ScaleFactor(10);
{
if (file_of(bksq) == file_of(wpsq))
return ScaleFactor(10);
- if ( dist(file_of(bksq), file_of(wpsq)) == 1
- && square_distance(wksq, bksq) > 2)
- return ScaleFactor(24 - 2 * square_distance(wksq, bksq));
+ if ( distance<File>(bksq, wpsq) == 1
+ && distance(wksq, bksq) > 2)
+ return ScaleFactor(24 - 2 * distance(wksq, bksq));
}
return SCALE_FACTOR_NONE;
}
}
return SCALE_FACTOR_NONE;
}
// corner but not trapped there.
if (rk == RANK_5 && !opposite_colors(bsq, psq))
{
// corner but not trapped there.
if (rk == RANK_5 && !opposite_colors(bsq, psq))
{
- int d = square_distance(psq + 3 * push, ksq);
+ int d = distance(psq + 3 * push, ksq);
if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongSide) + 2 * push))
return ScaleFactor(24);
if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongSide) + 2 * push))
return ScaleFactor(24);
// pawn from a reasonable distance and the defending king is near
// the corner
if ( rk == RANK_6
// pawn from a reasonable distance and the defending king is near
// the corner
if ( rk == RANK_6
- && square_distance(psq + 2 * push, ksq) <= 1
+ && distance(psq + 2 * push, ksq) <= 1
&& (PseudoAttacks[BISHOP][bsq] & (psq + push))
&& (PseudoAttacks[BISHOP][bsq] & (psq + push))
- && file_distance(bsq, psq) >= 2)
+ && distance<File>(bsq, psq) >= 2)
Rank r = std::max(relative_rank(strongSide, wpsq1), relative_rank(strongSide, wpsq2));
Rank r = std::max(relative_rank(strongSide, wpsq1), relative_rank(strongSide, wpsq2));
- if ( file_distance(bksq, wpsq1) <= 1
- && file_distance(bksq, wpsq2) <= 1
+ if ( distance<File>(bksq, wpsq1) <= 1
+ && distance<File>(bksq, wpsq2) <= 1
&& relative_rank(strongSide, bksq) > r)
{
switch (r) {
&& relative_rank(strongSide, bksq) > r)
{
switch (r) {
// the king is within one file of the pawns, it's a draw.
if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq)))
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
// the king is within one file of the pawns, it's a draw.
if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq)))
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
- && file_distance(ksq, psq) <= 1)
+ && distance<File>(ksq, psq) <= 1)
return SCALE_FACTOR_DRAW;
return SCALE_FACTOR_NONE;
return SCALE_FACTOR_DRAW;
return SCALE_FACTOR_NONE;
return SCALE_FACTOR_DRAW;
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
return SCALE_FACTOR_DRAW;
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
- && square_distance(weakBishopSq, pawnSq) >= 3)
+ && distance(weakBishopSq, pawnSq) >= 3)
return SCALE_FACTOR_DRAW;
}
}
return SCALE_FACTOR_DRAW;
}
}
blockSq2 = make_square(file_of(psq1), rank_of(psq2));
}
blockSq2 = make_square(file_of(psq1), rank_of(psq2));
}
- switch (file_distance(psq1, psq2))
+ switch (distance<File>(psq1, psq2))
{
case 0:
// Both pawns are on the same file. It's an easy draw if the defender firmly
{
case 0:
// Both pawns are on the same file. It's an easy draw if the defender firmly
&& opposite_colors(ksq, wbsq)
&& ( bbsq == blockSq2
|| (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(weakSide, BISHOP))
&& opposite_colors(ksq, wbsq)
&& ( bbsq == blockSq2
|| (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(weakSide, BISHOP))
+ || distance(r1, r2) >= 2))
return SCALE_FACTOR_DRAW;
else if ( ksq == blockSq2
return SCALE_FACTOR_DRAW;
else if ( ksq == blockSq2
Square pawnSq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
Square weakKingSq = normalize(pos, strongSide, pos.king_square(weakSide));
Square pawnSq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
Square weakKingSq = normalize(pos, strongSide, pos.king_square(weakSide));
- if (pawnSq == SQ_A7 && square_distance(SQ_A8, weakKingSq) <= 1)
+ if (pawnSq == SQ_A7 && distance(SQ_A8, weakKingSq) <= 1)
return SCALE_FACTOR_DRAW;
return SCALE_FACTOR_NONE;
return SCALE_FACTOR_DRAW;
return SCALE_FACTOR_NONE;
// King needs to get close to promoting pawn to prevent knight from blocking.
// Rules for this are very tricky, so just approximate.
if (forward_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
// King needs to get close to promoting pawn to prevent knight from blocking.
// Rules for this are very tricky, so just approximate.
if (forward_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
- return ScaleFactor(square_distance(weakKingSq, pawnSq));
+ return ScaleFactor(distance(weakKingSq, pawnSq));
return SCALE_FACTOR_NONE;
}
return SCALE_FACTOR_NONE;
}
Square blockSq = s + pawn_push(Us);
// Adjust bonus based on the king's proximity
Square blockSq = s + pawn_push(Us);
// Adjust bonus based on the king's proximity
- ebonus += square_distance(pos.king_square(Them), blockSq) * 5 * rr
- - square_distance(pos.king_square(Us ), blockSq) * 2 * rr;
+ ebonus += distance(pos.king_square(Them), blockSq) * 5 * rr
+ - distance(pos.king_square(Us ), blockSq) * 2 * rr;
// If blockSq is not the queening square then consider also a second push
if (relative_rank(Us, blockSq) != RANK_8)
// If blockSq is not the queening square then consider also a second push
if (relative_rank(Us, blockSq) != RANK_8)
- ebonus -= square_distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr;
+ ebonus -= distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr;
// If the pawn is free to advance, then increase the bonus
if (pos.empty(blockSq))
// If the pawn is free to advance, then increase the bonus
if (pos.empty(blockSq))
value -= UnsupportedPawnPenalty;
if (doubled)
value -= UnsupportedPawnPenalty;
if (doubled)
- value -= Doubled[f] / rank_distance(s, lsb(doubled));
+ value -= Doubled[f] / distance<Rank>(s, Square(lsb(doubled)));
if (backward)
value -= Backward[opposed][f];
if (backward)
value -= Backward[opposed][f];
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
};
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
};
-template <typename T> inline T dist(T x, T y) { return x < y ? y - x : x - y; }
/// The Score enum stores a middlegame and an endgame value in a single integer
/// (enum). The least significant 16 bits are used to store the endgame value
/// The Score enum stores a middlegame and an endgame value in a single integer
/// (enum). The least significant 16 bits are used to store the endgame value