Merge branch 'lua-ports' into 'next'

Some Lua ports from 2.2

See merge request KartKrew/Kart-Public!241
This commit is contained in:
Sal 2020-11-07 18:30:59 -05:00
commit 4c2b6de02c
7 changed files with 169 additions and 13 deletions

View file

@ -20,6 +20,14 @@
// Command buffer & command execution // Command buffer & command execution
//=================================== //===================================
/* Lua command registration flags. */
enum
{
COM_ADMIN = 1,
COM_SPLITSCREEN = 2,
COM_LOCAL = 4,
};
typedef void (*com_func_t)(void); typedef void (*com_func_t)(void);
void COM_AddCommand(const char *name, com_func_t func); void COM_AddCommand(const char *name, com_func_t func);

View file

@ -8685,6 +8685,11 @@ struct {
{"BT_CUSTOM2",BT_CUSTOM2}, // Lua customizable {"BT_CUSTOM2",BT_CUSTOM2}, // Lua customizable
{"BT_CUSTOM3",BT_CUSTOM3}, // Lua customizable {"BT_CUSTOM3",BT_CUSTOM3}, // Lua customizable
// Lua command registration flags
{"COM_ADMIN",COM_ADMIN},
{"COM_SPLITSCREEN",COM_SPLITSCREEN},
{"COM_LOCAL",COM_LOCAL},
// cvflags_t // cvflags_t
{"CV_SAVE",CV_SAVE}, {"CV_SAVE",CV_SAVE},
{"CV_CALL",CV_CALL}, {"CV_CALL",CV_CALL},
@ -9713,12 +9718,12 @@ static inline int lib_getenum(lua_State *L)
return 0; return 0;
LUA_PushUserdata(L, &players[consoleplayer], META_PLAYER); LUA_PushUserdata(L, &players[consoleplayer], META_PLAYER);
return 1; return 1;
/*} else if (fastcmp(word,"admin")) { } else if (fastcmp(word,"isserver")) {
LUA_Deprecated(L, "admin", "IsPlayerAdmin(player)"); lua_pushboolean(L, server);
if (!playeringame[adminplayers[0]] || IsPlayerAdmin(serverplayer)) return 1;
return 0; } else if (fastcmp(word, "isdedicatedserver")) {
LUA_PushUserdata(L, &players[adminplayers[0]], META_PLAYER); lua_pushboolean(L, dedicated);
return 1;*/ return 1;
} else if (fastcmp(word,"gravity")) { } else if (fastcmp(word,"gravity")) {
lua_pushinteger(L, gravity); lua_pushinteger(L, gravity);
return 1; return 1;

View file

@ -113,12 +113,12 @@ void COM_Lua_f(void)
lua_rawgeti(gL, -1, 2); // push flags from command info table lua_rawgeti(gL, -1, 2); // push flags from command info table
if (lua_isboolean(gL, -1)) if (lua_isboolean(gL, -1))
flags = (lua_toboolean(gL, -1) ? 1 : 0); flags = (lua_toboolean(gL, -1) ? COM_ADMIN : 0);
else else
flags = (UINT8)lua_tointeger(gL, -1); flags = (UINT8)lua_tointeger(gL, -1);
lua_pop(gL, 1); // pop flags lua_pop(gL, 1); // pop flags
if (flags & 2) // flag 2: splitscreen player command. TODO: support 4P if (flags & COM_SPLITSCREEN) // flag 2: splitscreen player command. TODO: support 4P
{ {
if (!splitscreen) if (!splitscreen)
{ {
@ -128,12 +128,12 @@ void COM_Lua_f(void)
playernum = displayplayers[1]; playernum = displayplayers[1];
} }
if (netgame) if (netgame && !( flags & COM_LOCAL ))/* don't send local commands */
{ // Send the command through the network { // Send the command through the network
UINT8 argc; UINT8 argc;
lua_pop(gL, 1); // pop command info table lua_pop(gL, 1); // pop command info table
if (flags & 1 && !server && !IsPlayerAdmin(playernum)) // flag 1: only server/admin can use this command. if (flags & COM_ADMIN && !server && !IsPlayerAdmin(playernum)) // flag 1: only server/admin can use this command.
{ {
CONS_Printf(M_GetText("Only the server or a remote admin can use this.\n")); CONS_Printf(M_GetText("Only the server or a remote admin can use this.\n"));
return; return;
@ -187,7 +187,15 @@ static int lib_comAddCommand(lua_State *L)
if (lua_gettop(L) >= 3) if (lua_gettop(L) >= 3)
{ // For the third argument, only take a boolean or a number. { // For the third argument, only take a boolean or a number.
lua_settop(L, 3); lua_settop(L, 3);
if (lua_type(L, 3) != LUA_TBOOLEAN) if (lua_type(L, 3) == LUA_TBOOLEAN)
{
CONS_Alert(CONS_WARNING,
"Using a boolean for admin commands is "
"deprecated and will be removed.\n"
"Use \"COM_ADMIN\" instead.\n"
);
}
else
luaL_checktype(L, 3, LUA_TNUMBER); luaL_checktype(L, 3, LUA_TNUMBER);
} }
else else
@ -436,6 +444,45 @@ static int lib_cvFindVar(lua_State *L)
return 0; return 0;
} }
static int CVarSetFunction
(
lua_State *L,
void (*Set)(consvar_t *, const char *),
void (*SetValue)(consvar_t *, INT32)
){
consvar_t *cvar = (consvar_t *)luaL_checkudata(L, 1, META_CVAR);
switch (lua_type(L, 2))
{
case LUA_TSTRING:
(*Set)(cvar, lua_tostring(L, 2));
break;
case LUA_TNUMBER:
(*SetValue)(cvar, (INT32)lua_tonumber(L, 2));
break;
default:
return luaL_typerror(L, 1, "string or number");
}
return 0;
}
static int lib_cvSet(lua_State *L)
{
return CVarSetFunction(L, CV_Set, CV_SetValue);
}
static int lib_cvStealthSet(lua_State *L)
{
return CVarSetFunction(L, CV_StealthSet, CV_StealthSetValue);
}
static int lib_cvAddValue(lua_State *L)
{
consvar_t *cvar = (consvar_t *)luaL_checkudata(L, 1, META_CVAR);
CV_AddValue(cvar, (INT32)luaL_checknumber(L, 2));
return 0;
}
// CONS_Printf for a single player // CONS_Printf for a single player
// Use 'print' in baselib for a global message. // Use 'print' in baselib for a global message.
@ -475,8 +522,11 @@ static luaL_Reg lib[] = {
{"COM_BufAddText", lib_comBufAddText}, {"COM_BufAddText", lib_comBufAddText},
{"COM_BufInsertText", lib_comBufInsertText}, {"COM_BufInsertText", lib_comBufInsertText},
{"CV_RegisterVar", lib_cvRegisterVar}, {"CV_RegisterVar", lib_cvRegisterVar},
{"CONS_Printf", lib_consPrintf},
{"CV_FindVar", lib_cvFindVar}, {"CV_FindVar", lib_cvFindVar},
{"CV_Set", lib_cvSet},
{"CV_StealthSet", lib_cvStealthSet},
{"CV_AddValue", lib_cvAddValue},
{"CONS_Printf", lib_consPrintf},
{NULL, NULL} {NULL, NULL}
}; };

View file

@ -20,7 +20,9 @@ enum hook {
hook_MapChange, hook_MapChange,
hook_MapLoad, hook_MapLoad,
hook_PlayerJoin, hook_PlayerJoin,
hook_PreThinkFrame,
hook_ThinkFrame, hook_ThinkFrame,
hook_PostThinkFrame,
hook_MobjSpawn, hook_MobjSpawn,
hook_MobjCollide, hook_MobjCollide,
hook_MobjMoveCollide, hook_MobjMoveCollide,
@ -44,6 +46,7 @@ enum hook {
hook_HurtMsg, hook_HurtMsg,
hook_PlayerSpawn, hook_PlayerSpawn,
hook_PlayerQuit, hook_PlayerQuit,
hook_PlayerThink,
hook_MusicChange, hook_MusicChange,
hook_ShouldSpin, //SRB2KART hook_ShouldSpin, //SRB2KART
hook_ShouldExplode, //SRB2KART hook_ShouldExplode, //SRB2KART
@ -64,7 +67,9 @@ extern boolean hook_cmd_running; // This is used by PlayerCmd and lua_playerlib
void LUAh_MapChange(INT16 mapnumber); // Hook for map change (before load) void LUAh_MapChange(INT16 mapnumber); // Hook for map change (before load)
void LUAh_MapLoad(void); // Hook for map load void LUAh_MapLoad(void); // Hook for map load
void LUAh_PlayerJoin(int playernum); // Hook for Got_AddPlayer void LUAh_PlayerJoin(int playernum); // Hook for Got_AddPlayer
void LUAh_PreThinkFrame(void); // Hook for frame (before mobj and player thinkers)
void LUAh_ThinkFrame(void); // Hook for frame (after mobj and player thinkers) void LUAh_ThinkFrame(void); // Hook for frame (after mobj and player thinkers)
void LUAh_PostThinkFrame(void); // Hook for frame (at end of tick, ie after overlays, precipitation, specials)
boolean LUAh_MobjHook(mobj_t *mo, enum hook which); boolean LUAh_MobjHook(mobj_t *mo, enum hook which);
boolean LUAh_PlayerHook(player_t *plr, enum hook which); boolean LUAh_PlayerHook(player_t *plr, enum hook which);
#define LUAh_MobjSpawn(mo) LUAh_MobjHook(mo, hook_MobjSpawn) // Hook for P_SpawnMobj by mobj type #define LUAh_MobjSpawn(mo) LUAh_MobjHook(mo, hook_MobjSpawn) // Hook for P_SpawnMobj by mobj type
@ -106,5 +111,6 @@ boolean LUAh_PlayerCmd(player_t *player, ticcmd_t *cmd); // Allows to write to p
void LUAh_IntermissionThinker(void); // Hook for Y_Ticker void LUAh_IntermissionThinker(void); // Hook for Y_Ticker
void LUAh_VoteThinker(void); // Hook for Y_VoteTicker void LUAh_VoteThinker(void); // Hook for Y_VoteTicker
#define LUAh_PlayerThink(player) LUAh_PlayerHook(player, hook_PlayerThink) // Hook for P_PlayerThink
#endif #endif

View file

@ -31,7 +31,9 @@ const char *const hookNames[hook_MAX+1] = {
"MapChange", "MapChange",
"MapLoad", "MapLoad",
"PlayerJoin", "PlayerJoin",
"PreThinkFrame",
"ThinkFrame", "ThinkFrame",
"PostThinkFrame",
"MobjSpawn", "MobjSpawn",
"MobjCollide", "MobjCollide",
"MobjMoveCollide", "MobjMoveCollide",
@ -55,6 +57,7 @@ const char *const hookNames[hook_MAX+1] = {
"HurtMsg", "HurtMsg",
"PlayerSpawn", "PlayerSpawn",
"PlayerQuit", "PlayerQuit",
"PlayerThink",
"MusicChange", "MusicChange",
"ShouldSpin", "ShouldSpin",
"ShouldExplode", "ShouldExplode",
@ -206,6 +209,7 @@ static int lib_addHook(lua_State *L)
case hook_SpinSpecial: case hook_SpinSpecial:
case hook_JumpSpinSpecial: case hook_JumpSpinSpecial:
case hook_PlayerSpawn: case hook_PlayerSpawn:
case hook_PlayerThink:
lastp = &playerhooks; lastp = &playerhooks;
break; break;
case hook_LinedefExecute: case hook_LinedefExecute:
@ -402,6 +406,29 @@ void LUAh_PlayerJoin(int playernum)
lua_settop(gL, 0); lua_settop(gL, 0);
} }
// Hook for frame (before mobj and player thinkers)
void LUAh_PreThinkFrame(void)
{
hook_p hookp;
if (!gL || !(hooksAvailable[hook_PreThinkFrame/8] & (1<<(hook_PreThinkFrame%8))))
return;
for (hookp = roothook; hookp; hookp = hookp->next)
{
if (hookp->type != hook_PreThinkFrame)
continue;
lua_pushfstring(gL, FMT_HOOKID, hookp->id);
lua_gettable(gL, LUA_REGISTRYINDEX);
if (lua_pcall(gL, 0, 0, 0)) {
if (!hookp->error || cv_debug & DBG_LUA)
CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL, -1));
lua_pop(gL, 1);
hookp->error = true;
}
}
}
// Hook for frame (after mobj and player thinkers) // Hook for frame (after mobj and player thinkers)
void LUAh_ThinkFrame(void) void LUAh_ThinkFrame(void)
{ {
@ -423,6 +450,29 @@ void LUAh_ThinkFrame(void)
} }
} }
// Hook for frame (at end of tick, ie after overlays, precipitation, specials)
void LUAh_PostThinkFrame(void)
{
hook_p hookp;
if (!gL || !(hooksAvailable[hook_PostThinkFrame/8] & (1<<(hook_PostThinkFrame%8))))
return;
for (hookp = roothook; hookp; hookp = hookp->next)
{
if (hookp->type != hook_PostThinkFrame)
continue;
lua_pushfstring(gL, FMT_HOOKID, hookp->id);
lua_gettable(gL, LUA_REGISTRYINDEX);
if (lua_pcall(gL, 0, 0, 0)) {
if (!hookp->error || cv_debug & DBG_LUA)
CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL, -1));
lua_pop(gL, 1);
hookp->error = true;
}
}
}
// Hook for Y_Ticker // Hook for Y_Ticker
void LUAh_IntermissionThinker(void) void LUAh_IntermissionThinker(void)
{ {

View file

@ -648,6 +648,10 @@ void P_Ticker(boolean run)
#endif #endif
} }
#ifdef HAVE_BLUA
LUAh_PreThinkFrame();
#endif
for (i = 0; i < MAXPLAYERS; i++) for (i = 0; i < MAXPLAYERS; i++)
if (playeringame[i] && players[i].mo && !P_MobjWasRemoved(players[i].mo)) if (playeringame[i] && players[i].mo && !P_MobjWasRemoved(players[i].mo))
P_PlayerThink(&players[i]); P_PlayerThink(&players[i]);
@ -779,6 +783,10 @@ void P_Ticker(boolean run)
&& --mapreset <= 1 && --mapreset <= 1
&& server) // Remember: server uses it for mapchange, but EVERYONE ticks down for the animation && server) // Remember: server uses it for mapchange, but EVERYONE ticks down for the animation
D_MapChange(gamemap, gametype, encoremode, true, 0, false, false); D_MapChange(gamemap, gametype, encoremode, true, 0, false, false);
#ifdef HAVE_BLUA
LUAh_PostThinkFrame();
#endif
} }
// Always move the camera. // Always move the camera.
@ -809,6 +817,10 @@ void P_PreTicker(INT32 frames)
{ {
P_MapStart(); P_MapStart();
#ifdef HAVE_BLUA
LUAh_PreThinkFrame();
#endif
for (i = 0; i < MAXPLAYERS; i++) for (i = 0; i < MAXPLAYERS; i++)
if (playeringame[i] && players[i].mo && !P_MobjWasRemoved(players[i].mo)) if (playeringame[i] && players[i].mo && !P_MobjWasRemoved(players[i].mo))
{ {
@ -843,6 +855,10 @@ void P_PreTicker(INT32 frames)
P_UpdateSpecials(); P_UpdateSpecials();
P_RespawnSpecials(); P_RespawnSpecials();
#ifdef HAVE_BLUA
LUAh_PostThinkFrame();
#endif
P_MapEnd(); P_MapEnd();
} }
} }

