Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RecipeModifier and OC system #2499

Merged
merged 11 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Integer copyInner(Integer content) {

@Override
public Integer copyWithModifier(Integer content, ContentModifier modifier) {
return modifier.apply(content).intValue();
return modifier.apply(content);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import com.gregtechceu.gtceu.api.machine.feature.ITieredMachine;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.RecipeHelper;
import com.gregtechceu.gtceu.api.recipe.chance.logic.ChanceLogic;
import com.gregtechceu.gtceu.api.recipe.content.Content;
import com.gregtechceu.gtceu.api.recipe.content.ContentModifier;
import com.gregtechceu.gtceu.api.recipe.content.SerializerLong;
import com.gregtechceu.gtceu.utils.GTMath;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
* @author KilaBash
Expand All @@ -31,7 +34,7 @@ public Long copyInner(Long content) {

@Override
public Long copyWithModifier(Long content, ContentModifier modifier) {
return modifier.apply(content).longValue();
return modifier.apply(content);
}

@Override
Expand Down Expand Up @@ -73,6 +76,27 @@ public int getMaxParallelRatio(IRecipeCapabilityHolder holder, GTRecipe recipe,
return Math.abs(GTMath.saturatedCast(maxVoltage / recipeEUt));
}

/**
* Creates a {@code List<Content>} with the specified EU
*
* @param eu EU/t value to put in the Content
* @return Singleton list of a new Content with the given EU value
*/
public static List<Content> makeEUContent(Long eu) {
return List.of(
new Content(eu, ChanceLogic.getMaxChancedValue(), ChanceLogic.getMaxChancedValue(), 0, null, null));
}

/**
* Puts an EU Singleton Content in the given content map
*
* @param contents content map
* @param eu EU value to put inside content map
*/
public static void putEUContent(Map<RecipeCapability<?>, List<Content>> contents, long eu) {
contents.put(EURecipeCapability.CAP, makeEUContent(eu));
}

public interface ICustomParallel {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public FluidIngredient copyInner(FluidIngredient content) {
public FluidIngredient copyWithModifier(FluidIngredient content, ContentModifier modifier) {
if (content.isEmpty()) return content.copy();
FluidIngredient copy = content.copy();
copy.setAmount(modifier.apply(copy.getAmount()).intValue());
copy.setAmount(modifier.apply(copy.getAmount()));
return copy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ public Ingredient copyInner(Ingredient content) {
public Ingredient copyWithModifier(Ingredient content, ContentModifier modifier) {
if (content instanceof SizedIngredient sizedIngredient) {
return SizedIngredient.create(sizedIngredient.getInner(),
modifier.apply(sizedIngredient.getAmount()).intValue());
modifier.apply(sizedIngredient.getAmount()));
} else if (content instanceof IntProviderIngredient intProviderIngredient) {
return new IntProviderIngredient(intProviderIngredient.getInner(),
new FlooredInt(
new AddedFloat(
new MultipliedFloat(
new CastedFloat(intProviderIngredient.getCountProvider()),
ConstantFloat.of((float) modifier.getMultiplier())),
ConstantFloat.of((float) modifier.getAddition()))));
ConstantFloat.of((float) modifier.multiplier())),
ConstantFloat.of((float) modifier.addition()))));
}
return SizedIngredient.create(content, modifier.apply(1).intValue());
return SizedIngredient.create(content, modifier.apply(1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ public boolean doMatchInRecipe() {
* @param recipe the recipe from which we get the input to product ratio
* @param holder the {@link IRecipeCapabilityHolder} that contains all the inputs and outputs of the machine.
* @param multiplier the maximum possible multiplied we can get from the input inventory
* see {@link ParallelLogic#getMaxRecipeMultiplier(GTRecipe, IRecipeCapabilityHolder, int)}
* see {@link ParallelLogic#limitByInput}
* @return the amount of times a {@link GTRecipe} outputs can be merged into an inventory without voiding products.
*/
// returns Integer.MAX_VALUE by default, to skip processing.
// TODO: kross - make it so caps check both regular outputs and tick outputs
public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int multiplier) {
return Integer.MAX_VALUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.RecipeHelper;
import com.gregtechceu.gtceu.api.recipe.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.api.recipe.content.ContentModifier;
import com.gregtechceu.gtceu.api.recipe.modifier.ModifierFunction;
import com.gregtechceu.gtceu.api.recipe.modifier.ParallelLogic;
import com.gregtechceu.gtceu.api.recipe.modifier.RecipeModifier;
import com.gregtechceu.gtceu.api.recipe.ui.GTRecipeTypeUI;
import com.gregtechceu.gtceu.common.data.GTRecipeModifiers;
import com.gregtechceu.gtceu.utils.GTMath;

import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
import com.lowdragmc.lowdraglib.utils.Position;
Expand All @@ -28,7 +28,6 @@
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.EnumMap;
Expand Down Expand Up @@ -94,21 +93,32 @@ public int tintColor(int index) {
// ****** RECIPE LOGIC *******//
//////////////////////////////////////

@Nullable
public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe recipe, @NotNull OCParams params,
@NotNull OCResult result) {
if (machine instanceof SimpleGeneratorMachine generator) {
var EUt = RecipeHelper.getOutputEUt(recipe);
if (EUt > 0) {
var maxParallel = GTMath.saturatedCast(Math.min(generator.getOverclockVoltage(),
GTValues.V[generator.getOverclockTier()]) / EUt);
var paraRecipe = GTRecipeModifiers.fastParallel(generator, recipe, maxParallel, false);
result.init(-RecipeHelper.getOutputEUt(paraRecipe.getFirst()), paraRecipe.getFirst().duration,
paraRecipe.getSecond(), params.getOcAmount());
return paraRecipe.getFirst();
}
/**
* Recipe Modifier for <b>Simple Generator Machines</b> - can be used as a valid {@link RecipeModifier}
* <p>
* Recipe is fast parallelized up to {@code desiredEUt / recipeEUt} times.
* </p>
*
* @param machine a {@link SimpleGeneratorMachine}
* @param recipe recipe
* @return A {@link ModifierFunction} for the given Simple Generator
*/
public static ModifierFunction recipeModifier(@NotNull MetaMachine machine, @NotNull GTRecipe recipe) {
if (!(machine instanceof SimpleGeneratorMachine generator)) {
return RecipeModifier.nullWrongType(SimpleGeneratorMachine.class, machine);
}
return null;
long EUt = RecipeHelper.getOutputEUt(recipe);
if (EUt <= 0) return ModifierFunction.NULL;

int maxParallel = (int) (generator.getOverclockVoltage() / EUt);
int parallels = ParallelLogic.getParallelAmountFast(generator, recipe, maxParallel);

return ModifierFunction.builder()
.inputModifier(ContentModifier.multiplier(parallels))
.outputModifier(ContentModifier.multiplier(parallels))
.eutMultiplier(parallels)
.parallels(parallels)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import com.gregtechceu.gtceu.api.machine.trait.RecipeLogic;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.config.ConfigHolder;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -46,20 +44,20 @@ default void notifyStatusChanged(RecipeLogic.Status oldStatus, RecipeLogic.Statu
@NotNull
RecipeLogic getRecipeLogic();

default GTRecipe fullModifyRecipe(GTRecipe recipe, @NotNull OCParams params, @NotNull OCResult result) {
return doModifyRecipe(recipe.trimRecipeOutputs(this.getOutputLimits()), params, result);
default GTRecipe fullModifyRecipe(GTRecipe recipe) {
return doModifyRecipe(recipe.trimRecipeOutputs(this.getOutputLimits()));
}

/**
* Override it to modify recipe on the fly e.g. applying overclock, change chance, etc
*
*
* @param recipe recipe from detected from GTRecipeType
* @return modified recipe.
* null -- this recipe is unavailable
*/
@Nullable
default GTRecipe doModifyRecipe(GTRecipe recipe, @NotNull OCParams params, @NotNull OCResult result) {
return self().getDefinition().getRecipeModifier().apply(self(), recipe, params, result);
default GTRecipe doModifyRecipe(GTRecipe recipe) {
return self().getDefinition().getRecipeModifier().applyModifier(self(), recipe);
}

/**
Expand Down Expand Up @@ -114,7 +112,7 @@ default boolean dampingWhenWaiting() {
}

/**
* Always try {@link IRecipeLogicMachine#fullModifyRecipe(GTRecipe, OCParams, OCResult)} before setting up recipe.
* Always try {@link IRecipeLogicMachine#fullModifyRecipe(GTRecipe)} before setting up recipe.
*
* @return true - will map {@link RecipeLogic#lastOriginRecipe} to the latest recipe for next round when finishing.
* false - keep using the {@link RecipeLogic#lastRecipe}, which is already modified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import com.gregtechceu.gtceu.api.machine.trait.RecipeLogic;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;

import com.lowdragmc.lowdraglib.syncdata.ISubscription;
import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
Expand Down Expand Up @@ -191,17 +189,17 @@ public void clientTick() {

@Nullable
@Override
public final GTRecipe doModifyRecipe(GTRecipe recipe, @NotNull OCParams params, @NotNull OCResult result) {
public final GTRecipe doModifyRecipe(GTRecipe recipe) {
for (IMultiPart part : getParts()) {
recipe = part.modifyRecipe(recipe);
if (recipe == null) return null;
}
return getRealRecipe(recipe, params, result);
return getRealRecipe(recipe);
}

@Nullable
protected GTRecipe getRealRecipe(GTRecipe recipe, @NotNull OCParams params, @NotNull OCResult result) {
return self().getDefinition().getRecipeModifier().apply(self(), recipe, params, result);
protected GTRecipe getRealRecipe(GTRecipe recipe) {
return self().getDefinition().getRecipeModifier().applyModifier(self(), recipe);
}

public void updateActiveBlocks(boolean active) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.gregtechceu.gtceu.api.machine.trait.NotifiableItemStackHandler;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.RecipeHelper;
import com.gregtechceu.gtceu.api.recipe.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.api.recipe.modifier.ModifierFunction;
import com.gregtechceu.gtceu.api.recipe.modifier.RecipeModifier;
import com.gregtechceu.gtceu.common.recipe.condition.VentCondition;

import com.lowdragmc.lowdraglib.gui.modular.ModularUI;
Expand All @@ -35,7 +35,6 @@
import com.google.common.collect.Tables;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

Expand Down Expand Up @@ -89,7 +88,7 @@ public void onLoad() {
super.onLoad();
// Fine, we use it to provide eu cap for recipe, simulating an EU machine.
capabilitiesProxy.put(IO.IN, EURecipeCapability.CAP,
List.of(new SteamEnergyRecipeHandler(steamTank, 1d)));
List.of(new SteamEnergyRecipeHandler(steamTank, getConversionRate())));
}

@Override
Expand Down Expand Up @@ -122,30 +121,36 @@ public void markVentingComplete() {
this.needsVenting = false;
}

public double getConversionRate() {
return isHighPressure() ? 2.0 : 1.0;
}

//////////////////////////////////////
// ****** Recipe Logic ******//
//////////////////////////////////////

@Nullable
public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe recipe, @NotNull OCParams params,
@NotNull OCResult result) {
if (machine instanceof SimpleSteamMachine steamMachine) {
if (RecipeHelper.getRecipeEUtTier(recipe) > GTValues.LV || !steamMachine.checkVenting()) {
return null;
}

var modified = recipe.copy();
modified.conditions.add(VentCondition.INSTANCE);

if (steamMachine.isHighPressure) {
result.init(RecipeHelper.getInputEUt(recipe) * 2L, modified.duration, params.getOcAmount());
} else {
result.init(RecipeHelper.getInputEUt(recipe), modified.duration * 2, params.getOcAmount());
}

return modified;
/**
* Recipe Modifier for <b>Simple Steam Machines</b> - can be used as a valid {@link RecipeModifier}
* <p>
* Recipe is rejected if tier is greater than LV or if machine cannot vent.<br>
* Duration is multiplied by {@code 2} if the machine is low pressure
* </p>
*
* @param machine a {@link SimpleSteamMachine}
* @param recipe recipe
* @return A {@link ModifierFunction} for the given Steam Machine
*/
public static ModifierFunction recipeModifier(@NotNull MetaMachine machine, @NotNull GTRecipe recipe) {
if (!(machine instanceof SimpleSteamMachine steamMachine)) {
return RecipeModifier.nullWrongType(SimpleSteamMachine.class, machine);
}
if (RecipeHelper.getRecipeEUtTier(recipe) > GTValues.LV || !steamMachine.checkVenting()) {
return ModifierFunction.NULL;
}
return null;

var builder = ModifierFunction.builder().conditions(VentCondition.INSTANCE);
if (!steamMachine.isHighPressure) builder.durationMultiplier(2);
return builder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.gregtechceu.gtceu.api.machine.feature.IUIMachine;
import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.api.recipe.modifier.ModifierFunction;
import com.gregtechceu.gtceu.api.recipe.modifier.RecipeModifier;
import com.gregtechceu.gtceu.common.data.GTMaterials;
import com.gregtechceu.gtceu.common.item.PortableScannerBehavior;
import com.gregtechceu.gtceu.config.ConfigHolder;
Expand Down Expand Up @@ -255,18 +255,24 @@ private double getTemperaturePercent() {

protected abstract long getBaseSteamOutput();

@Nullable
public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe recipe, @NotNull OCParams params,
@NotNull OCResult result) {
if (machine instanceof SteamBoilerMachine boilerMachine) {
recipe = recipe.copy();
result.init(0, recipe.duration, params.getOcAmount());
if (boilerMachine.isHighPressure)
result.setDuration(result.getDuration() / 2);
// recipe.duration *= 12; // maybe?
return recipe;
/**
* Recipe Modifier for <b>Steam Boiler Machines</b> - can be used as a valid {@link RecipeModifier}
* <p>
* Duration is multiplied by {@code 0.5} if the machine is high pressure
*
* @param machine a {@link SteamBoilerMachine}
* @param recipe recipe
* @return A {@link ModifierFunction} for the given Steam Boiler
*/
public static ModifierFunction recipeModifier(@NotNull MetaMachine machine, @NotNull GTRecipe recipe) {
if (!(machine instanceof SteamBoilerMachine boilerMachine)) {
return RecipeModifier.nullWrongType(SteamBoilerMachine.class, machine);
}
return null;
if (!boilerMachine.isHighPressure) return ModifierFunction.IDENTITY;

return ModifierFunction.builder()
.durationMultiplier(0.5)
.build();
}

@Override
Expand Down
Loading
Loading