forked from Mirroar/TopFit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.lua
More file actions
784 lines (684 loc) · 30 KB
/
inventory.lua
File metadata and controls
784 lines (684 loc) · 30 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
--[[ inventory management and caching
interesting variables:
TopFit.itemsCache - item Tables, indexed by itemLink
TopFit.scoresCache - scores, indexed by itemLink and setCode
]]--
local ReforgingInfo = LibStub("LibReforgingInfo-1.0") -- used for detecting reforged stats on items
local ReforgingStats = {
"ITEM_MOD_SPIRIT_SHORT",
"ITEM_MOD_DODGE_RATING_SHORT",
"ITEM_MOD_PARRY_RATING_SHORT",
"ITEM_MOD_HIT_RATING_SHORT",
"ITEM_MOD_CRIT_RATING_SHORT",
"ITEM_MOD_HASTE_RATING_SHORT",
"ITEM_MOD_EXPERTISE_RATING_SHORT",
"ITEM_MOD_MASTERY_RATING_SHORT"
}
local function tinsertonce(table, data)
local found = false
for _, v in pairs(table) do
if v == data then
found = true
break
end
end
if not found then
tinsert(table, data)
end
end
function TopFit:ClearCache()
TopFit.itemsCache = {}
TopFit.scoresCache = {}
end
function TopFit:GetSetItemFromSlot(slotID, setCode)
local itemPositions = GetEquipmentSetLocations(TopFit:GenerateSetName(TopFit.db.profile.sets[setCode].name))
if itemPositions then
local itemLocation = itemPositions[slotID]
if itemLocation and itemLocation ~= 1 and itemLocation ~= 0 then
local itemLink = nil
local player, bank, bags, slot, bag = EquipmentManager_UnpackLocation(itemLocation)
if player then
if bank then
-- item is banked, use itemID
local itemID = GetEquipmentSetItemIDs(TopFit:GenerateSetName(TopFit.db.profile.sets[setCode].name))[slotID]
if itemID and itemID ~= 1 then
_, itemLink = GetItemInfo(itemID)
end
elseif bags then
-- item is in player's bags
itemLink = GetContainerItemLink(bag, slot)
else
-- item is equipped
itemLink = GetInventoryItemLink("player", slot)
end
return itemLink
end
end
end
return nil
end
-- gather all items from inventory and bags, save their info to cache
function TopFit:collectItems(bag)
TopFit.characterLevel = UnitLevel("player")
if bag and bag >= 0 and bag <= 4 then
-- only check a specific bag (used on BAG_UPDATE)
for slot = 1, GetContainerNumSlots(bag) do
local item = GetContainerItemLink(bag, slot)
TopFit:UpdateCache(item)
end
else
-- check bags
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
local item = GetContainerItemLink(bag, slot)
TopFit:UpdateCache(item)
end
end
-- check equipped items
for _, invSlot in pairs(TopFit.slots) do
local item = GetInventoryItemLink("player", invSlot)
TopFit:UpdateCache(item)
end
end
end
-- collect item information if necessary
function TopFit:UpdateCache(item)
if item and (not TopFit.itemsCache[item]) then
-- check if it's equipment
if IsEquippableItem(item) then
local itemTable = TopFit:GetItemInfoTable(item)
if itemTable then
-- save in cache
TopFit.itemsCache[item] = itemTable
-- calculate set scores
TopFit:CalculateItemScore(item)
end
end
end
end
-- find out all we need to know about an item. and maybe even more
-- this does not return information which might change, only things you can get from the item link
function TopFit:GetItemInfoTable(item)
local itemName, itemLink, itemQuality, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture = GetItemInfo(item)
if not itemLink then
return nil
end
-- generate item info
local itemID = string.gsub(itemLink, ".*|Hitem:([0-9]*):.*", "%1")
itemID = tonumber(itemID)
local enchantID = string.gsub(itemLink, ".*|Hitem:[0-9]*:([0-9]*):.*", "%1")
enchantID = tonumber(enchantID)
-- item stats
local itemBonus = GetItemStats(itemLink)
-- gems
local gemBonus = {}
local gems = {}
for i = 1, 3 do
local _, gem = GetItemGem(item, i) -- name, itemlink
if gem then
gems[i] = gem
local gemID = string.gsub(gem, ".*|Hitem:([0-9]*):.*", "%1")
gemID = tonumber(gemID)
-- add gem uniqueness
local uniqueFamily, maxEquipped = GetItemUniqueness(gem)
local uniqueStat
if uniqueFamily then
if uniqueFamily == -1 then
-- single unique item
uniqueStat = "UNIQUE: item-"..gemID.."*"..maxEquipped
else
-- item belongs to a unique family
uniqueStat = "UNIQUE: family-"..uniqueFamily.."*"..maxEquipped
end
gemBonus[uniqueStat] = (gemBonus[uniqueStat] or 0) + 1
end
if (TopFit.gemIDs[gemID]) then
-- collect stats
for stat, value in pairs(TopFit.gemIDs[gemID].stats) do
gemBonus[stat] = (gemBonus[stat] or 0) + value
end
else
-- unknown gem, tell the user
TopFit:Warning("Could not identify gem "..i.." ("..gem..") of your "..itemLink..". Please tell the author so its stats can be added.")
end
end
end
if #gems > 0 then
-- try to find socket bonus by scanning item tooltip (though I hoped to avoid that entirely)
--TODO: this will have to be rewritten to be calculated on the fly at some point. meta gem requirements will not always work this way
TopFit.scanTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
TopFit.scanTooltip:SetHyperlink(itemLink)
local numLines = TopFit.scanTooltip:NumLines()
local socketBonusString = _G["ITEM_SOCKET_BONUS"] -- "Socket Bonus: %s" in enUS client, for example
socketBonusString = string.gsub(socketBonusString, "%%s", "(.*)")
local socketBonusIsActive = false
local socketBonus = nil
for i = 1, numLines do
local leftLine = getglobal("TFScanTooltip".."TextLeft"..i)
local leftLineText = leftLine:GetText()
if string.find(leftLineText, socketBonusString) then
-- This line is the socket bonus.
if leftLine.GetTextColor then
socketBonusIsActive = (leftLine:GetTextColor() == 0) -- green's red component is 0, but grey's red component is .5
else
socketBonusIsActive = true -- we can't get the text color, so we assume the bonus is active
end
socketBonus = string.gsub(leftLineText, "^"..socketBonusString.."$", "%1")
break
end
end
if (socketBonusIsActive) then
-- go through our stats to find the bonus
for _, sTable in pairs(TopFit.statList) do
for _, statCode in pairs(sTable) do
if (string.find(socketBonus, _G[statCode])) then -- simple short stat codes like "Intellect", "Hit Rating"
local bonusValue = string.gsub(socketBonus, _G[statCode], "")
bonusValue = (tonumber(bonusValue) or 0)
gemBonus[statCode] = (gemBonus[statCode] or 0) + bonusValue
end
end
end
end
TopFit.scanTooltip:Hide()
end
-- enchantment
local enchantBonus = {}
if enchantID > 0 then
local found = false
for _, slotID in pairs(TopFit.slots) do
if (TopFit.enchantIDs[slotID] and TopFit.enchantIDs[slotID][enchantID]) then
enchantBonus = TopFit.enchantIDs[slotID][enchantID]
found = true
end
end
if not found then
-- unknown enchant, tell the user
TopFit:Warning("Could not identify enchant ID "..enchantID.." of your "..itemLink..". Please tell the author so its stats can be added. Also include the enchant's name to make it easier to add, please.")
end
end
-- scan for setname
TopFit.scanTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
TopFit.scanTooltip:SetHyperlink(itemLink)
local numLines = TopFit.scanTooltip:NumLines()
local setName = nil
for i = 1, numLines do
local leftLine = getglobal("TFScanTooltip".."TextLeft"..i)
local leftLineText = leftLine:GetText()
if string.find(leftLineText, "(.*)%s%([0-9]+/[0-9+]%)") then
setName = select(3, string.find(leftLineText, "(.*)%s%([0-9]+/[0-9+]%)"))
break
end
end
-- add set name
if setName then
itemBonus["SET: "..setName] = 1
end
-- add item uniqueness
local uniqueFamily, maxEquipped = GetItemUniqueness(item)
if uniqueFamily then
if uniqueFamily == -1 then
-- single unique item
itemBonus["UNIQUE: item-"..itemID.."*"..maxEquipped] = 1
else
-- item belongs to a unique family
itemBonus["UNIQUE: family-"..uniqueFamily.."*"..maxEquipped] = 1
end
end
-- add armor type
if itemSubType == TOPFIT_ARMORTYPE_CLOTH then
itemBonus["TOPFIT_ARMORTYPE_CLOTH"] = 1
end
if itemSubType == TOPFIT_ARMORTYPE_LEATHER then
itemBonus["TOPFIT_ARMORTYPE_LEATHER"] = 1
end
if itemSubType == TOPFIT_ARMORTYPE_MAIL then
itemBonus["TOPFIT_ARMORTYPE_MAIL"] = 1
end
if itemSubType == TOPFIT_ARMORTYPE_PLATE then
itemBonus["TOPFIT_ARMORTYPE_PLATE"] = 1
end
-- for weapons, add melee/ranged dps
if (string.find(itemEquipLoc, "RANGED") or string.find(itemEquipLoc, "THROWN")) then
itemBonus["TOPFIT_RANGED_DPS"] = itemBonus["ITEM_MOD_DAMAGE_PER_SECOND_SHORT"] or nil
end
if (string.find(itemEquipLoc, "WEAPON")) then
itemBonus["TOPFIT_MELEE_DPS"] = itemBonus["ITEM_MOD_DAMAGE_PER_SECOND_SHORT"] or nil
end
-- add weapon speeds
if (string.find(itemEquipLoc, "RANGED") or string.find(itemEquipLoc, "THROWN") or string.find(itemEquipLoc, "WEAPON")) then
TopFit.scanTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
TopFit.scanTooltip:SetHyperlink(itemLink)
local numLines = TopFit.scanTooltip:NumLines()
local speedString = SPEED.." ([0-9.]*)";
for i = 1, numLines do
local rightLine = getglobal("TFScanTooltip".."TextRight"..i)
local rightLineText = rightLine:GetText()
if (rightLineText and string.find(rightLineText, speedString)) then
TopFit:Debug("SPEED FOUND!")
local speed = string.gsub(rightLineText, "^"..speedString.."$", "%1")
speed = tonumber(speed)
if (string.find(itemEquipLoc, "RANGED") or string.find(itemEquipLoc, "THROWN")) then
itemBonus["TOPFIT_RANGED_WEAPON_SPEED"] = speed
end
if (string.find(itemEquipLoc, "WEAPON")) then
itemBonus["TOPFIT_MELEE_WEAPON_SPEED"] = speed
end
end
end
TopFit.scanTooltip:Hide()
end
-- dirty little mana regen fix
itemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] = ((itemBonus["ITEM_MOD_POWER_REGEN0_SHORT"] or 0) + (itemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] or 0))
itemBonus["ITEM_MOD_POWER_REGEN0_SHORT"] = nil
if (itemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] == 0) then itemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] = nil end
gemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] = ((gemBonus["ITEM_MOD_POWER_REGEN0_SHORT"] or 0) + (gemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] or 0))
gemBonus["ITEM_MOD_POWER_REGEN0_SHORT"] = nil
if (gemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] == 0) then gemBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] = nil end
enchantBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] = ((enchantBonus["ITEM_MOD_POWER_REGEN0_SHORT"] or 0) + (enchantBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] or 0))
enchantBonus["ITEM_MOD_POWER_REGEN0_SHORT"] = nil
if (enchantBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] == 0) then enchantBonus["ITEM_MOD_MANA_REGENERATION_SHORT"] = nil end
-- add reforged stats to base item stats if applicable
local reforgeBonus = {}
if (ReforgingInfo:IsItemReforged(itemLink)) then
local reforgeID = ReforgingInfo:GetReforgeID(itemLink)
local minus, plus = ReforgingInfo:GetReforgedStatIDs(reforgeID)
-- replace IDs with their global string (the library only returns IDs or localized Strings)
minus = ReforgingStats[minus]
plus = ReforgingStats[plus]
local statValue = math.floor((itemBonus[minus] or 0) * 0.4)
reforgeBonus[minus] = -statValue
reforgeBonus[plus] = statValue
end
-- calculate total values
local totalBonus = {}
for _, bonusTable in pairs({itemBonus, gemBonus, enchantBonus, reforgeBonus}) do
for stat, value in pairs(bonusTable) do
totalBonus[stat] = (totalBonus[stat] or 0) + value
end
end
-- add hit for spirit for caster classes with the respective talent
local hitForSpirit = 0;
local specialization = GetSpecialization()
if (select(2, UnitClass("player")) == "PRIEST" and specialization == 3)
or (select(2, UnitClass("player")) == "DRUID" and specialization == 1)
or (select(2, UnitClass("player")) == "SHAMAN" and specialization == 1) then
if UnitLevel("player") >= 20 then
hitForSpirit = 1;
end
end
if (hitForSpirit > 0) then
totalBonus["ITEM_MOD_HIT_RATING_SHORT"] = (totalBonus["ITEM_MOD_HIT_RATING_SHORT"] or 0) + (totalBonus["ITEM_MOD_SPIRIT_SHORT"] or 0) * hitForSpirit
itemBonus["ITEM_MOD_HIT_RATING_SHORT"] = (itemBonus["ITEM_MOD_HIT_RATING_SHORT"] or 0) + (itemBonus["ITEM_MOD_SPIRIT_SHORT"] or 0) * hitForSpirit
end
-- also check proc / on-use effects for score calculation
if not TopFit.allStatsInATable then
TopFit.allStatsInATable = {}
for _, statsTable in pairs(TopFit.statList) do
for _, stat in pairs(statsTable) do
tinsert(TopFit.allStatsInATable, stat)
end
end
end
local procBonus = {}
local procUptime = 0.5
local searchStat, amount, duration, cooldown = TopFit:ItemHasSpecialBonus(itemLink, unpack(TopFit.allStatsInATable))
if searchStat and amount and amount > 0 then
if not cooldown or cooldown <= 0 then cooldown = 45 end
procBonus[searchStat] = procUptime * amount * duration / cooldown
end
local result = {
itemLink = itemLink,
itemID = itemID,
itemQuality = itemQuality,
itemMinLevel = itemMinLevel,
itemEquipLoc = itemEquipLoc,
equipLocationsByType = TopFit:GetEquipLocationsByInvType(itemEquipLoc),
gems = gems,
itemBonus = itemBonus,
enchantBonus = enchantBonus,
gemBonus = gemBonus,
reforgeBonus = reforgeBonus,
totalBonus = totalBonus,
procBonus = procBonus
}
return result
end
-- calculate an item's score relative to a given set
function TopFit:CalculateItemScore(itemLink)
local itemTable = TopFit:GetCachedItem(itemLink)
if not itemTable then return end
for setCode, setTable in pairs(TopFit.db.profile.sets) do
local set = setTable.weights
local caps = setTable.caps
-- calculate item score
local itemScore = 0
local capsModifier = 0
-- iterate given weights
for stat, statValue in pairs(set) do
if itemTable.totalBonus[stat] then
-- check for hard cap on this stat
if ((not caps) or (not caps[stat]) or (not caps[stat]["active"]) or (caps[stat]["soft"])) then
itemScore = itemScore + statValue * itemTable.totalBonus[stat]
else
-- part of hard cap, score calculated extra
capsModifier = capsModifier + statValue * itemTable.totalBonus[stat]
end
end
if itemTable.procBonus[stat] then
-- check for hard cap on this stat
if ((not caps) or (not caps[stat]) or (not caps[stat]["active"]) or (caps[stat]["soft"])) then
itemScore = itemScore + statValue * itemTable.procBonus[stat]
else
-- part of hard cap, score calculated extra
capsModifier = capsModifier + statValue * itemTable.procBonus[stat]
end
end
end
-- also calculate raw item score
local rawScore = 0
local rawModifier = 0
-- iterate given weights
for stat, statValue in pairs(set) do
if itemTable.itemBonus[stat] then
-- check for hard cap on this stat
if ((not caps) or (not caps[stat]) or (not caps[stat]["active"]) or (caps[stat]["soft"])) then
rawScore = rawScore + statValue * itemTable.itemBonus[stat]
else
-- part of hard cap, score calculated extra
rawModifier = rawModifier + statValue * itemTable.totalBonus[stat]
end
end
if itemTable.procBonus[stat] then
-- check for hard cap on this stat
if ((not caps) or (not caps[stat]) or (not caps[stat]["active"]) or (caps[stat]["soft"])) then
rawScore = itemScore + statValue * itemTable.procBonus[stat]
else
-- part of hard cap, score calculated extra
rawModifier = capsModifier + statValue * itemTable.procBonus[stat]
end
end
end
if not TopFit.scoresCache[itemLink] then
TopFit.scoresCache[itemLink] = {}
end
--TODO: could be rewritten slightly to save some tables
TopFit.scoresCache[itemLink][setCode] = {
itemScore = itemScore,
itemScoreWithoutCaps = itemScore + capsModifier,
rawScore = rawScore,
rawScoreWithoutCaps = rawScore + rawModifier,
}
end
end
-- calculate item scores
function TopFit:CalculateScores()
-- iterate all cached items and recalculate their scores
for itemLink, _ in pairs(TopFit.itemsCache) do
TopFit:CalculateItemScore(itemLink)
end
end
-- used by tooltip to decide which item slots to compare to
function TopFit:GetEquipLocationsByInvType(itemEquipLoc)
if itemEquipLoc == "INVTYPE_2HWEAPON" then
--TODO: check weapon type
return {16}
elseif itemEquipLoc == "INVTYPE_BODY" then
return {4}
elseif itemEquipLoc == "INVTYPE_CHEST" or itemEquipLoc == "INVTYPE_ROBE" then
return {5}
elseif itemEquipLoc == "INVTYPE_CLOAK" then
return {15}
elseif itemEquipLoc == "INVTYPE_FEET" then
return {8}
elseif itemEquipLoc == "INVTYPE_FINGER" then
return {11, 12}
elseif itemEquipLoc == "INVTYPE_HAND" then
return {10}
elseif itemEquipLoc == "INVTYPE_HEAD" then
return {1}
elseif itemEquipLoc == "INVTYPE_HOLDABLE" or itemEquipLoc == "INVTYPE_SHIELD" then
return {17}
elseif itemEquipLoc == "INVTYPE_LEGS" then
return {7}
elseif itemEquipLoc == "INVTYPE_NECK" then
return {2}
elseif itemEquipLoc == "INVTYPE_RANGED" or itemEquipLoc == "INVTYPE_RANGEDRIGHT" or itemEquipLoc == "INVTYPE_THROWN" then
return {16}
elseif itemEquipLoc == "INVTYPE_SHOULDER" then
return {3}
elseif itemEquipLoc == "INVTYPE_TABARD" then
return {19}
elseif itemEquipLoc == "INVTYPE_TRINKET" then
return {13, 14}
elseif itemEquipLoc == "INVTYPE_WAIST" then
return {6}
elseif itemEquipLoc == "INVTYPE_WEAPON" then
return {16, 17}
elseif itemEquipLoc == "INVTYPE_WEAPONMAINHAND" then
return {16}
elseif itemEquipLoc == "INVTYPE_WEAPONOFFHAND" then
return {17}
elseif itemEquipLoc == "INVTYPE_WRIST" then
return {9}
end
-- default / invalid location
return {}
end
-- items are deemed interesting if any of these conditions are true:
-- it is currently part of a set
-- there is no item with a higher score for the item's slot(s)
-- it contributes more to a set's cap than any item with a higher score
--
-- in case of errors or weird input, this function will return true
function TopFit:IsInterestingItem(itemID, setID)
if not itemID then return true, "no itemID given" end
local item = TopFit:GetCachedItem(itemID)
if not item then return true, "invalid itemID or no item info available" end
if not setID then
-- check if item is part of any current equipment set
for i = 1, GetNumEquipmentSets() do
local name, _, _ = GetEquipmentSetInfo(i)
itemIDs = GetEquipmentSetItemIDs(name)
for _, iID in pairs(itemIDs) do
if iID == item.itemID then return true, "part of current set" end
end
end
-- check for all sets
for set, _ in pairs(TopFit.db.profile.sets) do
local isInteresting, reason = TopFit:IsInterestingItem(itemID, set)
if (isInteresting) then
return isInteresting, reason
end
end
return false, "item is not interesting for any set"
end
-- get items available from the same slot(s)
for _, slotID in pairs(item.equipLocationsByType) do
if self.db.profile.sets[setID].forced then
for sID, forceID in pairs(self.db.profile.sets[setID].forced) do
if (sID == slotID and forceID == item.itemID) then
return true, "item is forced in a set"
end
end
end
-- try to see if an item exists which is definitely better
local betterItemExists = 0
local numBetterItemsNeeded = 1
-- For items that can be used in 2 slots, we also need at least 2 better items to declare an item useless
if (slotID == 17) -- offhand
or (slotID == 12) -- ring 2
or (slotID == 14) -- trinket 2
then
numBetterItemsNeeded = 2
end
local otherItems = TopFit:GetEquippableItems(slotID)
for _, otherItem in pairs(otherItems) do
local compareTable = TopFit:GetCachedItem(otherItem.itemLink)
if compareTable and (item.itemID ~= compareTable.itemID) and
(TopFit:GetItemScore(item.itemLink, setID, false) < TopFit:GetItemScore(compareTable.itemLink, setID, false)) and
(item.itemEquipLoc == compareTable.itemEquipLoc) then -- especially important for weapons, we do not want to compare 2h and 1h weapons
-- score is greater, see if caps are also better
local allStats = true
for statCode, preferences in pairs(TopFit.db.profile.sets[setID].caps) do
if preferences.active then
if (item.totalBonus[statCode] or 0) > (compareTable.totalBonus[statCode] or 0) then
allStats = false
break
end
end
end
if allStats then
betterItemExists = betterItemExists + 1
if (betterItemExists >= numBetterItemsNeeded) then
break
end
end
end
end
if betterItemExists < numBetterItemsNeeded then
-- there are not enough better alternatives, it is indeed an interesting item
return true, "one of the best items for this slot"
end
end
return false, "item is not interesting for this set"
end
-- checks whether an item is BoE by given bag and slot number
function TopFit:IsItemBoE(bag, slot)
TopFit.scanTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
TopFit.scanTooltip:SetBagItem(bag, slot)
local numLines = TopFit.scanTooltip:NumLines()
for i = 1, numLines do
local leftLine = getglobal("TFScanTooltip".."TextLeft"..i)
local leftLineText = leftLine:GetText()
if string.find(leftLineText, ITEM_BIND_ON_EQUIP) then
return true
end
end
return false
end
-- returns all equippable items, limited by slot, if given
local slotAvailableItems = {}
function TopFit:GetEquippableItems(requestedSlotID)
local itemListBySlot = {}
local availableSlots = {}
for i = 1, 20 do
itemListBySlot[i] = {}
end
-- find available item ids for each slot
for slotName, slotID in pairs(TopFit.slots) do
itemListBySlot[slotID] = {}
wipe(slotAvailableItems)
GetInventoryItemsForSlot(slotID, slotAvailableItems)
for availableLocation, availableItemID in pairs(slotAvailableItems) do
if (not availableSlots[availableItemID]) then
availableSlots[availableItemID] = { slotID }
else
tinsertonce(availableSlots[availableItemID], slotID)
end
end
-- special handling for plate heirlooms
if (TopFit.heirloomInfo.isPlateWearer and (slotID == 3 or slotID == 5) and UnitLevel("player") < 40) then
for i = 1, #(TopFit.heirloomInfo.plateHeirlooms[slotID]) do
if (not availableSlots[TopFit.heirloomInfo.plateHeirlooms[slotID][i]]) then
availableSlots[TopFit.heirloomInfo.plateHeirlooms[slotID][i]] = { slotID }
else
tinsertonce(availableSlots[TopFit.heirloomInfo.plateHeirlooms[slotID][i]], slotID)
end
end
end
-- special handling for mail heirlooms
if (TopFit.heirloomInfo.isMailWearer and (slotID == 3 or slotID == 5) and UnitLevel("player") < 40) then
for i = 1, #(TopFit.heirloomInfo.mailHeirlooms[slotID]) do
if (not availableSlots[TopFit.heirloomInfo.mailHeirlooms[slotID][i]]) then
availableSlots[TopFit.heirloomInfo.mailHeirlooms[slotID][i]] = { slotID }
else
tinsertonce(availableSlots[TopFit.heirloomInfo.mailHeirlooms[slotID][i]], slotID)
end
end
end
end
-- check player's bags
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
local itemLink = GetContainerItemLink(bag, slot)
if itemLink then
local itemID = string.gsub(itemLink, ".*|Hitem:([0-9]*):.*", "%1")
itemID = tonumber(itemID)
if (availableSlots[itemID]) then
-- check if item is BoE
local isBoE = TopFit:IsItemBoE(bag, slot)
for _, slotID in pairs(availableSlots[itemID]) do
tinsert(itemListBySlot[slotID], {
itemLink = itemLink,
isBoE = isBoE,
bag = bag,
slot = slot
})
end
end
end
end
end
-- check player's inventory
for _, invSlot in pairs(TopFit.slots) do
local itemLink = GetInventoryItemLink("player", invSlot)
if itemLink then
local itemID = string.gsub(itemLink, ".*|Hitem:([0-9]*):.*", "%1")
itemID = tonumber(itemID)
if (availableSlots[itemID]) then
for _, slotID in pairs(availableSlots[itemID]) do
tinsert(itemListBySlot[slotID], {
itemLink = itemLink,
isBoE = false, -- it is already equipped
slot = invSlot
})
end
end
end
end
if (TopFit.setCode) then
-- add virtual items
if (TopFit.setCode and TopFit.db.profile.sets[TopFit.setCode].virtualItems and not TopFit.db.profile.sets[TopFit.setCode].skipVirtualItems) then
for _, itemLink in pairs(TopFit.db.profile.sets[TopFit.setCode].virtualItems) do
local item = TopFit:GetCachedItem(itemLink)
if item then -- in case weird items end up in our cache
local equipSlots = TopFit:GetEquipLocationsByInvType(item.itemEquipLoc)
for _, slotID in pairs(equipSlots) do
tinsert(itemListBySlot[slotID], {
itemLink = itemLink,
isBoE = false, -- if it's in virtual items, we want to include it
isVirtual = true
})
end
end
end
end
end
if (requestedSlotID) then
return itemListBySlot[requestedSlotID]
else
return itemListBySlot
end
end
function TopFit:GetItemScore(itemLink, setCode, dontUseCaps, useRawItem)
if not TopFit.scoresCache[itemLink] or not TopFit.scoresCache[itemLink][setCode] then return 0 end
if dontUseCaps then
if useRawItem then
return TopFit.scoresCache[itemLink][setCode].rawScoreWithoutCaps
else
return TopFit.scoresCache[itemLink][setCode].itemScoreWithoutCaps
end
else
if useRawItem then
return TopFit.scoresCache[itemLink][setCode].rawScore
else
return TopFit.scoresCache[itemLink][setCode].itemScore
end
end
end
-- gets an item's info from the cache
function TopFit:GetCachedItem(itemLink)
if not itemLink then return nil end
TopFit:UpdateCache(itemLink)
return TopFit.itemsCache[itemLink]
end