Skip to content

Commit

Permalink
Merge pull request #3 from mcostalba/master
Browse files Browse the repository at this point in the history
sync with master
  • Loading branch information
snicolet committed Mar 3, 2014
2 parents 5629a2f + 553ead4 commit e4b1b52
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 89 deletions.
16 changes: 8 additions & 8 deletions src/evaluate.cpp
Expand Up @@ -494,16 +494,16 @@ Value do_evaluate(const Position& pos) {
if (ei.attackedBy[Them][PAWN] & s)
score -= ThreatenedByPawn[Pt];

// Penalty for bishop with same coloured pawns
if (Pt == BISHOP)
score -= BishopPawns * ei.pi->pawns_on_same_color_squares(Us, s);

// Penalty for knight when there are few enemy pawns
if (Pt == KNIGHT)
score -= KnightPawns * std::max(5 - pos.count<PAWN>(Them), 0);

if (Pt == BISHOP || Pt == KNIGHT)
{
// Penalty for bishop with same colored pawns
if (Pt == BISHOP)
score -= BishopPawns * ei.pi->pawns_on_same_color_squares(Us, s);

// Penalty for knight when there are few enemy pawns
if (Pt == KNIGHT)
score -= KnightPawns * std::max(5 - pos.count<PAWN>(Them), 0);

// Bishop and knight outposts squares
if (!(pos.pieces(Them, PAWN) & pawn_attack_span(Us, s)))
score += evaluate_outposts<Pt, Us>(pos, ei, s);
Expand Down
22 changes: 12 additions & 10 deletions src/movegen.cpp
Expand Up @@ -31,23 +31,25 @@
(mlist++)->move = make_move(to - (d), to); }
namespace {

template<CastlingSide Side, bool Checks, bool Chess960>
template<CastlingFlag Cf, bool Checks, bool Chess960>
ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {

if (pos.castling_impeded(us, Side) || !pos.can_castle(make_castling_flag(us, Side)))
static const bool KingSide = (Cf == WHITE_OO || Cf == BLACK_OO);

if (pos.castling_impeded(Cf) || !pos.can_castle(Cf))
return mlist;

// After castling, the rook and king final positions are the same in Chess960
// as they would be in standard chess.
Square kfrom = pos.king_square(us);
Square rfrom = pos.castling_rook_square(us, Side);
Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
Square rfrom = pos.castling_rook_square(Cf);
Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
Bitboard enemies = pos.pieces(~us);

assert(!pos.checkers());

const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
: Side == KING_SIDE ? DELTA_W : DELTA_E;
const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
: KingSide ? DELTA_W : DELTA_E;

for (Square s = kto; s != kfrom; s += K)
if (pos.attackers_to(s) & enemies)
Expand Down Expand Up @@ -262,13 +264,13 @@ namespace {
{
if (pos.is_chess960())
{
mlist = generate_castling< KING_SIDE, Checks, true>(pos, mlist, Us, ci);
mlist = generate_castling<QUEEN_SIDE, Checks, true>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, true>(pos, mlist, Us, ci);
}
else
{
mlist = generate_castling< KING_SIDE, Checks, false>(pos, mlist, Us, ci);
mlist = generate_castling<QUEEN_SIDE, Checks, false>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::flag, Checks, false>(pos, mlist, Us, ci);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/notation.cpp
Expand Up @@ -72,7 +72,7 @@ const string move_to_uci(Move m, bool chess960) {
if (type_of(m) == CASTLING && !chess960)
to = (to > from ? FILE_G : FILE_C) | rank_of(from);

string move = square_to_string(from) + square_to_string(to);
string move = to_string(from) + to_string(to);

if (type_of(m) == PROMOTION)
move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
Expand Down Expand Up @@ -140,22 +140,22 @@ const string move_to_san(Position& pos, Move m) {
if (others)
{
if (!(others & file_bb(from)))
san += file_to_char(file_of(from));
san += to_char(file_of(from));

else if (!(others & rank_bb(from)))
san += rank_to_char(rank_of(from));
san += to_char(rank_of(from));

else
san += square_to_string(from);
san += to_string(from);
}
}
else if (pos.capture(m))
san = file_to_char(file_of(from));
san = to_char(file_of(from));

if (pos.capture(m))
san += 'x';

san += square_to_string(to);
san += to_string(to);

if (type_of(m) == PROMOTION)
san += string("=") + PieceToChar[WHITE][promotion_type(m)];
Expand Down
4 changes: 2 additions & 2 deletions src/pawns.cpp
Expand Up @@ -291,10 +291,10 @@ Score Entry::update_safety(const Position& pos, Square ksq) {
Value bonus = shelter_storm<Us>(pos, ksq);

// If we can castle use the bonus after the castling if it is bigger
if (pos.can_castle(make_castling_flag(Us, KING_SIDE)))
if (pos.can_castle(MakeCastling<Us, KING_SIDE>::flag))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));

if (pos.can_castle(make_castling_flag(Us, QUEEN_SIDE)))
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::flag))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));

return kingSafety[Us] = make_score(bonus, -16 * minKPdistance[Us]);
Expand Down
30 changes: 14 additions & 16 deletions src/position.cpp
Expand Up @@ -304,23 +304,23 @@ void Position::set_castling_flag(Color c, Square rfrom) {

Square kfrom = king_square(c);
CastlingSide cs = kfrom < rfrom ? KING_SIDE : QUEEN_SIDE;
CastlingFlag cf = make_castling_flag(c, cs);
CastlingFlag cf = (c | cs);

st->castlingFlags |= cf;
castlingFlagsMask[kfrom] |= cf;
castlingFlagsMask[rfrom] |= cf;
castlingRookSquare[c][cs] = rfrom;
castlingRookSquare[cf] = rfrom;

Square kto = relative_square(c, cs == KING_SIDE ? SQ_G1 : SQ_C1);
Square rto = relative_square(c, cs == KING_SIDE ? SQ_F1 : SQ_D1);

for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); ++s)
if (s != kfrom && s != rfrom)
castlingPath[c][cs] |= s;
castlingPath[cf] |= s;

for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); ++s)
if (s != kfrom && s != rfrom)
castlingPath[c][cs] |= s;
castlingPath[cf] |= s;
}