View file

@ -8244,8 +8244,13 @@ void P_PlayerThink(player_t *player)
player->playerstate = PST_REBORN; player->playerstate = PST_REBORN;
} }
if (player->playerstate == PST_REBORN) if (player->playerstate == PST_REBORN)
{
#ifdef HAVE_BLUA
LUAh_PlayerThink(player);
#endif
return; return;
} }
}
#ifdef SEENAMES #ifdef SEENAMES
if (netgame && player == &players[displayplayers[0]] && !(leveltime % (TICRATE/5)) && !splitscreen) if (netgame && player == &players[displayplayers[0]] && !(leveltime % (TICRATE/5)) && !splitscreen)
@ -8368,9 +8373,14 @@ void P_PlayerThink(player_t *player)
P_DoTimeOver(player); P_DoTimeOver(player);
if (player->playerstate == PST_DEAD) if (player->playerstate == PST_DEAD)
{
#ifdef HAVE_BLUA
LUAh_PlayerThink(player);
#endif
return; return;
} }
} }
}
// If it is set, start subtracting // If it is set, start subtracting
// Don't allow it to go back to 0 // Don't allow it to go back to 0
@ -8432,7 +8442,9 @@ void P_PlayerThink(player_t *player)
else else
player->mo->flags2 &= ~MF2_SHADOW; player->mo->flags2 &= ~MF2_SHADOW;
P_DeathThink(player); P_DeathThink(player);
#ifdef HAVE_BLUA
LUAh_PlayerThink(player);
#endif
return; return;
} }
@ -8568,7 +8580,12 @@ void P_PlayerThink(player_t *player)
P_MovePlayer(player); P_MovePlayer(player);
if (!player->mo) if (!player->mo)
{
#ifdef HAVE_BLUA
LUAh_PlayerThink(player);
#endif
return; // P_MovePlayer removed player->mo. return; // P_MovePlayer removed player->mo.
}
// Unset statis flags after moving. // Unset statis flags after moving.
// In other words, if you manually set stasis via code, // In other words, if you manually set stasis via code,
@ -8764,6 +8781,10 @@ void P_PlayerThink(player_t *player)
K_KartPlayerThink(player, cmd); // SRB2kart K_KartPlayerThink(player, cmd); // SRB2kart
#ifdef HAVE_BLUA
LUAh_PlayerThink(player);
#endif
/* /*
// Colormap verification // Colormap verification
{ {