Skip to content

Commit 2cb3f2a

Browse files
committed
what's this? committing to a pr that's very old and supposedly finished?
move cache, machine tier, recipe tier, and boost function into RecipeContext add CalculatedOutput so that chance logic set amounts for overflowing chances fix XOR logic tests because the behavior of XOR was changed slightly
1 parent 7cbcb5f commit 2cb3f2a

File tree

9 files changed

+235
-183
lines changed

9 files changed

+235
-183
lines changed

src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import gregtech.api.metatileentity.multiblock.ParallelLogicType;
1515
import gregtech.api.recipes.Recipe;
1616
import gregtech.api.recipes.RecipeBuilder;
17+
import gregtech.api.recipes.RecipeContext;
1718
import gregtech.api.recipes.RecipeMap;
1819
import gregtech.api.recipes.logic.IParallelableRecipeLogic;
1920
import gregtech.api.recipes.logic.OCParams;
@@ -74,16 +75,18 @@ public abstract class AbstractRecipeLogic extends MTETrait implements IWorkable,
7475
protected int maxProgressTime;
7576
protected long recipeEUt;
7677
protected List<FluidStack> fluidOutputs;
77-
protected Map<FluidStack, Integer> fluidChancesCache = new Object2IntOpenCustomHashMap<>(
78+
protected final Map<FluidStack, Integer> fluidChancesCache = new Object2IntOpenCustomHashMap<>(
7879
FluidStackHashStrategy.builder()
7980
.compareFluid(true)
8081
.build());
8182
protected List<ItemStack> itemOutputs;
82-
protected Map<ItemStack, Integer> itemChancesCache = new Object2IntOpenCustomHashMap<>(
83+
protected final Map<ItemStack, Integer> itemChancesCache = new Object2IntOpenCustomHashMap<>(
8384
ItemStackHashStrategy.builder()
8485
.compareItem(true)
8586
.compareDamage(true)
8687
.build());
88+
private final RecipeContext<ItemStack> itemContext = new RecipeContext<>(itemChancesCache);
89+
private final RecipeContext<FluidStack> fluidContext = new RecipeContext<>(fluidChancesCache);
8790

8891
protected boolean isActive;
8992
protected boolean workingEnabled = true;
@@ -968,9 +971,11 @@ protected void setupRecipe(@NotNull Recipe recipe) {
968971
RecipeMap<?> map = getRecipeMap();
969972
if (map != null) {
970973
this.fluidOutputs = GTUtility
971-
.copyFluidList(recipe.getResultFluidOutputs(recipeTier, machineTier, map, fluidChancesCache));
974+
.copyFluidList(recipe.getResultFluidOutputs(
975+
fluidContext.update(map.getChanceFunction(), recipeTier, machineTier)));
972976
this.itemOutputs = GTUtility
973-
.copyStackList(recipe.getResultItemOutputs(recipeTier, machineTier, map, itemChancesCache));
977+
.copyStackList(recipe.getResultItemOutputs(
978+
itemContext.update(map.getChanceFunction(), recipeTier, machineTier)));
974979
}
975980

976981
if (this.wasActiveAndNeedsUpdate) {

src/main/java/gregtech/api/capability/impl/miner/MinerLogic.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import gregtech.api.capability.IMiner;
55
import gregtech.api.metatileentity.MetaTileEntity;
66
import gregtech.api.recipes.Recipe;
7+
import gregtech.api.recipes.RecipeContext;
78
import gregtech.api.recipes.RecipeMap;
89
import gregtech.api.unification.OreDictUnifier;
910
import gregtech.api.unification.ore.OrePrefix;
@@ -451,8 +452,9 @@ protected static void applyTieredHammerNoRandomDrops(@NotNull IBlockState blockS
451452
Recipe recipe = map.findRecipe(Long.MAX_VALUE, Collections.singletonList(itemStack), Collections.emptyList());
452453
if (recipe != null && !recipe.getOutputs().isEmpty()) {
453454
drops.clear();
454-
for (ItemStack outputStack : recipe.getResultItemOutputs(GTUtility.getTierByVoltage(recipe.getEUt()), tier,
455-
map)) {
455+
var context = new RecipeContext<ItemStack>()
456+
.update(map.getChanceFunction(), GTUtility.getTierByVoltage(recipe.getEUt()), tier);
457+
for (ItemStack outputStack : recipe.getResultItemOutputs(context)) {
456458
outputStack = outputStack.copy();
457459
if (OreDictUnifier.getPrefix(outputStack) == OrePrefix.crushed) {
458460
if (fortuneLevel > 0) {

src/main/java/gregtech/api/recipes/Recipe.java

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import gregtech.api.capability.IMultipleTankHandler;
44
import gregtech.api.recipes.category.GTRecipeCategory;
5-
import gregtech.api.recipes.chance.boost.ChanceBoostFunction;
65
import gregtech.api.recipes.chance.output.ChancedOutputList;
76
import gregtech.api.recipes.chance.output.ChancedOutputLogic;
87
import gregtech.api.recipes.chance.output.impl.ChancedFluidOutput;
@@ -18,7 +17,6 @@
1817
import net.minecraft.item.ItemStack;
1918
import net.minecraftforge.fluids.FluidStack;
2019
import net.minecraftforge.items.IItemHandlerModifiable;
21-
import net.minecraftforge.items.ItemHandlerHelper;
2220
import net.minecraftforge.oredict.OreDictionary;
2321

2422
import com.google.common.collect.ImmutableList;
@@ -464,49 +462,41 @@ public List<ItemStack> getOutputs() {
464462
* The Recipe should be trimmed by calling {@link Recipe#getItemAndChanceOutputs(int)} before calling this method,
465463
* if trimming is required.
466464
*
467-
* @param recipeTier The Voltage Tier of the Recipe, used for chanced output calculation
468-
* @param machineTier The Voltage Tier of the Machine, used for chanced output calculation
469-
* @param recipeMap The RecipeMap that the recipe is being performed upon, used for chanced output calculation
465+
* @param context Context containing machine and recipe tier, the boost function, and the chance cache
470466
* @return A list of all resulting ItemStacks from the recipe, after chance has been applied to any chanced outputs
471467
*/
472-
public List<ItemStack> getResultItemOutputs(int recipeTier, int machineTier, RecipeMap<?> recipeMap,
473-
Map<ItemStack, Integer> cache) {
468+
public List<ItemStack> getResultItemOutputs(RecipeContext<ItemStack> context) {
474469
List<ItemStack> outputs = new ArrayList<>(getOutputs());
475-
ChanceBoostFunction function = recipeMap.getChanceFunction();
476-
List<ChancedItemOutput> chancedOutputsList = getChancedOutputs().roll(function, recipeTier, machineTier, cache);
470+
var chancedOutputsList = getChancedOutputs().roll(context);
477471

478472
if (chancedOutputsList == null) return outputs;
479473

480474
Collection<ItemStack> resultChanced = new ArrayList<>();
481-
for (ChancedItemOutput chancedOutput : chancedOutputsList) {
482-
ItemStack stackToAdd = chancedOutput.getIngredient().copy();
483-
for (ItemStack stackInList : resultChanced) {
484-
int insertable = stackInList.getMaxStackSize() - stackInList.getCount();
485-
if (insertable > 0 && ItemHandlerHelper.canItemStacksStack(stackInList, stackToAdd)) {
486-
if (insertable >= stackToAdd.getCount()) {
487-
stackInList.grow(stackToAdd.getCount());
488-
stackToAdd = ItemStack.EMPTY;
489-
break;
490-
} else {
491-
stackInList.grow(insertable);
492-
stackToAdd.shrink(insertable);
493-
}
494-
}
495-
}
496-
if (!stackToAdd.isEmpty()) {
497-
resultChanced.add(stackToAdd);
498-
}
475+
for (var chancedOutput : chancedOutputsList) {
476+
ItemStack stackToAdd = chancedOutput.createStack((output, count) -> GTUtility.copy(count, output));
477+
// for (ItemStack stackInList : resultChanced) {
478+
// int insertable = stackInList.getMaxStackSize() - stackInList.getCount();
479+
// if (insertable > 0 && ItemHandlerHelper.canItemStacksStack(stackInList, stackToAdd)) {
480+
// if (insertable >= stackToAdd.getCount()) {
481+
// stackInList.grow(stackToAdd.getCount());
482+
// stackToAdd = ItemStack.EMPTY;
483+
// break;
484+
// } else {
485+
// stackInList.grow(insertable);
486+
// stackToAdd.shrink(insertable);
487+
// }
488+
// }
489+
// }
490+
// if (!stackToAdd.isEmpty()) {
491+
// }
492+
resultChanced.add(stackToAdd);
499493
}
500494

501495
outputs.addAll(resultChanced);
502496

503497
return outputs;
504498
}
505499

506-
public List<ItemStack> getResultItemOutputs(int recipeTier, int machineTier, RecipeMap<?> recipeMap) {
507-
return getResultItemOutputs(recipeTier, machineTier, recipeMap, null);
508-
}
509-
510500
/**
511501
* Returns the maximum possible recipe outputs from a recipe, divided into regular and chanced outputs
512502
* Takes into account any specific output limiters, ie macerator slots, to trim down the output list
@@ -664,24 +654,19 @@ public List<FluidStack> getAllFluidOutputs() {
664654
* The Recipe should be trimmed by calling {@link Recipe#getFluidAndChanceOutputs(int)} before calling this method,
665655
* if trimming is required.
666656
*
667-
* @param recipeTier The Voltage Tier of the Recipe, used for chanced output calculation
668-
* @param machineTier The Voltage Tier of the Machine, used for chanced output calculation
669-
* @param recipeMap The RecipeMap that the recipe is being performed upon, used for chanced output calculation
657+
* @param context Context containing machine and recipe tier, the boost function, and the chance cache
670658
* @return A list of all resulting ItemStacks from the recipe, after chance has been applied to any chanced outputs
671659
*/
672-
public List<FluidStack> getResultFluidOutputs(int recipeTier, int machineTier, RecipeMap<?> recipeMap,
673-
Map<FluidStack, Integer> cache) {
660+
public List<FluidStack> getResultFluidOutputs(RecipeContext<FluidStack> context) {
674661
List<FluidStack> outputs = new ArrayList<>(GTUtility.copyFluidList(getFluidOutputs()));
675662

676-
ChanceBoostFunction function = recipeMap.getChanceFunction();
677-
List<ChancedFluidOutput> chancedOutputsList = getChancedFluidOutputs().roll(function, recipeTier, machineTier,
678-
cache);
663+
var chancedOutputsList = getChancedFluidOutputs().roll(context);
679664

680665
if (chancedOutputsList == null) return outputs;
681666

682667
Collection<FluidStack> resultChanced = new ArrayList<>();
683-
for (ChancedFluidOutput chancedOutput : chancedOutputsList) {
684-
FluidStack stackToAdd = chancedOutput.getIngredient().copy();
668+
for (var chancedOutput : chancedOutputsList) {
669+
FluidStack stackToAdd = chancedOutput.createStack(FluidStack::new);
685670
for (FluidStack stackInList : resultChanced) {
686671
int insertable = stackInList.amount;
687672
if (insertable > 0 && stackInList.getFluid() == stackToAdd.getFluid()) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package gregtech.api.recipes;
2+
3+
import gregtech.api.recipes.chance.boost.BoostableChanceEntry;
4+
import gregtech.api.recipes.chance.boost.ChanceBoostFunction;
5+
import gregtech.api.recipes.chance.output.ChancedOutput;
6+
7+
import java.util.Map;
8+
9+
public class RecipeContext<I> {
10+
11+
private final Map<I, Integer> cache;
12+
ChanceBoostFunction boostFunction;
13+
public int baseTier, machineTier;
14+
15+
public RecipeContext(Map<I, Integer> cache) {
16+
this.cache = cache;
17+
}
18+
19+
public RecipeContext() {
20+
this(null);
21+
}
22+
23+
public RecipeContext<I> update(ChanceBoostFunction boostFunction,
24+
int baseTier, int machineTier) {
25+
this.boostFunction = boostFunction;
26+
this.baseTier = baseTier;
27+
this.machineTier = machineTier;
28+
return this;
29+
}
30+
31+
public void updateCachedChance(ChancedOutput<I> entry, int chance) {
32+
if (cache == null) return;
33+
cache.put(entry.getIngredient(), chance % entry.getMaxChance());
34+
}
35+
36+
public int getCachedChance(ChancedOutput<I> entry) {
37+
if (cache == null || !cache.containsKey(entry.getIngredient()))
38+
return -1;
39+
40+
return cache.get(entry.getIngredient());
41+
}
42+
43+
public int getChance(ChancedOutput<I> entry) {
44+
int cache = getCachedChance(entry);
45+
if (cache == -1) cache = 0;
46+
if (entry instanceof BoostableChanceEntry<?>boostableChanceEntry) {
47+
return boostChance(boostableChanceEntry) + cache;
48+
}
49+
return entry.getChance() + cache;
50+
}
51+
52+
public int boostChance(BoostableChanceEntry<?> entry) {
53+
return boostFunction.getBoostedChance(entry, baseTier, machineTier);
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package gregtech.api.recipes.chance.output;
2+
3+
public class CalculatedOutput<I> {
4+
5+
ChancedOutput<I> output;
6+
int amount;
7+
8+
public CalculatedOutput(ChancedOutput<I> output, int amount) {
9+
this.output = output;
10+
this.amount = amount;
11+
}
12+
13+
public CalculatedOutput(ChancedOutput<I> output) {
14+
this(output, 1);
15+
}
16+
17+
public I createStack(ChanceConverter<I> converter) {
18+
return converter.convert(output.getIngredient(), amount);
19+
}
20+
21+
public I getIngrediet() {
22+
return output.getIngredient();
23+
}
24+
25+
@FunctionalInterface
26+
public interface ChanceConverter<I> {
27+
28+
I convert(I output, int count);
29+
}
30+
}

src/main/java/gregtech/api/recipes/chance/output/ChancedOutputList.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package gregtech.api.recipes.chance.output;
22

3-
import gregtech.api.recipes.chance.boost.ChanceBoostFunction;
3+
import gregtech.api.recipes.RecipeContext;
44

55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
77
import org.jetbrains.annotations.Unmodifiable;
88

99
import java.util.Collections;
1010
import java.util.List;
11-
import java.util.Map;
1211

1312
/**
1413
* A list of rollable chanced outputs
@@ -34,14 +33,11 @@ public ChancedOutputList(@NotNull ChancedOutputLogic chancedOutputLogic, @NotNul
3433
/**
3534
* Roll the chances for this output list
3635
*
37-
* @param boostFunction the function used to boost the outputs
38-
* @param baseTier the base tier of the recipe
39-
* @param machineTier the tier the recipe is run at
36+
* @param context Context containing machine and recipe tier, the boost function, and the chance cache
4037
* @return a list of the rolled outputs
4138
*/
42-
public @Nullable @Unmodifiable List<T> roll(@NotNull ChanceBoostFunction boostFunction, int baseTier,
43-
int machineTier, Map<I, Integer> cache) {
44-
return chancedOutputLogic.roll(getChancedEntries(), boostFunction, baseTier, machineTier, cache);
39+
public @Nullable @Unmodifiable List<CalculatedOutput<I>> roll(@NotNull RecipeContext<I> context) {
40+
return chancedOutputLogic.roll(getChancedEntries(), context);
4541
}
4642

4743
public @NotNull ChancedOutputLogic getChancedOutputLogic() {

0 commit comments

Comments
 (0)