From df97bef4b5dd66d39ae8aee7899182a3e26e6a25 Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 22 Nov 2024 00:39:53 +0100 Subject: [PATCH] Refactor HUD hooks too --- src/hu_stuff.c | 2 +- src/lua_hook.h | 97 +++++++++++---------- src/lua_hooklib.c | 55 ++++++++++++ src/lua_hud.h | 5 +- src/lua_hudlib.c | 210 ++++------------------------------------------ src/st_stuff.c | 2 +- src/y_inter.c | 4 +- 7 files changed, 129 insertions(+), 246 deletions(-) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index 8a999b83f..6cbb46148 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -2274,7 +2274,7 @@ void HU_Drawer(void) if (renderisnewtic) { LUA_HUD_ClearDrawList(luahuddrawlist_scores); - LUAh_ScoresHUD(luahuddrawlist_scores); + LUA_HUDHOOK(scores, luahuddrawlist_scores); } LUA_HUD_DrawList(luahuddrawlist_scores); diff --git a/src/lua_hook.h b/src/lua_hook.h index ee43b90d2..45237aa80 100644 --- a/src/lua_hook.h +++ b/src/lua_hook.h @@ -13,6 +13,7 @@ #include "r_defs.h" #include "d_player.h" #include "s_sound.h" +#include "lua_hudlib_drawlist.h" /* * Do you know what an 'X Macro' is? Such a macro is called over each element of @@ -22,52 +23,58 @@ */ #define MOBJ_HOOK_LIST(X) \ -X (MobjSpawn),/* P_SpawnMobj */\ -X (MobjCollide),/* PIT_CheckThing */\ -X (MobjMoveCollide),/* tritto */\ -X (TouchSpecial),/* P_TouchSpecialThing */\ -X (MobjFuse),/* when mobj->fuse runs out */\ -X (MobjThinker),/* P_MobjThinker, P_SceneryThinker */\ -X (BossThinker),/* P_GenericBossThinker */\ -X (ShouldDamage),/* P_DamageMobj (Should mobj take damage?) */\ -X (MobjDamage),/* P_DamageMobj (Mobj actually takes damage!) */\ -X (MobjDeath),/* P_KillMobj */\ -X (BossDeath),/* A_BossDeath */\ -X (MobjRemoved),/* P_RemoveMobj */\ -X (HurtMsg),/* imhurttin */\ + X (MobjSpawn),/* P_SpawnMobj */\ + X (MobjCollide),/* PIT_CheckThing */\ + X (MobjMoveCollide),/* tritto */\ + X (TouchSpecial),/* P_TouchSpecialThing */\ + X (MobjFuse),/* when mobj->fuse runs out */\ + X (MobjThinker),/* P_MobjThinker, P_SceneryThinker */\ + X (BossThinker),/* P_GenericBossThinker */\ + X (ShouldDamage),/* P_DamageMobj (Should mobj take damage?) */\ + X (MobjDamage),/* P_DamageMobj (Mobj actually takes damage!) */\ + X (MobjDeath),/* P_KillMobj */\ + X (BossDeath),/* A_BossDeath */\ + X (MobjRemoved),/* P_RemoveMobj */\ + X (HurtMsg),/* imhurttin */\ #define HOOK_LIST(X) \ -X (NetVars),/* add to archive table (netsave) */\ -X (MapChange),/* (before map load) */\ -X (MapLoad),\ -X (PlayerJoin),/* Got_AddPlayer */\ -X (PreThinkFrame)/* frame (before mobj and player thinkers) */,\ -X (ThinkFrame),/* frame (after mobj and player thinkers) */\ -X (PostThinkFrame),/* frame (at end of tick, ie after overlays, precipitation, specials) */\ -X (JumpSpecial),/* P_DoJumpStuff (Any-jumping) */\ -X (AbilitySpecial),/* P_DoJumpStuff (Double-jumping) */\ -X (SpinSpecial),/* P_DoSpinAbility (Spin button effect) */\ -X (JumpSpinSpecial),/* P_DoJumpStuff (Spin button effect (mid-air)) */\ -X (BotTiccmd),/* B_BuildTiccmd */\ -X (PlayerMsg),/* chat messages */\ -X (PlayerSpawn),/* G_SpawnPlayer */\ -X (PlayerQuit),\ -X (PlayerThink),/* P_PlayerThink */\ -X (MusicChange),\ -X (ShouldSpin),/*SRB2KART*/\ -X (ShouldExplode),/*SRB2KART*/\ -X (ShouldSquish),/*SRB2KART*/\ -X (PlayerSpin),/*SRB2KART*/\ -X (PlayerExplode),/*SRB2KART*/\ -X (PlayerSquish),/*SRB2KART*/\ -X (PlayerCmd),/* building the player's ticcmd struct (Ported from SRB2Kart) */\ -X (IntermissionThinker),/* Y_Ticker */\ -X (VoteThinker),/*SRB2KART*/\ -X (ServerJoin),/* SRB2KART - Saturn 32p*/\ + X (NetVars),/* add to archive table (netsave) */\ + X (MapChange),/* (before map load) */\ + X (MapLoad),\ + X (PlayerJoin),/* Got_AddPlayer */\ + X (PreThinkFrame)/* frame (before mobj and player thinkers) */,\ + X (ThinkFrame),/* frame (after mobj and player thinkers) */\ + X (PostThinkFrame),/* frame (at end of tick, ie after overlays, precipitation, specials) */\ + X (JumpSpecial),/* P_DoJumpStuff (Any-jumping) */\ + X (AbilitySpecial),/* P_DoJumpStuff (Double-jumping) */\ + X (SpinSpecial),/* P_DoSpinAbility (Spin button effect) */\ + X (JumpSpinSpecial),/* P_DoJumpStuff (Spin button effect (mid-air)) */\ + X (BotTiccmd),/* B_BuildTiccmd */\ + X (PlayerMsg),/* chat messages */\ + X (PlayerSpawn),/* G_SpawnPlayer */\ + X (PlayerQuit),\ + X (PlayerThink),/* P_PlayerThink */\ + X (MusicChange),\ + X (ShouldSpin),/*SRB2KART*/\ + X (ShouldExplode),/*SRB2KART*/\ + X (ShouldSquish),/*SRB2KART*/\ + X (PlayerSpin),/*SRB2KART*/\ + X (PlayerExplode),/*SRB2KART*/\ + X (PlayerSquish),/*SRB2KART*/\ + X (PlayerCmd),/* building the player's ticcmd struct (Ported from SRB2Kart) */\ + X (IntermissionThinker),/* Y_Ticker */\ + X (VoteThinker),/*SRB2KART*/\ + X (ServerJoin),/* SRB2KART - Saturn 32p*/\ #define STRING_HOOK_LIST(X) \ -X (BotAI),/* B_BuildTailsTiccmd by skin name */\ -X (LinedefExecute),\ + X (BotAI),/* B_BuildTailsTiccmd by skin name */\ + X (LinedefExecute),\ + +#define HUD_HOOK_LIST(X) \ + X (game),\ + X (scores),/* emblems/multiplayer list */\ + X (intermission),\ + X (vote),\ /* * I chose to access the hook enums through a macro as well. This could provide @@ -79,25 +86,27 @@ X (LinedefExecute),\ #define MOBJ_HOOK(name) mobjhook_ ## name #define HOOK(name) hook_ ## name +#define HUD_HOOK(name) hudhook_ ## name #define STRING_HOOK(name) stringhook_ ## name - #define ENUM(X) enum { X ## _LIST (X) X(MAX) } ENUM (MOBJ_HOOK); ENUM (HOOK); +ENUM (HUD_HOOK); ENUM (STRING_HOOK); #undef ENUM /* dead simple, LUA_HOOK(GameQuit) */ #define LUA_HOOK(type) LUA_HookVoid(HOOK(type)) - +#define LUA_HUDHOOK(type,drawlist) LUA_HookHUD(HUD_HOOK(type),(drawlist)) extern boolean hook_cmd_running; // This is used by PlayerCmd and lua_playerlib to prevent anything from being wirtten to player while we run PlayerCmd. extern int hook_defrosting; void LUA_HookVoid(int hook); +void LUA_HookHUD(int hook, huddrawlist_h drawlist); int LUA_HookMobj(mobj_t *, int hook); int LUA_Hook2Mobj(mobj_t *, mobj_t *, int hook); diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index cc9a61695..8acc418d3 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -33,6 +33,7 @@ LIST (mobjHookNames, MOBJ_HOOK_LIST); LIST (hookNames, HOOK_LIST); +LIST (hudHookNames, HUD_HOOK_LIST); LIST (stringHookNames, STRING_HOOK_LIST); #undef LIST @@ -48,6 +49,7 @@ typedef struct { } stringhook_t; static hook_t hookIds[HOOK(MAX)]; +static hook_t hudHookIds[HUD_HOOK(MAX)]; static hook_t mobjHookIds[NUMMOBJTYPES][MOBJ_HOOK(MAX)]; // Lua tables are used to lookup string hook ids. @@ -189,6 +191,12 @@ static void add_mobj_hook(lua_State *L, int hook_type) add_hook(&mobjHookIds[mobj_type][hook_type]); } +static void add_hud_hook(lua_State *L, int idx) +{ + add_hook(&hudHookIds[luaL_checkoption(L, + idx, "game", hudHookNames)]); +} + static void add_hook_ref(lua_State *L, int idx) { if (!(nextid & 7)) @@ -231,6 +239,10 @@ static int lib_addHook(lua_State *L) { add_hook(&hookIds[type]); } + /*else if (strcmp(name, "HUD") == 0) // ehh this is cool and all, but i dont want modders potentially breaking vanilla clients lol + { + add_hud_hook(L, 3); + }*/ else { return luaL_argerror(L, 1, lua_pushfstring(L, "invalid hook " LUA_QS, name)); @@ -251,6 +263,18 @@ int LUA_HookLib(lua_State *L) return 0; } + +int lib_hudadd(lua_State *L);/* yeah compiler */ +int lib_hudadd(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TFUNCTION); + + add_hud_hook(L, 2); + add_hook_ref(L, 1); + + return 0; +} + typedef struct Hook_State Hook_State; typedef void (*Hook_Callback)(Hook_State *); @@ -545,6 +569,14 @@ static void res_force(Hook_State *hook) } } +static void res_hud(Hook_State *hook) +{ + (void)hook; + + hud_interpolate = hud_interpstring = hud_interplatch = false; + hud_interpcounter++; +} + /* ========================================================================= GENERALISED HOOKS ========================================================================= */ @@ -629,6 +661,29 @@ int LUA_HookTiccmd(player_t *player, ticcmd_t *cmd, int hook_type) return hook.status; } +void LUA_HookHUD(int hook_type, huddrawlist_h list) +{ + const hook_t * map = &hudHookIds[hook_type]; + Hook_State hook; + if (map->numHooks > 0) + { + start_hook_stack(); + begin_hook_values(&hook); + + LUA_SetHudHook(hook_type, list); + + hud_running = true; // local hook + hud_interpcounter = 0; + hud_interpolate = hud_interpstring = hud_interplatch = false; + init_hook_call(&hook, 1, res_hud); + call_mapped(&hook, map); + hud_running = false; + + lua_pushnil(gL); + lua_setfield(gL, LUA_REGISTRYINDEX, "HUD_DRAW_LIST"); + } +} + /* ========================================================================= SPECIALIZED HOOKS ========================================================================= */ diff --git a/src/lua_hud.h b/src/lua_hud.h index 5d1f41c83..76ce9eb48 100644 --- a/src/lua_hud.h +++ b/src/lua_hud.h @@ -50,9 +50,6 @@ extern boolean hud_interplatch; // string mode was toggled boolean LUA_HudEnabled(enum hud option); -void LUAh_GameHUD(huddrawlist_h list); -void LUAh_ScoresHUD(huddrawlist_h list); -void LUAh_IntermissionHUD(huddrawlist_h list); -void LUAh_VoteHUD(huddrawlist_h list); +void LUA_SetHudHook(int hook, huddrawlist_h list); #endif // __LUA_HUD_H__ diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index 2ee2297ad..235a89cde 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -28,6 +28,7 @@ #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" +#include "lua_hook.h" #define HUDONLY if (!hud_running) return luaL_error(L, "HUD rendering code should not be called outside of rendering hooks!"); @@ -40,8 +41,6 @@ boolean hud_interpstring = false; boolean hud_interplatch = false; static UINT8 hud_enabled[(hud_MAX/8)+1]; -static UINT8 hudAvailable; // hud hooks field - static UINT8 camnum = 1; // must match enum hud in lua_hud.h @@ -93,19 +92,6 @@ static const char *const patch_opt[] = { "topoffset", NULL}; -enum hudhook { - hudhook_game = 0, - hudhook_scores = 1, - hudhook_intermission = 2, - hudhook_vote = 3, -}; -static const char *const hudhook_opt[] = { - "game", - "scores", - "intermission", - "vote", - NULL}; - static int patch_fields_ref = LUA_NOREF; // alignment types for v.drawString @@ -1215,6 +1201,8 @@ static luaL_Reg lib_draw[] = { {NULL, NULL} }; +static int lib_draw_ref; + // // lib_hud // @@ -1248,25 +1236,7 @@ static int lib_hudenabled(lua_State *L) } // add a HUD element for rendering -static int lib_hudadd(lua_State *L) -{ - enum hudhook field; - - luaL_checktype(L, 1, LUA_TFUNCTION); - field = luaL_checkoption(L, 2, "game", hudhook_opt); - - lua_getfield(L, LUA_REGISTRYINDEX, "HUD"); - I_Assert(lua_istable(L, -1)); - lua_rawgeti(L, -1, field+2); // HUD[2+] - I_Assert(lua_istable(L, -1)); - lua_remove(L, -2); - - lua_pushvalue(L, 1); - lua_rawseti(L, -2, (int)(lua_objlen(L, -2) + 1)); - - hudAvailable |= 1<