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