From 067b4981ddb9f39900b0ea52d220263661b6bc94 Mon Sep 17 00:00:00 2001 From: WinterSolstice8 <60417494+wintersolstice8@users.noreply.github.com> Date: Thu, 23 Jan 2025 18:10:03 -0700 Subject: [PATCH] [core] Add safety to CLuaBaseEntity::facePlayer --- src/map/lua/lua_baseentity.cpp | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/map/lua/lua_baseentity.cpp b/src/map/lua/lua_baseentity.cpp index 8b4deaea1c7..d8bd4b2e0fd 100644 --- a/src/map/lua/lua_baseentity.cpp +++ b/src/map/lua/lua_baseentity.cpp @@ -1745,30 +1745,37 @@ void CLuaBaseEntity::lookAt(sol::object const& arg0, sol::object const& arg1, so void CLuaBaseEntity::facePlayer(CLuaBaseEntity* PLuaBaseEntity, sol::object const& nonGlobal) { - CCharEntity* PChar = static_cast(PLuaBaseEntity->GetBaseEntity()); - - if (PChar) + if (PLuaBaseEntity) { - bool onePersonOnly = nonGlobal.get_type() == sol::type::boolean ? nonGlobal.as() : true; + CCharEntity* PChar = dynamic_cast(PLuaBaseEntity->GetBaseEntity()); - if (onePersonOnly) + if (PChar) { - auto storedRotation = m_PBaseEntity->loc.p.rotation; - m_PBaseEntity->loc.p.rotation = worldAngle(m_PBaseEntity->loc.p, PChar->loc.p); - // Update 1 player's client only - PChar->updateEntityPacket(m_PBaseEntity, static_cast(ENTITY_UPDATE), UPDATE_POS); - // Now that the packet is sent to that 1 player, turn this back. - m_PBaseEntity->loc.p.rotation = storedRotation; + bool onePersonOnly = nonGlobal.get_type() == sol::type::boolean ? nonGlobal.as() : true; + + if (onePersonOnly) + { + auto storedRotation = m_PBaseEntity->loc.p.rotation; + m_PBaseEntity->loc.p.rotation = worldAngle(m_PBaseEntity->loc.p, PChar->loc.p); + // Update 1 player's client only + PChar->updateEntityPacket(m_PBaseEntity, static_cast(ENTITY_UPDATE), UPDATE_POS); + // Now that the packet is sent to that 1 player, turn this back. + m_PBaseEntity->loc.p.rotation = storedRotation; + } + else + { + m_PBaseEntity->loc.p.rotation = worldAngle(m_PBaseEntity->loc.p, PChar->loc.p); + m_PBaseEntity->updatemask |= UPDATE_POS; + } } else { - m_PBaseEntity->loc.p.rotation = worldAngle(m_PBaseEntity->loc.p, PChar->loc.p); - m_PBaseEntity->updatemask |= UPDATE_POS; + ShowError("non-player entity used as param in CLuaBaseEntity::facePlayer call"); } } else { - ShowError("missing or invalid entity in function call."); + ShowError("missing entity to CLuaBaseEntity::facePlayer call"); } }