Expand Down Expand Up @@ -353,21 +353,21 @@ const string Position::fen() const {
ss << (sideToMove == WHITE ? " w " : " b ");

if (can_castle(WHITE_OO))
ss << (chess960 ? file_to_char(file_of(castling_rook_square(WHITE, KING_SIDE)), false) : 'K');
ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE | KING_SIDE)), false) : 'K');

if (can_castle(WHITE_OOO))
ss << (chess960 ? file_to_char(file_of(castling_rook_square(WHITE, QUEEN_SIDE)), false) : 'Q');
ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE | QUEEN_SIDE)), false) : 'Q');

if (can_castle(BLACK_OO))
ss << (chess960 ? file_to_char(file_of(castling_rook_square(BLACK, KING_SIDE)), true) : 'k');
ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK | KING_SIDE)), true) : 'k');

if (can_castle(BLACK_OOO))
ss << (chess960 ? file_to_char(file_of(castling_rook_square(BLACK, QUEEN_SIDE)), true) : 'q');
ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK | QUEEN_SIDE)), true) : 'q');

if (!can_castle(WHITE) && !can_castle(BLACK))
ss << '-';

ss << (ep_square() == SQ_NONE ? " - " : " " + square_to_string(ep_square()) + " ")
ss << (ep_square() == SQ_NONE ? " - " : " " + to_string(ep_square()) + " ")
<< st->rule50 << " " << 1 + (gamePly - int(sideToMove == BLACK)) / 2;

