Search in sources :

Example 1 with FluidSinkReply

use of logisticspipes.utils.FluidSinkReply in project LogisticsPipes by RS485.

the class LogisticsFluidManager method getBestReply.

@Override
@Nullable
public Pair<Integer, FluidSinkReply> getBestReply(FluidIdentifierStack stack, IRouter sourceRouter, List<Integer> jamList) {
    Optional<Pair<Integer, FluidSinkReply>> bestReply = sourceRouter.getIRoutersByCost().stream().filter(it -> it.containsFlag(PipeRoutingConnectionType.canRouteTo) && it.destination.getId() != sourceRouter.getId() && !jamList.contains(it.destination.getSimpleID()) && it.destination.getPipe() instanceof IFluidSink && it.destination.getPipe().isEnabled() && !it.destination.getPipe().isOnSameContainer(sourceRouter.getPipe())).sorted().map(it -> new Pair<>(it.destination.getSimpleID(), ((IFluidSink) it.destination.getPipe()).sinkAmount(stack))).filter(pair -> pair.getValue2() != null && pair.getValue2().sinkAmount != 0L).reduce((left, right) -> left.getValue2().fixedPriority.compareTo(right.getValue2().fixedPriority) < 0 ? right : left);
    bestReply.ifPresent(pair -> {
        ServerRouter serverRouter = SimpleServiceLocator.routerManager.getServerRouter(pair.getValue1());
        if (serverRouter == null)
            return;
        CoreRoutedPipe pipe = serverRouter.getPipe();
        if (pipe != null)
            pipe.spawnParticle(Particles.BlueParticle, 10);
    });
    return bestReply.orElse(null);
}
Also used : IFluidSink(logisticspipes.interfaces.routing.IFluidSink) IRouter(logisticspipes.routing.IRouter) IFluidSink(logisticspipes.interfaces.routing.IFluidSink) Particles(logisticspipes.pipefxhandlers.Particles) HashMap(java.util.HashMap) LogisticsFluidContainer(logisticspipes.items.LogisticsFluidContainer) TreeSet(java.util.TreeSet) ItemStack(net.minecraft.item.ItemStack) LPItems(logisticspipes.LPItems) FluidIdentifier(logisticspipes.utils.FluidIdentifier) FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) FluidSinkReply(logisticspipes.utils.FluidSinkReply) Map(java.util.Map) IProvideFluids(logisticspipes.interfaces.routing.IProvideFluids) Nullable(javax.annotation.Nullable) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) PipeRoutingConnectionType(logisticspipes.routing.PipeRoutingConnectionType) Collectors(java.util.stream.Collectors) ExitRoute(logisticspipes.routing.ExitRoute) ServerRouter(logisticspipes.routing.ServerRouter) List(java.util.List) SimpleServiceLocator(logisticspipes.proxy.SimpleServiceLocator) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) Pair(logisticspipes.utils.tuples.Pair) Entry(java.util.Map.Entry) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) Optional(java.util.Optional) FluidStack(net.minecraftforge.fluids.FluidStack) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) ServerRouter(logisticspipes.routing.ServerRouter) Pair(logisticspipes.utils.tuples.Pair) Nullable(javax.annotation.Nullable)

Example 2 with FluidSinkReply

use of logisticspipes.utils.FluidSinkReply in project LogisticsPipes by RS485.

the class FluidRoutedPipe method endReached.

