Repository navigation
Expand file tree
/
Copy pathLibMobs.lua
More file actions
101 lines (86 loc) · 2.99 KB
/
Copy pathLibMobs.lua
File metadata and controls
101 lines (86 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local MAJOR, MINOR = "LibMobs", 10101
assert(LibStub, MAJOR .. " requires LibStub")
local lib = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end
-- Blizzard
local UnitGUID = _G.UnitGUID
local issecretvalue = _G.issecretvalue or function() return false end -- Retail only
-- Mine
function lib:print(...)
print("|cffffa1a1" .. MAJOR .. ":|r", ...)
end
lib.units = lib.units or {}
-- https://warcraft.wiki.gg/wiki/GUID#Creature
--[[ Method: lib:ParseGUID(guid)
## Parameters
* guid - unit GUID return from UnitGUID, e.g: "Creature-0-1465-0-2105-448-000043F59F"
## Returns
* info - the components of GUID
* unitType - the type of the entity. (string)
* serverID - the ID of the server (realm) where the entity was created. (number)
* instanceID - the ID of the instance where the entity exists. (number)
* zoneUID - the ID of the zone or map where the entity is located. (number)
* npcID - the entry ID of the NPC in the game database. (number)
* spawnUID - a unique identifier for the specific instance or spawn of this NPC. Differentiates between multiple spawns of the same NPC type. (string)
]]
function lib:ParseCreatureGUID(guid)
if not guid or issecretvalue(guid) then
return nil
end
local unitType, _, serverID, instanceID, zoneUID, npcID, spawnUID = string.split("-", guid)
-- validate the number of parts
if not unitType or not serverID or not instanceID or not zoneUID or not npcID or not spawnUID then
return nil -- invalid GUID type
end
-- check for valid entity types
if unitType ~= "Creature" and unitType ~= "Vehicle" and unitType ~= "Pet" then
return nil -- unsupported type
end
-- return the parsed GUID components
return {
guid = guid,
unitType = unitType,
serverID = tonumber(serverID),
instanceID = tonumber(instanceID),
zoneUID = tonumber(zoneUID),
npcID = tonumber(npcID),
spawnUID = spawnUID
}
end
--[[ Method: lib:GetNPCID(guid)
## Parameters
* unit - the unit for which you want to capture the npcID.
## Returns
* npcID - the entry ID of the NPC in the game database. (number)
]]
function lib:GetNPCID(unit)
local guid = UnitGUID(unit)
local info = self:ParseCreatureGUID(guid)
if not info then
return nil
end
return info.npcID
end
function lib:GetInstanceMobs(instanceID)
return lib.units[instanceID or "none"]
end
function lib:GetCreature(instanceID, npcID)
local data = self:GetInstanceMobs(instanceID)
if data then
return data[npcID or "none"]
end
return nil
end
function lib:UnitRole(unit)
local guid = UnitGUID(unit)
if not issecretvalue(guid) and guid then
local guidInfo = self:ParseCreatureGUID(guid)
if guidInfo then
local mobInfo = self:GetCreature(guidInfo.instanceID, guidInfo.npcID)
if mobInfo then
return mobInfo.role
end
end
end
return nil
end