Attempt to fix liquid pipe flows. Should close #336

This commit is contained in:
Christian 2012-10-13 12:14:07 -04:00
parent 1e0e23fe49
commit ad8b896ef7

View file

@ -9,6 +9,8 @@
package buildcraft.transport; package buildcraft.transport;
import java.util.Arrays;
import buildcraft.BuildCraftCore; import buildcraft.BuildCraftCore;
import buildcraft.api.core.Orientations; import buildcraft.api.core.Orientations;
import buildcraft.api.core.SafeTimeTracker; import buildcraft.api.core.SafeTimeTracker;
@ -258,30 +260,15 @@ public class PipeTransportLiquids extends PipeTransport implements ITankContaine
private void moveLiquids() { private void moveLiquids() {
short newTimeSlot = (short) (worldObj.getWorldTime() % travelDelay); short newTimeSlot = (short) (worldObj.getWorldTime() % travelDelay);
//Processes all internal tanks short outputCount = computeCurrentConnectionStatesAndTickFlows(newTimeSlot);
for (Orientations direction : Orientations.values()){
internalTanks[direction.ordinal()].setTime(newTimeSlot);
internalTanks[direction.ordinal()].moveLiquids();
}
for (Orientations direction : Orientations.dirs()){
if (transferState[direction.ordinal()] == TransferState.Input){
if ((inputTTL[direction.ordinal()]--) < 1){
transferState[direction.ordinal()] = TransferState.None;
}
}
}
short outputCount = computeOutputs();
moveFromPipe(outputCount); moveFromPipe(outputCount);
moveFromCenter(); moveFromCenter(outputCount);
moveToCenter(); moveToCenter();
} }
private void moveFromPipe(short outputCount) { private void moveFromPipe(short outputCount) {
//Move liquid from the non-center to the connected output blocks //Move liquid from the non-center to the connected output blocks
if (outputCount > 0){ if (outputCount > 0) {
for (Orientations o : Orientations.dirs()){ for (Orientations o : Orientations.dirs()){
if (transferState[o.ordinal()] == TransferState.Output){ if (transferState[o.ordinal()] == TransferState.Output){
TileEntity target = this.container.getTile(o); TileEntity target = this.container.getTile(o);
@ -300,30 +287,19 @@ public class PipeTransportLiquids extends PipeTransport implements ITankContaine
} }
} }
private void moveFromCenter() { private void moveFromCenter(short outputCount) {
//Split liquids moving to output equally based on flowrate, how much each side can accept and available liquid //Split liquids moving to output equally based on flowrate, how much each side can accept and available liquid
int[] maxOutput = new int[] {0,0,0,0,0,0};
int transferOutCount = 0;
LiquidStack pushStack = internalTanks[Orientations.Unknown.ordinal()].getLiquid(); LiquidStack pushStack = internalTanks[Orientations.Unknown.ordinal()].getLiquid();
int totalAvailable = internalTanks[Orientations.Unknown.ordinal()].getAvailable(); int totalAvailable = internalTanks[Orientations.Unknown.ordinal()].getAvailable();
if (totalAvailable < 1) return; if (totalAvailable < 1) return;
if (pushStack != null){ if (pushStack != null) {
LiquidStack testStack = pushStack.copy(); LiquidStack testStack = pushStack.copy();
testStack.amount = flowRate; testStack.amount = flowRate;
for (Orientations direction : Orientations.dirs()){
if (transferState[direction.ordinal()] != TransferState.Output ) continue;
maxOutput[direction.ordinal()] = internalTanks[direction.ordinal()].fill(testStack, false);
if(maxOutput[direction.ordinal()] > 0){
transferOutCount++;
}
}
if (transferOutCount <= 0) return;
//Move liquid from the center to the output sides //Move liquid from the center to the output sides
for (Orientations direction : Orientations.dirs()) { for (Orientations direction : Orientations.dirs()) {
if (transferState[direction.ordinal()] == TransferState.Output) { if (transferState[direction.ordinal()] == TransferState.Output) {
if (maxOutput[direction.ordinal()] == 0) continue; int available = internalTanks[direction.ordinal()].fill(testStack, false);
int ammountToPush = (int) ((double) maxOutput[direction.ordinal()] / (double) flowRate / (double) transferOutCount * (double) Math.min(flowRate, totalAvailable)); int ammountToPush = (int) (available / (double) flowRate / (double) outputCount * (double) Math.min(flowRate, totalAvailable));
if (ammountToPush < 1) ammountToPush++; if (ammountToPush < 1) ammountToPush++;
LiquidStack liquidToPush = internalTanks[Orientations.Unknown.ordinal()].drain(ammountToPush, false); LiquidStack liquidToPush = internalTanks[Orientations.Unknown.ordinal()].drain(ammountToPush, false);
@ -372,33 +348,45 @@ public class PipeTransportLiquids extends PipeTransport implements ITankContaine
} }
} }
private short computeOutputs() { private short computeCurrentConnectionStatesAndTickFlows(short newTimeSlot) {
short outputCount = 0; short outputCount = 0;
for (Orientations o : Orientations.dirs()) { //Processes all internal tanks
if (transferState[o.ordinal()] == TransferState.Input) continue; for (Orientations direction : Orientations.values()) {
if (!container.pipe.outputOpen(o)){ internalTanks[direction.ordinal()].setTime(newTimeSlot);
transferState[o.ordinal()] = TransferState.None; internalTanks[direction.ordinal()].moveLiquids();
// Input processing
if (direction == Orientations.Unknown)
{
continue; continue;
} }
if (outputCooldown[o.ordinal()] > 0){ if (transferState[direction.ordinal()] == TransferState.Input) {
outputCooldown[o.ordinal()]--; inputTTL[direction.ordinal()]--;
if (inputTTL[direction.ordinal()] <= 0) {
transferState[direction.ordinal()] = TransferState.None;
}
continue; continue;
} }
if (outputTTL[o.ordinal()] <= 0){ if (!container.pipe.outputOpen(direction)) {
transferState[o.ordinal()] = TransferState.None; transferState[direction.ordinal()] = TransferState.None;
outputCooldown[o.ordinal()] = OUTPUT_COOLDOWN;
outputTTL[o.ordinal()] = OUTPUT_TTL;
continue; continue;
} }
if (outputCooldown[direction.ordinal()] > 0) {
if (canReceiveLiquid(o)) { outputCooldown[direction.ordinal()]--;
transferState[o.ordinal()] = TransferState.Output; continue;
}
if (outputTTL[direction.ordinal()] <= 0) {
transferState[direction.ordinal()] = TransferState.None;
outputCooldown[direction.ordinal()] = OUTPUT_COOLDOWN;
outputTTL[direction.ordinal()] = OUTPUT_TTL;
continue;
}
if (canReceiveLiquid(direction)) {
transferState[direction.ordinal()] = TransferState.Output;
outputCount++; outputCount++;
} }
} }
System.out.printf("%d %d %d : %s\n", xCoord, yCoord, zCoord, Arrays.asList(transferState));
return outputCount; return outputCount;
} }