From 582c5332f44e4b8547f3b43ed59a9a87406a5023 Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Tue, 11 Dec 2018 18:38:35 +0000
Subject: [PATCH 1/3] Add some checks to prevent invalid awayviewmobjs from
 crashing the game. Not fullproof but at the least the P_CameraThinker crash
 no longer happens

---
 src/p_mobj.c |  2 +-
 src/p_user.c | 11 +++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/p_mobj.c b/src/p_mobj.c
index 8811308a7..4fea15802 100644
--- a/src/p_mobj.c
+++ b/src/p_mobj.c
@@ -3506,7 +3506,7 @@ boolean P_CameraThinker(player_t *player, camera_t *thiscam, boolean resetcalled
 
 	if (player->pflags & PF_FLIPCAM && !(player->pflags & PF_NIGHTSMODE) && player->mo->eflags & MFE_VERTICALFLIP)
 		postimg = postimg_flip;
-	else if (player->awayviewtics && player->awayviewmobj != NULL)	// Camera must obviously exist
+	else if (player->awayviewtics && player->awayviewmobj && !P_MobjWasRemoved(player->awayviewmobj)) // Camera must obviously exist
 	{
 		camera_t dummycam;
 		dummycam.subsector = player->awayviewmobj->subsector;
diff --git a/src/p_user.c b/src/p_user.c
index cf1296694..906167548 100644
--- a/src/p_user.c
+++ b/src/p_user.c
@@ -8372,7 +8372,7 @@ boolean P_MoveChaseCamera(player_t *player, camera_t *thiscam, boolean resetcall
 	if (!(multiplayer || netgame) && !splitscreen)
 	{
 		fixed_t vx = thiscam->x, vy = thiscam->y;
-		if (player->awayviewtics && player->awayviewmobj != NULL)		// Camera must obviously exist
+		if (player->awayviewtics && player->awayviewmobj != NULL && !P_MobjWasRemoved(player->awayviewmobj))		// Camera must obviously exist
 		{
 			vx = player->awayviewmobj->x;
 			vy = player->awayviewmobj->y;
@@ -8534,7 +8534,7 @@ static void P_CalcPostImg(player_t *player)
 	else
 		pviewheight = player->mo->z + player->viewheight;
 
-	if (player->awayviewtics)
+	if (player->awayviewtics && player->awayviewmobj && !P_MobjWasRemoved(player->awayviewmobj))
 	{
 		sector = player->awayviewmobj->subsector->sector;
 		pviewheight = player->awayviewmobj->z + 20*FRACUNIT;
@@ -8701,6 +8701,13 @@ void P_PlayerThink(player_t *player)
 		}
 	}
 #endif
+
+	if (player->awayviewmobj && P_MobjWasRemoved(player->awayviewmobj))
+	{
+		P_SetTarget(&player->awayviewmobj, NULL); // remove awayviewmobj asap if invalid
+		player->awayviewtics = 0; // reset to zero
+	}
+
 	if (player->pflags & PF_GLIDING)
 	{
 		if (player->panim != PA_ABILITY)

From 4ba23e1028f4fd2dbd714ac13596676ddf5bc2b2 Mon Sep 17 00:00:00 2001
From: TehRealSalt <tehrealsalt@gmail.com>
Date: Sun, 16 Dec 2018 15:58:47 -0500
Subject: [PATCH 2/3] Expose R_TextureNumForName

Backport from SRB2Kart
---
 src/lua_baselib.c | 23 +++++++++++++++++++++++
 src/lua_maplib.c  |  8 ++++----
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 969987762..a178b17fb 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -1682,6 +1682,25 @@ static int lib_rSetPlayerSkin(lua_State *L)
 	return 0;
 }
 
+// R_DATA
+////////////
+
+static int lib_rCheckTextureNumForName(lua_State *L)
+{
+	const char *name = luaL_checkstring(L, 1);
+	//HUDSAFE
+	lua_pushinteger(L, R_CheckTextureNumForName(name));
+	return 1;
+}
+
+static int lib_rTextureNumForName(lua_State *L)
+{
+	const char *name = luaL_checkstring(L, 1);
+	//HUDSAFE
+	lua_pushinteger(L, R_TextureNumForName(name));
+	return 1;
+}
+
 // S_SOUND
 ////////////
 
@@ -2165,6 +2184,10 @@ static luaL_Reg lib[] = {
 	{"R_Frame2Char",lib_rFrame2Char},
 	{"R_SetPlayerSkin",lib_rSetPlayerSkin},
 
+	// r_data
+	{"R_CheckTextureNumForName",lib_rCheckTextureNumForName),
+	{"R_TextureNumForName",lib_rTextureNumForName),
+
 	// s_sound
 	{"S_StartSound",lib_sStartSound},
 	{"S_StartSoundAtVolume",lib_sStartSoundAtVolume},
diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index efe9e6f4c..5eb6cb4a1 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -795,16 +795,16 @@ static int side_set(lua_State *L)
 		side->rowoffset = luaL_checkfixed(L, 3);
 		break;
 	case side_toptexture:
-        side->toptexture = luaL_checkinteger(L, 3);
+		side->toptexture = luaL_checkinteger(L, 3);
 		break;
 	case side_bottomtexture:
-        side->bottomtexture = luaL_checkinteger(L, 3);
+		side->bottomtexture = luaL_checkinteger(L, 3);
 		break;
 	case side_midtexture:
-        side->midtexture = luaL_checkinteger(L, 3);
+		side->midtexture = luaL_checkinteger(L, 3);
 		break;
 	case side_repeatcnt:
-        side->repeatcnt = luaL_checkinteger(L, 3);
+		side->repeatcnt = luaL_checkinteger(L, 3);
 		break;
 	}
 	return 0;

From 14e98df69b6c097e4e737a26763c660d28e8b067 Mon Sep 17 00:00:00 2001
From: Alam Ed Arias <alam@srb2.org>
Date: Sun, 16 Dec 2018 22:57:39 +0000
Subject: [PATCH 3/3] Revert "Merge branch 'kart-luatextures-backport' into
 'next'"

This reverts merge request !387
---
 src/lua_baselib.c | 23 -----------------------
 src/lua_maplib.c  |  8 ++++----
 2 files changed, 4 insertions(+), 27 deletions(-)

diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index a178b17fb..969987762 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -1682,25 +1682,6 @@ static int lib_rSetPlayerSkin(lua_State *L)
 	return 0;
 }
 
-// R_DATA
-////////////
-
-static int lib_rCheckTextureNumForName(lua_State *L)
-{
-	const char *name = luaL_checkstring(L, 1);
-	//HUDSAFE
-	lua_pushinteger(L, R_CheckTextureNumForName(name));
-	return 1;
-}
-
-static int lib_rTextureNumForName(lua_State *L)
-{
-	const char *name = luaL_checkstring(L, 1);
-	//HUDSAFE
-	lua_pushinteger(L, R_TextureNumForName(name));
-	return 1;
-}
-
 // S_SOUND
 ////////////
 
@@ -2184,10 +2165,6 @@ static luaL_Reg lib[] = {
 	{"R_Frame2Char",lib_rFrame2Char},
 	{"R_SetPlayerSkin",lib_rSetPlayerSkin},
 
-	// r_data
-	{"R_CheckTextureNumForName",lib_rCheckTextureNumForName),
-	{"R_TextureNumForName",lib_rTextureNumForName),
-
 	// s_sound
 	{"S_StartSound",lib_sStartSound},
 	{"S_StartSoundAtVolume",lib_sStartSoundAtVolume},
diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index 5eb6cb4a1..efe9e6f4c 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -795,16 +795,16 @@ static int side_set(lua_State *L)
 		side->rowoffset = luaL_checkfixed(L, 3);
 		break;
 	case side_toptexture:
-		side->toptexture = luaL_checkinteger(L, 3);
+        side->toptexture = luaL_checkinteger(L, 3);
 		break;
 	case side_bottomtexture:
-		side->bottomtexture = luaL_checkinteger(L, 3);
+        side->bottomtexture = luaL_checkinteger(L, 3);
 		break;
 	case side_midtexture:
-		side->midtexture = luaL_checkinteger(L, 3);
+        side->midtexture = luaL_checkinteger(L, 3);
 		break;
 	case side_repeatcnt:
-		side->repeatcnt = luaL_checkinteger(L, 3);
+        side->repeatcnt = luaL_checkinteger(L, 3);
 		break;
 	}
 	return 0;