- if ( pos.is_capture(threat)
- && ( PieceValueMidgame[pos.piece_on(tfrom)] >= PieceValueMidgame[pos.piece_on(tto)]
- || type_of(pos.piece_on(tfrom)) == KING)
- && pos.move_attacks_square(m, tto))
- return true;
-
- // Case 3: If the moving piece in the threatened move is a slider, don't
- // prune safe moves which block its ray.
- if ( piece_is_slider(pos.piece_on(tfrom))
- && bit_is_set(squares_between(tfrom, tto), mto)
- && pos.see_sign(m) >= 0)
- return true;
-
- return false;
- }
-
-
- // can_return_tt() returns true if a transposition table score can be used to
- // cut-off at a given point in search.
-
- bool can_return_tt(const TTEntry* tte, Depth depth, Value beta, int ply) {
-
- Value v = value_from_tt(tte->value(), ply);
-
- return ( tte->depth() >= depth
- || v >= std::max(VALUE_MATE_IN_MAX_PLY, beta)
- || v < std::min(VALUE_MATED_IN_MAX_PLY, beta))
-
- && ( ((tte->type() & VALUE_TYPE_LOWER) && v >= beta)
- || ((tte->type() & VALUE_TYPE_UPPER) && v < beta));
- }
-
-
- // refine_eval() returns the transposition table score if possible, otherwise
- // falls back on static position evaluation.
-
- Value refine_eval(const TTEntry* tte, Value defaultEval, int ply) {
-
- assert(tte);
-
- Value v = value_from_tt(tte->value(), ply);
-
- if ( ((tte->type() & VALUE_TYPE_LOWER) && v >= defaultEval)
- || ((tte->type() & VALUE_TYPE_UPPER) && v < defaultEval))
- return v;
-
- return defaultEval;
- }
-
-
- // current_search_time() returns the number of milliseconds which have passed
- // since the beginning of the current search.
-
- int elapsed_time(bool reset) {
-
- static int searchStartTime;
-
- if (reset)
- searchStartTime = system_time();
-
- return system_time() - searchStartTime;
- }
-
-
- // score_to_uci() converts a value to a string suitable for use with the UCI
- // protocol specifications:
- //
- // cp <x> The score from the engine's point of view in centipawns.
- // mate <y> Mate in y moves, not plies. If the engine is getting mated
- // use negative values for y.
-
- string score_to_uci(Value v, Value alpha, Value beta) {
-
- std::stringstream s;
-
- if (abs(v) < VALUE_MATE_IN_MAX_PLY)
- s << "cp " << v * 100 / int(PawnValueMidgame);
- else
- s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
-
- s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
-
- return s.str();
- }
-
-
- // pv_info_to_uci() sends search info to GUI. UCI protocol requires to send all
- // the PV lines also if are still to be searched and so refer to the previous
- // search score.
-
- void pv_info_to_uci(const Position& pos, int depth, Value alpha, Value beta) {
-
- int t = elapsed_time();
- int selDepth = 0;
-
- for (int i = 0; i < Threads.size(); i++)
- if (Threads[i].maxPly > selDepth)
- selDepth = Threads[i].maxPly;
-
- for (size_t i = 0; i < std::min(UCIMultiPV, RootMoves.size()); i++)
- {
- bool updated = (i <= PVIdx);
-
- if (depth == 1 && !updated)
- continue;
-
- int d = (updated ? depth : depth - 1);
- Value v = (updated ? RootMoves[i].score : RootMoves[i].prevScore);
- std::stringstream s;
-
- for (int j = 0; RootMoves[i].pv[j] != MOVE_NONE; j++)
- s << " " << move_to_uci(RootMoves[i].pv[j], Chess960);
-
- cout << "info depth " << d
- << " seldepth " << selDepth
- << " score " << (i == PVIdx ? score_to_uci(v, alpha, beta) : score_to_uci(v))
- << " nodes " << pos.nodes_searched()
- << " nps " << (t > 0 ? pos.nodes_searched() * 1000 / t : 0)
- << " time " << t
- << " multipv " << i + 1
- << " pv" << s.str() << endl;
- }
- }
-
-
- // pv_info_to_log() writes human-readable search information to the log file
- // (which is created when the UCI parameter "Use Search Log" is "true"). It
- // uses the two below helpers to pretty format time and score respectively.
-
- string time_to_string(int millisecs) {
-
- const int MSecMinute = 1000 * 60;
- const int MSecHour = 1000 * 60 * 60;
-
- int hours = millisecs / MSecHour;
- int minutes = (millisecs % MSecHour) / MSecMinute;
- int seconds = ((millisecs % MSecHour) % MSecMinute) / 1000;
-
- std::stringstream s;
-
- if (hours)
- s << hours << ':';
-
- s << std::setfill('0') << std::setw(2) << minutes << ':'
- << std::setw(2) << seconds;
- return s.str();
- }
-
- string score_to_string(Value v) {
-
- std::stringstream s;
-
- if (v >= VALUE_MATE_IN_MAX_PLY)
- s << "#" << (VALUE_MATE - v + 1) / 2;
- else if (v <= VALUE_MATED_IN_MAX_PLY)
- s << "-#" << (VALUE_MATE + v) / 2;
- else
- s << std::setprecision(2) << std::fixed << std::showpos
- << float(v) / PawnValueMidgame;
-
- return s.str();
- }
-
- void pv_info_to_log(Position& pos, int depth, Value value, int time, Move pv[]) {
-
- const int64_t K = 1000;
- const int64_t M = 1000000;
-
- StateInfo state[MAX_PLY_PLUS_2], *st = state;
- Move* m = pv;
- string san, padding;
- size_t length;
- std::stringstream s;
-
- s << std::setw(2) << depth
- << std::setw(8) << score_to_string(value)
- << std::setw(8) << time_to_string(time);
-
- if (pos.nodes_searched() < M)
- s << std::setw(8) << pos.nodes_searched() / 1 << " ";
-
- else if (pos.nodes_searched() < K * M)
- s << std::setw(7) << pos.nodes_searched() / K << "K ";
-
- else
- s << std::setw(7) << pos.nodes_searched() / M << "M ";
-
- padding = string(s.str().length(), ' ');
- length = padding.length();
-
- while (*m != MOVE_NONE)