Skip to content

Commit

Permalink
Save map instead of Map id/instance id combo.
Browse files Browse the repository at this point in the history
Added function 'GetStateMap' to get the map pointer the lua state is running on.
  • Loading branch information
Terrorblade committed Jan 23, 2024
1 parent 8dee497 commit d07e0b2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
7 changes: 4 additions & 3 deletions LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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();
Expand Down
29 changes: 21 additions & 8 deletions LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#endif
#include "Hooks.h"
#include "ElunaUtility.h"
#include "Map.h"
#include <mutex>
#include <memory>

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions TrinityCore/GlobalMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit d07e0b2

Please sign in to comment.