This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCombatPlates.lua
executable file
·992 lines (808 loc) · 28.2 KB
/
CombatPlates.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
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
-----------------------------------------------------------------------------------------------
-- Client Lua Script for CombatPlates
-- Copyright (c) NCsoft. All rights reserved
-----------------------------------------------------------------------------------------------
require "Window"
local CombatPlates = {}
CombatPlates.CodeEnumUnitState = {
Ok = 1,
Low = 2,
Critical = 3,
Vulnerable = 4,
Dead = 5
}
-- a simplified version of showing / hiding certain parts of the nameplates - just three "versions" available
-- Standard is what you see on enemies, Info is what you see on friendly NPCs that may be useful,
-- Hidden is for "trash" friendly NPCs and will show up only on non full health and when targeted
CombatPlates.CodeEnumMode = {
Standard = 1,
Info = 2,
Hidden = 3
}
local nAddonVersion = 10
local knNameplatePoolLimit = 500 -- the window pool max size
local tDefaultSettings = {
bDebug = false,
nAddonVersion = nAddonVersion,
nDrawDistance = 85,
nHealthLowFraction = 0.55,
nHealthCriticalFraction = 0.25,
tColors = {
tDisposition = {
[Unit.CodeEnumDisposition.Friendly] = "ff21c36f",
[Unit.CodeEnumDisposition.Neutral] = "ffffff14",
[Unit.CodeEnumDisposition.Hostile] = "ffe50000",
[Unit.CodeEnumDisposition.Unknown] = "ff0165fc",
},
tDispositionPlayerAlternate = {
["t" .. Unit.CodeEnumDisposition.Friendly] = "ff40fd14",
["f" .. Unit.CodeEnumDisposition.Hostile] = "fff97306"
},
tUnitState = {
[CombatPlates.CodeEnumUnitState.Ok] = "ff15b01a",
[CombatPlates.CodeEnumUnitState.Low] = "ffc1f80a",
[CombatPlates.CodeEnumUnitState.Critical] = "ffe50000",
[CombatPlates.CodeEnumUnitState.Vulnerable] = "ff720058",
[CombatPlates.CodeEnumUnitState.Dead] = "ff000000"
},
tUnitCastingInfo = {
[true] = "ff720058",
[false] = "222a6c83"
}
},
tClassNames = {
[GameLib.CodeEnumClass.Engineer] = "Eng",
[GameLib.CodeEnumClass.Esper] = "Esp",
[GameLib.CodeEnumClass.Medic] = "Md",
[GameLib.CodeEnumClass.Spellslinger] = "SS",
[GameLib.CodeEnumClass.Stalker] = "Stk",
[GameLib.CodeEnumClass.Warrior] = "War"
},
tIconsLimit = 4,
tIconSize = 14,
tIconWindowTop = 11
}
function CombatPlates:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.tUnitsBacklog = {}
o.tResourcePool = {}
o.tWindowLookup = {}
return o
end
function CombatPlates:Init()
Apollo.RegisterAddon(self)
end
--- load ---
function CombatPlates:OnLoad()
self.tSettings = tDefaultSettings
self:ConvertColorsToObjects()
self.tNameplates = {}
self.uPlayerUnit = nil
self.uPlayerWindow = nil
self.nTargetId = nil
self.uXml = XmlDoc.CreateFromFile("CombatPlates.xml")
-- basic events
Apollo.RegisterEventHandler("UnitCreated", "OnUnitCreated", self)
Apollo.RegisterEventHandler("UnitDestroyed", "OnUnitDestroyed", self)
Apollo.RegisterEventHandler("ChangeWorld", "OnChangeWorld", self)
Apollo.RegisterEventHandler("TargetUnitChanged", "OnTargetUnitChanged", self)
-- this is a timer ~= 0.1 sec (at least on my computer)
Apollo.RegisterEventHandler("VarChange_FrameCount", "UpdateAllNameplates", self)
local tRewardUpdateEvents = {
"QuestObjectiveUpdated", "QuestStateChanged", "ChallengeAbandon", "ChallengeLeftArea",
"ChallengeFailTime", "ChallengeFailArea", "ChallengeActivate", "ChallengeCompleted",
"ChallengeFailGeneric", "PublicEventObjectiveUpdate", "PublicEventUnitUpdate",
"PlayerPathMissionUpdate", "FriendshipAdd", "FriendshipRemoved", "FriendshipUpdate"
}
for nIndex, strEventName in pairs(tRewardUpdateEvents) do
Apollo.RegisterEventHandler(strEventName, "UpdateAllNameplates", self)
end
-- icon update events
Apollo.RegisterEventHandler("UnitActivationTypeChanged", "OnUnitActivationTypeChanged", self)
--[[ In Theory covered by above RewardUpdateEvents, need to verify
Apollo.RegisterEventHandler("QuestInit", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("PublicEventStart", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("PublicEventEnd", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("ChallengeUnlocked", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("PlayerPathMissionUnlocked", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("PlayerPathMissionUpdate", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("PlayerPathMissionComplete", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("PlayerPathMissionDeactivate", "UpdateAllNameplates", self)
Apollo.RegisterEventHandler("PlayerPathMissionActivate", "UpdateAllNameplates", self)
--]]
end
--- main nameplate manipulation ---
function CombatPlates:AddNameplate(uUnit)
local nUnitId = uUnit:GetId()
if self.tNameplates[nUnitId] ~= nil then
self:debug(nUnitId .. " - nameplate already added")
return
end
local uNameplate = nil
local wndReferences = nil
if next(self.tResourcePool) ~= nil then
local poolEntry = table.remove(self.tResourcePool)
uNameplate = poolEntry[1]
wndReferences = poolEntry[2]
end
if uNameplate == nil or not uNameplate:IsValid() then
uNameplate = Apollo.LoadForm(self.uXml, "Nameplate", "InWorldHudStratum", self)
wndReferences = nil
end
uNameplate:Show(false, true)
uNameplate:SetUnit(uUnit, 1)
wndReferences = wndReferences or {
BuffsLine = uNameplate:FindChild("BuffsLine"),
BuffsMinus = uNameplate:FindChild("BuffsMinus"),
LifeLine = uNameplate:FindChild("LifeLine"),
LifeBars = uNameplate:FindChild("LifeBars"),
Sprint = uNameplate:FindChild("Sprint"),
Health = uNameplate:FindChild("Health"),
LifeText = uNameplate:FindChild("LifeText"),
Shield = uNameplate:FindChild("Shield"),
Absorption = uNameplate:FindChild("Absorption"),
Name = uNameplate:FindChild("Name"),
State = uNameplate:FindChild("State"),
StateBack = uNameplate:FindChild("StateBack"),
Guild = uNameplate:FindChild("Guild"),
IconsLine = uNameplate:FindChild("IconsLine"),
FlickerProtector = uNameplate:FindChild("FlickerProtector"),
}
wndReferences.Health:Show(true, true)
wndReferences.BuffsMinus:SetUnit(uUnit)
local uLifeBars = wndReferences.LifeBars
self:MovePixieLineToFractionHorizontal(uLifeBars, 1, self.tSettings.nHealthCriticalFraction)
self:MovePixieLineToFractionHorizontal(uLifeBars, 2, self.tSettings.nHealthLowFraction)
local bIsMe = (nUnitId == self.uPlayerUnit:GetId())
if bIsMe then
wndReferences.Sprint:Show(true)
local nLeft, nTop, nRight, nBottom = wndReferences.BuffsLine:GetAnchorOffsets()
wndReferences.BuffsLine:SetAnchorOffsets(nLeft, nTop + 1, nRight, nBottom + 1)
else
wndReferences.Sprint:Show(false) -- Can we destroy? Possible could be recycled for us later.
end
self.tNameplates[nUnitId] = {
window = uNameplate,
unit = uUnit,
isVisible = nil,
lifeWidth = uLifeBars:GetWidth(),
lifeHash = "",
disposition = "",
isMyTarget = false,
isMe = bIsMe,
unitState = nil,
ccArmor = "",
sprint = nil,
icons = "",
isImportant = nil,
isWounded = nil,
unitTypeString = "",
bIsOver = true,
measurement = nil,
plateMode = self.CodeEnumMode.Standard,
refs = wndReferences,
}
self:SetVisible(self.tNameplates[nUnitId], false)
self.tWindowLookup[uNameplate:GetId()] = self.tNameplates[nUnitId]
self:UpdateWholeNameplate(nUnitId)
end
-- because I have to use a timer anyway (not enough events), updating some information on existing events makes no sense,
-- it's actually more efficient to do that on a timer tick
function CombatPlates:UpdateAllNameplates()
if self.uPlayerUnit == nil then
self.uPlayerUnit = GameLib.GetPlayerUnit()
if self.uPlayerUnit ~= nil then
self:AddUnitsFromBacklog()
self.uPlayerWindow = self.tNameplates[self.uPlayerUnit:GetId()].window
self:OnTargetUnitChanged(GameLib.GetTargetUnit())
end
return
end
for nUnitId, tData in pairs(self.tNameplates) do
self:UpdateNameplateEssentials(nUnitId)
end
end
function CombatPlates:UpdateAllNameplateIcons()
for nUnitId, tData in pairs(self.tNameplates) do
self:UpdateIcons(nUnitId)
end
end
function CombatPlates:RemoveNameplate(nUnitId)
if self.tNameplates[nUnitId] == nil then
self:debug(nUnitId .. " - nameplate already removed")
return
end
local tNameplate = self.tNameplates[nUnitId]
local wndNameplate = tNameplate.window
self.tWindowLookup[wndNameplate:GetId()] = nil
if #self.tResourcePool < knNameplatePoolLimit then
wndNameplate:Show(false, true)
wndNameplate:SetUnit(nil)
table.insert(self.tResourcePool, {wndNameplate, tNameplate.refs})
else
wndNameplate:Destroy()
end
self.tNameplates[nUnitId] = nil
end
--- main update methods ---
function CombatPlates:UpdateNameplateEssentials(nUnitId)
local tData = self.tNameplates[nUnitId]
local bShouldBeVisible = self:ShouldNameplateBeVisible(nUnitId)
if tData.isVisible ~= bShouldBeVisible then
self:SetVisible(tData, bShouldBeVisible)
end
if not tData.isVisible then
return
end
local x, y = tData.window:GetPos()
if y < 0 and tData.bIsOver then
tData.measurement = Apollo.LoadForm(self.uXml, "Measurement", "InWorldHudStratum", self)
tData.measurement:SetUnit(tData.unit, 1)
tData.window:SetUnit(tData.unit, 0)
tData.bIsOver = false
elseif tData.measurement then
local x, y = tData.measurement:GetPos()
if y - 10 > 0 then
tData.measurement:Destroy()
tData.measurement = nil
tData.window:SetUnit(tData.unit, 1)
tData.bIsOver = true
end
end
self:UpdateLife(nUnitId)
self:UpdateUnitState(nUnitId)
self:UpdateCCArmor(nUnitId)
self:UpdateDisposition(nUnitId)
self:UpdatePlateMode(nUnitId)
if tData.isMe then
self:UpdateSprint(nUnitId)
end
end
function CombatPlates:UpdateTargetStatus(nUnitId, bIsTarget)
local tData = self.tNameplates[nUnitId]
if tData == nil then
return
end
tData.isMyTarget = bIsTarget
self:UpdateUnitTypeString(nUnitId)
self:UpdateName(nUnitId)
end
-- can be used in emergency situations
function CombatPlates:UpdateWholeNameplate(nUnitId)
self:UpdateUnitTypeString(nUnitId)
self:UpdateNameplateEssentials(nUnitId)
self:UpdateName(nUnitId)
self:UpdateIcons(nUnitId)
end
--- nameplate partial updates - life ---
function CombatPlates:UpdateLife(nUnitId)
local tData = self.tNameplates[nUnitId]
local tLife = {
tData.unit:GetHealth(), -- 1
tData.unit:GetMaxHealth(), -- 2
tData.unit:GetShieldCapacity(), -- 3
tData.unit:GetShieldCapacityMax(), -- 4
tData.unit:GetAbsorptionValue(), -- 5
-- tData.unit:GetAbsorptionMax() -- 6, Does anyone care about how much you used to have?
}
local sLifeHash = "n/a"
if tLife[2] ~= nil and tLife[2] > 0 then
sLifeHash = table.concat(tLife, "-")
end
if tData.lifeHash == sLifeHash then
return
end
if sLifeHash == "n/a" then
tData.refs.Health:Show(false)
tData.refs.LifeText:SetText("")
tData.isWounded = false
else
local nMaxLife = tLife[2] + tLife[4] + tLife[5]
local nCurrentLife = tLife[1] + tLife[3] + tLife[5]
tData.isWounded = (nMaxLife ~= nCurrentLife)
if tData.lifeHash == "n/a" then
tData.refs.Health:Show(true)
end
local nAvailableLife, nAvailableWidth = self:SetLifeBarWidth(tData.refs.Absorption, tLife[2] + tLife[5], tData.lifeWidth, tLife[5], tLife[5])
self:SetShieldWidth(tData.refs.Shield, tLife[2], nAvailableWidth, tLife[4], tLife[3], 0)
self:SetLifeBarWidth(tData.refs.Health, nAvailableLife, nAvailableWidth, tLife[2], tLife[1])
tData.refs.LifeText:SetText(self:RenderShortNumber(tLife[1] + tLife[5]))
end
self.tNameplates[nUnitId].lifeHash = sLifeHash
end
function CombatPlates:SetShieldWidth(uWindow, nTotalNumber, nTotalPixels, nValueNumber, nCurrentNumber, nPixelsAdjustment)
if nCurrentNumber == 0 then
uWindow:SetAnchorOffsets(0, 0, 0, 0)
return
end
local nValuePixels = math.ceil(nValueNumber / nTotalNumber * nTotalPixels)
local nCurrentPixels = math.ceil(nCurrentNumber / nValueNumber * nValuePixels)
if nCurrentPixels > nTotalPixels then
nCurrentPixels = nTotalPixels
end
uWindow:SetAnchorOffsets(-(nCurrentPixels + nPixelsAdjustment), 0, -nPixelsAdjustment, 5)
end
function CombatPlates:SetLifeBarWidth(uWindow, nTotalNumber, nTotalPixels, nValueNumber, nCurrentNumber)
if nCurrentNumber == 0 then
uWindow:SetAnchorOffsets(0, 0, 0, 0)
return nTotalNumber, nTotalPixels
end
local nValuePixels = math.ceil(nValueNumber / nTotalNumber * nTotalPixels)
local nCurrentPixels = math.ceil(nCurrentNumber / nValueNumber * nValuePixels)
uWindow:SetAnchorOffsets(0, 0, nCurrentPixels, 0)
return nTotalNumber - nCurrentNumber, nTotalPixels - nCurrentPixels
end
function CombatPlates:UpdateDisposition(nUnitId)
local tData = self.tNameplates[nUnitId]
local nDisposition = tData.unit:GetDispositionTo(self.uPlayerUnit)
local sDispHash = ""
local sUnitType = tData.unit:GetType()
if sUnitType == "Pet" then
sDispHash = "p" .. nDisposition
elseif sUnitType ~= "Player" then
sDispHash = "x" .. nDisposition
elseif tData.unit:IsPvpFlagged() then
sDispHash = "t" .. nDisposition
else
sDispHash = "f" .. nDisposition
end
if tData.disposition == sDispHash then
return
end
local uColor = self.tSettings.tColors.tDispositionPlayerAlternate[sDispHash]
if uColor == nil then
uColor = self.tSettings.tColors.tDisposition[nDisposition]
end
tData.refs.Health:SetBGColor(uColor)
tData.disposition = sDispHash
end
--- nameplate partial updates - unit state ---
function CombatPlates:UpdateUnitState(nUnitId)
local tData = self.tNameplates[nUnitId]
local nVulnerabilityTime = tData.unit:GetCCStateTimeRemaining(Unit.CodeEnumCCState.Vulnerability)
local nUnitState = self.CodeEnumUnitState.Ok
local uStateWindow = tData.refs.State
local nUnitMaxHealth = tData.unit:GetMaxHealth()
local nLifeFraction = 1
if nUnitMaxHealth ~= nil and nUnitMaxHealth > 0 then
nLifeFraction = tData.unit:GetHealth() / tData.unit:GetMaxHealth()
end
if nVulnerabilityTime ~= nil and nVulnerabilityTime > 0 then
nUnitState = self.CodeEnumUnitState.Vulnerable
uStateWindow:SetText(string.format("%.1f", nVulnerabilityTime))
elseif tData.unit:IsDead() then
nUnitState = self.CodeEnumUnitState.Dead
if tData.unitState ~= nUnitState then
uStateWindow:SetText("xD")
end
elseif nLifeFraction <= self.tSettings.nHealthCriticalFraction then
nUnitState = self.CodeEnumUnitState.Critical
elseif nLifeFraction <= self.tSettings.nHealthLowFraction then
nUnitState = self.CodeEnumUnitState.Low
end
if tData.unitState == nUnitState then
return
end
if tData.unitState == self.CodeEnumUnitState.Dead then
-- raised from the dead
uStateWindow:SetText("")
end
uStateWindow:SetBGColor(self.tSettings.tColors.tUnitState[nUnitState])
tData.unitState = nUnitState
end
function CombatPlates:UpdateCCArmor(nUnitId)
local tData = self.tNameplates[nUnitId]
if tData.unitState == self.CodeEnumUnitState.Vulnerable then
if tData.ccArmor ~= "v" then
tData.ccArmor = "v"
end
return
end
local nCcArmorMax = nil
local nCcArmorCurrent = nil
if tData.isMe then
-- dash instead of interrupt armor
nCcArmorMax = math.floor(tData.unit:GetMaxResource(7) / 100)
nCcArmorCurrent = math.floor(tData.unit:GetResource(7) / 10) / 10
else
nCcArmorMax = tData.unit:GetInterruptArmorMax()
nCcArmorCurrent = tData.unit:GetInterruptArmorValue()
end
local bIsCasting = tData.unit:IsCasting()
local sCcArmorHash = ""
if bIsCasting and nCcArmorCurrent == 0 then
sCcArmorHash = "c/" .. nCcArmorMax
else
sCcArmorHash = nCcArmorCurrent .. "/" .. nCcArmorMax
end
if tData.ccArmor == sCcArmorHash then
return
end
if nCcArmorMax == -1 then
tData.refs.State:SetText("inf")
elseif bIsCasting and nCcArmorCurrent == 0 then
tData.refs.State:SetText("!!")
elseif nCcArmorMax ~= 0 then
tData.refs.State:SetText(nCcArmorCurrent)
else
tData.refs.State:SetText("")
end
tData.refs.StateBack:SetBGColor(self.tSettings.tColors.tUnitCastingInfo[bIsCasting])
tData.ccArmor = sCcArmorHash
end
--- nameplate partial updates - name ---
function CombatPlates:UpdateName(nUnitId)
local tData = self.tNameplates[nUnitId]
local bFullName = tData.isMyTarget
local sLevel = tData.unit:GetLevel() or "n/a"
local sType = tData.unitTypeString
local sName = ""
if bFullName then
sName = tData.unit:GetTitleOrName()
else
sName = tData.unit:GetName()
end
if sLevel ~= "n/a" then
if sType ~= "" then
sType = " " .. sType
end
tData.refs.Name:SetText(string.format("%s%s: %s", sLevel, sType, sName))
else
if sType ~= "" then
sType = sType .. ": "
end
tData.refs.Name:SetText(string.format("%s%s", sType, sName))
end
local uGuildWindow = tData.refs.Guild
local sGuild = tData.unit:GetGuildName()
local sAffiliation = tData.unit:GetAffiliationName()
if bFullName then
if sGuild ~= nil and sGuild ~= "" then
uGuildWindow:SetTextRaw("<" .. sGuild .. ">")
elseif sAffiliation ~= nil and sAffiliation ~= "" then
uGuildWindow:SetTextRaw(sAffiliation)
else
uGuildWindow:SetTextRaw("<-->")
end
uGuildWindow:Show(true)
else
uGuildWindow:SetText("")
uGuildWindow:Show(false)
end
end
function CombatPlates:UpdateUnitTypeString(nUnitId)
local tData = self.tNameplates[nUnitId]
local uUnit = tData.unit
local sUnitType = uUnit:GetType()
if sUnitType == "Player" then
tData.isImportant = true
tData.unitTypeString = self.tSettings.tClassNames[uUnit:GetClassId()]
return
end
if sUnitType == "Pet" then
tData.isImportant = true
tData.unitTypeString = sUnitType
return
end
local tUnitTypes = {}
local nEliteness = uUnit:GetEliteness()
local nRank = uUnit:GetRank()
local tActivationState = uUnit:GetActivationState()
local bIsBoss = false
-- rank and eliteness check
if nRank ~= Unit.CodeEnumRank.Elite and nRank ~= Unit.CodeEnumRank.Superior then
-- nothing special here
elseif nEliteness == Unit.CodeEnumEliteness.LargeRaid then
if nRank == Unit.CodeEnumRank.Elite then
-- I'm guessing that's a standard elite unit, like city guard etc, not a boss
table.insert(tUnitTypes, "Elite")
else
-- Other weird situations
table.insert(tUnitTypes, "Semi-Elite")
end
elseif nEliteness == Unit.CodeEnumEliteness.SmallRaid then
-- Perhaps that's an open world raid boss?
table.insert(tUnitTypes, "Raid Boss")
bIsBoss = true
elseif nEliteness == Unit.CodeEnumEliteness.Group then
-- those may or may not be bosses in instances and small open world ones
if nRank == Unit.CodeEnumRank.Elite then
table.insert(tUnitTypes, "Boss")
else
table.insert(tUnitTypes, "Mini-Boss")
end
bIsBoss = true
end
-- activation states check
local bQuestNameAdded = false
local bHasActivationState = false
for sActivationName, tData in pairs(tActivationState) do
if sActivationName:find("Quest") then
if not bQuestNameAdded then
table.insert(tUnitTypes, "Quest")
bQuestNameAdded = true
end
else
table.insert(tUnitTypes, sActivationName)
end
bHasActivationState = true
end
-- unit importance
tData.isImportant = (bIsBoss or bHasActivationState)
tData.unitTypeString = table.concat(tUnitTypes, ", ")
end
--- nameplate partial updates - icons, target marks, etc. ---
function CombatPlates:UpdateIcons(nUnitId)
local tData = self.tNameplates[nUnitId]
local tRewards = tData.unit:GetRewardInfo()
local tAllIcons = {[""] = false, Quest = false, Challenge = false, PublicEvent = false, Soldier = false, Settler = false, Explorer = false, Scientist = false, ScientistSpell = false}
local tIcons = {}
local nIconsCount = 0
if tRewards then
for i, tRewardInfo in pairs(tRewards) do
tAllIcons[self:GetIconTypeForReward(tRewardInfo)] = true
end
for sName, bEnabled in pairs(tAllIcons) do
if bEnabled and sName ~= "" then
table.insert(tIcons, sName)
nIconsCount = nIconsCount + 1
end
end
end
local sIconsHash = table.concat(tIcons, "/")
if sIconsHash == tData.icons then
return
end
local uIconsWindow = tData.refs.IconsLine
uIconsWindow:DestroyChildren()
tData.icons = sIconsHash
if nIconsCount == 0 then
return
end
if nIconsCount <= self.tSettings.tIconsLimit then
local nLeft, nTop, nRight, nBottom = uIconsWindow:GetAnchorOffsets()
uIconsWindow:SetAnchorOffsets(nLeft, self.tSettings.tIconWindowTop + math.floor(self.tSettings.tIconSize / 2 * (self.tSettings.tIconsLimit - nIconsCount)), nRight, nBottom)
end
for i, sName in pairs(tIcons) do
Apollo.LoadForm(self.uXml, "IconsShowcase." .. sName, uIconsWindow, self)
end
uIconsWindow:ArrangeChildrenVert()
end
function CombatPlates:GetIconTypeForReward(tRewardInfo)
local sIconType = tRewardInfo.strType
if sIconType == "Scientist" and tRewardInfo.splReward then
return "ScientistSpell"
end
if sIconType == "Challenge" then
-- that is a really terrible code copied from the original Nameplates, I shouldn't have to check that!
local tAllChallenges = ChallengesLib.GetActiveChallengeList()
for i, uChallengeData in pairs(tAllChallenges) do
if tRewardInfo.idChallenge == uChallengeData:GetId() and uChallengeData:IsActivated() and not uChallengeData:IsInCooldown() and not uChallengeData:ShouldCollectReward() then
return sIconType
end
end
return ""
end
return sIconType
end
--- nameplate partial updates - self specifics ---
function CombatPlates:UpdateSprint(nUnitId)
local tData = self.tNameplates[nUnitId]
local nSprintMax = tData.unit:GetMaxResource(0)
local nSprintCurrent = tData.unit:GetResource(0)
local sSprintHash = nSprintCurrent .. "/" .. nSprintMax
if tData.sprint == sSprintHash then
return
end
local uSprintWindow = tData.refs.Sprint
uSprintWindow:SetMax(nSprintMax)
uSprintWindow:SetProgress(nSprintCurrent)
tData.sprint = sSprintHash
end
--- conditions checks ---
function CombatPlates:ShouldNameplateBeVisible(nUnitId)
local tData = self.tNameplates[nUnitId]
local bCheckOccl = true
if self.uPlayerWindow ~= nil then
if self.uPlayerWindow:IsOccluded() then
bCheckOccl = false
end
end
return tData.window:IsOnScreen()
and ((not bCheckOccl) or (not tData.window:IsOccluded()) or tData.unit:IsMounted())
and tData.plateMode ~= self.CodeEnumMode.Hidden
and (self:GetDistanceTo(tData.unit) <= self.tSettings.nDrawDistance or tData.isMyTarget)
end
--- nameplate partial updates - plate mode ---
function CombatPlates:UpdatePlateMode(nUnitId)
local tData = self.tNameplates[nUnitId]
local nPlateMode = self.CodeEnumMode.Standard
if not tData.isWounded and not tData.isMe and not tData.isMyTarget and tData.disposition == "x2" then
if tData.isImportant then
nPlateMode = self.CodeEnumMode.Info
else
nPlateMode = self.CodeEnumMode.Hidden
end
end
if tData.plateMode == nPlateMode then
return
end
if tData.plateMode == self.CodeEnumMode.Hidden and self:ShouldNameplateBeVisible(nUnitId) then
self:SetVisible(tData, true)
elseif nPlateMode == self.CodeEnumMode.Hidden and tData.isVisible then
self:SetVisible(tData, false)
end
if nPlateMode ~= self.CodeEnumMode.Hidden then
local bShowLines = (nPlateMode == self.CodeEnumMode.Standard)
tData.refs.LifeLine:Show(bShowLines)
tData.refs.BuffsLine:Show(bShowLines)
tData.refs.IconsLine:Show(bShowLines)
end
tData.plateMode = nPlateMode
end
function CombatPlates:SetVisible(tData, bIsVisible)
tData.isVisible = bIsVisible
tData.window:Show(bIsVisible)
tData.refs.FlickerProtector:Show(bIsVisible)
end
--- generic helpers ---
function CombatPlates:GetDistanceTo(uUnit)
local tPos1 = self.uPlayerUnit:GetPosition()
local tPos2 = uUnit:GetPosition()
return math.sqrt(math.pow(tPos1.x - tPos2.x, 2) + math.pow(tPos1.y - tPos2.y, 2) + math.pow(tPos1.z - tPos2.z, 2))
end
function CombatPlates:RenderShortNumber(nValue)
if nValue < 1000 then
return nValue
end
if nValue < 100000 then
return string.format("%.1fk", math.ceil(nValue / 100) / 10)
end
if nValue < 1000000 then
return math.ceil(nValue / 1000) .. "k"
end
if nValue < 10000000 then
return string.format("%.1fm", math.ceil(nValue / 100000) / 10)
end
return math.ceil(nValue / 1000000) .. "m"
end
function CombatPlates:MovePixieLineToFractionHorizontal(uWindow, nPixieId, nFraction)
local tPixie = uWindow:GetPixieInfo(nPixieId)
local nOffset = uWindow:GetWidth() * nFraction
if nOffset % 1 < 0.5 then
nOffset = math.floor(nOffset)
else
nOffset = math.ceil(nOffset)
end
tPixie.loc.nOffsets[1] = nOffset
tPixie.loc.nOffsets[3] = nOffset
uWindow:UpdatePixie(nPixieId, tPixie)
end
--- update events - main ---
function CombatPlates:OnChangeWorld()
self.uPlayerUnit = nil
end
function CombatPlates:OnUnitCreated(uUnit)
if not uUnit:ShouldShowNamePlate()
or uUnit:GetType() == "Mount"
or uUnit:GetType() == "Plug"
or uUnit:GetType() == "Simple"
or uUnit:GetType() == "Collectible"
or uUnit:GetType() == "PinataLoot" then
-- this type of unit will never have a nameplate
return
end
local nUnitId = uUnit:GetId()
if self.uPlayerUnit == nil then
self.tUnitsBacklog[nUnitId] = uUnit
--Print("Backlogged " .. nUnitId .. " (" .. uUnit:GetName() .. ")")
return
end
-- why some units have this???
--if --[[not uUnit:ShouldShowNamePlate() or--]] uUnit:GetType() == "Mount" or uUnit:GetType() == "Plug" or uUnit:GetType() == "Simple" then
-- this unit will never have a nameplate
-- return
--end
if self.tNameplates[nUnitId] ~= nil then
-- unit already exists
--Print("Added " .. nUnitId .. " (" .. uUnit:GetName() .. ")")
return
end
self:AddNameplate(uUnit)
end
function CombatPlates:OnUnitDestroyed(uUnit)
local nUnitId = uUnit:GetId()
if self.uPlayerUnit == nil then
self.tUnitsBacklog[nUnitId] = nil
return
end
if self.tNameplates[nUnitId] == nil then
return
end
self:RemoveNameplate(nUnitId)
end
function CombatPlates:AddUnitsFromBacklog()
for nUnitId, uUnit in pairs(self.tUnitsBacklog) do
if uUnit:IsValid() then
self:OnUnitCreated(uUnit)
end
end
self.tUnitsBacklog = {}
end
function CombatPlates:OnWorldLocationOnScreen(uHandler, uControl, bOnScreen)
self:UpdateNameplateEssentials(uHandler:GetUnit():GetId())
end
function CombatPlates:OnTargetUnitChanged(uUnit)
if uUnit == nil then
if self.nTargetId ~= nil then
self:UpdateTargetStatus(self.nTargetId, false)
self.nTargetId = nil
end
return
end
local nUnitId = uUnit:GetId()
if self.nTargetId == nUnitId then
return
end
if self.nTargetId ~= nil then
self:UpdateTargetStatus(self.nTargetId, false)
end
self:UpdateTargetStatus(nUnitId, true)
if self.tNameplates[nUnitId] ~= nil then
self:UpdatePlateMode(nUnitId)
end
self.nTargetId = nUnitId
end
--- update events - icon updates ---
function CombatPlates:OnUnitActivationTypeChanged(uUnit)
local nUnitId = uUnit:GetId()
if self.tNameplates[nUnitId] == nil then
return
end
self:UpdateWholeNameplate(nUnitId)
end
--- one time updates ---
function CombatPlates:ConvertColorsToObjects()
for i, tList in pairs(self.tSettings.tColors) do
for j, tColor in pairs(tList) do
tList[j] = ApolloColor.new(tColor)
end
end
end
--- Event Handlers ---
function CombatPlates:OnTarget( wndHandler, wndControl, eMouseButton)
if wndHandler == wndControl then
local idUnit = wndHandler:GetId()
if self.tWindowLookup[idUnit] == nil or eMouseButton ~= GameLib.CodeEnumInputMouse.Left then
return
end
local unitOwner = self.tWindowLookup[idUnit].unit
if GameLib.GetTargetUnit() ~= unitOwner then
GameLib.SetTargetUnit(unitOwner)
end
return true
end
end
--- debug ---
function CombatPlates:debug(...)
if not self.tSettings.bDebug then
return
end
if arg.n == 1 then
Print(arg[1])
return
end
for i, value in pairs(arg) do
if i ~= "n" then
Print(value)
end
end
end
function CombatPlates:dump(name, value)
SendVarToRover(name, value)
end
--- create object ---
local CombatPlatesInst = CombatPlates:new()
CombatPlatesInst:Init()