mirror of
https://github.com/azerothcore/mod-ale
synced 2025-11-29 15:38:17 +08:00
Eluna improve object variable extension
This commit is contained in:
50
HookMgr.cpp
50
HookMgr.cpp
@@ -1272,20 +1272,18 @@ void Eluna::OnUpdate(Map* map, uint32 diff)
|
||||
EVENT_EXECUTE(0);
|
||||
ENDCALL();
|
||||
}
|
||||
void Eluna::OnRemove(Map* map, Creature* creature)
|
||||
void Eluna::OnRemove(GameObject* gameobject)
|
||||
{
|
||||
EVENT_BEGIN(ServerEventBindings, MAP_EVENT_ON_REMOVE_CREATURE, return);
|
||||
Push(L, map);
|
||||
Push(L, creature);
|
||||
ENTRY_EXECUTE(0);
|
||||
EVENT_BEGIN(ServerEventBindings, WORLD_EVENT_ON_DELETE_GAMEOBJECT, return);
|
||||
Push(L, gameobject);
|
||||
EVENT_EXECUTE(0);
|
||||
ENDCALL();
|
||||
}
|
||||
void Eluna::OnRemove(Map* map, GameObject* gameobject)
|
||||
void Eluna::OnRemove(Creature* creature)
|
||||
{
|
||||
EVENT_BEGIN(ServerEventBindings, MAP_EVENT_ON_REMOVE_GAMEOBJECT, return);
|
||||
Push(L, map);
|
||||
Push(L, gameobject);
|
||||
ENTRY_EXECUTE(0);
|
||||
EVENT_BEGIN(ServerEventBindings, WORLD_EVENT_ON_DELETE_CREATURE, return);
|
||||
Push(L, creature);
|
||||
EVENT_EXECUTE(0);
|
||||
ENDCALL();
|
||||
}
|
||||
|
||||
@@ -1392,6 +1390,22 @@ void Eluna::OnSummoned(Creature* pCreature, Unit* pSummoner)
|
||||
ENDCALL();
|
||||
}
|
||||
|
||||
void Eluna::OnAddToWorld(Creature* creature)
|
||||
{
|
||||
ENTRY_BEGIN(CreatureEventBindings, creature->GetEntry(), CREATURE_EVENT_ON_ADD, return);
|
||||
Push(L, creature);
|
||||
ENTRY_EXECUTE(0);
|
||||
ENDCALL();
|
||||
}
|
||||
|
||||
void Eluna::OnRemoveFromWorld(Creature* creature)
|
||||
{
|
||||
ENTRY_BEGIN(CreatureEventBindings, creature->GetEntry(), CREATURE_EVENT_ON_REMOVE, return);
|
||||
Push(L, creature);
|
||||
ENTRY_EXECUTE(0);
|
||||
ENDCALL();
|
||||
}
|
||||
|
||||
struct ElunaCreatureAI : ScriptedAI
|
||||
{
|
||||
#ifndef TRINITY
|
||||
@@ -1845,6 +1859,22 @@ void Eluna::OnSpawn(GameObject* gameobject)
|
||||
ENDCALL();
|
||||
}
|
||||
|
||||
void Eluna::OnAddToWorld(GameObject* gameobject)
|
||||
{
|
||||
ENTRY_BEGIN(GameObjectEventBindings, gameobject->GetEntry(), GAMEOBJECT_EVENT_ON_ADD, return);
|
||||
Push(L, gameobject);
|
||||
ENTRY_EXECUTE(0);
|
||||
ENDCALL();
|
||||
}
|
||||
|
||||
void Eluna::OnRemoveFromWorld(GameObject* gameobject)
|
||||
{
|
||||
ENTRY_BEGIN(GameObjectEventBindings, gameobject->GetEntry(), GAMEOBJECT_EVENT_ON_REMOVE, return);
|
||||
Push(L, gameobject);
|
||||
ENTRY_EXECUTE(0);
|
||||
ENDCALL();
|
||||
}
|
||||
|
||||
CreatureAI* Eluna::GetAI(Creature* creature)
|
||||
{
|
||||
if (!CreatureEventBindings->GetBindMap(creature->GetEntry()))
|
||||
|
||||
@@ -86,8 +86,8 @@ namespace HookMgr
|
||||
// AddOns
|
||||
ADDON_EVENT_ON_MESSAGE = 30, // (event, sender, type, prefix, msg, target) - target can be nil/whisper_target/guild/group/channel
|
||||
|
||||
MAP_EVENT_ON_REMOVE_CREATURE = 31, // (event, map, creature)
|
||||
MAP_EVENT_ON_REMOVE_GAMEOBJECT = 32, // (event, map, gameobject)
|
||||
WORLD_EVENT_ON_DELETE_CREATURE = 31, // (event, creature)
|
||||
WORLD_EVENT_ON_DELETE_GAMEOBJECT = 32, // (event, gameobject)
|
||||
|
||||
SERVER_EVENT_COUNT
|
||||
};
|
||||
@@ -227,6 +227,8 @@ namespace HookMgr
|
||||
CREATURE_EVENT_ON_QUEST_COMPLETE = 33, // (event, player, creature, quest)
|
||||
CREATURE_EVENT_ON_QUEST_REWARD = 34, // (event, player, creature, quest, opt)
|
||||
CREATURE_EVENT_ON_DIALOG_STATUS = 35, // (event, player, creature)
|
||||
CREATURE_EVENT_ON_ADD = 36, // (event, creature)
|
||||
CREATURE_EVENT_ON_REMOVE = 37, // (event, creature)
|
||||
CREATURE_EVENT_COUNT
|
||||
};
|
||||
|
||||
@@ -244,6 +246,8 @@ namespace HookMgr
|
||||
GAMEOBJECT_EVENT_ON_LOOT_STATE_CHANGE = 9, // (event, go, state)
|
||||
GAMEOBJECT_EVENT_ON_GO_STATE_CHANGED = 10, // (event, go, state)
|
||||
GAMEOBJECT_EVENT_ON_QUEST_COMPLETE = 11, // (event, player, go, quest)
|
||||
GAMEOBJECT_EVENT_ON_ADD = 12, // (event, gameobject)
|
||||
GAMEOBJECT_EVENT_ON_REMOVE = 13, // (event, gameobject)
|
||||
GAMEOBJECT_EVENT_COUNT
|
||||
};
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ void Eluna::Initialize()
|
||||
// GetScripts(lua_folderpath + "/extensions", lua_extensions);
|
||||
GetScripts(lua_folderpath, lua_scripts);
|
||||
|
||||
ELUNA_LOG_DEBUG("[Eluna]: Loaded %u scripts in %u ms", uint32(lua_scripts.size()), GetTimeDiff(oldMSTime));
|
||||
ELUNA_LOG_DEBUG("[Eluna]: Loaded %u scripts in %u ms", uint32(lua_scripts.size() + lua_extensions.size()), GetTimeDiff(oldMSTime));
|
||||
|
||||
// Create global eluna
|
||||
new Eluna();
|
||||
|
||||
12
LuaEngine.h
12
LuaEngine.h
@@ -675,8 +675,12 @@ public:
|
||||
void OnPlayerEnter(Map* map, Player* player);
|
||||
void OnPlayerLeave(Map* map, Player* player);
|
||||
void OnUpdate(Map* map, uint32 diff);
|
||||
void OnRemove(Map* map, Creature* creature);
|
||||
void OnRemove(Map* map, GameObject* gameobject);
|
||||
void OnAddToWorld(Creature* creature);
|
||||
void OnRemoveFromWorld(Creature* creature);
|
||||
void OnAddToWorld(GameObject* gameobject);
|
||||
void OnRemoveFromWorld(GameObject* gameobject);
|
||||
void OnRemove(Creature* creature);
|
||||
void OnRemove(GameObject* gameobject);
|
||||
|
||||
/* World */
|
||||
void OnOpenStateChange(bool open);
|
||||
@@ -881,10 +885,6 @@ public:
|
||||
lua_setfield(L, metatable, "__gc");
|
||||
}
|
||||
|
||||
// hide metatable
|
||||
lua_pushvalue(L, methods);
|
||||
lua_setfield(L, metatable, "__metatable");
|
||||
|
||||
// make methods accessible through metatable
|
||||
lua_pushvalue(L, methods);
|
||||
lua_setfield(L, metatable, "__index");
|
||||
|
||||
@@ -551,8 +551,13 @@ namespace LuaUnit
|
||||
|
||||
Powers PowerSelectorHelper(lua_State* L, Unit* unit, int powerType = -1)
|
||||
{
|
||||
#if (!defined(TRINITY) && defined(WOTLK))
|
||||
if (powerType == -1)
|
||||
return unit->GetPowerType();
|
||||
#else
|
||||
if (powerType == -1)
|
||||
return unit->getPowerType();
|
||||
#endif
|
||||
|
||||
if (powerType < 0 || powerType >= int(MAX_POWERS))
|
||||
luaL_argerror(L, 2, "valid Powers expected");
|
||||
@@ -910,7 +915,11 @@ namespace LuaUnit
|
||||
uint32 type = Eluna::CHECKVAL<uint32>(L, 2);
|
||||
if (type >= int(MAX_POWERS))
|
||||
return luaL_argerror(L, 2, "valid Powers expected");
|
||||
#if (!defined(TRINITY) && defined(WOTLK))
|
||||
unit->SetPowerType((Powers)type);
|
||||
#else
|
||||
unit->setPowerType((Powers)type);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,58 +4,83 @@
|
||||
-- Please see the included DOCS/LICENSE.md for more information
|
||||
--
|
||||
|
||||
--[[
|
||||
Functions:
|
||||
Object:GetData(instance) -- returns a table unique for the object or it's instance
|
||||
Object:RemoveData(instance) -- deletes the unique table for the object or it's instance
|
||||
local variableStores = {
|
||||
Map = {},
|
||||
Player = {},
|
||||
Creature = {},
|
||||
GameObject = {},
|
||||
}
|
||||
|
||||
Usage:
|
||||
-- Set a variable
|
||||
creature:GetData().escorted = true
|
||||
-- Check a variable
|
||||
if(creature:GetData().escorted) then
|
||||
print(creature:GetName().." has been escorted")
|
||||
end
|
||||
-- Remove all variables (on death, logout etc)
|
||||
creature:RemoveData()
|
||||
]]
|
||||
|
||||
local T = {}
|
||||
function Object:GetData(instance)
|
||||
assert(self, "ObjectVariables: self was nil")
|
||||
if(instance) then
|
||||
if(not self.GetInstanceId or not self.GetMapId) then
|
||||
error("instance is true and object is not worldobject", 2)
|
||||
end
|
||||
local map = -self:GetMapId()
|
||||
local ID = self:GetInstanceId()
|
||||
if(not T[map]) then
|
||||
T[map] = {}
|
||||
end
|
||||
if(not T[map][ID]) then
|
||||
T[map][ID] = {}
|
||||
end
|
||||
return T[map][ID]
|
||||
else
|
||||
local ID = self:GetGUID()
|
||||
if(not T[ID]) then
|
||||
T[ID] = {}
|
||||
end
|
||||
return T[ID]
|
||||
end
|
||||
end
|
||||
function Object:RemoveData(instance)
|
||||
assert(self, "ObjectVariables: self was nil")
|
||||
if(instance) then
|
||||
if(not self.GetInstance or not self.GetMapId) then
|
||||
error("instance or map is true and object is not worldobject", 2)
|
||||
end
|
||||
local map = -self:GetMapId()
|
||||
local ID = self:GetInstanceId()
|
||||
if(T[map]) then
|
||||
T[map][ID] = nil
|
||||
local function DestroyData(event, obj)
|
||||
if (event == 18) then
|
||||
local mapid = obj:GetMapId()
|
||||
local instid = obj:GetInstanceId()
|
||||
if (variableStores.Map[mapid]) then
|
||||
variableStores.Map[mapid][instid] = nil
|
||||
end
|
||||
else
|
||||
T[self:GetGUID()] = nil
|
||||
variableStores[obj:GetObjectType()][obj:GetGUIDLow()] = nil
|
||||
end
|
||||
end
|
||||
|
||||
local function GetData(self, field)
|
||||
local Type = self:GetObjectType()
|
||||
local varStore = variableStores[Type]
|
||||
local id
|
||||
if (Type == "Map") then
|
||||
local mapid = self:GetMapId()
|
||||
varStore = varStore[mapid]
|
||||
if (not varStore) then
|
||||
return nil
|
||||
end
|
||||
id = self:GetInstanceId()
|
||||
else
|
||||
id = self:GetGUIDLow()
|
||||
end
|
||||
|
||||
if (not varStore[id]) then
|
||||
return nil
|
||||
end
|
||||
|
||||
if (field == nil) then
|
||||
return varStore[id]
|
||||
else
|
||||
return varStore[id][field]
|
||||
end
|
||||
end
|
||||
|
||||
local function SetData(self, field, val)
|
||||
assert(field ~= nil, "field was nil", 2)
|
||||
|
||||
local Type = self:GetObjectType()
|
||||
local varStore = variableStores[Type]
|
||||
local id
|
||||
if (Type == "Map") then
|
||||
local mapid = self:GetMapId()
|
||||
|
||||
if (not varStore[mapid]) then
|
||||
varStore[mapid] = {}
|
||||
end
|
||||
varStore = varStore[mapid]
|
||||
|
||||
id = self:GetInstanceId()
|
||||
else
|
||||
id = self:GetGUIDLow()
|
||||
end
|
||||
|
||||
if (not varStore[id]) then
|
||||
varStore[id] = {}
|
||||
end
|
||||
|
||||
varStore[id][field] = val
|
||||
end
|
||||
|
||||
for k,v in pairs(variableStores) do
|
||||
_G[k].GetData = GetData
|
||||
_G[k].SetData = SetData
|
||||
end
|
||||
|
||||
RegisterPlayerEvent(4, DestroyData)
|
||||
RegisterServerEvent(31, DestroyData)
|
||||
RegisterServerEvent(32, DestroyData)
|
||||
RegisterServerEvent(18, DestroyData)
|
||||
|
||||
Reference in New Issue
Block a user