-
Notifications
You must be signed in to change notification settings - Fork 73
/
Concoction.java
1355 lines (1122 loc) · 42.5 KB
/
Concoction.java
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
package net.sourceforge.kolmafia.objectpool;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import net.sourceforge.kolmafia.AdventureResult;
import net.sourceforge.kolmafia.KoLCharacter;
import net.sourceforge.kolmafia.KoLConstants;
import net.sourceforge.kolmafia.KoLConstants.CraftingMisc;
import net.sourceforge.kolmafia.KoLConstants.CraftingRequirements;
import net.sourceforge.kolmafia.KoLConstants.CraftingType;
import net.sourceforge.kolmafia.ModifierType;
import net.sourceforge.kolmafia.Modifiers;
import net.sourceforge.kolmafia.RequestLogger;
import net.sourceforge.kolmafia.persistence.ConcoctionDatabase;
import net.sourceforge.kolmafia.persistence.Consumable;
import net.sourceforge.kolmafia.persistence.ConsumablesDatabase;
import net.sourceforge.kolmafia.persistence.ItemDatabase;
import net.sourceforge.kolmafia.preferences.Preferences;
import net.sourceforge.kolmafia.request.BarrelShrineRequest;
import net.sourceforge.kolmafia.request.ClanLoungeRequest;
import net.sourceforge.kolmafia.request.ClanLoungeRequest.SpeakeasyDrink;
import net.sourceforge.kolmafia.request.CombineMeatRequest;
import net.sourceforge.kolmafia.request.CreateItemRequest;
import net.sourceforge.kolmafia.request.PurchaseRequest;
import net.sourceforge.kolmafia.request.StillSuitRequest;
import net.sourceforge.kolmafia.utilities.StringUtilities;
/**
* Internal class used to represent a single concoction. It contains all the information needed to
* actually make the item.
*/
public class Concoction implements Comparable<Concoction> {
private String name;
private final int hashCode;
public final AdventureResult concoction;
private CreateItemRequest request;
private PurchaseRequest purchaseRequest;
private final int yield;
private CraftingType mixingMethod;
private final EnumSet<CraftingRequirements> mixingRequirements;
private final EnumSet<CraftingMisc> mixingMisc;
private final int row;
private final boolean isReagentPotion;
private boolean wasPossible;
private boolean visited;
private final List<AdventureResult> ingredients;
private int param;
private AdventureResult[] ingredientArray;
private int allocated;
public static int debugId = Integer.MAX_VALUE;
public static boolean debug = false;
public ConcoctionType type;
public int price;
public String property;
public int creatable;
public int queued;
public int queuedPulls;
public int initial;
public int pullable;
public int mallable;
public int total;
public int visibleTotal;
public int freeTotal;
public boolean special;
public boolean hotdog;
public boolean fancydog;
public SpeakeasyDrink speakeasy;
public boolean steelOrgan;
private Consumable consumable;
private String effectName;
private double mainstatGain;
private static final Set<String> steelOrgans =
Set.of("steel margarita", "steel lasagna", "steel-scented air freshener");
private static final Set<Integer> forceFood =
Set.of(
ItemPool.QUANTUM_TACO,
ItemPool.MUNCHIES_PILL,
ItemPool.WHETSTONE,
ItemPool.MAGICAL_SAUSAGE,
ItemPool.MAYONEX,
ItemPool.MAYODIOL,
ItemPool.MAYOSTAT,
ItemPool.MAYOZAPINE,
ItemPool.MAYOFLEX);
private static final Set<Integer> forceBooze = Set.of(ItemPool.ICE_STEIN);
public Concoction(
final AdventureResult concoction,
final CraftingType mixingMethod,
final EnumSet<CraftingRequirements> mixingRequirement,
final EnumSet<CraftingMisc> mixingInfo,
final int row) {
this.concoction = concoction;
this.wasPossible = false;
this.mixingMethod = mixingMethod;
this.mixingRequirements = mixingRequirement;
this.mixingMisc = mixingInfo;
this.row = row;
if (concoction == null) {
this.yield = 1;
this.name = "unknown";
this.hashCode = 0;
this.isReagentPotion = false;
this.mainstatGain = 0.0f;
this.effectName = "";
} else {
this.yield = Math.max(concoction.getCount(), 1);
int itemId = concoction.getItemId();
String name = concoction.getName();
this.name = itemId == -1 ? name : ItemDatabase.getItemDataName(itemId);
this.hashCode = this.name.toLowerCase().hashCode();
this.isReagentPotion = this.mixingMisc.contains(CraftingMisc.TRIPLE_SAUCE);
this.setConsumptionData();
if (CombineMeatRequest.getCost(concoction.getItemId()) > 0) {
this.request = new CombineMeatRequest(this);
}
this.setEffectName();
}
this.ingredients = new ArrayList<>();
this.ingredientArray = new AdventureResult[0];
this.price = -1;
this.property = null;
this.special = false;
this.steelOrgan = steelOrgans.contains(this.name);
this.resetCalculations();
}
public Concoction(final AdventureResult concoction, final CraftingType mixingMethod) {
this(
concoction,
mixingMethod,
EnumSet.noneOf(CraftingRequirements.class),
EnumSet.noneOf(CraftingMisc.class),
0);
}
public Concoction(final String name, final int price) {
this(null, CraftingType.NOCREATE);
this.name = name;
this.price = price;
this.resetCalculations();
this.setConsumptionData();
this.setEffectName();
}
public Concoction(final String name) {
this(null, CraftingType.NOCREATE);
this.name = name;
this.special = true;
this.resetCalculations();
this.setConsumptionData();
this.setEffectName();
}
public Concoction(final String name, final String property) {
this(null, CraftingType.NOCREATE);
this.name = name;
this.property = property;
this.resetCalculations();
this.setConsumptionData();
this.setEffectName();
}
public ConcoctionType computeType() {
int itemId = this.getItemId();
if (ConsumablesDatabase.getRawFullness(name) != null) {
return ConcoctionType.FOOD;
} else if (ConsumablesDatabase.getRawInebriety(name) != null) {
return ConcoctionType.BOOZE;
} else if (ConsumablesDatabase.getRawSpleenHit(name) != null) {
return ConcoctionType.SPLEEN;
} else
return switch (ItemDatabase.getConsumptionType(itemId)) {
case FOOD_HELPER -> ConcoctionType.FOOD;
case DRINK_HELPER -> ConcoctionType.BOOZE;
case USE, USE_MULTIPLE -> forceFood.contains(itemId)
? ConcoctionType.FOOD
: forceBooze.contains(itemId) ? ConcoctionType.BOOZE : ConcoctionType.NONE;
case POTION, AVATAR_POTION -> ConcoctionType.POTION;
default -> ConcoctionType.NONE;
};
}
public void setConsumptionData() {
this.setConsumptionData(ConsumablesDatabase.getConsumableByName(this.name));
}
public void setConsumptionData(Consumable consumable) {
this.consumable = consumable;
this.type = computeType();
this.setStatGain();
}
public void setEffectName() {
this.effectName = Modifiers.getStringModifier(ModifierType.ITEM, this.name, "Effect");
}
public void setStatGain() {
final String range =
switch (KoLCharacter.mainStat()) {
case MUSCLE -> ConsumablesDatabase.getStatRange(Consumable.MUSCLE, this.consumable);
case MYSTICALITY -> ConsumablesDatabase.getStatRange(
Consumable.MYSTICALITY, this.consumable);
case MOXIE -> ConsumablesDatabase.getStatRange(Consumable.MOXIE, this.consumable);
default -> "+0.0";
};
this.mainstatGain = StringUtilities.parseDouble(range);
}
public boolean usesIngredient(int itemId) {
for (AdventureResult adventureResult : this.ingredientArray) {
if (adventureResult.getItemId() == itemId) {
return true;
}
}
return false;
}
public boolean usesIngredient(AdventureResult ar) {
return this.usesIngredient(ar.getItemId());
}
public int getYield() {
if (KoLCharacter.tripleReagent() && this.isReagentPotion()) {
return 3 * this.yield;
}
return this.yield;
}
public boolean isReagentPotion() {
return this.isReagentPotion;
}
public boolean isHelper() {
return (this.type == ConcoctionType.FOOD && this.getRawFullness() == null)
|| (this.type == ConcoctionType.BOOZE && this.getRawInebriety() == null)
|| (this.type == ConcoctionType.SPLEEN && this.getRawSpleenHit() == null);
}
/*
From the definition of Comparable<T>:
This interface imposes a total ordering on the objects of each class
that implements it. This ordering is referred to as the class's
natural ordering, and the class's compareTo method is referred to as
its natural comparison method.
Lists (and arrays) of objects that implement this interface can be
sorted automatically by Collections.sort (and Arrays.sort). Objects
that implement this interface can be used as keys in a sorted map or
as elements in a sorted set, without the need to specify a
comparator.
The natural ordering for a class C is said to be consistent with
equals if and only if e1.compareTo(e2) == 0 has the same boolean
value as e1.equals(e2) for every e1 and e2 of class C. Note that null
is not an instance of any class, and e.compareTo(null) should throw a
NullPointerException even though e.equals(null) returns false.
It is strongly recommended (though not required) that natural
orderings be consistent with equals. This is so because sorted sets
(and sorted maps) without explicit comparators behave "strangely"
when they are used with elements (or keys) whose natural ordering is
inconsistent with equals. In particular, such a sorted set (or sorted
map) violates the general contract for set (or map), which is defined
in terms of the equals method.
*/
/*
From the definition of Object.equals:
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value x, x.equals(x)
should return true.
- It is symmetric: for any non-null reference values x and y,
x.equals(y) should return true if and only if y.equals(x) returns
true.
- It is transitive: for any non-null reference values x, y, and z, if
x.equals(y) returns true and y.equals(z) returns true, then
x.equals(z) should return true.
- It is consistent: for any non-null reference values x and y,
multiple invocations of x.equals(y) consistently return true or
consistently return false, provided no information used in equals
comparisons on the objects is modified.
- For any non-null reference value x, x.equals(null) should return
false.
Note that it is generally necessary to override the hashCode method
whenever this method is overridden, so as to maintain the general
contract for the hashCode method, which states that equal objects
must have equal hash codes.
*/
@Override
public boolean equals(final Object o) {
// For any non-null reference value x, x.equals(null) should
// return false.
if (!(o instanceof Concoction other)) {
return false;
}
// Concoction.compareTo() returns 0 only if the names match
// ignoring case
if (this.name == null) {
return other.name == null;
}
if (other.name == null) {
return false;
}
return nameCheckEquals(other);
}
private boolean nameCheckEquals(Concoction other) {
boolean names = this.name.equalsIgnoreCase(other.name);
boolean id = this.getItemId() == other.getItemId();
return names && id;
}
private int nameCheckCompare(Concoction other) {
int cname = this.name.compareToIgnoreCase(other.name);
if (cname != 0) {
return cname;
} else {
return Integer.compare(this.getItemId(), other.getItemId());
}
}
/*
From the definition of Object.hashcode:
The general contract of hashCode is:
- Whenever it is invoked on the same object more than once during an
execution of a Java application, the hashCode method must
consistently return the same integer, provided no information used
in equals comparisons on the object is modified. This integer need
not remain consistent from one execution of an application to
another execution of the same application.
- If two objects are equal according to the equals(Object) method,
then calling the hashCode method on each of the two objects must
produce the same integer result.
- It is not required that if two objects are unequal according to the
equals(java.lang.Object) method, then calling the hashCode method
on each of the two objects must produce distinct integer
results. However, the programmer should be aware that producing
distinct integer results for unequal objects may improve the
performance of hash tables.
*/
@Override
public int hashCode() {
// Concoctions are equal only if their names are equal
// We precalculated the hashCode
return this.hashCode;
}
/*
From the definition of Comparable.compareTo:
Compares this object with the specified object for order. Returns a
negative integer, zero, or a positive integer as this object is less
than, equal to, or greater than the specified object.
The implementor must ensure
sgn(x.compareTo(y)) == -sgn(y.compareTo(x)
for all x and y. (This implies that x.compareTo(y) must throw an
exception iff y.compareTo(x) throws an exception.)
The implementor must also ensure that the relation is transitive:
(x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.
Finally, the implementor must ensure that x.compareTo(y)==0 implies
that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
It is strongly recommended, but not strictly required that
(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class
that implements the Comparable interface and violates this condition
should clearly indicate this fact. The recommended language is "Note:
this class has a natural ordering that is inconsistent with equals."
In the foregoing description, the notation sgn(expression) designates
the mathematical signum function, which is defined to return one of
-1, 0, or 1 according to whether the value of expression is negative,
zero or positive.
*/
@Override
public int compareTo(final Concoction o) {
// Note that null is not an instance of any class, and
// e.compareTo(null) should throw a NullPointerException even
// though e.equals(null) returns false.
if (o == null) {
throw new NullPointerException();
}
if (this.equals(o)) {
return 0;
}
if (this.name == null) {
return o.name == null ? 0 : 1;
}
if (o.name == null) {
return -1;
}
if (this.type != o.type) {
return this.type.compareTo(o.type);
}
if (this.type == ConcoctionType.NONE) {
return this.nameCheckCompare(o);
} else if (this.type == ConcoctionType.POTION) {
if (Preferences.getBoolean("sortByEffect")) {
return this.getEffectName().compareTo(o.getEffectName());
} else {
return this.nameCheckCompare(o);
}
}
// Sort steel organs to the top.
if (this.steelOrgan) {
return -1;
} else if (o.steelOrgan) {
return 1;
}
// Sort helpers to the top next.
if (this.isHelper()) {
return -1;
} else if (o.isHelper()) {
return 1;
}
if (Preferences.getBoolean("sortByRoom")) {
int limit;
boolean thisCantConsume = false;
boolean oCantConsume = false;
switch (this.type) {
case FOOD -> {
limit =
KoLCharacter.getFullnessLimit()
- KoLCharacter.getFullness()
- ConcoctionDatabase.getQueuedFullness();
thisCantConsume = this.getFullness() > limit;
oCantConsume = o.getFullness() > limit;
}
case BOOZE -> {
limit =
KoLCharacter.getInebrietyLimit()
- KoLCharacter.getInebriety()
- ConcoctionDatabase.getQueuedInebriety();
thisCantConsume = this.getInebriety() > limit;
oCantConsume = o.getInebriety() > limit;
}
case SPLEEN -> {
limit =
KoLCharacter.getSpleenLimit()
- KoLCharacter.getSpleenUse()
- ConcoctionDatabase.getQueuedSpleenHit();
thisCantConsume = this.getSpleenHit() > limit;
oCantConsume = o.getSpleenHit() > limit;
}
}
if (thisCantConsume != oCantConsume) {
return thisCantConsume ? 1 : -1;
}
}
double adventures1 = ConsumablesDatabase.getAverageAdventures(this.consumable);
double adventures2 = ConsumablesDatabase.getAverageAdventures(o.consumable);
if (adventures1 != adventures2) {
return adventures2 > adventures1 ? 1 : -1;
}
int fullness1 = this.getFullness();
int fullness2 = o.getFullness();
if (fullness1 != fullness2) {
return fullness2 - fullness1;
}
int inebriety1 = this.getInebriety();
int inebriety2 = o.getInebriety();
if (inebriety1 != inebriety2) {
return inebriety2 - inebriety1;
}
int spleenhit1 = this.getSpleenHit();
int spleenhit2 = o.getSpleenHit();
if (spleenhit1 != spleenhit2) {
return spleenhit2 - spleenhit1;
}
double gain1 = this.mainstatGain;
double gain2 = o.mainstatGain;
if (gain1 != gain2) {
return gain2 > gain1 ? 1 : -1;
}
return nameCheckCompare(o);
}
public AdventureResult getItem() {
return this.concoction;
}
public int getItemId() {
return this.concoction == null ? -1 : this.concoction.getItemId();
}
public String getName() {
return this.name;
}
public int getInitial() {
return this.initial;
}
public int getAvailable() {
return this.visibleTotal;
}
public int getTurnFreeAvailable() {
return this.freeTotal;
}
public int getQueued() {
return this.queued;
}
public int getPrice() {
return this.price;
}
public Integer getRawFullness() {
return this.consumable != null ? this.consumable.getRawFullness() : null;
}
public int getFullness() {
return this.consumable != null ? this.consumable.getFullness() : 0;
}
public Integer getRawInebriety() {
return this.consumable != null ? this.consumable.getRawInebriety() : null;
}
public int getInebriety() {
return this.consumable != null ? this.consumable.getInebriety() : 0;
}
public Integer getRawSpleenHit() {
return this.consumable != null ? this.consumable.getRawSpleenHit() : null;
}
public int getSpleenHit() {
return this.consumable != null ? this.consumable.getSpleenHit() : 0;
}
public String getEffectName() {
return this.effectName;
}
public int getRow() {
return this.row;
}
public CreateItemRequest getRequest() {
if (this.request == null && this.mixingMethod != CraftingType.NOCREATE) {
this.request = CreateItemRequest.constructInstance(this);
}
return this.request;
}
public PurchaseRequest getPurchaseRequest() {
return this.purchaseRequest;
}
public void setPurchaseRequest(final PurchaseRequest purchaseRequest) {
this.purchaseRequest = purchaseRequest;
}
public boolean hasIngredients(final AdventureResult[] ingredients) {
AdventureResult[] ingredientTest = this.ingredientArray;
if (ingredientTest.length != ingredients.length) {
return false;
}
int[] ingredientTestIds = new int[ingredients.length];
for (int j = 0; j < ingredientTestIds.length; ++j) {
ingredientTestIds[j] = ingredientTest[j].getItemId();
}
boolean foundMatch = true;
for (int j = 0; j < ingredients.length && foundMatch; ++j) {
foundMatch = false;
for (int k = 0; k < ingredientTestIds.length && !foundMatch; ++k) {
foundMatch = ingredients[j].getItemId() == ingredientTestIds[k];
if (foundMatch) {
ingredientTestIds[k] = -1;
}
}
}
return foundMatch;
}
public void queue(
final List<AdventureResult> globalChanges,
final List<AdventureResult> localChanges,
final int amount) {
this.queue(globalChanges, localChanges, amount, true);
}
public void queue(
final List<AdventureResult> globalChanges,
final List<AdventureResult> localChanges,
final int amount,
boolean adjust) {
if (amount <= 0) {
return;
}
if (this.concoction == null) {
if (adjust) {
this.queued += amount;
}
return;
}
int decrementAmount = Math.min(this.initial, amount);
int creatableAmount = Math.max(this.creatable, 0);
int overAmount = Math.min(creatableAmount, amount - decrementAmount);
int pullAmount = amount - decrementAmount - overAmount;
if (this.price > 0 || this.property != null || this.special) {
pullAmount = 0;
}
if (pullAmount != 0) {
ConcoctionDatabase.queuedPullsUsed += pullAmount;
}
// Tiny plastic swords and Legend keys are special in that they
// are not used up.
if (this.concoction.getItemId() != ItemPool.PLASTIC_SWORD
&& this.concoction.getItemId() != ItemPool.BORIS_KEY
&& this.concoction.getItemId() != ItemPool.JARLSBERG_KEY
&& this.concoction.getItemId() != ItemPool.SNEAKY_PETE_KEY) {
AdventureResult ingredient = this.concoction.getInstance(decrementAmount);
AdventureResult.addResultToList(globalChanges, ingredient);
AdventureResult.addResultToList(localChanges, ingredient);
}
int advs = ConcoctionDatabase.getAdventureUsage(this.mixingMethod) * overAmount;
if (advs != 0) {
for (int i = 0; i < advs; ++i) {
if (ConcoctionDatabase.queuedFreeCraftingTurns
< ConcoctionDatabase.getFreeCraftingTurns()) {
++ConcoctionDatabase.queuedFreeCraftingTurns;
} else ++ConcoctionDatabase.queuedAdventuresUsed;
}
}
if (this.mixingMethod == CraftingType.STILL) {
ConcoctionDatabase.queuedStillsUsed += overAmount;
}
if (this.mixingMethod == CraftingType.CLIPART) {
ConcoctionDatabase.queuedTomesUsed += overAmount;
}
if (this.mixingMethod == CraftingType.TERMINAL) {
ConcoctionDatabase.queuedExtrudesUsed += overAmount;
}
if (adjust) {
this.queued += amount;
this.queuedPulls += pullAmount;
}
if (this.price > 0) {
ConcoctionDatabase.queuedMeatSpent += this.price * (amount - decrementAmount - overAmount);
}
if (this.price > 0
|| this.property != null
|| this.special
|| !ConcoctionDatabase.isPermittedMethod(this.mixingMethod, this.mixingRequirements)) {
return;
}
// Recipes that yield multiple units require smaller
// quantities of ingredients.
int mult = this.getYield();
int icount = (overAmount + (mult - 1)) / mult;
for (AdventureResult ingredient : this.ingredientArray) {
Concoction c = ConcoctionPool.get(ingredient);
if (c == null) {
continue;
}
c.queue(globalChanges, localChanges, icount * ingredient.getCount(), false);
}
// Recipes that yield multiple units might result in
// extra product which can be used for other recipes.
// Commented out code could start to handle that
// int excess = mult * icount - overAmount;
// if (excess > 0) {}
}
public static int getAvailableMeat() {
// This package assumes that available Meat fits in an int.
// Currently, we use a long to store it, since there no such limit.
// Additionally, KoL allows at most 2^24-1 items in any location:
// inventory, storage, etc.
//
// Limit Meat we will consider using to an Integer's worth
return (int) Math.min(Integer.MAX_VALUE, KoLCharacter.getAvailableMeat());
}
public final void resetCalculations() {
this.initial = -1;
this.creatable = 0;
this.pullable = 0;
this.mallable = 0;
this.total = 0;
this.visibleTotal = 0;
this.allocated = 0;
if (this.speakeasy != null) {
boolean available = ClanLoungeRequest.availableSpeakeasyDrink(this.speakeasy);
int affordableNumber = Concoction.getAvailableMeat() / this.price;
int drinkableNumber = 3 - Preferences.getInteger("_speakeasyDrinksDrunk");
this.initial = available ? Math.min(affordableNumber, drinkableNumber) : 0;
this.creatable = 0;
this.total = this.initial;
this.freeTotal = this.initial;
this.visibleTotal = this.initial;
return;
}
if (this.concoction == null && this.name != null) {
this.initial =
this.price > 0
? Concoction.getAvailableMeat() / this.price
: this.property != null ? Preferences.getInteger(property) : this.special ? 1 : 0;
this.creatable = 0;
this.total = this.initial;
this.freeTotal = this.initial;
this.visibleTotal = this.initial;
}
}
public void setPullable(final int pullable) {
this.pullable = pullable;
this.total += pullable;
this.visibleTotal += pullable;
}
public void setPossible(final boolean wasPossible) {
this.wasPossible = wasPossible;
}
public boolean wasPossible() {
return this.wasPossible;
}
public void resetIngredients() {
this.ingredients.clear();
this.ingredientArray = new AdventureResult[0];
}
public void addIngredient(final AdventureResult ingredient) {
int itemId = ingredient.getItemId();
if (itemId >= 0) {
Set<AdventureResult> uses = ConcoctionDatabase.knownUses.get(itemId);
if (uses == null) {
uses = new TreeSet<>();
ConcoctionDatabase.knownUses.put(ingredient.getItemId(), uses);
}
uses.add(this.concoction);
}
this.ingredients.add(ingredient);
this.ingredientArray = new AdventureResult[this.ingredients.size()];
this.ingredients.toArray(this.ingredientArray);
}
// Allow an arbitrary parameter to be set, to indicate creation details
// that can't be expressed via ingredients.
public void setParam(int param) {
this.param = param;
}
public int getParam() {
return this.param;
}
public CraftingType getMixingMethod() {
return this.mixingMethod;
}
public void setMixingMethod(final CraftingType mixingMethod) {
this.mixingMethod = mixingMethod;
}
public EnumSet<CraftingRequirements> getRequirements() {
return this.mixingRequirements;
}
public EnumSet<CraftingMisc> getMisc() {
return this.mixingMisc;
}
public AdventureResult[] getIngredients() {
return this.ingredientArray;
}
public void calculate2() {
if (this.speakeasy != null) {
return;
}
int id = this.getItemId();
int maxSuccess = this.initial;
int minFailure = Integer.MAX_VALUE;
int guess = maxSuccess + 1;
ArrayList<Concoction> visited = new ArrayList<>();
if (id == Concoction.debugId) {
Concoction.debug = true;
}
while (true) {
int res = this.canMake(guess, visited);
if (Concoction.debug) {
RequestLogger.printLine(this.name + ".canMake(" + guess + ") => " + res);
RequestLogger.printLine();
}
if (res >= guess) { // guess was good, try a higher guess
maxSuccess = guess;
} else { // guess was too high, try lower
minFailure = guess;
// In this situation, the algorithm tends to produce estimates
// that are way too low. Bump it up to the midpoint of the
// range, so that the worst-case behavior is the O(log n) of
// a binary search, not the O(n) of a linear search.
res = Math.max(res, (maxSuccess + minFailure) / 2);
}
if (maxSuccess + 1 >= minFailure) break;
guess = Math.min(Math.max(res, maxSuccess + 1), minFailure - 1);
for (Concoction c : visited) { // clean up for next iteration of this item
c.allocated = 0;
}
}
for (Concoction c : visited) { // clean up for next item
c.allocated = 0;
c.visited = false;
}
if (id == Concoction.debugId) {
Concoction.debug = false;
}
this.total = maxSuccess;
this.creatable = this.total - this.initial;
if (this.price > 0
&& id != ItemPool.MEAT_PASTE
&& id != ItemPool.MEAT_STACK
&& id != ItemPool.DENSE_STACK) {
this.creatable -= Concoction.getAvailableMeat() / this.price;
}
this.visibleTotal = this.total;
}
// Like calculate2, but just calculates turn-free creations.
public void calculate3() {
if (this.speakeasy != null) {
return;
}
int maxSuccess = this.initial;
int minFailure = Integer.MAX_VALUE;
int guess = maxSuccess + 1;
ArrayList<Concoction> visited = new ArrayList<>();
while (true) {
int res = this.canMake(guess, visited, true);
if (res >= guess) {
maxSuccess = guess;
} else {
minFailure = guess;
res = Math.max(res, (maxSuccess + minFailure) / 2);
}
if (maxSuccess + 1 >= minFailure) break;
guess = Math.min(Math.max(res, maxSuccess + 1), minFailure - 1);
for (Concoction c : visited) {
c.allocated = 0;
}
}
for (Concoction c : visited) {
c.allocated = 0;
c.visited = false;
}
this.freeTotal = maxSuccess;
}
// Determine if the requested amount of this item can be made from
// available ingredients. Return value must be >= requested if true,
// < requested if false. The return value is treated as an estimate
// of the exact amount that can be made, but isn't assumed to be
// accurate. This method will be called with distinct requested
// values until some N is found to be possible, while N+1 is impossible.
private int canMake(int requested, ArrayList<Concoction> visited) {
return canMake(requested, visited, false);
}
private int canMake(int requested, ArrayList<Concoction> visited, boolean turnFreeOnly) {
if (!this.visited) {
visited.add(this);
this.visited = true;
}
int alreadyHave = this.initial - this.allocated;
if (alreadyHave < 0
|| requested <= 0) { // Already overspent this ingredient - either due to it being
// present multiple times in the recipe, or being part of a
// creation loop with insufficient initial quantities.
return 0;
}
this.allocated += requested;
int needToMake = requested - alreadyHave;
if (needToMake > 0 && this.price > 0) {