Skip to content

Commit

Permalink
Optimize garbage appear timing in replay
Browse files Browse the repository at this point in the history
  • Loading branch information
chouhy committed Jan 6, 2022
1 parent c3165c9 commit ec8890d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
34 changes: 24 additions & 10 deletions tetris_ai/Replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace RP {
int totalFrames;
int undoSteps;
std::list<step_t> temp_evt;
std::list<json> ige_reorder;
public:
playerRecord() {
info = {
Expand All @@ -89,6 +90,7 @@ namespace RP {
}
void reset(int undo) {
temp_evt.clear();
ige_reorder.clear();
undoSteps = undo;
evt.clear();
options.clear();
Expand Down Expand Up @@ -164,7 +166,7 @@ namespace RP {
{"forfeit_time",150},
{"are",0},
{"lineclear_are",0},
{"infinitemovement",false},
{"infinitemovement",true},
{"lockresets",15},
{"allow180",true},
{"manual_allowed",false},
Expand Down Expand Up @@ -202,21 +204,33 @@ namespace RP {
}
int excessSteps = max(0, totalStoredSteps - undoSteps);
while (excessSteps--) {
for (auto it = temp_evt.front().evts.begin(); it != temp_evt.front().evts.end(); it++) {
this->evt["events"].push_back(*it);
flushOneStep();
}
}
void flushOneStep() {
for (auto it = temp_evt.front().evts.begin(); it != temp_evt.front().evts.end(); it++) {
if ((*it)["type"] == "ige") {
ige_reorder.push_back(*it);
continue;
}
while (!ige_reorder.empty() && ige_reorder.front()["frame"] < (*it)["frame"]) {
this->evt["events"].push_back(ige_reorder.front());
ige_reorder.pop_front();
}
temp_evt.pop_front();
this->evt["events"].push_back(*it);
}
temp_evt.pop_front();
}
void flush() {
while (!temp_evt.empty()) {
for (auto it = temp_evt.front().evts.begin(); it != temp_evt.front().evts.end(); it++) {
this->evt["events"].push_back(*it);
}
temp_evt.pop_front();
flushOneStep();
}
while (!ige_reorder.empty()) {
this->evt["events"].push_back(ige_reorder.front());
ige_reorder.pop_front();
}
}
void amendIGEEvt(atk_t atk) {
void amendIGEEvt(int frame, atk_t atk) {
step_t last;
bool lastExist = false;
if (!temp_evt.back().done) {
Expand All @@ -225,7 +239,7 @@ namespace RP {
lastExist = true;
}
json ige;
int frame = temp_evt.back().evts.back()["frame"];
//frame = temp_evt.back().evts.back()["frame"];
ige["frame"] = frame;
ige["type"] = "ige";
ige["data"] = {
Expand Down
10 changes: 6 additions & 4 deletions tetris_ai/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,11 @@ void tetris_draw(const TetrisGame& tetris, bool showAttackLine, bool showGrid, i
}
}
{
int w = textwidth(tetris.m_name.c_str());
std::string f = tetris.m_name;// +" " + std::to_string(tetris.m_frames);
int w = textwidth(f.c_str());
if ( tetris.pTetrisAI ) setcolor(EGERGB(0xa0, 0x0, 0xff));
xyprintf(int(tetris.m_base.x + tetris.m_size.x * ( 5 + tetris.poolw() / 2 )) - w / 2, int(tetris.m_base.y + tetris.m_size.y * 0 ),
"%s", tetris.m_name.c_str() );
"%s", f.c_str() );
}
}

Expand Down Expand Up @@ -1455,7 +1456,7 @@ void mainscene() {
if (atk.attacker == j) continue;
if (rule.GarbageBuffer) {
tetris[j].accept_atts.push_back(atk.attack);
pRecord[j].insertEvent(RP::IGE, tetris[j].m_frames, &atk);
pRecord[j].insertEvent(RP::IGE, tetris[j].m_frames, &(atk.attack));
if (rule.turnbase) tetris[j].env_change = 2; // don't know what it means
}
else {
Expand Down Expand Up @@ -1523,7 +1524,7 @@ void mainscene() {
tetris[j].accept_atts.push_back(tetris[i].genAttack(att));
if (j == 0 && saved_board[j].back().n_pieces == tetris[j].n_pieces) {
saved_board[j].back().accept_atts = tetris[j].accept_atts;
pRecord[j].amendIGEEvt(tetris[j].accept_atts.back());
pRecord[j].amendIGEEvt(tetris[j].m_frames, tetris[j].accept_atts.back());
}
else {
pRecord[j].insertEvent(RP::IGE, tetris[j].m_frames, &tetris[j].accept_atts.back());
Expand Down Expand Up @@ -1777,6 +1778,7 @@ void mainscene() {
while (tetris[1].ai_movs_flag != -1) ::Sleep(1);
tetris[1] = saved_board[1].back();
}
tetris[1].m_frames = tetris[0].m_frames;
}
for (int i = 0; i < players_num; i++) {
tetris_draw(tetris[i], showAttackLine, showGrid, rule.GarbageCap);
Expand Down

0 comments on commit ec8890d

Please sign in to comment.