From 0f7939675705dceeeb1c7c844d477d8b39dfaf42 Mon Sep 17 00:00:00 2001
From: Steel Titanium <steeltitanium1@gmail.com>
Date: Sun, 13 Oct 2019 20:50:46 -0400
Subject: [PATCH] New intermission thinker hook

---
 src/lua_hook.h    |  2 ++
 src/lua_hooklib.c | 22 ++++++++++++++++++++++
 src/y_inter.c     |  5 +++++
 3 files changed, 29 insertions(+)

diff --git a/src/lua_hook.h b/src/lua_hook.h
index 45e116c344..37b1f3e066 100644
--- a/src/lua_hook.h
+++ b/src/lua_hook.h
@@ -50,6 +50,7 @@ enum hook {
 	hook_FollowMobj,
 	hook_PlayerCanDamage,
 	hook_PlayerQuit,
+	hook_IntermissionThinker,
 
 	hook_MAX // last hook
 };
@@ -91,5 +92,6 @@ boolean LUAh_MapThingSpawn(mobj_t *mo, mapthing_t *mthing); // Hook for P_SpawnM
 boolean LUAh_FollowMobj(player_t *player, mobj_t *mobj); // Hook for P_PlayerAfterThink Smiles mobj-following
 UINT8 LUAh_PlayerCanDamage(player_t *player, mobj_t *mobj); // Hook for P_PlayerCanDamage
 void LUAh_PlayerQuit(player_t *plr, int reason); // Hook for player quitting
+void LUAh_IntermissionThinker(void); // Hook for Y_Ticker
 
 #endif
diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c
index 7f7e8adc67..ffc5cf9903 100644
--- a/src/lua_hooklib.c
+++ b/src/lua_hooklib.c
@@ -61,6 +61,7 @@ const char *const hookNames[hook_MAX+1] = {
 	"FollowMobj",
 	"PlayerCanDamage",
 	"PlayerQuit",
+	"IntermissionThinker",
 	NULL
 };
 
@@ -1322,4 +1323,25 @@ void LUAh_PlayerQuit(player_t *plr, int reason)
 	lua_settop(gL, 0);
 }
 
+// Hook for Y_Ticker
+void LUAh_IntermissionThinker(void)
+{
+	hook_p hookp;
+	if (!gL || !(hooksAvailable[hook_IntermissionThinker/8] & (1<<(hook_IntermissionThinker%8))))
+		return;
+
+	for (hookp = roothook; hookp; hookp = hookp->next)
+		if (hookp->type == hook_IntermissionThinker)
+		{
+			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;
+			}
+		}
+}
+
 #endif
diff --git a/src/y_inter.c b/src/y_inter.c
index 0d6a3d03cf..81532dd7fd 100644
--- a/src/y_inter.c
+++ b/src/y_inter.c
@@ -35,6 +35,7 @@
 #include "p_local.h"
 
 #include "m_cond.h" // condition sets
+#include "lua_hook.h" // IntermissionThinker hook
 
 #ifdef HWRENDER
 #include "hardware/hw_main.h"
@@ -802,6 +803,10 @@ void Y_Ticker(void)
 	if (paused || P_AutoPause())
 		return;
 
+#ifdef HAVE_BLUA
+	LUAh_IntermissionThinker();
+#endif
+
 	intertic++;
 
 	// Team scramble code for team match and CTF.
-- 
GitLab