Search in sources :

Example 1 with OnMoveToCentre

use of buildcraft.api.transport.pipe.PipeEventFluid.OnMoveToCentre in project BuildCraft by BuildCraft.

the class PipeFlowFluids method moveToCenter.

private void moveToCenter() {
    int transferInCount = 0;
    Section center = sections.get(EnumPipePart.CENTER);
    int spaceAvailable = capacity - center.amount;
    int flowRate = fluidTransferInfo.transferPerTick;
    List<EnumPipePart> faces = new ArrayList<>();
    Collections.addAll(faces, EnumPipePart.FACES);
    Collections.shuffle(faces);
    int[] inputPerTick = new int[6];
    for (EnumPipePart part : faces) {
        Section section = sections.get(part);
        inputPerTick[part.getIndex()] = 0;
        if (section.getCurrentDirection().canInput()) {
            inputPerTick[part.getIndex()] = section.drainInternal(flowRate, false);
            if (inputPerTick[part.getIndex()] > 0) {
                transferInCount++;
            }
        }
    }
    int[] totalOffered = Arrays.copyOf(inputPerTick, 6);
    PreMoveToCentre preMove = new PreMoveToCentre(pipe.getHolder(), this, currentFluid, Math.min(flowRate, spaceAvailable), totalOffered, inputPerTick);
    // Event handlers edit the array in-place
    pipe.getHolder().fireEvent(preMove);
    int[] fluidLeavingSide = new int[6];
    // Work out how much fluid should leave
    int left = Math.min(flowRate, spaceAvailable);
    float min = Math.min(flowRate * transferInCount, spaceAvailable) / (float) flowRate / transferInCount;
    for (EnumPipePart part : EnumPipePart.FACES) {
        Section section = sections.get(part);
        // Move liquid from input sides to the centre
        int i = part.getIndex();
        if (inputPerTick[i] > 0) {
            int amountToDrain = (int) (inputPerTick[i] * min);
            if (amountToDrain < 1) {
                amountToDrain++;
            }
            if (amountToDrain > left) {
                amountToDrain = left;
            }
            int amountToPush = section.drainInternal(amountToDrain, false);
            if (amountToPush > 0) {
                fluidLeavingSide[i] = amountToPush;
                left -= amountToPush;
            }
        }
    }
    int[] fluidEnteringCentre = Arrays.copyOf(fluidLeavingSide, 6);
    OnMoveToCentre move = new OnMoveToCentre(pipe.getHolder(), this, currentFluid, fluidLeavingSide, fluidEnteringCentre);
    pipe.getHolder().fireEvent(move);
    for (EnumPipePart part : EnumPipePart.FACES) {
        Section section = sections.get(part);
        int i = part.getIndex();
        int leaving = fluidLeavingSide[i];
        if (leaving > 0) {
            int actuallyDrained = section.drainInternal(leaving, true);
            if (actuallyDrained != leaving) {
                throw new IllegalStateException("Couldn't drain " + leaving + " from " + part + ", only drained " + actuallyDrained);
            }
            if (actuallyDrained > 0) {
                section.ticksInDirection = COOLDOWN_INPUT;
            }
            int entering = fluidEnteringCentre[i];
            if (entering > 0) {
                int actuallyFilled = center.fill(entering, true);
                if (actuallyFilled != entering) {
                    throw new IllegalStateException("Couldn't fill " + entering + " from " + part + ", only filled " + actuallyFilled);
                }
            }
        }
    }
}
Also used : EnumPipePart(buildcraft.api.core.EnumPipePart) ArrayList(java.util.ArrayList) OnMoveToCentre(buildcraft.api.transport.pipe.PipeEventFluid.OnMoveToCentre) PreMoveToCentre(buildcraft.api.transport.pipe.PipeEventFluid.PreMoveToCentre)

Aggregations

EnumPipePart (buildcraft.api.core.EnumPipePart)1 OnMoveToCentre (buildcraft.api.transport.pipe.PipeEventFluid.OnMoveToCentre)1 PreMoveToCentre (buildcraft.api.transport.pipe.PipeEventFluid.PreMoveToCentre)1 ArrayList (java.util.ArrayList)1