BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
EngineBase.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <array>
23#include <atomic>
24#include <cassert>
25#include <functional>
29#include <span>
30#include <string>
31#include <string_view>
32#include <utility>
33
34namespace chess::uci {
35
36using game::Position;
37using std::string_view;
38
42struct EngineCommand final {
44 using Callback = std::function<void(string_view)>;
45
49 string_view name;
50
55
57 string_view description;
58
64 string_view argsHelp;
65
67 [[nodiscard]] static auto void_cb(std::function<void()>&& func) -> Callback
68 {
69 return [callback = std::move(func)]([[maybe_unused]] const string_view args) {
70 callback();
71 };
72 }
73};
74
88struct EngineBase {
89 EngineBase() = default;
90
91 virtual ~EngineBase();
92
93 EngineBase(const EngineBase&) = delete;
95 EngineBase& operator=(const EngineBase&) = delete;
97
102 [[nodiscard]] virtual auto get_name() const -> std::string = 0;
103
105 [[nodiscard]] virtual auto get_author() const -> string_view = 0;
106
111 virtual void wait() { }
112
116 [[nodiscard]] virtual auto is_searching() const noexcept -> bool = 0;
117
128 virtual void new_game([[maybe_unused]] bool firstCall) { }
129
131 virtual void abort_search() { }
132
137 virtual void ponder_hit() { }
138
140 void set_position(const Position& pos)
141 {
142 position = pos;
143 position_changed(pos);
144 }
145
147 [[nodiscard]] auto get_position() const -> const Position& { return position; }
148
153 virtual void go([[maybe_unused]] const GoCommandOptions& opts) = 0;
154
161 void handle_command(string_view command);
162
169 void loop();
170
174 virtual void handle_registration([[maybe_unused]] const RegisterOptions& opts) { }
175
177 using CommandList = std::span<const EngineCommand>;
178
180 [[nodiscard]] auto get_standard_uci_commands() const noexcept -> CommandList
181 {
182 return standardUCICommands;
183 }
184
186 [[nodiscard]] virtual auto get_custom_uci_commands() const noexcept -> CommandList
187 {
188 return { };
189 }
190
192 using OptionList = std::span<Option*>;
193
195 [[nodiscard]] virtual auto get_custom_uci_options() noexcept -> OptionList
196 {
197 return { };
198 }
199
201 [[nodiscard]] auto get_standard_uci_options() noexcept -> OptionList
202 {
203 return standardUCIOptions;
204 }
205
207 [[nodiscard]] auto is_debug_mode() const noexcept -> bool
208 {
209 return debugMode.load(std::memory_order_relaxed);
210 }
211
213 void set_debug_mode(const bool shouldDebug) noexcept
214 {
215 debugMode.store(shouldDebug, std::memory_order_relaxed);
216 }
217
225 void set_sanitize_positions(const bool shouldSanitize) noexcept
226 {
227 sanitizeIncomingPositions.store(shouldSanitize, std::memory_order_release);
228 }
229
231 virtual void resize_transposition_table([[maybe_unused]] const size_t sizeMB) { }
232
234 virtual void set_ponder([[maybe_unused]] const bool shouldPonder) { }
235
238
241 "Hash",
242 1, 2048, 16,
243 "Sets the transposition table size (in MB).",
244 [this](const int sizeMB) {
245 assert(sizeMB >= 0);
246 resize_transposition_table(static_cast<size_t>(sizeMB));
247 }
248 };
249
257 "Ponder",
258 false,
259 "Controls whether pondering is allowed.",
260 [this](const bool shouldPonder) { set_ponder(shouldPonder); }
261 };
262
264
265protected:
267 virtual void position_changed([[maybe_unused]] const Position& pos) { }
268
269 std::array<EngineCommand, 11uz> standardUCICommands {
271 .name = "uci",
272 .action = EngineCommand::void_cb([this] { respond_to_uci(); }),
273 .description = "Initialize UCI communication",
274 .argsHelp = { } },
276 .name = "isready",
277 .action = EngineCommand::void_cb([this] { respond_to_isready(); }),
278 .description = "Wait for engine to complete background tasks",
279 .argsHelp = { } },
281 .name = "ucinewgame",
282 .action = EngineCommand::void_cb([this] { respond_to_newgame(); }),
283 .description = "Initialize a new game",
284 .argsHelp = { } },
286 .name = "quit",
287 .action = EngineCommand::void_cb([this] { handle_quit(); }),
288 .description = "Exit the engine as quickly as possible",
289 .argsHelp = { } },
291 .name = "stop",
292 .action = EngineCommand::void_cb([this] { abort_search(); }),
293 .description = "Abort the current search, if any",
294 .argsHelp = { } },
296 .name = "ponderhit",
297 .action = EngineCommand::void_cb([this] { ponder_hit(); }),
298 .description = "Indicate that the user played the ponder move",
299 .argsHelp = { } },
301 .name = "position",
302 .action = [this](const string_view args) { handle_setpos(args); },
303 .description = "Set the position on the engine's internal board",
304 .argsHelp = "[startpos|fen <fen>] [moves <move...>]" },
306 .name = "go",
307 .action = [this](const string_view args) { handle_go(args); },
308 .description = "Start a search",
309 .argsHelp = { } },
311 .name = "setoption",
312 .action = [this](const string_view args) { handle_setoption(args); },
313 .description = "Set UCI option values",
314 .argsHelp = "[name <name>] [value <value>]" },
316 .name = "debug",
317 .action = [this](const string_view args) noexcept { set_debug_mode(args == "on"); },
318 .description = "Enable/disable engine debug mode",
319 .argsHelp = "[on|off]" },
321 .name = "register",
322 .action = [this](const string_view args) {
324 },
325 .description = "Handle license registration",
326 .argsHelp = { } }
327 };
328
329 std::array<Option*, 2uz> standardUCIOptions {
331 };
332
333private:
334 void respond_to_uci();
335 void respond_to_isready();
336 void respond_to_newgame();
337 void handle_quit();
338 void handle_setpos(string_view arguments);
339 void handle_setoption(string_view arguments);
340 void handle_go(string_view arguments);
341
342 static_assert(
343 std::atomic_bool::is_always_lock_free,
344 "Platform doesn't support lock-free atomic operations");
345
346 Position position;
347
348 std::atomic_bool shouldExit { false }; // used as flag for exiting the loop() function
349 std::atomic_bool initialized { false };
350 std::atomic_bool debugMode { false };
351 std::atomic_bool sanitizeIncomingPositions { false };
352};
353
354} // namespace chess::uci
optional< RegisterNowOptions > RegisterOptions
auto parse_register_options(string_view options) -> RegisterOptions
virtual void set_ponder(const bool shouldPonder)
virtual auto get_author() const -> string_view=0
virtual void handle_registration(const RegisterOptions &opts)
virtual void ponder_hit()
std::span< const EngineCommand > CommandList
virtual auto get_name() const -> std::string=0
std::span< Option * > OptionList
EngineBase(EngineBase &&)=delete
virtual void resize_transposition_table(const size_t sizeMB)
void handle_command(string_view command)
virtual void go(const GoCommandOptions &opts)=0
void set_sanitize_positions(const bool shouldSanitize) noexcept
std::array< Option *, 2uz > standardUCIOptions
virtual auto is_searching() const noexcept -> bool=0
virtual void position_changed(const Position &pos)
virtual auto get_custom_uci_commands() const noexcept -> CommandList
EngineBase & operator=(EngineBase &&)=delete
virtual void new_game(bool firstCall)
auto get_standard_uci_commands() const noexcept -> CommandList
void set_position(const Position &pos)
auto get_position() const -> const Position &
virtual void abort_search()
EngineBase & operator=(const EngineBase &)=delete
auto is_debug_mode() const noexcept -> bool
std::array< EngineCommand, 11uz > standardUCICommands
EngineBase(const EngineBase &)=delete
void set_debug_mode(const bool shouldDebug) noexcept
auto get_standard_uci_options() noexcept -> OptionList
virtual auto get_custom_uci_options() noexcept -> OptionList
std::function< void(string_view)> Callback
static auto void_cb(std::function< void()> &&func) -> Callback