Skip to content

Commit a86e625

Browse files
committed
Merge branch 'luabackports'
2 parents 6a5ad18 + 4d0a96b commit a86e625

File tree

8 files changed

+154
-6
lines changed

8 files changed

+154
-6
lines changed

src/command.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
// Command buffer & command execution
2121
//===================================
2222

23+
24+
/* Lua command registration flags. */
25+
enum
26+
{
27+
COM_ADMIN = 1,
28+
COM_SPLITSCREEN = 2,
29+
COM_LOCAL = 4,
30+
};
31+
2332
typedef void (*com_func_t)(void);
2433

2534
void COM_AddCommand(const char *name, com_func_t func);

src/dehacked.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7237,6 +7237,11 @@ struct {
72377237
{"BT_CUSTOM2",BT_CUSTOM2}, // Lua customizable
72387238
{"BT_CUSTOM3",BT_CUSTOM3}, // Lua customizable
72397239

7240+
// Lua command registration flags
7241+
{"COM_ADMIN",COM_ADMIN},
7242+
{"COM_SPLITSCREEN",COM_SPLITSCREEN},
7243+
{"COM_LOCAL",COM_LOCAL},
7244+
72407245
// cvflags_t
72417246
{"CV_SAVE",CV_SAVE},
72427247
{"CV_CALL",CV_CALL},
@@ -7961,6 +7966,12 @@ static inline int lib_getenum(lua_State *L)
79617966
return 0;
79627967
LUA_PushUserdata(L, &players[adminplayers[0]], META_PLAYER);
79637968
return 1;
7969+
} else if (fastcmp(word,"isserver")) {
7970+
lua_pushboolean(L, server);
7971+
return 1;
7972+
} else if (fastcmp(word, "isdedicatedserver")) {
7973+
lua_pushboolean(L, dedicated);
7974+
return 1;
79647975
} else if (fastcmp(word,"emeralds")) {
79657976
lua_pushinteger(L, emeralds);
79667977
return 1;

src/lua_consolelib.c

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ void COM_Lua_f(void)
112112

113113
lua_rawgeti(gL, -1, 2); // push flags from command info table
114114
if (lua_isboolean(gL, -1))
115-
flags = (lua_toboolean(gL, -1) ? 1 : 0);
115+
flags = (lua_toboolean(gL, -1) ? COM_ADMIN : 0);
116116
else
117117
flags = (UINT8)lua_tointeger(gL, -1);
118118
lua_pop(gL, 1); // pop flags
119119

120-
if (flags & 2) // flag 2: splitscreen player command.
120+
if (flags & COM_SPLITSCREEN) // flag 2: splitscreen player command. TODO: support 4P
121121
{
122122
if (!splitscreen)
123123
{
@@ -127,7 +127,7 @@ void COM_Lua_f(void)
127127
playernum = secondarydisplayplayer;
128128
}
129129

130-
if (netgame)
130+
if (netgame && !( flags & COM_LOCAL ))/* don't send local commands */
131131
{ // Send the command through the network
132132
UINT8 argc;
133133
lua_pop(gL, 1); // pop command info table
@@ -186,7 +186,15 @@ static int lib_comAddCommand(lua_State *L)
186186
if (lua_gettop(L) >= 3)
187187
{ // For the third argument, only take a boolean or a number.
188188
lua_settop(L, 3);
189-
if (lua_type(L, 3) != LUA_TBOOLEAN)
189+
if (lua_type(L, 3) == LUA_TBOOLEAN)
190+
{
191+
CONS_Alert(CONS_WARNING,
192+
"Using a boolean for admin commands is "
193+
"deprecated.\n"
194+
"Use \"COM_ADMIN\" instead.\n"
195+
);
196+
}
197+
else
190198
luaL_checktype(L, 3, LUA_TNUMBER);
191199
}
192200
else
@@ -421,6 +429,46 @@ static int lib_cvRegisterVar(lua_State *L)
421429
return 1;
422430
}
423431

432+
static int CVarSetFunction
433+
(
434+
lua_State *L,
435+
void (*Set)(consvar_t *, const char *),
436+
void (*SetValue)(consvar_t *, INT32)
437+
){
438+
consvar_t *cvar = (consvar_t *)luaL_checkudata(L, 1, META_CVAR);
439+
440+
switch (lua_type(L, 2))
441+
{
442+
case LUA_TSTRING:
443+
(*Set)(cvar, lua_tostring(L, 2));
444+
break;
445+
case LUA_TNUMBER:
446+
(*SetValue)(cvar, (INT32)lua_tonumber(L, 2));
447+
break;
448+
default:
449+
return luaL_typerror(L, 1, "string or number");
450+
}
451+
452+
return 0;
453+
}
454+
455+
static int lib_cvSet(lua_State *L)
456+
{
457+
return CVarSetFunction(L, CV_Set, CV_SetValue);
458+
}
459+
460+
static int lib_cvStealthSet(lua_State *L)
461+
{
462+
return CVarSetFunction(L, CV_StealthSet, CV_StealthSetValue);
463+
}
464+
465+
static int lib_cvAddValue(lua_State *L)
466+
{
467+
consvar_t *cvar = (consvar_t *)luaL_checkudata(L, 1, META_CVAR);
468+
CV_AddValue(cvar, (INT32)luaL_checknumber(L, 2));
469+
return 0;
470+
}
471+
424472
// CONS_Printf for a single player
425473
// Use 'print' in baselib for a global message.
426474
static int lib_consPrintf(lua_State *L)
@@ -459,6 +507,9 @@ static luaL_Reg lib[] = {
459507
{"COM_BufAddText", lib_comBufAddText},
460508
{"COM_BufInsertText", lib_comBufInsertText},
461509
{"CV_RegisterVar", lib_cvRegisterVar},
510+
{"CV_Set", lib_cvSet},
511+
{"CV_StealthSet", lib_cvStealthSet},
512+
{"CV_AddValue", lib_cvAddValue},
462513
{"CONS_Printf", lib_consPrintf},
463514
{NULL, NULL}
464515
};

src/lua_hook.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ enum hook {
1818
hook_MapChange,
1919
hook_MapLoad,
2020
hook_PlayerJoin,
21+
hook_PreThinkFrame,
2122
hook_ThinkFrame,
23+
hook_PostThinkFrame,
2224
hook_MobjSpawn,
2325
hook_MobjCollide,
2426
hook_MobjMoveCollide,
@@ -42,6 +44,7 @@ enum hook {
4244
hook_HurtMsg,
4345
hook_PlayerSpawn,
4446
hook_PlayerQuit,
47+
hook_PlayerThink,
4548

4649
hook_MAX // last hook
4750
};
@@ -50,10 +53,13 @@ extern const char *const hookNames[];
5053
void LUAh_MapChange(INT16 mapnumber); // Hook for map change (before load)
5154
void LUAh_MapLoad(void); // Hook for map load
5255
void LUAh_PlayerJoin(int playernum); // Hook for Got_AddPlayer
56+
void LUAh_PreThinkFrame(void); // Hook for frame (before mobj and player thinkers)
5357
void LUAh_ThinkFrame(void); // Hook for frame (after mobj and player thinkers)
58+
void LUAh_PostThinkFrame(void); // Hook for frame (at end of tick, ie after overlays, precipitation, specials)
5459
boolean LUAh_MobjHook(mobj_t *mo, enum hook which);
5560
boolean LUAh_PlayerHook(player_t *plr, enum hook which);
5661
#define LUAh_MobjSpawn(mo) LUAh_MobjHook(mo, hook_MobjSpawn) // Hook for P_SpawnMobj by mobj type
62+
#define LUAh_PlayerThink(player) LUAh_PlayerHook(player, hook_PlayerThink) // Hook for P_PlayerThink
5763
UINT8 LUAh_MobjCollideHook(mobj_t *thing1, mobj_t *thing2, enum hook which);
5864
#define LUAh_MobjCollide(thing1, thing2) LUAh_MobjCollideHook(thing1, thing2, hook_MobjCollide) // Hook for PIT_CheckThing by (thing) mobj type
5965
#define LUAh_MobjMoveCollide(thing1, thing2) LUAh_MobjCollideHook(thing1, thing2, hook_MobjMoveCollide) // Hook for PIT_CheckThing by (tmthing) mobj type

src/lua_hooklib.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ const char *const hookNames[hook_MAX+1] = {
3131
"MapChange",
3232
"MapLoad",
3333
"PlayerJoin",
34+
"PreThinkFrame",
3435
"ThinkFrame",
36+
"PostThinkFrame",
3537
"MobjSpawn",
3638
"MobjCollide",
3739
"MobjMoveCollide",
@@ -55,6 +57,7 @@ const char *const hookNames[hook_MAX+1] = {
5557
"HurtMsg",
5658
"PlayerSpawn",
5759
"PlayerQuit",
60+
"PlayerThink",
5861
NULL
5962
};
6063

@@ -190,6 +193,7 @@ static int lib_addHook(lua_State *L)
190193
case hook_SpinSpecial:
191194
case hook_JumpSpinSpecial:
192195
case hook_PlayerSpawn:
196+
case hook_PlayerThink:
193197
lastp = &playerhooks;
194198
break;
195199
case hook_LinedefExecute:
@@ -383,6 +387,29 @@ void LUAh_PlayerJoin(int playernum)
383387
lua_settop(gL, 0);
384388
}
385389

390+
// Hook for frame (before mobj and player thinkers)
391+
void LUAh_PreThinkFrame(void)
392+
{
393+
hook_p hookp;
394+
if (!gL || !(hooksAvailable[hook_PreThinkFrame/8] & (1<<(hook_PreThinkFrame%8))))
395+
return;
396+
397+
for (hookp = roothook; hookp; hookp = hookp->next)
398+
{
399+
if (hookp->type != hook_PreThinkFrame)
400+
continue;
401+
402+
lua_pushfstring(gL, FMT_HOOKID, hookp->id);
403+
lua_gettable(gL, LUA_REGISTRYINDEX);
404+
if (lua_pcall(gL, 0, 0, 0)) {
405+
if (!hookp->error || cv_debug & DBG_LUA)
406+
CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL, -1));
407+
lua_pop(gL, 1);
408+
hookp->error = true;
409+
}
410+
}
411+
}
412+
386413
// Hook for frame (after mobj and player thinkers)
387414
void LUAh_ThinkFrame(void)
388415
{
@@ -420,6 +447,30 @@ void LUAh_ThinkFrame(void)
420447
}
421448
}
422449

