Search in sources :

Example 1 with PacketPowerUpdate

use of buildcraft.transport.network.PacketPowerUpdate in project BuildCraft by BuildCraft.

the class PipeTransportPower method updateEntity.

@Override
public void updateEntity() {
    if (container.getWorld().isRemote) {
        for (int i = 0; i < 6; i++) {
            displayPowerAverage[i].tick(displayPower[i]);
        }
        return;
    }
    if (PipeTransportPower.canExplode) {
        if (overload >= 3) {
            destroyPipe();
            return;
        }
    }
    step();
    init();
    for (EnumFacing side : EnumFacing.VALUES) {
        if (tiles[side.ordinal()] != null && tiles[side.ordinal()].isInvalid()) {
            updateTile(side);
        }
    }
    // FIXME: LEFT OVER FROM MERGE! LOOK AT THIS!
    Arrays.fill(displayFlow, (short) 0);
    // Send the power to nearby pipes who requested it
    for (int i = 0; i < 6; ++i) {
        if (internalPower[i] > 0) {
            int totalPowerQuery = 0;
            for (int j = 0; j < 6; ++j) {
                if (j != i && powerQuery[j] > 0) {
                    Object ep = providers[j];
                    if (ep instanceof IPipeTile || ep instanceof IEnergyReceiver || ep instanceof IEnergyHandler) {
                        totalPowerQuery += powerQuery[j];
                    }
                }
            }
            if (totalPowerQuery > 0) {
                int unusedPowerQuery = totalPowerQuery;
                for (int j = 0; j < 6; ++j) {
                    if (j != i && powerQuery[j] > 0) {
                        Object ep = providers[j];
                        double watts = Math.min(internalPower[i] * powerQuery[j] / unusedPowerQuery, internalPower[i]);
                        unusedPowerQuery -= powerQuery[j];
                        if (ep instanceof IPipeTile && ((IPipeTile) ep).getPipeType() == IPipeTile.PipeType.POWER) {
                            Pipe<?> nearbyPipe = (Pipe<?>) ((IPipeTile) ep).getPipe();
                            PipeTransportPower nearbyTransport = (PipeTransportPower) nearbyPipe.transport;
                            watts = nearbyTransport.receiveEnergy(EnumFacing.VALUES[j].getOpposite(), watts);
                            internalPower[i] -= watts;
                            dbgEnergyOutput[j] += watts;
                            powerAverage[j].push((int) Math.ceil(watts));
                            powerAverage[i].push((int) Math.ceil(watts));
                            displayFlow[i] = 1;
                            displayFlow[j] = -1;
                        } else {
                            int iWatts = (int) watts;
                            if (ep instanceof IEnergyReceiver) {
                                IEnergyReceiver handler = (IEnergyReceiver) ep;
                                if (handler.canConnectEnergy(EnumFacing.values()[j].getOpposite())) {
                                    iWatts = handler.receiveEnergy(EnumFacing.values()[j].getOpposite(), iWatts, false);
                                }
                            }
                            internalPower[i] -= iWatts;
                            dbgEnergyOutput[j] += iWatts;
                            powerAverage[j].push(iWatts);
                            powerAverage[i].push(iWatts);
                            displayFlow[i] = 1;
                            displayFlow[j] = -1;
                        }
                    }
                }
            }
        }
    }
    short highestPower = 0;
    for (int i = 0; i < 6; i++) {
        powerAverage[i].tick();
        displayPower[i] = (short) Math.round(powerAverage[i].getAverage());
        if (displayPower[i] > highestPower) {
            highestPower = displayPower[i];
        }
    }
    if (PipeTransportPower.canExplode) {
        if (energyInputTick > powerLimit || overload > 0) {
            overload++;
        } else {
            overload = 0;
        }
    } else {
        overload += highestPower > (maxPower * 0.95F) ? 1 : -1;
        if (overload < 0) {
            overload = 0;
        }
        if (overload > OVERLOAD_TICKS) {
            overload = OVERLOAD_TICKS;
        }
    }
    energyInputTick = 0;
    // Compute the tiles requesting energy that are not power pipes
    for (EnumFacing dir : EnumFacing.VALUES) {
        if (!outputOpen(dir)) {
            continue;
        }
        Object tile = providers[dir.ordinal()];
        if (tile instanceof IPipeTile && ((IPipeTile) tile).getPipe() != null && ((Pipe<?>) ((IPipeTile) tile).getPipe()).transport instanceof PipeTransportPower) {
            continue;
        }
        if (tile instanceof IEnergyReceiver) {
            IEnergyReceiver handler = (IEnergyReceiver) tile;
            if (handler.canConnectEnergy(dir.getOpposite())) {
                int request = handler.receiveEnergy(dir.getOpposite(), this.maxPower, true);
                if (request > 0) {
                    requestEnergy(dir, request);
                }
            }
        } else if (tile instanceof IEnergyReceiver) {
            IEnergyReceiver handler = (IEnergyReceiver) tile;
            if (handler.canConnectEnergy(dir.getOpposite())) {
                int request = handler.receiveEnergy(dir.getOpposite(), this.maxPower, true);
                if (request > 0) {
                    requestEnergy(dir, request);
                }
            }
        }
    }
    // Sum the amount of energy requested on each side
    int[] transferQuery = new int[6];
    for (int i = 0; i < 6; ++i) {
        transferQuery[i] = 0;
        if (!inputOpen(EnumFacing.getFront(i))) {
            continue;
        }
        for (int j = 0; j < 6; ++j) {
            if (j != i) {
                transferQuery[i] += powerQuery[j];
            }
        }
        transferQuery[i] = Math.min(transferQuery[i], maxPower);
    }
    // Transfer the requested energy to nearby pipes
    for (int i = 0; i < 6; ++i) {
        if (transferQuery[i] != 0 && tiles[i] != null) {
            TileEntity entity = tiles[i];
            if (entity instanceof IPipeTile && ((IPipeTile) entity).getPipeType() == IPipeTile.PipeType.POWER) {
                IPipeTile nearbyTile = (IPipeTile) entity;
                if (nearbyTile.getPipe() == null || nearbyTile.getPipeType() != IPipeTile.PipeType.POWER) {
                    continue;
                }
                PipeTransportPower nearbyTransport = (PipeTransportPower) ((Pipe<?>) nearbyTile.getPipe()).transport;
                nearbyTransport.requestEnergy(EnumFacing.VALUES[i].getOpposite(), transferQuery[i]);
            }
        }
    }
    if (tracker.markTimeIfDelay(container.getWorld())) {
        PacketPowerUpdate packet = new PacketPowerUpdate(container);
        packet.displayPower = new short[6];
        for (int i = 0; i < 6; i++) {
            double val = displayPower[i];
            val /= MAX_POWER;
            val = Math.sqrt(val);
            val *= POWER_STAGES;
            packet.displayPower[i] = (short) val;
        }
        packet.displayFlow = displayFlow;
        packet.overload = isOverloaded();
        BuildCraftTransport.instance.sendToPlayersNear(packet, container);
    }
}
Also used : IPipeTile(buildcraft.api.transport.IPipeTile) EnumFacing(net.minecraft.util.EnumFacing) TileEntity(net.minecraft.tileentity.TileEntity) IEnergyHandler(cofh.api.energy.IEnergyHandler) PacketPowerUpdate(buildcraft.transport.network.PacketPowerUpdate) IEnergyReceiver(cofh.api.energy.IEnergyReceiver)

Aggregations

IPipeTile (buildcraft.api.transport.IPipeTile)1 PacketPowerUpdate (buildcraft.transport.network.PacketPowerUpdate)1 IEnergyHandler (cofh.api.energy.IEnergyHandler)1 IEnergyReceiver (cofh.api.energy.IEnergyReceiver)1 TileEntity (net.minecraft.tileentity.TileEntity)1 EnumFacing (net.minecraft.util.EnumFacing)1