-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPetLeash.lua
535 lines (440 loc) · 12.6 KB
/
PetLeash.lua
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
local addon_name, addon = ...
PetLeash = LibStub("AceAddon-3.0"):NewAddon(addon, addon_name, "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("PetLeash")
local LibPetJournal = LibStub("LibPetJournal-2.0")
local assert, C_PetJournal, fastrandom, getmetatable,
hooksecurefunc, InCombatLockdown, IsInInstance, next, pairs,
setmetatable, strfind, tinsert, wipe
= assert, C_PetJournal, fastrandom, getmetatable,
hooksecurefunc, InCombatLockdown, IsInInstance, next, pairs,
setmetatable, strfind, tinsert, wipe
local math_huge = math.huge
--
-- Binding globals
--
BINDING_HEADER_PETLEASH = addon_name
BINDING_NAME_PETLEASH_SUMMON = L["Summon Another Pet"]
BINDING_NAME_PETLEASH_DESUMMON = L["Dismiss Pet"]
BINDING_NAME_PETLEASH_TOGGLE = L["Toggle Non-Combat Pet"]
BINDING_NAME_PETLEASH_CONFIG = L["Open Configuration"]
--
-- Default DB
--
local defaults = {
profile = {
enable = true,
enableInBattleground = true,
dismissInBattleground = false,
disableOutsideCities = false,
dismissWhenStealthed = false,
disableForQuestItems = true,
enableInPVE = true,
dismissInPVE = false,
nearPEWOnly = false,
overrideSetLoadout = false,
waitTimer = 3,
verbose = true,
autoSwitch = {
timer = false,
timerValue = 30*60,
citiesOnly = false,
onZone = false,
},
minimap = {
hide = true,
},
sets = {
["$Default"] = {
name = DEFAULT,
enabled = true,
defaultValue = 1,
priority = -1,
pets = {},
filter = {},
},
["*"] = {
enabled = true,
defaultValue = 0,
priority = 1,
pets = {},
filter = {},
trigger = {}
}
},
}
}
local default_ignore_override = {
-- by speciesID
[114] = true, -- Disgusing Oozling (Combat Effect)
[283] = true, -- Guild Page, Horde (Long cooldown)
[281] = true, -- Guild Herald, Horde (Long cooldown)
[280] = true, -- Guild Page, Alliance (Long cooldown)
[282] = true, -- Guild Herald, Alliance (Long cooldown)
}
--
-- Initialization
--
function addon:OnInitialize()
self.setCache = {}
self.db = LibStub("AceDB-3.0"):New("PetLeash3DB", defaults, true)
self.db.RegisterCallback(self, "OnProfileChanged", "OnProfileChange")
self.db.RegisterCallback(self, "OnProfileCopied", "OnProfileChange")
self.db.RegisterCallback(self, "OnProfileReset", "OnProfileChange")
hooksecurefunc(C_PetJournal, "SetPetLoadOutInfo", function(...) self:OnSetPetLoadOut(...) end)
LibPetJournal.RegisterCallback(self, "PetsUpdated")
end
function addon:OnEnable()
self:ReloadSets()
end
function addon:OnProfileChange()
self:ReloadSets()
self.Ready:UpdateChecks(self.db.profile)
end
function addon:ReloadSets()
for _,set in pairs(self.setCache) do
set:Free()
end
wipe(self.setCache)
self.currentSet = nil
self:UpdateCurrentSet()
end
--
-- Utility
--
function addon:IsEnabledSummoning()
return self.db.profile.enable
end
function addon:EnableSummoning(v)
local oldv = self.db.profile.enable
if((not oldv) ~= (not v)) then
self.db.profile.enable = v
-- TODO: is there a better way to trigger config update?
LibStub("AceConfigRegistry-3.0"):NotifyChange("PetLeash")
-- XXX Use :Enable/:Disable instead
self:SendMessage("PetLeash-EnableState", v)
end
end
function addon:OpenOptions()
self:GetModule("Options"):OpenOptions()
end
function addon:DumpDebugState()
if self:IsEnabledSummoning() then
self:Printf("Enabled")
else
self:Printf("Disabled")
end
for _, module in self:IterateModules() do
local f = module["DumpDebugState"]
if f then
f(module)
end
end
end
function addon:InBattlegroundOrArena()
local _,t = IsInInstance()
return t == "pvp" or t == "arena"
end
function addon:InInstanceOrRaid()
local _,t = IsInInstance()
return t == "party" or t == "raid"
end
function addon:OnSetPetLoadOut(slotid)
if self.db.profile.overrideSetLoadout and slotid == 1 then
self:ResummonPet()
end
end
function addon:CurrentPet()
return C_PetJournal.GetSummonedPetGUID()
end
function addon:PetsUpdated()
for _, set in self:IterateSets() do
if set.RefreshSet then
set:RefreshSet()
end
end
end
--
-- Sets
--
local SetMT = {
Refresh = function(self)
self.noNotify = true
self.name = self.settings.name or self.name
self:RefreshTriggers()
self:RefreshSet()
self.noNotify = false
self:_NotifyAddOn()
end,
RefreshTriggers = function(self)
assert(not self.invalid)
addon:RebuildSetTriggers(self)
self:UpdateValue()
end,
RefreshSet = function(self)
assert(not self.invalid)
if self.settings.enabled and self.value then
self:_NotifyAddOn()
end
self.setDirty = true
wipe(self.weightTable)
end,
GetPriority = function(self)
if self.invalid then
return -math_huge
end
return self.settings.priority
end,
IsActive = function(self)
if self.invalid then
return false
end
if not self.settings.enabled or not self.value then
return false
end
if self.setDirty then
addon:BuildSetWeights(self.settings, self.weightTable)
end
return self.weightTable.total > 0
end,
IsImmediate = function(self)
return self.settings.immediate
end,
UpdateValue = function(self)
local alltrue = true
for tr in pairs(self.triggers) do
if not tr.value then
alltrue = false
break
end
end
if self.value ~= alltrue then
self.value = alltrue
self:_NotifyAddOn()
end
end,
Contains = function(self, petID)
if self.setDirty then
addon:BuildSetWeights(self.settings, self.weightTable)
end
for i = 1,#self.weightTable,2 do
if petID == self.weightTable[i+1] then
return true
end
end
return false
end,
PickPet = function(self)
assert(not self.invalid)
if self.setDirty then
addon:BuildSetWeights(self.settings, self.weightTable)
end
local r = fastrandom()*self.weightTable.total
for i = 1,#self.weightTable,2 do
local w, id = self.weightTable[i], self.weightTable[i+1]
r = r - w
if r <= 0 then
return id
end
end
end,
_NotifyAddOn = function(self)
if not self.noNotify then
addon:NotifySetStateChanged(self)
end
end,
Free = function(self)
self.invalid = true
if addon.currentSet == self then
addon:UpdateCurrentSet()
end
addon:FreeSetTriggers(self)
end,
Rename = function(self, name)
assert(not strfind(name, "^%$"))
assert(addon.db.profile.sets[self.name])
-- XXX handle renaming over an existing (reload everything?)
addon.db.profile.sets[name] = addon.db.profile.sets[self.name]
addon.db.profile.sets[self.name] = nil
self.settings.name = name
self:Refresh()
end,
Delete = function(self)
addon.db.profile.sets[self.name] = nil
self:Free()
end,
}
SetMT.__index = SetMT
function addon:GetSetByName(name)
if self.setCache[name] then
return self.setCache[name]
elseif not self.db.profile.sets[name] then
return
end
local set = setmetatable({
name = name,
setDirty = false,
weightTable = {},
settings = self.db.profile.sets[name],
}, SetMT)
local mt = getmetatable(set.settings.pets)
if not mt or not mt.isPetsMT then
setmetatable(set.settings.pets, {
isPetsMT = true,
__index = function(_, key)
local speciesID = C_PetJournal.GetPetInfoByPetID(key)
if speciesID and default_ignore_override[speciesID] then
return 0
end
return set.settings.defaultValue
end
})
end
self.setCache[name] = set
set:Refresh()
return set
end
function addon:UpdateCurrentSet()
-- scan sets for highest priority active
local current
for _, set in self:IterateSets() do
if set:IsActive() and (not current or current:GetPriority() <= set:GetPriority()) then
current = set
end
end
if current then
self.currentSet = current
if current:IsImmediate() and not current:Contains(self:CurrentPet()) then
self:QueueResummon()
end
end
end
function addon:NotifySetStateChanged(set)
local oldCurrent = self.currentSet
local higherPrio = not oldCurrent or set:GetPriority() >= oldCurrent:GetPriority()
if higherPrio or oldCurrent == set then
self:UpdateCurrentSet()
end
end
function addon:NewSet(name)
-- XXX check for existance?
self.db.profile.sets[name].name = name
return self:GetSetByName(name)
end
function addon:DeleteSet(name)
local set = self:GetSetByName()
assert(set ~= nil)
set:Delete()
end
local function nextSetIt(a, i)
local n = next(a.db.profile.sets, i)
if n then
return n, a:GetSetByName(n)
end
end
function addon:IterateSets()
return nextSetIt, self
end
function addon:BuildSetWeights(input, output)
output = output or {}
output.total = 0
for _, petID in LibPetJournal:IteratePetIDs() do
local value = input.pets[petID]
local isSummonable = C_PetJournal.PetIsSummonable(petID)
if value and value > 0 and isSummonable then
tinsert(output, value)
output.total = output.total + value
tinsert(output, petID)
end
end
for _, filterOpt in pairs(input.filter) do
local filter = self:GetFilterByKey(filterOpt[1])
if filter then
filter:GetPets(filterOpt, output)
end
end
return output
end
function addon:GetCurrentSet()
if not self.currentSet then
self:UpdateCurrentSet()
end
return self.currentSet
end
--
-- Pet Summoning
--
function addon:PickPet()
for _, module in self:IterateModules() do
local f = module["PickPet"]
if f then
local id = f(module)
if id then
return id
end
end
end
local currentSet = self:GetCurrentSet()
if currentSet then
return currentSet:PickPet()
end
end
function addon:GetPetName(petid)
local _, customName, _, _, _, _, _, petName = C_PetJournal.GetPetInfoByPetID(petid)
return customName or petName or "?"
end
function addon:SummonPet()
local petid = self:PickPet()
if self:CurrentPet() == petid then
return false
elseif not petid then
return false
end
if C_PetJournal.GetPetCooldownByGUID and (C_PetJournal.GetPetCooldownByGUID(petid) or 0) > 0 then
return false
end
if self.db.profile.verbose then
self:Printf(L["SUMMONING_MSG"], self:GetPetName(petid))
end
C_PetJournal.SummonPetByGUID(petid)
self.SwitchTimer:Start()
return true
end
function addon:_DismissVerify(petID)
if C_PetJournal.GetSummonedPetGUID() == petID then
if not InCombatLockdown() then
C_PetJournal.SummonPetByGUID(petID)
end
else
self:CancelTimer(self.dismissVerifyHandle)
self.dismissVerifyHandle = nil
end
end
function addon:DismissPet(disable, verify)
local curid = C_PetJournal.GetSummonedPetGUID()
if curid ~= nil and curid ~= 0 then
C_PetJournal.SummonPetByGUID(curid)
if verify then
self:CancelTimer(self.dismissVerifyHandle)
self.dismissVerifyHandle = self:ScheduleRepeatingTimer("_DismissVerify", 0.3, curid)
end
end
if disable then
addon:EnableSummoning(false)
end
end
function addon:ResummonPet(enable, nowait)
if enable then
self:EnableSummoning(true)
end
self.Ready:QueueResummon(nowait)
end
function addon:QueueResummon()
self.Ready:QueueResummon()
end
function addon:TogglePet()
if self:CurrentPet() then
self:DismissPet(true)
else
self:ResummonPet(true, true)
end
end