forked from Mirroar/TopFit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculation.lua
More file actions
881 lines (789 loc) · 40.5 KB
/
calculation.lua
File metadata and controls
881 lines (789 loc) · 40.5 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
function TopFit:StartCalculations()
-- generate table of set codes
TopFit.workSetList = {}
for setCode, _ in pairs(self.db.profile.sets) do
tinsert(TopFit.workSetList, setCode)
end
TopFit:CalculateSets()
end
function TopFit:AbortCalculations()
if TopFit.isBlocked then
TopFit.abortCalculation = true
end
end
function TopFit:CalculateSets(silent)
if (not TopFit.isBlocked) then
TopFit.silentCalculation = silent
local setCode = tremove(TopFit.workSetList)
while not self.db.profile.sets[setCode] and #(TopFit.workSetList) > 0 do
setCode = tremove(TopFit.workSetList)
end
if self.db.profile.sets[setCode] then
TopFit.setCode = setCode -- globally save the current set that is being calculated
TopFit:Debug("Calculating items for "..setCode)
-- set as working to prevent any further calls from "interfering"
TopFit.isBlocked = true
-- copy caps
TopFit.Utopia = {}
for key, value in pairs(TopFit.db.profile.sets[setCode].caps) do
TopFit.Utopia[key] = value
end
TopFit.ignoreCapsForCalculation = false
-- do the actual work
TopFit:collectItems()
TopFit:CalculateRecommendations()
end
end
end
--start calculation for setName
function TopFit:CalculateRecommendations()
local setName = self.db.profile.sets[TopFit.setCode].name
TopFit.itemRecommendations = {}
TopFit.currentItemCombination = {}
TopFit.itemCombinations = {}
TopFit.currentSetName = setName
-- determine if the player can dualwield
TopFit.playerCanDualWield = TopFit:PlayerCanDualWield()
TopFit.playerCanTitansGrip = TopFit:PlayerHasTitansGrip()
if (TopFit.db.profile.sets[TopFit.setCode].simulateDualWield) then
TopFit.playerCanDualWield = true
end
if (TopFit.db.profile.sets[TopFit.setCode].simulateTitansGrip) then
TopFit.playerCanTitansGrip = true
end
TopFit:InitSemiRecursiveCalculations()
end
function TopFit:PlayerCanDualWield()
local playerClass = select(2, UnitClass("player"))
local specialization = GetSpecialization()
if (playerClass == "ROGUE")
or (playerClass == "DEATHKNIGHT")
or (playerClass == "HUNTER" and UnitLevel("player") >= 20)
or (playerClass == "WARRIOR" and specialization == 2)
or (playerClass == "SHAMAN" and specialization == 2)
or (playerClass == "MONK" and specialization ~= 2) then
return true
end
end
function TopFit:PlayerHasTitansGrip()
local playerClass = select(2, UnitClass("player"))
local specialization = GetSpecialization()
if (playerClass == "WARRIOR") and (UnitLevel("player") >= 38) and (specialization == 2) then
return true
end
end
function TopFit:InitSemiRecursiveCalculations()
-- save equippable items
TopFit.itemListBySlot = TopFit:GetEquippableItems()
TopFit:ReduceItemList()
TopFit.slotCounters = {}
TopFit.currentSlotCounter = 0
TopFit.operationsPerFrame = 500
TopFit.combinationCount = 0
TopFit.bestCombination = nil
TopFit.maxScore = nil
TopFit.firstCombination = true
TopFit.capHeuristics = {}
TopFit.maxRestStat = {}
TopFit.currentCapValues = {}
-- create maximum values for each cap and item slot
for statCode, preferences in pairs(TopFit.Utopia) do
if preferences.active then
TopFit.capHeuristics[statCode] = {}
TopFit.maxRestStat[statCode] = {}
for _, slotID in pairs(TopFit.slots) do
if (TopFit.itemListBySlot[slotID]) then
-- get maximum value contributed to cap in this slot
local maxStat = nil
for _, locationTable in pairs(TopFit.itemListBySlot[slotID]) do
local itemTable = TopFit:GetCachedItem(locationTable.itemLink)
if itemTable then
local thisStat = itemTable.totalBonus[statCode] or 0
if ((thisStat > 0) and ((maxStat == nil) or (thisStat > maxStat))) then
maxStat = thisStat
end
end
end
TopFit.capHeuristics[statCode][slotID] = maxStat
end
end
for i = 0, 20 do
TopFit.maxRestStat[statCode][i] = 0
if (TopFit.capHeuristics[statCode][i]) then
for j = 0, i do
TopFit.maxRestStat[statCode][j] = TopFit.maxRestStat[statCode][j] + TopFit.capHeuristics[statCode][i]
end
end
end
end
end
-- cache up to which slot unique items are available
TopFit.moreUniquesAvailable = {}
local uniqueFound = false
for slotID = 20, 0, -1 do
if uniqueFound then
TopFit.moreUniquesAvailable[slotID] = true
else
TopFit.moreUniquesAvailable[slotID] = false
if (TopFit.itemListBySlot[slotID]) then
for _, locationTable in pairs(TopFit.itemListBySlot[slotID]) do
local itemTable = TopFit:GetCachedItem(locationTable.itemLink)
if itemTable then
for statCode, _ in pairs(itemTable.totalBonus) do
if (string.sub(statCode, 1, 8) == "UNIQUE: ") then
uniqueFound = true
break
end
end
end
end
end
end
end
TopFit.calculationsFrame:SetScript("OnUpdate", TopFit.SemiRecursiveCalculation)
TopFit:ResetProgress()
end
function TopFit:ReduceItemList()
-- remove all non-forced items from item list
for slotID, _ in pairs(TopFit.slotNames) do
local forcedItems = TopFit:GetForcedItems(TopFit.setCode, slotID)
if TopFit.itemListBySlot[slotID] and #forcedItems > 0 then
for i = #(TopFit.itemListBySlot[slotID]), 1, -1 do
local itemTable = TopFit:GetCachedItem(TopFit.itemListBySlot[slotID][i].itemLink)
if not itemTable then
tremove(TopFit.itemListBySlot[slotID], i)
else
local found = false
for _, forceID in pairs(forcedItems) do
if forceID == itemTable.itemID then
found = true
break
end
end
if not found then
tremove(TopFit.itemListBySlot[slotID], i)
end
end
end
end
if (slotID == 17 and #forcedItems > 0) then -- offhand
--TODO: check if forced item is a weapon and remove all weapons from mainhand if player cannot dualwield
-- always remove all 2H-weapons from mainhand
for i = #(TopFit.itemListBySlot[16]), 1, -1 do
if (not TopFit:IsOnehandedWeapon(TopFit.itemListBySlot[16][i].itemLink)) then
tremove(TopFit.itemListBySlot[16], i)
end
end
end
end
-- if enabled, remove armor that is not part of armor specialization
if TopFit.db.profile.sets[TopFit.setCode].forceArmorType and TopFit.characterLevel >= 50 then
local playerClass = select(2, UnitClass("player"))
for slotID, _ in pairs(TopFit.armoredSlots) do
if TopFit.itemListBySlot[slotID] and #(TopFit:GetForcedItems(TopFit.setCode, slotID)) == 0 then
for i = #(TopFit.itemListBySlot[slotID]), 1, -1 do
local itemTable = TopFit:GetCachedItem(TopFit.itemListBySlot[slotID][i].itemLink)
if playerClass == "DRUID" or playerClass == "ROGUE" or playerClass == "MONK" then
if not itemTable or not itemTable.totalBonus["TOPFIT_ARMORTYPE_LEATHER"] then
tremove(TopFit.itemListBySlot[slotID], i)
end
elseif playerClass == "HUNTER" or playerClass == "SHAMAN" then
if not itemTable or not itemTable.totalBonus["TOPFIT_ARMORTYPE_MAIL"] then
tremove(TopFit.itemListBySlot[slotID], i)
end
elseif playerClass == "WARRIOR" or playerClass == "DEATHKNIGHT" or playerClass == "PALADIN" then
if not itemTable or not itemTable.totalBonus["TOPFIT_ARMORTYPE_PLATE"] then
tremove(TopFit.itemListBySlot[slotID], i)
end
end
end
end
end
end
-- remove all items with score <= 0 that are neither forced nor contribute to caps
for slotID, itemList in pairs(TopFit.itemListBySlot) do
if #itemList >= 1 then
for i = #itemList, 1, -1 do
if (TopFit:GetItemScore(itemList[i].itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) <= 0) then
if #(TopFit:GetForcedItems(TopFit.setCode, slotID)) == 0 then
-- check caps
local hasCap = false
for statCode, preferences in pairs(TopFit.Utopia) do
if preferences.active then
local itemTable = TopFit:GetCachedItem(itemList[i].itemLink)
if itemTable and (itemTable.totalBonus[statCode] or -1) > 0 then
hasCap = true
break
end
end
end
if not hasCap then
tremove(itemList, i)
--itemList[i].reason = itemList[i].reason.."score <= 0, no cap contribution and not forced; "
end
end
end
end
end
end
-- remove BoE items
for slotID, itemList in pairs(TopFit.itemListBySlot) do
if #itemList > 0 then
for i = #itemList, 1, -1 do
if itemList[i].isBoE then
tremove(itemList, i)
--itemList[i].reason = itemList[i].reason.."BoE item; "
end
end
end
end
-- preprocess unique items - so we are able to remove items when uniqueness doesn't matter in the next step
-- step 1: sum up the number of unique items for each uniqueness family
local preprocessUniqueness = {}
for slotID, itemList in pairs(TopFit.itemListBySlot) do
if #itemList > 1 then
for i = #itemList, 1, -1 do
local itemTable = TopFit:GetCachedItem(itemList[i].itemLink)
if itemTable then
for stat, value in pairs(itemTable.totalBonus) do
if (string.sub(stat, 1, 8) == "UNIQUE: ") then
preprocessUniqueness[stat] = (preprocessUniqueness[stat] or 0) + value
end
end
end
end
end
end
-- step 2: remember all uniqueness families where uniqueness could actually be violated
local problematicUniqueness = {}
for stat, value in pairs(preprocessUniqueness) do
local _, maxCount = strsplit("*", stat)
maxCount = tonumber(maxCount)
if value > maxCount then
problematicUniqueness[stat] = true
end
end
-- reduce item list: remove items with < cap and < score
for slotID, itemList in pairs(TopFit.itemListBySlot) do
if #itemList > 1 then
for i = #itemList, 1, -1 do
local itemTable = TopFit:GetCachedItem(itemList[i].itemLink)
if not itemTable then
tremove(itemList, i)
else
-- 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
for j = 1, #itemList do
if i ~= j then
local compareTable = TopFit:GetCachedItem(itemList[j].itemLink)
if compareTable and
(TopFit:GetItemScore(itemTable.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) < TopFit:GetItemScore(compareTable.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation)) and
(itemTable.itemEquipLoc == compareTable.itemEquipLoc) then -- especially important for weapons, we do not want to compare 2h and 1h weapons
--TopFit:Debug("score: "..TopFit:GetItemScore(itemTable.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation).."; compareScore: "..TopFit:GetItemScore(compareTable.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation)..
-- " when comparing "..itemTable.itemLink.." with "..compareTable.itemLink)
-- score is greater, see if caps are also better
local allStats = true
for statCode, preferences in pairs(TopFit.Utopia) do
if preferences.active then
if (itemTable.totalBonus[statCode] or 0) > (compareTable.totalBonus[statCode] or 0) then
allStats = false
break
end
end
end
if allStats then
-- items with a problematic uniqueness are special and don't count as a better item
for stat, _ in pairs(itemTable.totalBonus) do
if (string.sub(stat, 1, 8) == "UNIQUE: ") and problematicUniqueness[stat] then
allStats = false
end
end
if allStats then
betterItemExists = betterItemExists + 1
if (betterItemExists >= numBetterItemsNeeded) then
break
end
end
end
end
end
end
if betterItemExists >= numBetterItemsNeeded then
-- remove this item
--TopFit:Debug(itemTable.itemLink.." removed because "..betterItemExists.." better items found.")
tremove(itemList, i)
--itemList[i].reason = itemList[i].reason..betterItemExists.." better items found (setCode: "..(TopFit.setCode or "nil").."; relevantScore: "..(TopFit.ignoreCapsForCalculation or "nil").."); "
end
end
end
end
end
end
function TopFit:SemiRecursiveCalculation()
local operation
local done = false
for operation = 1, TopFit.operationsPerFrame do
if (not done) and (not TopFit.abortCalculation) then
-- set counters to next combination
-- check all nil counters from the end
local currentSlot = 19
local increased = false
while (not increased) and (currentSlot > 0) do
while (TopFit.slotCounters[currentSlot] == nil or TopFit.slotCounters[currentSlot] == #(TopFit.itemListBySlot[currentSlot])) and (currentSlot > 0) do
TopFit.slotCounters[currentSlot] = nil -- reset to "no item"
currentSlot = currentSlot - 1
end
if (currentSlot > 0) then
-- increase combination, starting at currentSlot
TopFit.slotCounters[currentSlot] = TopFit.slotCounters[currentSlot] + 1
if (not TopFit:IsDuplicateItem(currentSlot)) and (TopFit:IsOffhandValid(currentSlot)) then
increased = true
end
else
if TopFit.firstCombination then
TopFit.firstCombination = false
else
-- we're back here, and so we're done
done = true
TopFit.calculationsFrame:SetScript("OnUpdate", nil)
operation = TopFit.operationsPerFrame
-- save a default set of only best-in-slot items
TopFit:SaveCurrentCombination()
-- find best combination that satisfies ALL caps
if (TopFit.bestCombination) then
-- caps are reached, save and equip best combination
--local itemsAlreadyChosen = {}
for slotID, locationTable in pairs(TopFit.bestCombination.items) do
TopFit.itemRecommendations[slotID] = {
locationTable = locationTable,
}
--tinsert(itemsAlreadyChosen, itemTable)
end
TopFit:EquipRecommendedItems()
else
-- caps could not all be reached, calculate without caps instead
if not TopFit.silentCalculation then
TopFit:Print(TopFit.locale.ErrorCapNotReached)
end
TopFit.Utopia = {}
TopFit.ignoreCapsForCalculation = true
TopFit:CalculateRecommendations(TopFit.currentSetName)
return
end
end
end
end
if not done then
-- fill all further slots with first choices again - until caps are reached or unreachable
while (not TopFit:IsCapsReached(currentSlot) or TopFit:MoreUniquesAvailable(currentSlot)) and not TopFit:IsCapsUnreachable(currentSlot) and not TopFit:UniquenessViolated(currentSlot) and (currentSlot < 19) do
currentSlot = currentSlot + 1
if #(TopFit.itemListBySlot[currentSlot]) > 0 then
TopFit.slotCounters[currentSlot] = 1
while TopFit:IsDuplicateItem(currentSlot) or TopFit:UniquenessViolated(currentSlot) or (not TopFit:IsOffhandValid(currentSlot)) do
TopFit.slotCounters[currentSlot] = TopFit.slotCounters[currentSlot] + 1
end
if TopFit.slotCounters[currentSlot] > #(TopFit.itemListBySlot[currentSlot]) then
TopFit.slotCounters[currentSlot] = 0
end
else
TopFit.slotCounters[currentSlot] = 0
end
end
if TopFit:IsCapsReached(currentSlot) and not TopFit:UniquenessViolated(currentSlot) then
-- valid combination, save
TopFit:SaveCurrentCombination()
end
end
end
end
-- update progress
if not done then
local progress = 0
local impact = 1
local slot
for slot = 1, 20 do
-- check if slot has items for calculation
if TopFit.itemListBySlot[slot] then
-- calculate current progress towards finish
local numItemsInSlot = #(TopFit.itemListBySlot[slot]) or 1
local selectedItem = (TopFit.slotCounters[slot] == 0) and (#(TopFit.itemListBySlot[slot]) or 1) or (TopFit.slotCounters[slot] or 1)
if numItemsInSlot == 0 then numItemsInSlot = 1 end
if selectedItem == 0 then selectedItem = 1 end
impact = impact / numItemsInSlot
progress = progress + impact * (selectedItem - 1)
end
end
TopFit:SetProgress(progress)
else
TopFit:SetProgress(1) -- done
end
-- update icons and statistics
if TopFit.bestCombination then
TopFit:SetCurrentCombination(TopFit.bestCombination)
end
if TopFit.abortCalculation then
TopFit.calculationsFrame:SetScript("OnUpdate", nil)
--TopFit:Print("Calculation aborted.")
TopFit.abortCalculation = nil
TopFit.isBlocked = false
TopFit:StoppedCalculation()
end
TopFit:Debug("Current combination count: "..TopFit.combinationCount)
end
function TopFit:IsCapsReached(currentSlot)
local currentValues = {}
local i
for i = 1, currentSlot do
if TopFit.slotCounters[i] ~= nil and TopFit.slotCounters[i] > 0 and TopFit.itemListBySlot[i][TopFit.slotCounters[i]] then
for stat, preferences in pairs(TopFit.Utopia) do
if preferences.active then
local itemTable = TopFit:GetCachedItem(TopFit.itemListBySlot[i][TopFit.slotCounters[i]].itemLink)
if itemTable then
currentValues[stat] = (currentValues[stat] or 0) + (itemTable.totalBonus[stat] or 0)
end
end
end
end
end
for stat, preferences in pairs(TopFit.Utopia) do
if preferences.active and (currentValues[stat] or 0) < preferences.value then
return false
end
end
return true
end
function TopFit:IsCapsUnreachable(currentSlot)
local currentValues = {}
local restValues = {}
local i
for stat, preferences in pairs(TopFit.Utopia) do
if preferences.active then
for i = 1, currentSlot do
if TopFit.slotCounters[i] ~= nil and TopFit.slotCounters[i] > 0 and TopFit.itemListBySlot[i][TopFit.slotCounters[i]] then
local itemTable = TopFit:GetCachedItem(TopFit.itemListBySlot[i][TopFit.slotCounters[i]].itemLink)
if itemTable then
currentValues[stat] = (currentValues[stat] or 0) + (itemTable.totalBonus[stat] or 0)
end
end
end
for i = currentSlot + 1, 19 do
restValues[stat] = (restValues[stat] or 0) + (TopFit.capHeuristics[stat][i] or 0)
end
if (currentValues[stat] or 0) + (restValues[stat] or 0) < preferences.value then
TopFit:Debug("|cffff0000Caps unreachable - "..stat.." reached "..(currentValues[stat] or 0).." + "..(restValues[stat] or 0).." / "..preferences.value)
return true
end
end
end
return false
end
function TopFit:UniquenessViolated(currentSlot)
local currentValues = {}
local i
for i = 1, currentSlot do
if TopFit.slotCounters[i] ~= nil and TopFit.slotCounters[i] > 0 and TopFit.itemListBySlot[i][TopFit.slotCounters[i]] then
for stat, preferences in pairs(TopFit.Utopia) do
if preferences.active then
local itemTable = TopFit:GetCachedItem(TopFit.itemListBySlot[i][TopFit.slotCounters[i]].itemLink)
if itemTable then
currentValues[stat] = (currentValues[stat] or 0) + (itemTable.totalBonus[stat] or 0)
end
end
end
end
end
for stat, value in pairs(currentValues) do
if (string.sub(stat, 1, 8) == "UNIQUE: ") then
local _, maxCount = strsplit("*", stat)
maxCount = tonumber(maxCount)
if value > maxCount then
return true
end
end
end
return false
end
function TopFit:MoreUniquesAvailable(currentSlot)
return TopFit.moreUniquesAvailable[currentSlot]
end
function TopFit:IsDuplicateItem(currentSlot)
-- check if the item is already equipped in another slot
local i
for i = 1, currentSlot - 1 do
if TopFit.slotCounters[i] and TopFit.slotCounters[i] > 0 then
local lTable1 = TopFit.itemListBySlot[i][TopFit.slotCounters[i]]
local lTable2 = TopFit.itemListBySlot[currentSlot][TopFit.slotCounters[currentSlot]]
if lTable1 and lTable2 and lTable1.itemLink == lTable2.itemLink and lTable1.bag == lTable2.bag and lTable1.slot == lTable2.slot then
return true
end
end
end
return false
end
function TopFit:IsOffhandValid(currentSlot)
if currentSlot == 17 then -- offhand slot
if (TopFit.slotCounters[17] ~= nil) and (TopFit.slotCounters[17] > 0) and (TopFit.slotCounters[17] <= #(TopFit.itemListBySlot[17])) then -- offhand is set to something
if (TopFit.slotCounters[16] == nil or TopFit.slotCounters[16] == 0) or -- no Mainhand is forced
(TopFit:IsOnehandedWeapon(TopFit.itemListBySlot[16][TopFit.slotCounters[16]].itemLink)) then -- Mainhand is not a Two-Handed Weapon
local itemTable = TopFit:GetCachedItem(TopFit.itemListBySlot[17][TopFit.slotCounters[17]].itemLink)
if not itemTable then return false end
if (not TopFit.playerCanDualWield) then
if (string.find(itemTable.itemEquipLoc, "WEAPON")) then
-- no weapon in offhand if you cannot dualwield
return false
end
else -- player can dualwield
if (not TopFit:IsOnehandedWeapon(itemTable.itemID)) then
-- no 2h-weapon in offhand
return false
end
end
else
-- a 2H-Mainhand is set, there can be no offhand!
return false
end
end
end
return true
end
function TopFit:SaveCurrentCombination()
TopFit.combinationCount = TopFit.combinationCount + 1
local cIC = {
items = {},
totalScore = 0,
totalStats = {},
}
local itemsAlreadyChosen = {}
local i
for i = 1, 20 do
local itemTable, locationTable = nil, nil
local stat, slotTable
if TopFit.slotCounters[i] ~= nil and TopFit.slotCounters[i] > 0 then
locationTable = TopFit.itemListBySlot[i][TopFit.slotCounters[i]]
itemTable = TopFit:GetCachedItem(locationTable.itemLink)
else
-- choose highest valued item for otherwise empty slots, if possible
locationTable = TopFit:CalculateBestInSlot(itemsAlreadyChosen, false, i)
if locationTable then
itemTable = TopFit:GetCachedItem(locationTable.itemLink)
end
if (itemTable) then
-- special cases for main an offhand (to account for dualwielding and Titan's Grip)
if (i == 16) then
-- check if offhand is forced
if TopFit.slotCounters[17] then
-- use 1H-weapon in Mainhand (or a titan's grip 2H, if applicable)
locationTable = TopFit:CalculateBestInSlot(itemsAlreadyChosen, false, i, TopFit.setCode, function(locationTable) return TopFit:IsOnehandedWeapon(locationTable.itemLink) end)
if locationTable then
itemTable = TopFit:GetCachedItem(locationTable.itemLink)
end
else
-- choose best main- and offhand combo
if not TopFit:IsOnehandedWeapon(itemTable.itemID) then
-- see if a combination of main and offhand would have a better score
local bestMainScore, bestOffScore = 0, 0
local bestOff = nil
local bestMain = TopFit:CalculateBestInSlot(itemsAlreadyChosen, false, i, TopFit.setCode, function(locationTable) return TopFit:IsOnehandedWeapon(locationTable.itemLink) end)
if bestMain ~= nil then
bestMainScore = (TopFit:GetItemScore(bestMain.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) or 0)
end
if (TopFit.playerCanDualWield) then
-- any non-two-handed offhand is fine
bestOff = TopFit:CalculateBestInSlot(TopFit:JoinTables(itemsAlreadyChosen, {bestMain}), false, i + 1, TopFit.setCode, function(locationTable) return TopFit:IsOnehandedWeapon(locationTable.itemLink) end)
else
-- offhand may not be a weapon (only shield, other offhand...)
bestOff = TopFit:CalculateBestInSlot(TopFit:JoinTables(itemsAlreadyChosen, {bestMain}), false, i + 1, TopFit.setCode, function(locationTable) local itemTable = TopFit:GetCachedItem(locationTable.itemLink); if not itemTable or string.find(itemTable.itemEquipLoc, "WEAPON") then return false else return true end end)
end
if bestOff ~= nil then
bestOffScore = (TopFit:GetItemScore(bestOff.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) or 0)
end
-- alternatively, calculate offhand first, then mainhand
local bestMainScore2, bestOffScore2 = 0, 0
local bestMain2 = nil
local bestOff2 = nil
if (TopFit.playerCanDualWield) then
-- any non-two-handed offhand is fine
bestOff2 = TopFit:CalculateBestInSlot(itemsAlreadyChosen, false, i + 1, TopFit.setCode, function(locationTable) return TopFit:IsOnehandedWeapon(locationTable.itemLink) end)
else
-- offhand may not be a weapon (only shield, other offhand...)
bestOff2 = TopFit:CalculateBestInSlot(itemsAlreadyChosen, false, i + 1, TopFit.setCode, function(locationTable) local itemTable = TopFit:GetCachedItem(locationTable.itemLink); if not itemTable or string.find(itemTable.itemEquipLoc, "WEAPON") then return false else return true end end)
end
if bestOff2 ~= nil then
bestOffScore2 = (TopFit:GetItemScore(bestOff2.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) or 0)
end
bestMain2 = TopFit:CalculateBestInSlot(TopFit:JoinTables(itemsAlreadyChosen, {bestOff2}), false, i, TopFit.setCode, function(locationTable) return TopFit:IsOnehandedWeapon(locationTable.itemLink) end)
if bestMain2 ~= nil then
bestMainScore2 = (TopFit:GetItemScore(bestMain2.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) or 0)
end
local maxScore = (TopFit:GetItemScore(itemTable.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) or 0)
if (maxScore < (bestMainScore + bestOffScore)) then
-- main- + offhand is better, use the one-handed mainhand
locationTable = bestMain
if locationTable then
itemTable = TopFit:GetCachedItem(locationTable.itemLink)
end
maxScore = bestMainScore + bestOffScore
--TopFit:Debug("Choosing Mainhand "..itemTable.itemLink)
end
if (maxScore < (bestMainScore2 + bestOffScore2)) then
-- main- + offhand is better, use the one-handed mainhand
locationTable = bestMain2
if locationTable then
itemTable = TopFit:GetCachedItem(locationTable.itemLink)
end
--TopFit:Debug("Choosing Mainhand "..itemTable.itemLink)
end
end -- if mainhand would not be twohanded anyway, it can just be used
end
elseif (i == 17) then
-- check if mainhand is empty or one-handed
if (not cIC.items[i - 1]) or (TopFit:IsOnehandedWeapon(cIC.items[i - 1].itemLink)) then
-- check if player can dual wield
if TopFit.playerCanDualWield then
-- only use 1H-weapons in Offhand
locationTable = TopFit:CalculateBestInSlot(itemsAlreadyChosen, false, i, TopFit.setCode, function(locationTable) return TopFit:IsOnehandedWeapon(locationTable.itemLink) end)
if locationTable then
itemTable = TopFit:GetCachedItem(locationTable.itemLink)
end
else
-- player cannot dualwield, only use offhands which are not weapons
locationTable = TopFit:CalculateBestInSlot(itemsAlreadyChosen, false, i, TopFit.setCode, function(locationTable) local itemTable = TopFit:GetCachedItem(locationTable.itemLink); if not itemTable or string.find(itemTable.itemEquipLoc, "WEAPON") then return false else return true end end)
if locationTable then
itemTable = TopFit:GetCachedItem(locationTable.itemLink)
end
end
else
-- Two-handed mainhand means we leave offhand empty
locationTable = nil
itemTable = nil
end
end
end
end
if locationTable and itemTable then -- slot will be filled
tinsert(itemsAlreadyChosen, locationTable)
cIC.items[i] = locationTable
cIC.totalScore = cIC.totalScore + (TopFit:GetItemScore(itemTable.itemLink, TopFit.setCode, TopFit.ignoreCapsForCalculation) or 0)
-- add total stats
local stat, value
for stat, value in pairs(itemTable.totalBonus) do
cIC.totalStats[stat] = (cIC.totalStats[stat] or 0) + value
end
end
end
-- check all caps one last time and see if all are reached
local satisfied = true
for stat, preferences in pairs(TopFit.Utopia) do
if preferences.active and ((not cIC.totalStats[stat]) or (cIC.totalStats[stat] < tonumber(preferences["value"]))) then
satisfied = false
break
end
end
-- check if any uniqueness contraints are broken
if not TopFit.ignoreCapsForCalculation then
for stat, value in pairs(cIC.totalStats) do
if (string.sub(stat, 1, 8) == "UNIQUE: ") then
local _, maxCount = strsplit("*", stat)
maxCount = tonumber(maxCount)
if value > maxCount then
satisfied = false
break
end
end
end--]]
end
-- check if it's better than old best
if ((satisfied) and ((TopFit.maxScore == nil) or (TopFit.maxScore < cIC.totalScore))) then
TopFit.maxScore = cIC.totalScore
TopFit.bestCombination = cIC
TopFit.debugSlotCounters = {} -- save slot counters for best combination
for i = 1, 20 do
TopFit.debugSlotCounters[i] = TopFit.slotCounters[i]
end
end
end
-- now with assertion as optional parameter
function TopFit:CalculateBestInSlot(itemsAlreadyChosen, insert, sID, setCode, assertion)
if not setCode then setCode = TopFit.setCode end
-- get best item(s) for each equipment slot
local bis = {}
local itemListBySlot = TopFit.itemListBySlot or TopFit:GetEquippableItems()
for slotID, itemsTable in pairs(itemListBySlot) do
if ((not sID) or (sID == slotID)) then -- use single slot if sID is set, or all slots
bis[slotID] = {}
local maxScore = nil
-- iterate all items of given location
for _, locationTable in pairs(itemsTable) do
local itemTable = TopFit:GetCachedItem(locationTable.itemLink)
if (itemTable and ((maxScore == nil) or (maxScore < TopFit:GetItemScore(itemTable.itemLink, setCode, TopFit.ignoreCapsForCalculation))) -- score
and (itemTable.itemMinLevel <= TopFit.characterLevel or locationTable.isVirtual)) -- character level
and (not assertion or assertion(locationTable)) then -- optional assertion is true
-- also check if item has been chosen already (so we don't get the same ring / trinket twice)
local found = false
if (itemsAlreadyChosen) then
for _, lTable in pairs(itemsAlreadyChosen) do
if ((not lTable.bag and not lTable.slot) or ((lTable.bag == locationTable.bag) and (lTable.slot == locationTable.slot))) and (lTable.itemLink == locationTable.itemLink) then
found = true
end
end
end
if not found then
bis[slotID].locationTable = locationTable
maxScore = TopFit:GetItemScore(itemTable.itemLink, setCode, TopFit.ignoreCapsForCalculation)
end
end
end
if (not bis[slotID].locationTable) then
-- remove dummy table if no item has been found
bis[slotID] = nil
else
-- mark this item as used
if (itemsAlreadyChosen and insert) then
tinsert(itemsAlreadyChosen, bis[slotID].locationTable)
end
end
end
end
if (not sID) then
return bis
else
-- return only the slot item's table (if it exists)
if (bis[sID]) then
return bis[sID].locationTable
else
return nil
end
end
end
function TopFit:IsOnehandedWeapon(itemID)
_, _, _, _, _, class, subclass, _, equipSlot, _, _ = GetItemInfo(itemID)
if equipSlot and string.find(equipSlot, "2HWEAPON") then
if (TopFit.playerCanTitansGrip) then
local polearms = select(7, GetAuctionItemSubClasses(1))
local staves = select(10, GetAuctionItemSubClasses(1))
local fishingPoles = select(17, GetAuctionItemSubClasses(1))
if (subclass == polearms) or -- Polearms
(subclass == staves) or -- Staves
(subclass == fishingPoles) then -- Fishing Poles
return false
end
else
return false
end
elseif equipSlot and string.find(equipSlot, "RANGED") then
local wands = select(16, GetAuctionItemSubClasses(1))
if (subclass == wands) then
return true
end
return false
end
return true
end