use of logisticspipes.routing.IRouter in project LogisticsPipes by RS485.
the class ModuleCCBasedQuickSort method handle.
private boolean handle(IInventoryUtil invUtil, int slot, List<CCSinkResponder> list) {
if (list.isEmpty()) {
return false;
}
ItemIdentifier ident = list.get(0).getStack().getItem();
ItemStack stack = invUtil.getStackInSlot(slot);
if (stack == null || !ItemIdentifier.get(stack).equals(ident)) {
return false;
}
final IRouter source = _service.getRouter();
List<Triplet<Integer, Double, CCSinkResponder>> posibilities = new ArrayList<>();
for (CCSinkResponder sink : list) {
if (!sink.isDone()) {
continue;
}
if (sink.getCanSink() < 1) {
continue;
}
IRouter r = SimpleServiceLocator.routerManager.getRouter(sink.getRouterId());
if (r == null) {
continue;
}
List<ExitRoute> ways = source.getDistanceTo(r);
double minDistance = Double.MAX_VALUE;
outer: for (ExitRoute route : ways) {
for (IFilter filter : route.filters) {
if (filter.blockRouting() || filter.isFilteredItem(ident) == filter.isBlocked()) {
continue outer;
}
}
minDistance = Math.min(route.distanceToDestination, minDistance);
}
if (minDistance != Integer.MAX_VALUE) {
posibilities.add(new Triplet<>(sink.getPriority(), minDistance, sink));
}
}
if (posibilities.isEmpty()) {
return false;
}
Collections.sort(posibilities, (o1, o2) -> {
int c = o2.getValue1() - o1.getValue1();
if (c != 0) {
return c;
}
double e = o1.getValue2() - o2.getValue2();
return e < 0 ? -1 : 1;
});
boolean sended = false;
for (Triplet<Integer, Double, CCSinkResponder> triple : posibilities) {
CCSinkResponder sink = triple.getValue3();
if (sink.getCanSink() < 0) {
continue;
}
stack = invUtil.getStackInSlot(slot);
if (stack == null || stack.stackSize <= 0) {
continue;
}
int amount = Math.min(stack.stackSize, sink.getCanSink());
ItemStack extracted = invUtil.decrStackSize(slot, amount);
_service.sendStack(extracted, sink.getRouterId(), ItemSendMode.Fast, null);
sended = true;
}
return sended;
}
use of logisticspipes.routing.IRouter in project LogisticsPipes by RS485.
the class PipeItemsProviderLogistics method sendStack.
private int sendStack(ItemIdentifierStack stack, int maxCount, int destination, IAdditionalTargetInformation info) {
ItemIdentifier item = stack.getItem();
WorldCoordinatesWrapper worldCoordinates = new WorldCoordinatesWrapper(container);
//@formatter:off
Iterator<Pair<IInventoryUtil, ForgeDirection>> iterator = worldCoordinates.getConnectedAdjacentTileEntities(ConnectionPipeType.ITEM).filter(adjacent -> adjacent.tileEntity instanceof IInventory).filter(adjacent -> !SimpleServiceLocator.pipeInformationManager.isItemPipe(adjacent.tileEntity)).map(adjacent -> new Pair<>(getAdaptedInventoryUtil(adjacent), adjacent.direction)).iterator();
while (iterator.hasNext()) {
Pair<IInventoryUtil, ForgeDirection> next = iterator.next();
int available = next.getValue1().itemCount(item);
if (available == 0) {
continue;
}
int wanted = Math.min(available, stack.getStackSize());
wanted = Math.min(wanted, maxCount);
wanted = Math.min(wanted, item.getMaxStackSize());
IRouter dRtr = SimpleServiceLocator.routerManager.getRouterUnsafe(destination, false);
if (dRtr == null) {
_orderManager.sendFailed();
return 0;
}
SinkReply reply = LogisticsManager.canSink(dRtr, null, true, stack.getItem(), null, true, false);
boolean defersend = false;
if (reply != null) {
// some pipes are not aware of the space in the adjacent inventory, so they return null
if (reply.maxNumberOfItems < wanted) {
wanted = reply.maxNumberOfItems;
if (wanted <= 0) {
_orderManager.deferSend();
return 0;
}
defersend = true;
}
}
if (!canUseEnergy(wanted * neededEnergy())) {
return -1;
}
ItemStack removed = next.getValue1().getMultipleItems(item, wanted);
if (removed == null || removed.stackSize == 0) {
continue;
}
int sent = removed.stackSize;
useEnergy(sent * neededEnergy());
IRoutedItem routedItem = SimpleServiceLocator.routedItemHelper.createNewTravelItem(removed);
routedItem.setDestination(destination);
routedItem.setTransportMode(TransportMode.Active);
routedItem.setAdditionalTargetInformation(info);
super.queueRoutedItem(routedItem, next.getValue2());
_orderManager.sendSuccessfull(sent, defersend, routedItem);
return sent;
}
_orderManager.sendFailed();
return 0;
}
use of logisticspipes.routing.IRouter in project LogisticsPipes by RS485.
the class CoreRoutedPipe method sendRoutedItem.
private void sendRoutedItem(IRoutedItem routedItem, ForgeDirection from) {
if (from == null) {
throw new NullPointerException();
}
transport.injectItem(routedItem, from.getOpposite());
IRouter r = SimpleServiceLocator.routerManager.getRouterUnsafe(routedItem.getDestination(), false);
if (r != null) {
CoreRoutedPipe pipe = r.getCachedPipe();
if (pipe != null) {
pipe.notifyOfSend(routedItem.getInfo());
} else {
// TODO: handle sending items to known chunk-unloaded destination?
}
}
// should not be able to send to a non-existing router
// router.startTrackingRoutedItem((RoutedEntityItem) routedItem.getTravelingItem());
spawnParticle(Particles.OrangeParticle, 2);
stat_lifetime_sent++;
stat_session_sent++;
updateStats();
}
use of logisticspipes.routing.IRouter in project LogisticsPipes by RS485.
the class TDDuctInformationProvider method getDistanceTo.
@Override
public double getDistanceTo(int destinationint, ForgeDirection 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;
}
Iterable<Route> paramIterable = duct.getCache(true).outputRoutes;
double closesedConnection = Integer.MAX_VALUE;
for (Route localRoute1 : paramIterable) {
if (localRoute1.endPoint instanceof LPItemDuct) {
LPItemDuct lpDuct = (LPItemDuct) 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, ForgeDirection.getOrientation(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;
}
use of logisticspipes.routing.IRouter 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);
}
Aggregations