Search in sources :

Example 6 with ExitRoute

use of logisticspipes.routing.ExitRoute in project LogisticsPipes by RS485.

the class LogisticsManager method getAmountFor.

@Override
public int getAmountFor(ItemIdentifier itemType, List<ExitRoute> validDestinations) {
    // TODO: Replace this entire function wiht a fetch from the pre-built arrays (path incoming later)
    List<Map<ItemIdentifier, Integer>> items = new ArrayList<>(ServerRouter.getBiggestSimpleID());
    for (int i = 0; i < ServerRouter.getBiggestSimpleID(); i++) {
        items.add(new HashMap<>());
    }
    BitSet used = new BitSet(ServerRouter.getBiggestSimpleID());
    outer: for (ExitRoute r : validDestinations) {
        if (r == null) {
            continue;
        }
        if (!r.containsFlag(PipeRoutingConnectionType.canRequestFrom)) {
            continue;
        }
        if (!(r.destination.getPipe() instanceof IProvideItems)) {
            continue;
        }
        for (IFilter filter : r.filters) {
            if (filter.blockProvider()) {
                continue outer;
            }
        }
        IProvideItems provider = (IProvideItems) r.destination.getPipe();
        provider.getAllItems(items.get(r.destination.getSimpleID()), r.filters);
        used.set(r.destination.getSimpleID(), true);
    }
    // TODO: Fix this doubly nested list
    int amount = 0;
    for (Map<ItemIdentifier, Integer> allItems : items) {
        for (Entry<ItemIdentifier, Integer> item : allItems.entrySet()) {
            if (item.getKey().equals(itemType)) {
                amount += item.getValue();
            }
        }
    }
    return amount;
}
Also used : ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) IProvideItems(logisticspipes.interfaces.routing.IProvideItems) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IFilter(logisticspipes.interfaces.routing.IFilter) ExitRoute(logisticspipes.routing.ExitRoute) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ExitRoute

use of logisticspipes.routing.ExitRoute in project LogisticsPipes by RS485.

the class LogisticsFluidManager method getBestReply.

@Override
public Pair<Integer, Integer> getBestReply(FluidStack stack, IRouter sourceRouter, List<Integer> jamList) {
    for (ExitRoute candidateRouter : sourceRouter.getIRoutersByCost()) {
        if (!candidateRouter.containsFlag(PipeRoutingConnectionType.canRouteTo)) {
            continue;
        }
        if (candidateRouter.destination.getSimpleID() == sourceRouter.getSimpleID()) {
            continue;
        }
        if (jamList.contains(candidateRouter.destination.getSimpleID())) {
            continue;
        }
        if (candidateRouter.destination.getPipe() == null || !candidateRouter.destination.getPipe().isEnabled()) {
            continue;
        }
        CoreRoutedPipe pipe = candidateRouter.destination.getPipe();
        if (!(pipe instanceof IFluidSink)) {
            continue;
        }
        int amount = ((IFluidSink) pipe).sinkAmount(stack);
        if (amount > 0) {
            Pair<Integer, Integer> result = new Pair<>(candidateRouter.destination.getSimpleID(), amount);
            return result;
        }
    }
    Pair<Integer, Integer> result = new Pair<>(0, 0);
    return result;
}
Also used : IFluidSink(logisticspipes.interfaces.routing.IFluidSink) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) ExitRoute(logisticspipes.routing.ExitRoute) Pair(logisticspipes.utils.tuples.Pair)

Example 8 with ExitRoute

use of logisticspipes.routing.ExitRoute in project LogisticsPipes by RS485.

the class LogisticsManager method getBestReply.

private Triplet<Integer, SinkReply, List<IFilter>> getBestReply(ItemIdentifier stack, IRouter sourceRouter, List<ExitRoute> validDestinations, boolean excludeSource, List<Integer> jamList, Triplet<Integer, SinkReply, List<IFilter>> result, boolean allowDefault) {
    if (result == null) {
        result = new Triplet<>(null, null, null);
    }
    outer: for (ExitRoute candidateRouter : validDestinations) {
        if (excludeSource) {
            if (candidateRouter.destination.getId().equals(sourceRouter.getId())) {
                continue;
            }
        }
        if (jamList.contains(candidateRouter.destination.getSimpleID())) {
            continue;
        }
        if (!candidateRouter.containsFlag(PipeRoutingConnectionType.canRouteTo)) {
            continue;
        }
        for (IFilter filter : candidateRouter.filters) {
            if (filter.blockRouting() || (filter.isBlocked() == filter.isFilteredItem(stack))) {
                continue outer;
            }
        }
        SinkReply reply = LogisticsManager.canSink(candidateRouter.destination, sourceRouter, excludeSource, stack, result.getValue2(), false, allowDefault);
        if (reply == null) {
            continue;
        }
        if (result.getValue1() == null) {
            result.setValue1(candidateRouter.destination.getSimpleID());
            result.setValue2(reply);
            List<IFilter> list = new LinkedList<>();
            result.setValue3(list);
            continue;
        }
        if (reply.fixedPriority.ordinal() > result.getValue2().fixedPriority.ordinal()) {
            result.setValue1(candidateRouter.destination.getSimpleID());
            result.setValue2(reply);
            List<IFilter> list = new LinkedList<>();
            result.setValue3(list);
            continue;
        }
        if (reply.fixedPriority == result.getValue2().fixedPriority && reply.customPriority > result.getValue2().customPriority) {
            result.setValue1(candidateRouter.destination.getSimpleID());
            result.setValue2(reply);
            List<IFilter> list = new LinkedList<>();
            result.setValue3(list);
            continue;
        }
    }
    if (result.getValue1() != null) {
        CoreRoutedPipe pipe = SimpleServiceLocator.routerManager.getRouterUnsafe(result.getValue1(), false).getPipe();
        pipe.useEnergy(result.getValue2().energyUse);
        pipe.spawnParticle(Particles.BlueParticle, 10);
    }
    return result;
}
Also used : IFilter(logisticspipes.interfaces.routing.IFilter) SinkReply(logisticspipes.utils.SinkReply) CoreRoutedPipe(logisticspipes.pipes.basic.CoreRoutedPipe) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExitRoute(logisticspipes.routing.ExitRoute)

