Skip to content

Commit

Permalink
preemptively check cached path is valid before simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Sep 1, 2024
1 parent 4728456 commit b9186ef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import mekanism.common.content.network.transmitter.LogisticalTransporterBase;
import mekanism.common.content.transporter.PathfinderCache;
import mekanism.common.content.transporter.TransporterManager;
import mekanism.common.content.transporter.TransporterPathfinder;
import mekanism.common.content.transporter.TransporterStack;
import mekanism.common.lib.inventory.TransitRequest;
import mekanism.common.lib.inventory.TransitRequest.TransitResponse;
Expand Down Expand Up @@ -42,7 +43,7 @@ public InventoryNetwork(Collection<InventoryNetwork> networks) {
}

public List<AcceptorData> calculateAcceptors(TransitRequest request, TransporterStack stack, Long2ObjectMap<ChunkAccess> chunkMap,
Map<GlobalPos, Set<TransporterStack>> additionalFlowingStacks) {
Map<GlobalPos, Set<TransporterStack>> additionalFlowingStacks, LogisticalTransporterBase start) {
List<AcceptorData> toReturn = new ArrayList<>();
for (Long2ObjectMap.Entry<Map<Direction, IItemHandler>> entry : acceptorCache.getAcceptorEntrySet()) {
BlockPos pos = BlockPos.of(entry.getLongKey());
Expand All @@ -53,6 +54,10 @@ public List<AcceptorData> calculateAcceptors(TransitRequest request, Transporter
for (Map.Entry<Direction, IItemHandler> acceptorEntry : entry.getValue().entrySet()) {
IItemHandler handler = acceptorEntry.getValue();
Direction side = acceptorEntry.getKey();
PathfinderCache.CachedPath cachedPath = PathfinderCache.getSingleCache(start, pos, side);
if (cachedPath != null && !TransporterPathfinder.checkPath(this, cachedPath.path(), stack)) {
continue;//invalid path, no need to simulate
}
//TODO: Figure out how we want to best handle the color check, as without doing it here we don't
// actually need to even query the TE
if (acceptor instanceof ISideConfiguration config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import it.unimi.dsi.fastutil.longs.LongList;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -53,6 +52,17 @@ public static CachedPath getCache(LogisticalTransporterBase start, BlockPos end,
return ret;
}

@Nullable
public static CachedPath getSingleCache(LogisticalTransporterBase start, BlockPos end, Direction side) {
UUID uuid = start.getTransmitterNetwork().getUUID();
Map<PathData, CachedPath> pathMap = cachedPaths.get(uuid);
if (pathMap != null) {
BlockPos startPos = start.getBlockPos();
return pathMap.get(new PathData(startPos, end, side));
}
return null;
}

public static void reset() {
cachedPaths.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static List<Destination> getPaths(LogisticalTransporterBase start, Trans
return Collections.emptyList();
}
Long2ObjectMap<ChunkAccess> chunkMap = new Long2ObjectOpenHashMap<>();
List<AcceptorData> acceptors = network.calculateAcceptors(request, stack, chunkMap, additionalFlowingStacks);
List<AcceptorData> acceptors = network.calculateAcceptors(request, stack, chunkMap, additionalFlowingStacks, start);
List<Destination> paths = new ArrayList<>();
for (AcceptorData data : acceptors) {
Destination path = getPath(network, data, start, stack, min, chunkMap);
Expand All @@ -64,7 +64,7 @@ private static List<Destination> getPaths(LogisticalTransporterBase start, Trans
return paths;
}

private static boolean checkPath(InventoryNetwork network, LongList path, TransporterStack stack) {
public static boolean checkPath(InventoryNetwork network, LongList path, TransporterStack stack) {
for (int i = path.size() - 1; i > 0; i--) {
LogisticalTransporterBase transmitter = network.getTransmitter(path.getLong(i));
if (transmitter == null) {
Expand Down

0 comments on commit b9186ef

Please sign in to comment.