Ported Lat's PlayerCmd hook to vanilla SRB2

This commit is contained in:
Zachary McAlpin 2020-07-17 00:08:38 -05:00
parent 7c2f81e4ed
commit d26c7654ff
8 changed files with 109 additions and 5 deletions

View File

@ -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)

View File

@ -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);
}

View File

@ -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
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)

View File

@ -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<<hook_PlayerCmd%8))))
return false;
lua_settop(gL, 0);
lua_pushcfunction(gL, LUA_GetErrorMessage);
hook_cmd_running = true;
for (hookp = roothook; hookp; hookp = hookp->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;
}

View File

@ -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;

View File

@ -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
{

View File

@ -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);

View File

@ -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);