]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Revert "Halve king eval margin"
[stockfish] / src / uci.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <iomanip>
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24
25 #include "evaluate.h"
26 #include "notation.h"
27 #include "position.h"
28 #include "search.h"
29 #include "thread.h"
30 #include "tt.h"
31 #include "ucioption.h"
32
33 using namespace std;
34
35 extern void benchmark(const Position& pos, istream& is);
36
37 namespace {
38
39   // FEN string of the initial position, normal chess
40   const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
41
42   // Keep track of position keys along the setup moves (from start position to the
43   // position just before to start searching). Needed by repetition draw detection.
44   Search::StateStackPtr SetupStates;
45
46   void setoption(istringstream& up);
47   void position(Position& pos, istringstream& up);
48   void go(const Position& pos, istringstream& up);
49 }
50
51
52 /// Wait for a command from the user, parse this text string as an UCI command,
53 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
54 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
55 /// commands, the function also supports a few debug commands.
56
57 void UCI::loop(const string& args) {
58
59   Position pos(StartFEN, false, Threads.main_thread()); // The root position
60   string token, cmd = args;
61
62   do {
63       if (args.empty() && !getline(cin, cmd)) // Block here waiting for input
64           cmd = "quit";
65
66       istringstream is(cmd);
67
68       is >> skipws >> token;
69
70       if (token == "quit" || token == "stop" || token == "ponderhit")
71       {
72           // GUI sends 'ponderhit' to tell us to ponder on the same move the
73           // opponent has played. In case Signals.stopOnPonderhit is set we are
74           // waiting for 'ponderhit' to stop the search (for instance because we
75           // already ran out of time), otherwise we should continue searching but
76           // switching from pondering to normal search.
77           if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
78           {
79               Search::Signals.stop = true;
80               Threads.main_thread()->notify_one(); // Could be sleeping
81           }
82           else
83               Search::Limits.ponder = false;
84       }
85       else if (token == "perft" && (is >> token)) // Read perft depth
86       {
87           stringstream ss;
88
89           ss << Options["Hash"]    << " "
90              << Options["Threads"] << " " << token << " current perft";
91
92           benchmark(pos, ss);
93       }
94       else if (token == "key")
95           sync_cout << hex << uppercase << setfill('0')
96                     << "position key: "   << setw(16) << pos.key()
97                     << "\nmaterial key: " << setw(16) << pos.material_key()
98                     << "\npawn key:     " << setw(16) << pos.pawn_key()
99                     << dec << sync_endl;
100
101       else if (token == "uci")
102           sync_cout << "id name " << engine_info(true)
103                     << "\n"       << Options
104                     << "\nuciok"  << sync_endl;
105
106       else if (token == "ucinewgame") TT.clear();
107       else if (token == "go")         go(pos, is);
108       else if (token == "position")   position(pos, is);
109       else if (token == "setoption")  setoption(is);
110       else if (token == "flip")       pos.flip();
111       else if (token == "bench")      benchmark(pos, is);
112       else if (token == "d")          sync_cout << pos.pretty() << sync_endl;
113       else if (token == "isready")    sync_cout << "readyok" << sync_endl;
114       else if (token == "eval")       sync_cout << Eval::trace(pos) << sync_endl;
115       else
116           sync_cout << "Unknown command: " << cmd << sync_endl;
117
118   } while (token != "quit" && args.empty()); // Args have one-shot behaviour
119
120   Threads.wait_for_think_finished(); // Cannot quit while search is running
121 }
122
123
124 namespace {
125
126   // position() is called when engine receives the "position" UCI command.
127   // The function sets up the position described in the given fen string ("fen")
128   // or the starting position ("startpos") and then makes the moves given in the
129   // following move list ("moves").
130
131   void position(Position& pos, istringstream& is) {
132
133     Move m;
134     string token, fen;
135
136     is >> token;
137
138     if (token == "startpos")
139     {
140         fen = StartFEN;
141         is >> token; // Consume "moves" token if any
142     }
143     else if (token == "fen")
144         while (is >> token && token != "moves")
145             fen += token + " ";
146     else
147         return;
148
149     pos.set(fen, Options["UCI_Chess960"], Threads.main_thread());
150     SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
151
152     // Parse move list (if any)
153     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
154     {
155         SetupStates->push(StateInfo());
156         pos.do_move(m, SetupStates->top());
157     }
158   }
159
160
161   // setoption() is called when engine receives the "setoption" UCI command. The
162   // function updates the UCI option ("name") to the given value ("value").
163
164   void setoption(istringstream& is) {
165
166     string token, name, value;
167
168     is >> token; // Consume "name" token
169
170     // Read option name (can contain spaces)
171     while (is >> token && token != "value")
172         name += string(" ", !name.empty()) + token;
173
174     // Read option value (can contain spaces)
175     while (is >> token)
176         value += string(" ", !value.empty()) + token;
177
178     if (Options.count(name))
179         Options[name] = value;
180     else
181         sync_cout << "No such option: " << name << sync_endl;
182   }
183
184
185   // go() is called when engine receives the "go" UCI command. The function sets
186   // the thinking time and other parameters from the input string, and starts
187   // the search.
188
189   void go(const Position& pos, istringstream& is) {
190
191     Search::LimitsType limits;
192     vector<Move> searchMoves;
193     string token;
194
195     while (is >> token)
196     {
197         if (token == "searchmoves")
198             while (is >> token)
199                 searchMoves.push_back(move_from_uci(pos, token));
200
201         else if (token == "wtime")     is >> limits.time[WHITE];
202         else if (token == "btime")     is >> limits.time[BLACK];
203         else if (token == "winc")      is >> limits.inc[WHITE];
204         else if (token == "binc")      is >> limits.inc[BLACK];
205         else if (token == "movestogo") is >> limits.movestogo;
206         else if (token == "depth")     is >> limits.depth;
207         else if (token == "nodes")     is >> limits.nodes;
208         else if (token == "movetime")  is >> limits.movetime;
209         else if (token == "mate")      is >> limits.mate;
210         else if (token == "infinite")  limits.infinite = true;
211         else if (token == "ponder")    limits.ponder = true;
212     }
213
214     Threads.start_thinking(pos, limits, searchMoves, SetupStates);
215   }
216 }