450+
451+
// Hook for frame (at end of tick, ie after overlays, precipitation, specials)
452+
void LUAh_PostThinkFrame(void)
453+
{
454+
hook_p hookp;
455+
if (!gL || !(hooksAvailable[hook_PostThinkFrame/8] & (1<<(hook_PostThinkFrame%8))))
456+
return;
457+
458+
for (hookp = roothook; hookp; hookp = hookp->next)
459+
{
460+
if (hookp->type != hook_PostThinkFrame)
461+
continue;
462+
463+
lua_pushfstring(gL, FMT_HOOKID, hookp->id);
464+
lua_gettable(gL, LUA_REGISTRYINDEX);
465+
if (lua_pcall(gL, 0, 0, 0)) {
466+
if (!hookp->error || cv_debug & DBG_LUA)
467+
CONS_Alert(CONS_WARNING,"%s\n",lua_tostring(gL, -1));
468+
lua_pop(gL, 1);
469+
hookp->error = true;
470+
}
471+
}
472+
}
473+
423474
// Hook for mobj collisions
424475
UINT8 LUAh_MobjCollideHook(mobj_t *thing1, mobj_t *thing2, enum hook which)
425476
{

src/lua_script.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void COM_Lua_f(void);
7575
static UINT8 seen = 0;\
7676
if (!seen) {\
7777
seen = 1;\
78-
CONS_Alert(CONS_WARNING,"\"%s\" is deprecated and will be removed.\nUse \"%s\" instead.\n", this_func, use_instead);\
78+
CONS_Alert(CONS_WARNING,"\"%s\" is deprecated.\nUse \"%s\" instead.\n", this_func, use_instead);\
7979
}\
8080
}
8181

