From f783df718cbd902e35b3f054e9dcc02429df27ce Mon Sep 17 00:00:00 2001
From: JTE <jason.the.echidna@gmail.com>
Date: Fri, 22 May 2015 01:51:40 -0400
Subject: [PATCH] Reset tmthing after P_CheckSector calls from Lua

tmthing must not be set outside of P_MapStart and P_MapEnd
or the game will fail a sanity check which ensures that
mobj references are not persistent across frames and crash.
---
 src/lua_maplib.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index 2dd922a3aa..0a12478cae 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -396,6 +396,7 @@ static int sector_set(lua_State *L)
 		return luaL_error(L, "sector_t field " LUA_QS " cannot be set.", sector_opt[field]);
 	case sector_floorheight: { // floorheight
 		boolean flag;
+		mobj_t *ptmthing = tmthing;
 		fixed_t lastpos = sector->floorheight;
 		sector->floorheight = luaL_checkfixed(L, 3);
 		flag = P_CheckSector(sector, true);
@@ -404,10 +405,12 @@ static int sector_set(lua_State *L)
 			sector->floorheight = lastpos;
 			P_CheckSector(sector, true);
 		}
+		P_SetTarget(&tmthing, ptmthing);
 		break;
 	}
 	case sector_ceilingheight: { // ceilingheight
 		boolean flag;
+		mobj_t *ptmthing = tmthing;
 		fixed_t lastpos = sector->ceilingheight;
 		sector->ceilingheight = luaL_checkfixed(L, 3);
 		flag = P_CheckSector(sector, true);
@@ -416,6 +419,7 @@ static int sector_set(lua_State *L)
 			sector->ceilingheight = lastpos;
 			P_CheckSector(sector, true);
 		}
+		P_SetTarget(&tmthing, ptmthing);
 		break;
 	}
 	case sector_floorpic:
@@ -1071,6 +1075,7 @@ static int ffloor_set(lua_State *L)
 	case ffloor_topheight: { // topheight
 		boolean flag;
 		fixed_t lastpos = *ffloor->topheight;
+		mobj_t *ptmthing = tmthing;
 		sector_t *sector = &sectors[ffloor->secnum];
 		sector->ceilingheight = luaL_checkfixed(L, 3);
 		flag = P_CheckSector(sector, true);
@@ -1079,6 +1084,7 @@ static int ffloor_set(lua_State *L)
 			*ffloor->topheight = lastpos;
 			P_CheckSector(sector, true);
 		}
+		P_SetTarget(&tmthing, ptmthing);
 		break;
 	}
 	case ffloor_toppic:
@@ -1090,6 +1096,7 @@ static int ffloor_set(lua_State *L)
 	case ffloor_bottomheight: { // bottomheight
 		boolean flag;
 		fixed_t lastpos = *ffloor->bottomheight;
+		mobj_t *ptmthing = tmthing;
 		sector_t *sector = &sectors[ffloor->secnum];
 		sector->floorheight = luaL_checkfixed(L, 3);
 		flag = P_CheckSector(sector, true);
@@ -1098,6 +1105,7 @@ static int ffloor_set(lua_State *L)
 			*ffloor->bottomheight = lastpos;
 			P_CheckSector(sector, true);
 		}
+		P_SetTarget(&tmthing, ptmthing);
 		break;
 	}
 	case ffloor_bottompic:
-- 
GitLab