diff options
| author | Loic Guegan <manzerbredes@mailbox.org> | 2022-02-28 13:44:27 +0100 |
|---|---|---|
| committer | Loic Guegan <manzerbredes@mailbox.org> | 2022-02-28 13:44:27 +0100 |
| commit | bf485fa577a76731f9eac97de3b0a647cd492e49 (patch) | |
| tree | 731d9ec91070406124044e75b4b27949e5d230c0 /src/game_tab/right_panel/LiveEngineDialog.cpp | |
| parent | 7178f18ab88bcc93bfbf2019adf53d2f60d8fa20 (diff) | |
Refactoring game tab
Diffstat (limited to 'src/game_tab/right_panel/LiveEngineDialog.cpp')
| -rw-r--r-- | src/game_tab/right_panel/LiveEngineDialog.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/game_tab/right_panel/LiveEngineDialog.cpp b/src/game_tab/right_panel/LiveEngineDialog.cpp new file mode 100644 index 0000000..1f509ae --- /dev/null +++ b/src/game_tab/right_panel/LiveEngineDialog.cpp @@ -0,0 +1,111 @@ +#include "LiveEngineDialog.hpp" + +LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::string engine_name) + : DialogLiveEngine(parent), engine_name(engine_name), interval(1000), + engine(NULL) { + lines_list->InsertColumn(0, "#", wxLIST_FORMAT_LEFT, 50); + lines_list->InsertColumn(1, "CP", wxLIST_FORMAT_LEFT, 70); + lines_list->InsertColumn(2, "Line", wxLIST_FORMAT_LEFT, 300); + current_engine->SetLabel(engine_name); + InitEngine(); + Bind(wxEVT_BUTTON, &LiveEngineDialog::TogglePauseEngine, this, + LIVE_ENGINE_PAUSE_BUTTON); + Bind(wxEVT_CLOSE_WINDOW, &LiveEngineDialog::OnClose, this); +} + +void LiveEngineDialog::InitEngine() { + if (engine == NULL) { + wxLogDebug("Start engine: %s", engine_name); + CONFIG_OPEN(conf); + engine = new uciadapter::UCI( + conf->Read("engines/" + engine_name + "/path").ToStdString()); + engine->ucinewgame(); + + long index; + std::string optsPath = "engines/" + engine_name + "/options"; + conf->SetPath(optsPath); + wxString opt_name; + if (conf->GetFirstGroup(opt_name, index)) { + do { + wxString optPath = opt_name + "/"; + wxString default_value_wxString = conf->Read(optPath + "value"); + std::string default_value = default_value_wxString.ToStdString(); + engine->setoption(opt_name.ToStdString(), default_value); + if (opt_name.Lower() == "multipv") { + multipv->SetLabel(default_value_wxString); + } else if (opt_name.Lower() == "threads") { + threads->SetLabel(default_value_wxString); + } + } while (conf->GetNextGroup(opt_name, index)); + } + + CONFIG_CLOSE(conf); + } + timer.Start(interval); + timer.Bind(wxEVT_TIMER, &LiveEngineDialog::OnTimerTick, this); +} + +void LiveEngineDialog::OnClose(wxCloseEvent &e) { + if (engine != NULL) { + wxLogDebug("Close live engine!!"); + timer.Stop(); + engine->stop(); + engine->quit(); + delete engine; + } + e.Skip(); +} + +void LiveEngineDialog::SetFEN(std::string fen) { + StopEngine(); + engine->position(fen); + StartEngine(); +} + +void LiveEngineDialog::TogglePauseEngine(wxCommandEvent &event) { + if (timer.IsRunning()) { + StopEngine(); + engine_stop_button->SetLabel("Restart"); + depth->Enable(true); + } else { + engine_stop_button->SetLabel("Stop"); + depth->Enable(false); + StartEngine(); + } +} + +void LiveEngineDialog::StopEngine() { + if (timer.IsRunning()) { + timer.Stop(); + } + engine->stop(); +}; + +void LiveEngineDialog::StartEngine() { + uciadapter::Go args; + args.depth = depth->GetValue(); + engine->go(args); + if (!timer.IsRunning()) { + timer.Start(interval); + } +} + +void LiveEngineDialog::OnTimerTick(wxTimerEvent &event) { + wxLogDebug("Tick!"); + lines_list->DeleteAllItems(); + engine->SyncAfter(0); + for (auto const &line : engine->GetLines()) { + long index = lines_list->InsertItem(0, std::to_string(line.first)); + std::string line_moves; + for (std::string move : line.second.pv) { + line_moves += move + " "; + } + std::string cp_str = std::to_string(line.second.score_cp); + if (line.second.score_cp > 0) { + cp_str = "+" + cp_str; + } + lines_list->SetItem(index, 1, cp_str); + lines_list->SetItem(index, 2, line_moves); + } + wxLogDebug("%s", engine->GetBuffer()); +}
\ No newline at end of file |
