diff --git a/ui_main.cpp b/ui_main.cpp index 1c0281d..c03c8bc 100644 --- a/ui_main.cpp +++ b/ui_main.cpp @@ -150,7 +150,23 @@ void ui_main_c::PCall(int narg, int nret) { sys->SetWorkDir(scriptWorkDir); inLua = true; + hasActiveCoroutine = false; int err = lua_pcall(L, narg, nret, 1); + lua_getglobal(L, "coroutine"); + lua_getfield(L, -1, "_list"); + lua_pcall(L, 0, 1, 0); + + if (lua_istable(L, -1)) { + lua_pushnil(L); + while (lua_next(L, -2)) { + lua_State* co = lua_tothread(L, -2); + if (co && lua_status(co) == LUA_YIELD) { + hasActiveCoroutine = true; + } + lua_pop(L, 1); + } + } + lua_pop(L, 1); inLua = false; sys->SetWorkDir(); if (err && !didExit) { @@ -354,13 +370,23 @@ void ui_main_c::ScriptInit() void ui_main_c::Frame() { + // Check for any subscripts we need to run + bool hasSubscript = false; + for (dword i = 0; i < subScriptSize; i++) { + if (subScriptList[i]) { + hasSubscript = true; + break; + } + } + // Always runs 10 frames after finishing the boot process if (!sys->video->IsVisible() || sys->conWin->IsVisible() || restartFlag || didExit) { framesSinceWindowHidden = 0; } else if (framesSinceWindowHidden <= 10) { framesSinceWindowHidden++; } - else if (!sys->video->IsActive() && !sys->video->IsCursorOverWindow()) { + // Otherwise only runs frames if the mouse is on screen, there is an active coroutine, or there is an active subscript + else if (!sys->video->IsActive() && !sys->video->IsCursorOverWindow() && !hasActiveCoroutine && !hasSubscript) { sys->Sleep(100); return; } @@ -406,7 +432,7 @@ void ui_main_c::Frame() } //sys->con->Printf("Finishing up...\n"); - if ( !sys->video->IsActive() ) { + if ( !sys->video->IsActive() && !hasActiveCoroutine && !hasSubscript ) { sys->Sleep(100); } diff --git a/ui_main.h b/ui_main.h index 321513b..87bdb58 100644 --- a/ui_main.h +++ b/ui_main.h @@ -49,6 +49,7 @@ class ui_main_c: public ui_IMain { int cursorY = 0; int framesSinceWindowHidden = 0; volatile bool inLua = false; + bool hasActiveCoroutine = false; int ioOpenf = LUA_NOREF; static int InitAPI(lua_State* L);