From b397abae97db48a9d042b5d77e7865d03129bb0a Mon Sep 17 00:00:00 2001 From: Dimach Date: Thu, 12 Apr 2018 00:33:13 +0300 Subject: [PATCH] Fixed #1406 (#1413) * Fixed #1406 * Rewritten pipes logic, now it less complex and more optimized. Fixed bug when pipes sometimes cant insert steam into steam machines. --- .../GT_MetaPipeEntity_Fluid.java | 153 +++++------------- 1 file changed, 43 insertions(+), 110 deletions(-) diff --git a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaPipeEntity_Fluid.java b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaPipeEntity_Fluid.java index 0b7b40d2..ec6b1645 100644 --- a/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaPipeEntity_Fluid.java +++ b/src/main/java/gregtech/api/metatileentity/implementations/GT_MetaPipeEntity_Fluid.java @@ -8,7 +8,6 @@ import gregtech.api.interfaces.metatileentity.IMetaTileEntity; import gregtech.api.interfaces.tileentity.ICoverable; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.metatileentity.BaseMetaPipeEntity; -import gregtech.api.metatileentity.BaseTileEntity; import gregtech.api.metatileentity.MetaPipeEntity; import gregtech.api.objects.GT_RenderedTexture; import gregtech.api.objects.XSTR; @@ -27,11 +26,10 @@ import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTankInfo; import net.minecraftforge.fluids.IFluidHandler; +import org.apache.commons.lang3.tuple.MutableTriple; import java.util.ArrayList; import java.util.List; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentHashMap; import static gregtech.api.enums.GT_Values.D1; @@ -229,8 +227,9 @@ public class GT_MetaPipeEntity_Fluid extends MetaPipeEntity { mLastReceivedFrom = 0; } - for (FluidStack tFluid : mFluids) { - if (tFluid != null && tFluid.amount > 0) { + for (int i = 0; i < mFluids.length; i++) { + FluidStack tFluid = mFluids[i]; + if (tFluid != null && tFluid.amount > 0) { int tTemperature = tFluid.getFluid().getTemperature(tFluid); if (tTemperature > mHeatResistance) { if (aBaseMetaTileEntity.getRandomNumber(100) == 0) { @@ -259,31 +258,29 @@ public class GT_MetaPipeEntity_Fluid extends MetaPipeEntity { if (D1) e.printStackTrace(GT_Log.err); } } - if (tFluid.amount <= 0) tFluid = null; + if (tFluid.amount <= 0) mFluids[i] = null; } } } if (mLastReceivedFrom == oLastReceivedFrom) { - ArrayList tTanksList = new ArrayList<>(); - ArrayList tDirectionsList = new ArrayList<>(); + List> tanksList = new ArrayList<>(); - for (byte tSide = 0, uSide = 0, i = 0, j = (byte) aBaseMetaTileEntity.getRandomNumber(6); i < 6; i++) { - tSide = (byte) ((i + j) % 6); - uSide = GT_Utility.getOppositeSide(tSide); - IFluidHandler tTank = aBaseMetaTileEntity.getITankContainerAtSide(tSide); - ICoverable tBaseMetaTileEntity = tTank instanceof ICoverable ? (ICoverable) tTank : null; - if (mCheckConnections || isConnectedAtSide(tSide) - || aBaseMetaTileEntity.getCoverBehaviorAtSide(tSide).alwaysLookConnected(tSide, aBaseMetaTileEntity.getCoverIDAtSide(tSide), aBaseMetaTileEntity.getCoverDataAtSide(tSide), aBaseMetaTileEntity) - || (tBaseMetaTileEntity != null && tBaseMetaTileEntity.getCoverBehaviorAtSide(uSide).alwaysLookConnected(uSide, tBaseMetaTileEntity.getCoverIDAtSide(uSide), tBaseMetaTileEntity.getCoverDataAtSide(uSide), tBaseMetaTileEntity))) { - switch (connect(tSide)) { - case 0: - disconnect(tSide); break; - case 2: - if ((mLastReceivedFrom & (1 << tSide)) == 0) { - tTanksList.add(tTank); - tDirectionsList.add(ForgeDirection.getOrientation(tSide).getOpposite()); - } + for (byte tSide = 0; tSide < 6; tSide++) { + byte uSide = GT_Utility.getOppositeSide(tSide); + IFluidHandler tTank = aBaseMetaTileEntity.getITankContainerAtSide(tSide); + ICoverable tBaseMetaTileEntity = tTank instanceof ICoverable ? (ICoverable) tTank : null; + if (mCheckConnections || isConnectedAtSide(tSide) + || aBaseMetaTileEntity.getCoverBehaviorAtSide(tSide).alwaysLookConnected(tSide, aBaseMetaTileEntity.getCoverIDAtSide(tSide), aBaseMetaTileEntity.getCoverDataAtSide(tSide), aBaseMetaTileEntity) + || (tBaseMetaTileEntity != null && tBaseMetaTileEntity.getCoverBehaviorAtSide(uSide).alwaysLookConnected(uSide, tBaseMetaTileEntity.getCoverIDAtSide(uSide), tBaseMetaTileEntity.getCoverDataAtSide(uSide), tBaseMetaTileEntity))) { + switch (connect(tSide)) { + case 0: + disconnect(tSide); + break; + case 2: + if ((mLastReceivedFrom & (1 << tSide)) == 0) { + tanksList.add(new MutableTriple<>(tTank, ForgeDirection.getOrientation(uSide), 0)); + } } } } @@ -291,94 +288,29 @@ public class GT_MetaPipeEntity_Fluid extends MetaPipeEntity { for (int i = 0, j = aBaseMetaTileEntity.getRandomNumber(mPipeAmount); i < mPipeAmount; i++) { int index = (i + j) % mPipeAmount; - if (mFluids[index] != null && mFluids[index].amount > 0) { - int tAmount = Math.max(1, Math.min(mCapacity * 10, mFluids[index].amount)); + if (mFluids[index] == null) { + continue; + } + double amount = Math.max(1, Math.min(mCapacity * 10, mFluids[index].amount)); + FluidStack s = mFluids[index].copy(); + s.amount = Integer.MAX_VALUE; + double totalFreeAmount = 0; - List tTankAmounts = new ArrayList(); - int totalFreeAmount = 0; - int totalOverAmount = 0; - - // Calculating of amounts by fullness of consumers - for (int tankIndex = 0; tankIndex < tTanksList.size(); tankIndex++) { - IFluidHandler tTileEntity = tTanksList.get(tankIndex); - ForgeDirection dir = tDirectionsList.get(tankIndex); - FluidTankInfo[] tTankInfoList = tTileEntity.getTankInfo(dir.getOpposite()); - tTankAmounts.add(0); - - FluidTankInfo successfulTankInfo = null; - for (FluidTankInfo tankInfo : tTankInfoList) { - // Empty tank - if (successfulTankInfo == null && tankInfo.fluid == null) - successfulTankInfo = tankInfo; - // Tank with equal fluid - if (tankInfo.fluid == null || !tankInfo.fluid.isFluidEqual(mFluids[index])) continue; - successfulTankInfo = tankInfo; - break; - } - if (successfulTankInfo != null) { - tTankAmounts.set(tankIndex, (successfulTankInfo.fluid != null) - ? successfulTankInfo.capacity - successfulTankInfo.fluid.amount - : successfulTankInfo.capacity); - } else { - tTankAmounts.set(tankIndex, mCapacity * 20); - } - - totalFreeAmount += tTankAmounts.get(tankIndex); + // Calculating of amounts by fullness of consumers + for (MutableTriple tank : tanksList) { + tank.right = tank.left.fill(tank.middle, s, false); + totalFreeAmount += tank.right; + } + // Filling and amount correction + for (MutableTriple tank : tanksList) { + if (totalFreeAmount > amount) { + tank.right = (int) Math.floor(tank.right * amount / totalFreeAmount); } + if (tank.right <= 0) continue; - // Accounting of proportions - if (totalFreeAmount > tAmount) { - for (int tankIndex = 0; tankIndex < tTanksList.size(); tankIndex++) { - double tankAmount = (double)tTankAmounts.get(tankIndex); - tTankAmounts.set(tankIndex, (int)Math.floor(tankAmount * tAmount / totalFreeAmount)); - totalOverAmount += tTankAmounts.get(tankIndex); - } - totalOverAmount = tAmount - totalOverAmount; - } else { - tAmount = totalFreeAmount; - } - - int tOverAmount = totalOverAmount; - int tRemainingAmount = tAmount; - int tFilledTanksCnt = 0; - - // Filling and amount correction - for (int tFillIndex = 0; tFillIndex < tTanksList.size(); tFillIndex++) { - if (tFilledTanksCnt >= tTanksList.size() || tRemainingAmount <= 0) break; - - for (int tankIndex = 0; tankIndex < tTanksList.size(); tankIndex++) { - int tankAmount = tTankAmounts.get(tankIndex); - if (tankAmount < 0) continue; - - IFluidHandler tTileEntity = tTanksList.get(tankIndex); - ForgeDirection tDir = tDirectionsList.get(tankIndex); - - int tFilledAmount = tTileEntity.fill(tDir, drainFromIndex(tankAmount, false, index), false); - if (tFilledAmount > 0) { - tTileEntity.fill(tDir, drainFromIndex(tFilledAmount, true, index), true); - } - tRemainingAmount -= tFilledAmount; - - if (tFilledAmount < tankAmount) { - tFilledTanksCnt++; - tOverAmount += tankAmount - tFilledAmount; - tTankAmounts.set(tankIndex, -1); - } - } - - // Unused amount will be transferred to the rest - if (tFilledTanksCnt < tTanksList.size()) { - totalOverAmount = tOverAmount; - tOverAmount = tOverAmount / (tTanksList.size() - tFilledTanksCnt); - - for (int tankIndex = 0; tankIndex < tTanksList.size(); tankIndex++) { - int tankAmount = tTankAmounts.get(tankIndex); - if (tankAmount < 0) continue; - - tTankAmounts.set(tankIndex, tankAmount + tOverAmount); - } - tOverAmount = totalOverAmount - tOverAmount * (tTanksList.size() - tFilledTanksCnt); - } + int tFilledAmount = tank.left.fill(tank.middle, drainFromIndex(tank.right, false, index), false); + if (tFilledAmount > 0) { + tank.left.fill(tank.middle, drainFromIndex(tFilledAmount, true, index), true); } } } @@ -387,7 +319,8 @@ public class GT_MetaPipeEntity_Fluid extends MetaPipeEntity { } oLastReceivedFrom = mLastReceivedFrom; - }else if(aBaseMetaTileEntity.isClientSide() && GT_Client.changeDetected==4) aBaseMetaTileEntity.issueTextureUpdate(); + } else if (aBaseMetaTileEntity.isClientSide() && GT_Client.changeDetected == 4) + aBaseMetaTileEntity.issueTextureUpdate(); } @Override