Example 9 with ExitRoute

use of logisticspipes.routing.ExitRoute in project LogisticsPipes by RS485.

the class LogisticsManager method hasDestination.

/**
	 * Method used to check if a given stack has a destination.
	 * 
	 * @return Triplet of destinationSimpleID, sinkreply, relays; null if
	 *         nothing found
	 * @param stack
	 *            The stack to check if it has destination.
	 * @param allowDefault
	 *            Boolean, if true then a default route will be considered a
	 *            valid destination.
	 * @param sourceRouter
	 *            The UUID of the router pipe that wants to send the stack.
	 * @param excludeSource
	 *            Boolean, true means it will not consider the pipe itself as a
	 *            valid destination.
	 */
@Override
public Triplet<Integer, SinkReply, List<IFilter>> hasDestination(ItemIdentifier stack, boolean allowDefault, int sourceID, List<Integer> routerIDsToExclude) {
    IRouter sourceRouter = SimpleServiceLocator.routerManager.getRouter(sourceID);
    if (sourceRouter == null) {
        return null;
    }
    BitSet routersIndex = ServerRouter.getRoutersInterestedIn(stack);
    // 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.getRouterUnsafe(i, false);
        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);
    Triplet<Integer, SinkReply, List<IFilter>> search = getBestReply(stack, sourceRouter, validDestinations, true, routerIDsToExclude, null, allowDefault);
    if (search.getValue2() == null) {
        return null;
    }
    if (!allowDefault && search.getValue2().isDefault) {
        return null;
    }
    return search;
}
Also used : IRouter(logisticspipes.routing.IRouter) SinkReply(logisticspipes.utils.SinkReply) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExitRoute(logisticspipes.routing.ExitRoute)

Example 10 with ExitRoute

use of logisticspipes.routing.ExitRoute in project LogisticsPipes by RS485.

the class LogisticsManager method getCraftableItems.

/**
	 * @param validDestinations
	 *            a List of ExitRoute of valid destinations.
	 * @return LinkedList with ItemIdentifier
	 */
@Override
public LinkedList<ItemIdentifier> getCraftableItems(List<ExitRoute> validDestinations) {
    LinkedList<ItemIdentifier> craftableItems = new LinkedList<>();
    BitSet used = new BitSet(ServerRouter.getBiggestSimpleID());
    outer: for (ExitRoute r : validDestinations) {
        if (r == null) {
            continue;
        }
        if (!r.containsFlag(PipeRoutingConnectionType.canRequestFrom)) {
            continue;
        }
        if (used.get(r.destination.getSimpleID())) {
            continue;
        }
        if (!(r.destination.getPipe() instanceof ICraftItems)) {
            continue;
        }
        for (IFilter filter : r.filters) {
            if (filter.blockCrafting()) {
                continue outer;
            }
        }
        ICraftItems crafter = (ICraftItems) r.destination.getPipe();
        List<ItemIdentifierStack> craftedItems = crafter.getCraftedItems();
        if (craftedItems != null) {
            outer2: for (ItemIdentifierStack craftedItem : craftedItems) {
                if (craftedItem != null && !craftableItems.contains(craftedItem.getItem())) {
                    for (IFilter filter : r.filters) {
                        if (filter.isBlocked() == filter.isFilteredItem(craftedItem.getItem())) {
                            continue outer2;
                        }
                    }
                    craftableItems.add(craftedItem.getItem());
                }
            }
        }
        used.set(r.destination.getSimpleID(), true);
    }
    return craftableItems;
}
Also used : ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) IFilter(logisticspipes.interfaces.routing.IFilter) BitSet(java.util.BitSet) ICraftItems(logisticspipes.interfaces.routing.ICraftItems) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExitRoute(logisticspipes.routing.ExitRoute) ItemIdentifierStack(logisticspipes.utils.item.ItemIdentifierStack) LinkedList(java.util.LinkedList)

Aggregations

ExitRoute (logisticspipes.routing.ExitRoute)30 IRouter (logisticspipes.routing.IRouter)17 ArrayList (java.util.ArrayList)16 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)16 LinkedList (java.util.LinkedList)12 IFilter (logisticspipes.interfaces.routing.IFilter)12 List (java.util.List)11 BitSet (java.util.BitSet)10 PipeRoutingConnectionType (logisticspipes.routing.PipeRoutingConnectionType)8 HashMap (java.util.HashMap)7 Pair (logisticspipes.utils.tuples.Pair)7 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)7 Map (java.util.Map)5 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)5 LogisticsTileGenericPipe (logisticspipes.pipes.basic.LogisticsTileGenericPipe)4 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)4 LaserData (logisticspipes.routing.LaserData)3 ServerRouter (logisticspipes.routing.ServerRouter)3 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2