- // update_history() registers a good move that produced a beta-cutoff
- // in history and marks as failures all the other moves of that ply.
-
- void update_history(const Position& pos, Move move, Depth depth,
- Move movesSearched[], int moveCount) {
- Move m;
-
- H.success(pos.piece_on(move_from(move)), move_to(move), depth);
-
- for (int i = 0; i < moveCount - 1; i++)
- {
- m = movesSearched[i];
-
- assert(m != move);
-
- if (!pos.move_is_capture_or_promotion(m))
- H.failure(pos.piece_on(move_from(m)), move_to(m), depth);
- }
- }
-
-
- // update_killers() add a good move that produced a beta-cutoff
- // among the killer moves of that ply.
-
- void update_killers(Move m, SearchStack* ss) {
-
- if (m == ss->killers[0])
- return;
-
- ss->killers[1] = ss->killers[0];
- ss->killers[0] = m;
- }
-
-
- // update_gains() updates the gains table of a non-capture move given
- // the static position evaluation before and after the move.
-
- void update_gains(const Position& pos, Move m, Value before, Value after) {
-
- if ( m != MOVE_NULL
- && before != VALUE_NONE
- && after != VALUE_NONE
- && pos.captured_piece_type() == PIECE_TYPE_NONE
- && !move_is_special(m))
- H.set_gain(pos.piece_on(move_to(m)), move_to(m), -(before + after));
- }
-
-
- // current_search_time() returns the number of milliseconds which have passed
- // since the beginning of the current search.
-
- int current_search_time() {
-
- return get_system_time() - SearchStartTime;
- }
-
-
- // value_to_uci() converts a value to a string suitable for use with the UCI protocol
-
- std::string value_to_uci(Value v) {
-
- std::stringstream s;
-
- if (abs(v) < VALUE_MATE - PLY_MAX * ONE_PLY)
- s << "cp " << int(v) * 100 / int(PawnValueMidgame); // Scale to pawn = 100
- else
- s << "mate " << (v > 0 ? (VALUE_MATE - v + 1) / 2 : -(VALUE_MATE + v) / 2 );
-
- return s.str();
- }
-
- // nps() computes the current nodes/second count.
-
- int nps() {
-
- int t = current_search_time();
- return (t > 0 ? int((ThreadsMgr.nodes_searched() * 1000) / t) : 0);
- }
-
-
- // poll() performs two different functions: It polls for user input, and it
- // looks at the time consumed so far and decides if it's time to abort the
- // search.
-
- void poll() {
-
- static int lastInfoTime;
- int t = current_search_time();
-
- // Poll for input
- if (Bioskey())
- {
- // We are line oriented, don't read single chars
- std::string command;
-
- if (!std::getline(std::cin, command))
- command = "quit";
-
- if (command == "quit")
- {
- AbortSearch = true;
- PonderSearch = false;
- Quit = true;
- return;
- }
- else if (command == "stop")
- {
- AbortSearch = true;
- PonderSearch = false;
- }
- else if (command == "ponderhit")
- ponderhit();
- }
-
- // Print search information
- if (t < 1000)
- lastInfoTime = 0;
-
- else if (lastInfoTime > t)
- // HACK: Must be a new search where we searched less than
- // NodesBetweenPolls nodes during the first second of search.
- lastInfoTime = 0;
-
- else if (t - lastInfoTime >= 1000)
- {
- lastInfoTime = t;
-
- if (dbg_show_mean)
- dbg_print_mean();
-
- if (dbg_show_hit_rate)
- dbg_print_hit_rate();
-
- cout << "info nodes " << ThreadsMgr.nodes_searched() << " nps " << nps()
- << " time " << t << endl;
- }
-
- // Should we stop the search?
- if (PonderSearch)
- return;
-
- bool stillAtFirstMove = FirstRootMove
- && !AspirationFailLow
- && t > TimeMgr.available_time();
-
- bool noMoreTime = t > TimeMgr.maximum_time()
- || stillAtFirstMove;
-
- if ( (Iteration >= 3 && UseTimeManagement && noMoreTime)
- || (ExactMaxTime && t >= ExactMaxTime)
- || (Iteration >= 3 && MaxNodes && ThreadsMgr.nodes_searched() >= MaxNodes))
- AbortSearch = true;
- }
-
-
- // ponderhit() is called when the program is pondering (i.e. thinking while
- // it's the opponent's turn to move) in order to let the engine know that
- // it correctly predicted the opponent's move.
-
- void ponderhit() {
-
- int t = current_search_time();
- PonderSearch = false;
-
- bool stillAtFirstMove = FirstRootMove
- && !AspirationFailLow
- && t > TimeMgr.available_time();
-
- bool noMoreTime = t > TimeMgr.maximum_time()
- || stillAtFirstMove;
-
- if (Iteration >= 3 && UseTimeManagement && (noMoreTime || StopOnPonderhit))
- AbortSearch = true;
- }
-
-
- // init_ss_array() does a fast reset of the first entries of a SearchStack
- // array and of all the excludedMove and skipNullMove entries.
-
- void init_ss_array(SearchStack* ss, int size) {
-
- for (int i = 0; i < size; i++, ss++)
- {
- ss->excludedMove = MOVE_NONE;
- ss->skipNullMove = false;
- ss->reduction = DEPTH_ZERO;
- ss->sp = NULL;