1189 lines
52 KiB
Diff
1189 lines
52 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: peaches94 <peachescu94@gmail.com>
|
|
Date: Sun, 26 Jun 2022 16:51:37 -0500
|
|
Subject: [PATCH] Async Pathfinding
|
|
|
|
This patch was ported downstream from the Petal fork.
|
|
|
|
Makes most pathfinding-related work happen asynchronously
|
|
|
|
diff --git a/src/main/java/dev/etil/mirai/MiraiConfig.java b/src/main/java/dev/etil/mirai/MiraiConfig.java
|
|
index 24e2c81fe0de5eb2e9a17554d81cfd14a36f90c4..8dffa2fadd615cfd59cc85630b4a1454c87e5aa9 100644
|
|
--- a/src/main/java/dev/etil/mirai/MiraiConfig.java
|
|
+++ b/src/main/java/dev/etil/mirai/MiraiConfig.java
|
|
@@ -263,4 +263,16 @@ public class MiraiConfig {
|
|
"pick up items on the ground.");
|
|
}
|
|
|
|
+ public static boolean enableAsyncPathfinding;
|
|
+ public static boolean enableAsyncPathfindingInitialized;
|
|
+ private static void asyncPathfinding() {
|
|
+ boolean temp = getBoolean("enable-async-pathfinding", true,
|
|
+ "Whether or not async pathfinding should be enabled.",
|
|
+ "You may encounter issues with water interactions.");
|
|
+ if (!enableAsyncPathfindingInitialized) {
|
|
+ enableAsyncPathfindingInitialized = true;
|
|
+ enableAsyncPathfinding = temp;
|
|
+ }
|
|
+ }
|
|
+
|
|
}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/dev/etil/mirai/path/AsyncPath.java b/src/main/java/dev/etil/mirai/path/AsyncPath.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..2ecf19f9623a4c6c99a7b97e950387e8b9c7757a
|
|
--- /dev/null
|
|
+++ b/src/main/java/dev/etil/mirai/path/AsyncPath.java
|
|
@@ -0,0 +1,282 @@
|
|
+package dev.etil.mirai.path;
|
|
+
|
|
+import net.minecraft.core.BlockPos;
|
|
+import net.minecraft.world.entity.Entity;
|
|
+import net.minecraft.world.level.pathfinder.Node;
|
|
+import net.minecraft.world.level.pathfinder.Path;
|
|
+import net.minecraft.world.phys.Vec3;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+
|
|
+import java.util.ArrayList;
|
|
+import java.util.List;
|
|
+import java.util.Set;
|
|
+import java.util.function.Supplier;
|
|
+
|
|
+/**
|
|
+ * i'll be using this to represent a path that not be processed yet!
|
|
+ */
|
|
+public class AsyncPath extends Path {
|
|
+
|
|
+ /**
|
|
+ * marks whether this async path has been processed
|
|
+ */
|
|
+ private volatile boolean processed = false;
|
|
+
|
|
+ /**
|
|
+ * runnables waiting for path to be processed
|
|
+ */
|
|
+ private final @NotNull List<Runnable> postProcessing = new ArrayList<>();
|
|
+
|
|
+ /**
|
|
+ * a list of positions that this path could path towards
|
|
+ */
|
|
+ private final Set<BlockPos> positions;
|
|
+
|
|
+ /**
|
|
+ * the supplier of the real processed path
|
|
+ */
|
|
+ private final Supplier<Path> pathSupplier;
|
|
+
|
|
+ /*
|
|
+ * Processed values
|
|
+ */
|
|
+
|
|
+ /**
|
|
+ * this is a reference to the nodes list in the parent `Path` object
|
|
+ */
|
|
+ private final List<Node> nodes;
|
|
+ /**
|
|
+ * the block we're trying to path to
|
|
+ *
|
|
+ * while processing, we have no idea where this is so consumers of `Path` should check that the path is processed before checking the target block
|
|
+ */
|
|
+ private @Nullable BlockPos target;
|
|
+ /**
|
|
+ * how far we are to the target
|
|
+ *
|
|
+ * while processing, the target could be anywhere but theoretically we're always "close" to a theoretical target so default is 0
|
|
+ */
|
|
+ private float distToTarget = 0;
|
|
+ /**
|
|
+ * whether we can reach the target
|
|
+ *
|
|
+ * while processing we can always theoretically reach the target so default is true
|
|
+ */
|
|
+ private boolean canReach = true;
|
|
+
|
|
+ public AsyncPath(@NotNull List<Node> emptyNodeList, @NotNull Set<BlockPos> positions, @NotNull Supplier<Path> pathSupplier) {
|
|
+ //noinspection ConstantConditions
|
|
+ super(emptyNodeList, null, false);
|
|
+
|
|
+ this.nodes = emptyNodeList;
|
|
+ this.positions = positions;
|
|
+ this.pathSupplier = pathSupplier;
|
|
+
|
|
+ AsyncPathProcessor.queue(this);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean isProcessed() {
|
|
+ return this.processed;
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * add a post-processing action
|
|
+ */
|
|
+ public synchronized void postProcessing(@NotNull Runnable runnable) {
|
|
+ if (processed) runnable.run();
|
|
+ else postProcessing.add(runnable);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * an easy way to check if this processing path is the same as an attempted new path
|
|
+ *
|
|
+ * @param positions - the positions to compare against
|
|
+ * @return true if we are processing the same positions
|
|
+ */
|
|
+ public boolean hasSameProcessingPositions(final Set<BlockPos> positions) {
|
|
+ if (this.positions.size() != positions.size()) {
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ return this.positions.containsAll(positions);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * starts processing this path
|
|
+ */
|
|
+ public synchronized void process() {
|
|
+ if (this.processed) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ final Path bestPath = this.pathSupplier.get();
|
|
+
|
|
+ this.nodes.addAll(bestPath.nodes); // we mutate this list to reuse the logic in Path
|
|
+ this.target = bestPath.getTarget();
|
|
+ this.distToTarget = bestPath.getDistToTarget();
|
|
+ this.canReach = bestPath.canReach();
|
|
+
|
|
+ this.processed = true;
|
|
+
|
|
+ this.postProcessing.forEach(Runnable::run);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * if this path is accessed while it hasn't processed, just process it in-place
|
|
+ */
|
|
+ private void checkProcessed() {
|
|
+ if (!this.processed) {
|
|
+ this.process();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * overrides we need for final fields that we cannot modify after processing
|
|
+ */
|
|
+
|
|
+ @Override
|
|
+ public @NotNull BlockPos getTarget() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return this.target;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public float getDistToTarget() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return this.distToTarget;
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean canReach() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return this.canReach;
|
|
+ }
|
|
+
|
|
+ /*
|
|
+ * overrides to ensure we're processed first
|
|
+ */
|
|
+
|
|
+ @Override
|
|
+ public boolean isDone() {
|
|
+ return this.isProcessed() && super.isDone();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void advance() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.advance();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean notStarted() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.notStarted();
|
|
+ }
|
|
+
|
|
+ @Nullable
|
|
+ @Override
|
|
+ public Node getEndNode() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getEndNode();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Node getNode(int index) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNode(index);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void truncateNodes(int length) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.truncateNodes(length);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void replaceNode(int index, Node node) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.replaceNode(index, node);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public int getNodeCount() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNodeCount();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public int getNextNodeIndex() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextNodeIndex();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public void setNextNodeIndex(int nodeIndex) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ super.setNextNodeIndex(nodeIndex);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Vec3 getEntityPosAtNode(Entity entity, int index) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getEntityPosAtNode(entity, index);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public BlockPos getNodePos(int index) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNodePos(index);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Vec3 getNextEntityPos(Entity entity) {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextEntityPos(entity);
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public BlockPos getNextNodePos() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextNodePos();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public Node getNextNode() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getNextNode();
|
|
+ }
|
|
+
|
|
+ @Nullable
|
|
+ @Override
|
|
+ public Node getPreviousNode() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.getPreviousNode();
|
|
+ }
|
|
+
|
|
+ @Override
|
|
+ public boolean hasNext() {
|
|
+ this.checkProcessed();
|
|
+
|
|
+ return super.hasNext();
|
|
+ }
|
|
+}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/dev/etil/mirai/path/AsyncPathProcessor.java b/src/main/java/dev/etil/mirai/path/AsyncPathProcessor.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..6dc8f7bb7ba6b78b5db50801a61abe526c17c939
|
|
--- /dev/null
|
|
+++ b/src/main/java/dev/etil/mirai/path/AsyncPathProcessor.java
|
|
@@ -0,0 +1,44 @@
|
|
+package dev.etil.mirai.path;
|
|
+
|
|
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
+import net.minecraft.server.MinecraftServer;
|
|
+import net.minecraft.world.level.pathfinder.Path;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+import org.jetbrains.annotations.Nullable;
|
|
+
|
|
+import java.util.concurrent.CompletableFuture;
|
|
+import java.util.concurrent.Executor;
|
|
+import java.util.concurrent.Executors;
|
|
+import java.util.function.Consumer;
|
|
+
|
|
+/**
|
|
+ * used to handle the scheduling of async path processing
|
|
+ */
|
|
+public class AsyncPathProcessor {
|
|
+
|
|
+ private static final Executor mainThreadExecutor = MinecraftServer.getServer();
|
|
+ private static final Executor pathProcessingExecutor = Executors.newCachedThreadPool(new ThreadFactoryBuilder()
|
|
+ .setNameFormat("mirai-path-processor-%d")
|
|
+ .setPriority(Thread.NORM_PRIORITY - 2)
|
|
+ .build());
|
|
+
|
|
+ protected static CompletableFuture<Void> queue(@NotNull AsyncPath path) {
|
|
+ return CompletableFuture.runAsync(path::process, pathProcessingExecutor);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * takes a possibly unprocessed path, and waits until it is completed
|
|
+ * the consumer will be immediately invoked if the path is already processed
|
|
+ * the consumer will always be called on the main thread
|
|
+ *
|
|
+ * @param path a path to wait on
|
|
+ * @param afterProcessing a consumer to be called
|
|
+ */
|
|
+ public static void awaitProcessing(@Nullable Path path, Consumer<@Nullable Path> afterProcessing) {
|
|
+ if (path != null && !path.isProcessed() && path instanceof AsyncPath asyncPath) {
|
|
+ asyncPath.postProcessing(() -> mainThreadExecutor.execute(() -> afterProcessing.accept(path)));
|
|
+ } else {
|
|
+ afterProcessing.accept(path);
|
|
+ }
|
|
+ }
|
|
+}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/dev/etil/mirai/path/NodeEvaluatorCache.java b/src/main/java/dev/etil/mirai/path/NodeEvaluatorCache.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..310fa2f0f6869fbab8a6fe7356fbf0de9066b5cf
|
|
--- /dev/null
|
|
+++ b/src/main/java/dev/etil/mirai/path/NodeEvaluatorCache.java
|
|
@@ -0,0 +1,43 @@
|
|
+package dev.etil.mirai.path;
|
|
+
|
|
+import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
|
+import org.apache.commons.lang.Validate;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+import java.util.Map;
|
|
+import java.util.Queue;
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
+import java.util.concurrent.ConcurrentLinkedQueue;
|
|
+
|
|
+public class NodeEvaluatorCache {
|
|
+ private static final Map<NodeEvaluatorGenerator, ConcurrentLinkedQueue<NodeEvaluator>> threadLocalNodeEvaluators = new ConcurrentHashMap<>();
|
|
+ private static final Map<NodeEvaluator, NodeEvaluatorGenerator> nodeEvaluatorToGenerator = new ConcurrentHashMap<>();
|
|
+
|
|
+ private static @NotNull Queue<NodeEvaluator> getDequeForGenerator(@NotNull NodeEvaluatorGenerator generator) {
|
|
+ return threadLocalNodeEvaluators.computeIfAbsent(generator, (key) -> new ConcurrentLinkedQueue<>());
|
|
+ }
|
|
+
|
|
+ public static @NotNull NodeEvaluator takeNodeEvaluator(@NotNull NodeEvaluatorGenerator generator) {
|
|
+ var nodeEvaluator = getDequeForGenerator(generator).poll();
|
|
+
|
|
+ if (nodeEvaluator == null) {
|
|
+ nodeEvaluator = generator.generate();
|
|
+ }
|
|
+
|
|
+ nodeEvaluatorToGenerator.put(nodeEvaluator, generator);
|
|
+
|
|
+ return nodeEvaluator;
|
|
+ }
|
|
+
|
|
+ public static void returnNodeEvaluator(@NotNull NodeEvaluator nodeEvaluator) {
|
|
+ final var generator = nodeEvaluatorToGenerator.remove(nodeEvaluator);
|
|
+ Validate.notNull(generator, "NodeEvaluator already returned");
|
|
+
|
|
+ getDequeForGenerator(generator).offer(nodeEvaluator);
|
|
+ }
|
|
+
|
|
+ public static void removeNodeEvaluator(@NotNull NodeEvaluator nodeEvaluator) {
|
|
+ nodeEvaluatorToGenerator.remove(nodeEvaluator);
|
|
+ }
|
|
+
|
|
+}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/dev/etil/mirai/path/NodeEvaluatorGenerator.java b/src/main/java/dev/etil/mirai/path/NodeEvaluatorGenerator.java
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..763bd0fec45fff471dea3186d3e785d72a973cbb
|
|
--- /dev/null
|
|
+++ b/src/main/java/dev/etil/mirai/path/NodeEvaluatorGenerator.java
|
|
@@ -0,0 +1,10 @@
|
|
+package dev.etil.mirai.path;
|
|
+
|
|
+import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
|
+import org.jetbrains.annotations.NotNull;
|
|
+
|
|
+public interface NodeEvaluatorGenerator {
|
|
+
|
|
+ @NotNull NodeEvaluator generate();
|
|
+
|
|
+}
|
|
\ No newline at end of file
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java b/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
|
|
index bf3b8ccb3e031e0ad24cd51e28ea8cbd4f8a8030..8efe9dfe75e1dfc430cd011f9dbeb3d1a2bd0145 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/AcquirePoi.java
|
|
@@ -93,28 +93,61 @@ public class AcquirePoi extends Behavior<PathfinderMob> {
|
|
io.papermc.paper.util.PoiAccess.findNearestPoiPositions(poiManager, this.poiType, predicate, entity.blockPosition(), 48, 48*48, PoiManager.Occupancy.HAS_SPACE, false, 5, poiposes);
|
|
Set<Pair<Holder<PoiType>, BlockPos>> set = new java.util.HashSet<>(poiposes);
|
|
// Paper end - optimise POI access
|
|
- Path path = findPathToPois(entity, set);
|
|
- if (path != null && path.canReach()) {
|
|
- BlockPos blockPos = path.getTarget();
|
|
- poiManager.getType(blockPos).ifPresent((holder) -> {
|
|
- poiManager.take(this.poiType, (holderx, blockPos2) -> {
|
|
- return blockPos2.equals(blockPos);
|
|
- }, blockPos, 1);
|
|
- entity.getBrain().setMemory(this.memoryToAcquire, GlobalPos.of(world.dimension(), blockPos));
|
|
- this.onPoiAcquisitionEvent.ifPresent((byte_) -> {
|
|
- world.broadcastEntityEvent(entity, byte_);
|
|
+ // Mirai start - await on path async
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ Path possiblePath = findPathToPois(entity, set);
|
|
+
|
|
+ // Mirai - wait on the path to be processed
|
|
+ dev.etil.mirai.path.AsyncPathProcessor.awaitProcessing(possiblePath, path -> {
|
|
+ // Mirai - readd canReach check
|
|
+ if (path == null || !path.canReach()) {
|
|
+ for(Pair<Holder<PoiType>, BlockPos> pair : set) {
|
|
+ this.batchCache.computeIfAbsent(pair.getSecond().asLong(), (m) -> {
|
|
+ return new AcquirePoi.JitteredLinearRetry(entity.level.random, time);
|
|
+ });
|
|
+ }
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ poiManager.getType(blockPos).ifPresent((holder) -> {
|
|
+ poiManager.take(this.poiType, (holderx, blockPos2) -> {
|
|
+ return blockPos2.equals(blockPos);
|
|
+ }, blockPos, 1);
|
|
+ entity.getBrain().setMemory(this.memoryToAcquire, GlobalPos.of(world.dimension(), blockPos));
|
|
+ this.onPoiAcquisitionEvent.ifPresent((byte_) -> {
|
|
+ world.broadcastEntityEvent(entity, byte_);
|
|
+ });
|
|
+ this.batchCache.clear();
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
});
|
|
- this.batchCache.clear();
|
|
- DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
});
|
|
} else {
|
|
- for(Pair<Holder<PoiType>, BlockPos> pair : set) {
|
|
- this.batchCache.computeIfAbsent(pair.getSecond().asLong(), (m) -> {
|
|
- return new AcquirePoi.JitteredLinearRetry(entity.level.random, time);
|
|
+ Path path = findPathToPois(entity, set);
|
|
+ if (path != null && path.canReach()) {
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ poiManager.getType(blockPos).ifPresent((holder) -> {
|
|
+ poiManager.take(this.poiType, (holderx, blockPos2) -> {
|
|
+ return blockPos2.equals(blockPos);
|
|
+ }, blockPos, 1);
|
|
+ entity.getBrain().setMemory(this.memoryToAcquire, GlobalPos.of(world.dimension(), blockPos));
|
|
+ this.onPoiAcquisitionEvent.ifPresent((byte_) -> {
|
|
+ world.broadcastEntityEvent(entity, byte_);
|
|
+ });
|
|
+ this.batchCache.clear();
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
});
|
|
+ } else {
|
|
+ for(Pair<Holder<PoiType>, BlockPos> pair : set) {
|
|
+ this.batchCache.computeIfAbsent(pair.getSecond().asLong(), (m) -> {
|
|
+ return new AcquirePoi.JitteredLinearRetry(entity.level.random, time);
|
|
+ });
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ // Mirai end
|
|
+
|
|
}
|
|
|
|
@Nullable
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java b/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
|
|
index 18364ce4c60172529b10bc9e3a813dcedc4b766f..fd2abf5a865518b12d2a64d52596015aede1177e 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/MoveToTargetSink.java
|
|
@@ -21,6 +21,7 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
private int remainingCooldown;
|
|
@Nullable
|
|
private Path path;
|
|
+ private boolean finishedProcessing; // Mirai
|
|
@Nullable
|
|
private BlockPos lastTargetPos;
|
|
private float speedModifier;
|
|
@@ -42,9 +43,10 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
Brain<?> brain = entity.getBrain();
|
|
WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
|
boolean bl = this.reachedTarget(entity, walkTarget);
|
|
- if (!bl && this.tryComputePath(entity, walkTarget, world.getGameTime())) {
|
|
+ if (!bl && (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && this.tryComputePath(entity, walkTarget, world.getGameTime()))) { // Mirai
|
|
this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
|
return true;
|
|
+ } else if (!bl) { return true; // Mirai
|
|
} else {
|
|
brain.eraseMemory(MemoryModuleType.WALK_TARGET);
|
|
if (bl) {
|
|
@@ -58,6 +60,7 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
|
|
@Override
|
|
protected boolean canStillUse(ServerLevel serverLevel, Mob mob, long l) {
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && !finishedProcessing) return true; // Mirai - wait for path to process
|
|
if (this.path != null && this.lastTargetPos != null) {
|
|
Optional<WalkTarget> optional = mob.getBrain().getMemory(MemoryModuleType.WALK_TARGET);
|
|
PathNavigation pathNavigation = mob.getNavigation();
|
|
@@ -81,28 +84,96 @@ public class MoveToTargetSink extends Behavior<Mob> {
|
|
|
|
@Override
|
|
protected void start(ServerLevel serverLevel, Mob mob, long l) {
|
|
+ if (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) { // Mirai
|
|
mob.getBrain().setMemory(MemoryModuleType.PATH, this.path);
|
|
mob.getNavigation().moveTo(this.path, (double)this.speedModifier);
|
|
+ // Mirai start
|
|
+ } else {
|
|
+ Brain<?> brain = mob.getBrain();
|
|
+ WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
|
+
|
|
+ this.finishedProcessing = false;
|
|
+ this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
|
+ this.path = this.computePath(mob, walkTarget);
|
|
+ }
|
|
+ // Mirai end
|
|
}
|
|
|
|
@Override
|
|
protected void tick(ServerLevel world, Mob entity, long time) {
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && this.path != null && !this.path.isProcessed()) return; // Mirai - wait for processing
|
|
+
|
|
+ // Mirai start
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && !finishedProcessing) {
|
|
+ this.finishedProcessing = true;
|
|
+ Brain<?> brain = entity.getBrain();
|
|
+ boolean canReach = this.path != null && this.path.canReach();
|
|
+ if (canReach) {
|
|
+ brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
|
+ } else if (brain.hasMemoryValue(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE)) {
|
|
+ brain.setMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE, time);
|
|
+ }
|
|
+
|
|
+ if (!canReach) {
|
|
+ Optional<WalkTarget> walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET);
|
|
+
|
|
+ if (walkTarget.isPresent()) {
|
|
+ BlockPos blockPos = walkTarget.get().getTarget().currentBlockPosition();
|
|
+ Vec3 vec3 = DefaultRandomPos.getPosTowards((PathfinderMob)entity, 10, 7, Vec3.atBottomCenterOf(blockPos), (double)((float)Math.PI / 2F));
|
|
+ if (vec3 != null) {
|
|
+ // try recalculating the path using a random position
|
|
+ this.path = entity.getNavigation().createPath(vec3.x, vec3.y, vec3.z, 0);
|
|
+ this.finishedProcessing = false;
|
|
+ return;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ brain.eraseMemory(MemoryModuleType.WALK_TARGET);
|
|
+ this.path = null;
|
|
+
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ entity.getBrain().setMemory(MemoryModuleType.PATH, this.path);
|
|
+ entity.getNavigation().moveTo(this.path, (double)this.speedModifier);
|
|
+ }
|
|
+ // Mirai end
|
|
+
|
|
Path path = entity.getNavigation().getPath();
|
|
Brain<?> brain = entity.getBrain();
|
|
- if (this.path != path) {
|
|
+ if (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && this.path != path) { // Mirai
|
|
this.path = path;
|
|
brain.setMemory(MemoryModuleType.PATH, path);
|
|
}
|
|
|
|
- if (path != null && this.lastTargetPos != null) {
|
|
+ if (path != null && this.lastTargetPos != null && (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding || brain.hasMemoryValue(MemoryModuleType.WALK_TARGET))) { // Mirai
|
|
WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
|
+ if (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) { // Mirai
|
|
if (walkTarget.getTarget().currentBlockPosition().distSqr(this.lastTargetPos) > 4.0D && this.tryComputePath(entity, walkTarget, world.getGameTime())) {
|
|
this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
|
this.start(world, entity, time);
|
|
}
|
|
+ // Mirai start
|
|
+ } else {
|
|
+ if (walkTarget.getTarget().currentBlockPosition().distSqr(this.lastTargetPos) > 4.0D) this.start(world, entity, time);
|
|
+ }
|
|
+ // Mirai end
|
|
+
|
|
+ }
|
|
+ }
|
|
|
|
+ // Mirai start
|
|
+ private Path computePath(Mob entity, WalkTarget walkTarget) {
|
|
+ BlockPos blockPos = walkTarget.getTarget().currentBlockPosition();
|
|
+ this.speedModifier = walkTarget.getSpeedModifier();
|
|
+ Brain<?> brain = entity.getBrain();
|
|
+ if (this.reachedTarget(entity, walkTarget)) {
|
|
+ brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
|
}
|
|
+
|
|
+ return entity.getNavigation().createPath(blockPos, 0);
|
|
}
|
|
+ // Mirai end
|
|
|
|
private boolean tryComputePath(Mob entity, WalkTarget walkTarget, long time) {
|
|
BlockPos blockPos = walkTarget.getTarget().currentBlockPosition();
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java b/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java
|
|
index 9bd6d4f7b86daaaa9cfbad454dde06b797e3f667..9aca3f84b60273a0eb0ab657ff8403f21b46d4d5 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/behavior/SetClosestHomeAsWalkTarget.java
|
|
@@ -71,19 +71,41 @@ public class SetClosestHomeAsWalkTarget extends Behavior<LivingEntity> {
|
|
Set<Pair<Holder<PoiType>, BlockPos>> set = poiManager.findAllWithType((poiType) -> {
|
|
return poiType.is(PoiTypes.HOME);
|
|
}, predicate, entity.blockPosition(), 48, PoiManager.Occupancy.ANY).collect(Collectors.toSet());
|
|
- Path path = AcquirePoi.findPathToPois(pathfinderMob, set);
|
|
- if (path != null && path.canReach()) {
|
|
- BlockPos blockPos = path.getTarget();
|
|
- Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
- if (optional.isPresent()) {
|
|
- entity.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(blockPos, this.speedModifier, 1));
|
|
- DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
- }
|
|
- } else if (this.triedCount < 5) {
|
|
- this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
- return entry.getLongValue() < this.lastUpdate;
|
|
+ // Mirai start - await on path async
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ Path possiblePath = AcquirePoi.findPathToPois(pathfinderMob, set);
|
|
+
|
|
+ // Mirai - wait on the path to be processed
|
|
+ dev.etil.mirai.path.AsyncPathProcessor.awaitProcessing(possiblePath, path -> {
|
|
+ if (path == null || !path.canReach() || this.triedCount < 5) { // Mirai - readd canReach check
|
|
+ this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
+ return entry.getLongValue() < this.lastUpdate;
|
|
+ });
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
+ if (optional.isPresent()) {
|
|
+ entity.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(blockPos, this.speedModifier, 1));
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
+ }
|
|
});
|
|
+ } else {
|
|
+ Path path = AcquirePoi.findPathToPois(pathfinderMob, set);
|
|
+ if (path != null && path.canReach()) {
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
+ if (optional.isPresent()) {
|
|
+ entity.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(blockPos, this.speedModifier, 1));
|
|
+ DebugPackets.sendPoiTicketCountPacket(world, blockPos);
|
|
+ }
|
|
+ } else if (this.triedCount < 5) {
|
|
+ this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
+ return entry.getLongValue() < this.lastUpdate;
|
|
+ });
|
|
+ }
|
|
}
|
|
-
|
|
+ // Mirai end
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java
|
|
index 29a872393f2f995b13b4ed26b42c6464ab27ca73..b3dd15d9785e01a467eccabdc9d925e5305df414 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/AmphibiousPathNavigation.java
|
|
@@ -8,6 +8,14 @@ import net.minecraft.world.level.pathfinder.PathFinder;
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
public class AmphibiousPathNavigation extends PathNavigation {
|
|
+ // Mirai start
|
|
+ private static final dev.etil.mirai.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = () -> {
|
|
+ var nodeEvaluator = new AmphibiousNodeEvaluator(false);
|
|
+ nodeEvaluator.setCanPassDoors(true);
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Mirai end
|
|
+
|
|
public AmphibiousPathNavigation(Mob mob, Level world) {
|
|
super(mob, world);
|
|
}
|
|
@@ -16,7 +24,13 @@ public class AmphibiousPathNavigation extends PathNavigation {
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new AmphibiousNodeEvaluator(false);
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
- return new PathFinder(this.nodeEvaluator, range);
|
|
+ // Mirai start
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator);
|
|
+ } else {
|
|
+ return new PathFinder(this.nodeEvaluator, range);
|
|
+ }
|
|
+ // Mirai end
|
|
}
|
|
|
|
@Override
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
|
|
index 27cd393e81f6ef9b5690c051624d8d2af50acd34..706ee2db6422e2fdb11b88d8c22eff972dbd0d94 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/FlyingPathNavigation.java
|
|
@@ -12,6 +12,15 @@ import net.minecraft.world.level.pathfinder.PathFinder;
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
public class FlyingPathNavigation extends PathNavigation {
|
|
+
|
|
+ // Mirai start
|
|
+ private static final dev.etil.mirai.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = () -> {
|
|
+ var nodeEvaluator = new FlyNodeEvaluator();
|
|
+ nodeEvaluator.setCanPassDoors(true);
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Mirai end
|
|
+
|
|
public FlyingPathNavigation(Mob entity, Level world) {
|
|
super(entity, world);
|
|
}
|
|
@@ -20,7 +29,13 @@ public class FlyingPathNavigation extends PathNavigation {
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new FlyNodeEvaluator();
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
- return new PathFinder(this.nodeEvaluator, range);
|
|
+ // Mirai start
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator);
|
|
+ } else {
|
|
+ return new PathFinder(this.nodeEvaluator, range);
|
|
+ }
|
|
+ // Mirai end
|
|
}
|
|
|
|
@Override
|
|
@@ -45,9 +60,11 @@ public class FlyingPathNavigation extends PathNavigation {
|
|
this.recomputePath();
|
|
}
|
|
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && this.path != null && !this.path.isProcessed()) return; // Mirai
|
|
+
|
|
if (!this.isDone()) {
|
|
if (this.canUpdatePath()) {
|
|
- this.followThePath();
|
|
+ this.followThePathSuper(); // Mirai
|
|
} else if (this.path != null && !this.path.isDone()) {
|
|
Vec3 vec3 = this.path.getNextEntityPos(this.mob);
|
|
if (this.mob.getBlockX() == Mth.floor(vec3.x) && this.mob.getBlockY() == Mth.floor(vec3.y) && this.mob.getBlockZ() == Mth.floor(vec3.z)) {
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java
|
|
index f0248d839255763005ba333b0bfcf691407fb69b..dd627d720320b09ac909a19646f50435aa1880ee 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/GroundPathNavigation.java
|
|
@@ -15,6 +15,15 @@ import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
public class GroundPathNavigation extends PathNavigation {
|
|
+
|
|
+ // Mirai start
|
|
+ private static final dev.etil.mirai.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = () -> {
|
|
+ var nodeEvaluator = new WalkNodeEvaluator();
|
|
+ nodeEvaluator.setCanPassDoors(true);
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Mirai end
|
|
+
|
|
private boolean avoidSun;
|
|
|
|
public GroundPathNavigation(Mob entity, Level world) {
|
|
@@ -25,7 +34,13 @@ public class GroundPathNavigation extends PathNavigation {
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new WalkNodeEvaluator();
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
- return new PathFinder(this.nodeEvaluator, range);
|
|
+ // Mirai start
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator);
|
|
+ } else {
|
|
+ return new PathFinder(this.nodeEvaluator, range);
|
|
+ }
|
|
+ // Mirai end
|
|
}
|
|
|
|
@Override
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java b/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
|
|
index c1781c92ff59f0c9eb47cbbef01e3252c5e1a1bf..3803d5b79b4bb99d1a87c1af4d7e20ae4128a416 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/navigation/PathNavigation.java
|
|
@@ -150,6 +150,9 @@ public abstract class PathNavigation {
|
|
return null;
|
|
} else if (!this.canUpdatePath()) {
|
|
return null;
|
|
+ } else if (this.path instanceof dev.etil.mirai.path.AsyncPath asyncPath && !asyncPath.isProcessed() && asyncPath.hasSameProcessingPositions(positions)) { // Mirai start - catch early if it's still processing these positions let it keep processing
|
|
+ return this.path;
|
|
+ // Mirai end
|
|
} else if (this.path != null && !this.path.isDone() && positions.contains(this.targetPos)) {
|
|
return this.path;
|
|
} else {
|
|
@@ -176,11 +179,28 @@ public abstract class PathNavigation {
|
|
PathNavigationRegion pathNavigationRegion = new PathNavigationRegion(this.level, blockPos.offset(-i, -i, -i), blockPos.offset(i, i, i));
|
|
Path path = this.pathFinder.findPath(pathNavigationRegion, this.mob, positions, followRange, distance, this.maxVisitedNodesMultiplier);
|
|
this.level.getProfiler().pop();
|
|
- if (path != null && path.getTarget() != null) {
|
|
+
|
|
+ // Mirai start
|
|
+ if (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ if (path != null && path.getTarget() != null) {
|
|
this.targetPos = path.getTarget();
|
|
this.reachRange = distance;
|
|
this.resetStuckTimeout();
|
|
+ }
|
|
+ } else {
|
|
+ if (!positions.isEmpty()) this.targetPos = positions.iterator().next(); // Mirai - assign early a target position. most calls will only have 1 position
|
|
+
|
|
+ dev.etil.mirai.path.AsyncPathProcessor.awaitProcessing(path, processedPath -> {
|
|
+ if (processedPath != this.path) return; // Mirai - check that processing didn't take so long that we calculated a new path
|
|
+
|
|
+ if (processedPath != null && processedPath.getTarget() != null) {
|
|
+ this.targetPos = processedPath.getTarget();
|
|
+ this.reachRange = distance;
|
|
+ this.resetStuckTimeout();
|
|
+ }
|
|
+ });
|
|
}
|
|
+ // Mirai end
|
|
|
|
return path;
|
|
}
|
|
@@ -227,8 +247,8 @@ public abstract class PathNavigation {
|
|
if (this.isDone()) {
|
|
return false;
|
|
} else {
|
|
- this.trimPath();
|
|
- if (this.path.getNodeCount() <= 0) {
|
|
+ if (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding || path.isProcessed()) this.trimPath(); // Mirai - only trim if processed
|
|
+ if ((!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding || path.isProcessed()) && this.path.getNodeCount() <= 0) { // Mirai - only check node count if processed
|
|
return false;
|
|
} else {
|
|
this.speedModifier = speed;
|
|
@@ -252,9 +272,11 @@ public abstract class PathNavigation {
|
|
this.recomputePath();
|
|
}
|
|
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && this.path != null && !this.path.isProcessed()) return; // Mirai - skip pathfinding if we're still processing
|
|
+
|
|
if (!this.isDone()) {
|
|
if (this.canUpdatePath()) {
|
|
- this.followThePath();
|
|
+ this.followThePathSuper(); // Mirai
|
|
} else if (this.path != null && !this.path.isDone()) {
|
|
Vec3 vec3 = this.getTempMobPos();
|
|
Vec3 vec32 = this.path.getNextEntityPos(this.mob);
|
|
@@ -276,6 +298,13 @@ public abstract class PathNavigation {
|
|
return this.level.getBlockState(blockPos.below()).isAir() ? pos.y : WalkNodeEvaluator.getFloorLevel(this.level, blockPos);
|
|
}
|
|
|
|
+ // Mirai start - this fixes plugin compat by ensuring the isProcessed check is completed properly.
|
|
+ protected final void followThePathSuper() {
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding && !this.path.isProcessed()) return; // Mirai
|
|
+ followThePath();
|
|
+ }
|
|
+ // Mirai end
|
|
+
|
|
protected void followThePath() {
|
|
Vec3 vec3 = this.getTempMobPos();
|
|
this.maxDistanceToWaypoint = this.mob.getBbWidth() > 0.75F ? this.mob.getBbWidth() / 2.0F : 0.75F - this.mob.getBbWidth() / 2.0F;
|
|
@@ -419,7 +448,7 @@ public abstract class PathNavigation {
|
|
public boolean shouldRecomputePath(BlockPos pos) {
|
|
if (this.hasDelayedRecomputation) {
|
|
return false;
|
|
- } else if (this.path != null && !this.path.isDone() && this.path.getNodeCount() != 0) {
|
|
+ } else if (this.path != null && this.path.isProcessed() && !this.path.isDone() && this.path.getNodeCount() != 0) { // Mirai
|
|
Node node = this.path.getEndNode();
|
|
Vec3 vec3 = new Vec3(((double)node.x + this.mob.getX()) / 2.0D, ((double)node.y + this.mob.getY()) / 2.0D, ((double)node.z + this.mob.getZ()) / 2.0D);
|
|
return pos.closerToCenterThan(vec3, (double)(this.path.getNodeCount() - this.path.getNextNodeIndex()));
|
|
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
|
|
index 8db20db72cd51046213625fac46c35854c59ec5d..39b95ecb2b9dd313d2e58ee98349af0d665e7f01 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestBedSensor.java
|
|
@@ -57,20 +57,42 @@ public class NearestBedSensor extends Sensor<Mob> {
|
|
java.util.List<Pair<Holder<PoiType>, BlockPos>> poiposes = new java.util.ArrayList<>();
|
|
// don't ask me why it's unbounded. ask mojang.
|
|
io.papermc.paper.util.PoiAccess.findAnyPoiPositions(poiManager, type -> type.is(PoiTypes.HOME), predicate, entity.blockPosition(), 48, PoiManager.Occupancy.ANY, false, Integer.MAX_VALUE, poiposes);
|
|
- Path path = AcquirePoi.findPathToPois(entity, new java.util.HashSet<>(poiposes));
|
|
- // Paper end - optimise POI access
|
|
- if (path != null && path.canReach()) {
|
|
- BlockPos blockPos = path.getTarget();
|
|
- Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
- if (optional.isPresent()) {
|
|
- entity.getBrain().setMemory(MemoryModuleType.NEAREST_BED, blockPos);
|
|
- }
|
|
- } else if (this.triedCount < 5) {
|
|
- this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
- return entry.getLongValue() < this.lastUpdate;
|
|
+
|
|
+ // Mirai start - await on path async
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ Path possiblePath = AcquirePoi.findPathToPois(entity, new java.util.HashSet<>(poiposes));
|
|
+ // Paper end - optimise POI access
|
|
+ // Mirai - wait on the path to be processed
|
|
+ dev.etil.mirai.path.AsyncPathProcessor.awaitProcessing(possiblePath, path -> {
|
|
+ // Mirai - readd canReach check
|
|
+ if (path == null || !path.canReach()) {
|
|
+ this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
+ return entry.getLongValue() < this.lastUpdate;
|
|
+ });
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
+ if (optional.isPresent()) {
|
|
+ entity.getBrain().setMemory(MemoryModuleType.NEAREST_BED, blockPos);
|
|
+ }
|
|
});
|
|
+ } else {
|
|
+ Path path = AcquirePoi.findPathToPois(entity, new java.util.HashSet<>(poiposes));
|
|
+ if (path != null && path.canReach()) {
|
|
+ BlockPos blockPos = path.getTarget();
|
|
+ Optional<Holder<PoiType>> optional = poiManager.getType(blockPos);
|
|
+ if (optional.isPresent()) {
|
|
+ entity.getBrain().setMemory(MemoryModuleType.NEAREST_BED, blockPos);
|
|
+ }
|
|
+ } else if (this.triedCount < 5) {
|
|
+ this.batchCache.long2LongEntrySet().removeIf((entry) -> {
|
|
+ return entry.getLongValue() < this.lastUpdate;
|
|
+ });
|
|
+ }
|
|
}
|
|
-
|
|
+ // Mirai end
|
|
}
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/animal/Bee.java b/src/main/java/net/minecraft/world/entity/animal/Bee.java
|
|
index 2e05c953182c27e3571b2c33eceeb379e60b54be..511359bfae80dda5a549d3dfd58bcf7adf2738c5 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/animal/Bee.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/animal/Bee.java
|
|
@@ -1071,7 +1071,7 @@ public class Bee extends Animal implements NeutralMob, FlyingAnimal {
|
|
} else {
|
|
Bee.this.pathfindRandomlyTowards(Bee.this.hivePos);
|
|
}
|
|
- } else {
|
|
+ } else if (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding || (navigation.getPath() != null && navigation.getPath().isProcessed())) { // Mirai - check processing
|
|
boolean flag = this.pathfindDirectlyTowards(Bee.this.hivePos);
|
|
|
|
if (!flag) {
|
|
@@ -1133,7 +1133,7 @@ public class Bee extends Animal implements NeutralMob, FlyingAnimal {
|
|
} else {
|
|
Path pathentity = Bee.this.navigation.getPath();
|
|
|
|
- return pathentity != null && pathentity.getTarget().equals(pos) && pathentity.canReach() && pathentity.isDone();
|
|
+ return pathentity != null && (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding || pathentity.isProcessed()) && pathentity.getTarget().equals(pos) && pathentity.canReach() && pathentity.isDone(); // Mirai - ensure path is processed
|
|
}
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java b/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
|
|
index 74cb2741a5cd66bdac3a22de938ae82968705a56..df909d9df9f2d20be41497377c3ba9649cda24d5 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
|
|
@@ -413,6 +413,14 @@ public class Frog extends Animal {
|
|
}
|
|
|
|
static class FrogPathNavigation extends AmphibiousPathNavigation {
|
|
+ // Mirai start
|
|
+ private static final dev.etil.mirai.path.NodeEvaluatorGenerator nodeEvaluatorGenerator = () -> {
|
|
+ var nodeEvaluator = new Frog.FrogNodeEvaluator(true);
|
|
+ nodeEvaluator.setCanPassDoors(true);
|
|
+ return nodeEvaluator;
|
|
+ };
|
|
+ // Mirai end
|
|
+
|
|
FrogPathNavigation(Frog frog, Level world) {
|
|
super(frog, world);
|
|
}
|
|
@@ -421,7 +429,13 @@ public class Frog extends Animal {
|
|
protected PathFinder createPathFinder(int range) {
|
|
this.nodeEvaluator = new Frog.FrogNodeEvaluator(true);
|
|
this.nodeEvaluator.setCanPassDoors(true);
|
|
- return new PathFinder(this.nodeEvaluator, range);
|
|
+ // Mirai start
|
|
+ if (dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) {
|
|
+ return new PathFinder(this.nodeEvaluator, range, nodeEvaluatorGenerator);
|
|
+ } else {
|
|
+ return new PathFinder(this.nodeEvaluator, range);
|
|
+ }
|
|
+ // Mirai end
|
|
}
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/world/entity/monster/Drowned.java b/src/main/java/net/minecraft/world/entity/monster/Drowned.java
|
|
index 1b1305f5eaf5710b72c57ab4c3953e703a23f1e0..2b2afdaf062fa66fc9ee79efaee4ed47abb026cb 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/monster/Drowned.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/monster/Drowned.java
|
|
@@ -222,7 +222,7 @@ public class Drowned extends Zombie implements RangedAttackMob {
|
|
protected boolean closeToNextPos() {
|
|
Path pathentity = this.getNavigation().getPath();
|
|
|
|
- if (pathentity != null) {
|
|
+ if (pathentity != null && (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding || pathentity.isProcessed())) { // Mirai - ensure path is processed
|
|
BlockPos blockposition = pathentity.getTarget();
|
|
|
|
if (blockposition != null) {
|
|
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/Path.java b/src/main/java/net/minecraft/world/level/pathfinder/Path.java
|
|
index 2a335f277bd0e4b8ad0f60d8226eb8aaa80a871f..36e448c36f4d4cf2acb89d5a378d883079803dd8 100644
|
|
--- a/src/main/java/net/minecraft/world/level/pathfinder/Path.java
|
|
+++ b/src/main/java/net/minecraft/world/level/pathfinder/Path.java
|
|
@@ -30,6 +30,17 @@ public class Path {
|
|
this.reached = reachesTarget;
|
|
}
|
|
|
|
+ // Mirai start
|
|
+ /**
|
|
+ * checks if the path is completely processed in the case of it being computed async
|
|
+ *
|
|
+ * @return true if the path is processed
|
|
+ */
|
|
+ public boolean isProcessed() {
|
|
+ return true;
|
|
+ }
|
|
+ // Mirai end
|
|
+
|
|
public void advance() {
|
|
++this.nextNodeIndex;
|
|
}
|
|
@@ -104,6 +115,8 @@ public class Path {
|
|
}
|
|
|
|
public boolean sameAs(@Nullable Path o) {
|
|
+ if (o == this) return true; // Mirai - short circuit
|
|
+
|
|
if (o == null) {
|
|
return false;
|
|
} else if (o.nodes.size() != this.nodes.size()) {
|
|
diff --git a/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java b/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java
|
|
index d23481453717f715124156b5d83f6448f720d049..b0f2314adc69a573f0959dceac755207f694eff8 100644
|
|
--- a/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java
|
|
+++ b/src/main/java/net/minecraft/world/level/pathfinder/PathFinder.java
|
|
@@ -25,36 +25,75 @@ public class PathFinder {
|
|
private static final boolean DEBUG = false;
|
|
private final BinaryHeap openSet = new BinaryHeap();
|
|
|
|
- public PathFinder(NodeEvaluator pathNodeMaker, int range) {
|
|
+ private final @Nullable dev.etil.mirai.path.NodeEvaluatorGenerator nodeEvaluatorGenerator; // Mirai - we use this later to generate an evaluator
|
|
+
|
|
+ // Mirai start - add nodeEvaluatorGenerator as optional param
|
|
+ public PathFinder(NodeEvaluator pathNodeMaker, int range, @Nullable dev.etil.mirai.path.NodeEvaluatorGenerator nodeEvaluatorGenerator) {
|
|
this.nodeEvaluator = pathNodeMaker;
|
|
this.maxVisitedNodes = range;
|
|
+ this.nodeEvaluatorGenerator = nodeEvaluatorGenerator;
|
|
+ }
|
|
+
|
|
+ public PathFinder(NodeEvaluator pathNodeMaker, int range) {
|
|
+ this(pathNodeMaker, range, null);
|
|
}
|
|
+ // Mirai end
|
|
|
|
@Nullable
|
|
public Path findPath(PathNavigationRegion world, Mob mob, Set<BlockPos> positions, float followRange, int distance, float rangeMultiplier) {
|
|
- this.openSet.clear();
|
|
- this.nodeEvaluator.prepare(world, mob);
|
|
- Node node = this.nodeEvaluator.getStart();
|
|
+ if (!dev.etil.mirai.MiraiConfig.enableAsyncPathfinding) this.openSet.clear(); // Mirai - it's always cleared in processPath
|
|
+ // Mirai start - use a generated evaluator if we have one otherwise run sync
|
|
+ var nodeEvaluator = this.nodeEvaluatorGenerator == null ? this.nodeEvaluator : dev.etil.mirai.path.NodeEvaluatorCache.takeNodeEvaluator(this.nodeEvaluatorGenerator);
|
|
+ nodeEvaluator.prepare(world, mob);
|
|
+ Node node = nodeEvaluator.getStart();
|
|
if (node == null) {
|
|
+ dev.etil.mirai.path.NodeEvaluatorCache.removeNodeEvaluator(nodeEvaluator);
|
|
return null;
|
|
} else {
|
|
// Paper start - remove streams - and optimize collection
|
|
List<Map.Entry<Target, BlockPos>> map = Lists.newArrayList();
|
|
for (BlockPos pos : positions) {
|
|
- map.add(new java.util.AbstractMap.SimpleEntry<>(this.nodeEvaluator.getGoal(pos.getX(), pos.getY(), pos.getZ()), pos));
|
|
+ map.add(new java.util.AbstractMap.SimpleEntry<>(nodeEvaluator.getGoal(pos.getX(), pos.getY(), pos.getZ()), pos));
|
|
}
|
|
// Paper end
|
|
- Path path = this.findPath(world.getProfiler(), node, map, followRange, distance, rangeMultiplier);
|
|
- this.nodeEvaluator.done();
|
|
- return path;
|
|
+
|
|
+ // Mirai start
|
|
+ if (this.nodeEvaluatorGenerator == null) {
|
|
+ // run sync :(
|
|
+ dev.etil.mirai.path.NodeEvaluatorCache.removeNodeEvaluator(nodeEvaluator);
|
|
+ return this.findPath(world.getProfiler(), node, map, followRange, distance, rangeMultiplier);
|
|
+ }
|
|
+
|
|
+ return new dev.etil.mirai.path.AsyncPath(Lists.newArrayList(), positions, () -> {
|
|
+ try {
|
|
+ return this.processPath(nodeEvaluator, node, map, followRange, distance, rangeMultiplier);
|
|
+ } finally {
|
|
+ nodeEvaluator.done();
|
|
+ dev.etil.mirai.path.NodeEvaluatorCache.returnNodeEvaluator(nodeEvaluator);
|
|
+ }
|
|
+ });
|
|
+ // Mirai end
|
|
}
|
|
}
|
|
|
|
- @Nullable
|
|
+ // Mirai start - split pathfinding into the original sync method for compat and processing for delaying
|
|
// Paper start - optimize collection
|
|
private Path findPath(ProfilerFiller profiler, Node startNode, List<Map.Entry<Target, BlockPos>> positions, float followRange, int distance, float rangeMultiplier) {
|
|
+ // readd the profiler code for sync
|
|
profiler.push("find_path");
|
|
profiler.markForCharting(MetricCategory.PATH_FINDING);
|
|
+
|
|
+ try {
|
|
+ return this.processPath(this.nodeEvaluator, startNode, positions, followRange, distance, rangeMultiplier);
|
|
+ } finally {
|
|
+ this.nodeEvaluator.done();
|
|
+ }
|
|
+ }
|
|
+ // Mirai end
|
|
+
|
|
+ private synchronized @org.jetbrains.annotations.NotNull Path processPath(NodeEvaluator nodeEvaluator, Node startNode, List<Map.Entry<Target, BlockPos>> positions, float followRange, int distance, float rangeMultiplier) { // Mirai - sync to only use the caching functions in this class on a single thread
|
|
+ org.apache.commons.lang3.Validate.isTrue(!positions.isEmpty()); // ensure that we have at least one position, which means we'll always return a path
|
|
+
|
|
// Set<Target> set = positions.keySet();
|
|
startNode.g = 0.0F;
|
|
startNode.h = this.getBestH(startNode, positions); // Paper - optimize collection
|
|
@@ -91,7 +130,7 @@ public class PathFinder {
|
|
}
|
|
|
|
if (!(node.distanceTo(startNode) >= followRange)) {
|
|
- int k = this.nodeEvaluator.getNeighbors(this.neighbors, node);
|
|
+ int k = nodeEvaluator.getNeighbors(this.neighbors, node);
|
|
|
|
for(int l = 0; l < k; ++l) {
|
|
Node node2 = this.neighbors[l];
|
|
@@ -123,9 +162,14 @@ public class PathFinder {
|
|
if (best == null || comparator.compare(path, best) < 0)
|
|
best = path;
|
|
}
|
|
+
|
|
+ // Mirai start - ignore this warning, we know that the above loop always runs at least once since positions is not empty
|
|
+ //noinspection ConstantConditions
|
|
return best;
|
|
// Paper end
|
|
+ // Mirai end
|
|
}
|
|
+ // Mirai end
|
|
|
|
protected float distance(Node a, Node b) {
|
|
return a.distanceTo(b);
|