public boolean endReached(LPTravelingItemServer arrivingItem, TileEntity tile) {
    if (canInsertToTanks() && MainProxy.isServer(getWorld())) {
        getCacheHolder().trigger(CacheTypes.Inventory);
        if (arrivingItem.getItemIdentifierStack() == null || !(arrivingItem.getItemIdentifierStack().getItem().isFluidContainer())) {
            return false;
        }
        if (getRouter().getSimpleID() != arrivingItem.getDestination()) {
            return false;
        }
        int filled;
        FluidIdentifierStack liquid = SimpleServiceLocator.logisticsFluidManager.getFluidFromContainer(arrivingItem.getItemIdentifierStack());
        if (isConnectableTank(tile, arrivingItem.output, false)) {
            // Try to put liquid into all adjacent tanks.
            for (Pair<NeighborTileEntity<TileEntity>, ITankUtil> util : PipeFluidUtil.INSTANCE.getAdjacentTanks(this, false)) {
                filled = util.getValue2().fill(liquid, true);
                liquid.lowerAmount(filled);
                if (liquid.getAmount() != 0) {
                    continue;
                }
                return true;
            }
            // Try inserting the liquid into the pipe side tank
            filled = ((PipeFluidTransportLogistics) transport).sideTanks[arrivingItem.output.ordinal()].fill(liquid.makeFluidStack(), true);
            if (filled == liquid.getAmount()) {
                return true;
            }
            liquid.lowerAmount(filled);
        }
        // Try inserting the liquid into the pipe internal tank
        filled = ((PipeFluidTransportLogistics) transport).internalTank.fill(liquid.makeFluidStack(), true);
        if (filled == liquid.getAmount()) {
            return true;
        }
        // If liquids still exist,
        liquid.lowerAmount(filled);
        // TODO: FIX THIS
        if (this instanceof IRequireReliableFluidTransport) {
            ((IRequireReliableFluidTransport) this).liquidNotInserted(liquid.getFluid(), liquid.getAmount());
        }
        IRoutedItem routedItem = SimpleServiceLocator.routedItemHelper.createNewTravelItem(SimpleServiceLocator.logisticsFluidManager.getFluidContainer(liquid));
        Pair<Integer, FluidSinkReply> replies = SimpleServiceLocator.logisticsFluidManager.getBestReply(liquid, getRouter(), routedItem.getJamList());
        if (replies == null) {
            // clear destination without marking item as lost
            routedItem.setDestination(0);
        } else {
            int dest = replies.getValue1();
            routedItem.setDestination(dest);
        }
        routedItem.setTransportMode(TransportMode.Passive);
        this.queueRoutedItem(routedItem, arrivingItem.output.getOpposite());
        return true;
    }
    return false;
}
Also used : FluidIdentifierStack(logisticspipes.utils.FluidIdentifierStack) ITankUtil(logisticspipes.interfaces.ITankUtil) FluidSinkReply(logisticspipes.utils.FluidSinkReply) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) NeighborTileEntity(network.rs485.logisticspipes.connection.NeighborTileEntity) PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) IRequireReliableFluidTransport(logisticspipes.interfaces.routing.IRequireReliableFluidTransport)

Example 3 with FluidSinkReply

use of logisticspipes.utils.FluidSinkReply in project LogisticsPipes by RS485.

the class LogisticsManager method assignDestinationFor.

/**
 * Will assign a destination for a IRoutedItem based on a best sink reply
 * recieved from other pipes.
 *
 * @param item
 *            The item that needs to be routed.
 * @param sourceRouterID
 *            The SimpleID of the pipe that is sending the item. (the
 *            routedItem will cache the UUID, and that the SimpleID belongs
 *            to the UUID will be checked when appropriate)
 * @param excludeSource
 *            Boolean, true means that it wont set the source as the
 *            destination.
 * @return IRoutedItem with a newly assigned destination
 */
@Override
public IRoutedItem assignDestinationFor(IRoutedItem item, int sourceRouterID, boolean excludeSource) {
    // Assert: only called server side.
    // If we for some reason can't get the router we can't do anything either
    IRouter sourceRouter = SimpleServiceLocator.routerManager.getServerRouter(sourceRouterID);
    if (sourceRouter == null) {
        return item;
    }
    // Wipe current destination
    item.clearDestination();
    final ItemIdentifierStack itemIdStack = item.getItemIdentifierStack();
    if (itemIdStack == null) {
        return item;
    }
    BitSet routersIndex = ServerRouter.getRoutersInterestedIn(itemIdStack.getItem());
    // get the routing table
    List<ExitRoute> validDestinations = new ArrayList<>();
    for (int i = routersIndex.nextSetBit(0); i >= 0; i = routersIndex.nextSetBit(i + 1)) {
        IRouter r = SimpleServiceLocator.routerManager.getServerRouter(i);
        List<ExitRoute> exits = sourceRouter.getDistanceTo(r);
        if (exits != null) {
            validDestinations.addAll(exits.stream().filter(e -> e.containsFlag(PipeRoutingConnectionType.canRouteTo)).collect(Collectors.toList()));
        }
    }
    Collections.sort(validDestinations);
    final ItemStack stack = itemIdStack.makeNormalStack();
    if (stack.getItem() instanceof LogisticsFluidContainer) {
        Pair<Integer, FluidSinkReply> bestReply = SimpleServiceLocator.logisticsFluidManager.getBestReply(SimpleServiceLocator.logisticsFluidManager.getFluidFromContainer(itemIdStack), sourceRouter, item.getJamList());
        if (bestReply != null) {
            item.setDestination(bestReply.getValue1());
        }
    } else {
        Triplet<Integer, SinkReply, List<IFilter>> bestReply = getBestReply(stack, itemIdStack.getItem(), sourceRouter, validDestinations, excludeSource, item.getJamList(), null, true);
        if (bestReply.getValue1() != null && bestReply.getValue1() != 0) {
            item.setDestination(bestReply.getValue1());
            if (bestReply.getValue2().isPassive) {
                if (bestReply.getValue2().isDefault) {
                    item.setTransportMode(TransportMode.Default);
                } else {
                    item.setTransportMode(TransportMode.Passive);
                }
            } else {
                item.setTransportMode(TransportMode.Active);
            }
            item.setAdditionalTargetInformation(bestReply.getValue2().addInfo);
        }
    }
    return item;
}
Also used : BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) FluidSinkReply(logisticspipes.utils.FluidSinkReply) LogisticsFluidContainer(logisticspipes.items.LogisticsFluidContainer) IRouter(logisticspipes.routing.IRouter) SinkReply(logisticspipes.utils.SinkReply) FluidSinkReply(logisticspipes.utils.FluidSinkReply) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) ExitRoute(logisticspipes.routing.ExitRoute) ItemStack(net.minecraft.item.ItemStack)

