Skip to content

Commit 84a8b96

Browse files
committed
move Logistical pathfinding to long blockpos and some transmitter methods
1 parent 5248e78 commit 84a8b96

File tree

9 files changed

+185
-142
lines changed

9 files changed

+185
-142
lines changed

src/main/java/mekanism/common/capabilities/item/CursedTransporterItemHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public ItemStack insertItem(int slot, @NotNull ItemStack itemStack, boolean simu
9999
if (stack.getPathType().hasTarget()) {
100100
//If the stack actually has a path add that simulated insert to a list of locally simulated flowing stacks so that
101101
// if the mod simulates against the next slot as well we can give a more accurate result
102-
simulatedFlowingStacks.computeIfAbsent(GlobalPos.of(transporter.getLevel().dimension(), stack.getDest()), k -> new ObjectOpenHashSet<>()).add(stack);
102+
simulatedFlowingStacks.computeIfAbsent(GlobalPos.of(transporter.getLevel().dimension(), BlockPos.of(stack.getDest())), k -> new ObjectOpenHashSet<>()).add(stack);
103103
}
104104
} else {
105105
if (!seenExecutedStacks.add(itemStack)) {

src/main/java/mekanism/common/content/network/transmitter/LogisticalTransporterBase.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void onUpdateServer() {
152152
int stackId = entry.getIntKey();
153153
TransporterStack stack = entry.getValue();
154154
if (!stack.initiatedPath) {//Initiate any paths and remove things that can't go places
155-
if (stack.itemStack.isEmpty() || !recalculate(stackId, stack, null)) {
155+
if (stack.itemStack.isEmpty() || !recalculate(stackId, stack, Long.MAX_VALUE)) {
156156
deletes.add(stackId);
157157
continue;
158158
}
@@ -161,15 +161,16 @@ public void onUpdateServer() {
161161
int prevProgress = stack.progress;
162162
stack.progress += tier.getSpeed();
163163
if (stack.progress >= 100) {
164-
BlockPos prevSet = null;
164+
long prevSet = Long.MAX_VALUE;
165165
if (stack.hasPath()) {
166-
int currentIndex = stack.getPath().indexOf(pos);
166+
int currentIndex = stack.getPath().indexOf(pos.asLong());
167167
if (currentIndex == 0) { //Necessary for transition reasons, not sure why
168168
deletes.add(stackId);
169169
continue;
170170
}
171-
BlockPos next = stack.getPath().get(currentIndex - 1);
172-
if (next != null) {
171+
long next = stack.getPath().getLong(currentIndex - 1);
172+
if (next != Long.MAX_VALUE) {
173+
BlockPos nextPos = BlockPos.of(next);
173174
if (!stack.isFinal(this)) {
174175
//If this is not the final transporter try transferring it to the next one
175176
LogisticalTransporterBase transmitter = network.getTransmitter(next);
@@ -183,13 +184,13 @@ public void onUpdateServer() {
183184
//Otherwise, try to insert it into the destination inventory
184185
//Get the handler we are trying to insert into from the network's acceptor cache
185186
Direction side = stack.getSide(this).getOpposite();
186-
IItemHandler acceptor = network.getCachedAcceptor(next, side);
187+
IItemHandler acceptor = network.getCachedAcceptor(nextPos, side);
187188
if (acceptor == null && stack.getPathType().isHome()) {
188189
//TODO: Cache this capability. The issue is that when we are sending it back home
189190
// if it pulled the item itself, then it isn't in our cached acceptors, and thus won't be able to insert it
190-
acceptor = Capabilities.ITEM.getCapabilityIfLoaded(getLevel(), next, side);
191+
acceptor = Capabilities.ITEM.getCapabilityIfLoaded(getLevel(), nextPos, side);
191192
}
192-
TransitResponse response = TransitRequest.simple(stack.itemStack).addToInventory(getLevel(), next, acceptor, 0,
193+
TransitResponse response = TransitRequest.simple(stack.itemStack).addToInventory(getLevel(), nextPos, acceptor, 0,
193194
stack.getPathType().isHome());
194195
if (!response.isEmpty()) {
195196
//We were able to add at least part of the stack to the inventory
@@ -211,7 +212,7 @@ public void onUpdateServer() {
211212
}
212213
if (!recalculate(stackId, stack, prevSet)) {
213214
deletes.add(stackId);
214-
} else if (prevSet == null) {
215+
} else if (prevSet == Long.MAX_VALUE) {
215216
stack.progress = 50;
216217
} else {
217218
stack.progress = 0;
@@ -224,14 +225,14 @@ public void onUpdateServer() {
224225
Direction side = stack.getSide(this);
225226
ConnectionType connectionType = getConnectionType(side);
226227
tryRecalculate = !connectionType.canSendTo() ||
227-
!TransporterUtils.canInsert(getLevel(), stack.getDest(), stack.color, stack.itemStack, side, pathType.isHome());
228+
!TransporterUtils.canInsert(getLevel(), BlockPos.of(stack.getDest()), stack.color, stack.itemStack, side, pathType.isHome());
228229
} else {
229230
//Try to recalculate idles once they reach their destination
230231
tryRecalculate = true;
231232
}
232233
} else {
233-
BlockPos nextPos = stack.getNext(this);
234-
if (nextPos == null) {
234+
long nextPos = stack.getNext(this);
235+
if (nextPos == Long.MAX_VALUE) {
235236
tryRecalculate = true;
236237
} else {
237238
Direction nextSide = stack.getSide(pos, nextPos);
@@ -246,7 +247,7 @@ public void onUpdateServer() {
246247
}
247248
}
248249
}
249-
if (tryRecalculate && !recalculate(stackId, stack, null)) {
250+
if (tryRecalculate && !recalculate(stackId, stack, Long.MAX_VALUE)) {
250251
deletes.add(stackId);
251252
}
252253
}
@@ -379,7 +380,7 @@ public void addStack(int id, TransporterStack s) {
379380
transit.put(id, s);
380381
}
381382

382-
private boolean recalculate(int stackId, TransporterStack stack, BlockPos from) {
383+
private boolean recalculate(int stackId, TransporterStack stack, long from) {
383384
//TODO: Why do we skip recalculating the path if it is idle. Is it possible for idle paths to eventually stop being idle or are they just idle forever??
384385
boolean noPath = stack.getPathType().noTarget() || stack.recalculatePath(TransitRequest.simple(stack.itemStack), this, 0).isEmpty();
385386
if (noPath && !stack.calculateIdle(this)) {
@@ -389,8 +390,8 @@ private boolean recalculate(int stackId, TransporterStack stack, BlockPos from)
389390

390391
//Only add to needsSync if true is being returned; otherwise it gets added to deletes
391392
needsSync.put(stackId, stack);
392-
if (from != null) {
393-
stack.originalLocation = from;
393+
if (from != Long.MAX_VALUE) {
394+
stack.originalLocation = BlockPos.of(from);
394395
}
395396
return true;
396397
}

src/main/java/mekanism/common/content/transporter/PathfinderCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mekanism.common.content.transporter;
22

3+
import it.unimi.dsi.fastutil.longs.LongList;
34
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
45
import java.util.HashMap;
56
import java.util.List;
@@ -56,7 +57,7 @@ public static void reset() {
5657
cachedPaths.clear();
5758
}
5859

59-
public record CachedPath(List<BlockPos> path, double cost) {
60+
public record CachedPath(LongList path, double cost) {
6061
}
6162

6263
private record PathData(BlockPos startTransporter, BlockPos end, Direction endSide) {

src/main/java/mekanism/common/content/transporter/TransporterManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import mekanism.common.lib.inventory.TransitRequest.TransitResponse;
1111
import mekanism.common.util.InventoryUtils;
1212
import mekanism.common.util.StackUtils;
13+
import net.minecraft.core.BlockPos;
1314
import net.minecraft.core.Direction;
1415
import net.minecraft.core.GlobalPos;
1516
import net.minecraft.world.item.ItemStack;
@@ -21,19 +22,20 @@ public class TransporterManager {
2122
private TransporterManager() {
2223
}
2324

25+
//todo determine if a custom pos record is better storing the long
2426
private static final Map<GlobalPos, Set<TransporterStack>> flowingStacks = new Object2ObjectOpenHashMap<>();
2527

2628
public static void reset() {
2729
flowingStacks.clear();
2830
}
2931

3032
public static void add(Level world, TransporterStack stack) {
31-
flowingStacks.computeIfAbsent(GlobalPos.of(world.dimension(), stack.getDest()), k -> new HashSet<>()).add(stack);
33+
flowingStacks.computeIfAbsent(GlobalPos.of(world.dimension(), BlockPos.of(stack.getDest())), k -> new HashSet<>()).add(stack);
3234
}
3335

3436
public static void remove(Level world, TransporterStack stack) {
3537
if (stack.hasPath() && stack.getPathType().hasTarget()) {
36-
GlobalPos pos = GlobalPos.of(world.dimension(), stack.getDest());
38+
GlobalPos pos = GlobalPos.of(world.dimension(), BlockPos.of(stack.getDest()));
3739
Set<TransporterStack> transporterStacks = flowingStacks.get(pos);
3840
if (transporterStacks != null && transporterStacks.remove(stack) && transporterStacks.isEmpty()) {
3941
flowingStacks.remove(pos);

0 commit comments

Comments
 (0)