From 9f82cdb4010cd38994cfc09e5b75212085050230 Mon Sep 17 00:00:00 2001 From: Zachary McAlpin Date: Thu, 19 Dec 2019 16:41:25 -0600 Subject: [PATCH] Added CalculateCamera Hook --- src/lua_hook.h | 2 ++ src/lua_hooklib.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/p_user.c | 33 ++++++++++++++++++++------------- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/lua_hook.h b/src/lua_hook.h index 073773f52..4b31fda28 100644 --- a/src/lua_hook.h +++ b/src/lua_hook.h @@ -52,6 +52,7 @@ enum hook { hook_PlayerQuit, hook_IntermissionThinker, hook_PlayerThink, + hook_CalculateCamera, hook_MAX // last hook }; @@ -95,5 +96,6 @@ UINT8 LUAh_PlayerCanDamage(player_t *player, mobj_t *mobj); // Hook for P_Player void LUAh_PlayerQuit(player_t *plr, int reason); // Hook for player quitting void LUAh_IntermissionThinker(void); // Hook for Y_Ticker #define LUAh_PlayerThink(player) LUAh_PlayerHook(player, hook_PlayerThink) // Hook for P_PlayerThink +boolean LUAh_CalculateCamera(player_t *player, camera_t *camera); // Hook for P_PlayerAfterThink Camera calculations #endif diff --git a/src/lua_hooklib.c b/src/lua_hooklib.c index 30ab27da1..b7e544ed0 100644 --- a/src/lua_hooklib.c +++ b/src/lua_hooklib.c @@ -63,6 +63,7 @@ const char *const hookNames[hook_MAX+1] = { "PlayerQuit", "IntermissionThinker", "PlayerThink", + "CalculateCamera", NULL }; @@ -207,6 +208,7 @@ static int lib_addHook(lua_State *L) case hook_ShieldSpawn: case hook_ShieldSpecial: case hook_PlayerThink: + case hook_CalculateCamera: lastp = &playerhooks; break; case hook_LinedefExecute: @@ -1348,4 +1350,43 @@ void LUAh_IntermissionThinker(void) } } +boolean LUAh_CalculateCamera(player_t *player, camera_t *camera) +{ + hook_p hookp; + boolean hooked; + if (!gL || !(hooksAvailable[hook_CalculateCamera/8] & (1<<(hook_CalculateCamera%8)))) + return 0; + + lua_settop(gL, 0); + + for (hookp = playerhooks; hookp; hookp = hookp->next) + { + if (hookp->type != hook_CalculateCamera) + continue; + + if (lua_gettop(gL) == 0) + { + LUA_PushUserdata(gL, player, META_PLAYER); + LUA_PushUserdata(gL, camera, META_CAMERA); + } + lua_pushfstring(gL, FMT_HOOKID, hookp->id); + lua_gettable(gL, LUA_REGISTRYINDEX); + lua_pushvalue(gL, -3); + lua_pushvalue(gL, -3); + if (lua_pcall(gL, 2, 1, 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; + continue; + } + if (lua_toboolean(gL, -1)) + hooked = true; + lua_pop(gL, 1); + } + + lua_settop(gL, 0); + return hooked; +} + #endif diff --git a/src/p_user.c b/src/p_user.c index 3baf8cd04..6eedec10f 100644 --- a/src/p_user.c +++ b/src/p_user.c @@ -12626,22 +12626,29 @@ void P_PlayerAfterThink(player_t *player) if (thiscam) { - if (!thiscam->chase) // bob view only if looking through the player's eyes - { - P_CalcHeight(player); - P_CalcPostImg(player); - } +#ifdef HAVE_BLUA + if (LUAh_CalculateCamera(player, thiscam)) + {;} else +#endif { - // defaults to make sure 1st person cam doesn't do anything weird on startup - player->deltaviewheight = 0; - player->viewheight = FixedMul(41*player->height/48, player->mo->scale); - if (player->mo->eflags & MFE_VERTICALFLIP) - player->viewz = player->mo->z + player->mo->height - player->viewheight; + if (!thiscam->chase) // bob view only if looking through the player's eyes + { + P_CalcHeight(player); + P_CalcPostImg(player); + } else - player->viewz = player->mo->z + player->viewheight; - if (server || addedtogame) - P_MoveChaseCamera(player, thiscam, false); // calculate the camera movement + { + // defaults to make sure 1st person cam doesn't do anything weird on startup + player->deltaviewheight = 0; + player->viewheight = FixedMul(41*player->height/48, player->mo->scale); + if (player->mo->eflags & MFE_VERTICALFLIP) + player->viewz = player->mo->z + player->mo->height - player->viewheight; + else + player->viewz = player->mo->z + player->viewheight; + if (server || addedtogame) + P_MoveChaseCamera(player, thiscam, false); // calculate the camera movement + } } }