From d26c7654ff1a2a6f75cf2fd40714687de3a844ca Mon Sep 17 00:00:00 2001 From: Zachary McAlpin Date: Fri, 17 Jul 2020 00:08:38 -0500 Subject: [PATCH] Ported Lat's PlayerCmd hook to vanilla SRB2 --- src/g_game.c | 7 +++++++ src/lua_baselib.c | 9 ++++++--- src/lua_hook.h | 6 +++++- src/lua_hooklib.c | 46 ++++++++++++++++++++++++++++++++++++++++++++- src/lua_infolib.c | 27 ++++++++++++++++++++++++++ src/lua_maplib.c | 7 +++++++ src/lua_mobjlib.c | 5 +++++ src/lua_playerlib.c | 7 +++++++ 8 files changed, 109 insertions(+), 5 deletions(-) diff --git a/src/g_game.c b/src/g_game.c index cce4ac822..ecd1d5208 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -1675,6 +1675,13 @@ void G_BuildTiccmd(ticcmd_t *cmd, INT32 realtics, UINT8 ssplayer) } } + /* Note: Lat originally made the PlayerCmd hook for SRB2 Kart so credit goes to him. + Also, unlike in SRB2 Kart, the cmd's variables cannot be set using the PlayerCmd, because + it is recommended to use the PreThinkFrame to set the cmd's variables, because PreThinkFrame is actually synched, + unlike the PlayerCmd hook. */ + if (gamestate == GS_LEVEL) + LUAh_PlayerCmd(player, cmd); + //Reset away view if a command is given. if (ssplayer == 1 && (cmd->forwardmove || cmd->sidemove || cmd->buttons) && displayplayer != consoleplayer) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 3f62ef890..0e60cbb90 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -32,9 +32,12 @@ #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" // hud_running errors +#include "lua_hook.h" // hook_cmd_running errors #define NOHUD if (hud_running)\ -return luaL_error(L, "HUD rendering code should not call this function!"); +return luaL_error(L, "HUD rendering code should not call this function!");\ +else if (hook_cmd_running)\ +return luaL_error(L, "CMD building code should not call this function!"); boolean luaL_checkboolean(lua_State *L, int narg) { luaL_checktype(L, narg, LUA_TBOOLEAN); @@ -2593,8 +2596,8 @@ static int lib_sStartSound(lua_State *L) } if (!player || P_IsLocalPlayer(player)) { - if (hud_running) - origin = NULL; // HUD rendering startsound shouldn't have an origin, just remove it instead of having a retarded error. + if (hud_running || hook_cmd_running) + origin = NULL; // HUD rendering and CMD building startsound shouldn't have an origin, just remove it instead of having a retarded error. S_StartSound(origin, sound_id); } diff --git a/src/lua_hook.h b/src/lua_hook.h index 48f6cab32..47850812f 100644 --- a/src/lua_hook.h +++ b/src/lua_hook.h @@ -59,11 +59,14 @@ enum hook { hook_PlayerThink, hook_ShouldJingleContinue, hook_GameQuit, + hook_PlayerCmd, hook_MAX // last hook }; extern const char *const hookNames[]; +extern boolean hook_cmd_running; + void LUAh_MapChange(INT16 mapnumber); // Hook for map change (before load) void LUAh_MapLoad(void); // Hook for map load void LUAh_PlayerJoin(int playernum); // Hook for Got_AddPlayer @@ -113,4 +116,5 @@ boolean LUAh_SeenPlayer(player_t *player, player_t *seenfriend); // Hook for MT_ #endif #define LUAh_PlayerThink(player) LUAh_PlayerHook(player, hook_PlayerThink) // Hook for P_PlayerThink boolean LUAh_ShouldJingleContinue(player_t *player, const char *musname); // Hook for whether a jingle of the given music should continue playing -void LUAh_GameQuit(void); // Hook for game quitting \ No newline at end of file +void LUAh_GameQuit(void); // Hook for game quitting +boolean LUAh_PlayerCmd(player_t *player, ticcmd_t *cmd); // Hook for building player's ticcmd struct (Ported from SRB2Kart) diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 5cfd1bd3d..9157b08d6 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -71,6 +71,7 @@ const char *const hookNames[hook_MAX+1] = { "PlayerThink", "ShouldJingleContinue", "GameQuit", + "PlayerCmd", NULL }; @@ -1793,6 +1794,49 @@ void LUAh_GameQuit(void) hookp->error = true; } } - + lua_pop(gL, 1); // Pop error handler } + +// Hook for building player's ticcmd struct (Ported from SRB2Kart) +boolean hook_cmd_running = false; +boolean LUAh_PlayerCmd(player_t *player, ticcmd_t *cmd) +{ + hook_p hookp; + boolean hooked = false; + if (!gL || !(hooksAvailable[hook_PlayerCmd/8] & (1<next) + { + if (hookp->type != hook_PlayerCmd) + continue; + + if (lua_gettop(gL) == 1) + { + LUA_PushUserdata(gL, player, META_PLAYER); + LUA_PushUserdata(gL, cmd, META_TICCMD); + } + PushHook(gL, hookp); + lua_pushvalue(gL, -3); + lua_pushvalue(gL, -3); + if (lua_pcall(gL, 2, 1, 1)) { + if (!hook->error || cv_debug & DBG_LUA) + CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL, -1)); + lua_pop(gL, 1); + hookp->error = true; + continue; + } + if (lua_toboolean(gL, -1)) + hooked = true; + lua_pop(gL, 1); + } + + lua_settop(gL, 0); + lua_cmd_running = false; + return hooked; +} diff --git a/src/lua_infolib.c b/src/lua_infolib.c index b3e92f833..0bd388b5b 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -25,6 +25,7 @@ #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" // hud_running errors +#include "lua_hook.h" // hook_cmd_running errors extern CV_PossibleValue_t Color_cons_t[]; extern UINT8 skincolor_modified[]; @@ -165,6 +166,8 @@ static int lib_setSpr2default(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter spr2defaults[] in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter spr2defaults[] in CMD building code!"); // todo: maybe allow setting below first freeslot..? step 1 is toggling this, step 2 is testing to see whether it's net-safe #ifdef SETALLSPR2DEFAULTS @@ -371,6 +374,8 @@ static int lib_setSpriteInfo(lua_State *L) return luaL_error(L, "Do not alter spriteinfo_t from within a hook or coroutine!"); if (hud_running) return luaL_error(L, "Do not alter spriteinfo_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter spriteinfo_t in CMD building code!"); lua_remove(L, 1); { @@ -455,6 +460,8 @@ static int spriteinfo_set(lua_State *L) return luaL_error(L, "Do not alter spriteinfo_t from within a hook or coroutine!"); if (hud_running) return luaL_error(L, "Do not alter spriteinfo_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter spriteinfo_t in CMD building code!"); I_Assert(sprinfo != NULL); @@ -533,6 +540,8 @@ static int pivotlist_set(lua_State *L) return luaL_error(L, "Do not alter spriteframepivot_t from within a hook or coroutine!"); if (hud_running) return luaL_error(L, "Do not alter spriteframepivot_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter spriteframepivot_t in CMD building code!"); I_Assert(pivotlist != NULL); @@ -587,6 +596,8 @@ static int framepivot_set(lua_State *L) return luaL_error(L, "Do not alter spriteframepivot_t from within a hook or coroutine!"); if (hud_running) return luaL_error(L, "Do not alter spriteframepivot_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter spriteframepivot_t in CMD building code!"); I_Assert(framepivot != NULL); @@ -686,6 +697,8 @@ static int lib_setState(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter states in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter states in CMD building code!"); // clear the state to start with, in case of missing table elements memset(state,0,sizeof(state_t)); @@ -1006,6 +1019,8 @@ static int lib_setMobjInfo(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter mobjinfo in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter mobjinfo in CMD building code!"); // clear the mobjinfo to start with, in case of missing table elements memset(info,0,sizeof(mobjinfo_t)); @@ -1173,6 +1188,8 @@ static int mobjinfo_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter mobjinfo in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter mobjinfo in CMD building code!"); I_Assert(info != NULL); I_Assert(info >= mobjinfo); @@ -1295,6 +1312,8 @@ static int lib_setSfxInfo(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter sfxinfo in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter sfxinfo in CMD building code!"); lua_pushnil(L); while (lua_next(L, 1)) { @@ -1376,6 +1395,8 @@ static int sfxinfo_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter S_sfx in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter S_sfx in CMD building code!"); I_Assert(sfx != NULL); @@ -1443,6 +1464,8 @@ static int lib_setluabanks(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter luabanks[] in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter luabanks[] in CMD building code!"); lua_remove(L, 1); // don't care about luabanks[] dummy userdata. @@ -1523,6 +1546,8 @@ static int lib_setSkinColor(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter skincolors in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter skincolors in CMD building code!"); // clear the skincolor to start with, in case of missing table elements memset(info,0,sizeof(skincolor_t)); @@ -1697,6 +1722,8 @@ static int colorramp_set(lua_State *L) return luaL_error(L, LUA_QL("skincolor_t") " field 'ramp' index %d out of range (0 - %d)", n, COLORRAMPSIZE-1); if (hud_running) return luaL_error(L, "Do not alter skincolor_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter skincolor_t in CMD building code!"); colorramp[n] = i; skincolor_modified[cnum] = true; return 0; diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 144a6b3f1..3c088f482 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -21,6 +21,7 @@ #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" // hud_running errors +#include "lua_hook.h" // hook_cmd_running errors #include "dehacked.h" #include "fastcmp.h" @@ -577,6 +578,8 @@ static int sector_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter sector_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter sector_t in CMD building code!"); switch(field) { @@ -1716,6 +1719,8 @@ static int ffloor_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter ffloor_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter ffloor_t in CMD building code!"); switch(field) { @@ -1840,6 +1845,8 @@ static int slope_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter pslope_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter pslope_t in CMD building code!"); switch(field) // todo: reorganize this shit { diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c index e2826e160..2f0b2ce66 100644 --- a/src/lua_mobjlib.c +++ b/src/lua_mobjlib.c @@ -20,6 +20,7 @@ #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" // hud_running errors +#include "lua_hook.h" // hook_cmd_running errors static const char *const array_opt[] ={"iterate",NULL}; @@ -427,6 +428,8 @@ static int mobj_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter mobj_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter mobj_t in CMD building code!"); switch(field) { @@ -808,6 +811,8 @@ static int mapthing_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter mapthing_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter mapthing_t in CMD building code!"); if(fastcmp(field,"x")) mt->x = (INT16)luaL_checkinteger(L, 3); diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index 1ce9be525..8c5ce5282 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -20,6 +20,7 @@ #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" // hud_running errors +#include "lua_hook.h" // hook_cmd_running errors static int lib_iteratePlayers(lua_State *L) { @@ -400,6 +401,8 @@ static int player_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter player_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter player_t in CMD building code!"); if (fastcmp(field,"mo") || fastcmp(field,"realmo")) { mobj_t *newmo = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); @@ -770,6 +773,8 @@ static int power_set(lua_State *L) return luaL_error(L, LUA_QL("powertype_t") " cannot be %d", (INT16)p); if (hud_running) return luaL_error(L, "Do not alter player_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter player_t in CMD building code!"); powers[p] = i; return 0; } @@ -815,6 +820,8 @@ static int ticcmd_set(lua_State *L) if (hud_running) return luaL_error(L, "Do not alter player_t in HUD rendering code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter player_t in CMD building code!"); if (fastcmp(field,"forwardmove")) cmd->forwardmove = (SINT8)luaL_checkinteger(L, 3);