src/p_tick.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ void P_Ticker(boolean run)
630630
ps_lua_mobjhooks.value.i = 0;
631631
ps_checkposition_calls.value.i = 0;
632632

633+
LUAh_PreThinkFrame();
634+
633635
PS_START_TIMING(ps_playerthink_time);
634636
for (i = 0; i < MAXPLAYERS; i++)
635637
if (playeringame[i] && players[i].mo && !P_MobjWasRemoved(players[i].mo))
@@ -738,6 +740,8 @@ void P_Ticker(boolean run)
738740
G_ConsGhostTic();
739741
if (modeattacking)
740742
G_GhostTicker();
743+
744+
LUAh_PostThinkFrame();
741745
}
742746

743747
if (run)
@@ -796,6 +800,8 @@ void P_PreTicker(INT32 frames)
796800
{
797801
P_MapStart();
798802

803+
LUAh_PreThinkFrame();
804+
799805
R_UpdateMobjInterpolators();
800806

801807
for (i = 0; i < MAXPLAYERS; i++)
@@ -833,7 +839,9 @@ void P_PreTicker(INT32 frames)
833839
R_UpdateLevelInterpolators();
834840
R_UpdateViewInterpolation();
835841
R_ResetViewInterpolation(0);
836-
842+
843+
LUAh_PostThinkFrame();
844+
837845
P_MapEnd();
838846
}
839847
}

