forked from goobsnake/Pet-Health
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPetHealth.lua
More file actions
1231 lines (1092 loc) · 43.2 KB
/
PetHealth.lua
File metadata and controls
1231 lines (1092 loc) · 43.2 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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--Elder Scrolls: Online addon (written in LUA) which adds persistent in-game health bars to all permanent pets.
--Original/base work of this addon was developed by SCOOTWORKS and I was granted permission by him to take over full development and distribution of this addon.
PetHealth = PetHealth or {}
--The supported classes for this addon (ClassId from function GetUnitClassId("player"))
PetHealth.supportedClasses = {
[2] = true, -- Sorcerer
[4] = true, -- Warden
}
local addon = {
name = "PetHealth",
displayName = "PetHealth",
version = "1.12",
savedVarName = "PetHealth_Save",
savedVarVersion = 2,
lamDisplayName = "PetHealth",
lamAuthor = "Scootworks, Goobsnake",
lamUrl = "https://www.esoui.com/downloads/info1884-PetHealth.html",
}
PetHealth.addonData = addon
local default = {
saveMode = 1, -- Default for each character setting
point = TOPLEFT,
relPoint = CENTER,
x = 0,
y = 0,
onlyInCombat = false,
showValues = true,
showLabels = true,
hideInDungeon = false,
lockWindow = false,
lowHealthAlertSlider = 0,
lowShieldAlertSlider = 0,
petUnsummonedAlerts = false,
onlyInCombatHealthSlider = 0,
showBackground = true,
useZosStyle = false,
debug = false,
showCompanion = true,
}
local UNIT_PLAYER_PET = "playerpet"
local UNIT_COMPANION = "companion"
local UNIT_PLAYER_TAG = "player"
local base, background, savedVars--, savedVarCopy
local currentPets = {}
local PetHealthWarner
local window = {}
local inCombatAddon = false
local AddOnManager = GetAddOnManager()
local hideInDungeon = false
local LAM
local LSC
local lockWindow = false
local lowHealthAlertPercentage = 0
local lowShieldAlertPercentage = 0
local onlyInCombatHealthMax = 0
local onlyInCombatHealthCurrent = 0
local onlyInCombatHealthPercentage = 0
local onScreenHealthAlertPetOne = 0
local onScreenHealthAlertPetTwo = 0
local onScreenHealthAlertPetThree = 0
local onScreenShieldAlertPetOne = 0
local onScreenShieldAlertPetTwo = 0
local onScreenShieldAlertPetThree = 0
local unsummonedAlerts = false
local WINDOW_MANAGER = GetWindowManager()
local WINDOW_WIDTH = 250
local WINDOW_HEIGHT_ONE = 76
local WINDOW_HEIGHT_TWO = 116
local WINDOW_HEIGHT_THREE = 156
local PET_BAR_FRAGMENT
----------
-- UTIL --
----------
local function OnScreenMessage(message)
local messageParams = CENTER_SCREEN_ANNOUNCE:CreateMessageParams(CSA_CATEGORY_LARGE_TEXT)
messageParams:SetCSAType(CENTER_SCREEN_ANNOUNCE_TYPE_COUNTDOWN)
messageParams:SetText(message)
CENTER_SCREEN_ANNOUNCE:AddMessageWithParams(messageParams)
end
local function ChatOutput(message)
CHAT_SYSTEM:AddMessage(message)
end
local function CheckAddon(addonName)
for i = 1, AddOnManager:GetNumAddOns() do
local name, title, author, description, enabled, state, isOutOfDate = AddOnManager:GetAddOnInfo(i)
if title == addonName and enabled == true and state == 2 then
return true
end
end
end
local function GetPetNameLower(abilityId)
--[[
Um die Namen einfacher zu vergleichen, nur Kleinbuchstaben nutzen.
Zudem formatiert zo_strformat() den Namen ins richtige Format.
]]
local abilityName = GetAbilityName(abilityId)
local petName
--Removing text from Sorc pet ability names to derive pet names / Not currently needed for Warden pets
--Unstable Clannfear is abilityId 23319
--if abilityId == 23319 or abilityId == 23304 then
if abilityName:match('Summon ') then
if abilityName:match('Summon Unstable ') then
petName = abilityName:gsub("Summon Unstable ","")
else
petName = abilityName:gsub("Summon ","")
end
else
petName = abilityName
end
return zo_strformat("<<z:1>>", petName)
end
local validPets = {
--[[
Da einige abilityNames nicht mit abilityId übereinstimmt,
müssen wir hier ein paar Sachen hardcoden.
]]
-- Familiar
[GetPetNameLower(23304)] = true,
["begleiter"] = true, -- de
["familier"] = true, -- fr
["призванный слуга"] = true, -- ru
-- Clannfear
[GetPetNameLower(23319)] = true,
["clannbann"] = true, -- de
["faucheclan"] = true, -- fr
["кланфир"] = true, -- ru
-- Volatile Familiar
[GetPetNameLower(23316)] = true, -- en
["explosiver begleiter"] = true, -- de
["familier explosif"] = true, -- fr
["взрывной призванный слуга"] = true, -- ru
-- Winged Twilight
[GetPetNameLower(24613)] = true, -- en
["zwielichtschwinge"] = true, -- de
["crépuscule ailé"] = true, -- fr
["крылатый сумрак"] = true, -- ru
-- Twilight Tormentor
[GetPetNameLower(24636)] = true, -- en
["zwielichtpeinigerin"] = true, -- de
["tourmenteur crépusculaire"] = true, -- fr
["сумрак-мучитель"] = true, -- ru
-- Twilight Matriarch
[GetPetNameLower(24639)] = true,
["zwielichtmatriarchin"] = true, -- de
["matriarche crépusculaire"] = true, -- fr
["сумрак-матриарх"] = true, -- ru
-- Warden Pets don't seem to need any de/fr localization entries
-- Feral Guardian
[GetPetNameLower(85982)] = true,
["хищный страж"] = true, -- ru
-- Eternal Guardian
[GetPetNameLower(85986)] = true,
["вечный страж"] = true, -- ru
-- Wild Guardian
[GetPetNameLower(85990)] = true,
["дикий защитник"] = true, -- ru
}
local function IsUnitValidPet(unitTag)
--[[
Hier durchsuchen wir die Tabellen oben, ob wir den unitTag wirklich in unsere Tabelle aufnehmen.
]]
local unitName = zo_strformat("<<z:1>>", GetUnitName(unitTag))
--zo_callLater(function() ChatOutput(unitName) end, 10000)
return DoesUnitExist(unitTag) and validPets[unitName]
end
local function GetKeyWithData(unitTag)
for k, v in pairs(currentPets) do
if v.unitTag == unitTag then return k end
end
return nil
end
local function GetAlphaFromControl(savedVariable)
return (not savedVariable and 0) or 1
end
local function GetCombatState()
return not inCombatAddon and savedVars.onlyInCombat
end
local function SetPetWindowHidden(hidden, combatState)
local setToHidden = hidden
if combatState then
setToHidden = true
end
PET_BAR_FRAGMENT:SetHiddenForReason("NoPetOrOnlyInCombat", setToHidden)
-- debug
--ChatOutput(string.format("SetPetWindowHidden() setToHidden: %s, onlyInCombat: %s", tostring(setToHidden), tostring(onlyInCombat)))
end
local function PetUnSummonedAlerts(unitTag)
if unsummonedAlerts then
local i = GetKeyWithData(unitTag)
if i == nil then
return
end
local petName = currentPets[i].unitName
local swimming = IsUnitSwimming(UNIT_PLAYER_TAG)
local inCombat = IsUnitInCombat(UNIT_PLAYER_TAG)
if swimming then
OnScreenMessage(string.format(GetString(SI_PET_HEALTH_UNSUMMONED_SWIMMING_MSG)))
elseif inCombat then
OnScreenMessage(zo_strformat("<<1>> <<2>>", petName, GetString(SI_PET_HEALTH_UNSUMMONED_MSG)))
end
end
end
local function RefreshPetWindow()
--GetSlotBoundId values 3 thru 8 to obtain the slotbar's abiltiyId's (8 is ultimate)
-- d(GetSlotBoundId(3))
-- d(GetPetNameLower(GetSlotBoundId(3)))
-- d(GetAbilityName(23319))
local countPets = #currentPets
local combatState = GetCombatState()
if PET_BAR_FRAGMENT:IsHidden() and countPets == 0 and combatState then
return
end
local height = 0
local setToHidden = true
if countPets > 0 then
for i=1, #window do
if i > countPets then
window[i]:SetHidden(true)
else
window[i]:SetHidden(false)
end
end
if countPets == 1 then
height = WINDOW_HEIGHT_ONE
elseif countPets == 2 then
height = WINDOW_HEIGHT_TWO
elseif countPets == 3 then
height = WINDOW_HEIGHT_THREE
end
setToHidden = false
end
if not combatState and savedVars.onlyInCombat == true then
if onlyInCombatHealthPercentage == 0 then
setToHidden = false
elseif onlyInCombatHealthCurrent > (onlyInCombatHealthMax*.01*onlyInCombatHealthPercentage) then
setToHidden = true
end
end
if savedVars.hideInDungeon == true then
local inDungeon = IsUnitInDungeon(UNIT_PLAYER_TAG)
local zoneDifficulty = GetCurrentZoneDungeonDifficulty()
--zoneDifficulty 0 is for all overland/non-dungeon content, 1 = normal dungeon/arena/trial, 2 = veteran dungeon/arena/trial
if inDungeon == true and zoneDifficulty > 0 then
local currentZone = GetUnitZone(UNIT_PLAYER_TAG)
if currentZone ~= 'Maelstrom Arena' then
setToHidden = true
end
end
end
base:SetHeight(height)
background:SetHeight(height)
-- set hidden state
SetPetWindowHidden(setToHidden, combatState)
-- debug
--ChatOutput(string.format("RefreshPetWindow() countPets: %d", countPets))
end
------------
-- SHIELD --
------------
local function OnShieldUpdate(handler, unitTag, value, maxValue, initial)
local i = GetKeyWithData(unitTag)
if i == nil or i > #window then
return
end
if i == 1 then
local petOne = currentPets[1].unitName
if lowShieldAlertPercentage > 1 and value ~= 0 and value < (maxValue*.01*lowShieldAlertPercentage) then
if onScreenShieldAlertPetOne == 0 then
OnScreenMessage(zo_strformat("|c000099<<1>>\'s <<2>>|r", petOne, GetString(SI_PET_HEALTH_LOW_SHIELD_WARNING_MSG)))
onScreenShieldAlertPetOne = 1
end
else
onScreenShieldAlertPetOne = 0
end
elseif i == 2 then
local name = currentPets[i].unitName
local petOne = currentPets[1].unitName
local petTwo = currentPets[2].unitName
if lowShieldAlertPercentage > 1 and value ~= 0 and value < (maxValue*.01*lowShieldAlertPercentage) then
if name == petOne and onScreenShieldAlertPetOne == 0 then
OnScreenMessage(zo_strformat("|c000099<<1>>\'s <<2>>|r", petOne, GetString(SI_PET_HEALTH_LOW_SHIELD_WARNING_MSG)))
onScreenShieldAlertPetOne = 1
elseif name == petTwo and onScreenShieldAlertPetTwo == 0 then
OnScreenMessage(zo_strformat("|c000099<<1>>\'s <<2>>|r", petTwo, GetString(SI_PET_HEALTH_LOW_SHIELD_WARNING_MSG)))
onScreenShieldAlertPetTwo = 1
end
else
if name == petOne then
onScreenShieldAlertPetOne = 0
elseif name == petTwo then
onScreenShieldAlertPetTwo = 0
end
end
elseif i == 3 then
local name = currentPets[i].unitName
local petOne = currentPets[1].unitName
local petTwo = currentPets[2].unitName
local petThree = currentPets[3].unitName
if lowShieldAlertPercentage > 1 and value ~= 0 and value < (maxValue*.01*lowShieldAlertPercentage) then
if name == petOne and onScreenShieldAlertPetOne == 0 then
OnScreenMessage(zo_strformat("|c000099<<1>>\'s <<2>>|r", petOne, GetString(SI_PET_HEALTH_LOW_SHIELD_WARNING_MSG)))
onScreenShieldAlertPetOne = 1
elseif name == petTwo and onScreenShieldAlertPetTwo == 0 then
OnScreenMessage(zo_strformat("|c000099<<1>>\'s <<2>>|r", petTwo, GetString(SI_PET_HEALTH_LOW_SHIELD_WARNING_MSG)))
onScreenShieldAlertPetTwo = 1
elseif name == petThree and onScreenShieldAlertPetThree == 0 then
OnScreenMessage(zo_strformat("|c000099<<1>>\'s <<2>>|r", petThree, GetString(SI_PET_HEALTH_LOW_SHIELD_WARNING_MSG)))
onScreenShieldAlertPetThree = 1
end
else
if name == petOne then
onScreenShieldAlertPetOne = 0
elseif name == petTwo then
onScreenShieldAlertPetTwo = 0
elseif name == petThree then
onScreenShieldAlertPetThree = 0
end
end
end
local ctrl, ctrlr;
if (not savedVars.useZosStyle) then
ctrl = window[i].shield
else
ctrl = window[i].shieldleft
ctrlr = window[i].shieldright
end
if handler ~= nil then
if not ctrl:IsHidden() or value == 0 then
ctrl:SetHidden(true)
if (savedVars.useZosStyle) then
ctrlr:SetHidden(true)
end
end
else
if ctrl:IsHidden() then
ctrl:SetHidden(false)
if (savedVars.useZosStyle) then
ctrlr:SetHidden(false)
end
end
end
if maxValue > 0 then
if (savedVars.useZosStyle) then
value = value / 2;
maxValue = maxValue / 2;
ZO_StatusBar_SmoothTransition(window[i].shieldleft, value, maxValue, (initial == "true" and true or false))
ZO_StatusBar_SmoothTransition(window[i].shieldright, value, maxValue, (initial == "true" and true or false))
else
ZO_StatusBar_SmoothTransition(window[i].shield, value, maxValue, (initial == "true" and true or false))
end
end
end
local function GetShield(unitTag)
local value, maxValue = GetUnitAttributeVisualizerEffectInfo(unitTag, ATTRIBUTE_VISUAL_POWER_SHIELDING, STAT_MITIGATION, ATTRIBUTE_HEALTH, POWERTYPE_HEALTH)
if value == nil then
value = 0
maxValue = 0
end
OnShieldUpdate(_, unitTag, value, maxValue, "true")
end
------------
-- HEALTH --
------------
local function OnHealthUpdate(_, unitTag, powerIndex, powerType, powerValue, powerMax, powerEffectiveMax)
local i = GetKeyWithData(unitTag)
if i == nil or i > #window then
return
end
if powerIndex == 1 then
return
end
--d(unitTag .. ", powerIndex: " .. powerIndex .. ", powerType: " .. powerType .. ", powerValue: " .. powerValue .. ", powerMax: " .. powerMax .. ", powerEffectiveMax: " .. powerEffectiveMax)
if onlyInCombatHealthPercentage > 1 and savedVars.onlyInCombat == true then
onlyInCombatHealthMax = powerMax
onlyInCombatHealthCurrent = powerValue
RefreshPetWindow()
end
if i == 1 then
local petOne = currentPets[1].unitName
if lowHealthAlertPercentage > 1 and powerValue ~= 0 and powerValue < (powerMax*.01*lowHealthAlertPercentage) then
if onScreenHealthAlertPetOne == 0 then
OnScreenMessage(zo_strformat("|cff0000<<1>> <<2>>|r", petOne, GetString(SI_PET_HEALTH_LOW_HEALTH_WARNING_MSG)))
onScreenHealthAlertPetOne = 1
end
else
onScreenHealthAlertPetOne = 0
end
elseif i == 2 then
local name = currentPets[i].unitName
local petOne = currentPets[1].unitName
local petTwo = currentPets[2].unitName
if lowHealthAlertPercentage > 1 and powerValue ~= 0 and powerValue < (powerMax*.01*lowHealthAlertPercentage) then
if name == petOne and onScreenHealthAlertPetOne == 0 then
OnScreenMessage(zo_strformat("|cff0000<<1>> <<2>>|r", petOne, GetString(SI_PET_HEALTH_LOW_HEALTH_WARNING_MSG)))
onScreenHealthAlertPetOne = 1
elseif name == petTwo and onScreenHealthAlertPetTwo == 0 then
OnScreenMessage(zo_strformat("|cff0000<<1>> <<2>>|r", petTwo, GetString(SI_PET_HEALTH_LOW_HEALTH_WARNING_MSG)))
onScreenHealthAlertPetTwo = 1
end
else
if name == petOne then
onScreenHealthAlertPetOne = 0
elseif name == petTwo then
onScreenHealthAlertPetTwo = 0
end
end
elseif i == 3 then
local name = currentPets[i].unitName
local petOne = currentPets[1].unitName
local petTwo = currentPets[2].unitName
local petThree = currentPets[3].unitName
if lowHealthAlertPercentage > 1 and powerValue ~= 0 and powerValue < (powerMax*.01*lowHealthAlertPercentage) then
if name == petOne and onScreenHealthAlertPetOne == 0 then
OnScreenMessage(zo_strformat("|cff0000<<1>> <<2>>|r", petOne, GetString(SI_PET_HEALTH_LOW_HEALTH_WARNING_MSG)))
onScreenHealthAlertPetOne = 1
elseif name == petTwo and onScreenHealthAlertPetTwo == 0 then
OnScreenMessage(zo_strformat("|cff0000<<1>> <<2>>|r", petTwo, GetString(SI_PET_HEALTH_LOW_HEALTH_WARNING_MSG)))
onScreenHealthAlertPetTwo = 1
elseif name == petThree and onScreenHealthAlertPetThree == 0 then
OnScreenMessage(zo_strformat("|cff0000<<1>> <<2>>|r", petThree, GetString(SI_PET_HEALTH_LOW_HEALTH_WARNING_MSG)))
onScreenHealthAlertPetThree = 1
end
else
if name == petOne then
onScreenHealthAlertPetOne = 0
elseif name == petTwo then
onScreenHealthAlertPetTwo = 0
elseif name == petThree then
onScreenHealthAlertPetThree = 0
end
end
end
-- health values
window[i].values:SetText(ZO_FormatResourceBarCurrentAndMax(powerValue, powerMax))
-- health bar
if (savedVars.useZosStyle) then
local halfValue = powerValue / 2
local halfMax = powerMax / 2
ZO_StatusBar_SmoothTransition(window[i].barleft, halfValue, halfMax, (powerEffectiveMax == "true" and true or false))
ZO_StatusBar_SmoothTransition(window[i].barright, halfValue, halfMax, (powerEffectiveMax == "true" and true or false))
window[i].warner:OnHealthUpdate(powerValue, powerMax);
else
ZO_StatusBar_SmoothTransition(window[i].healthbar, powerValue, powerMax, (powerEffectiveMax == "true" and true or false))
end
end
local function GetHealth(unitTag)
local powerValue, powerMax = GetUnitPower(unitTag, POWERTYPE_HEALTH)
OnHealthUpdate(_, unitTag, _, _, powerValue, powerMax, "true")
end
-----------
-- STATS --
-----------
local function GetControlText(control)
local controlText = control:GetText()
if controlText ~= nil then return controlText end
return ""
end
local function UpdatePetStats(unitTag)
local i = GetKeyWithData(unitTag)
if i == nil or i > #window then
return
end
local name = currentPets[i].unitName
local control = window[i].label
if GetControlText(control) ~= name then
window[i].label:SetText(name)
end
GetHealth(unitTag)
GetShield(unitTag)
-- debug
--ChatOutput(string.format("UpdatePetStats() unitTag: %s, name: %s", unitTag, name))
end
local function GetActivePets()
--[[
Hier werden alle Begleiter des Spielers ausgelesen und in die Begleitertabelle geschrieben.
]]
currentPets = {}
for i=1,7 do
local unitTag = UNIT_PLAYER_PET..i
if IsUnitValidPet(unitTag) then
table.insert(currentPets, { unitTag = unitTag, unitName = GetUnitName(unitTag) })
UpdatePetStats(unitTag)
end
end
if savedVars.showCompanion and HasActiveCompanion() and DoesUnitExist(UNIT_COMPANION) then
table.insert(currentPets, { unitTag = UNIT_COMPANION, unitName = zo_strformat("<<1>>", GetCompanionName(GetActiveCompanionDefId())) })
UpdatePetStats(UNIT_COMPANION)
end
-- update
zo_callLater(function() RefreshPetWindow() end, 300)
end
-----------
-- COMBAT --
-----------
local function OnPlayerCombatState(_, inCombat)
--[[
Setzt den Kampfstatus: in Kampf oder ausserhalb Kampf.
]]
inCombatAddon = inCombat
-- debug
--ChatOutput(string.format("OnPlayerCombatState() inCombat: %s, inCombatAddon: %s", tostring(inCombat), tostring(inCombatAddon)))
-- refresh
RefreshPetWindow()
end
local function CreateWarner()
if savedVars.useZosStyle then
local HEALTH_ALPHA_PULSE_THRESHOLD = 0.25
local RESOURCE_WARNER_FLASH_TIME = 300
PetHealthWarner = ZO_Object:Subclass()
function PetHealthWarner:New(...)
local warner = ZO_Object.New(self)
warner:Initialize(...)
return warner
end
function PetHealthWarner:Initialize(parent)
self.warning = GetControl(parent, "Warner")
self.OnPowerUpdate = function(_, unitTag, powerIndex, powerType, health, maxHealth)
self:OnHealthUpdate(health, maxHealth)
end
local function OnPlayerActivated()
local current, max = GetUnitPower(self.unitTag, POWERTYPE_HEALTH)
self:OnHealthUpdate(current, max)
end
self.warning:RegisterForEvent(EVENT_PLAYER_ACTIVATED, OnPlayerActivated)
self.warnAnimation = ZO_AlphaAnimation:New(self.warning)
self.statusBar = parent
self.paused = false
end
function PetHealthWarner:SetPaused(paused)
if self.paused ~= paused then
self.paused = paused
if paused then
if self.warnAnimation:IsPlaying() then
self.warnAnimation:Stop()
end
else
local current, max = GetUnitPower(UNIT_PLAYER_TAG, POWERTYPE_HEALTH)
self.warning:SetAlpha(0)
self:UpdateAlphaPulse(current / max)
end
end
end
function PetHealthWarner:UpdateAlphaPulse(healthPerc)
if healthPerc <= HEALTH_ALPHA_PULSE_THRESHOLD then
if not self.warnAnimation:IsPlaying() then
self.warnAnimation:PingPong(0, 1, RESOURCE_WARNER_FLASH_TIME)
end
else
if self.warnAnimation:IsPlaying() then
self.warnAnimation:Stop()
self.warning:SetAlpha(0)
end
end
end
function PetHealthWarner:OnHealthUpdate(health, maxHealth)
if not self.paused then
local healthPerc = health / maxHealth
self:UpdateAlphaPulse(healthPerc)
end
end
end
end
--------------
-- CONTROLS --
--------------
local function CreateControls()
-----------------
-- ADD CONTROL --
-----------------
local function AddControl(parent, cType, level)
local c = WINDOW_MANAGER:CreateControl(nil, parent, cType)
c:SetDrawLayer(DL_OVERLAY)
c:SetDrawLevel(level)
return c, c
end
---------------
-- TOP LAYER --
---------------
base = WINDOW_MANAGER:CreateTopLevelWindow(addon.name.."_TopLevel")
base:SetDimensions(WINDOW_WIDTH, WINDOW_HEIGHT_TWO)
base:SetAnchor(savedVars.point, GuiRoot, savedVars.relPoint, savedVars.x, savedVars.y)
base:SetMouseEnabled(true)
if savedVars.lockWindow == true then
base:SetMovable(false)
else
base:SetMovable(true)
end
base:SetDrawLayer(DL_OVERLAY)
base:SetDrawLevel(0)
base:SetHandler("OnMouseUp", function()
local a, b
a, savedVars.point, b, savedVars.relPoint, savedVars.x, savedVars.y = base:GetAnchor(0)
end)
base:SetHidden(true)
----------------
-- BACKGROUND --
----------------
local INSET_BACKGROUND = 32
local baseWidth = base:GetWidth()
local baseHeight = base:GetHeight()
local ctrl
background, ctrl = AddControl(base, CT_BACKDROP, 1)
ctrl:SetEdgeTexture("esoui/art/chatwindow/chat_bg_edge.dds", 256, 128, INSET_BACKGROUND)
ctrl:SetCenterTexture("esoui/art/chatwindow/chat_bg_center.dds")
ctrl:SetInsets(INSET_BACKGROUND, INSET_BACKGROUND, -INSET_BACKGROUND, -INSET_BACKGROUND)
ctrl:SetCenterColor(1,1,1,0.8)
ctrl:SetEdgeColor(1,1,1,0.8)
ctrl:SetDimensions(baseWidth, baseHeight)
ctrl:SetAnchor(TOPLEFT)
ctrl:SetAlpha(GetAlphaFromControl(savedVars.showBackground))
--------------
-- PET BARS --
--------------
if (not savedVars.useZosStyle) then
for i=1,3 do
-- frame
window[i], ctrl = AddControl(base, CT_BACKDROP, 5)
ctrl:SetDimensions(baseWidth*0.8, 36)
ctrl:SetCenterColor(1,0,1,0)
ctrl:SetEdgeColor(1,0,1,0)
ctrl:SetAnchor(CENTER, base)
-- label
local windowHeight = window[i]:GetHeight()
window[i].label, ctrl = AddControl(window[i], CT_LABEL, 10)
ctrl:SetFont("$(BOLD_FONT)|$(KB_16)|soft-shadow-thin")
ctrl:SetColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_TEXT_COLORS, INTERFACE_TEXT_COLOR_NORMAL))
ctrl:SetDimensions(baseWidth, windowHeight*0.4)
ctrl:SetAnchor(TOPLEFT, window[i])
ctrl:SetAlpha(GetAlphaFromControl(savedVars.showLabels))
-- border and background
window[i].border, ctrl = AddControl(window[i], CT_BACKDROP, 20)
ctrl:SetDimensions(window[i]:GetWidth(), windowHeight*0.45)
ctrl:SetCenterColor(0,0,0,.6)
ctrl:SetEdgeColor(1,1,1,0.4)
ctrl:SetEdgeTexture("", 1, 1, 1)
ctrl:SetAnchor(BOTTOM, window[i])
-- healthbar
local borderWidth = window[i].border:GetWidth()
local borderHeight = window[i].border:GetHeight()
window[i].healthbar, ctrl = AddControl(window[i].border, CT_STATUSBAR, 30)
ctrl:SetColor(1,1,1,0.5)
ctrl:SetGradientColors(.45, .13, .13, 1, .85, .19, .19, 1)
ctrl:SetDimensions(borderWidth-2, borderHeight-2)
ctrl:SetAnchor(CENTER, window[i].border)
-- shield
window[i].shield, ctrl = AddControl(window[i].healthbar, CT_STATUSBAR, 40)
ctrl:SetColor(1,1,1,0.5)
ctrl:SetGradientColors(.5, .5, 1, .3, .25, .25, .5, .5)
ctrl:SetDimensions(borderWidth-2, borderHeight-2)
ctrl:SetAnchor(CENTER, window[i].healthbar)
ctrl:SetValue(0)
ctrl:SetMinMax(0,1)
-- values
window[i].values, ctrl = AddControl(window[i].healthbar, CT_LABEL, 50)
ctrl:SetFont("$(BOLD_FONT)|$(KB_14)|soft-shadow-thin")
ctrl:SetColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_TEXT_COLORS, INTERFACE_TEXT_COLOR_SELECTED))
ctrl:SetAnchor(CENTER, window[i].healthbar)
ctrl:SetAlpha(GetAlphaFromControl(savedVars.showValues))
-- ctrl:SetHidden(not savedVars.showValues or false)
-- clear anchors to reset it
window[i]:ClearAnchors()
end
window[1]:SetAnchor(TOP, base, TOP, 0, 18)
window[2]:SetAnchor(TOP, window[1], BOTTOM, 0, 2)
window[3]:SetAnchor(TOP, window[2], BOTTOM, 0, 2)
else
local CHILD_DIRECTIONS = { "Left", "Right", "Center" }
local function SetColors(self)
local powerType = self.powerType
local gradient = ZO_POWER_BAR_GRADIENT_COLORS[powerType]
for i, control in ipairs(self.barControls) do
ZO_StatusBar_SetGradientColor(control, gradient)
control:SetFadeOutLossColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_POWER_FADE_OUT, powerType))
control:SetFadeOutGainColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_POWER_FADE_IN, powerType))
end
end
local PAB_TEMPLATES = {
[POWERTYPE_HEALTH] = {
background = {
Left = "ZO_PlayerAttributeBgLeftArrow",
Right = "ZO_PlayerAttributeBgRightArrow",
Center = "ZO_PlayerAttributeBgCenter",
},
frame = {
Left = "ZO_PlayerAttributeFrameLeftArrow",
Right = "ZO_PlayerAttributeFrameRightArrow",
Center = "ZO_PlayerAttributeFrameCenter",
},
warner = {
texture = "ZO_PlayerAttributeHealthWarnerTexture",
Left = "ZO_PlayerAttributeWarnerLeftArrow",
Right = "ZO_PlayerAttributeWarnerRightArrow",
Center = "ZO_PlayerAttributeWarnerCenter",
},
anchors = {
"ZO_PlayerAttributeHealthBarAnchorLeft",
"ZO_PlayerAttributeHealthBarAnchorRight",
},
},
statusBar = "ZO_PlayerAttributeStatusBar",
statusBarGloss = "ZO_PlayerAttributeStatusBarGloss",
resourceNumbersLabel = "ZO_PlayerAttributeResourceNumbers",
}
local function ApplyStyle(bar)
local powerTypeTemplates = PAB_TEMPLATES[bar.powerType]
local backgroundTemplates = powerTypeTemplates.background
local frameTemplates = powerTypeTemplates.frame
local warnerControl = bar:GetNamedChild("Warner")
local bgControl = bar:GetNamedChild("BgContainer")
local warnerTemplates = powerTypeTemplates.warner
for _, direction in pairs(CHILD_DIRECTIONS) do
local bgChild = bgControl:GetNamedChild("Bg" .. direction)
ApplyTemplateToControl(bgChild, ZO_GetPlatformTemplate(backgroundTemplates[direction]))
local frameControl = bar:GetNamedChild("Frame" .. direction)
ApplyTemplateToControl(frameControl, ZO_GetPlatformTemplate(frameTemplates[direction]))
local warnerChild = warnerControl:GetNamedChild(direction)
ApplyTemplateToControl(warnerChild, ZO_GetPlatformTemplate(warnerTemplates.texture))
ApplyTemplateToControl(warnerChild, ZO_GetPlatformTemplate(warnerTemplates[direction]))
end
for i, subBar in pairs(bar.barControls) do
ApplyTemplateToControl(subBar, ZO_GetPlatformTemplate(PAB_TEMPLATES.statusBar))
local gloss = subBar:GetNamedChild("Gloss")
ApplyTemplateToControl(gloss, ZO_GetPlatformTemplate(PAB_TEMPLATES.statusBarGloss))
local anchorTemplates = powerTypeTemplates.anchors
if anchorTemplates then
subBar:ClearAnchors()
ApplyTemplateToControl(subBar, ZO_GetPlatformTemplate(anchorTemplates[i]))
else
ApplyTemplateToControl(subBar, ZO_GetPlatformTemplate(PAB_TEMPLATES.anchor))
end
end
local resourceNumbersLabel = bar:GetNamedChild("ResourceNumbers")
if resourceNumbersLabel then
ApplyTemplateToControl(resourceNumbersLabel, ZO_GetPlatformTemplate(PAB_TEMPLATES.resourceNumbersLabel))
end
end
for i=1,3 do
window[i] = WINDOW_MANAGER:CreateControlFromVirtual("PetHealth"..i, base, "PetHealth_ZOSStyleBar")
-- label
local windowHeight = window[i]:GetHeight()
window[i].label, ctrl = AddControl(window[i], CT_LABEL, 10)
ctrl:SetFont("$(BOLD_FONT)|$(KB_16)|soft-shadow-thin")
ctrl:SetColor(GetInterfaceColor(INTERFACE_COLOR_TYPE_TEXT_COLORS, INTERFACE_TEXT_COLOR_NORMAL))
ctrl:SetDimensions(baseWidth, windowHeight*0.4)
ctrl:SetAnchor(BOTTOMLEFT, window[i], TOPLEFT, 0, -10.5)
ctrl:SetAlpha(GetAlphaFromControl(savedVars.showLabels))
-- bars
window[i].barleft = window[i]:GetNamedChild("BarLeft")
window[i].barright = window[i]:GetNamedChild("BarRight")
window[i].barControls = { window[i].barleft, window[i].barright }
window[i].powerType = POWERTYPE_HEALTH
SetColors(window[i])
ApplyStyle(window[i])
-- shield
window[i].shieldleft = window[i]:GetNamedChild("ShieldLeft")
window[i].shieldright = window[i]:GetNamedChild("ShieldRight")
-- values
window[i].values = window[i]:GetNamedChild("ResourceNumbers")
window[i].values:SetAlpha(GetAlphaFromControl(savedVars.showValues))
-- ctrl:SetHidden(not savedVars.showValues or false)
window[i].warner = PetHealthWarner:New(window[i]);
end
window[1]:SetAnchor(TOP, base, TOP, 0, 18)
window[2]:SetAnchor(TOP, window[1], BOTTOM, 0, 20)
window[3]:SetAnchor(TOP, window[2], BOTTOM, 0, 20)
end
-----------
-- SCENE --
-----------
PET_BAR_FRAGMENT = ZO_HUDFadeSceneFragment:New(base)
HUD_SCENE:AddFragment(PET_BAR_FRAGMENT)
HUD_UI_SCENE:AddFragment(PET_BAR_FRAGMENT)
PET_BAR_FRAGMENT:SetHiddenForReason("NoPetOrOnlyInCombat", true)
end
local function OnUnitDestroyed(eventCode, unitTag)
PetUnSummonedAlerts(unitTag)
local key = GetKeyWithData(unitTag)
if key ~= nil then
table.remove(currentPets, key)
-- debug
--ChatOutput(string.format("%s destroyed", unitTag))
-- refresh
local countPets = #currentPets
if countPets > 0 then
for i = 1, countPets do
local name = currentPets[i].unitName
local control = window[i].label
unitTag = currentPets[i].unitTag
if GetControlText(control) ~= name then
window[i].label:SetText(name)
end
GetHealth(unitTag)
GetShield(unitTag)
end
end
RefreshPetWindow()
end
end
local function OnUnitCreated(eventCode, unitTag)
if IsUnitValidPet(unitTag) or unitTag == UNIT_COMPANION then
GetActivePets()
end
end
local INACTIVE_COMPANION_STATES =
{
[COMPANION_STATE_INACTIVE] = true,
[COMPANION_STATE_BLOCKED_PERMANENT] = true,
[COMPANION_STATE_BLOCKED_TEMPORARY] = true,
[COMPANION_STATE_HIDDEN] = true,
[COMPANION_STATE_INITIALIZING] = true,
}
local PENDING_COMPANION_STATES =
{
[COMPANION_STATE_PENDING] = true,
[COMPANION_STATE_INITIALIZED_PENDING] = true,
}
local ACTIVE_COMPANION_STATES =
{
[COMPANION_STATE_ACTIVE] = true,
}
local function OnCompanionStateChanged(eventCode, newState, oldState)
if savedVars.showCompanion ~= true then
return
end
if INACTIVE_COMPANION_STATES[newState] then
OnUnitDestroyed(eventCode, UNIT_COMPANION)
elseif PENDING_COMPANION_STATES[newState] then
-- Could display pending...
elseif ACTIVE_COMPANION_STATES[newState] then
OnUnitCreated(eventCode, UNIT_COMPANION)
else
--internalassert(false, "Unhandled companion state")
end
end
----------
-- INIT --
----------
local function LoadEvents()
-- events
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_ACTIVE_COMPANION_STATE_CHANGED, OnCompanionStateChanged)
--pet
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_POWER_UPDATE, OnHealthUpdate)
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_UNIT_CREATED, OnUnitCreated)
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_UNIT_DESTROYED, OnUnitDestroyed)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_POWER_UPDATE, REGISTER_FILTER_UNIT_TAG_PREFIX, UNIT_PLAYER_PET)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_UNIT_CREATED, REGISTER_FILTER_UNIT_TAG_PREFIX, UNIT_PLAYER_PET)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_UNIT_DESTROYED, REGISTER_FILTER_UNIT_TAG_PREFIX, UNIT_PLAYER_PET)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_UNIT_ATTRIBUTE_VISUAL_ADDED, REGISTER_FILTER_UNIT_TAG, UNIT_PLAYER_PET)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_UNIT_ATTRIBUTE_VISUAL_REMOVED, REGISTER_FILTER_UNIT_TAG, UNIT_PLAYER_PET)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_UNIT_ATTRIBUTE_VISUAL_UPDATED, REGISTER_FILTER_UNIT_TAG, UNIT_PLAYER_PET)
--companion
EVENT_MANAGER:RegisterForEvent(addon.name .. UNIT_COMPANION, EVENT_POWER_UPDATE, OnHealthUpdate)
EVENT_MANAGER:RegisterForEvent(addon.name .. UNIT_COMPANION, EVENT_UNIT_CREATED, OnUnitCreated)
EVENT_MANAGER:RegisterForEvent(addon.name .. UNIT_COMPANION, EVENT_UNIT_DESTROYED, OnUnitDestroyed)
EVENT_MANAGER:AddFilterForEvent(addon.name .. UNIT_COMPANION, EVENT_POWER_UPDATE, REGISTER_FILTER_UNIT_TAG_PREFIX, UNIT_COMPANION)
EVENT_MANAGER:AddFilterForEvent(addon.name .. UNIT_COMPANION, EVENT_UNIT_CREATED, REGISTER_FILTER_UNIT_TAG_PREFIX, UNIT_COMPANION)
EVENT_MANAGER:AddFilterForEvent(addon.name .. UNIT_COMPANION, EVENT_UNIT_DESTROYED, REGISTER_FILTER_UNIT_TAG_PREFIX, UNIT_COMPANION)
EVENT_MANAGER:AddFilterForEvent(addon.name .. UNIT_COMPANION, EVENT_UNIT_ATTRIBUTE_VISUAL_ADDED, REGISTER_FILTER_UNIT_TAG, UNIT_COMPANION)
EVENT_MANAGER:AddFilterForEvent(addon.name .. UNIT_COMPANION, EVENT_UNIT_ATTRIBUTE_VISUAL_REMOVED, REGISTER_FILTER_UNIT_TAG, UNIT_COMPANION)
EVENT_MANAGER:AddFilterForEvent(addon.name .. UNIT_COMPANION, EVENT_UNIT_ATTRIBUTE_VISUAL_UPDATED, REGISTER_FILTER_UNIT_TAG, UNIT_COMPANION)
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_PLAYER_DEAD, GetActivePets)
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_UNIT_DEATH_STATE_CHANGE, GetActivePets)
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_ACTION_SLOT_ABILITY_SLOTTED, GetActivePets)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_PLAYER_DEAD, REGISTER_FILTER_UNIT_TAG, UNIT_PLAYER_TAG)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_UNIT_DEATH_STATE_CHANGE, REGISTER_FILTER_UNIT_TAG, UNIT_PLAYER_TAG)
-- shield
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_UNIT_ATTRIBUTE_VISUAL_ADDED, function(_, unitTag, unitAttributeVisual, _, _, _, value, maxValue)
if unitAttributeVisual == ATTRIBUTE_VISUAL_POWER_SHIELDING then
OnShieldUpdate(nil, unitTag, value, maxValue, "true")
end
end)
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_UNIT_ATTRIBUTE_VISUAL_REMOVED, function(_, unitTag, unitAttributeVisual, _, _, _, value, maxValue)
if unitAttributeVisual == ATTRIBUTE_VISUAL_POWER_SHIELDING then
OnShieldUpdate("removed", unitTag, value, maxValue, "false")
end
end)
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_UNIT_ATTRIBUTE_VISUAL_UPDATED, function(_, unitTag, unitAttributeVisual, _, _, _, _, newValue, _, newMaxValue)
if unitAttributeVisual == ATTRIBUTE_VISUAL_POWER_SHIELDING then
OnShieldUpdate(nil, unitTag, newValue, newMaxValue, "false")
end
end)
-- for changes the style of the values
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_INTERFACE_SETTING_CHANGED, GetActivePets)
EVENT_MANAGER:AddFilterForEvent(addon.name, EVENT_INTERFACE_SETTING_CHANGED, REGISTER_FILTER_SETTING_SYSTEM_TYPE, SETTING_TYPE_UI)
-- handles the in combat stuff
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_PLAYER_COMBAT_STATE, OnPlayerCombatState)
OnPlayerCombatState(_, IsUnitInCombat(UNIT_PLAYER_TAG))
-- zone changes
EVENT_MANAGER:RegisterForEvent(addon.name, EVENT_PLAYER_ACTIVATED, function() zo_callLater(function() GetActivePets() end, 75) end)
end
function PetHealth.changeCombatState()
OnPlayerCombatState(_, IsUnitInCombat(UNIT_PLAYER_TAG))