From c23ca65193beb995e748e6c2174aead9b970b042 Mon Sep 17 00:00:00 2001 From: Hanicef <gustaf@hanicef.me> Date: Tue, 23 Jan 2024 22:43:25 +0100 Subject: [PATCH] Fix segfault when trying to spawn an MT_PLAYER from Lua --- src/lua_baselib.c | 4 ++-- src/p_local.h | 2 +- src/p_mobj.c | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index b70c63ece1..ccdc8baa06 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -627,7 +627,7 @@ static int lib_pSpawnMobj(lua_State *L) INLEVEL if (type >= NUMMOBJTYPES) return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); - LUA_PushUserdata(L, P_SpawnMobj(x, y, z, type), META_MOBJ); + LUA_PushUserdata(L, P_SpawnMobj(x, y, z, type, NULL), META_MOBJ); return 1; } @@ -644,7 +644,7 @@ static int lib_pSpawnMobjFromMobj(lua_State *L) return LUA_ErrInvalid(L, "mobj_t"); if (type >= NUMMOBJTYPES) return luaL_error(L, "mobj type %d out of range (0 - %d)", type, NUMMOBJTYPES-1); - LUA_PushUserdata(L, P_SpawnMobjFromMobj(actor, x, y, z, type), META_MOBJ); + LUA_PushUserdata(L, P_SpawnMobjFromMobj(actor, x, y, z, type, NULL), META_MOBJ); return 1; } diff --git a/src/p_local.h b/src/p_local.h index 8bdc89841f..eeee1ca363 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -329,7 +329,7 @@ boolean P_CheckDeathPitCollide(mobj_t *mo); boolean P_CheckSolidLava(ffloor_t *rover); void P_AdjustMobjFloorZ_FFloors(mobj_t *mo, sector_t *sector, UINT8 motype); -mobj_t *P_SpawnMobjFromMobj(mobj_t *mobj, fixed_t xofs, fixed_t yofs, fixed_t zofs, mobjtype_t type); +mobj_t *P_SpawnMobjFromMobj(mobj_t *mobj, fixed_t xofs, fixed_t yofs, fixed_t zofs, mobjtype_t type, ...); mobj_t *P_SpawnMissile(mobj_t *source, mobj_t *dest, mobjtype_t type); mobj_t *P_SpawnXYZMissile(mobj_t *source, mobj_t *dest, mobjtype_t type, fixed_t x, fixed_t y, fixed_t z); diff --git a/src/p_mobj.c b/src/p_mobj.c index f16fef2f00..55b3184069 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -14410,15 +14410,29 @@ void P_FlashPal(player_t *pl, UINT16 type, UINT16 duration) // Spawns an object with offsets relative to the position of another object. // Scale, gravity flip, etc. is taken into account automatically. // -mobj_t *P_SpawnMobjFromMobj(mobj_t *mobj, fixed_t xofs, fixed_t yofs, fixed_t zofs, mobjtype_t type) +mobj_t *P_SpawnMobjFromMobj(mobj_t *mobj, fixed_t xofs, fixed_t yofs, fixed_t zofs, mobjtype_t type, ...) { + va_list args; mobj_t *newmobj; xofs = FixedMul(xofs, mobj->scale); yofs = FixedMul(yofs, mobj->scale); zofs = FixedMul(zofs, mobj->scale); - newmobj = P_SpawnMobj(mobj->x + xofs, mobj->y + yofs, mobj->z + zofs, type); + if (type == MT_PLAYER) + { + player_t *player; + // MT_PLAYER requires an additional parameter for the player, so pass that forth. + va_start(args, type); + player = va_arg(args, player_t *); + va_end(args); + newmobj = P_SpawnMobj(mobj->x + xofs, mobj->y + yofs, mobj->z + zofs, type, player); + } + else + { + newmobj = P_SpawnMobj(mobj->x + xofs, mobj->y + yofs, mobj->z + zofs, type); + } + if (!newmobj) return NULL; -- GitLab