Skip to content

Commit

Permalink
Various Recipe Logic fixes (#1880)
Browse files Browse the repository at this point in the history
Co-authored-by: YoungOnionMC <[email protected]>
  • Loading branch information
YoungOnionMC and YoungOnionMC authored Sep 2, 2024
1 parent ba66a92 commit 78b7416
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,35 @@ public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int mu
.map(IFluidTransfer.class::cast)
.toList()));

List<FluidStack> recipeOutputs = recipe.getOutputContents(FluidRecipeCapability.CAP)
.stream()
.map(content -> FluidRecipeCapability.CAP.of(content.getContent()))
.filter(ingredient -> !ingredient.isEmpty())
.map(ingredient -> ingredient.getStacks()[0])
.toList();

while (minMultiplier != maxMultiplier) {
overlayedFluidHandler.reset();

long amountLeft = 0;
long returnedAmount = 0;
long amountToInsert = 0;

for (FluidStack fluidStack : recipe.getOutputContents(FluidRecipeCapability.CAP)
.stream()
.map(FluidRecipeCapability.CAP::of)
.filter(ingredient -> !ingredient.isEmpty())
.map(ingredient -> ingredient.getStacks()[0])
.toList()) {
for (FluidStack fluidStack : recipeOutputs) {
if (fluidStack.getAmount() <= 0) continue;
if (fluidStack.isEmpty()) continue;
// Since multiplier starts at Int.MAX, check here for integer overflow
if (multiplier > Integer.MAX_VALUE / fluidStack.getAmount()) {
amountLeft = Integer.MAX_VALUE;
amountToInsert = Integer.MAX_VALUE;
} else {
amountLeft = fluidStack.getAmount() * multiplier;
}
long inserted = overlayedFluidHandler.insertFluid(fluidStack, amountLeft);
if (inserted > 0) {
amountLeft -= inserted;
amountToInsert = fluidStack.getAmount() * multiplier;
}
if (amountLeft > 0) {
returnedAmount = amountToInsert - overlayedFluidHandler.insertFluid(fluidStack, amountToInsert);
if (returnedAmount > 0) {
break;
}
}

int[] bin = ParallelLogic.adjustMultiplier(amountLeft == 0, minMultiplier, multiplier, maxMultiplier);
int[] bin = ParallelLogic.adjustMultiplier(returnedAmount == 0, minMultiplier, multiplier, maxMultiplier);
minMultiplier = bin[0];
multiplier = bin[1];
maxMultiplier = bin[2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe rec
if (EUt > 0) {
var maxParallel = (int) (Math.min(generator.getOverclockVoltage(),
GTValues.V[generator.getOverclockTier()]) / EUt);
return GTRecipeModifiers.fastParallel(generator, recipe, maxParallel, false).getFirst();
var paraRecipe = GTRecipeModifiers.fastParallel(generator, recipe, maxParallel, false);
result.init(-RecipeHelper.getOutputEUt(paraRecipe.getFirst()), paraRecipe.getFirst().duration,
paraRecipe.getSecond());
return paraRecipe.getFirst();
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe rec
result.init(RecipeHelper.getInputEUt(recipe), modified.duration * 2);
}

if (!steamMachine.isHighPressure) {
modified.duration *= 2;
RecipeHelper.setInputEUt(modified, RecipeHelper.getInputEUt(recipe) / 2);
}

return modified;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.RecipeHelper;
import com.gregtechceu.gtceu.api.recipe.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.common.data.GTMaterials;
Expand Down Expand Up @@ -260,10 +261,10 @@ public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe rec
@NotNull OCResult result) {
if (machine instanceof SteamBoilerMachine boilerMachine) {
recipe = recipe.copy();
result.init(RecipeHelper.getInputEUt(recipe), recipe.duration);
if (boilerMachine.isHighPressure)
result.setDuration(result.getDuration() / 2);
// recipe.duration *= 12; // maybe?
recipe.duration = boilerMachine.isHighPressure ? recipe.duration / 2 : recipe.duration;
return recipe;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static int getRecipeEUtTier(GTRecipe recipe) {
if (EUt == 0) {
EUt = getOutputEUt(recipe);
}
if (recipe.parallels > 1) EUt /= recipe.parallels;
return GTUtil.getTierByVoltage(EUt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ public GTRecipe apply(MetaMachine machine, @NotNull GTRecipe recipe, @NotNull OC

if (modifiedRecipe != null) {
modifiedRecipe.duration = result.getDuration();
modifiedRecipe.tickInputs.put(EURecipeCapability.CAP, List.of(new Content(result.getEut(),
ChanceLogic.getMaxChancedValue(), ChanceLogic.getMaxChancedValue(), 0, null, null)));
if (result.getEut() > 0) {
modifiedRecipe.tickInputs.put(EURecipeCapability.CAP, List.of(new Content(result.getEut(),
ChanceLogic.getMaxChancedValue(), ChanceLogic.getMaxChancedValue(), 0, null, null)));
} else {
modifiedRecipe.tickOutputs.put(EURecipeCapability.CAP, List.of(new Content(-result.getEut(),
ChanceLogic.getMaxChancedValue(), ChanceLogic.getMaxChancedValue(), 0, null, null)));
}

if (result.getParallel() > 1) {
modifiedRecipe = ParallelLogic.applyParallel(machine, modifiedRecipe, result.getParallel(), false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ public static GTRecipe pyrolyseOvenOverclock(MetaMachine machine, @NotNull GTRec

if (coilMachine.getCoilTier() == 0) {
// 75% speed with cupro coils
result.setDuration(Math.max(1, (int) recipe.duration * 4 / 3));
result.setDuration(Math.max(1, (int) result.getDuration() * 4 / 3));
} else {
result.setDuration(Math.max(1, (int) (recipe.duration * 2.0 / (tier + 1))));
result.setDuration(Math.max(1, (int) (result.getDuration() * 2.0 / (tier + 1))));
}
return re;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gregtechceu.gtceu.common.machine.multiblock.generator;

import com.gregtechceu.gtceu.api.GTValues;
import com.gregtechceu.gtceu.api.capability.recipe.EURecipeCapability;
import com.gregtechceu.gtceu.api.capability.recipe.IO;
import com.gregtechceu.gtceu.api.fluids.store.FluidStorageKeys;
import com.gregtechceu.gtceu.api.gui.GuiTextures;
Expand All @@ -13,8 +12,6 @@
import com.gregtechceu.gtceu.api.machine.multiblock.WorkableElectricMultiblockMachine;
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.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.common.data.GTMaterials;
Expand Down Expand Up @@ -122,11 +119,12 @@ public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe rec
if (engineMachine.isOxygenBoosted) { // boost production
recipe = parallelResult.getFirst() == recipe ? recipe.copy() : parallelResult.getFirst();
long eut = (long) (EUt * parallelResult.getSecond() * (engineMachine.isExtreme() ? 2 : 1.5));
recipe.tickOutputs.put(EURecipeCapability.CAP, List.of(new Content(eut,
ChanceLogic.getMaxChancedValue(), ChanceLogic.getMaxChancedValue(), 0, null, null)));
result.init(-eut, recipe.duration, parallelResult.getSecond());
} else {
recipe = parallelResult.getFirst();
result.init(-RecipeHelper.getOutputEUt(recipe), recipe.duration, parallelResult.getSecond());
}

return recipe;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import com.gregtechceu.gtceu.api.machine.multiblock.WorkableElectricMultiblockMachine;
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.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.common.data.GTRecipeModifiers;
Expand Down Expand Up @@ -116,8 +114,8 @@ public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe rec
recipe = parallelResult.getFirst() == recipe ? recipe.copy() : parallelResult.getFirst();

long eut = turbineMachine.boostProduction((long) (EUt * holderEfficiency * parallelResult.getSecond()));
recipe.tickOutputs.put(EURecipeCapability.CAP, List.of(new Content(eut,
ChanceLogic.getMaxChancedValue(), ChanceLogic.getMaxChancedValue(), 0, null, null)));

result.init(-eut, recipe.duration, parallelResult.getSecond());

return recipe;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank;
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.logic.OCParams;
import com.gregtechceu.gtceu.api.recipe.logic.OCResult;
import com.gregtechceu.gtceu.common.data.GTMaterials;
Expand Down Expand Up @@ -81,15 +79,11 @@ public static GTRecipe recipeModifier(MetaMachine machine, @NotNull GTRecipe rec
int duration = recipe.duration;
var eut = RecipeHelper.getInputEUt(recipe);
var parallelRecipe = GTRecipeModifiers.accurateParallel(machine, recipe, MAX_PARALLELS, false);
recipe = parallelRecipe.getFirst() == recipe ? parallelRecipe.getFirst().copy() : parallelRecipe.getFirst();
recipe = parallelRecipe.getFirst() == recipe ? recipe : parallelRecipe.getFirst();

// we remove tick inputs, as our "cost" is just steam now, just stored as EU/t
// also set the duration to just 1.5x the original, instead of fully multiplied
result.setDuration((int) (duration * 1.5));
result.setEut((long) Math.min(32, Math.ceil(eut * 1.33)));
result.setParallel(parallelRecipe.getSecond());
recipe.tickInputs.put(EURecipeCapability.CAP, List.of(new Content(eut,
ChanceLogic.getMaxChancedValue(), ChanceLogic.getMaxChancedValue(), 0, null, null)));
result.init((long) Math.min(32, Math.ceil(eut * 1.33)), (int) (duration * 1.5), parallelRecipe.getSecond());
return recipe;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import com.gregtechceu.gtceu.integration.ae2.utils.KeyStorage;

import com.lowdragmc.lowdraglib.gui.widget.LabelWidget;
import com.lowdragmc.lowdraglib.gui.widget.SlotWidget;
import com.lowdragmc.lowdraglib.gui.widget.Widget;
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
import com.lowdragmc.lowdraglib.misc.ItemStackTransfer;
import com.lowdragmc.lowdraglib.syncdata.ISubscription;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;

Expand All @@ -23,6 +25,8 @@

import appeng.api.config.Actionable;
import appeng.api.stacks.AEItemKey;
import appeng.items.materials.StorageComponentItem;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.Nullable;

Expand All @@ -44,6 +48,14 @@ public class MEOutputBusPartMachine extends MEBusPartMachine implements IMachine

@Persisted
private KeyStorage internalBuffer; // Do not use KeyCounter, use our simple implementation
@Getter
@Persisted
protected NotifiableItemStackHandler storageSlot;

@Nullable
protected ISubscription storageSub;

private long capacitySize = 0;

public MEOutputBusPartMachine(IMachineBlockEntity holder, Object... args) {
super(holder, IO.OUT, args);
Expand All @@ -56,9 +68,41 @@ public MEOutputBusPartMachine(IMachineBlockEntity holder, Object... args) {
@Override
protected NotifiableItemStackHandler createInventory(Object... args) {
this.internalBuffer = new KeyStorage();
this.storageSlot = new NotifiableItemStackHandler(this, 1, io);
this.storageSlot.setFilter(item -> canInsertCell(item));
return new InaccessibleInfiniteHandler(this);
}

@Override
public void onLoad() {
super.onLoad();
if (isRemote()) return;

storageSub = storageSlot.addChangedListener(this::updateStorageSize);
updateStorageSize();
}

private boolean canInsertCell(ItemStack item) {
var grid = getMainNode().getGrid();
if (item.getItem() instanceof StorageComponentItem compItem) {
long newSize = (long) compItem.getBytes(item) * 8L;
if (newSize >= capacitySize) {
return true;
} else {
return ((InaccessibleInfiniteHandler) (getInventory())).getCachedAmount() >= newSize;
}
}
return false;
}

private void updateStorageSize() {
if (this.storageSlot.getStackInSlot(0).getItem() instanceof StorageComponentItem compItem) {
capacitySize = (compItem.getBytes(this.storageSlot.getStackInSlot(0)) * 8L);
} else if (this.storageSlot.getStackInSlot(0).isEmpty()) {
capacitySize = 64L;
}
}

@Override
public void onMachineRemoved() {
var grid = getMainNode().getGrid();
Expand Down Expand Up @@ -110,6 +154,7 @@ public Widget createUIWidget() {
"gtceu.gui.me_network.offline"));
group.addWidget(new LabelWidget(5, 10, "gtceu.gui.waiting_list"));
// display list
group.addWidget(new SlotWidget(storageSlot.storage, 0, 140, 0));
group.addWidget(new AEListGridWidget.Item(5, 20, 3, this.internalBuffer));

return group;
Expand All @@ -136,9 +181,25 @@ public int getSize() {
return Integer.MAX_VALUE;
}

private long getCachedAmount() {
long itemAmount = 0;
var grid = getMainNode().getGrid();
if (grid != null && !internalBuffer.isEmpty()) {
for (var slot : internalBuffer) {
itemAmount += grid.getStorageService().getInventory().getAvailableStacks()
.get(slot.getKey());
}
}
return itemAmount;
}

private boolean canInsertItem() {
return getCachedAmount() < capacitySize;
}

@Override
public int getSlotLimit(int slot) {
return Integer.MAX_VALUE; // todo add me components for sizing
return Integer.MAX_VALUE;
}

@Override
Expand Down Expand Up @@ -177,15 +238,18 @@ public ItemStack insertItem(
int count = stack.getCount();
long oldValue = internalBuffer.storage.getOrDefault(key, 0);
long changeValue = Math.min(Long.MAX_VALUE - oldValue, count);
if (changeValue > 0) {
if (!simulate) {
internalBuffer.storage.put(key, oldValue + changeValue);
internalBuffer.onChanged();
if (canInsertItem()) {
if (changeValue > 0) {
if (!simulate) {
internalBuffer.storage.put(key, oldValue + changeValue);
internalBuffer.onChanged();
}
return stack.copyWithCount((int) (count - changeValue));
} else {
return ItemStack.EMPTY;
}
return stack.copyWithCount((int) (count - changeValue));
} else {
return ItemStack.EMPTY;
}
return ItemStack.EMPTY;
}

@Override
Expand Down
Loading

0 comments on commit 78b7416

Please sign in to comment.