Skip to content

Commit

Permalink
Fix void protection for mutes (#2298) - fix conflicts
Browse files Browse the repository at this point in the history
* initial variables

* implement working void protection on items and fluids
  • Loading branch information
BlueWeabo committed Dec 27, 2023
1 parent 77efdfe commit b6339fa
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 7 deletions.
26 changes: 24 additions & 2 deletions src/main/java/gregtech/api/util/GT_ParallelHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public class GT_ParallelHelper {
* The inputs of the machine for current recipe check
*/
private ItemInventoryLogic itemInputInventory;
/**
* The output item inventory of the machine
*/
private ItemInventoryLogic itemOutputInventory;
/**
* The outputs of the recipe with the applied parallel
*/
Expand All @@ -76,6 +80,10 @@ public class GT_ParallelHelper {
* The inputs of the machine for the current recipe check
*/
private FluidInventoryLogic fluidInputInventory;
/**
* The output fluid inventory of the machine;
*/
private FluidInventoryLogic fluidOutputInventory;
/**
* The outputs of the recipe with the applied parallel
*/
Expand Down Expand Up @@ -125,7 +133,7 @@ public class GT_ParallelHelper {
* Calculator to use for overclocking
*/
private GT_OverclockCalculator calculator;

@Nonnull
private CheckRecipeResult result = CheckRecipeResultRegistry.NONE;

private Function<Integer, ItemStack[]> customItemOutputCalculation;
Expand Down Expand Up @@ -312,6 +320,17 @@ public GT_ParallelHelper setInputConsumer(InputConsumer inputConsumer) {
this.inputConsumer = inputConsumer;
return this;
}
@Nonnull
public GT_ParallelHelper setItemOutputInventory(ItemInventoryLogic itemOutputInventory) {
this.itemOutputInventory = itemOutputInventory;
return this;
}

@Nonnull
public GT_ParallelHelper setFluidOutputInventory(FluidInventoryLogic fluidOutputInventory) {
this.fluidOutputInventory = fluidOutputInventory;
return this;
}

/**
* Finishes the GT_ParallelHelper. Anything changed after this will not effect anything
Expand Down Expand Up @@ -455,14 +474,17 @@ protected void determineParallel() {

// Let's look at how many parallels we can get with void protection
if (protectExcessItem || protectExcessFluid) {
if (machine == null) {
if (machine == null && !muteMode) {
throw new IllegalStateException("Tried to calculate void protection, but machine is not set");
}
VoidProtectionHelper voidProtectionHelper = new VoidProtectionHelper();
voidProtectionHelper.setMachine(machine)
.setItemOutputs(truncatedItemOutputs)
.setFluidOutputs(truncatedFluidOutputs)
.setMaxParallel(maxParallel)
.setItemOutputInventory(itemOutputInventory)
.setFluidOutputInventory(fluidOutputInventory)
.setMuTEMode(muteMode)
.build();
maxParallel = Math.min(voidProtectionHelper.getMaxParallel(), maxParallel);
if (voidProtectionHelper.isItemFull()) {
Expand Down
124 changes: 119 additions & 5 deletions src/main/java/gregtech/api/util/VoidProtectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import net.minecraftforge.fluids.FluidStack;

import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap;
import com.gtnewhorizons.modularui.api.fluids.IFluidTankLong;

import gregtech.api.interfaces.fluid.IFluidStore;
import gregtech.api.interfaces.tileentity.IVoidable;
import gregtech.api.logic.FluidInventoryLogic;
import gregtech.api.logic.ItemInventoryLogic;

/**
* Helper class to calculate how many parallels of items / fluids can fit in the output buses / hatches.
Expand Down Expand Up @@ -51,10 +54,22 @@ public class VoidProtectionHelper {
* The fluid outputs to check
*/
private FluidStack[] fluidOutputs;
/**
* The item output inventory
*/
private ItemInventoryLogic itemOutputInventory;
/**
* The fluid output inventory
*/
private FluidInventoryLogic fluidOutputInventory;
/**
* Has this helper been built?
*/
private boolean built;
/**
* Is this helper working for a MuTE?
*/
private boolean muteMode;

public VoidProtectionHelper() {}

Expand Down Expand Up @@ -93,6 +108,21 @@ public VoidProtectionHelper setMaxParallel(int maxParallel) {
return this;
}

public VoidProtectionHelper setItemOutputInventory(ItemInventoryLogic itemOutputInventory) {
this.itemOutputInventory = itemOutputInventory;
return this;
}

public VoidProtectionHelper setFluidOutputInventory(FluidInventoryLogic fluidOutputInventory) {
this.fluidOutputInventory = fluidOutputInventory;
return this;
}

public VoidProtectionHelper setMuTEMode(boolean muteMode) {
this.muteMode = muteMode;
return this;
}

/**
* Finishes the VoidProtectionHelper. Anything changed after this will not affect anything
*/
Expand Down Expand Up @@ -241,11 +271,95 @@ private int calculateMaxFluidParallels() {
return aParallelQueue.element().batch;
}

private int calculateMaxFluidParallelsMuTE() {
if (fluidOutputs.length > fluidOutputInventory.getInventory()
.getTanks()) {
return 0;
}

// A map to hold the items we will be 'inputting' into the output hatches. These fluidstacks are actually
// the recipe outputs.
Map<FluidStack, Integer> tFluidOutputMap = new HashMap<>();

// Map that keeps track of the number of parallel crafts we can accommodate for each fluid output.
// In the pair, we keep track of number of full crafts plus mb of fluid in a partial craft, to avoid
// issues with floating point math not being completely accurate when summing.
Map<FluidStack, ParallelData> tParallels = new HashMap<>();

// Iterate over the outputs, calculating require stack spacing they will require.
for (FluidStack aY : fluidOutputs) {
if (aY == null || aY.amount <= 0) {
continue;
}
tFluidOutputMap.merge(aY, aY.amount, Integer::sum);
tParallels.put(aY, new ParallelData(0, 0));
}

if (tFluidOutputMap.isEmpty()) {
// nothing to output, bail early
return maxParallel;
}

for (int i = 0; i < fluidOutputInventory.getInventory()
.getTanks(); i++) {
IFluidTankLong tank = fluidOutputInventory.getInventory()
.getFluidTank(i);
long tSpaceLeft = tank.getCapacityLong() - tank.getFluidAmountLong();
// check if hatch filled
if (tSpaceLeft <= 0) continue;
// check if hatch is empty and unrestricted
if (tank.getStoredFluid() == null) continue;

for (Map.Entry<FluidStack, ParallelData> entry : tParallels.entrySet()) {
FluidStack tFluidOutput = entry.getKey();
if (tank.fill(tFluidOutput.getFluid(), tFluidOutput.amount, false) == tFluidOutput.amount) continue;
// this fluid is not prevented by restrictions on output hatch
ParallelData tParallel = entry.getValue();
Integer tCraftSize = tFluidOutputMap.get(tFluidOutput);
tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
}
}
// now that all partial/restricted hatches have been counted, create a priority queue for our outputs
// the lowest priority fluid is the number of complete parallel crafts we can support
PriorityQueue<ParallelStackInfo<FluidStack>> aParallelQueue = new PriorityQueue<>(
Comparator.comparing(i -> i.batch));
for (Map.Entry<FluidStack, ParallelData> entry : tParallels.entrySet()) {
aParallelQueue
.add(new ParallelStackInfo<>(entry.getValue().batch, entry.getValue().partial, entry.getKey()));
}
// add extra parallels for open slots as well
for (int i = 0; i < fluidOutputInventory.getInventory()
.getTanks(); i++) {
IFluidTankLong tank = fluidOutputInventory.getInventory()
.getFluidTank(i);
// partially filled or restricted hatch. done in the last pass
if (tank.getStoredFluid() != null) continue;

ParallelStackInfo<FluidStack> tParallel = aParallelQueue.poll();
assert tParallel != null; // will always be true, specifying assert here to avoid IDE/compiler warnings
Integer tCraftSize = tFluidOutputMap.get(tParallel.stack);
long tSpaceLeft = tank.getCapacityLong();
tParallel.batch += (tParallel.partial + tSpaceLeft) / tCraftSize;
tParallel.partial = (tParallel.partial + tSpaceLeft) % tCraftSize;
aParallelQueue.add(tParallel);
}

return aParallelQueue.element().batch;
}

/**
* Calculates the max parallels one can do with items if void protection is on
*/
private int calculateMaxItemParallels() {
List<ItemStack> busStacks = machine.getItemOutputSlots(itemOutputs);
List<ItemStack> busStacks;

if (muteMode) {
busStacks = itemOutputInventory.getInventory()
.getStacks();
} else {
busStacks = machine.getItemOutputSlots(itemOutputs);
}
// A map to hold the items we will be 'inputting' into the output buses. These itemstacks are actually the
// recipe outputs.
Map<ItemStack, Integer> tItemOutputMap = new ItemStackMap<>();
Expand Down Expand Up @@ -320,9 +434,9 @@ private int calculateMaxItemParallels() {
private static class ParallelData {

private int batch;
private int partial;
private long partial;

private ParallelData(int batch, int partial) {
private ParallelData(int batch, long partial) {
this.batch = batch;
this.partial = partial;
}
Expand All @@ -331,10 +445,10 @@ private ParallelData(int batch, int partial) {
private static class ParallelStackInfo<T> {

private int batch;
private int partial;
private long partial;
private final T stack;

private ParallelStackInfo(int batch, int partial, T stack) {
private ParallelStackInfo(int batch, long partial, T stack) {
this.batch = batch;
this.partial = partial;
this.stack = stack;
Expand Down

0 comments on commit b6339fa

Please sign in to comment.