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.
This commit is contained in:
Sally Coolatta 2022-02-06 18:31:10 -05:00
parent dcdbdec2bf
commit c53f57a22f
3 changed files with 20 additions and 12 deletions

View file

@ -214,7 +214,6 @@ static int action_call(lua_State *L)
// Set in lua_infolib. // Set in lua_infolib.
const char *luaactions[MAX_ACTION_RECURSION]; const char *luaactions[MAX_ACTION_RECURSION];
UINT8 luaactionstack = 0; UINT8 luaactionstack = 0;
UINT8 superstack = 0;
static int lib_dummysuper(lua_State *L) static int lib_dummysuper(lua_State *L)
{ {

View file

@ -43,7 +43,6 @@ extern boolean introchanged;
#define MAX_ACTION_RECURSION 30 #define MAX_ACTION_RECURSION 30
extern const char *luaactions[MAX_ACTION_RECURSION]; extern const char *luaactions[MAX_ACTION_RECURSION];
extern UINT8 luaactionstack; extern UINT8 luaactionstack;
extern UINT8 superstack;
// If the dehacked patch does not match this version, we throw a warning // If the dehacked patch does not match this version, we throw a warning
#define PATCHVERSION 220 #define PATCHVERSION 220

View file

@ -812,6 +812,7 @@ boolean LUA_SetLuaAction(void *stv, const char *action)
return true; // action successfully set. return true; // action successfully set.
} }
static UINT8 superstack[NUMACTIONS];
boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor) boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor)
{ {
I_Assert(actor != NULL); I_Assert(actor != NULL);
@ -819,7 +820,7 @@ boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor)
if (actionsoverridden[actionnum][0] == LUA_REFNIL) if (actionsoverridden[actionnum][0] == LUA_REFNIL)
{ {
// The action was not overridden at all, // The action was not overridden at all,
// so call the hardcoded version. // so just call the hardcoded version.
return false; 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, // 0 is just the reference to the one we're calling,
// so we increment here. // 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"); CONS_Alert(CONS_WARNING, "Max Lua super recursion reached! Cool it on calling super!\n");
superstack[actionnum] = 0;
return false; 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. // No Lua reference beyond this point.
// Let it call the hardcoded function instead. // Let it call the hardcoded function instead.
if (superstack[actionnum])
{
// Decrement super stack
superstack[actionnum]--;
}
return false; return false;
} }
@ -855,7 +859,7 @@ boolean LUA_CallAction(enum actionnum actionnum, mobj_t *actor)
lua_pushcfunction(gL, LUA_GetErrorMessage); lua_pushcfunction(gL, LUA_GetErrorMessage);
// Push function by reference. // Push function by reference.
lua_getref(gL, actionsoverridden[actionnum][superstack]); lua_getref(gL, actionsoverridden[actionnum][superstack[actionnum]]);
if (lua_isnil(gL, -1)) // no match 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_Call(gL, 3, 0, -(2 + 3));
lua_pop(gL, -1); // Error handler lua_pop(gL, -1); // Error handler
if (superstack[actionnum])
{
// Decrement super stack
superstack[actionnum]--;
}
--luaactionstack; --luaactionstack;
luaactions[luaactionstack] = NULL; luaactions[luaactionstack] = NULL;
return true; // action successfully called. return true; // action successfully called.