-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOutfitterOptimize.lua
1266 lines (969 loc) · 34.7 KB
/
OutfitterOptimize.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
993
994
995
996
997
998
999
1000
----------------------------------------
-- Specialty items for outfit optimization
----------------------------------------
Outfitter.cArgentDawnTrinkets =
{
{Code = 13209, SubCode = 0}, -- Seal of the Dawn
{Code = 19812, SubCode = 0}, -- Rune of the Dawn
{Code = 12846, SubCode = 0}, -- Argent Dawn Commission
}
Outfitter.cStatIDItems =
{
ArgentDawn = Outfitter.cArgentDawnTrinkets,
}
----------------------------------------
-- Linear optimization (item-to-item comparisons)
----------------------------------------
function Outfitter:GenerateSmartOutfit(pName, pStatConfig, pInventoryCache, pAllowEmptyOutfit, pCompletionFunc)
pInventoryCache:ResetIgnoreItemFlags()
if type(pStatConfig) == "string" then
local vStatID = pStatConfig
-- Backward compatibility
if vStatID == "Fishing" then
vStatID = "FISHING"
end
-- Hard-coded item lists
if vStatID then
local vStatIDItems = self.cStatIDItems[vStatID]
if vStatIDItems then
local vOutfit = self:NewEmptyOutfit(pName)
self:FindAndAddItemsToOutfit(vOutfit, nil, vStatIDItems, pInventoryCache)
vOutfit.StatID = vStatID
if pCompletionFunc then
pCompletionFunc(vOutfit)
end
return vOutfit
end
end
--
pStatConfig = {{StatID = vStatID}}
end
-- Get the stat objects
local vStatConfig = {}
for _, vConfig in ipairs(pStatConfig) do
local vStat = self:GetStatByID(vConfig.StatID)
if not vStat then
Outfitter:ErrorMessage("Unknown stat ID: %s", tostring(vConfig.StatID))
return
end
table.insert(vStatConfig, {Stat = vStat, StatID = vConfig.StatID, MinValue = vConfig.MinValue, MaxValue = vConfig.MaxValue})
end
-- Determine if this is complex
local vComplex = #vStatConfig > 1
or vStatConfig[1].Stat.Complex
or vStatConfig[1].MinValue
or vStatConfig[1].MaxValue
--
local vOutfit
if vComplex then
local vMultiStat = {Config = vStatConfig}
setmetatable(vMultiStat, Outfitter._MultiStatMetaTable)
vOutfit = self:FindGeneticCombination(
pName,
pInventoryCache,
nil,
vMultiStat, vStatConfig, pCompletionFunc)
else
vOutfit = self:NewEmptyOutfit(pName)
self:AddItemsWithStatToOutfit(vOutfit, vStatConfig[1].Stat, pInventoryCache)
if not pAllowEmptyOutfit
and vOutfit:IsEmpty() then
return nil
end
end
if vOutfit then
vOutfit.StatConfig = pStatConfig
if pCompletionFunc then
pCompletionFunc(vOutfit)
end
end
return vOutfit
end
function Outfitter:AddItemsWithStatToOutfit(pOutfit, pStat, pInventoryCache)
local vItemStats
if not pInventoryCache then
return
end
for vInventorySlot, vItems in pairs(pInventoryCache.ItemsBySlot) do
for vIndex, vItem in ipairs(vItems) do
-- Only consider items which aren't BoE (ie, must be already bound)
if not vItem:GetBoE() and vItem:GetMeetsRequirements() then
-- Score the item
local vScore = pStat:GetItemScore(vItem)
if vScore then
-- Calculate the slot
local vSlotName = vItem.MetaSlotName
if not vSlotName then
vSlotName = vItem.ItemSlotName
end
-- Change the slot to Weapon0 for two-handers
if vItem.InvType == "INVTYPE_2HWEAPON"
and not self:ItemUsesBothWeaponSlots(vItem) then
vSlotName = "Weapon0Slot"
end
-- Compare and add the item
self:AddOutfitStatItemIfBetter(pOutfit, vSlotName, vItem, pStat, vScore)
end -- vScore
end -- not BoE
end -- for vItems
end -- for ItemsBySlot
-- Collapse the meta slots (currently just 2H vs. 1H/OH)
self:CollapseMetaSlotsIfBetter(pOutfit, pStat)
end
function Outfitter:AddOutfitStatItemIfBetter(pOutfit, pSlotName, pItemInfo, pStat, pScore)
local vCurrentItem = pOutfit:GetItem(pSlotName)
local vAlternateSlotName = self.cHalfAlternateStatSlot[pSlotName]
local vCurrentScore = vCurrentItem and pStat:GetItemScore(vCurrentItem)
if self.Debug.Optimize then
self:DebugMessage("AddOutfitStatItemIfBetter for %s: %s (%s) compared to %s (%s)", tostring(pSlotName), tostring(vCurrentItem and vCurrentItem.Name), tostring(vCurrentScore), tostring(pItemInfo.Name), tostring(pScore))
end
if not vCurrentScore
or pScore > vCurrentScore then
-- If we're bumping the current item, see if it should be moved to the alternate slot
if vCurrentScore
and vAlternateSlotName then
if self.Debug.Optimize then
self:DebugMessage("AddOutfitStatItemIfBetter: Trying to push slot %s to slot %s", tostring(pSlotName), tostring(vAlternateSlotName))
end
self:AddOutfitStatItemIfBetter(pOutfit, vAlternateSlotName, vCurrentItem, pStat, vCurrentScore)
end
if self.Debug.Optimize then
self:DebugMessage("AddOutfitStatItemIfBetter: putting %s (%s) into slot %s replacing %s (%s)", tostring(pItemInfo.Name), tostring(pScore), tostring(pSlotName), tostring(vCurrentItem and vCurrentItem.Name), tostring(vCurrentScore))
end
self:AddOutfitStatItem(pOutfit, pSlotName, pItemInfo, pStat, pScore)
else
if not vAlternateSlotName then
return
end
return self:AddOutfitStatItemIfBetter(pOutfit, vAlternateSlotName, pItemInfo, pStat, pScore)
end
end
function Outfitter:AddOutfitStatItem(pOutfit, pSlotName, pItemInfo, pStat, pScore)
if not pSlotName then
Outfitter:ErrorMessage("AddOutfitStatItem: SlotName is nil for %s", tostring(pItemName))
return
end
if not pStat then
Outfitter:ErrorMessage("AddOutfitStatItem: Stat is nil for %s", tostring(pItemName))
return
end
pOutfit:AddItem(pSlotName, pItemInfo)
end
function Outfitter:CollapseMetaSlotsIfBetter(pOutfit, pStat)
if self.Debug.Optimize then
self:DebugMessage("CollapseMetaSlotsIfBetter: %s", tostring(pStat.ID))
self:DebugOutfitTable(pOutfit)
end
-- Compare the weapon slot with the 1H/OH slots
local vWeapon0Item = pOutfit:GetItem("Weapon0Slot")
local vWeapon1Item = pOutfit:GetItem("Weapon1Slot")
if vWeapon0Item or vWeapon1Item then
-- Try the various combinations of MH/OH/W0/W1
local v1HItem = pOutfit:GetItem("MainHandSlot")
local vOHItem = pOutfit:GetItem("SecondaryHandSlot")
local vCombinations
if pStat.ID ~= "ITEM_LEVEL" then
vCombinations =
{
{MainHand = v1HItem, SecondaryHand = vOHItem, AllowEmptyMainHand = true},
{MainHand = v1HItem, SecondaryHand = vWeapon0Item, AllowEmptyMainHand = false},
{MainHand = v1HItem, SecondaryHand = vWeapon1Item, AllowEmptyMainHand = false},
{MainHand = vWeapon0Item, SecondaryHand = vOHItem, AllowEmptyMainHand = true},
{MainHand = vWeapon1Item, SecondaryHand = vOHItem, AllowEmptyMainHand = true},
{MainHand = vWeapon0Item, SecondaryHand = vWeapon1Item, AllowEmptyMainHand = false},
}
else
vCombinations =
{
{MainHand = v1HItem, SecondaryHand = vWeapon1Item, AllowEmptyMainHand = false},
{MainHand = vWeapon0Item, SecondaryHand = vOHItem, AllowEmptyMainHand = false},
{MainHand = vWeapon1Item, SecondaryHand = vOHItem, AllowEmptyMainHand = false},
}
end
local vBestCombinationIndex = nil
local vBestCombinationValue = nil
for vIndex, vCombination in ipairs(vCombinations) do
local vCombination = vCombinations[vIndex]
-- Ignore combinations where the main hand is empty if
-- that's not allowed with this combination
if vCombination.AllowEmptyMainHand
or vCombination.MainHand then
local vCombinationValue = self:AddScores(vCombination.MainHand, vCombination.SecondaryHand, pStat)
if not vBestCombinationIndex
or vCombinationValue > vBestCombinationValue then
vBestCombinationIndex = vIndex
vBestCombinationValue = vCombinationValue
end
end
end
if vBestCombinationIndex then
local vCombination = vCombinations[vBestCombinationIndex]
pOutfit:SetItem("MainHandSlot", vCombination.MainHand)
pOutfit:SetItem("SecondaryHandSlot", vCombination.SecondaryHand)
end
pOutfit:RemoveItem("Weapon0Slot")
pOutfit:RemoveItem("Weapon1Slot")
end
-- Compare the 2H slot with the 1H/OH slots
local v2HItem = pOutfit:GetItem("TwoHandSlot")
if v2HItem then
local v1HItem = pOutfit:GetItem("MainHandSlot")
local vOHItem = pOutfit:GetItem("SecondaryHandSlot")
local v1HOHScore = (v1HItem and pStat:GetItemScore(v1HItem) or 0) + (vOHItem and pStat:GetItemScore(vOHItem) or 0)
local v2HScore = pStat:GetItemScore(v2HItem)
if v2HScore
and v2HScore > v1HOHScore then
pOutfit:SetItem("MainHandSlot", v2HItem)
pOutfit:RemoveItem("SecondaryHandSlot")
end
pOutfit:RemoveItem("TwoHandSlot")
end
end
function Outfitter:AddScores(pItem1, pItem2, pStat)
return (pItem1 and pStat:GetItemScore(pItem1) or 0)
+ (pItem2 and pStat:GetItemScore(pItem2) or 0)
end
----------------------------------------
-- Combinatorial progress dialog
----------------------------------------
function Outfitter:BeginCombiProgress(pName, pNumCombos, pCancelFunc, pHighSpeedFunc)
if not self.CombiProgressDialog then
self.CombiProgressDialog = Outfitter:New(self._CombiProgressDialog)
end
self.NumCombos = pNumCombos
self.CombiProgressDialog:SetTitle(pName)
self.CombiProgressDialog:SetCancelFunc(pCancelFunc)
self.CombiProgressDialog:SetHighSpeedFunc(pHighSpeedFunc)
self.CombiProgressDialog:SetHighSpeed(false)
self.CombiProgressDialog:Show()
end
function Outfitter:FormatThousands(pNumber)
local vResult = tostring(math.floor(pNumber + 0.5)):reverse():gsub("(%d%d%d)", "%1,"):reverse()
return vResult:sub(1, 1) == "," and vResult:sub(2) or vResult
end
function Outfitter:UpdateCombiProgress(pComboIndex)
self.CombiProgressDialog:SetProgress(pComboIndex / self.NumCombos)
self.CombiProgressDialog:SetProgressText(format("%s of %s", self:FormatThousands(pComboIndex), self:FormatThousands(self.NumCombos)))
end
function Outfitter:EndCombiProgress()
Outfitter.CombiProgressDialog:Hide()
end
function Outfitter:ItemContainsStats(pItem, pFilterStats)
for vStatID, _ in pairs(pFilterStats) do
if pItem.Stats[vStatID] then
return true
end
end
return false
end
----------------------------------------
-- Genetic optimization
----------------------------------------
function Outfitter:FindGeneticCombination(pName, pInventoryCache, pFilterStats, pStat, pStatParams, pCompletionFunc)
assert(pCompletionFunc)
self.FindGeneticCombinationCoroutineRef = coroutine.create(self.FindGeneticCombinationThread)
local vResult, vMessage = coroutine.resume(self.FindGeneticCombinationCoroutineRef, self, pName, pInventoryCache, pFilterStats, pStat, pStatParams, pCompletionFunc)
if not vResult then
self:ErrorMessage(vMessage)
elseif self.FindGeneticCombinationCoroutineRef then
self.SchedulerLib:ScheduleRepeatingTask(0, self.RunGeneticThread, self)
end
end
function Outfitter:RunGeneticThread()
local vResult, vMessage = coroutine.resume(self.FindGeneticCombinationCoroutineRef, self)
if not vResult then
self:ErrorMessage(vMessage)
end
if not vResult
or not self.FindGeneticCombinationCoroutineRef then
self.FindGeneticCombinationCoroutineRef = nil
self.SchedulerLib:UnscheduleTask(self.RunGeneticThread, self)
end
end
function Outfitter:CompareCompoundScores(pScore1, pScore2, pStatParams)
for vIndex, vScore1 in ipairs(pScore1) do
local vScore2 = pScore2[vIndex]
local vStatConfig = pStatParams[vIndex]
if vScore1 == vScore2 then
-- Do nothing if they're equal
elseif vStatConfig and vStatConfig.MinValue then
if vScore1 >= vStatConfig.MinValue
and vScore2 >= vStatConfig.MinValue then
return vScore1 < vScore2 -- The one closer to the minValue is better
else
return vScore1 > vScore2 -- The one closer to the minValue is better
end
elseif vStatConfig and vStatConfig.MaxValue then
if vScore1 <= vStatConfig.MaxValue
and vScore2 <= vStatConfig.MaxValue then
return vScore1 > vScore2 -- The one closer to the maxValue is better
else
return vScore1 < vScore2 -- The one closer to the maxValue is better
end
elseif vScore1 > vScore2 then
return true
elseif vScore1 < vScore2 then
return false
end
end
return false
end
function Outfitter:SortCitizens(pCitizens, pStatParams)
if type(pCitizens[1].GeneticScore) == "table" then
table.sort(pCitizens, function (pOutfit1, pOutfit2)
return self:CompareCompoundScores(pOutfit1.GeneticScore, pOutfit2.GeneticScore, pStatParams)
end)
else
table.sort(pCitizens, function (pOutfit1, pOutfit2)
return pOutfit1.GeneticScore > pOutfit2.GeneticScore
end)
end
end
function Outfitter:FindGeneticCombinationThread(pName, pInventoryCache, pFilterStats, pStat, pStatParams, pCompletionFunc)
assert(pCompletionFunc)
local vMaxGenerations = 1000
local vPopulation = 300
local vNumParents = 25
local vCitizens = {}
local vNumOutfitsGenerated = 0
local vMutationRate = 0.2
local vMaxNoChangeCount = 100
pInventoryCache:ResetIgnoreItemFlags()
local vOutfitIterator = Outfitter:New(self._OutfitIterator, pName, pInventoryCache, pFilterStats)
local vMaxYieldTime = 0.02
local vCancel
self:BeginCombiProgress(
string.format("Genetically optimizing %s for %s", pName, pStat.Name),
vMaxNoChangeCount,
function ()
vCancel = true
end,
function (pSet)
if pSet then
vMaxYieldTime = 0.1
else
vMaxYieldTime = 0.02
end
end)
--
local vStartTime = GetTime()
local vLoopCountYieldFactor = 5000
if pStat.Begin then
pStat:Begin(pInventoryCache)
end
-- Generate the initial population and their scores
for vCitizenIndex = 1, vPopulation do
local vOutfit = vOutfitIterator:GenerateRandomOutfit()
vOutfit.GeneticScore = Outfitter:GetOutfitScoreWithEmptySlots(vOutfit, pStat, pStatParams)
table.insert(vCitizens, vOutfit)
vNumOutfitsGenerated = vNumOutfitsGenerated + 1
end
-- Sort the initial population by score
self:SortCitizens(vCitizens, pStatParams)
--
local vYieldTime = GetTime() + vMaxYieldTime
local vLoopCount = vLoopCountYieldFactor * vMaxYieldTime
local vPreviousBestScore, vBestScore
local vNoChangeCount = 0
local vPreviousNoChangeCount
for vGenerationIndex = 1, vMaxGenerations do
-- Mate the best parents randomly to generate new citizens
for vChildIndex = vNumParents + 1, vPopulation do
local vParentIndex1 = math.random(vNumParents)
local vParentIndex2 = math.random(vNumParents)
while vParentIndex2 == vParentIndex1 do
vParentIndex2 = math.random(vNumParents)
end
local vChildOutfit = vOutfitIterator:SpliceOutfits(vCitizens[vParentIndex1], vCitizens[vParentIndex2])
vOutfitIterator:MutateOutfit(vChildOutfit, vMutationRate)
vChildOutfit.GeneticScore = Outfitter:GetOutfitScoreWithEmptySlots(vChildOutfit, pStat, pStatParams)
vCitizens[vChildIndex] = vChildOutfit
vNumOutfitsGenerated = vNumOutfitsGenerated + 1
vLoopCount = vLoopCount - 1
if GetTime() >= vYieldTime or vLoopCount <= 0 then
coroutine.yield()
vYieldTime = GetTime() + vMaxYieldTime
vLoopCount = vLoopCountYieldFactor * vMaxYieldTime
end
if vCancel then
break
end
end
-- Sort the new population by score
self:SortCitizens(vCitizens, pStatParams)
--
if not vPreviousNoChangeCount
or vNoChangeCount > vPreviousNoChangeCount then
self:UpdateCombiProgress(vNoChangeCount)
vPreviousNoChangeCount = vNoChangeCount
end
vBestScore = vCitizens[1].GeneticScore
if type(vBestScore) == "table" then
vBestScore = table.concat(vBestScore, ", ")
end
if vPreviousBestScore == vBestScore then
vNoChangeCount = vNoChangeCount + 1
if vNoChangeCount > vMaxNoChangeCount then
break
end
else
vNoChangeCount = 0
vPreviousBestScore = vBestScore
end
self.CombiProgressDialog:SetDescription(string.format("Best score: %s", tostring(vBestScore)))
if vCancel then
break
end
end
if pStat.End then
pStat:End(pInventoryCache)
end
local vEndTime = GetTime()
--
self:EndCombiProgress()
self.FindGeneticCombinationCoroutineRef = nil
if not vCancel then
local vElapsedTime = vEndTime - vStartTime
Outfitter:NoteMessage(
"Checked %s outfits in %s seconds (%s outfits per second)",
Outfitter:FormatThousands(vNumOutfitsGenerated),
math.floor(vElapsedTime),
Outfitter:FormatThousands(vNumOutfitsGenerated / vElapsedTime))
Outfitter:NoteMessage("Best outfit score: %s", tostring(vBestScore))
pCompletionFunc(vCitizens[1])
end
end
function Outfitter:GetOutfitScoreWithEmptySlots(pOutfit, pStat, pStatParams)
local vGeneticScore = pStat:GetOutfitScore(pOutfit, pStatParams)
if type(vGeneticScore) ~= "table" then
vGeneticScore = {vGeneticScore}
end
table.insert(vGeneticScore, pOutfit:GetNumEmptySlots())
return vGeneticScore
end
----------------------------------------
Outfitter._CombiProgressDialog = {}
----------------------------------------
function Outfitter._CombiProgressDialog:New()
return Outfitter:New(Outfitter.UIElementsLib._FloatingWindow)
end
function Outfitter._CombiProgressDialog:Construct()
self:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
self:SetWidth(300)
self:SetHeight(60)
local vMargin = 8
self.CancelButton = Outfitter:New(Outfitter.UIElementsLib._PushButton, self.ContentFrame, CANCEL, 80)
self.CancelButton:SetPoint("TOPRIGHT", self.ContentFrame, "TOPRIGHT", -vMargin, -vMargin)
self.CancelButton:SetScript("OnClick", function ()
self.CancelFunc()
end)
self.ProgressBar = Outfitter:New(Outfitter.UIElementsLib._ProgressBar, self.ContentFrame)
self.ProgressBar:SetPoint("TOPLEFT", self.ContentFrame, "TOPLEFT", vMargin, -vMargin)
self.ProgressBar:SetPoint("RIGHT", self.CancelButton, "LEFT", -4, 0)
self.HighSpeed = Outfitter:New(Outfitter.UIElementsLib._CheckButton, self.ContentFrame, "High speed")
self.HighSpeed:SetPoint("TOPLEFT", self.ProgressBar, "BOTTOMLEFT", 0, -4)
self.HighSpeed:SetScript("OnClick", function (pButton)
self.HighSpeedFunc(pButton:GetChecked())
end)
self.Description = self.ContentFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
self.Description:SetJustifyH("RIGHT")
self.Description:SetJustifyV("TOP")
self.Description:SetPoint("LEFT", self.HighSpeed.Title, "RIGHT", 4, 0)
self.Description:SetPoint("RIGHT", self.ContentFrame, "RIGHT", -vMargin, 0)
self.TitleBar:SetCloseFunc(function () self.CancelFunc() end)
end
function Outfitter._CombiProgressDialog:SetDescription(pDescription)
self.Description:SetText(pDescription)
end
function Outfitter._CombiProgressDialog:SetCancelFunc(pCancelFunc)
self.CancelFunc = pCancelFunc
end
function Outfitter._CombiProgressDialog:SetHighSpeed(pHighSpeed)
self.HighSpeed:SetChecked(pHighSpeed)
end
function Outfitter._CombiProgressDialog:SetHighSpeedFunc(pHighSpeedFunc)
self.HighSpeedFunc = pHighSpeedFunc
end
function Outfitter._CombiProgressDialog:SetProgress(pProgress)
self.ProgressBar:SetProgress(pProgress)
end
function Outfitter._CombiProgressDialog:SetProgressText(pText)
self.ProgressBar:SetText(pText)
end
----------------------------------------
Outfitter._OutfitIterator = {}
----------------------------------------
function Outfitter._OutfitIterator:Construct(pName, pInventoryCache, pFilterStats)
self.Name = pName
self.Slots = {}
self.NumCombinations = 1
if Outfitter.Debug.Optimize then
print(pFilterStats)
end
for vInventorySlot, vItems in pairs(pInventoryCache.ItemsBySlot) do
local vNumSlotItems = 0
local vNumItems = #vItems
if vNumItems > 0 then
-- Filter the items by stat
local vFilteredItems = nil
if pFilterStats then
vNumItems = 0
for vItemIndex, vItem in ipairs(vItems) do
if vItem:GetMeetsRequirements() then
for _, vStat in ipairs(pFilterStats) do
if vStat:GetItemScore(vItem) then
if not vFilteredItems then
vFilteredItems = {}
end
table.insert(vFilteredItems, vItem)
vNumItems = vNumItems + 1
vNumSlotItems = vNumSlotItems + 1
break
end -- if GetItemScore
end -- for pFileStats
end -- GetMeetsRequirements
end
else
vFilteredItems = vItems
end
-- Add the filtered list
if vFilteredItems then
table.insert(self.Slots, {ItemSlotName = vInventorySlot, Items = vFilteredItems, Index = 1, MaxIndex = vNumItems})
self.NumCombinations = self.NumCombinations * vNumItems
-- Duplicate the list for slots with alternates (finger, weapon, trinket)
local vAltInventorySlot = Outfitter.cHalfAlternateStatSlot[vInventorySlot]
if vAltInventorySlot then
table.insert(self.Slots, {ItemSlotName = vAltInventorySlot, Items = vFilteredItems, Index = 1, MaxIndex = vNumItems})
self.NumCombinations = self.NumCombinations * vNumItems
end
end
if Outfitter.Debug.Optimize then
Outfitter:DebugMessage("%s has %s items", vInventorySlot, vNumSlotItems)
end
end
end
end
function Outfitter._OutfitIterator:Outfits()
-- Reset the slots
for vSlotIndex, vSlotIterator in ipairs(self.Slots) do
vSlotIterator.Index = 1
end
return self.Next, self, 0, nil
end
function Outfitter._OutfitIterator:Next(pPreviousIndex)
if not self.Slots[1] then
return nil
end
if pPreviousIndex == 0 then
return 1, self:GetOutfit()
end
for vSlotIndex, vSlotIterator in ipairs(self.Slots) do
vSlotIterator.Index = vSlotIterator.Index + 1
if vSlotIterator.Index <= vSlotIterator.MaxIndex then
return pPreviousIndex + 1, self:GetOutfit()
end
vSlotIterator.Index = 1
end
return nil -- Couldn't increment
end
function Outfitter._OutfitIterator:GetOutfit()
local vOutfit = Outfitter:NewEmptyOutfit(self.Name)
for _, vItems in ipairs(self.Slots) do
if vItems.Index > 0 then
local vItem = vItems.Items[vItems.Index]
local vAltSlotName = Outfitter.cFullAlternateStatSlot[vItems.ItemSlotName]
-- Only include the item if it isn't already being tried in an alternate slot (ie, trinket0 vs. trinket1)
if not vAltSlotName
or vOutfit.Items[vAltSlotName] ~= vItem then
vOutfit.Items[vItems.ItemSlotName] = vItem
end
end
end
if vOutfit.Items.MainHandSlot
and vOutfit.Items.MainHandSlot.InvType == "INVTYPE_2HWEAPON"
and not Outfitter.CanDualWield2H then
vOutfit.Items.SecondaryHandSlot = nil
end
return vOutfit
end
-- Genetic methods
function Outfitter._OutfitIterator:SetRandomItemForSlot(pOutfit, pItems)
local vNumItems = #pItems.Items
-- Set the slot to empty if there are no items for it
if vNumItems < 1 then
pOutfit.Items[pItems.ItemSlotName] = nil
return
end
-- Pick a random item (zero means that an empty slot was chosen)
local vIndex = math.random(vNumItems + 1) - 1
local vItem = vIndex > 0 and pItems.Items[vIndex] or nil
-- All done if there's no alternate slot or that
-- slot has a different item
local vAltSlotName = Outfitter.cFullAlternateStatSlot[pItems.ItemSlotName]
if not vItem
or not vAltSlotName
or pOutfit.Items[vAltSlotName] ~= vItem then
pOutfit.Items[pItems.ItemSlotName] = vItem
return
end
-- Choose an empty slot if there's only one item to choose from
if vNumItems < 2 then
pOutfit.Items[pItems.ItemSlotName] = nil
return
end
-- If there are only two items to choose from then choose the other one
if vNumItems == 2 then
pOutfit.Items[pItems.ItemSlotName] = pItems.Items[3 - vIndex]
return
end
-- Try to find an item until there's no conflict
while vItem and pOutfit.Items[vAltSlotName] == vItem do
local vItemIndex = math.random(vNumItems + 1) - 1
vItem = vItemIndex > 0 and pItems.Items[vItemIndex] or nil
end
pOutfit.Items[pItems.ItemSlotName] = vItem
end
function Outfitter._OutfitIterator:GenerateRandomOutfit()
local vOutfit = Outfitter:NewEmptyOutfit(self.Name)
for _, vItems in ipairs(self.Slots) do
self:SetRandomItemForSlot(vOutfit, vItems)
end
-- Remove the OH choice if there's a 2H in the MH and
-- they can't dual-wield
if vOutfit.Items.MainHandSlot
and vOutfit.Items.MainHandSlot.InvType == "INVTYPE_2HWEAPON"
and not Outfitter.CanDualWield2H then
vOutfit.Items.SecondaryHandSlot = nil
end
return vOutfit
end
function Outfitter._OutfitIterator:SpliceOutfits(pOutfit1, pOutfit2)
local vOutfit = Outfitter:NewEmptyOutfit(self.Name)
for _, vItems in ipairs(self.Slots) do
local vItem = (math.random() < 0.5) and pOutfit1.Items[vItems.ItemSlotName] or pOutfit2.Items[vItems.ItemSlotName]
local vAltSlotName = Outfitter.cFullAlternateStatSlot[vItems.ItemSlotName]
-- This may need more work, but I'm not sure. If the same item gets selected
-- for two slots (trinket 1 and 2, ring 1 and 2, etc.) then it can't be used.
-- Right now I just punt and leave one of them empty, but this isn't optimal of course.
-- I think it may not matter though since that outfit will be genetically inferior
-- to other outfits and will get weeded out.
if not vAltSlotName
or vOutfit.Items[vAltSlotName] ~= vItem then
vOutfit.Items[vItems.ItemSlotName] = vItem
end
end
-- Remove the OH choice if there's a 2H in the MH and
-- they can't dual-wield
if vOutfit.Items.MainHandSlot
and vOutfit.Items.MainHandSlot.InvType == "INVTYPE_2HWEAPON"
and not Outfitter.CanDualWield2H then
vOutfit.Items.SecondaryHandSlot = nil
end
return vOutfit
end
function Outfitter._OutfitIterator:MutateOutfit(pOutfit, pMutationRate)
for _, vItems in ipairs(self.Slots) do
if math.random() < pMutationRate then
self:SetRandomItemForSlot(pOutfit, vItems)
end
end
-- Remove the OH choice if there's a 2H in the MH and
-- they can't dual-wield
if pOutfit.Items.MainHandSlot
and pOutfit.Items.MainHandSlot.InvType == "INVTYPE_2HWEAPON"
and not Outfitter.CanDualWield2H then
pOutfit.Items.SecondaryHandSlot = nil
end
end
local gOutfitter_CompareStat
function Outfitter._OutfitIterator:SortItemsByStat(pStat)
gOutfitter_CompareStat = pStat
for _, vSlotInfo in ipairs(self.Slots) do
table.sort(vSlotInfo.Items,
function (pItem1, pItem2)
local vStat1, vStat2 = pItem1[gOutfitter_CompareStat], pItem2[gOutfitter_CompareStat]
if vStat1 == nil then
return vStat2 ~= nil
elseif not vStat2 then
return false
else
return vStat1 < vStat2
end
end
)
end
end
function Outfitter._OutfitIterator:CalcMinNumSlotsByStat(pStat, pMinValue)
local vStatValues = {}
for _, vSlotInfo in ipairs(self.Slots) do
local vNumItems = #vSlotInfo.Items
if vNumItems > 0 then
local vValue = vSlotInfo.Items[1].Stats[pStat]
if vValue then
table.insert(vStatValues, vValue)
end
if vNumItems > 1
and (vSlotInfo.ItemSlotName == "Trinket0Slot"
or vSlotInfo.ItemSlotName == "Finger0Slot"
or vSlotInfo.ItemSlotName == "WeaponSlot") then
local vValue = vSlotInfo.Items[2].Stats[pStat]
if vValue then
table.insert(vStatValues, vValue)
end
end
end
end
table.sort(vStatValues, function (pValue1, pValue2) return pValue1 > pValue2 end)
local vTotal = 0
for vIndex, vValue in ipairs(vStatValues) do
vTotal = vTotal + vValue
if vTotal >= pMinValue then
return vIndex
end
end
return #vStatValues
end
----------------------------------------
Outfitter._MultiStatConfig = {}
----------------------------------------
function Outfitter._MultiStatConfig:New(pParent)
return CreateFrame("Frame", nil, pParent)
end
function Outfitter._MultiStatConfig:Construct(pParent)
self.ConfigLines = {}
self.AddStatButton = Outfitter:New(Outfitter.UIElementsLib._PushButton, self, "Add another stat", 200)
self.AddStatButton:SetScript("OnClick", function ()
self:SetNumConfigLines(self.NumConfigLines + 1)
end)
self:AdjustSize()
end
function Outfitter._MultiStatConfig:SetConfig(pConfig)
local vNumConfigLines
if not pConfig then
pConfig = {{StatID = "STA"}}
end
local vNumConfigLines = #pConfig
self:SetNumConfigLines(vNumConfigLines)
for vIndex, vConfig in ipairs(pConfig) do
self.ConfigLines[vIndex]:SetConfig(vConfig)
end
end
function Outfitter._MultiStatConfig:SetNumConfigLines(pNumLines)
self.NumConfigLines = pNumLines
local vOldNumConfigLines = #self.ConfigLines