2022-02-27 16:29:14 +01:00
|
|
|
#include "LiveEngineDialog.hpp"
|
|
|
|
|
2022-02-27 17:02:21 +01:00
|
|
|
LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::string engine_name)
|
2022-02-27 18:32:57 +01:00
|
|
|
: LiveEngineDialogFB(parent), engine_name(engine_name), interval(1000),
|
|
|
|
engine(NULL) {
|
2022-02-27 17:02:21 +01:00
|
|
|
lines_list->InsertColumn(0, "#", wxLIST_FORMAT_LEFT, 50);
|
2022-02-27 19:14:50 +01:00
|
|
|
lines_list->InsertColumn(1, "CP", wxLIST_FORMAT_LEFT, 70);
|
|
|
|
lines_list->InsertColumn(2, "Line", wxLIST_FORMAT_LEFT, 300);
|
2022-02-27 17:02:21 +01:00
|
|
|
current_engine->SetLabel(engine_name);
|
2022-02-27 18:32:57 +01:00
|
|
|
InitEngine();
|
|
|
|
Bind(wxEVT_BUTTON, &LiveEngineDialog::TogglePauseEngine, this,
|
|
|
|
LIVE_ENGINE_PAUSE_BUTTON);
|
2022-02-27 19:55:30 +01:00
|
|
|
Bind(wxEVT_CLOSE_WINDOW, &LiveEngineDialog::OnClose, this);
|
2022-02-27 17:02:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-27 18:32:57 +01:00
|
|
|
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();
|
2022-02-27 19:14:50 +01:00
|
|
|
|
|
|
|
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();
|
2022-02-27 19:55:30 +01:00
|
|
|
engine->setoption(opt_name.ToStdString(), default_value);
|
2022-02-27 19:14:50 +01:00
|
|
|
} while (conf->GetNextGroup(opt_name, index));
|
|
|
|
}
|
|
|
|
|
2022-02-27 18:32:57 +01:00
|
|
|
CONFIG_CLOSE(conf);
|
|
|
|
}
|
|
|
|
timer.Start(interval);
|
2022-02-27 17:02:21 +01:00
|
|
|
timer.Bind(wxEVT_TIMER, &LiveEngineDialog::OnTimerTick, this);
|
|
|
|
}
|
|
|
|
|
2022-02-27 19:55:30 +01:00
|
|
|
void LiveEngineDialog::OnClose(wxCloseEvent &e) {
|
|
|
|
if (engine != NULL) {
|
|
|
|
wxLogDebug("Close live engine!!");
|
|
|
|
timer.Stop();
|
|
|
|
engine->stop();
|
|
|
|
engine->quit();
|
|
|
|
delete engine;
|
|
|
|
}
|
|
|
|
e.Skip();
|
|
|
|
}
|
|
|
|
|
2022-02-27 18:32:57 +01:00
|
|
|
void LiveEngineDialog::SetFEN(std::string fen) {
|
|
|
|
timer.Stop();
|
|
|
|
engine->position(fen);
|
|
|
|
uciadapter::Go args;
|
|
|
|
engine->go(args);
|
|
|
|
timer.Start(interval);
|
|
|
|
}
|
|
|
|
|
2022-02-27 17:02:21 +01:00
|
|
|
void LiveEngineDialog::TogglePauseEngine(wxCommandEvent &event) {
|
|
|
|
if (timer.IsRunning()) {
|
|
|
|
timer.Stop();
|
|
|
|
engine_pause_button->SetLabel("Continue");
|
|
|
|
} else {
|
2022-02-27 18:32:57 +01:00
|
|
|
timer.Start(interval);
|
2022-02-27 17:02:21 +01:00
|
|
|
engine_pause_button->SetLabel("Pause");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 18:32:57 +01:00
|
|
|
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;
|
2022-02-27 19:14:50 +01:00
|
|
|
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;
|
2022-02-27 18:32:57 +01:00
|
|
|
}
|
2022-02-27 19:14:50 +01:00
|
|
|
lines_list->SetItem(index, 1, cp_str);
|
|
|
|
lines_list->SetItem(index, 2, line_moves);
|
2022-02-27 18:32:57 +01:00
|
|
|
}
|
|
|
|
wxLogDebug("%s", engine->GetBuffer());
|
|
|
|
}
|