-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngineBase.cpp
More file actions
281 lines (220 loc) · 9.74 KB
/
EngineBase.cpp
File metadata and controls
281 lines (220 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* ======================================================================================
*
* ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
* ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
* ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
* ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
*
* ======================================================================================
*/
#include <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <expected>
#include <format>
#include <iostream>
#include <libchess/notation/FEN.hpp>
#include <libchess/uci/CommandParsing.hpp>
#include <libchess/uci/EngineBase.hpp>
#include <libchess/uci/Options.hpp>
#include <libchess/uci/Printing.hpp>
#include <libutil/Strings.hpp>
#include <print>
#include <ranges>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace chess::uci {
using printing::info_string;
using std::cout;
using std::memory_order_acquire;
using std::memory_order_relaxed;
using std::memory_order_release;
using std::println;
using std::string_view;
using util::strings::split_at_first_space;
using util::strings::trim;
// NB. With our println calls, we explicitly provide std::cout as the first parameter, because
// when the first parameter is omitted, the default overload writes to the FILE* stdout, but we
// want to use C++ iostreams for all our I/O so that our logging facility works correctly
// defined out-of-line to address -Wweak-vtables
EngineBase::~EngineBase() = default;
namespace {
// returns name of closest known command
[[nodiscard]] auto find_nearest_command(
const string_view input, const EngineBase::CommandList standardCommands, const EngineBase::CommandList customCommands)
-> string_view
{
// map commands to pair of: command name, Levenshtein distance from input
const auto mapped
= std::views::join(std::array { standardCommands, customCommands })
| std::views::transform([input](const EngineCommand& command) {
return std::make_pair(
command.name,
util::strings::levenshtein_distance(input, command.name));
})
| std::ranges::to<std::vector>();
const auto closest = std::ranges::min(
mapped, std::ranges::less { }, [](const auto& item) { return item.second; });
return closest.first;
}
} // namespace
void EngineBase::handle_command(const string_view command)
{
auto [firstWord, rest] = split_at_first_space(command);
firstWord = trim(firstWord);
rest = trim(rest);
if (firstWord.empty())
return;
if (const auto it = std::ranges::find(standardUCICommands, firstWord, &EngineCommand::name);
it != standardUCICommands.end()) {
it->action(rest);
return;
}
const auto customCommands = get_custom_uci_commands();
if (const auto it = std::ranges::find(customCommands, firstWord, &EngineCommand::name);
it != customCommands.end()) {
it->action(rest);
return;
}
info_string(std::format(
"Unknown UCI command: '{}'", firstWord));
info_string(std::format(
"The closest known command is: {}",
find_nearest_command(firstWord, standardUCICommands, customCommands)));
}
void EngineBase::respond_to_uci()
{
// this command is sent once after program boot
println(cout, "id name {}", get_name());
println(cout, "id author {}", get_author());
for (const auto* option : get_custom_uci_options())
println(cout, "{}", option->get_declaration_string());
println(cout, "uciok");
cout.flush();
}
void EngineBase::respond_to_isready()
{
// reply immediately if search is in progress
// if not searching, wait on any background tasks before replying
if (not is_searching())
wait();
println(cout, "readyok");
cout.flush();
}
void EngineBase::respond_to_newgame()
{
const bool wasInitialized = initialized.exchange(true, memory_order_relaxed);
new_game(not wasInitialized);
}
void EngineBase::handle_quit()
{
abort_search();
shouldExit.store(true, memory_order_release);
wait();
}
void EngineBase::handle_setpos(const string_view arguments)
{
// According to the UCI spec, engines should ignore invalid commands.
// If the FEN or movelist sent is invalid, we could terminate the engine
// with an error exit code; however, it seems to be the most spec-compliant
// behavior to ignore the invalid command and not terminate the engine.
// If parsing returns an error, or if the new position seems to be illegal,
// we print an error message via `info string` and keep the old position.
// See this Stockfish PR discussion: https://github.com/official-stockfish/Stockfish/pull/4563
[[maybe_unused]] const auto obj
= parse_position_options(arguments)
.and_then([this](const Position& pos) -> std::expected<void, std::string> {
if (sanitizeIncomingPositions.load(memory_order_acquire)) {
if (const auto errorStr = pos.is_illegal()) {
[[unlikely]];
return std::unexpected {
std::format("Position is illegal: {}", errorStr.value())
};
}
}
position = pos;
set_position(pos);
return { };
})
.transform_error([this](const string_view error) {
info_string(std::format("Error setting position: {}", error));
info_string(std::format("Retained previous position: {}", notation::to_fen(position)));
return std::monostate { };
});
}
namespace {
// returns name of closest known option
[[nodiscard]] auto find_nearest_option(
const string_view input, const EngineBase::OptionList standardOptions, const EngineBase::OptionList customOptions)
-> string_view
{
// map options to pair of: option name, Levenshtein distance from input
const auto mapped
= std::views::join(std::array { standardOptions, customOptions })
| std::views::transform([input](const Option* option) {
return std::make_pair(
option->get_name(),
util::strings::levenshtein_distance(input, option->get_name()));
})
| std::ranges::to<std::vector>();
const auto closest = std::ranges::min(
mapped, std::ranges::less { }, [](const auto& item) { return item.second; });
return closest.first;
}
} // namespace
void EngineBase::handle_setoption(const string_view arguments)
{
auto [firstWord, rest] = split_at_first_space(arguments);
firstWord = trim(firstWord);
// code defensively against unrecognized tokens
if (firstWord != "name")
return;
rest = trim(rest);
// we can't just use split_at_first_space() here, because option names
// may legally contain spaces
const auto valueTokenIdx = rest.find("value");
const bool isNPos = valueTokenIdx == string_view::npos;
auto name = isNPos ? rest : rest.substr(0uz, valueTokenIdx);
name = trim(name);
wait();
auto update_option = [name, isNPos, rest, valueTokenIdx](const OptionList options) {
if (const auto it = std::ranges::find_if(
options,
[name](const Option* opt) { return opt->get_name() == name; });
it != options.end()) {
auto* option = *it;
assert(option != nullptr);
if (isNPos)
option->handle_setvalue({ });
else
option->handle_setvalue(trim(rest.substr(valueTokenIdx)));
return true;
}
return false;
};
if (update_option(standardUCIOptions))
return;
if (update_option(get_custom_uci_options()))
return;
info_string(std::format(
"Attempted to set unknown option '{}'", name));
info_string(std::format(
"The closest known option is: {}",
find_nearest_option(name, standardUCIOptions, get_custom_uci_options())));
}
void EngineBase::loop()
{
std::string inputBuf;
do {
std::getline(std::cin, inputBuf);
handle_command(inputBuf);
} while (not shouldExit.load(memory_order_acquire));
}
} // namespace chess::uci