From ec8890db776b9465c134d61777a1ca2319825b85 Mon Sep 17 00:00:00 2001 From: chouhy Date: Wed, 5 Jan 2022 23:29:41 -0500 Subject: [PATCH] Optimize garbage appear timing in replay --- tetris_ai/Replay.h | 34 ++++++++++++++++++++++++---------- tetris_ai/main.cpp | 10 ++++++---- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/tetris_ai/Replay.h b/tetris_ai/Replay.h index 59095ec..d1f6e0e 100644 --- a/tetris_ai/Replay.h +++ b/tetris_ai/Replay.h @@ -72,6 +72,7 @@ namespace RP { int totalFrames; int undoSteps; std::list temp_evt; + std::list ige_reorder; public: playerRecord() { info = { @@ -89,6 +90,7 @@ namespace RP { } void reset(int undo) { temp_evt.clear(); + ige_reorder.clear(); undoSteps = undo; evt.clear(); options.clear(); @@ -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}, @@ -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) { @@ -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"] = { diff --git a/tetris_ai/main.cpp b/tetris_ai/main.cpp index e79ba54..39a5fff 100644 --- a/tetris_ai/main.cpp +++ b/tetris_ai/main.cpp @@ -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() ); } } @@ -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 { @@ -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()); @@ -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);