Example 4 with FluidSinkReply

use of logisticspipes.utils.FluidSinkReply in project LogisticsPipes by RS485.

the class PipeFluidInsertion method enabledUpdateEntity.

@Override
public void enabledUpdateEntity() {
    super.enabledUpdateEntity();
    List<Integer> tempJamList = new ArrayList<>();
    if (!localJamList.isEmpty()) {
        List<Pair<Integer, Integer>> toRemove = new ArrayList<>();
        for (Pair<Integer, Integer> part : localJamList) {
            part.setValue2(part.getValue2() - 1);
            if (part.getValue2() <= 0) {
                toRemove.add(part);
            } else {
                tempJamList.add(part.getValue1());
            }
        }
        if (!toRemove.isEmpty()) {
            localJamList.removeAll(toRemove);
        }
    }
    PipeFluidTransportLogistics transport = (PipeFluidTransportLogistics) this.transport;
    for (EnumFacing dir : EnumFacing.VALUES) {
        FluidStack stack = transport.sideTanks[dir.ordinal()].getFluid();
        if (stack == null) {
            continue;
        }
        stack = stack.copy();
        if (nextSendMax[dir.ordinal()] > 0 && stack.amount < transport.sideTanks[dir.ordinal()].getCapacity()) {
            nextSendMax[dir.ordinal()]--;
            continue;
        }
        if (nextSendMin[dir.ordinal()] > 0) {
            nextSendMin[dir.ordinal()]--;
            continue;
        }
        Pair<Integer, FluidSinkReply> result = SimpleServiceLocator.logisticsFluidManager.getBestReply(FluidIdentifierStack.getFromStack(stack), getRouter(), tempJamList);
        if (result == null || result.getValue2().sinkAmount <= 0) {
            nextSendMax[dir.ordinal()] = 100;
            nextSendMin[dir.ordinal()] = 10;
            continue;
        }
        if (!useEnergy((int) (0.01 * result.getValue2().getSinkAmountInt()))) {
            nextSendMax[dir.ordinal()] = 100;
            nextSendMin[dir.ordinal()] = 10;
            continue;
        }
        FluidStack toSend = transport.sideTanks[dir.ordinal()].drain(result.getValue2().getSinkAmountInt(), true);
        ItemIdentifierStack liquidContainer = SimpleServiceLocator.logisticsFluidManager.getFluidContainer(FluidIdentifierStack.getFromStack(toSend));
        IRoutedItem routed = SimpleServiceLocator.routedItemHelper.createNewTravelItem(liquidContainer);
        routed.setDestination(result.getValue1());
        routed.setTransportMode(TransportMode.Passive);
        this.queueRoutedItem(routed, dir);
        nextSendMax[dir.ordinal()] = 100;
        nextSendMin[dir.ordinal()] = 5;
    }
}
Also used : PipeFluidTransportLogistics(logisticspipes.transport.PipeFluidTransportLogistics) FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) FluidSinkReply(logisticspipes.utils.FluidSinkReply) IRoutedItem(logisticspipes.logisticspipes.IRoutedItem) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) Pair(logisticspipes.utils.tuples.Pair)

Aggregations

FluidSinkReply (logisticspipes.utils.FluidSinkReply)4 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 LogisticsFluidContainer (logisticspipes.items.LogisticsFluidContainer)2 IRoutedItem (logisticspipes.logisticspipes.IRoutedItem)2 ExitRoute (logisticspipes.routing.ExitRoute)2 IRouter (logisticspipes.routing.IRouter)2 PipeFluidTransportLogistics (logisticspipes.transport.PipeFluidTransportLogistics)2 FluidIdentifierStack (logisticspipes.utils.FluidIdentifierStack)2 Pair (logisticspipes.utils.tuples.Pair)2 ItemStack (net.minecraft.item.ItemStack)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 TreeSet (java.util.TreeSet)1