From d07e0b2035207646ef9ef17b591da5876d153f3a Mon Sep 17 00:00:00 2001 From: Tb Date: Tue, 23 Jan 2024 17:16:36 -0500 Subject: [PATCH] Save map instead of Map id/instance id combo. Added function 'GetStateMap' to get the map pointer the lua state is running on. --- LuaEngine.cpp | 7 ++++--- LuaEngine.h | 29 +++++++++++++++++++++-------- TrinityCore/GlobalMethods.h | 12 ++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/LuaEngine.cpp b/LuaEngine.cpp index b6630d3932..74fcd3e499 100644 --- a/LuaEngine.cpp +++ b/LuaEngine.cpp @@ -74,12 +74,11 @@ void Eluna::_ReloadEluna() reload = false; } -Eluna::Eluna(int32 mapId, uint32 instanceId, bool compatMode) : +Eluna::Eluna(Map* map, bool compatMode) : event_level(0), push_counter(0), enabled(false), -boundMapId(mapId), -boundInstanceId(instanceId), +boundMap(map), compatibilityMode(compatMode), L(NULL), @@ -235,6 +234,8 @@ void Eluna::DestroyBindStores() void Eluna::RunScripts() { + int32 const boundMapId = GetBoundMapId(); + uint32 const boundInstanceId = GetBoundInstanceId(); ELUNA_LOG_DEBUG("[Eluna]: Running scripts for state: %i, instance: %u", boundMapId, boundInstanceId); uint32 oldMSTime = ElunaUtil::GetCurrTime(); diff --git a/LuaEngine.h b/LuaEngine.h index 769647e037..35d9ee9dab 100644 --- a/LuaEngine.h +++ b/LuaEngine.h @@ -36,6 +36,7 @@ #endif #include "Hooks.h" #include "ElunaUtility.h" +#include "Map.h" #include #include @@ -181,11 +182,7 @@ class ELUNA_GAME_API Eluna uint8 push_counter; bool enabled; - // The map id that this Eluna object is tied to. -1 is reserved for objects without a bound map id. - int32 boundMapId; - - // The instance id that this Eluna object is tied to. -1 is reserved for objects without a bound map id. - uint32 boundInstanceId; + Map* const boundMap; // Whether or not Eluna is in compatibility mode. Used in some method wrappers. bool compatibilityMode; @@ -362,11 +359,27 @@ class ELUNA_GAME_API Eluna InstanceData* GetInstanceData(Map* map); void FreeInstanceId(uint32 instanceId); - int32 GetBoundMapId() const { return boundMapId; } - uint32 GetBoundInstanceId() const { return boundInstanceId; } + Map* GetBoundMap() const { return boundMap; } + + int32 GetBoundMapId() const + { + if(const Map * map = GetBoundMap()) + return map->GetId(); + + return -1; + } + + uint32 GetBoundInstanceId() const + { + if(const Map * map = GetBoundMap()) + return map->GetInstanceId(); + + return 0; + } + bool GetCompatibilityMode() const { return compatibilityMode; } - Eluna(int32 mapId, uint32 instanceId, bool compatMode = false); + Eluna(Map * map, bool compatMode = false); ~Eluna(); // Prevent copy diff --git a/TrinityCore/GlobalMethods.h b/TrinityCore/GlobalMethods.h index 1a81b17989..75be1bb855 100644 --- a/TrinityCore/GlobalMethods.h +++ b/TrinityCore/GlobalMethods.h @@ -86,6 +86,17 @@ namespace LuaGlobalFunctions return 1; } + /** + * Returns the map pointer of the Lua state. Returns null for the "World" state. + * + * @return int32 mapId + */ + int GetStateMap(Eluna* E) + { + E->Push(E->GetBoundMap()); + return 1; + } + /** * Returns the map ID of the Lua state. Returns -1 for the "World" state. * @@ -3122,6 +3133,7 @@ namespace LuaGlobalFunctions { "GetRealmID", &LuaGlobalFunctions::GetRealmID }, { "GetCoreVersion", &LuaGlobalFunctions::GetCoreVersion }, { "GetCoreExpansion", &LuaGlobalFunctions::GetCoreExpansion }, + { "GetStateMap", &LuaGlobalFunctions::GetStateMap, METHOD_REG_MAP }, { "GetStateMapId", &LuaGlobalFunctions::GetStateMapId }, { "GetStateInstanceId", &LuaGlobalFunctions::GetStateInstanceId }, { "GetQuest", &LuaGlobalFunctions::GetQuest },