Search in sources :

Example 21 with Adjacent

use of network.rs485.logisticspipes.connection.Adjacent in project LogisticsPipes by RS485.

the class PowerSupplierHandler method requestICPower.

private void requestICPower() {
    // Use Buffer
    final List<LPNeighborTileEntity<TileEntity>> adjacentTileEntities = new WorldCoordinatesWrapper(pipe.container).allNeighborTileEntities();
    double globalNeed = 0;
    double[] need = new double[adjacentTileEntities.size()];
    int i = 0;
    for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
        if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity())) {
            if (pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection())) {
                if (SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
                    // TODO pipe.container must be IEnergySource
                    globalNeed += need[i] = SimpleServiceLocator.IC2Proxy.demandedEnergyUnits(adjacent.getTileEntity());
                }
            }
        }
        ++i;
    }
    if (globalNeed != 0 && !Double.isNaN(globalNeed)) {
        double fullfillable = Math.min(1, internalBufferIC2 / globalNeed);
        i = 0;
        for (NeighborTileEntity<TileEntity> adjacent : adjacentTileEntities) {
            if (SimpleServiceLocator.IC2Proxy.isEnergySink(adjacent.getTileEntity()) && pipe.canPipeConnect(adjacent.getTileEntity(), adjacent.getDirection()) && SimpleServiceLocator.IC2Proxy.acceptsEnergyFrom(adjacent.getTileEntity(), pipe.container, adjacent.getOurDirection())) {
                // TODO pipe.container must be IEnergySource
                if (internalBufferIC2 + 1 < need[i] * fullfillable) {
                    return;
                }
                double toUse = Math.min(pipe.getUpgradeManager().getIC2PowerLevel(), need[i] * fullfillable);
                double unUsed = SimpleServiceLocator.IC2Proxy.injectEnergyUnits(adjacent.getTileEntity(), adjacent.getOurDirection(), toUse);
                double used = toUse - unUsed;
                if (used > 0) {
                    // MainProxy.sendPacketToAllWatchingChunk(this.pipe.getX(), this.pipe.getZ(), MainProxy.getDimensionForWorld(this.pipe.getWorld()), PacketHandler.getPacket(PowerPacketLaser.class).setColor(LogisticsPowerProviderTileEntity.IC2_COLOR).setPos(this.pipe.getLPPosition()).setRenderBall(true).setDir(adTile.orientation).setLength(0.5F));
                    pipe.container.addLaser(adjacent.getDirection(), 0.5F, LogisticsPowerProviderTileEntity.IC2_COLOR, false, true);
                    internalBufferIC2 -= used;
                }
                if (internalBufferIC2 < 0) {
                    internalBufferIC2 = 0;
                    return;
                }
            }
            ++i;
        }
    }
    // Rerequest Buffer
    List<Pair<ISubSystemPowerProvider, List<IFilter>>> provider = pipe.getRouter().getSubSystemPowerProvider();
    double available = 0;
    outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
        for (IFilter filter : pair.getValue2()) {
            if (filter.blockPower()) {
                continue outer;
            }
        }
        if (pair.getValue1().usePaused()) {
            continue;
        }
        if (!pair.getValue1().getBrand().equals("EU")) {
            continue;
        }
        available += pair.getValue1().getPowerLevel();
    }
    if (available > 0) {
        double neededPower = PowerSupplierHandler.INTERNAL_IC2_BUFFER_MAX - internalBufferIC2;
        if (neededPower > 0) {
            if (pipe.useEnergy((int) (neededPower / 10000), false)) {
                outer: for (Pair<ISubSystemPowerProvider, List<IFilter>> pair : provider) {
                    for (IFilter filter : pair.getValue2()) {
                        if (filter.blockPower()) {
                            continue outer;
                        }
                    }
                    if (pair.getValue1().usePaused()) {
                        continue;
                    }
                    if (!pair.getValue1().getBrand().equals("EU")) {
                        continue;
                    }
                    double requestamount = neededPower * (pair.getValue1().getPowerLevel() / available);
                    pair.getValue1().requestPower(pipe.getRouterId(), requestamount);
                }
            }
        }
    }
}
Also used : LogisticsPowerProviderTileEntity(logisticspipes.blocks.powertile.LogisticsPowerProviderTileEntity) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) LPNeighborTileEntity(network.rs485.logisticspipes.connection.LPNeighborTileEntity) IFilter(logisticspipes.interfaces.routing.IFilter) LPNeighborTileEntity(network.rs485.logisticspipes.connection.LPNeighborTileEntity) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) Pair(logisticspipes.utils.tuples.Pair)

Example 22 with Adjacent

use of network.rs485.logisticspipes.connection.Adjacent in project LogisticsPipes by RS485.

the class PipeLogisticsChassis method updateAdjacentCache.

/**
 * Updates pointedAdjacent on {@link CoreRoutedPipe}.
 */
@Override
protected void updateAdjacentCache() {
    super.updateAdjacentCache();
    final Adjacent adjacent = getAdjacent();
    if (adjacent instanceof SingleAdjacent) {
        pointedAdjacent = ((SingleAdjacent) adjacent);
    } else {
        final SingleAdjacent oldPointedAdjacent = pointedAdjacent;
        SingleAdjacent newPointedAdjacent = null;
        if (oldPointedAdjacent != null) {
            // update pointed adjacent with connection type or reset it
            newPointedAdjacent = adjacent.optionalGet(oldPointedAdjacent.getDir()).map(connectionType -> new SingleAdjacent(this, oldPointedAdjacent.getDir(), connectionType)).orElse(null);
        }
        if (newPointedAdjacent == null) {
            newPointedAdjacent = adjacent.neighbors().entrySet().stream().findAny().map(connectedNeighbor -> new SingleAdjacent(this, connectedNeighbor.getKey().getDirection(), connectedNeighbor.getValue())).orElse(null);
        }
        pointedAdjacent = newPointedAdjacent;
    }
}
Also used : Adjacent(network.rs485.logisticspipes.connection.Adjacent) SingleAdjacent(network.rs485.logisticspipes.connection.SingleAdjacent) NoAdjacent(network.rs485.logisticspipes.connection.NoAdjacent) SingleAdjacent(network.rs485.logisticspipes.connection.SingleAdjacent)

Aggregations

WorldCoordinatesWrapper (network.rs485.logisticspipes.world.WorldCoordinatesWrapper)16 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)13 IFilter (logisticspipes.interfaces.routing.IFilter)12 TileEntity (net.minecraft.tileentity.TileEntity)12 Pair (logisticspipes.utils.tuples.Pair)11 List (java.util.List)10 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)10 MainProxy (logisticspipes.proxy.MainProxy)10 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)10 ItemStack (net.minecraft.item.ItemStack)10 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)10 Map (java.util.Map)9 PacketHandler (logisticspipes.network.PacketHandler)9 EntityPlayer (net.minecraft.entity.player.EntityPlayer)9 LinkedList (java.util.LinkedList)8 Collectors (java.util.stream.Collectors)8 IInventoryUtil (logisticspipes.interfaces.IInventoryUtil)8 PlayerCollectionList (logisticspipes.utils.PlayerCollectionList)8 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)8 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)8