return ss.str();
Expand Down Expand Up @@ -401,7 +401,7 @@ const string Position::pretty(Move move) const {
<< std::setfill('0') << std::setw(16) << st->key << "\nCheckers: ";

for (Bitboard b = checkers(); b; )
ss << square_to_string(pop_lsb(&b)) << " ";
ss << to_string(pop_lsb(&b)) << " ";

ss << "\nLegal moves: ";
for (MoveList<LEGAL> it(*this); *it; ++it)
Expand Down Expand Up @@ -1393,14 +1393,12 @@ bool Position::pos_is_ok(int* failedStep) const {
for (Color c = WHITE; c <= BLACK; ++c)
for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
{
CastlingFlag cf = make_castling_flag(c, s);

if (!can_castle(cf))
if (!can_castle(c | s))
continue;

if ( (castlingFlagsMask[king_square(c)] & cf) != cf
|| piece_on(castlingRookSquare[c][s]) != make_piece(c, ROOK)
|| castlingFlagsMask[castlingRookSquare[c][s]] != cf)
if ( (castlingFlagsMask[king_square(c)] & (c | s)) != (c | s)
|| piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
|| castlingFlagsMask[castlingRookSquare[c | s]] != (c | s))
return false;
}

Expand Down
18 changes: 9 additions & 9 deletions src/position.h
Expand Up @@ -100,10 +100,10 @@ class Position {
template<PieceType Pt> const Square* list(Color c) const;

// Castling
int can_castle(CastlingFlag f) const;
int can_castle(Color c) const;
bool castling_impeded(Color c, CastlingSide s) const;
Square castling_rook_square(Color c, CastlingSide s) const;
int can_castle(CastlingFlag f) const;
bool castling_impeded(CastlingFlag f) const;
Square castling_rook_square(CastlingFlag f) const;

// Checking
Bitboard checkers() const;
Expand Down Expand Up @@ -198,8 +198,8 @@ class Position {

// Other info
int castlingFlagsMask[SQUARE_NB];
Square castlingRookSquare[COLOR_NB][CASTLING_SIDE_NB];
Bitboard castlingPath[COLOR_NB][CASTLING_SIDE_NB];
Square castlingRookSquare[CASTLING_FLAG_NB];
Bitboard castlingPath[CASTLING_FLAG_NB];
StateInfo startState;
uint64_t nodes;
int gamePly;
Expand Down Expand Up @@ -281,12 +281,12 @@ inline int Position::can_castle(Color c) const {
return st->castlingFlags & ((WHITE_OO | WHITE_OOO) << (2 * c));
}

inline bool Position::castling_impeded(Color c, CastlingSide s) const {
return byTypeBB[ALL_PIECES] & castlingPath[c][s];
inline bool Position::castling_impeded(CastlingFlag f) const {
return byTypeBB[ALL_PIECES] & castlingPath[f];
}

inline Square Position::castling_rook_square(Color c, CastlingSide s) const {
return castlingRookSquare[c][s];
inline Square Position::castling_rook_square(CastlingFlag f) const {
return castlingRookSquare[f];
}

template<PieceType Pt>
Expand Down
16 changes: 4 additions & 12 deletions src/search.cpp
Expand Up @@ -185,8 +185,7 @@ void Search::think() {
RootColor = RootPos.side_to_move();
TimeMgr.init(Limits, RootPos.game_ply(), RootColor);

// Dynamic draw value: try to avoid repetition draws at early midgame
int cf = std::max(70 - RootPos.game_ply(), 0);
int cf = Options["Contempt Factor"] * PawnValueMg / 100; // From centipawns
DrawValue[ RootColor] = VALUE_DRAW - Value(cf);
DrawValue[~RootColor] = VALUE_DRAW + Value(cf);

Expand Down Expand Up @@ -392,8 +391,6 @@ namespace {
sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl;
}

Time::point iterationTime = Time::now() - SearchTime;

// If skill levels are enabled and time is up, pick a sub-optimal best move
if (skill.enabled() && skill.time_to_pick(depth))
skill.pick_move();
Expand All @@ -418,19 +415,14 @@ namespace {
// Do we have time for the next iteration? Can we stop searching now?
if (Limits.use_time_management() && !Signals.stop && !Signals.stopOnPonderhit)
{
bool stop = false; // Local variable, not the volatile Signals.stop

// Take some extra time if the best move has changed
if (depth > 4 && depth < 50 && MultiPV == 1)
TimeMgr.pv_instability(BestMoveChanges);

// Stop the search if only one legal move is available or all
// of the available time has been used.
if ( RootMoves.size() == 1
|| iterationTime > TimeMgr.available_time() )
stop = true;

if (stop)
|| Time::now() - SearchTime > TimeMgr.available_time())
{
// If we are allowed to ponder do not stop the search now but
// keep pondering until the GUI sends "ponderhit" or "stop".
Expand Down Expand Up @@ -664,7 +656,7 @@ namespace {
&& abs(beta) < VALUE_MATE_IN_MAX_PLY)
{
Value rbeta = std::min(beta + 200, VALUE_INFINITE);
Depth rdepth = depth - ONE_PLY - 3 * ONE_PLY;
Depth rdepth = depth - 4 * ONE_PLY;

assert(rdepth >= ONE_PLY);
assert((ss-1)->currentMove != MOVE_NONE);
Expand Down Expand Up @@ -1017,7 +1009,7 @@ namespace {
// case of Signals.stop or thread.cutoff_occurred() are set, but this is
// harmless because return value is discarded anyhow in the parent nodes.
// If we are in a singular extension search then return a fail low score.
// A split node has at least one move - the one tried before to be splitted.
// A split node has at least one move - the one tried before to be split.
if (!moveCount)
return excludedMove ? alpha
: inCheck ? mated_in(ss->ply) : DrawValue[pos.side_to_move()];
Expand Down
15 changes: 6 additions & 9 deletions src/thread.cpp
Expand Up @@ -59,7 +59,7 @@ namespace {
}


// ThreadBase::notify_one() wakes up the thread when there is some work to do
// notify_one() wakes up the thread when there is some work to do

void ThreadBase::notify_one() {

Expand All @@ -69,7 +69,7 @@ void ThreadBase::notify_one() {
}


// ThreadBase::wait_for() set the thread to sleep until condition 'b' turns true
// wait_for() set the thread to sleep until condition 'b' turns true

void ThreadBase::wait_for(volatile const bool& b) {

Expand All @@ -92,7 +92,7 @@ Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
}


// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
// cutoff_occurred() checks whether a beta cutoff has occurred in the
// current active split point, or in some ancestor of the split point.

bool Thread::cutoff_occurred() const {
Expand Down Expand Up @@ -219,12 +219,9 @@ void ThreadPool::read_uci_options() {

assert(requested > 0);

// Value 0 has a special meaning: We determine the optimal minimum split depth
// automatically. Anyhow the minimumSplitDepth should never be under 4 plies.
// If zero (default) then set best minimum split depth automatically
if (!minimumSplitDepth)
minimumSplitDepth = (requested < 8 ? 4 : 7) * ONE_PLY;
else
minimumSplitDepth = std::max(4 * ONE_PLY, minimumSplitDepth);
minimumSplitDepth = requested < 8 ? 4 * ONE_PLY : 7 * ONE_PLY;

while (size() < requested)
push_back(new_thread<Thread>());
Expand All @@ -237,7 +234,7 @@ void ThreadPool::read_uci_options() {
}


// slave_available() tries to find an idle thread which is available as a slave
// available_slave() tries to find an idle thread which is available as a slave
// for the thread 'master'.

Thread* ThreadPool::available_slave(const Thread* master) const {
Expand Down
1 change: 0 additions & 1 deletion src/tt.h
Expand Up @@ -32,7 +32,6 @@
/// value: 16 bit
/// depth: 16 bit
/// static value: 16 bit
/// static margin: 16 bit

struct TTEntry {

Expand Down

0 comments on commit e4b1b52

Please sign in to comment.