-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinventory.lua
More file actions
774 lines (680 loc) · 24.4 KB
/
inventory.lua
File metadata and controls
774 lines (680 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
local addonName, ns, _ = ...
--[[ inventory management and caching
interesting variables:
TopFit.itemsCache - item Tables, indexed by itemLink
TopFit.scoresCache - scores, indexed by itemLink and setCode
]]--
-- FIXME: this should not be used
-- GLOBALS: TopFit, TOPFIT_ARMORTYPE_CLOTH, TOPFIT_ARMORTYPE_LEATHER, TOPFIT_ARMORTYPE_MAIL, TOPFIT_ARMORTYPE_PLATE
-- GLOBALS: _G, UIParent, MAX_PLAYER_LEVEL_TABLE, ITEM_BIND_ON_EQUIP, SPEED, MAX_NUM_SOCKETS
-- GLOBALS: C_EquipmentSet, EquipmentManager_UnpackLocation, UnitLevel, UnitClass, GetItemInfo, GetContainerNumSlots, GetContainerItemID, GetContainerItemLink, GetInventoryItemLink, GetItemStats, GetInventoryItemsForSlot, GetItemGem, GetSpecialization
-- GLOBALS: string, math, select, pairs, tonumber, wipe, unpack
local tinsert = table.insert
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
ns.enhancementWarnings = {
show = false,
gems = {},
enchants = {},
}
local primaryStatMap = {
[LE_UNIT_STAT_STRENGTH] = 'ITEM_MOD_STRENGTH_SHORT',
[LE_UNIT_STAT_AGILITY] = 'ITEM_MOD_AGILITY_SHORT',
[LE_UNIT_STAT_INTELLECT] = 'ITEM_MOD_INTELLECT_SHORT',
}
function TopFit:ClearCache()
TopFit.itemsCache = {}
TopFit.scoresCache = {}
end
--- Gather all items from inventory and bags and save their info to cache.
-- @param bag Limit collection to this bag.
function ns:updateItemsCache(bag)
ns.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)
ns:UpdateCache(item)
end
else
-- check all bags
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
local item = GetContainerItemLink(bag, slot)
ns:UpdateCache(item)
end
end
-- check equipped items
for _, invSlot in pairs(ns.slots) do
local item = GetInventoryItemLink("player", invSlot)
ns:UpdateCache(item)
end
end
end
-- collect item information if necessary
function ns:UpdateCache(item)
if item and (not ns.itemsCache[item]) then
-- check if it's equipment
--if IsEquippableItem(item) then --TODO: check if removing this breaks anything; it causes problems with gems
local itemTable = ns:GetItemInfoTable(item)
if itemTable then
-- save in cache
ns.itemsCache[item] = itemTable
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
elseif type(item) == 'string' then
-- Why does GetItemInfo return a DIFFERENT link from the called one?!
itemLink = item
end
-- generate item info
local itemID = GetItemInfoInstant(itemLink)
local enchantID = string.gsub(itemLink, ".*|Hitem:[0-9]*:([0-9]*):.*", "%1")
enchantID = tonumber(enchantID)
-- item stats
local itemBonus = GetItemStats(itemLink)
-- item stats for gems from our database
if ns.gemIDs[itemID] then
itemBonus = ns.gemIDs[itemID].stats
local skillReq = ns.gemIDs[itemID].skill
if skillReq then
local skill, req = string.split("-", skillReq)
itemBonus["SKILL: " .. skill] = req
end
end
-- gems
local gemBonus = {}
local gems = {}
for i = 1, MAX_NUM_SOCKETS 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 = ns: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
local skillReq = TopFit.gemIDs[gemID].skill
if skillReq then
local skill, req = string.split("-", skillReq)
gemBonus["SKILL: " .. skill] = req
end
else
-- unknown gem, tell the user
if not ns.enhancementWarnings.gems[gemID] then
ns.enhancementWarnings.show = true
ns.enhancementWarnings.gems[gemID] = {
itemID = gemID,
itemLink = gem,
hostItemLink = itemLink,
}
end
end
end
end
-- try to find socket bonus and some other info by scanning item tooltip (though I hoped to avoid that entirely)
local tooltip = TopFit.scanTooltip
tooltip:SetOwner(UIParent, 'ANCHOR_NONE')
tooltip:SetHyperlink(itemLink)
local numLines = tooltip:NumLines()
if #gems > 0 then
--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
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 = _G[tooltip:GetName()..'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 _G[statCode] and (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
end
-- scan for siege of Orgrimmar %-based Trinkets
if itemEquipLoc == 'INVTYPE_TRINKET' then
for i = 1, numLines do
local leftLine = _G[tooltip:GetName()..'TextLeft'..i]
local leftLineText = leftLine:GetText()
if string.find(leftLineText, TopFit.locale.ItemScan.PercentBonusTrigger) then
-- SoO Percent bonus, figure out percentage
local percentBonus = string.gsub(leftLineText, "^.*(%d+)%%.*$", "%1")
if percentBonus then
itemBonus['TOPFIT_SECONDARY_PERCENT'] = tonumber(percentBonus)
break
end
end
end
end
-- enchantment
local enchantBonus = {}
if enchantID and 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].stats
if TopFit.enchantIDs[slotID][enchantID].couldNotParse then
local enchantItemID = TopFit.enchantIDs[slotID][enchantID].itemID
local _, enchantLink = GetItemInfo(enchantItemID)
if not ns.enhancementWarnings.enchants[enchantID] then
ns.enhancementWarnings.show = true
ns.enhancementWarnings.enchants[enchantID] = {
enchantID = enchantID,
itemID = enchantItemID,
itemLink = enchantLink,
hostItemLink = itemLink,
}
end
end
found = true
end
end
if not found then
-- unknown enchant, tell the user
if not ns.enhancementWarnings.enchants[enchantID] then
ns.enhancementWarnings.show = true
ns.enhancementWarnings.enchants[enchantID] = {
enchantID = enchantID,
hostItemLink = itemLink,
}
end
end
end
-- scan for setname
local setName = nil
for i = 1, numLines do
local textLeft = _G[tooltip:GetName()..'TextLeft'..i]:GetText()
if string.find(textLeft, "(.*)%s%([0-9]+/[0-9+]%)") then
setName = select(3, string.find(textLeft, "(.*)%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 = ns:GetItemUniqueness(item)
if uniqueFamily then
if uniqueFamily == -1 then
-- single unique item
itemBonus["UNIQUE: item-"..itemID.."*"..maxEquipped] = 1
-- since WoD, unique items are determined by name
itemBonus["UNIQUE: item-"..itemName.."*"..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
elseif itemSubType == TOPFIT_ARMORTYPE_LEATHER then
itemBonus["TOPFIT_ARMORTYPE_LEATHER"] = 1
elseif itemSubType == TOPFIT_ARMORTYPE_MAIL then
itemBonus["TOPFIT_ARMORTYPE_MAIL"] = 1
elseif itemSubType == TOPFIT_ARMORTYPE_PLATE then
itemBonus["TOPFIT_ARMORTYPE_PLATE"] = 1
end
-- add spec info
local specs = GetItemSpecInfo(item)
if specs and #specs <= 0 then
specs = nil
end
-- Scan for currently inactive primary stats.
if specs then
local primaryStatValue
for statID, statName in pairs(primaryStatMap) do
primaryStatValue = primaryStatValue or itemBonus[statName]
end
for specIndex = 1, GetNumSpecializations() do
local specID, _, _, _, specRole, specPrimaryStat = GetSpecializationInfo(specIndex)
if tContains(specs, specID) and primaryStatMap[specPrimaryStat] then
itemBonus[primaryStatMap[specPrimaryStat]] = itemBonus[primaryStatMap[specPrimaryStat]] or primaryStatValue
end
end
else
-- Rely on tooltip scanning for grayed out lines.
-- TODO Can we remove this?
for i = 1, numLines do
local leftLine = _G[tooltip:GetName()..'TextLeft'..i]
local textLeft = leftLine:GetText()
local start, _, amount, stat = string.find(textLeft, "^%+(%d*) (.*)")
if start then --and math.abs(_G[tooltip:GetName()..'TextLeft'..i]:GetTextColor() - GRAY_FONT_COLOR.r) < 0.01 then
local r, g, b = leftLine:GetTextColor()
if math.abs(GRAY_FONT_COLOR.r - r) < 0.01 and math.abs(GRAY_FONT_COLOR.g - g) < 0.01 and math.abs(GRAY_FONT_COLOR.b - b) < 0.01 then
if stat == ITEM_MOD_INTELLECT_SHORT then
itemBonus.ITEM_MOD_INTELLECT_SHORT = tonumber(amount)
elseif stat == ITEM_MOD_AGILITY_SHORT then
itemBonus.ITEM_MOD_AGILITY_SHORT = tonumber(amount)
elseif stat == ITEM_MOD_STRENGTH_SHORT then
itemBonus.ITEM_MOD_STRENGTH_SHORT = tonumber(amount)
end
end
end
end
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
local speedString = SPEED.." ([0-9.]*)";
for i = 1, numLines do
local rightLine = _G[tooltip:GetName()..'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
-- done scanning the tooltip
tooltip:Hide()
-- 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
-- also check proc / on-use effects for score calculation
-- TODO: clean up this mess!
if not TopFit.allStatsInATable then
TopFit.allStatsInATable = {
"TOPFIT_PRIMARY_STAT",
}
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
-- @todo This does not handle procs with multiple stats.
local procValue = procUptime * amount * duration / cooldown
if searchStat == 'TOPFIT_PRIMARY_STAT' then
--
for specIndex = 1, GetNumSpecializations() do
local _, _, _, _, _, specPrimaryStat = GetSpecializationInfo(specIndex)
procBonus[primaryStatMap[specPrimaryStat]] = procValue
end
else
procBonus[searchStat] = procValue
end
end
-- calculate total values
local totalBonus = {}
for _, bonusTable in pairs({itemBonus, gemBonus, enchantBonus, procBonus}) do
for stat, value in pairs(bonusTable) do
totalBonus[stat] = (totalBonus[stat] or 0) + value
end
end
-- generate result
local result = {
itemLink = itemLink,
itemID = itemID,
itemQuality = itemQuality,
itemMinLevel = itemMinLevel,
itemLevel = itemLevel,
subClass = itemSubType,
itemEquipLoc = itemEquipLoc,
equipLocationsByType = TopFit:GetEquipLocationsByInvType(itemEquipLoc, true),
gems = gems,
itemBonus = itemBonus,
enchantBonus = enchantBonus,
gemBonus = gemBonus,
totalBonus = totalBonus,
procBonus = procBonus,
specs = specs,
}
-- allow plugins to modify result
ns.InvokeAll('OnGetItemStats', result)
return result
end
-- used by tooltip to decide which item slots to compare to
local compareToSlots = {}
function TopFit:GetEquipLocationsByInvType(itemEquipLoc, createTable)
local slots = createTable and {} or compareToSlots
wipe(slots)
if itemEquipLoc == "INVTYPE_HEAD" then
tinsert(slots, 1)
elseif itemEquipLoc == "INVTYPE_NECK" then
tinsert(slots, 2)
elseif itemEquipLoc == "INVTYPE_SHOULDER" then
tinsert(slots, 3)
elseif itemEquipLoc == "INVTYPE_BODY" then
tinsert(slots, 4)
elseif itemEquipLoc == "INVTYPE_CHEST" or itemEquipLoc == "INVTYPE_ROBE" then
tinsert(slots, 5)
elseif itemEquipLoc == "INVTYPE_WAIST" then
tinsert(slots, 6)
elseif itemEquipLoc == "INVTYPE_LEGS" then
tinsert(slots, 7)
elseif itemEquipLoc == "INVTYPE_FEET" then
tinsert(slots, 8)
elseif itemEquipLoc == "INVTYPE_WRIST" then
tinsert(slots, 9)
elseif itemEquipLoc == "INVTYPE_HAND" then
tinsert(slots, 10)
elseif itemEquipLoc == "INVTYPE_FINGER" then
tinsert(slots, 11)
tinsert(slots, 12)
elseif itemEquipLoc == "INVTYPE_TRINKET" then
tinsert(slots, 13)
tinsert(slots, 14)
elseif itemEquipLoc == "INVTYPE_CLOAK" then
tinsert(slots, 15)
elseif itemEquipLoc == "INVTYPE_2HWEAPON" then
--TODO: check weapon type
tinsert(slots, 16)
elseif itemEquipLoc == "INVTYPE_RANGED" or itemEquipLoc == "INVTYPE_RANGEDRIGHT" or itemEquipLoc == "INVTYPE_THROWN" then
tinsert(slots, 16)
elseif itemEquipLoc == "INVTYPE_WEAPONMAINHAND" then
tinsert(slots, 16)
elseif itemEquipLoc == "INVTYPE_WEAPON" then
tinsert(slots, 16)
tinsert(slots, 17)
elseif itemEquipLoc == "INVTYPE_WEAPONOFFHAND" then
tinsert(slots, 17)
elseif itemEquipLoc == "INVTYPE_HOLDABLE" or itemEquipLoc == "INVTYPE_SHIELD" then
tinsert(slots, 17)
elseif itemEquipLoc == "INVTYPE_TABARD" then
tinsert(slots, 19)
end
-- default / invalid location
return slots
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 _, id in ipairs(C_EquipmentSet.GetEquipmentSetIDs()) do
local itemIDs = C_EquipmentSet.GetItemIDs(id)
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 _, setID in pairs(ns.GetSetList()) do
local isInteresting, reason = TopFit:IsInterestingItem(itemID, setID)
if (isInteresting) then
return isInteresting, reason
end
end
return false, "item is not interesting for any set"
end
local set = ns.GetSetByID(setID, true)
-- get items available from the same slot(s)
for _, slotID in pairs(item.equipLocationsByType) do
if set:IsForcedItem(itemID) then
return true, "item is forced in a set"
end
-- try to see if an item exists which is definitely better
-- TODO: this is similar to what happens in ReduceItemList and should be reused
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
(set:GetItemScore(item.itemLink) < set:GetItemScore(compareTable.itemLink)) 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, _ in pairs(set:GetHardCaps()) do
if (item.totalBonus[statCode] or 0) > (compareTable.totalBonus[statCode] or 0) then
allStats = false
break
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
-- TopFit will only equip items that are already bound or do not bind at all
local bindings = {
[false] = true, -- item does not bind
[ITEM_SOULBOUND] = true,
[ITEM_ACCOUNTBOUND] = true,
[ITEM_BIND_ON_PICKUP] = true,
[ITEM_BIND_QUEST] = true,
[ITEM_BIND_TO_ACCOUNT] = true,
[ITEM_BIND_TO_BNETACCOUNT] = true,
[ITEM_BIND_ON_EQUIP] = false,
[ITEM_BIND_ON_USE] = false,
}
-- cache results since tooltip scanning is expensive
local GetItemBinding = setmetatable({}, {
__mode = 'kv',
__index = function(self, item)
local itemLink
if not rawget(self, 'isBagItem') then
-- we cache data by itemLink but also allow querying by itemID or name
itemLink = select(2, GetItemInfo(item))
if item ~= itemLink then return self[itemLink] end
ns.scanTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
ns.scanTooltip:SetHyperlink(itemLink)
end
local binding
local tipName = ns.scanTooltip:GetName()
for lineNum = 1, ns.scanTooltip:NumLines() do
local left, right = _G[tipName..'TextLeft'..lineNum], _G[tipName..'TextRight'..lineNum]
local textLeft, textRight = left and left:GetText(), right and right:GetText()
if textLeft and bindings[textLeft] ~= nil then
binding = textLeft; break
elseif textRight and bindings[textRight] ~= nil then
binding = textRight; break
end
end
ns.scanTooltip:Hide()
if not rawget(self, 'isBagItem') then -- cache result
self[itemLink] = binding or false
end
rawset(self, 'isBagItem', nil)
return binding or false
end ,
__call = function(self, item, slot)
if item and slot then
-- requires uncached data
ns.scanTooltip:SetOwner(UIParent, 'ANCHOR_NONE')
ns.scanTooltip:SetBagItem(item, slot)
rawset(self, 'isBagItem', true)
rawset(self, item, nil)
end
return self[item]
end
})
-- returns true if item has no binding or is already bound
function ns:CanUseItemBinding(container, slot)
local binding = GetItemBinding(container, slot)
return bindings[binding]
end
-- returns all equippable items, limited by slot, if given
-- TODO: rewrite this complex of functions so available items are determined and then saved as part of a calculation, not globally
local slotAvailableItems = {}
local itemListBySlot = {}
for slotID = INVSLOT_FIRST_EQUIPPED, INVSLOT_LAST_EQUIPPED do
itemListBySlot[slotID] = {}
end
function TopFit:GetEquippableItems(requestedSlotID)
local availableSlots = {}
for slotID, items in pairs(itemListBySlot) do
wipe(items)
end
-- find available item ids for each slot
for slotName, slotID in pairs(TopFit.slots) do
if not requestedSlotID or requestedSlotID == slotID then
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
end
end
-- check player's inventory
for _, invSlot in pairs(TopFit.slots) do
local itemID = GetInventoryItemID("player", invSlot)
if itemID and availableSlots[itemID] then
local itemLink = GetInventoryItemLink("player", invSlot)
for _, slotID in pairs(availableSlots[itemID]) do
tinsert(itemListBySlot[slotID], {
itemLink = itemLink,
isBoE = false, -- item is already equipped
slot = invSlot
})
end
end
end
-- check player's bags
for bag = 0, NUM_BAG_SLOTS do
for slot = 1, GetContainerNumSlots(bag) do
local itemID = GetContainerItemID(bag, slot)
if itemID and availableSlots[itemID] then
-- check if item is BoE
for _, slotID in pairs(availableSlots[itemID]) do
tinsert(itemListBySlot[slotID], {
itemLink = GetContainerItemLink(bag, slot),
isBoE = not ns:CanUseItemBinding(bag, slot),
bag = bag,
slot = slot
})
end
end
end
end
if (TopFit.setCode) then
-- add virtual items
--TODO: this does not belong in this function and should instead be handled wherever it is currently needed
local set = ns.GetSetByID(TopFit.setCode, true)
if set:GetUseVirtualItems() then
for _, itemLink in pairs(set:GetVirtualItems()) 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
-- 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
-- legacy support, kind of
function TopFit:IsOnehandedWeapon(set, item)
return set:IsOnehandedWeapon(item)
end