aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/editor/LiveEngineDialog.cpp
blob: 07fd7824ab51806cc95b7a3cf992aa7b869e33c9 (plain)
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
#include "LiveEngineDialog.hpp"

LiveEngineDialog::LiveEngineDialog(wxWindow *parent, std::string engine_name)
    : LiveEngineDialogFB(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);
}

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);
      } while (conf->GetNextGroup(opt_name, index));
    }

    CONFIG_CLOSE(conf);
  }
  timer.Start(interval);
  timer.Bind(wxEVT_TIMER, &LiveEngineDialog::OnTimerTick, this);
}

void LiveEngineDialog::SetFEN(std::string fen) {
  timer.Stop();
  engine->position(fen);
  uciadapter::Go args;
  engine->go(args);
  timer.Start(interval);
}

void LiveEngineDialog::TogglePauseEngine(wxCommandEvent &event) {
  if (timer.IsRunning()) {
    timer.Stop();
    engine_pause_button->SetLabel("Continue");
  } else {
    timer.Start(interval);
    engine_pause_button->SetLabel("Pause");
  }
}

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());
}