ochess/src/game_tab/right_panel/LiveEngineDialog.cpp

148 lines
4.2 KiB
C++
Raw Normal View History

2022-02-27 16:29:14 +01:00
#include "LiveEngineDialog.hpp"
2023-01-31 10:26:40 +01:00
LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::uint32_t engine_id)
: DialogLiveEngine(parent), interval(1000),
2022-12-31 20:45:03 +01:00
engine(nullptr) {
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);
2023-01-31 10:26:40 +01:00
// Load engine name
confGroup="engines/"+std::to_string(engine_id);
CONFIG_OPEN(conf);
engine_name=conf->Read(confGroup+"/name");
CONFIG_CLOSE(conf);
2022-02-27 17:02:21 +01:00
current_engine->SetLabel(engine_name);
2023-01-31 10:26:40 +01:00
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
}
LiveEngineDialog::~LiveEngineDialog() {
2022-12-31 20:45:03 +01:00
if (engine != nullptr) {
wxLogDebug("LiveEngineDialog destructor: delete engine");
delete engine;
}
}
2022-02-27 18:32:57 +01:00
void LiveEngineDialog::InitEngine() {
2022-12-31 20:45:03 +01:00
if (engine == nullptr) {
2022-02-27 18:32:57 +01:00
wxLogDebug("Start engine: %s", engine_name);
CONFIG_OPEN(conf);
engine = new uciadapter::UCI(
2023-01-31 10:26:40 +01:00
conf->Read(confGroup + "/path").ToStdString());
2022-02-27 18:32:57 +01:00
engine->ucinewgame();
2022-02-27 19:14:50 +01:00
long index;
2023-01-31 10:26:40 +01:00
std::string optsPath = confGroup + "/options";
2022-02-27 19:14:50 +01:00
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-28 10:38:56 +01:00
if (opt_name.Lower() == "multipv") {
multipv->SetLabel(default_value_wxString);
} else if (opt_name.Lower() == "threads") {
threads->SetLabel(default_value_wxString);
}
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) {
2022-12-31 20:45:03 +01:00
if (engine != nullptr) {
2022-02-27 19:55:30 +01:00
wxLogDebug("Close live engine!!");
timer.Stop();
engine->stop();
engine->quit();
}
Destroy();
2022-02-27 19:55:30 +01:00
}
2022-02-27 18:32:57 +01:00
void LiveEngineDialog::SetFEN(std::string fen) {
2022-03-01 09:16:13 +01:00
if (timer.IsRunning()) {
StopEngine();
2022-12-22 13:53:02 +01:00
engine->position(fen);
2022-03-01 09:16:13 +01:00
StartEngine();
} else {
engine->position(fen);
}
2022-02-27 18:32:57 +01:00
}
2022-02-27 17:02:21 +01:00
void LiveEngineDialog::TogglePauseEngine(wxCommandEvent &event) {
if (timer.IsRunning()) {
2022-02-28 10:38:56 +01:00
StopEngine();
engine_stop_button->SetLabel("Restart");
depth->Enable(true);
2022-02-27 17:02:21 +01:00
} else {
2022-02-28 10:38:56 +01:00
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()) {
2022-02-27 18:32:57 +01:00
timer.Start(interval);
2022-02-27 17:02:21 +01:00
}
}
2022-02-27 18:32:57 +01:00
void LiveEngineDialog::OnTimerTick(wxTimerEvent &event) {
lines_list->DeleteAllItems(); // Clear lines_list
2022-02-27 18:32:57 +01:00
engine->SyncAfter(0);
EngineEvaluation *eval=new EngineEvaluation();
auto lines=engine->GetLines(); // First best lines
// First retrieve lines ids from unordered map lines
std::vector<int> ids;
for(auto const &line : lines){
ids.push_back(line.first);
}
std::sort(ids.begin(),ids.end());
2023-01-11 10:46:14 +01:00
if(ids.size()>0)
eval->eval=lines[ids[0]].score_cp;
// Now fill eval and lines_list
for (int &id:ids) {
auto const &line=lines[id];
long index = lines_list->InsertItem(id, std::to_string(id));
// Update eval that will be deplayed on the board
if(line.pv.size()>0)
eval->best_lines.push_back(line.pv[0]);
// Refresh lines_list
2022-02-27 18:32:57 +01:00
std::string line_moves;
for (std::string move : line.pv)
2022-02-27 19:14:50 +01:00
line_moves += move + " ";
std::string cp_str = std::to_string(line.score_cp);
if (line.score_cp > 0) {
2022-02-27 19:14:50 +01:00
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());
// Notify GameTab
wxCommandEvent notifyEvent(SHOW_ENGINE_EVALUATION,GetId());
notifyEvent.SetEventObject(this);
notifyEvent.SetClientData(eval);
ProcessEvent(notifyEvent);
2022-02-27 18:32:57 +01:00
}