forked from MMOSimca/LibObjectiveProgress-1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibObjectiveProgress-1.0.lua
More file actions
89 lines (74 loc) · 3.77 KB
/
LibObjectiveProgress-1.0.lua
File metadata and controls
89 lines (74 loc) · 3.77 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
-- -----------------------------------------------------------------------------
-- LibObjectiveProgress: Core API
-- Developed by: Simca@Malfurion (MMOSimca)
-- Updated by: Dack
-- -----------------------------------------------------------------------------
-- Set major/minor version
local MAJOR, MINOR = "LibObjectiveProgress", @project-revision@
assert(LibStub, MAJOR .. " requires LibStub")
-- Initialize library
local LOP, oldversion = LibStub:NewLibrary(MAJOR, MINOR)
if not LOP then return end
-- Localized function references
local table_create = _G.table.create
local CQL_GetNumQuestLogEntries = _G.C_QuestLog.GetNumQuestLogEntries
local CQL_GetInfo = _G.C_QuestLog.GetInfo
--- Precreate a table for generated weight data (``table.create``).
--- @param arraySizeHint number
--- @param hashSizeHint? number # default 0
--- @return table
function LOP:CreateWeightsTable(arraySizeHint, hashSizeHint)
return table_create(arraySizeHint, hashSizeHint or 0)
end
function LOP:GetNPCWeightByMap(mapID, npcID, isTeeming, isAlternate)
-- MapBasedWeights is preallocated below; loaders replace or fill it. Use a flag so we still run
-- LoadWeightDataByMap when the root table already exists but data has not been loaded yet.
if not LOP._weightDataByMapLoaded then
LOP:LoadWeightDataByMap()
LOP._weightDataByMapLoaded = true
end
-- Ensure that specified map/NPC is valid
local tableIndex = 1
if isTeeming then tableIndex = 2 end
if isAlternate then tableIndex = tableIndex + 2 end -- Allows alternate maps for weird scenarios
-- For Karazhan, isAlternate=false means Lower Karazhan and isAlternate=true means Upper Karazhan
-- For Siege of Boralus, isAlternate=false means Alliance and isAlternate=true means Horde
if not LOP.MapBasedWeights[mapID] or not LOP.MapBasedWeights[mapID][tableIndex] or not LOP.MapBasedWeights[mapID][tableIndex][npcID] then return nil end
-- Get map-based weight data for specified NPC
return LOP.MapBasedWeights[mapID][tableIndex][npcID]
end
function LOP:GetNPCWeightByQuest(questID, npcID)
-- QuestBasedWeights is preallocated below. Merged data assigns per-quest keys into that root;
-- stock data replaces the root. Use a load flag so we do not skip loading when the root exists
-- but is still empty.
if not LOP._weightDataByQuestLoaded then
LOP:LoadWeightDataByQuest()
LOP._weightDataByQuestLoaded = true
end
-- Ensure that specified map / NPC is valid
if not LOP.QuestBasedWeights[questID] or not LOP.QuestBasedWeights[questID][npcID] then return nil end
-- Get map-based weight data for specified NPC
return LOP.QuestBasedWeights[questID][npcID]
end
function LOP:GetNPCWeightByCurrentQuests(npcID)
-- Table variable declared here
local questTable = nil
-- Get NPC weight for all quests in log
local numEntries = CQL_GetNumQuestLogEntries()
for questLogIndex = 1, numEntries do
local questInfo = CQL_GetInfo(questLogIndex)
-- If this row isn't a header, has a valid questID, and has a valid weight, then initialize the table and record the questID/weight pair
if questInfo and not questInfo.isHeader and questInfo.questID ~= 0 then
local weight = LOP:GetNPCWeightByQuest(questInfo.questID, npcID)
if weight then
questTable = questTable or {}
questTable[questInfo.questID] = weight
end
end
end
-- Return completed table (or nil if no quests reference the NPC in question)
return questTable
end
-- Presized hash roots for weight data loaders (hints >= distinct map IDs / quest IDs in shipped data).
LOP.MapBasedWeights = LOP.MapBasedWeights or LOP:CreateWeightsTable(0, 83)
LOP.QuestBasedWeights = LOP.QuestBasedWeights or LOP:CreateWeightsTable(0, 4364)