Skip to content

Commit

Permalink
Fix a long-standing bug in GTCE's chanced outputs computation (#7)
Browse files Browse the repository at this point in the history
- supply chance function with a computed overclocking tier delta rather than the tier ordinal of the machine
  • Loading branch information
Exaxxion authored Jun 3, 2024
1 parent c68049f commit f193ca9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ protected void setupRecipe(Recipe recipe) {
setMaxProgress(resultOverclock[1]);
this.recipeEUt = resultOverclock[0];
this.fluidOutputs = GTUtility.copyFluidList(recipe.getFluidOutputs());
int tier = getMachineTierForRecipe(recipe);
this.itemOutputs = GTUtility.copyStackList(recipe.getResultItemOutputs(getOutputInventory().getSlots(), random, tier));
int overclocks = getMachineTierForRecipe(recipe) - recipe.getBaseTier();
this.itemOutputs = GTUtility.copyStackList(recipe.getResultItemOutputs(getOutputInventory().getSlots(), random, overclocks));
if (this.wasActiveAndNeedsUpdate) {
this.wasActiveAndNeedsUpdate = false;
} else {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/gregtech/api/recipes/Recipe.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.recipes;

import com.google.common.collect.ImmutableList;
import gregtech.api.GTValues;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.recipes.recipeproperties.RecipeProperty;
import gregtech.api.recipes.recipeproperties.RecipePropertyStorage;
Expand All @@ -15,6 +16,9 @@
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Arrays.binarySearch;
import static net.minecraft.util.math.MathHelper.abs;

/**
* Class that represent machine recipe.<p>
* <p>
Expand Down Expand Up @@ -54,6 +58,11 @@ public static String formatChanceValue(int outputChance) {
*/
private final int EUt;

/**
* GTValues ordinal for this Recipe's base power tier
*/
private int baseTier = -1;

/**
* If this Recipe is hidden from JEI
*/
Expand Down Expand Up @@ -320,6 +329,22 @@ public int getEUt() {
return EUt;
}

/**
* @return The tier of machine required to run this recipe, as defined by {@link GTValues}
*/
public int getBaseTier() {

// Lazily compute and store for later recall
if(baseTier == -1) {
int tier = binarySearch(GTValues.V, abs(EUt));
if(tier < 0)
tier = abs(tier + 1);
baseTier = tier;
}

return baseTier;
}

public boolean isHidden() {
return hidden;
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,29 @@ public String toString() {
'}';
}

/**
* A ChanceFunction specifies alternative behavior applied to all chanced outputs, overriding
* the default {@code RecipeMap.chanceFunction}. The default behavior is additive, applying
* a flat bonus multiplied by the number of tiers the machine is above the base recipe tier.<br />
* <br />
* All integer parameters to this function and the resulting value express a "chance value" which
* is a percent chance out of 100 with two decimal places, multiplied by 100 to produce a resulting
* integer value. For example {@code 10000} is 100% and {@code 750} is 7.5%.<br />
* <br />
* Values can exceed 100% but chanced outputs will simply be guaranteed at or above 100% (you won't get twice
* the amount with 200%, for example). As long as the result of {@code chanceFor} is of the appropriate format,
* how exactly you compute the result doesn't really matter.
*/
@FunctionalInterface
@ZenClass("mods.gregtech.recipe.IChanceFunction")
@ZenRegister
public interface IChanceFunction {
/**
* @param chance the base chance value
* @param boostPerTier the increase in chance value to apply per tier of boost
* @param boostTier the number of times to apply {@code boostPerTier}
* @return the computed chance value
*/
int chanceFor(int chance, int boostPerTier, int boostTier);
}
}

0 comments on commit f193ca9

Please sign in to comment.