From c53f57a22fcf5e26164c41b29ba5be8e675d82a9 Mon Sep 17 00:00:00 2001 From: Sally Coolatta Date: Sun, 6 Feb 2022 18:31:10 -0500 Subject: [PATCH] Handle super stack more intelligently Counted per action type, and it is decremented after the function is called instead of keeping its old value or even resetting completely when using multiple actions together. --- src/deh_lua.c | 1 - src/dehacked.h | 1 - src/lua_infolib.c | 30 ++++++++++++++++++++---------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/deh_lua.c b/src/deh_lua.c index 43e25a49d..62a1717a8 100644 --- a/src/deh_lua.c +++ b/src/deh_lua.c @@ -214,7 +214,6 @@ static int action_call(lua_State *L) // Set in lua_infolib. const char *luaactions[MAX_ACTION_RECURSION]; UINT8 luaactionstack = 0; -UINT8 superstack = 0; static int lib_dummysuper(lua_State *L) { diff --git a/src/dehacked.h b/src/dehacked.h index e29aef6ff..bf4d5c121 100644 --- a/src/dehacked.h +++ b/src/dehacked.h @@ -43,7 +43,6 @@ extern boolean introchanged; #define MAX_ACTION_RECURSION 30 extern const char *luaactions[MAX_ACTION_RECURSION]; extern UINT8 luaactionstack; -extern UINT8 superstack; // If the dehacked patch does not match this version, we throw a warning #define PATCHVERSION 220 diff --git a/src/lua_infolib.c b/src/lua_infolib.c index e20202495..ad7bc7b6f 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -812,6 +812,7 @@ boolean LUA_SetLuaAction(void *stv, const char *action) return true; // action successfully set. } +static UINT8 superstack[NUMACTIONS]; boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor) { I_Assert(actor != NULL); @@ -819,7 +820,7 @@ boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor) if (actionsoverridden[actionnum][0] == LUA_REFNIL) { // The action was not overridden at all, - // so call the hardcoded version. + // so just call the hardcoded version. return false; } @@ -830,24 +831,27 @@ boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor) // 0 is just the reference to the one we're calling, // so we increment here. - superstack++; + superstack[actionnum]++; - if (superstack >= MAX_ACTION_RECURSION) + if (superstack[actionnum] >= MAX_ACTION_RECURSION) { CONS_Alert(CONS_WARNING, "Max Lua super recursion reached! Cool it on calling super!\n"); + superstack[actionnum] = 0; return false; } } - else - { - // Not calling itself, reset the super counter. - superstack = 0; - } - if (actionsoverridden[actionnum][superstack] == LUA_REFNIL) + if (actionsoverridden[actionnum][superstack[actionnum]] == LUA_REFNIL) { // No Lua reference beyond this point. // Let it call the hardcoded function instead. + + if (superstack[actionnum]) + { + // Decrement super stack + superstack[actionnum]--; + } + return false; } @@ -855,7 +859,7 @@ boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor) lua_pushcfunction(gL, LUA_GetErrorMessage); // Push function by reference. - lua_getref(gL, actionsoverridden[actionnum][superstack]); + lua_getref(gL, actionsoverridden[actionnum][superstack[actionnum]]); if (lua_isnil(gL, -1)) // no match { @@ -883,6 +887,12 @@ boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor) LUA_Call(gL, 3, 0, -(2 + 3)); lua_pop(gL, -1); // Error handler + if (superstack[actionnum]) + { + // Decrement super stack + superstack[actionnum]--; + } + --luaactionstack; luaactions[luaactionstack] = NULL; return true; // action successfully called.