Repository navigation
Expand file tree
/
Copy pathConditions.lua
More file actions
416 lines (387 loc) · 17.9 KB
/
Copy pathConditions.lua
File metadata and controls
416 lines (387 loc) · 17.9 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
local _, BindCards = ...
local VALID_ROLES = { DAMAGER = true, HEALER = true, TANK = true }
local VALID_CONTENT = { ANY = true, PVE = true, PVP = true }
local VALID_ACTIVITIES = { SKYRIDING = true }
local VALID_CLASSES = BindCards.VALID_CLASSES
-- `advflyable,flying` is true only while advanced flight is actually airborne.
-- Deliberately do not require `mounted`: Evoker Soar and flight forms use the
-- same secure state without being mounts.
local SKYRIDING_CONDITION = "nodead,advflyable,flying"
local FORM_DEFINITIONS = {
druid_caster = { class = "DRUID", label = "Caster" },
druid_bear = { class = "DRUID", label = "Bear", spellID = 5487 },
druid_cat = { class = "DRUID", label = "Cat", spellID = 768 },
druid_moonkin = { class = "DRUID", label = "Moonkin", spellID = 24858 },
druid_travel = { class = "DRUID", label = "Travel", spellID = 783, spellAliases = { 1066, 33943, 40120 } },
priest_shadowform = { class = "PRIEST", label = "Shadowform", spellID = 232698, spellAliases = { 15473 } },
rogue_stealth = { class = "ROGUE", label = "Stealth", stealth = true },
}
local FORM_OPTIONS = {
DRUID = { "druid_caster", "druid_bear", "druid_cat", "druid_moonkin", "druid_travel" },
PRIEST = { "priest_shadowform" },
ROGUE = { "rogue_stealth" },
}
BindCards.FORM_DEFINITIONS = FORM_DEFINITIONS
function BindCards:GetFormOptions(class)
local result = {}
for _, id in ipairs(FORM_OPTIONS[class] or {}) do
local definition = FORM_DEFINITIONS[id]
result[#result + 1] = { id = id, label = definition.label, class = definition.class }
end
return result
end
function BindCards:IsFormValidForClass(form, class)
local definition = FORM_DEFINITIONS[form]
return definition ~= nil and definition.class == class
end
function BindCards:ResolveFormIndices(class)
self.resolvedFormIndices = self.resolvedFormIndices or {}
if InCombatLockdown and InCombatLockdown() then return self.resolvedFormIndices[class] or {} end
local resolved = {}
if class ~= "DRUID" and class ~= "PRIEST" then
self.resolvedFormIndices[class] = resolved
return resolved
end
if not GetNumShapeshiftForms or not GetShapeshiftFormInfo then return self.resolvedFormIndices[class] or resolved end
local ok, count = pcall(GetNumShapeshiftForms)
if not ok then return self.resolvedFormIndices[class] or resolved end
if issecretvalue and issecretvalue(count) then return self.resolvedFormIndices[class] or resolved end
count = tonumber(count) or 0
local wanted = {}
for _, id in ipairs(FORM_OPTIONS[class] or {}) do
local spellID = FORM_DEFINITIONS[id].spellID
if spellID then wanted[spellID] = id end
for _, alias in ipairs(FORM_DEFINITIONS[id].spellAliases or {}) do wanted[alias] = id end
end
for index = 1, count do
local infoOK, _, _, _, spellID = pcall(GetShapeshiftFormInfo, index)
if not infoOK then spellID = nil end
if issecretvalue and issecretvalue(spellID) then spellID = nil end
local id = wanted[tonumber(spellID)]
if id then
resolved[id] = resolved[id] or {}
resolved[id][#resolved[id] + 1] = index
end
end
self.resolvedFormIndices[class] = resolved
return resolved
end
function BindCards:GetCurrentForm(class)
if class == "ROGUE" then
if not IsStealthed then return nil end
local ok, active = pcall(IsStealthed)
if not ok then return nil end
if issecretvalue and issecretvalue(active) then return nil end
return active == true and "rogue_stealth" or nil
end
if class ~= "DRUID" and class ~= "PRIEST" then return nil end
if not GetShapeshiftForm then return nil end
local ok, index = pcall(GetShapeshiftForm)
if not ok then return nil end
if issecretvalue and issecretvalue(index) then return nil end
index = tonumber(index)
if class == "DRUID" and (not index or index == 0) then return "druid_caster" end
for id, resolvedIndices in pairs(self:ResolveFormIndices(class)) do
for _, resolvedIndex in ipairs(resolvedIndices) do
if resolvedIndex == index then return id end
end
end
return nil
end
function BindCards:GetFormStateDriver(class)
if class == "ROGUE" then return "[stealth] rogue_stealth; base" end
local clauses, indices = {}, self:ResolveFormIndices(class)
for _, id in ipairs(FORM_OPTIONS[class] or {}) do
if id ~= "druid_caster" then
for _, index in ipairs(indices[id] or {}) do
clauses[#clauses + 1] = ("[form:%d] %s"):format(index, id)
end
end
end
if class == "DRUID" then clauses[#clauses + 1] = "[noform] druid_caster" end
clauses[#clauses + 1] = "base"
return table.concat(clauses, "; ")
end
function BindCards:GetConditionalBindingStateDriver(class, includeSkyriding)
if not includeSkyriding then return self:GetFormStateDriver(class) end
local clauses, indices = {}, self:ResolveFormIndices(class)
if class == "ROGUE" then
clauses[#clauses + 1] = "[" .. SKYRIDING_CONDITION .. ",stealth] skyriding_rogue_stealth"
else
for _, id in ipairs(FORM_OPTIONS[class] or {}) do
if id ~= "druid_caster" then
for _, index in ipairs(indices[id] or {}) do
clauses[#clauses + 1] = ("[%s,form:%d] skyriding_%s"):format(
SKYRIDING_CONDITION, index, id)
end
end
end
if class == "DRUID" then
clauses[#clauses + 1] = "[" .. SKYRIDING_CONDITION .. ",noform] skyriding_druid_caster"
end
end
clauses[#clauses + 1] = "[" .. SKYRIDING_CONDITION .. "] skyriding"
if class == "ROGUE" then
clauses[#clauses + 1] = "[stealth] rogue_stealth"
else
for _, id in ipairs(FORM_OPTIONS[class] or {}) do
if id ~= "druid_caster" then
for _, index in ipairs(indices[id] or {}) do
clauses[#clauses + 1] = ("[form:%d] %s"):format(index, id)
end
end
end
if class == "DRUID" then clauses[#clauses + 1] = "[noform] druid_caster" end
end
clauses[#clauses + 1] = "base"
return table.concat(clauses, "; ")
end
function BindCards.GetCurrentActivity()
if type(SecureCmdOptionParse) ~= "function" then return nil end
local ok, state = pcall(SecureCmdOptionParse, "[" .. SKYRIDING_CONDITION .. "] SKYRIDING")
if not ok or (issecretvalue and issecretvalue(state)) then return nil end
return state == "SKYRIDING" and state or nil
end
function BindCards:GetPlayerContext()
local _, class = UnitClass("player")
if issecretvalue and issecretvalue(class) then class = nil end
local getSpec = C_SpecializationInfo and C_SpecializationInfo.GetSpecialization or GetSpecialization
local specIndex = getSpec and getSpec() or nil
if issecretvalue and issecretvalue(specIndex) then specIndex = nil end
local specID, role
if specIndex then
local _, _, _, _, specRole
local getSpecInfo = C_SpecializationInfo and C_SpecializationInfo.GetSpecializationInfo or GetSpecializationInfo
if getSpecInfo then specID, _, _, _, specRole = getSpecInfo(specIndex) end
if issecretvalue and issecretvalue(specID) then specID = nil end
if issecretvalue and issecretvalue(specRole) then specRole = nil end
role = specRole
end
return {
class = class,
role = role,
specID = specID,
content = self.GetCurrentContentType(),
form = self:GetCurrentForm(class),
activity = self.GetCurrentActivity(),
}
end
local function SafeResult(callback, ...)
if type(callback) ~= "function" then return nil end
local ok, value, second = pcall(callback, ...)
if not ok or (issecretvalue and issecretvalue(value)) then return nil end
if issecretvalue and issecretvalue(second) then second = nil end
return value, second
end
function BindCards.GetCurrentContentType()
local inInstance, instanceType = SafeResult(IsInInstance)
if inInstance == true then
return (instanceType == "arena" or instanceType == "pvp") and "PVP" or "PVE"
end
local warModeActive = C_PvP and SafeResult(C_PvP.IsWarModeActive)
local warModeDesired = C_PvP and SafeResult(C_PvP.IsWarModeDesired)
if warModeActive == true or warModeDesired == true then return "PVP" end
local zoneType = C_PvP and SafeResult(C_PvP.GetZonePVPInfo)
if zoneType == "arena" or zoneType == "combat" then return "PVP" end
if SafeResult(UnitIsPVP, "player") == true then return "PVP" end
return "PVE"
end
function BindCards.StripTalentLoadoutCondition(condition)
if type(condition) ~= "table" or
(condition.talentConfigID == nil and condition.talentConfigOwner == nil) then
return condition, false
end
local stripped = {}
for key, value in pairs(condition) do
if key ~= "talentConfigID" and key ~= "talentConfigOwner" then stripped[key] = value end
end
return stripped, true
end
function BindCards:NormalizeCondition(condition, resolveCurrent)
if condition == nil then condition = {} end
if type(condition) ~= "table" then return {}, false, "Condition must be a table." end
local result = {}
local role = condition.role
if role == "Any role" then role = nil end
if role ~= nil and not VALID_ROLES[role] then return {}, false, "Invalid role condition." end
result.role = role
local class = condition.class
if class == "Any class" then class = nil end
if class ~= nil and not VALID_CLASSES[class] then return {}, false, "Invalid class condition." end
result.class = class
local specID = condition.specID
if specID == "Any spec" then specID = nil end
if specID == "Current spec" and resolveCurrent then specID = self:GetPlayerContext().specID end
if specID ~= nil then
specID = tonumber(specID)
if not specID or specID <= 0 or specID % 1 ~= 0 then return {}, false, "Invalid specialization condition." end
result.specID = specID
end
local content = condition.content
if content == nil or content == "Any content" then content = "ANY" end
if type(content) == "string" then content = content:upper() end
if not VALID_CONTENT[content] then return {}, false, "Invalid content condition." end
result.content = content
if condition.talentConfigID ~= nil or condition.talentConfigOwner ~= nil then
return {}, false, "Talent loadout card conditions are no longer supported."
end
local form = condition.form
if form == "Any form" then form = nil end
if form ~= nil and not FORM_DEFINITIONS[form] then return {}, false, "Invalid form condition." end
if form ~= nil and result.class ~= nil and FORM_DEFINITIONS[form].class ~= result.class then
return {}, false, "Form and class conditions do not match."
end
result.form = form
local activity = condition.activity
if activity == "Any activity" then activity = nil end
if type(activity) == "string" then activity = activity:upper() end
if activity ~= nil and not VALID_ACTIVITIES[activity] then return {}, false, "Invalid activity condition." end
result.activity = activity
return result, true
end
local function ConditionSpecificity(condition)
local score = 0
if condition.role then score = score + 1 end
if condition.class then score = score + 2 end
if condition.specID then score = score + 4 end
if condition.content ~= "ANY" then score = score + 8 end
if condition.form then score = score + 32 end
if condition.activity then score = score + 64 end
return score
end
local function NormalizedConditionMatches(condition, context)
if condition == false then return false end
if condition.role and condition.role ~= context.role then return false end
if condition.class and condition.class ~= context.class then return false end
if condition.specID and condition.specID ~= context.specID then return false end
if condition.content ~= "ANY" and condition.content ~= context.content then return false end
if condition.form and condition.form ~= context.form then return false end
if condition.activity and condition.activity ~= context.activity then return false end
return true
end
function BindCards:GetConditionSpecificity(condition)
local valid
condition, valid = self:NormalizeCondition(condition, false)
if not valid then return 0 end
return ConditionSpecificity(condition)
end
function BindCards:ConditionMatches(condition, context)
local valid
condition, valid = self:NormalizeCondition(condition, false)
if not valid then return false end
context = context or self:GetPlayerContext()
return NormalizedConditionMatches(condition, context)
end
function BindCards:GetActiveCards(profile, context)
profile = profile or self:GetSelectedProfile()
if not profile then return {}, {} end
context = context or self:GetPlayerContext()
local cardsById, active, depths, specificity, binderFamilies, binderIds = {}, {}, {}, {}, {}, {}
local normalizedConditions, conditionSpecificity = {}, {}
for _, card in ipairs(profile.cards or {}) do
cardsById[card.id] = card
local normalized, valid = self:NormalizeCondition(card.condition, false)
normalizedConditions[card.id] = valid and normalized or false
conditionSpecificity[card.id] = valid and ConditionSpecificity(normalized) or 0
end
local function resolveBinder(card, trail)
if binderIds[card.id] ~= nil then return binderIds[card.id] or nil end
trail = trail or {}
if trail[card.id] then binderIds[card.id] = false; return nil end
trail[card.id] = true
local owner = cardsById[card.parentId or card.homeParentId]
local binderId
if owner then
binderId = resolveBinder(owner, trail)
else
binderId = self:NormalizeBinderId(card.binderId)
if not binderId then
local templateClass = type(card.templateKey) == "string" and
card.templateKey:match("^default:class:(.+)$")
local conditionClass = card.condition and card.condition.class
binderId = templateClass and ("class:" .. templateClass) or
(conditionClass and ("class:" .. conditionClass) or self.GENERAL_BINDER_ID)
end
end
trail[card.id] = nil
binderIds[card.id] = binderId or false
binderFamilies[card.id] = type(binderId) == "string" and binderId:match("^class:") and "class" or "general"
return binderId
end
for _, card in ipairs(profile.cards or {}) do resolveBinder(card) end
local function evaluate(card, trail)
if active[card.id] ~= nil then return active[card.id], depths[card.id], specificity[card.id] end
if trail[card.id] then
active[card.id], depths[card.id], specificity[card.id] = false, 0, 0
return false, 0, 0
end
trail[card.id] = true
local parentActive, depth, parentSpecificity = true, 0, 0
local ownerId = card.parentId or card.homeParentId
if ownerId then
local parent = cardsById[ownerId]
if not parent then
parentActive = false
else
parentActive, depth, parentSpecificity = evaluate(parent, trail)
depth = depth + 1
end
end
trail[card.id] = nil
local binderClass = binderIds[card.id] and binderIds[card.id]:match("^class:(.+)$")
local binderMatches = not binderClass or binderClass == context.class
active[card.id] = card.enabled ~= false and parentActive and binderMatches and
NormalizedConditionMatches(normalizedConditions[card.id], context)
depths[card.id] = depth
specificity[card.id] = parentSpecificity + conditionSpecificity[card.id]
return active[card.id], depth, specificity[card.id]
end
local result = {}
for _, card in ipairs(profile.cards or {}) do
if evaluate(card, {}) then result[#result + 1] = card end
end
local children, roots = {}, { general = {}, class = {} }
local branchSpecificity = {}
local function BranchSpecificity(card)
if branchSpecificity[card.id] then return branchSpecificity[card.id] end
local score = specificity[card.id]
for _, child in ipairs(children[card.id] or {}) do
score = math.max(score, BranchSpecificity(child))
end
branchSpecificity[card.id] = score
return score
end
local function SortPeers(peers)
table.sort(peers, function(left, right)
if (left.priority or 0) ~= (right.priority or 0) then
return (left.priority or 0) < (right.priority or 0)
end
local leftSpecificity, rightSpecificity = BranchSpecificity(left), BranchSpecificity(right)
if leftSpecificity ~= rightSpecificity then return leftSpecificity < rightSpecificity end
if (left.order or 0) ~= (right.order or 0) then return (left.order or 0) < (right.order or 0) end
return left.id < right.id
end)
end
for _, card in ipairs(result) do
local ownerId = card.parentId or card.homeParentId
if ownerId and active[ownerId] then
children[ownerId] = children[ownerId] or {}
children[ownerId][#children[ownerId] + 1] = card
else
local family = binderFamilies[card.id] == "class" and "class" or "general"
roots[family][#roots[family] + 1] = card
end
end
SortPeers(roots.general)
SortPeers(roots.class)
for _, peers in pairs(children) do SortPeers(peers) end
local ordered = {}
local function Emit(card)
ordered[#ordered + 1] = card
for _, child in ipairs(children[card.id] or {}) do Emit(child) end
end
for _, family in ipairs({ "general", "class" }) do
for _, root in ipairs(roots[family]) do Emit(root) end
end
return ordered, active
end