src/p_user.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8745,7 +8745,10 @@ void P_PlayerThink(player_t *player)
87458745
player->playerstate = PST_REBORN;
87468746
}
87478747
if (player->playerstate == PST_REBORN)
8748+
{
8749+
LUAh_PlayerThink(player);
87488750
return;
8751+
}
87498752
}
87508753

87518754
#ifdef SEENAMES
@@ -8857,7 +8860,10 @@ void P_PlayerThink(player_t *player)
88578860
player->lives = 0;
88588861

88598862
if (player->playerstate == PST_DEAD)
8863+
{
8864+
LUAh_PlayerThink(player);
88608865
return;
8866+
}
88618867
}
88628868
}
88638869

@@ -8959,6 +8965,7 @@ void P_PlayerThink(player_t *player)
89598965
{
89608966
player->mo->flags2 &= ~MF2_SHADOW;
89618967
P_DeathThink(player);
8968+
LUAh_PlayerThink(player);
89628969

89638970
return;
89648971
}
@@ -9075,7 +9082,10 @@ void P_PlayerThink(player_t *player)
90759082
P_MovePlayer(player);
90769083

90779084
if (!player->mo)
9085+
{
9086+
LUAh_PlayerThink(player);
90789087
return; // P_MovePlayer removed player->mo.
9088+
}
90799089

90809090
// Unset statis flags after moving.
90819091
// In other words, if you manually set stasis via code,
@@ -9249,6 +9259,8 @@ void P_PlayerThink(player_t *player)
92499259

92509260
player->pflags &= ~PF_SLIDING;
92519261

9262+
LUAh_PlayerThink(player);
9263+
92529264
/*
92539265
// Colormap verification
92549266
{

0 commit comments

Comments
 (0)