]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
b2096f5ddda8b74481d6c7424089944d43bc5a4e
[stockfish] / src / ucioption.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-2014 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 <algorithm>
21 #include <cassert>
22 #include <cstdlib>
23 #include <sstream>
24
25 #include "misc.h"
26 #include "thread.h"
27 #include "tt.h"
28 #include "uci.h"
29
30 #ifdef SYZYGY
31 #include "syzygy/tbprobe.h"
32 #endif
33
34 using std::string;
35
36 UCI::OptionsMap Options; // Global object
37
38 namespace UCI {
39
40 /// 'On change' actions, triggered by an option's value change
41 void on_logger(const Option& o) { start_logger(o); }
42 void on_threads(const Option&) { Threads.read_uci_options(); }
43 void on_hash_size(const Option& o) { TT.resize(o); }
44 void on_clear_hash(const Option&) { TT.clear(); }
45 #ifdef SYZYGY
46 void on_tb_path(const Option& o) { Tablebases::init(o); }
47 #endif
48
49
50 /// Our case insensitive less() function as required by UCI protocol
51 bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); }
52
53 bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
54   return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
55 }
56
57
58 /// init() initializes the UCI options to their hard-coded default values
59
60 void init(OptionsMap& o) {
61
62   o["Write Debug Log"]       << Option(false, on_logger);
63   o["Contempt"]              << Option(0, -100, 100);
64   o["Min Split Depth"]       << Option(0, 0, 12, on_threads);
65   o["Threads"]               << Option(1, 1, MAX_THREADS, on_threads);
66   o["Hash"]                  << Option(16, 1, 1024 * 1024, on_hash_size);
67   o["Clear Hash"]            << Option(on_clear_hash);
68   o["Ponder"]                << Option(true);
69   o["MultiPV"]               << Option(1, 1, 500);
70   o["Skill Level"]           << Option(20, 0, 20);
71   o["Move Overhead"]         << Option(30, 0, 5000);
72   o["Minimum Thinking Time"] << Option(20, 0, 5000);
73   o["Slow Mover"]            << Option(80, 10, 1000);
74   o["UCI_Chess960"]          << Option(false);
75 #ifdef SYZYGY
76   o["SyzygyPath"]            << Option("<empty>", on_tb_path);
77   o["SyzygyProbeDepth"]      << Option(1, 1, 100);
78   o["Syzygy50MoveRule"]      << Option(true);
79   o["SyzygyProbeLimit"]      << Option(6, 0, 6);
80 #endif
81 }
82
83
84 /// operator<<() is used to print all the options default values in chronological
85 /// insertion order (the idx field) and in the format defined by the UCI protocol.
86
87 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
88
89   for (size_t idx = 0; idx < om.size(); ++idx)
90       for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
91           if (it->second.idx == idx)
92           {
93               const Option& o = it->second;
94               os << "\noption name " << it->first << " type " << o.type;
95
96               if (o.type != "button")
97                   os << " default " << o.defaultValue;
98
99               if (o.type == "spin")
100                   os << " min " << o.min << " max " << o.max;
101
102               break;
103           }
104   return os;
105 }
106
107
108 /// Option class constructors and conversion operators
109
110 Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), on_change(f)
111 { defaultValue = currentValue = v; }
112
113 Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
114 { defaultValue = currentValue = (v ? "true" : "false"); }
115
116 Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
117 {}
118
119 Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
120 { std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
121
122
123 Option::operator int() const {
124   assert(type == "check" || type == "spin");
125   return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true");
126 }
127
128 Option::operator std::string() const {
129   assert(type == "string");
130   return currentValue;
131 }
132
133
134 /// operator<<() inits options and assigns idx in the correct printing order
135
136 void Option::operator<<(const Option& o) {
137
138   static size_t insert_order = 0;
139
140   *this = o;
141   idx = insert_order++;
142 }
143
144
145 /// operator=() updates currentValue and triggers on_change() action. It's up to
146 /// the GUI to check for option's limits, but we could receive the new value from
147 /// the user by console window, so let's check the bounds anyway.
148
149 Option& Option::operator=(const string& v) {
150
151   assert(!type.empty());
152
153   if (   (type != "button" && v.empty())
154       || (type == "check" && v != "true" && v != "false")
155       || (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max)))
156       return *this;
157
158   if (type != "button")
159       currentValue = v;
160
161   if (on_change)
162       on_change(*this);
163
164   return *this;
165 }
166
167 } // namespace UCI