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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations