Search in sources :

Example 16 with WorldCoordinatesWrapper

use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper in project LogisticsPipes by RS485.

the class LogisticsPowerProviderTileEntity method update.

@Override
public void update() {
    super.update();
    pauseRequesting = false;
    if (!init) {
        if (MainProxy.isClient(getWorld())) {
            LogisticsHUDRenderer.instance().add(this);
        }
        init = true;
    }
    double globalRequest = orders.values().stream().reduce(Double::sum).orElse(0.0);
    if (globalRequest > 0) {
        final double fullfillRatio = Math.min(1, Math.min(internalStorage, getMaxProvidePerTick()) / globalRequest);
        if (fullfillRatio > 0) {
            final Function<NeighborTileEntity<LogisticsTileGenericPipe>, CoreRoutedPipe> getPipe = (NeighborTileEntity<LogisticsTileGenericPipe> neighbor) -> (CoreRoutedPipe) neighbor.getTileEntity().pipe;
            orders.entrySet().stream().map(routerIdToOrderCount -> new Pair<>(SimpleServiceLocator.routerManager.getRouter(routerIdToOrderCount.getKey()), Math.min(internalStorage, routerIdToOrderCount.getValue() * fullfillRatio))).filter(destinationToPower -> destinationToPower.getValue1() != null && destinationToPower.getValue1().getPipe() != null).forEach(destinationToPower -> new WorldCoordinatesWrapper(this).allNeighborTileEntities().stream().flatMap(neighbor -> LPNeighborTileEntityKt.optionalIs(neighbor, LogisticsTileGenericPipe.class).map(Stream::of).orElseGet(Stream::empty)).filter(neighbor -> neighbor.getTileEntity().pipe instanceof CoreRoutedPipe && !getPipe.apply(neighbor).stillNeedReplace()).flatMap(neighbor -> getPipe.apply(neighbor).getRouter().getDistanceTo(destinationToPower.getValue1()).stream().map(exitRoute -> new Pair<>(neighbor, exitRoute))).filter(neighborToExit -> neighborToExit.getValue2().containsFlag(PipeRoutingConnectionType.canPowerSubSystemFrom) && neighborToExit.getValue2().filters.stream().noneMatch(IFilter::blockPower)).findFirst().ifPresent(neighborToSource -> {
                CoreRoutedPipe sourcePipe = getPipe.apply(neighborToSource.getValue1());
                if (sourcePipe.isInitialized()) {
                    sourcePipe.container.addLaser(neighborToSource.getValue1().getOurDirection(), 1, getLaserColor(), true, true);
                }
                sendPowerLaserPackets(sourcePipe.getRouter(), destinationToPower.getValue1(), neighborToSource.getValue2().exitOrientation, neighborToSource.getValue2().exitOrientation != neighborToSource.getValue1().getDirection());
                internalStorage -= destinationToPower.getValue2();
                // because calculations with floats
                if (internalStorage <= 0)
                    internalStorage = 0;
                handlePower(destinationToPower.getValue1().getPipe(), destinationToPower.getValue2());
            }));
        }
    }
    orders.clear();
    if (MainProxy.isServer(world)) {
        if (internalStorage != lastUpdateStorage) {
            updateClients();
            lastUpdateStorage = internalStorage;
        }
    }
}
Also used : PowerProviderGui(logisticspipes.network.guis.block.PowerProviderGui) IRouter(logisticspipes.routing.IRouter) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) LogisticsPipes(logisticspipes.LogisticsPipes) LogisticsSolidTileEntity(logisticspipes.blocks.LogisticsSolidTileEntity) HashMap(java.util.HashMap) CCType(logisticspipes.proxy.computers.interfaces.CCType) MainProxy(logisticspipes.proxy.MainProxy) Function(java.util.function.Function) IHeadUpDisplayRenderer(logisticspipes.interfaces.IHeadUpDisplayRenderer) HUDStartBlockWatchingPacket(logisticspipes.network.packets.hud.HUDStartBlockWatchingPacket) PlayerCollectionList(logisticspipes.utils.PlayerCollectionList) NBTTagFloat(net.minecraft.nbt.NBTTagFloat) IHeadUpDisplayBlockRendererProvider(logisticspipes.interfaces.IHeadUpDisplayBlockRendererProvider) ISubSystemPowerProvider(logisticspipes.interfaces.ISubSystemPowerProvider) CrashReportCategory(net.minecraft.crash.CrashReportCategory) LogisticsHUDRenderer(logisticspipes.renderer.LogisticsHUDRenderer) Map(java.util.Map) IBlockWatchingHandler(logisticspipes.interfaces.IBlockWatchingHandler) LPNeighborTileEntityKt(network.rs485.logisticspipes.connection.LPNeighborTileEntityKt) LinkedList(java.util.LinkedList) CoordinatesGuiProvider(logisticspipes.network.abstractguis.CoordinatesGuiProvider) CCCommand(logisticspipes.proxy.computers.interfaces.CCCommand) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NewGuiHandler(logisticspipes.network.NewGuiHandler) PipeRoutingConnectionType(logisticspipes.routing.PipeRoutingConnectionType) HUDStopBlockWatchingPacket(logisticspipes.network.packets.hud.HUDStopBlockWatchingPacket) IGuiOpenControler(logisticspipes.interfaces.IGuiOpenControler) EnumFacing(net.minecraft.util.EnumFacing) Triplet(logisticspipes.utils.tuples.Triplet) PacketHandler(logisticspipes.network.PacketHandler) ExitRoute(logisticspipes.routing.ExitRoute) PowerProviderLevel(logisticspipes.network.packets.block.PowerProviderLevel) ServerRouter(logisticspipes.routing.ServerRouter) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) Stream(java.util.stream.Stream) IFilter(logisticspipes.interfaces.routing.IFilter) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Pair(logisticspipes.utils.tuples.Pair) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) IGuiTileEntity(logisticspipes.interfaces.IGuiTileEntity) IPowerLevelDisplay(logisticspipes.interfaces.IPowerLevelDisplay) BitSet(java.util.BitSet) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) HUDPowerLevel(logisticspipes.gui.hud.HUDPowerLevel) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) Stream(java.util.stream.Stream) Pair(logisticspipes.utils.tuples.Pair)

Example 17 with WorldCoordinatesWrapper

use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper in project LogisticsPipes by RS485.

the class SneakyUpgradeConfigGuiProvider method getClientGui.

@Override
public Object getClientGui(EntityPlayer player) {
    LogisticsTileGenericPipe bPipe = getTileAs(player.world, LogisticsTileGenericPipe.class);
    if (!(bPipe.pipe instanceof CoreRoutedPipe)) {
        return null;
    }
    List<DoubleCoordinates> list = new WorldCoordinatesWrapper(bPipe).connectedTileEntities().stream().filter(in -> SimpleServiceLocator.pipeInformationManager.isNotAPipe(in.getTileEntity())).map(in -> new DoubleCoordinates(in.getTileEntity())).collect(Collectors.toList());
    if (list.isEmpty()) {
        list = new WorldCoordinatesWrapper(bPipe).connectedTileEntities().stream().map(in -> new DoubleCoordinates(in.getTileEntity())).collect(Collectors.toList());
    }
    return new SneakyConfigurationPopup(list, getSlot(player, UpgradeSlot.class));
}
Also used : UpgradeSlot(logisticspipes.utils.gui.UpgradeSlot) UpgradeCoordinatesGuiProvider(logisticspipes.network.abstractguis.UpgradeCoordinatesGuiProvider) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) Collectors(java.util.stream.Collectors) GuiProvider(logisticspipes.network.abstractguis.GuiProvider) DoubleCoordinates(network.rs485.logisticspipes.world.DoubleCoordinates) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) EntityPlayer(net.minecraft.entity.player.EntityPlayer) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) StaticResolve(logisticspipes.utils.StaticResolve) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) SneakyConfigurationPopup(logisticspipes.gui.popup.SneakyConfigurationPopup) LogisticsTileGenericPipe(logisticspipes.pipes.basic.LogisticsTileGenericPipe) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) SneakyConfigurationPopup(logisticspipes.gui.popup.SneakyConfigurationPopup) WorldCoordinatesWrapper(network.rs485.logisticspipes.world.WorldCoordinatesWrapper) DoubleCoordinates(network.rs485.logisticspipes.world.DoubleCoordinates) UpgradeSlot(logisticspipes.utils.gui.UpgradeSlot)

Example 18 with WorldCoordinatesWrapper

use of network.rs485.logisticspipes.world.WorldCoordinatesWrapper 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)

Aggregations

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