Search in sources :

Example 31 with IRouter

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

the class TDDuctInformationProvider method acceptItem.

@Override
public boolean acceptItem(LPTravelingItem item, TileEntity from) {
    if (item instanceof LPTravelingItemServer) {
        LPTravelingItemServer serverItem = (LPTravelingItemServer) item;
        int id = serverItem.getInfo().destinationint;
        if (id == -1) {
            id = SimpleServiceLocator.routerManager.getIDforUUID(serverItem.getInfo().destinationUUID);
        }
        IRouter destination = SimpleServiceLocator.routerManager.getRouter(id);
        if (destination == null) {
            return false;
        }
        RouteCache<DuctUnitItem, GridItem> routes = getDuct().getCache(true);
        Iterable<Route<DuctUnitItem, GridItem>> paramIterable = routes.outputRoutes;
        Route<DuctUnitItem, GridItem> route = null;
        Object cache = null;
        Triplet<Integer, ItemIdentifier, Boolean> key = new Triplet<>(id, item.getItemIdentifierStack().getItem(), serverItem.getInfo()._transportMode == TransportMode.Active);
        if (duct instanceof ILPTEInformation && ((ILPTEInformation) duct).getObject() != null) {
            cache = ((ILPTEInformation) duct).getObject().getCacheHolder().getCacheFor(CacheTypes.Routing, key);
        }
        if (cache instanceof Route) {
            // noinspection unchecked
            route = (Route<DuctUnitItem, GridItem>) cache;
            if (!routes.outputRoutes.contains(route)) {
                route = null;
            }
        }
        if (route == null) {
            Pair<Double, Route<DuctUnitItem, GridItem>> closesedConnection = null;
            List<DoubleCoordinates> visited = new ArrayList<>();
            visited.add(new DoubleCoordinates(from));
            for (Route<DuctUnitItem, GridItem> localRoute1 : paramIterable) {
                if (localRoute1.endPoint instanceof LPDuctUnitItem) {
                    LPDuctUnitItem lpDuct = (LPDuctUnitItem) localRoute1.endPoint;
                    double max = Integer.MAX_VALUE;
                    if (closesedConnection != null) {
                        max = closesedConnection.getValue1();
                    }
                    DoubleCoordinates pos = new DoubleCoordinates((TileEntity) lpDuct.pipe);
                    if (visited.contains(pos)) {
                        continue;
                    }
                    visited.add(pos);
                    double distance = lpDuct.pipe.getDistanceTo(id, EnumFacing.getFront(localRoute1.pathDirections.get(localRoute1.pathDirections.size() - 1)).getOpposite(), item.getItemIdentifierStack().getItem(), serverItem.getInfo()._transportMode == TransportMode.Active, localRoute1.pathWeight, max, visited);
                    visited.remove(pos);
                    if (distance != Integer.MAX_VALUE && (closesedConnection == null || distance + localRoute1.pathDirections.size() < closesedConnection.getValue1())) {
                        closesedConnection = new Pair<>(distance + localRoute1.pathWeight, localRoute1);
                    }
                }
            }
            if (closesedConnection != null) {
                route = closesedConnection.getValue2();
            }
        }
        if (route != null) {
            if (duct instanceof ILPTEInformation && ((ILPTEInformation) duct).getObject() != null) {
                ((ILPTEInformation) duct).getObject().getCacheHolder().setCache(CacheTypes.Routing, key, route);
            }
            TravelingItem travelItem = new TravelingItem(item.getItemIdentifierStack().makeNormalStack(), getDuct(), route.copy(), (byte) serverItem.output.ordinal(), (byte) 1);
            // noinspection ConstantConditions
            ((ILPTravelingItemInfo) travelItem).setLPRoutingInfoAddition(serverItem.getInfo());
            getDuct().insertNewItem(travelItem);
            return true;
        }
    } else {
        return true;
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) ItemIdentifier(logisticspipes.utils.item.ItemIdentifier) ILPTravelingItemInfo(logisticspipes.asm.td.ILPTravelingItemInfo) GridItem(cofh.thermaldynamics.duct.item.GridItem) IRouter(logisticspipes.routing.IRouter) TravelingItem(cofh.thermaldynamics.duct.item.TravelingItem) LPTravelingItem(logisticspipes.transport.LPTravelingItem) Route(cofh.thermaldynamics.multiblock.Route) LPTravelingItemServer(logisticspipes.transport.LPTravelingItem.LPTravelingItemServer) ILPTEInformation(logisticspipes.asm.te.ILPTEInformation) Triplet(logisticspipes.utils.tuples.Triplet) DoubleCoordinates(network.rs485.logisticspipes.world.DoubleCoordinates) DuctUnitItem(cofh.thermaldynamics.duct.item.DuctUnitItem)

Example 32 with IRouter

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

the class TDDuctInformationProvider method getDistanceTo.

@Override
public double getDistanceTo(int destinationint, EnumFacing ignore, ItemIdentifier ident, boolean isActive, double traveled, double max, List<DoubleCoordinates> visited) {
    if (traveled >= max) {
        return Integer.MAX_VALUE;
    }
    IRouter destination = SimpleServiceLocator.routerManager.getRouter(destinationint);
    if (destination == null) {
        return Integer.MAX_VALUE;
    }
    LinkedList<Route<DuctUnitItem, GridItem>> paramIterable = getDuct().getCache(true).outputRoutes;
    double closesedConnection = Integer.MAX_VALUE;
    for (Route<DuctUnitItem, GridItem> localRoute1 : paramIterable) {
        if (localRoute1.endPoint instanceof LPDuctUnitItem) {
            LPDuctUnitItem lpDuct = (LPDuctUnitItem) localRoute1.endPoint;
            if (traveled + localRoute1.pathWeight > max) {
                continue;
            }
            DoubleCoordinates pos = new DoubleCoordinates((TileEntity) lpDuct.pipe);
            if (visited.contains(pos)) {
                continue;
            }
            visited.add(pos);
            double distance = lpDuct.pipe.getDistanceTo(destinationint, EnumFacing.getFront(localRoute1.pathDirections.get(localRoute1.pathDirections.size() - 1)).getOpposite(), ident, isActive, traveled + localRoute1.pathWeight, Math.min(max, closesedConnection), visited);
            visited.remove(pos);
            if (distance != Integer.MAX_VALUE && distance + localRoute1.pathWeight < closesedConnection) {
                closesedConnection = distance + localRoute1.pathWeight;
            }
        }
    }
    return closesedConnection;
}
Also used : GridItem(cofh.thermaldynamics.duct.item.GridItem) IRouter(logisticspipes.routing.IRouter) DuctUnitItem(cofh.thermaldynamics.duct.item.DuctUnitItem) DoubleCoordinates(network.rs485.logisticspipes.world.DoubleCoordinates) Route(cofh.thermaldynamics.multiblock.Route)

Aggregations

IRouter (logisticspipes.routing.IRouter)32 ExitRoute (logisticspipes.routing.ExitRoute)19 CoreRoutedPipe (logisticspipes.pipes.basic.CoreRoutedPipe)16 ArrayList (java.util.ArrayList)15 LinkedList (java.util.LinkedList)13 List (java.util.List)12 IFilter (logisticspipes.interfaces.routing.IFilter)10 PipeRoutingConnectionType (logisticspipes.routing.PipeRoutingConnectionType)8 Pair (logisticspipes.utils.tuples.Pair)7 ItemStack (net.minecraft.item.ItemStack)7 BitSet (java.util.BitSet)6 HashMap (java.util.HashMap)6 SimpleServiceLocator (logisticspipes.proxy.SimpleServiceLocator)6 Map (java.util.Map)5 ItemIdentifier (logisticspipes.utils.item.ItemIdentifier)5 ItemIdentifierStack (logisticspipes.utils.item.ItemIdentifierStack)5 EnumFacing (net.minecraft.util.EnumFacing)5 LogisticsTileGenericPipe (logisticspipes.pipes.basic.LogisticsTileGenericPipe)4 IResource (logisticspipes.request.resources.IResource)4 LaserData (logisticspipes.routing.LaserData)4