From fdde96ab6fef30064b67e28390008ee4ba455655 Mon Sep 17 00:00:00 2001 From: Maxim Date: Sat, 22 Apr 2023 17:38:49 +0200 Subject: MuTE overhaul and ACR (#1883) * complex controller start * Added methods to get input fluids and items * Added logic to complex parallel mute * Added ACR and fixed many, many, many, many bugs * Added void protection setting to checkRecipe * do not init nbt, if mteID and mteRegistry are the same * Improved GUI design * Force structure check when pressing power switch * ACR Textures * Added T1 structure * Added perfect OC * Added WAILA * fix mutes resetting their nbt * Fix ACR GUI * fix npe * Added void protection for MuTEs * Fixed ACR starting recipe while another one is ongoing * nbt saving * maybe fix structure breaking * Fix complex machine disabling on startup * correctly update input tanks * move casings over * Changed logic of casings to change mode and facing in one go by sneaking * Fixed the casing target not resetting * Added side only annotations * don't leave it empty * Added power logic and tiered blocks to ACR * Change facing to wrench side if casing mode is currently none * lasers anyone? * Added ACR item chaining * Remove unncessary item lists * Use HashSet for process whitelists * Optimize list capacities * Fix potential recipe voiding bug * Rename methods for consistancy * Fix NPE * Duct tape fix structure check * allow MuTEs to connect to cables * Added separate tank inventories for input separation (#1887) * Fixed unregistering tank function * Fixed input busses not being automatable * Added fluid chaining * Fixed saving of input tanks * Forbid inventory registering with empty name * Display all input tanks in controller GUI * Fixed fluid hatch GUI height * Reset casing lists when checking the structure * Make inventory GUI size consistant * Make use of the tooltip cache * rename thing clean up * Forgot to put tooltip into map * Added tooltip to ACR * Reset whitelists when one whitelist window was opened * Refined scanner string * Fixed progress times * Fixed MuTE not consuming fluids * Properly register controller inventories * switch to ForgeDirection * switch to new Renderer * Added missing contains check on registerInventory * Fixed output tanks not registering * Fixed upgrade tank loading * fix machines not having active/inactive textures * fix overlays not loading correctly * Don't register controller directly * Remove magic strings all * fix active not setting to inactive * allow glow * item renderer * fix glow * MuTE improved hatch GUI and fluid output locking (#1889) * Allow output hatches to be fluid locked * Reworked hatch GUI * Check target before trying to open GUI * Make ACR GUI easier to look at * fix covers not rendering on mutes * fix covers not displaying above the item/fluid in/out * new folder texture structure * Reduce network traffic caused by covers * Fixed WAILA fluid locking display * Don't save everything to the itemstack NBT * Added possibility to save NBT of MuTE to its itemstack * fix textures, but make sacrifices * mah textures * Removed the need for all textures to be present * Added glow texture for active coke oven * Removed unncesssary upgrade casing textures * shorten nbt tags --------- Co-authored-by: BlueWeabo <76872108+BlueWeabo@users.noreply.github.com> Co-authored-by: Martin Robertz Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../java/gregtech/api/util/GT_ParallelHelper.java | 193 +++++++++++++++++++-- src/main/java/gregtech/api/util/GT_Waila.java | 4 + 2 files changed, 179 insertions(+), 18 deletions(-) (limited to 'src/main/java/gregtech/api/util') diff --git a/src/main/java/gregtech/api/util/GT_ParallelHelper.java b/src/main/java/gregtech/api/util/GT_ParallelHelper.java index 4ae33cd48d..4e281e976f 100644 --- a/src/main/java/gregtech/api/util/GT_ParallelHelper.java +++ b/src/main/java/gregtech/api/util/GT_ParallelHelper.java @@ -15,10 +15,11 @@ import org.apache.commons.lang3.tuple.MutableTriple; import com.gtnewhorizon.gtnhlib.util.map.ItemStackMap; +import gregtech.api.fluid.FluidTankGT; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch_Output; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase; -import gregtech.api.multitileentity.multiblock.base.MultiBlockController; +import gregtech.api.multitileentity.multiblock.base.Controller; import gregtech.api.objects.XSTR; import gregtech.common.tileentities.machines.GT_MetaTileEntity_Hatch_OutputBus_ME; import gregtech.common.tileentities.machines.GT_MetaTileEntity_Hatch_Output_ME; @@ -32,7 +33,7 @@ public class GT_ParallelHelper { /** * @mMachineMulti a MultiTileEntity Controller */ - private MultiBlockController mMachineMulti; + private Controller mMachineMulti; /** * @mRecipe Recipe used when trying to calculate parallels */ @@ -87,7 +88,7 @@ public class GT_ParallelHelper { /** * Enables void protection on a multitile multiblock. Experimental! Still needs to be worked on */ - public GT_ParallelHelper enableVoidProtection(MultiBlockController aMachineMulti) { + public GT_ParallelHelper enableVoidProtection(Controller aMachineMulti) { mVoidProtection = true; mMachineMulti = aMachineMulti; return this; @@ -265,25 +266,30 @@ public class GT_ParallelHelper { } // Let's look at how many parallels we can get with void protection if (mVoidProtection) { - for (GT_MetaTileEntity_Hatch tHatch : mMachineMeta.mOutputBusses) { - if (tHatch instanceof GT_MetaTileEntity_Hatch_OutputBus_ME) { - tMEOutputBus = true; - break; + if (mMachineMeta != null) { + for (GT_MetaTileEntity_Hatch tHatch : mMachineMeta.mOutputBusses) { + if (tHatch instanceof GT_MetaTileEntity_Hatch_OutputBus_ME) { + tMEOutputBus = true; + break; + } } - } - for (GT_MetaTileEntity_Hatch tHatch : mMachineMeta.mOutputHatches) { - if (tHatch instanceof GT_MetaTileEntity_Hatch_Output_ME) { - tMEOutputHatch = true; - break; + for (GT_MetaTileEntity_Hatch tHatch : mMachineMeta.mOutputHatches) { + if (tHatch instanceof GT_MetaTileEntity_Hatch_Output_ME) { + tMEOutputHatch = true; + break; + } + } + if (!tMEOutputBus) { + mMaxParallel = Math.min(calculateMaxParallelsForBusses(), mMaxParallel); } - } - if (!tMEOutputBus) { - mMaxParallel = Math.min(calculateMaxParallelsForBusses(), mMaxParallel); - } - if (!tMEOutputHatch) { - mMaxParallel = Math.min(calculateMaxParallelsForHatches(), mMaxParallel); + if (!tMEOutputHatch) { + mMaxParallel = Math.min(calculateMaxParallelsForHatches(), mMaxParallel); + } + } else if (mMachineMulti != null) { + mMaxParallel = Math.min(calculateMaxItemParallelsForMuTEs(), mMaxParallel); + mMaxParallel = Math.min(calculateMaxFluidParallelsForMuTEs(), mMaxParallel); } } @@ -343,6 +349,81 @@ public class GT_ParallelHelper { } } + /** + * Calculates the max parallel for fluids if void protection is turned on + */ + private int calculateMaxFluidParallelsForMuTEs() { + if (mMachineMulti != null && mMachineMulti.getOutputTanks() != null + && mMachineMulti.getOutputTanks().length >= mRecipe.mFluidOutputs.length) { + // A map to hold the items we will be 'inputting' into the output hatches. These fluidstacks are actually + // the + // recipe outputs. + Map 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> tParallels = new HashMap<>(); + + // Iterate over the outputs, calculating require stack spacing they will require. + for (FluidStack aY : mRecipe.mFluidOutputs) { + if (aY == null) { + continue; + } + tFluidOutputMap.merge(aY, aY.amount, Integer::sum); + tParallels.put(aY, new MutablePair<>(0, 0)); + } + + if (tFluidOutputMap.isEmpty()) { + // nothing to output, bail early + return mMaxParallel; + } + + for (FluidTankGT tHatch : mMachineMulti.getOutputTanks()) { + int tSpaceLeft = tHatch.getCapacity() - tHatch.getFluidAmount(); + + // check if hatch filled + if (tSpaceLeft <= 0) continue; + + // check if hatch is empty and unrestricted + if (tHatch.getFluidAmount() == 0) continue; + + for (Entry> entry : tParallels.entrySet()) { + FluidStack tFluidOutput = entry.getKey(); + // this fluid is not prevented by restrictions on output hatch + if (tHatch.getFluidAmount() == 0 || GT_Utility.areFluidsEqual(tHatch.getFluid(), tFluidOutput)) { + MutablePair tParallel = entry.getValue(); + Integer tCraftSize = tFluidOutputMap.get(tFluidOutput); + tParallel.left += (tParallel.right + tSpaceLeft) / tCraftSize; + tParallel.right = (tParallel.right + 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> aParallelQueue = new PriorityQueue<>( + Comparator.comparing(MutableTriple::getLeft)); + for (Entry> entry : tParallels.entrySet()) { + aParallelQueue.add(new MutableTriple<>(entry.getValue().left, entry.getValue().right, entry.getKey())); + } + // add extra parallels for open slots as well + for (FluidTankGT tHatch : mMachineMulti.getOutputTanks()) { + // partially filled or restricted hatch. done in last pass + if (tHatch.getFluidAmount() > 0) continue; + + MutableTriple tParallel = aParallelQueue.poll(); + assert tParallel != null; // will always be true, specifying assert here to avoid IDE/compiler warnings + Integer tCraftSize = tFluidOutputMap.get(tParallel.right); + int tSpaceLeft = tHatch.getCapacity(); + tParallel.left += (tParallel.middle + tSpaceLeft) / tCraftSize; + tParallel.middle = (tParallel.middle + tSpaceLeft) % tCraftSize; + aParallelQueue.add(tParallel); + } + return aParallelQueue.element().left; + } + return 0; + } + /** * Calculates the max parallel for fluids if void protection is turned on */ @@ -434,6 +515,82 @@ public class GT_ParallelHelper { return 0; } + /** + * Calculates the max parallels one can do with items if void protection is on + */ + private int calculateMaxItemParallelsForMuTEs() { + + // A map to hold the items we will be 'inputting' into the output buses. These itemstacks are actually the + // recipe outputs. + Map tItemOutputMap = new ItemStackMap<>(); + + // Map that keeps track of the number of parallel crafts we can accommodate for each item output. + // In the pair, we keep track of number of full crafts plus number of items in a partial craft, to avoid + // issues with floating point math not being completely accurate when summing. + Map> tParallels = new ItemStackMap<>(); + int tSlotsFree = 0; + for (ItemStack tItem : mRecipe.mOutputs) { + tItemOutputMap.merge(tItem, tItem.stackSize, Integer::sum); + tParallels.put(tItem, new MutablePair<>(0, 0)); + } + + if (tItemOutputMap.isEmpty()) { + // nothing to output, bail early + return mMaxParallel; + } + + if (mRecipe.mOutputs.length > 0 && mMachineMulti != null + && mMachineMulti.getOutputInventory() != null + && mMachineMulti.getOutputInventory() + .getSlots() > 0) { + for (int i = 0; i < mMachineMulti.getOutputInventory() + .getSlots(); i++) { + ItemStack tBusStack = mMachineMulti.getOutputInventory() + .getStackInSlot(i); + if (tBusStack == null) { + tSlotsFree++; + } else { + // get the real stack size + // we ignore the bus inventory stack limit here as no one set it to anything other than 64 + int tMaxBusStackSize = tBusStack.getMaxStackSize(); + if (tBusStack.stackSize >= tMaxBusStackSize) + // this bus stack is full. no checking + continue; + int tSpaceLeft = tMaxBusStackSize - tBusStack.stackSize; + Integer tCraftSize = tItemOutputMap.get(tBusStack); + if (tCraftSize == null) { + // we don't have a matching stack to output, ignore this bus stack + continue; + } + MutablePair tParallel = tParallels.get(tBusStack); + tParallel.left += (tParallel.right + tSpaceLeft) / tCraftSize; + tParallel.right = (tParallel.right + tSpaceLeft) % tCraftSize; + } + } + // now that all partial stacks have been counted, create a priority queue for our outputs + // the lowest priority item is the number of complete parallel crafts we can support + PriorityQueue> aParallelQueue = new PriorityQueue<>( + Comparator.comparing(MutableTriple::getLeft)); + for (Entry> entry : tParallels.entrySet()) { + aParallelQueue.add(new MutableTriple<>(entry.getValue().left, entry.getValue().right, entry.getKey())); + } + + while (tSlotsFree > 0) { + MutableTriple tParallel = aParallelQueue.poll(); + assert tParallel != null; // will always be true, specifying assert here to avoid IDE/compiler warnings + Integer tCraftSize = tItemOutputMap.get(tParallel.right); + int tStackSize = tParallel.right.getMaxStackSize(); + tParallel.left += (tParallel.middle + tStackSize) / tCraftSize; + tParallel.middle = (tParallel.middle + tStackSize) % tCraftSize; + aParallelQueue.add(tParallel); + --tSlotsFree; + } + + return aParallelQueue.element().left; + } + return 0; + } + /** * Calculates the max parallels one can do with items if void protection is on */ diff --git a/src/main/java/gregtech/api/util/GT_Waila.java b/src/main/java/gregtech/api/util/GT_Waila.java index 9ea42a1028..1c5b5301c8 100644 --- a/src/main/java/gregtech/api/util/GT_Waila.java +++ b/src/main/java/gregtech/api/util/GT_Waila.java @@ -3,6 +3,10 @@ package gregtech.api.util; public abstract class GT_Waila { public static String getMachineProgressString(boolean isActive, int maxProgress, int progress) { + return getMachineProgressString(isActive, (long) maxProgress, (long) progress); + } + + public static String getMachineProgressString(boolean isActive, long maxProgress, long progress) { final String result; if (isActive) { -- cgit