From 20677c7a660d1a7dd53ac0b17e9e99ae79ab16ed Mon Sep 17 00:00:00 2001
From: toasterbabe <rollerorbital@gmail.com>
Date: Fri, 11 Nov 2016 23:23:41 +0000
Subject: [PATCH] * Introducing new Lua-exclusive function,
 P_IsValidSprite2(mo, spr2). Basically just a wrapper for (((skin_t
 *)mobj->skin)->sprites[spr2].numframes > 0), useful for creating custom
 sprite2 defaulting functions since hooking into P_GetMobjSprite2 wouldn't be
 worth it. * All Lua-originated sprite2 settings are now forced through
 P_GetMobjSprite2. Makes sense because of SPR2_JUMP, which none of the main
 characters have sprites for yet all use. * Cleaned up P_GetMobjSprite2 to not
 set irrelevant, otherwise-unused variable.

---
 src/lua_baselib.c | 14 ++++++++++++++
 src/lua_mobjlib.c |  2 +-
 src/p_local.h     |  1 +
 src/p_mobj.c      |  5 ++---
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index bbcec06d3a..e6ab7a7e1c 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -309,6 +309,19 @@ static int lib_pRemoveMobj(lua_State *L)
 	return 0;
 }
 
+// P_IsValidSprite2 technically doesn't exist, and probably never should... but too much would need to be exposed to allow this to be checked by other methods.
+
+static int lib_pIsValidSprite2(lua_State *L)
+{
+	mobj_t *mobj = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
+	UINT8 spr2 = (UINT8)luaL_checkinteger(L, 2);
+	//HUDSAFE
+	if (!mobj)
+		return LUA_ErrInvalid(L, "mobj_t");
+	lua_pushboolean(L, (mobj->skin && (((skin_t *)mobj->skin)->sprites[spr2].numframes > 0)));
+	return 1;
+}
+
 static int lib_pSpawnMissile(lua_State *L)
 {
 	mobj_t *source = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ));
@@ -2005,6 +2018,7 @@ static luaL_Reg lib[] = {
 	// don't add P_SetMobjState or P_SetPlayerMobjState, use "mobj.state = S_NEWSTATE" instead.
 	{"P_SpawnMobj",lib_pSpawnMobj},
 	{"P_RemoveMobj",lib_pRemoveMobj},
+	{"P_IsValidSprite2", lib_pIsValidSprite2},
 	{"P_SpawnMissile",lib_pSpawnMissile},
 	{"P_SpawnXYZMissile",lib_pSpawnXYZMissile},
 	{"P_SpawnPointMissile",lib_pSpawnPointMissile},
diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c
index 58c1b14f20..9ebc38a611 100644
--- a/src/lua_mobjlib.c
+++ b/src/lua_mobjlib.c
@@ -417,7 +417,7 @@ static int mobj_set(lua_State *L)
 		mo->frame = (UINT32)luaL_checkinteger(L, 3);
 		break;
 	case mobj_sprite2:
-		mo->sprite2 = (UINT8)luaL_checkinteger(L, 3);
+		mo->sprite2 = P_GetMobjSprite2(mo, (UINT8)luaL_checkinteger(L, 3));
 		break;
 	case mobj_anim_duration:
 		mo->anim_duration = (UINT16)luaL_checkinteger(L, 3);
diff --git a/src/p_local.h b/src/p_local.h
index de717801e7..ee1aacb4f1 100644
--- a/src/p_local.h
+++ b/src/p_local.h
@@ -211,6 +211,7 @@ void P_PrecipitationEffects(void);
 void P_RemoveMobj(mobj_t *th);
 boolean P_MobjWasRemoved(mobj_t *th);
 void P_RemoveSavegameMobj(mobj_t *th);
+UINT8 P_GetMobjSprite2(mobj_t *mobj, UINT8 spr2);
 boolean P_SetPlayerMobjState(mobj_t *mobj, statenum_t state);
 boolean P_SetMobjState(mobj_t *mobj, statenum_t state);
 void P_RunShields(void);
diff --git a/src/p_mobj.c b/src/p_mobj.c
index 82a923279b..669f8cd1f8 100644
--- a/src/p_mobj.c
+++ b/src/p_mobj.c
@@ -189,17 +189,16 @@ static void P_CyclePlayerMobjState(mobj_t *mobj)
 // P_GetMobjSprite2
 //
 
-static UINT8 P_GetMobjSprite2(mobj_t *mobj, UINT8 spr2)
+UINT8 P_GetMobjSprite2(mobj_t *mobj, UINT8 spr2)
 {
 	player_t *player = mobj->player;
 	skin_t *skin = ((skin_t *)mobj->skin);
 	boolean noalt = false;
-	UINT8 numframes;
 
 	if (!skin)
 		return 0;
 
-	while (((numframes = skin->sprites[spr2].numframes) <= 0)
+	while ((skin->sprites[spr2].numframes <= 0)
 		&& spr2 != SPR2_STND)
 	{
 		switch(spr2)
-- 
GitLab