-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.lua
More file actions
executable file
·851 lines (778 loc) · 31.4 KB
/
Copy pathmain.lua
File metadata and controls
executable file
·851 lines (778 loc) · 31.4 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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
-- SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
-- Copyright (c) 2022-2026 Thomas Floeren
local _, ns = ...
--[[===========================================================================
API
===========================================================================]]--
local C_PetJournal_PetIsFavorite = _G.C_PetJournal.PetIsFavorite
local C_PetJournal_SetFavorite = _G.C_PetJournal.SetFavorite
local C_PetJournal_GetPetInfoByIndex = _G.C_PetJournal.GetPetInfoByIndex
local C_PetJournal_SummonPetByGUID = _G.C_PetJournal.SummonPetByGUID
local C_PetJournal_GetSummonedPetGUID = _G.C_PetJournal.GetSummonedPetGUID
local C_PetJournal_GetPetInfoByPetID = _G.C_PetJournal.GetPetInfoByPetID
local C_PetJournal_GetPetInfoBySpeciesID = _G.C_PetJournal.GetPetInfoBySpeciesID
local C_PetJournal_FindPetIDByName = _G.C_PetJournal.FindPetIDByName
local C_PetJournal_GetBattlePetLink = _G.C_PetJournal.GetBattlePetLink
local C_PetJournal_GetOwnedBattlePetString = _G.C_PetJournal.GetOwnedBattlePetString
local C_PetJournal_GetPetSummonInfo = _G.C_PetJournal.GetPetSummonInfo
local C_Map_GetBestMapForUnit = _G.C_Map.GetBestMapForUnit
local C_UnitAuras_GetPlayerAuraBySpellID = _G.C_UnitAuras.GetPlayerAuraBySpellID
local InCombatLockdown = _G.InCombatLockdown
local IsFlying = _G.IsFlying
local IsFalling = _G.IsFalling
-- local IsMounted = _G.IsMounted
local UnitOnTaxi = _G.UnitOnTaxi
local UnitHasVehicleUI = _G.UnitHasVehicleUI
local IsPossessBarVisible = _G.IsPossessBarVisible
local UnitIsGhost = _G.UnitIsGhost
local UnitIsBattlePet = _G.UnitIsBattlePet
local GetInstanceInfo = _G.GetInstanceInfo
local IsInInstance = _G.IsInInstance
local IsStealthed = _G.IsStealthed
-- local UnitIsControlling = _G.UnitIsControlling
local UnitChannelInfo = _G.UnitChannelInfo
local C_PlayerInfo_GetGlidingInfo = C_PlayerInfo.GetGlidingInfo
local time = _G.time
local debugprint = ns.debugprint
--[[===========================================================================
Some Variables/Constants
===========================================================================]]--
ns.pool_initialized = false
--[[ This prevents the "wrong" active pet from being saved. We get a "wrong" pet
mainly after login, if the game summons the last active pet on this toon,
instead of the last saved pet in our DB (which can be the last active pet of the
alt we just logged out). Also when slotting a pet into a team in the Pet Journal
(slot#1 gets force-summoned). Caution, to not lock out manually summoned pets
from being saved.
]]
ns.pet_verified = false
ns.in_battlesleep = false
-- Last time AutoRestore() was called.
local time_restore_pet = 0
-- local time_save_pet = 0 -- What did we use this for? Debugging?
local time_pool_msg = 0
-- local time_transitioncheck = 0 -- What did we use this for? Debugging?
local msg_onlyfavisactive_alreadydisplayed = false
local time_responded_to_summoning_event = 0
local throttle_min = 3
local throttle = 0 -- throttle_min * 2
-- local throttle_reason
local bypass_throttle = false
local savedpet_is_summonable = true
local excluded_species = {
-- Pet is vendor and goes on CD when summoned
280, -- Guild Page, Alliance
281, -- Guild Page, Horde
282, -- Guild Herald, Alliance
283, -- Guild Herald, Horde
-- Pet is vendor/bank/mail, if the char has the Pony Bridle achievement (ID 3736).
-- But it should be safe, bc CD starts only after activating the ability via dialog.
-- 214, -- Argent Squire (Alliance)
-- 216, -- Argent Gruntling (Horde)
-- Self-despawns outside of Winter Veil event. Makes no sense to summon these.
1349, -- Rotten Little Helper
117, -- Tiny Snowman
119, -- Father Winter's Helper
120, -- Winter's Little Helper
-- Pocopoc is special: he cannot be summoned in Zereth Mortis (1970)).
-- See the extra checks in is_excluded_by_species() and transitioncheck().
3247, -- Pocopoc
-- Dummy ID for debugging. Keep this commented out!
-- 2403, -- Abyssal Eel
}
-- Debug
ns.time_summonspell = 0
--[[---------------------------------------------------------------------------
Summoning prevention
---------------------------------------------------------------------------]]--
-- Besides combat lockdown, this is the only test we use with manual summoning
local function is_inair()
return IsFlying() or IsFalling() or UnitOnTaxi 'player'
end
-- Other possibility: UnitPowerBarID('player') == 631
local function is_skyride_mounted()
return select(2, C_PlayerInfo_GetGlidingInfo())
end
local function forbidden_instance()
local in_instance, instance_type = IsInInstance()
if not in_instance then return false end
if instance_type == 'arena' then return true end
if instance_type == 'party' then
local _, _, difficulty_id = GetInstanceInfo()
if difficulty_id == 8 then return true end
end
end
-- To test against if pet-on-back aura is found (AFAIK, only Daisy)
local function saved_pet_is_backpet()
local backpet = 2780 -- Daisy
local db = ns.dbc.charFavsEnabled and ns.db.favsOnly and ns.dbc or ns.db
return ns.id_to_species(db.recentPets[1]) == backpet
end
-- To test against if pet-on-shoulder aura is found
local function saved_pet_is_shoulderpet()
local shoulderpets = { 2526, 1997, 2185 } -- Feathers, Crackers, Cap'n Crackers
local db = ns.dbc.charFavsEnabled and ns.db.favsOnly and ns.dbc or ns.db
for _, species in ipairs(shoulderpets) do
if ns.id_to_species(db.recentPets[1]) == species then return true end
end
end
-- To be called from autoaction (and - testwise- from transitioncheck)
local function stop_auto_summon(now)
-- Base/existing throttle
if not bypass_throttle then
throttle = max(throttle, throttle_min)
now = now or time()
if now - time_responded_to_summoning_event < throttle then
debugprint('‹stop_auto_summon()›: existing throttle found:', throttle)
return true
end
time_responded_to_summoning_event, throttle = now, 0
end
-- Prevent summoning and add extra throttle
-- throttle_reason = nil
-- Impossible to summon in flight, but this prevents wrong 'restored' messages
if is_inair() then
throttle = 5 -- throttle_reason = 'inair'
debugprint 'We are in the air!'
elseif InCombatLockdown()
or not ns.db.drSummoning and is_skyride_mounted()
or IsStealthed() -- Includes Hunter Camouflage
or C_UnitAuras_GetPlayerAuraBySpellID(32612) -- Mage: Invisibility
or C_UnitAuras_GetPlayerAuraBySpellID(110960) -- Mage: Greater Invisibility
or C_UnitAuras_GetPlayerAuraBySpellID(131347) -- DH: Gliding -- Prolly not needed, should be caught by IsFlying() (?)
-- or C_UnitAuras_GetPlayerAuraBySpellID(5384) -- Hunter: Feign Death (only useful if we use a different event than PLAYER_STARTED_MOVING)
-- *Any* channeling. Also prevents interrupting a Fishing channel that was started while still mounted.
or UnitChannelInfo 'player' -- Cata/Classic: `ChannelInfo()`
then
throttle = 10
elseif UnitIsGhost 'player'
-- Still needed? Why did we add this? Maybe covered by the newly added `HasVehicleActionBar()`?
or UnitHasVehicleUI 'player'
-- Controller toys etc. and some quests; coincides sometimes with `HasVehicleActionBar()` (not in case of aura 212754!)
or IsPossessBarVisible()
-- Quest 'The Hole Deal' (84142) and 'Boomball' (85263) in TWW Undermine zone; possibly more, who knows.
-- Also Mr. Delver in the Sidestreet delve.
or HasVehicleActionBar()
-- This is the only one that is true when we have the D.R.I.V.E. UI.
-- But so far there aren't any conflicts. Let's see if users report some.
-- or HasOverrideActionBar()
-- Potentially useful (1): `CanExitVehicle()`, `UnitInVehicle 'player'`
-- Potentially useful (2); this is the 1-button bar with the ExtraActionButton.
-- This would cover things like the aura 467865 (Sunrise Sudser), used in WQ 85390 in Undermine. (This particular aura
-- gives the player a sort of channeling, but not really, though it does get interrupted by pet summoning.) However,
-- this is not critical because – unlike the above vehicle-like stuff – the ExtraActionBar usually does not force-dismiss
-- the pet, so it is unlikely that it triggers a pet summoning by PW. And if it really does, the user can simply click
-- the ExtraActionButton again.
-- If we use this, we would also disable PW in many situations where it is absolutely not necessary (and not desirable).
-- HasExtraActionBar()
-- Note: The action bar modification by Skyriding is a so called Bonus Bar.
-- Daisy pet as backpack (/beckon). Disappears when Daisy is summoned.
or C_UnitAuras_GetPlayerAuraBySpellID(311796) and saved_pet_is_backpet() -- Daisy
-- Pets on shoulder (/whistle). Disappears when any of the "shoulder pets" is summoned.
or C_UnitAuras_GetPlayerAuraBySpellID(302954) and saved_pet_is_shoulderpet() -- Feathers
or C_UnitAuras_GetPlayerAuraBySpellID(232871) and saved_pet_is_shoulderpet() -- Crackers
or C_UnitAuras_GetPlayerAuraBySpellID(286268) and saved_pet_is_shoulderpet() -- Cap'n Crackers
-- Game events
or C_UnitAuras_GetPlayerAuraBySpellID(312993) -- Carrying Forbidden Tomes (Scrivener Lenua event, Revendreth)
or C_UnitAuras_GetPlayerAuraBySpellID(43880) -- Ramstein's Swift Work Ram (Brewfest daily; important bc the quest cannot be restarted if messed up)
or C_UnitAuras_GetPlayerAuraBySpellID(43883) -- Rental Racing Ram (Brewfest daily)
then
throttle = 40
elseif forbidden_instance() then
-- Our events will be re-enabled at the next PLAYER_ENTERING_WORLD
ns.events:unregister_summon_events()
throttle = 1 -- Must be > 0 to stop the autoaction in progress
end
if throttle > 0 then
debugprint('‹stop_auto_summon()›: new throttle:', throttle)
return true
end
end
-- For a manual action, we don't want all the checks from `stop_auto_summon` or a throttle.
-- Combat check is needed though to not generate errors.
local function stop_manual_summon()
if InCombatLockdown() or is_inair() then
ns.msg_manual_summon_stopped()
return true
end
end
--[[---------------------------------------------------------------------------
Unsummonable pets (faction-locked pets)
---------------------------------------------------------------------------]]--
--[[ This can happen when…
- Restore: Logging in to other-faction alt and the saved pet is locked to the previous toon's faction.
- Random summon: A faction-locked pet that is not detectable as such (Blizz bug, see below) gets selected from the pool.
]]
local unsummonable_species
local player_faction = UnitFactionGroup 'player'
-- Add only pets here that are bugged, i.e. not detected as unsummonable by `GetPetSummonInfo`.
if player_faction == 'Alliance' then
unsummonable_species = {
[2777] = true, -- Gillvanas
[342] = true, -- Festival Lantern
[332] = true, -- Horde Balloon
}
else
unsummonable_species = {
[2778] = true, -- Finduin
[341] = true, -- Lunar Lantern
[331] = true, -- Alliance Balloon
}
end
local EnumPetJournalError = _G.Enum.PetJournalError
-- Couldn't find a Blizz constant for the string
-- This should match the output of C_PetJournal.GetPetSummonInfo for Enum.PetJournalError.InvalidFaction (3).
local ERROR_TEXT_WRONG_PET_FACTION = 'You are not the right faction for this companion.'
local function is_pet_summonable(guid)
local is_summonable, error_num, error_text
local species_id = ns.id_to_species(guid)
if unsummonable_species[species_id] then
is_summonable, error_num, error_text = false, EnumPetJournalError.InvalidFaction, ERROR_TEXT_WRONG_PET_FACTION
else
is_summonable, error_num, error_text = C_PetJournal_GetPetSummonInfo(guid)
end
if not is_summonable then
return false, error_num, error_text
end
return true
end
--[[
TODO: Rework this function:
- Since implementation of history we can have more than one "previous" pets
- A nil at index 2 will stop the iteration! --> done
]]
function ns.saved_pet_summonability_check() --- After login
local priorities, perchar = {}, nil
if ns.dbc.charFavsEnabled then
perchar = true
priorities = {
ns.dbc.recentPets[1],
ns.dbc.recentPets[2],
ns.db.recentPets[1],
ns.db.recentPets[2]
}
else
priorities = {
ns.db.recentPets[1],
ns.db.recentPets[2],
ns.dbc.recentPets[1],
ns.dbc.recentPets[2],
}
end
for i, guid in pairs(priorities) do
if guid then
local is_summonable, error_num, error_text = is_pet_summonable(guid)
if i == 1 then
if is_summonable then return end
ns.msg_saved_pet_unsummonable(error_text, error_num)
else
if is_summonable then
if perchar then
ns.dbc.recentPets[1] = guid
else
ns.db.recentPets[1] = guid
end
return
end
end
elseif i == 1 then
return
end
end
ns.msg_previous_pet_unsummonable()
savedpet_is_summonable = false
end
--[[===========================================================================
Main actions
===========================================================================]]--
--[[ To be used only in func initialize_pool and is_excluded_by_id ]]
local function is_excluded_by_species(spec)
for _, e in ipairs(excluded_species) do
if e == spec then
if e ~= 3247 or ns.current_zone == 1970 then -- Pocopoc
return true
end
end
end
return false
end
--[[ To be used only in func new_pet and save_pet ]]
local function is_excluded_by_id(id)
local species_id = C_PetJournal_GetPetInfoByPetID(id)
return is_excluded_by_species(species_id)
end
--[[---------------------------------------------------------------------------
Auto action
The main function that runs when player started moving. It decides whether to
restore a lost pet, or summon a new one (if the timer is set and due).
---------------------------------------------------------------------------]]--
function ns.autoaction()
local now = time()
if stop_auto_summon(now) then return end
if ns.db.newPetTimer ~= 0 then
if ns.remaining_timer(now) == 0 then
ns.debugprint_pet '‹autoaction()› calls ‹new_pet()›'
ns:new_pet(now, false)
return
end
end
-- TODO: Could we not simply check against the saved db pet, and restore if it isn't correct or missing?
if not ns.pet_verified then
ns.debugprint_pet '‹autoaction()› calls ‹transitioncheck()›, pet not verified'
ns.transitioncheck(true)
return
end
local actpet = C_PetJournal_GetSummonedPetGUID()
if not actpet then
ns.debugprint_pet '‹autoaction()› calls ‹restore_pet()›'
ns:restore_pet()
end
end
--[[---------------------------------------------------------------------------
Restore pet
---------------------------------------------------------------------------]]--
function ns:restore_pet()
local now = time()
local savedpet
local db = ns.dbc.charFavsEnabled and ns.db.favsOnly and ns.dbc or ns.db
savedpet = db.recentPets[1]
time_restore_pet = now
if savedpet then
debugprint '‹restore_pet()› restored saved pet'
ns.set_sum_msg_to_restore_pet(savedpet)
ns.pet_restored = true
ns:summon_pet(savedpet, false)
else
debugprint '‹restore_pet()› COULD NOT FIND SAVED PET. Summoning new pet via Blizz ‹SummonRandomPet()›'
ns.msg_no_saved_pet()
-- Do not use `new_pet()` as fallback, since it isn't guaranteed that the user has a valid pool.
-- (e.g. set to char favs and no char favs defined)
-- `false` to summon any, `true` for favorites.
C_PetJournal.SummonRandomPet(false)
end
end
--[[---------------------------------------------------------------------------
New pet - timer is due, or via command/key
---------------------------------------------------------------------------]]--
function ns:new_pet(the_time, manually_called)
if manually_called and stop_manual_summon() then return end
debugprint('‹new_pet()› called; manually:', manually_called)
local now = the_time or time()
if now - ns.time_newpet_success < 1.5 then return end
local actpet = C_PetJournal_GetSummonedPetGUID()
-- TODO: if not manually_called, should we also prevent Argent Squire from being replaced?
-- See line 77; not really urgent, since the user simply can avoid moving, and additionally
-- the bank/mail of Argent Squire does not restart the CD when closed.
if actpet and is_excluded_by_id(actpet) then
debugprint '‹new_pet()›: ‹actpet› is excluded'
return
end
if not ns.pool_initialized then
debugprint '‹new_pet()› calls ‹initialize_pool()›'
ns.initialize_pool()
end
local pool = ns.pet_pool
or random() <= ns.db.favsProbability and ns.pet_pool_favs
or ns.pet_pool_other
or {}
local npool = #pool
local newpet
if npool == 0 then
if now - time_pool_msg > 90 then
ns.msg_low_petpool(npool)
time_pool_msg = now
end
-- We are intentionally no longer summoning a fallback pet here.
-- See the loop issue https://github.com/tflo/PetWalker/issues/20.
-- Also, it may just confuse the user if we show the "low pet pool" warning and in the same moment
-- summon a new (and unrelated) pet. The next auto-summon trigger will restore the saved pet anyway.
debugprint '‹new_pet()›: zero pet pool, warning msg on cooldown'
else
if npool == 1 then
newpet = pool[1]
if actpet == newpet then
if not msg_onlyfavisactive_alreadydisplayed or manually_called then
ns.msg_onlyfavisactive(actpet)
msg_onlyfavisactive_alreadydisplayed = true
end
return
end
else
repeat
newpet = pool[random(npool)]
until actpet ~= newpet
end
ns.set_sum_msg_to_newpet(newpet, pool, npool)
ns:summon_pet(newpet, true)
ns.pool = pool
end
end
--[[---------------------------------------------------------------------------
Summon previous
---------------------------------------------------------------------------]]--
function ns.previous_pet()
if stop_manual_summon() then return end
local db = ns.dbc.charFavsEnabled and ns.db.favsOnly and ns.dbc or ns.db
if #db.recentPets > 1 then
-- Rotate: move current (1) to end
local current = table.remove(db.recentPets, 1)
table.insert(db.recentPets, current)
ns.set_sum_msg_to_previouspet(db.recentPets[1])
ns:summon_pet(db.recentPets[1], true)
else
ns.msg_no_previous_pet()
end
end
--[[---------------------------------------------------------------------------
Summon targeted pet
---------------------------------------------------------------------------]]--
function ns.summon_targetpet()
if stop_manual_summon() then return end
if not UnitIsBattlePet 'target' then
ns.msg_target_is_not_battlepet()
return
end
local target_species_id = UnitBattlePetSpeciesID 'target'
local target_pet_name = C_PetJournal_GetPetInfoBySpeciesID(target_species_id) or ''
local _, tarpet = C_PetJournal_FindPetIDByName(target_pet_name)
local target_pet_link = tarpet and C_PetJournal_GetBattlePetLink(tarpet) or '[link: UNKNOWN]'
if not C_PetJournal_GetOwnedBattlePetString(target_species_id) then
if not UnitIsBattlePetCompanion 'target' then
ns.msg_target_is_not_companion_battlepet(target_pet_name)
else
ns.msg_target_not_in_collection(target_pet_link, target_pet_name)
end
return
end
local current_pet = C_PetJournal_GetSummonedPetGUID()
if not current_pet or C_PetJournal_GetPetInfoByPetID(current_pet) ~= target_species_id then
ns:summon_pet(tarpet, true)
ns.msg_target_summoned(target_pet_link)
else
ns.msg_target_is_same(target_pet_link) -- Without web link
-- ns.msg_target_is_same(target_pet_link, target_pet_name) -- With web link
end
end
--[[----------------------------------------------------------------------------
Transition check
One time action, after big transitions, like login, portals, entering instance, etc.
Basically a standalone restore_pet func; in addition, it not only checks for presence
of a pet, but also against the saved pet. This makes sure that a newly logged toon gets
the same pet as the previous toon had at logout. We need more checks here than in
restore_pet, as restore_pet is prefiltered by autoaction, and here we are not.
----------------------------------------------------------------------------]]--
function ns.transitioncheck(checks_done)
-- Can be called via the entering-world events, or via `autoaction`, so we
-- if ns.pet_verified or InCombatLockdown() or IsFlying() or UnitOnTaxi 'player' then
-- TODO: Observe if stop_auto_summon works as expected here!
-- Never run the stop_auto_summon check twice, as the 2nd one will always find a throttle then!
if not checks_done and stop_auto_summon() then
debugprint(
'‹transitioncheck()› stopped by ‹stop_auto_summon()›; ‹pet_verified›:',
ns.pet_verified
)
return
end
local now = time()
--[[ If toon starts moving immediately after transition, then `restore_pet`
might come before us. Also prevents redundant run in case we use both events
NEW_AREA and ENTERING_WORLD. ]]
if now - time_restore_pet < 6 then
debugprint('‹transitioncheck()› aborted; less than 6s since ‹restore_pet()›')
return
end
ns.current_zone = C_Map_GetBestMapForUnit 'player'
local savedpet
-- TODO: shouldn't we initialize here instead?!
ns:cfavs_update()
local actpet = C_PetJournal_GetSummonedPetGUID()
local db = ns.dbc.charFavsEnabled and ns.db.favsOnly and ns.dbc or ns.db
if not actpet or actpet ~= db.recentPets[1] then savedpet = db.recentPets[1] end
if ns.current_zone == 1970 then -- Pocopoc issue
if ns.id_to_species(savedpet) == 3247 or ns.id_to_species(actpet) == 3247 then
savedpet = db.recentPets[2]
end
end
if savedpet and savedpet_is_summonable then
debugprint '‹transitioncheck()› restores saved pet'
ns.set_sum_msg_to_transcheck(savedpet)
ns:summon_pet(savedpet, false)
--[[ Should only come into play if savedpet is still nil due to a slow
loading process ]]
elseif not actpet then
debugprint '‹transitioncheck()› could not find saved pet; summoning new pet'
ns.msg_no_saved_pet()
ns:new_pet(now, false)
end
-- TODO: Do we still need this timestamp?
time_restore_pet = now
--[[ This is not 100% reliable here, but should do the trick most of the time. ]]
ns.pet_verified, savedpet_is_summonable = true, true
-- Because we are unregistering now with every type of PLAYER_ENTERING_WORLD
-- HACK: Called separately after entering world, bc of the possible early return
-- ns.events:register_summon_events()
debugprint '‹transitioncheck()› completed'
end
--[[---------------------------------------------------------------------------
Save pet
Triggered by COMPANION_UPDATE
---------------------------------------------------------------------------]]--
function ns.save_pet()
-- Flag must be unset …
-- when a pet is force-summoned by the Pet Journal when we put in a team slot,
-- after entering world events.
if not ns.pet_verified then
debugprint '‹save_pet()› ABORTED, pet not verified'
return
end
local actpet = C_PetJournal_GetSummonedPetGUID()
if not actpet or is_excluded_by_id(actpet) then
debugprint '‹save_pet()› ABORTED, no ‹actpet› or ‹actpet› is excluded'
return
end
-- NewRecents
local db = ns.dbc.charFavsEnabled and ns.db.favsOnly and ns.dbc or ns.db
if db.recentPets[1] == actpet then return end
for i = #db.recentPets, 1, -1 do
if db.recentPets[i] == actpet then
table.remove(db.recentPets, i)
debugprint('‹save_pet()› deduped recent pets at idx:', i)
-- ns.msg_recents_dupe_removed(i)
end
end
table.insert(db.recentPets, 1, actpet)
while #db.recentPets > ns.db.numRecents do
table.remove(db.recentPets)
end
-- TOOD:
-- ns.time_newpet_success = now
-- debugprint 'Timer reset by save_pet()'
ns.debugprint_pet '‹save_pet()› completed'
end
--[[---------------------------------------------------------------------------
Summon Pet
Used in the Auto Summon function, and currently also in the
Manual Summon function
---------------------------------------------------------------------------]]--
function ns:summon_pet(pet, resettimer)
if not pet then -- TODO: needed?
debugprint '‹summon_pet()› called without ‹pet› arg!'
return
end
if ns.db.debugMode then
local is_summonable, error_num, error_text = C_PetJournal_GetPetSummonInfo(pet)
if not is_summonable then
debugprint(
'‹summon_pet()›: Something is wrong with our summonability check: pet cannot be summoned, ‹GetPetSummonInfo()› returned',
is_summonable,
error_num,
error_text
)
return
end
end
local now = time()
ns.pet_verified = true
if resettimer then
ns.time_newpet_success = now
-- TOOD:
-- debugprint 'Timer reset by summon_pet()'
end
ns.msg_pet_summon_success()
C_PetJournal_SummonPetByGUID(pet)
end
--[[===========================================================================
Pet pool
Creating the POOL, from where the random pet is summoned.
This can be, depending on user setting:
— Global favorites
— Per-character favorites
— All available pets (except the exclusions). Note that this is affected by the
filter settings in the Pet Journal, which means the player can create custom
pools without changing his favorites.
===========================================================================]]--
local function clean_charfavs()
local count, link = 0, nil
for id, _ in pairs(ns.dbc.charFavs) do
link = C_PetJournal_GetBattlePetLink(id)
if not link then
ns.dbc.charFavs[id] = nil
count = count + 1
end
end
if count > 0 then ns.msg_removed_invalid_id(count) end
end
local function can_add_to_pool(species_id, pet_id)
return not is_excluded_by_species(species_id) and is_pet_summonable(pet_id)
end
function ns.initialize_pool(rerun)
local pet_pool, pet_pool_favs, pet_pool_other = {}, {}, {}
debugprint('‹initialize_pool()› called; rerun:', rerun)
clean_charfavs()
local now = time()
local can_send_warning = now - time_pool_msg > 90 and ns.db.newPetTimer ~= 0
local idx = 1
if ns.db.favsOnly then
-- "Favs Only"
pet_pool_favs, pet_pool_other = nil, nil
while true do
local pet_id, species_id, _, _, _, favorite = C_PetJournal_GetPetInfoByIndex(idx)
if not pet_id then break end
if favorite and can_add_to_pool(species_id, pet_id) then
table.insert(pet_pool, pet_id)
end
idx = idx + 1
end
elseif ns.db.favsProbability == 1 then
-- "All equal", treat favs as regulars
pet_pool_favs, pet_pool_other = nil, nil
while true do
local pet_id, species_id = C_PetJournal_GetPetInfoByIndex(idx)
if not pet_id then break end
if can_add_to_pool(species_id, pet_id) then table.insert(pet_pool, pet_id) end
idx = idx + 1
end
elseif ns.db.favsProbability == 0 then
-- "Regulars Only"
pet_pool_favs, pet_pool_other = nil, nil
while true do
local pet_id, species_id, _, _, _, favorite = C_PetJournal_GetPetInfoByIndex(idx)
if not pet_id then break end
if not favorite and can_add_to_pool(species_id, pet_id) then
table.insert(pet_pool, pet_id)
end
idx = idx + 1
end
else
-- "Mixed", two pools
while true do
local pet_id, species_id, _, _, _, favorite = C_PetJournal_GetPetInfoByIndex(idx)
if not pet_id then break end
if can_add_to_pool(species_id, pet_id) then
if favorite then
table.insert(pet_pool_favs, pet_id)
else
table.insert(pet_pool_other, pet_id)
end
end
idx = idx + 1
end
if #pet_pool_favs < 1 or #pet_pool_other < 1 then
ns.db.favsProbability = 1
ns.msg_force_changed_pool()
pet_pool = #pet_pool_other > 0 and pet_pool_other or pet_pool_favs
pet_pool_favs, pet_pool_other = nil, nil
else
pet_pool = nil
end
end
if pet_pool and #pet_pool < 1 then
if rerun then
if ns.db.newPetTimer ~= 0 then
ns.db.newPetTimer = 0
ns.db.newPetTimer_reset_by_pw = true
end
if can_send_warning then
ns.msg_low_petpool(#pet_pool)
time_pool_msg = now
end
else
if ns.db.favsOnly ~= false then
ns.db.favsOnly = false
ns.db.favsOnly_reset_by_pw = true
end
if ns.db.favsProbability ~= 1 then
ns.db.favsProbability = 1
ns.db.favsProbability_reset_by_pw = true
end
ns.msg_force_changed_pool()
ns.initialize_pool(true)
end
debugprint '‹initialize_pool()› failed!'
return
end
ns.pool_initialized = true
ns.pet_pool_favs, ns.pet_pool_other, ns.pet_pool =
pet_pool_favs, pet_pool_other, pet_pool
debugprint '‹initialize_pool()› completed'
end
--[[===========================================================================
Char Favs
===========================================================================]]--
local C_PetJournal_PetIsFavorite1, C_PetJournal_SetFavorite1, C_PetJournal_GetPetInfoByIndex1
-- Largely unaltered code from NugMiniPet
function ns.cfavs_update()
debugprint '‹cfavs_update()› called'
if ns.dbc.charFavsEnabled then
C_PetJournal_PetIsFavorite1 = C_PetJournal_PetIsFavorite1 or C_PetJournal_PetIsFavorite
C_PetJournal_SetFavorite1 = C_PetJournal_SetFavorite1 or C_PetJournal_SetFavorite
C_PetJournal_GetPetInfoByIndex1 = C_PetJournal_GetPetInfoByIndex1 or C_PetJournal_GetPetInfoByIndex
C_PetJournal_PetIsFavorite = function(petGUID) return ns.dbc.charFavs[petGUID] or false end
C_PetJournal_SetFavorite = function(petGUID, new)
if new == 1 then
ns.dbc.charFavs[petGUID] = true
else
ns.dbc.charFavs[petGUID] = nil
end
if PetJournal then PetJournal_OnEvent(PetJournal, 'PET_JOURNAL_LIST_UPDATE') end
ns:PET_JOURNAL_LIST_UPDATE() -- Do not remove this
end
local gpi = C_PetJournal_GetPetInfoByIndex1
C_PetJournal_GetPetInfoByIndex = function(...)
local petGUID, speciesID, isOwned, customName, level, favorite, isRevoked, name, icon, petType, creatureID, sourceText, description, isWildPet, canBattle, arg1, arg2, arg3 = gpi(...)
local customFavorite = C_PetJournal_PetIsFavorite(petGUID)
return petGUID, speciesID, isOwned, customName, level, customFavorite, isRevoked, name, icon, petType, creatureID, sourceText, description, isWildPet, canBattle, arg1, arg2, arg3
end
else
if C_PetJournal_PetIsFavorite1 then C_PetJournal_PetIsFavorite = C_PetJournal_PetIsFavorite1 end
if C_PetJournal_SetFavorite1 then C_PetJournal_SetFavorite = C_PetJournal_SetFavorite1 end
if C_PetJournal_GetPetInfoByIndex1 then C_PetJournal_GetPetInfoByIndex = C_PetJournal_GetPetInfoByIndex1 end
end
_G.C_PetJournal.PetIsFavorite = C_PetJournal_PetIsFavorite
_G.C_PetJournal.SetFavorite = C_PetJournal_SetFavorite
_G.C_PetJournal.GetPetInfoByIndex = C_PetJournal_GetPetInfoByIndex
if PetJournal then PetJournal_OnEvent(PetJournal, 'PET_JOURNAL_LIST_UPDATE') end
ns:PET_JOURNAL_LIST_UPDATE() -- Do not remove this
debugprint '‹cfavs_update()› completed'
end
--[[===========================================================================
GUI stuff for Pet Journal
===========================================================================]]--
--[[
We disabled most of the GUI elements, since now we have more settings than we
can fit there. We leave the CharFavorites checkbox, because it makes sense to
see at a glance (in the opened Pet Journal) what type of favs are enabled.
]]
function ns:create_cfavs_checkbox()
local btn =
CreateFrame('CheckButton', 'PetWalkerCharFavsCheckbox', PetJournal, 'UICheckButtonTemplate')
btn:SetSize(26, 26)
btn:SetHitRectInsets(-2, -80, -2, -2)
btn:SetPoint('BOTTOMLEFT', PetJournal, 'BOTTOMLEFT', 400, 1)
-- btn:SetChecked(ns.dbc.charFavsEnabled)
btn:SetScript('OnEnter', function(self)
GameTooltip:SetOwner(self, 'ANCHOR_TOPLEFT')
GameTooltip:AddLine('\124cnDIM_GREEN_FONT_COLOR:PetWalker')
GameTooltip:AddLine(
'Select this to use per-character favorites, or toggle with \124cnORANGE_FONT_COLOR:/pw c\124r.'
)
GameTooltip:AddLine(
'Toggle favs/all: \124cnORANGE_FONT_COLOR:/pw f\124r | Status: \124cnORANGE_FONT_COLOR:/pw s\124r | Help & all commands: \124cnORANGE_FONT_COLOR:/pw h\124r'
)
GameTooltip:Show()
end)
btn:SetScript('OnClick', function()
ns.dbc.charFavsEnabled = not ns.dbc.charFavsEnabled
if ns.db.autoEnabled then
ns.transitioncheck()
else
ns:cfavs_update()
end
end)
btn:SetScript('OnLeave', function() GameTooltip:Hide() end)
local label = btn:CreateFontString(nil, 'OVERLAY')
label:SetFontObject(GameFontNormal)
label:SetPoint('LEFT', btn, 'RIGHT', 0, 0)
label:SetText 'Char Favs (PW)'
return btn, label
end