forked from wofsauge/External-Item-Descriptions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eid_bagofcrafting.lua
1249 lines (1144 loc) · 44.7 KB
/
eid_bagofcrafting.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
local game = Game()
--these aren't local so that they can be saved and reloaded, or cleared in the Mod Config Menu
EID.BoC = {}
EID.BoC.CurrentPickupCount = -1
EID.BoC.BagItems = {}
EID.BoC.BagItemsOverride = nil
EID.BoC.RoomQueries = {}
EID.BoC.RoomOverride = nil -- Override items displayed for current room's content
EID.BoC.InventoryQuery = {}
EID.BoC.InventoryOverride = nil -- Override items the player has in its inventory (Cards, pills, etc.)
EID.BoC.FloorQuery = {}
EID.BoC.FloorOverride = nil -- Override total items displayed to be as floor content
EID.RefreshBagTextbox = false
local pickupValues = {
0x00000000, -- 0 None
-- Hearts
0x00000001, -- 1 Red Heart
0x00000004, -- 2 Soul Heart
0x00000005, -- 3 Black Heart
0x00000005, -- 4 Eternal Heart
0x00000005, -- 5 Gold Heart
0x00000005, -- 6 Bone Heart
0x00000001, -- 7 Rotten Heart
-- Pennies
0x00000001, -- 8 Penny
0x00000003, -- 9 Nickel
0x00000005, -- 10 Dime
0x00000008, -- 11 Lucky Penny
-- Keys
0x00000002, -- 12 Key
0x00000007, -- 13 Golden Key
0x00000005, -- 14 Charged Key
-- Bombs
0x00000002, -- 15 Bomb
0x00000007, -- 16 Golden Bomb
0x0000000a, -- 17 Giga Bomb
-- Batteries
0x00000002, -- 18 Micro Battery
0x00000004, -- 19 Lil' Battery
0x00000008, -- 20 Mega Battery
-- Usables
0x00000002, -- 21 Card
0x00000002, -- 22 Pill
0x00000004, -- 23 Rune
0x00000004, -- 24 Dice Shard
0x00000002, -- 25 Cracked Key
-- Added in Update
0x00000007, -- 26 Golden Penny
0x00000007, -- 27 Golden Pill
0x00000007, -- 28 Golden Battery
0x00000000, -- 29 Tainted ??? Poop
0x00000001,
}
local pickupIDLookup = {
["10.1"] = {1}, -- Red heart
["10.2"] = {1}, -- half heart
["10.3"] = {2}, -- soul heart
["10.4"] = {4}, -- eternal heart
["10.5"] = {1, 1}, -- double heart
["10.6"] = {3}, -- black heart
["10.7"] = {5}, -- gold heart
["10.8"] = {2}, -- half soul heart
["10.9"] = {1}, -- scared red heart
["10.10"] = {2, 1}, -- blended heart
["10.11"] = {6}, -- Bone heart
["10.12"] = {7}, -- Rotten heart
["20.1"] = {8}, -- Penny
["20.2"] = {9}, -- Nickel
["20.3"] = {10}, -- Dime
["20.4"] = {8, 8}, -- Double penny
["20.5"] = {11}, -- Lucky Penny
["20.6"] = {9}, -- Sticky Nickel
["20.7"] = {26}, -- Golden Penny
["30.1"] = {12}, -- Key
["30.2"] = {13}, -- golden Key
["30.3"] = {12,12}, -- Key Ring
["30.4"] = {14}, -- charged Key
["40.1"] = {15}, -- bomb
["40.2"] = {15,15}, -- double bomb
["40.4"] = {16}, -- golden bomb
["40.7"] = {17}, -- giga bomb
["42.0"] = {29}, -- poop nugget
["42.1"] = {29}, -- big poop nugget
["70.14"] = {27}, -- golden pill
["70.2062"] = {27}, -- golden horse pill
["90.1"] = {19}, -- Lil Battery
["90.2"] = {18}, -- Micro Battery
["90.3"] = {20}, -- Mega Battery
["90.4"] = {28}, -- Golden Battery
["300.49"] = {24}, -- Dice shard
["300.50"] = {21}, -- Emergency Contact
["300.78"] = {25}, -- Cracked key
}
local function IsTaintedCain()
-- this check is necessary for tracking Bag usage since Tainted Cain's pocket bag works differently than everyone else's
return EID.bagPlayer:GetPlayerType() == 23
end
local componentShifts = {
{0x00000001, 0x00000005, 0x00000010},
{0x00000001, 0x00000005, 0x00000013},
{0x00000001, 0x00000009, 0x0000001D},
{0x00000001, 0x0000000B, 0x00000006},
{0x00000001, 0x0000000B, 0x00000010},
{0x00000001, 0x00000013, 0x00000003},
{0x00000001, 0x00000015, 0x00000014},
{0x00000001, 0x0000001B, 0x0000001B},
{0x00000002, 0x00000005, 0x0000000F},
{0x00000002, 0x00000005, 0x00000015},
{0x00000002, 0x00000007, 0x00000007},
{0x00000002, 0x00000007, 0x00000009},
{0x00000002, 0x00000007, 0x00000019},
{0x00000002, 0x00000009, 0x0000000F},
{0x00000002, 0x0000000F, 0x00000011},
{0x00000002, 0x0000000F, 0x00000019},
{0x00000002, 0x00000015, 0x00000009},
{0x00000003, 0x00000001, 0x0000000E},
{0x00000003, 0x00000003, 0x0000001A},
{0x00000003, 0x00000003, 0x0000001C},
{0x00000003, 0x00000003, 0x0000001D},
{0x00000003, 0x00000005, 0x00000014},
{0x00000003, 0x00000005, 0x00000016},
{0x00000003, 0x00000005, 0x00000019},
{0x00000003, 0x00000007, 0x0000001D},
{0x00000003, 0x0000000D, 0x00000007},
{0x00000003, 0x00000017, 0x00000019},
{0x00000003, 0x00000019, 0x00000018},
{0x00000003, 0x0000001B, 0x0000000B},
{0x00000004, 0x00000003, 0x00000011},
{0x00000004, 0x00000003, 0x0000001B},
{0x00000004, 0x00000005, 0x0000000F},
{0x00000005, 0x00000003, 0x00000015},
{0x00000005, 0x00000007, 0x00000016},
{0x00000005, 0x00000009, 0x00000007},
{0x00000005, 0x00000009, 0x0000001C},
{0x00000005, 0x00000009, 0x0000001F},
{0x00000005, 0x0000000D, 0x00000006},
{0x00000005, 0x0000000F, 0x00000011},
{0x00000005, 0x00000011, 0x0000000D},
{0x00000005, 0x00000015, 0x0000000C},
{0x00000005, 0x0000001B, 0x00000008},
{0x00000005, 0x0000001B, 0x00000015},
{0x00000005, 0x0000001B, 0x00000019},
{0x00000005, 0x0000001B, 0x0000001C},
{0x00000006, 0x00000001, 0x0000000B},
{0x00000006, 0x00000003, 0x00000011},
{0x00000006, 0x00000011, 0x00000009},
{0x00000006, 0x00000015, 0x00000007},
{0x00000006, 0x00000015, 0x0000000D},
{0x00000007, 0x00000001, 0x00000009},
{0x00000007, 0x00000001, 0x00000012},
{0x00000007, 0x00000001, 0x00000019},
{0x00000007, 0x0000000D, 0x00000019},
{0x00000007, 0x00000011, 0x00000015},
{0x00000007, 0x00000019, 0x0000000C},
{0x00000007, 0x00000019, 0x00000014},
{0x00000008, 0x00000007, 0x00000017},
{0x00000008, 0x00000009, 0x00000017},
{0x00000009, 0x00000005, 0x0000000E},
{0x00000009, 0x00000005, 0x00000019},
{0x00000009, 0x0000000B, 0x00000013},
{0x00000009, 0x00000015, 0x00000010},
{0x0000000A, 0x00000009, 0x00000015},
{0x0000000A, 0x00000009, 0x00000019},
{0x0000000B, 0x00000007, 0x0000000C},
{0x0000000B, 0x00000007, 0x00000010},
{0x0000000B, 0x00000011, 0x0000000D},
{0x0000000B, 0x00000015, 0x0000000D},
{0x0000000C, 0x00000009, 0x00000017},
{0x0000000D, 0x00000003, 0x00000011},
{0x0000000D, 0x00000003, 0x0000001B},
{0x0000000D, 0x00000005, 0x00000013},
{0x0000000D, 0x00000011, 0x0000000F},
{0x0000000E, 0x00000001, 0x0000000F},
{0x0000000E, 0x0000000D, 0x0000000F},
{0x0000000F, 0x00000001, 0x0000001D},
{0x00000011, 0x0000000F, 0x00000014},
{0x00000011, 0x0000000F, 0x00000017},
{0x00000011, 0x0000000F, 0x0000001A}
}
-- The icon each item pool will use in the No Recipes display
local poolToIcon = { [0]="{{TreasureRoom}}",[1]="{{Shop}}",[2]="{{BossRoom}}",[3]="{{DevilRoom}}",[4]="{{AngelRoom}}",
[5]="{{SecretRoom}}",[7]="{{PoopRoomIcon}}",[8]="{{GoldenChestRoomIcon}}",[9]="{{RedChestRoomIcon}}",[12]="{{CursedRoom}}",[26]="{{Planetarium}}" }
-- local copies of our XML data in case it's slightly faster
local CraftingMaxItemID = EID.XMLMaxItemID
local CraftingFixedRecipes = EID.XMLRecipes
local CraftingItemPools = EID.XMLItemPools
local CraftingItemQualities = {}
--These are recipes that have already been calculated, plus the contents of recipes.xml
local calculatedRecipes = {}
--Backup recipes in case of potential achievement lock
local lockedRecipes = {}
--If the seed changes, the above two tables will be wiped
local lastSeedUsed = 0
--A list of item IDs, sorted by quality, then by name, to help with sorting our recipe list faster
local sortedIDs = {}
local function sortAllItems()
sortedIDs = {}
local objectNames = {}
for i = 1, CraftingMaxItemID do
if CraftingItemQualities[i] ~= nil then
table.insert(sortedIDs, i)
objectNames[i] = EID:getObjectName(5, 100, i)
end
end
table.sort(sortedIDs, function(a, b)
if CraftingItemQualities[a] == CraftingItemQualities[b] then
return (objectNames[a] < objectNames[b])
else
return (CraftingItemQualities[a] > CraftingItemQualities[b])
end
end)
end
-- delay the initial sort until needed, in case of modded items
local sortNeeded = true
local recheckPickups = false
local customRNGSeed = 0x77777770
local customRNGShift = {0,0,0}
-- Use local RNG functions to possibly reduce processing time a little bit
local function RNGNext()
local num = customRNGSeed
num = num ~ ((num >> customRNGShift[1]) & 4294967295)
num = num ~ ((num << customRNGShift[2]) & 4294967295)
num = num ~ ((num >> customRNGShift[3]) & 4294967295)
customRNGSeed = num >> 0;
return customRNGSeed;
end
local function nextFloat()
local multi = 2.3283061589829401E-10;
return RNGNext() * multi;
end
-- Convert a pickup's ID into what ingredient it counts as
function EID:getBagOfCraftingID(Variant, SubType)
local entry = pickupIDLookup[Variant.."."..SubType]
if entry ~= nil then
return entry
elseif Variant == 300 then
if SubType == 0 then -- player:GetCard() returned 0
return nil
elseif EID.runeIDs[SubType] then -- runes
return {23}
else -- cards
return {21}
end
elseif Variant == 70 then -- pills
if SubType == 0 then -- player:GetPill() returned 0
return nil
else
return {22}
end
end
return nil
end
-- NO RECIPES MODE: Get percentages of what quality / item pool a given set of 8 ingredients can yield
function EID:simulateBagOfCrafting(componentsTable)
local components = componentsTable
local compTotalWeight = 0
local compCounts = {}
for i = 1, #componentShifts do
compCounts[i] = 0
end
for _, compId in ipairs(components) do
if (_ > 8) then break end
compCounts[compId + 1] = compCounts[compId + 1] + 1
compTotalWeight = compTotalWeight + pickupValues[compId + 1]
end
local poolWeights = {
{idx = 0, weight = 1, totalWeight = 0},
{idx = 1, weight = 2, totalWeight = 0},
{idx = 2, weight = 2, totalWeight = 0},
{idx = 3, weight = compCounts[4] * 10, totalWeight = 0},
{idx = 4, weight = compCounts[5] * 10, totalWeight = 0},
{idx = 5, weight = compCounts[7] * 5, totalWeight = 0},
{idx = 7, weight = compCounts[30] * 10, totalWeight = 0},
{idx = 8, weight = compCounts[6] * 10, totalWeight = 0},
{idx = 9, weight = compCounts[26] * 10, totalWeight = 0},
{idx = 12, weight = compCounts[8] * 10, totalWeight = 0},
}
if compCounts[9] + compCounts[2] + compCounts[13] + compCounts[16] == 0 then
table.insert(poolWeights, {idx = 26, weight = compCounts[24] * 10, totalWeight = 0})
end
local totalWeight = 0
local qualityWeights = {[0] = 0, 0, 0, 0, 0}
for _, poolWeight in ipairs(poolWeights) do
if poolWeight.weight > 0 then
local qualityMin = 0
local qualityMax = 1
local n = compTotalWeight
if (poolWeight.idx >= 3) and (poolWeight.idx <= 5) then
n = n - 5
end
if n > 34 then
qualityMin = 4
qualityMax = 4
elseif n > 26 then
qualityMin = 3
qualityMax = 4
elseif n > 22 then
qualityMin = 2
qualityMax = 4
elseif n > 18 then
qualityMin = 2
qualityMax = 3
elseif n > 14 then
qualityMin = 1
qualityMax = 2
elseif n > 8 then
qualityMin = 0
qualityMax = 2
end
local pool = CraftingItemPools[poolWeight.idx + 1]
for _, item in ipairs(pool) do
local quality = CraftingItemQualities[item[1]]
if quality >= qualityMin and quality <= qualityMax then
local w = item[2] * poolWeight.weight
poolWeight.totalWeight = poolWeight.totalWeight + w
qualityWeights[quality] = qualityWeights[quality] + w
totalWeight = totalWeight + w
end
end
end
end
local poolString = ""
local firstAfterBoss = false
for k,v in ipairs(poolWeights) do
if (v.totalWeight > 0) then
--line break after boss pool
if (firstAfterBoss) then poolString = poolString .. " " end
poolString = poolString .. poolToIcon[v.idx] .. ":" .. math.floor(v.totalWeight/totalWeight*100+0.5) .. "%,"
firstAfterBoss = (k == 3)
end
end
poolString = string.sub(poolString,1,-2) .. "#"
for i=0,4 do
local v = qualityWeights[i]
if (v > 0) then
poolString = poolString .. "{{Quality" .. i .. "}}:" .. math.floor(v/totalWeight*100+0.5) .. "%,"
end
end
poolString = string.sub(poolString,1,-2)
return compTotalWeight, poolString
end
-- The main function that takes 8 ingredients and tells you what collectible you will get in return
-- Only pass in a table with 8 valid ingredients!!!
function EID:calculateBagOfCrafting(componentsTable)
-- ingredients must be sorted by ID for the RNG shifting to be accurate, so make a local copy
local components = {table.unpack(componentsTable)}
table.sort(components)
local componentsAsString = table.concat(components, ",")
-- Check the fixed recipes
local fixedRecipeResult = nil
local cacheResult = CraftingFixedRecipes[componentsAsString]
if cacheResult ~= nil then
if EID:isCollectibleUnlockedAnyPool(cacheResult) then
return cacheResult, cacheResult
else
fixedRecipeResult = cacheResult
end
end
-- Check the recipes already calculated for this seed
cacheResult = calculatedRecipes[componentsAsString]
local lockedResult = lockedRecipes[componentsAsString]
if cacheResult ~= nil then
return cacheResult, lockedResult
end
-- Count up the ingredients, and shift the RNG based on the components in the bag
customRNGSeed = lastSeedUsed
local compTotalWeight = 0
local compCounts = {}
for i = 1, #componentShifts do
compCounts[i] = 0
end
for _, compId in ipairs(components) do
compCounts[compId + 1] = compCounts[compId + 1] + 1
compTotalWeight = compTotalWeight + pickupValues[compId + 1]
customRNGShift = componentShifts[compId + 1]
RNGNext()
end
customRNGShift = componentShifts[7]
local poolWeights = {
{idx = 0, weight = 1},
{idx = 1, weight = 2},
{idx = 2, weight = 2},
{idx = 3, weight = compCounts[4] * 10},
{idx = 4, weight = compCounts[5] * 10},
{idx = 5, weight = compCounts[7] * 5},
{idx = 7, weight = compCounts[30] * 10},
{idx = 8, weight = compCounts[6] * 10},
{idx = 9, weight = compCounts[26] * 10},
{idx = 12, weight = compCounts[8] * 10},
}
if compCounts[9] + compCounts[2] + compCounts[13] + compCounts[16] == 0 then
table.insert(poolWeights, {idx = 26, weight = compCounts[24] * 10})
end
local totalWeight = 0
local itemWeights = {}
for i = 1, CraftingMaxItemID do
itemWeights[i] = 0
end
for _, poolWeight in ipairs(poolWeights) do
if poolWeight.weight > 0 then
local qualityMin = 0
local qualityMax = 1
local n = compTotalWeight
-- Devil, Angel, and Secret Room Pools have a 5 point penalty
if (poolWeight.idx >= 3) and (poolWeight.idx <= 5) then
n = n - 5
end
if n > 34 then
qualityMin = 4
qualityMax = 4
elseif n > 26 then
qualityMin = 3
qualityMax = 4
elseif n > 22 then
qualityMin = 2
qualityMax = 4
elseif n > 18 then
qualityMin = 2
qualityMax = 3
elseif n > 14 then
qualityMin = 1
qualityMax = 2
elseif n > 8 then
qualityMin = 0
qualityMax = 2
end
local pool = CraftingItemPools[poolWeight.idx + 1]
for _, item in ipairs(pool) do
local quality = CraftingItemQualities[item[1]]
if quality >= qualityMin and quality <= qualityMax then
local w = item[2] * poolWeight.weight
itemWeights[item[1]] = itemWeights[item[1]] + w
totalWeight = totalWeight + w
end
end
end
end
--unsure if this emergency Breakfast would ever occur, without massively modified item pools at least, but it's in the game's code
if totalWeight <= 0 then
return 25, 25
end
--When the first crafting result is an achievement locked item, this process gets repeated a second time to choose a new result
--That 2nd pick could also be achievement locked but we're ignoring that...
local firstOption = fixedRecipeResult
while true do
local t = nextFloat() -- random number between 0 and 1
local target = t * totalWeight -- number between 0 and total weight of possible results
for k,v in ipairs(itemWeights) do
target = target - v
if target < 0 then
if firstOption and k ~= firstOption then
calculatedRecipes[componentsAsString] = firstOption
lockedRecipes[componentsAsString] = k
return firstOption, k
else
--Don't do the 2nd pass if this item is definitely unlocked
if EID:isCollectibleUnlockedAnyPool(k) then
calculatedRecipes[componentsAsString] = k
lockedRecipes[componentsAsString] = k
return k, k
else
firstOption = k
break
end
end
end
end
end
end
local function calcHeldItems()
EID.BoC.InventoryQuery = {}
for i = 0, game:GetNumPlayers() - 1 do
local player = Isaac.GetPlayer(i)
for j = 0, 3 do
local card = EID:getBagOfCraftingID(300, player:GetCard(j))
local pill = EID:getBagOfCraftingID(70, player:GetPill(j))
-- assume the card/pill is only 1 ingredient
if card then table.insert(EID.BoC.InventoryQuery, card[1]) end
if pill then table.insert(EID.BoC.InventoryQuery, pill[1]) end
end
end
end
local function calcFloorItems()
EID.BoC.FloorQuery = {}
for _,v in pairs(EID.BoC.RoomQueries) do
for _,v1 in ipairs(v) do
table.insert(EID.BoC.FloorQuery, v1)
end
end
end
local function qualitySort(a, b)
if (pickupValues[a+1] == pickupValues[b+1]) then
return a > b
else
return pickupValues[a+1] > pickupValues[b+1]
end
end
local moddedCrafting = false
local function GameStartCrafting()
for i=1, EID.XMLMaxItemID do
local item = EID.itemConfig:GetCollectible(i)
if item ~= nil then
CraftingItemQualities[item.ID] = item.Quality
end
end
if not EID:PlayersHaveCollectible(CollectibleType.COLLECTIBLE_TMTRAINER) then
-- Check for modded items past the known max item ID on game start (can also support game updates)
-- Only works if the new items are at Weight 1.0 in their item pools, but better than nothing
if EID.Config["BagOfCraftingModdedRecipes"] and EID.itemConfig:GetCollectible(EID.XMLMaxItemID+1) ~= nil and not moddedCrafting then
-- Items past max ID detected
CraftingMaxItemID = EID.XMLMaxItemID -- XMLMaxItemID is never modified
-- Add new item qualities
local coll = EID.itemConfig:GetCollectible(CraftingMaxItemID+1)
while coll ~= nil do
CraftingMaxItemID = CraftingMaxItemID + 1
CraftingItemQualities[coll.ID] = coll.Quality
coll = EID.itemConfig:GetCollectible(CraftingMaxItemID+1)
end
local itemPool = game:GetItemPool()
-- Add new items to the crafting item pools, assuming Weight 1.0
for poolNum,_ in pairs(poolToIcon) do
for i=1,EID.XMLMaxItemID do itemPool:AddRoomBlacklist(i) end
local collID = itemPool:GetCollectible(poolNum, false, 1, 25)
local attempts = CraftingMaxItemID
while collID ~= 25 and collID ~= 642 and collID > 0 and attempts > 0 do
attempts = attempts - 1
table.insert(CraftingItemPools[poolNum+1], {collID, 1.0})
itemPool:AddRoomBlacklist(collID)
collID = itemPool:GetCollectible(poolNum, false, 1, 25)
end
itemPool:ResetRoomBlacklist()
end
moddedCrafting = true
end
sortNeeded = true
end
end
EID:AddCallback(ModCallbacks.MC_POST_GAME_STARTED, GameStartCrafting)
------------------------------------------
------------------------------------------
-----------Bag content detection ---------
------------------------------------------
------------------------------------------
local pickupsCollected = {} -- table of collected pickup indexes, reset each room
local pickupsJustTouched = {} -- flags of pickups a player/pickup-collector has touched, so the bag doesn't think it collected it
EID:AddCallback(ModCallbacks.MC_PRE_PICKUP_COLLISION, function(_, pickup,collider,_)
if collider.Type == EntityType.ENTITY_PLAYER or collider.Type == EntityType.ENTITY_FAMILIAR or
collider.Type == EntityType.ENTITY_BUMBINO or collider.Type == EntityType.ENTITY_ULTRA_GREED then
pickupsJustTouched[pickup.Index] = true
end
end)
-- Formerly a MC_POST_PICKUP_UPDATE, but moved to this so that it's only called when we own a bag
local function checkForPickups()
for _,pickup in ipairs(Isaac.FindByType(EntityType.ENTITY_PICKUP, -1, -1, false, false)) do
if pickup:GetSprite():GetAnimation() == "Collect" and not pickupsCollected[pickup.Index] then
pickupsCollected[pickup.Index] = true
if not pickupsJustTouched[pickup.Index] then
local craftingIDs = EID:getBagOfCraftingID(pickup.Variant, pickup.SubType)
if craftingIDs ~= nil then
recheckPickups = true
for _,v in ipairs(craftingIDs) do
if #EID.BoC.BagItems >= 8 then table.remove(EID.BoC.BagItems, 1) end
table.insert(EID.BoC.BagItems, v)
end
end
end
end
pickupsJustTouched[pickup.Index] = nil
end
end
EID:AddCallback(ModCallbacks.MC_POST_NEW_ROOM, function(_)
-- We're using the pickup indexes for quick checking, which reset on each new room
pickupsCollected = {}
end)
-- Using a Card/Pill will change our inventory craftable items, so force a refresh then
-- (Note: Items that directly add a card/pill to you, i.e. Bottle of Pills, also need a refresh, but aren't tracked for performance)
EID:AddCallback(ModCallbacks.MC_USE_CARD, function(_)
recheckPickups = true
end)
EID:AddCallback(ModCallbacks.MC_USE_PILL, function(_)
recheckPickups = true
end)
--Tainted Cain "hold to craft" check
local holdCounter = 0
local icount = 0
local function trackBagHolding()
if not IsTaintedCain() then return end
local isCardHold = Input.IsActionPressed(ButtonAction.ACTION_PILLCARD, EID.bagPlayer.ControllerIndex)
local animationName = EID.bagPlayer:GetSprite():GetAnimation()
if isCardHold and string.match(animationName, "PickupWalk") and #EID.BoC.BagItems>=8 then
holdCounter = holdCounter + 1
if holdCounter < 30 then
icount = EID.bagPlayer:GetCollectibleCount()
end
else
if isCardHold and holdCounter >= 30 and (string.match(animationName, "Walk") and not string.match(animationName, "Pickup") or (EID.bagPlayer:GetCollectibleCount() ~= icount)) then
EID.BoC.BagItems = {}
else
holdCounter = 0
end
end
end
--Active slot "press to craft" check
EID:AddCallback(ModCallbacks.MC_PRE_USE_ITEM, function(_, _, _, _, _, slot)
if slot ~= 0 or #EID.BoC.BagItems < 8 then return end
EID.BoC.BagItems = {}
end, CollectibleType.COLLECTIBLE_BAG_OF_CRAFTING)
local function shiftBagContent()
local newContent = {}
for i=2,#EID.BoC.BagItems do
table.insert(newContent, EID.BoC.BagItems[i])
end
table.insert(newContent, EID.BoC.BagItems[1])
EID.BoC.BagItems = newContent
end
-- only Tainted Cain's consumable slot bag can have its ingredients shifted
local function detectBagContentShift()
if Input.IsActionTriggered(ButtonAction.ACTION_DROP, EID.bagPlayer.ControllerIndex) and IsTaintedCain() then
shiftBagContent()
end
end
-----------------------------
-----------------------------
-----------RENDERING---------
-----------------------------
-----------------------------
local randResultCache = {}
local calcResultCache = {}
local numResults = 0
local bagOfCraftingOffset = 0
local lockedResults = nil
local refreshNextTick = false
local refreshPosition = 0
local bagOfCraftingRefreshes = 0
local downHeld = 0
local upHeld = 0
local resetBagCounter = 0
local craftingIsHidden = false
local showCraftingResult = false
local prevDesc = ""
--this combination algorithm was adopted from this Java code: https://stackoverflow.com/a/16256122
--note that it will run into duplicates, for example if you have eight pennies and a key, it can't tell the difference between
--PPPPPPPK (pennies 1-7) and PPPPPPPK (pennies 2-8) and PPPPPPPK (pennies 1-4,6-8) etc..., I don't know of a way to prevent that
local coTimer = 0
-- number of milliseconds we allow these to run without yielding (1/60th of a second = 16.66667 milliseconds)
local coTimerLength = 5
local function combinations(arr, length, startPos, tempResult, randResults, newResults)
if Isaac.GetTime() > coTimer + coTimerLength then
coroutine.yield()
coTimer = Isaac.GetTime()
end
local length = length or 8
local startPos = startPos or 1
local tempResult = tempResult or {}
if (length == 0) then
local resultString = table.concat(tempResult,",")
if (randResults[resultString] == nil) then
randResults[resultString] = {table.unpack(tempResult)}
newResults[resultString] = {table.unpack(tempResult)}
end
return
end
for i = startPos, #arr-length+1 do
tempResult[8-length+1] = arr[i]
combinations(arr,length-1, i+1, tempResult, randResults, newResults)
end
end
--code from InputHelper in MCM
local HotkeyToString = {}
for key,num in pairs(Keyboard) do
local keyString = key
local keyStart, keyEnd = string.find(keyString, "KEY_")
keyString = string.sub(keyString, keyEnd+1, string.len(keyString))
keyString = string.gsub(keyString, "_", " ")
HotkeyToString[num] = keyString
end
--convert controller enum to buttons
local ControllerToString = { [0] = "{{ButtonDLeft}}", "{{ButtonDRight}}", "{{ButtonDUp}}", "{{ButtonDDown}}",
"{{ButtonA}}", "{{ButtonB}}", "{{ButtonX}}", "{{ButtonY}}", "{{ButtonLB}}", "{{ButtonLT}}", "{{ButtonLStick}}",
"{{ButtonRB}}", "{{ButtonRT}}", "{{ButtonRStick}}", "{{ButtonSelect}}", "{{ButtonMenu}}" }
local function getHotkeyString()
if (not EID.Config["BagOfCraftingShowControls"]) then return "" end
local hotkeyString = ""
local hideDesc = EID:getDescriptionEntry("CraftingHideKey")
local previewDesc = EID:getDescriptionEntry("CraftingPreviewKey")
local controllerEnabled = EID.bagPlayer.ControllerIndex > 0
local hideKey = HotkeyToString[EID.Config["CraftingHideKey"]]
local hideButton = controllerEnabled and ControllerToString[EID.Config["CraftingHideButton"]]
local previewKey = HotkeyToString[EID.Config["CraftingResultKey"]]
local previewButton = controllerEnabled and ControllerToString[EID.Config["CraftingResultButton"]]
if hideKey or hideButton then hotkeyString = hideDesc .. " " end
if hideKey and hideButton then
hotkeyString = hotkeyString .. hideKey .. "/" .. hideButton
else
hotkeyString = hotkeyString .. (hideKey or hideButton)
end
if #EID.BoC.BagItems >= 8 and EID.Config["BagOfCraftingDisplayMode"] ~= "Preview Only" then
if previewKey or previewButton then hotkeyString = hotkeyString .. ", " .. previewDesc .. " " end
if previewKey and previewButton then
hotkeyString = hotkeyString .. previewKey .. "/" .. previewButton
else
hotkeyString = hotkeyString .. (previewKey or previewButton)
end
end
if hotkeyString ~= "" then
hotkeyString = "!!! " .. hotkeyString .. "#"
end
return hotkeyString
end
local function getFloorItemsString(showPreviews, roomItems)
local floorString = ""
local bagItems = EID.BoC.BagItemsOverride or EID.BoC.BagItems
if #bagItems >0 then
if showPreviews and #bagItems == 8 then
local recipe = EID:calculateBagOfCrafting(bagItems)
floorString = floorString .. "{{Collectible"..recipe.."}} "
end
local bagDesc = EID:getDescriptionEntry("CraftingBagContent")
floorString = floorString .. bagDesc.. EID:tableToCraftingIconsMerged(bagItems).."#"
end
local curRoomItems = EID.BoC.RoomOverride or roomItems
if #curRoomItems >0 then
if showPreviews and #curRoomItems == 8 then
local recipe = EID:calculateBagOfCrafting(curRoomItems)
floorString = floorString .. "{{Collectible"..recipe.."}} "
end
local roomDesc = EID:getDescriptionEntry("CraftingRoomContent")
floorString = floorString .. roomDesc..EID:tableToCraftingIconsMerged(curRoomItems).."#"
end
local floorQuery = EID.BoC.FloorOverride or EID.BoC.FloorQuery
if #floorQuery >0 and #curRoomItems ~= #floorQuery then
if showPreviews and #floorQuery == 8 then
local recipe = EID:calculateBagOfCrafting(floorQuery)
floorString = floorString .. "{{Collectible"..recipe.."}} "
end
local floorDesc = EID:getDescriptionEntry("CraftingFloorContent")
floorString = floorString .. floorDesc..EID:tableToCraftingIconsMerged(floorQuery)
end
local inventoryQuery= EID.BoC.InventoryOverride or EID.BoC.InventoryQuery
if #inventoryQuery > 0 then
floorString = floorString .. "(+" .. EID:tableToCraftingIconsMerged(inventoryQuery) .. ")#"
else
floorString = floorString .. "#"
end
return floorString
end
-- This list will be modified once the coroutine finishes; until then it will have the last finished list
local currentRecipesList = {}
local itemQuery = {}
local mostValuable = {}
local randResults = {}
local newResults = {}
local skipRandom = false
local isRefresh = false
local queryString = ""
local displayingRecipeList = false
local function RecipeCrunchCoroutine()
coTimer = Isaac.GetTime()
-- Fill randResults/newResults with every possible combination of our most valuable ingredients
-- The number is has an option to limit it in the config, since the number of total combinations quickly grows (nCr):
-- 12 = 495, 13 = 1287, 14 = 3003, 15 = 6435, 16 = 12870
combinations(mostValuable, nil, nil, nil, randResults, newResults)
--do random pulls for some more recipe choices
if (not skipRandom) then
for i = 0, EID.Config["BagOfCraftingRandomResults"] do
if Isaac.GetTime() > coTimer + coTimerLength then
coroutine.yield()
coTimer = Isaac.GetTime()
end
local newTable = {}
local tableCopy = {table.unpack(itemQuery)}
for k = 1, 8 do
local pos = math.random(1, #tableCopy)
table.insert(newTable, tableCopy[pos])
table.remove(tableCopy, pos)
end
table.sort(newTable, qualitySort)
local resultString = table.concat(newTable,",")
if (randResults[resultString] == nil) then
randResults[resultString] = {table.unpack(newTable)}
newResults[resultString] = {table.unpack(newTable)}
end
end
end
local sortedResults = {}
if (calcResultCache[queryString]) then
sortedResults = calcResultCache[queryString]
else
for _, v in ipairs(sortedIDs) do
sortedResults[v] = {}
end
end
for _, v in pairs(newResults) do
if Isaac.GetTime() > coTimer + coTimerLength then
coroutine.yield()
coTimer = Isaac.GetTime()
end
local resultID, lockedAchievementID = EID:calculateBagOfCrafting(v)
if resultID ~= lockedAchievementID then
table.insert(sortedResults[resultID], {v, resultID, lockedAchievementID})
else
table.insert(sortedResults[resultID], {v, resultID})
end
end
calcResultCache[queryString] = sortedResults
randResultCache[queryString] = randResults
currentRecipesList = sortedResults
numResults = 0
for _,v in ipairs(sortedIDs) do
-- keep our cursor position if we're not at the top of the list, and bag's contents don't matter for list size
if (isRefresh and bagOfCraftingOffset > 0 and v == refreshPosition and (IsTaintedCain() or #EID.BoC.BagItems == 0)) then
--jump to the item we were looking at before, so you can more easily refresh for variants of recipes
bagOfCraftingOffset = numResults
end
numResults = numResults + #currentRecipesList[v]
end
if not isRefresh then
bagOfCraftingOffset = 0
bagOfCraftingRefreshes = 0
end
isRefresh = false
EID.RefreshBagTextbox = true
end
local prevOffset = 0
-- Called 60 times a second so we can read input properly
function EID:handleBagOfCraftingUpdating()
-- reset our calculated recipes when the game seed changes
local curSeed = game:GetSeeds():GetStartSeed()
if (curSeed ~= lastSeedUsed) then
calculatedRecipes = {}
lockedRecipes = {}
calcResultCache = {}
randResultCache = {}
lockedResults = nil
end
lastSeedUsed = curSeed
-- watch for holding the Craft button, and pressing the ingredient shift button
trackBagHolding()
detectBagContentShift()
if EID.GameRenderCount % 2 == 0 then checkForPickups() end
-- Check for Hide/Preview hotkeys; prevent them from triggering while in MCM
if not ModConfigMenu or not ModConfigMenu.IsVisible then
if Input.IsButtonTriggered(EID.Config["CraftingHideKey"], 0) or Input.IsButtonTriggered(EID.Config["CraftingHideButton"], EID.bagPlayer.ControllerIndex) then
craftingIsHidden = not craftingIsHidden
end
if Input.IsButtonTriggered(EID.Config["CraftingResultKey"], 0) or Input.IsButtonTriggered(EID.Config["CraftingResultButton"], EID.bagPlayer.ControllerIndex) then
showCraftingResult = not showCraftingResult
end
end
-- Check for Hold Tab key inputs
if displayingRecipeList and Input.IsActionPressed(EID.Config["BagOfCraftingToggleKey"], EID.bagPlayer.ControllerIndex) then
EID.TabDescThisFrame = true
EID.bagPlayer.ControlsCooldown = 2
if Input.IsActionTriggered(ButtonAction.ACTION_SHOOTDOWN, EID.bagPlayer.ControllerIndex) then
bagOfCraftingOffset = math.min(numResults-(numResults%EID.Config["BagOfCraftingResults"]), bagOfCraftingOffset + EID.Config["BagOfCraftingResults"])
downHeld = Isaac.GetTime()
elseif Input.IsActionTriggered(ButtonAction.ACTION_SHOOTUP, EID.bagPlayer.ControllerIndex) then
bagOfCraftingOffset = math.max(0, bagOfCraftingOffset - EID.Config["BagOfCraftingResults"])
upHeld = Isaac.GetTime()
--lock the current results so you can actually do a recipe that you've scrolled down to without losing it
elseif Input.IsActionTriggered(ButtonAction.ACTION_SHOOTLEFT, EID.bagPlayer.ControllerIndex) then
EID.RefreshBagTextbox = true
if (lockedResults == nil) then lockedResults = queryString
else lockedResults = nil end
--refresh the recipes
elseif Input.IsActionTriggered(ButtonAction.ACTION_SHOOTRIGHT, EID.bagPlayer.ControllerIndex) then
if (lockedResults == nil) then
refreshNextTick = true
end
end
--scroll pages quickly if the button is held
if Input.IsActionPressed(ButtonAction.ACTION_SHOOTDOWN, EID.bagPlayer.ControllerIndex) and Isaac.GetTime() - downHeld > 750 then
bagOfCraftingOffset = math.min(numResults-(numResults%EID.Config["BagOfCraftingResults"]), bagOfCraftingOffset + EID.Config["BagOfCraftingResults"])
downHeld = Isaac.GetTime() - 700
elseif Input.IsActionPressed(ButtonAction.ACTION_SHOOTUP, EID.bagPlayer.ControllerIndex) and Isaac.GetTime() - upHeld > 750 then
bagOfCraftingOffset = math.max(0, bagOfCraftingOffset - EID.Config["BagOfCraftingResults"])
upHeld = Isaac.GetTime() - 700
end
--reset bag contents when holding Use Pill/Card
if Input.IsActionPressed(ButtonAction.ACTION_PILLCARD, EID.bagPlayer.ControllerIndex) then
resetBagCounter = resetBagCounter + 1
if resetBagCounter > 120 then
EID.BoC.BagItems = {}
recheckPickups = true
resetBagCounter = 0
end
else
resetBagCounter = 0
end
end
--fix bug with being allowed to go to an empty page if recipe count = multiple of page size (or if we refresh on last page)
if (bagOfCraftingOffset >= numResults) then bagOfCraftingOffset = bagOfCraftingOffset - EID.Config["BagOfCraftingResults"] end
end
-- Called when needed based on EID.Config["RefreshRate"]
function EID:handleBagOfCraftingRendering(ignoreRefreshRate)
-- Determine if we should display anything at all
if not EID:RefreshThisFrame() and not ignoreRefreshRate then
return false
end
displayingRecipeList = false
if ((EID.isHidden or craftingIsHidden) and EID.MCMCompat_isDisplayingEIDTab ~= "Crafting") or game.Challenge == Challenge.CHALLENGE_CANTRIPPED then
return false
elseif EID.Config["BagOfCraftingHideInBattle"] and (Isaac.CountBosses() > 0 or Isaac.CountEnemies() > 0) then
return false
elseif EID.Config["DisplayBagOfCrafting"] == "never" then
return false
elseif EID.Config["DisplayBagOfCrafting"] == "hold" and not string.find(EID.bagPlayer:GetSprite():GetAnimation(), "PickupWalk") then
return false
elseif game:GetRoom():GetFrameCount() < 2 then
return false
end
local bagItems = EID.BoC.BagItemsOverride or EID.BoC.BagItems
-- Display the result of the 8 items in our bag if applicable
if (showCraftingResult or EID.Config["BagOfCraftingDisplayMode"] == "Preview Only") and #bagItems == 8 then
if EID.Config["BagOfCraftingDisplayMode"] ~= "Recipe List" and EID:hasCurseBlind() and EID.Config["DisableOnCurse"] then
showCraftingResult = false
return false
end
local craftingResult, backupResult = EID:calculateBagOfCrafting(bagItems)
if (backupResult ~= craftingResult) then EID.TabPreviewID = backupResult end
local descriptionObj = EID:getDescriptionObj(5, 100, craftingResult)
-- prepend the Hide/Preview hotkeys to the description
descriptionObj.Description = getHotkeyString() .. descriptionObj.Description
if (backupResult ~= craftingResult and descriptionObj.ObjSubType == craftingResult) then
local backupDesc = EID:getDescriptionEntry("CraftingPreviewBackup")