| 1 | package net.minecraft.world.level.pathfinder;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import java.util.Comparator;
|
| 6 | import java.util.HashSet;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.function.BooleanSupplier;
|
| 12 | import java.util.function.Function;
|
| 13 | import java.util.stream.Collectors;
|
| 14 | import net.minecraft.core.BlockPos;
|
| 15 | import net.minecraft.util.profiling.Profiler;
|
| 16 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 17 | import net.minecraft.util.profiling.metrics.MetricCategory;
|
| 18 | import net.minecraft.world.entity.Mob;
|
| 19 | import net.minecraft.world.level.PathNavigationRegion;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public class PathFinder {
|
| 23 | private static final float FUDGING = 1.5F;
|
| 24 | private final Node[] neighbors = new Node[32];
|
| 25 | private int maxVisitedNodes;
|
| 26 | private final NodeEvaluator nodeEvaluator;
|
| 27 | private final BinaryHeap openSet = new BinaryHeap();
|
| 28 | private BooleanSupplier captureDebug = () -> false;
|
| 29 |
|
| 30 | public PathFinder(final NodeEvaluator nodeEvaluator, final int maxVisitedNodes) {
|
| 31 | this.nodeEvaluator = nodeEvaluator;
|
| 32 | this.maxVisitedNodes = maxVisitedNodes;
|
| 33 | }
|
| 34 |
|
| 35 | public void setCaptureDebug(final BooleanSupplier captureDebug) {
|
| 36 | this.captureDebug = captureDebug;
|
| 37 | }
|
| 38 |
|
| 39 | public void setMaxVisitedNodes(final int maxVisitedNodes) {
|
| 40 | this.maxVisitedNodes = maxVisitedNodes;
|
| 41 | }
|
| 42 |
|
| 43 | public @Nullable Path findPath(
|
| 44 | final PathNavigationRegion level,
|
| 45 | final Mob entity,
|
| 46 | final Set<BlockPos> targets,
|
| 47 | final float maxPathLength,
|
| 48 | final int reachRange,
|
| 49 | final float maxVisitedNodesMultiplier
|
| 50 | ) {
|
| 51 | this.openSet.clear();
|
| 52 | this.nodeEvaluator.prepare(level, entity);
|
| 53 | Node from = this.nodeEvaluator.getStart();
|
| 54 | if (from == null) {
|
| 55 | return null;
|
| 56 | }
|
| 57 |
|
| 58 | Map<Target, BlockPos> tos = targets.stream()
|
| 59 | .collect(Collectors.toMap(pos -> this.nodeEvaluator.getTarget(pos.getX(), pos.getY(), pos.getZ()), Function.identity()));
|
| 60 | Path path = this.findPath(from, tos, maxPathLength, reachRange, maxVisitedNodesMultiplier);
|
| 61 | this.nodeEvaluator.done();
|
| 62 | return path;
|
| 63 | }
|
| 64 |
|
| 65 | private @Nullable Path findPath(
|
| 66 | final Node from, final Map<Target, BlockPos> targetMap, final float maxPathLength, final int reachRange, final float maxVisitedNodesMultiplier
|
| 67 | ) {
|
| 68 | ProfilerFiller profiler = Profiler.get();
|
| 69 | profiler.push("find_path");
|
| 70 | profiler.markForCharting(MetricCategory.PATH_FINDING);
|
| 71 | Set<Target> targets = targetMap.keySet();
|
| 72 | from.g = 0.0F;
|
| 73 | from.h = this.getBestH(from, targets);
|
| 74 | from.f = from.h;
|
| 75 | this.openSet.clear();
|
| 76 | this.openSet.insert(from);
|
| 77 | boolean captureDebug = this.captureDebug.getAsBoolean();
|
| 78 | Set<Node> closedSet = captureDebug ? new HashSet<>() : Set.of();
|
| 79 | int count = 0;
|
| 80 | Set<Target> reachedTargets = Sets.newHashSetWithExpectedSize(targets.size());
|
| 81 | int maxVisitedNodesAdjusted = (int)(this.maxVisitedNodes * maxVisitedNodesMultiplier);
|
| 82 |
|
| 83 | while (!this.openSet.isEmpty()) {
|
| 84 | if (++count >= maxVisitedNodesAdjusted) {
|
| 85 | break;
|
| 86 | }
|
| 87 |
|
| 88 | Node current = this.openSet.pop();
|
| 89 | current.closed = true;
|
| 90 |
|
| 91 | for (Target target : targets) {
|
| 92 | if (current.distanceManhattan(target) <= reachRange) {
|
| 93 | target.setReached();
|
| 94 | reachedTargets.add(target);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | if (!reachedTargets.isEmpty()) {
|
| 99 | break;
|
| 100 | }
|
| 101 |
|
| 102 | if (captureDebug) {
|
| 103 | closedSet.add(current);
|
| 104 | }
|
| 105 |
|
| 106 | if (!(current.distanceTo(from) >= maxPathLength)) {
|
| 107 | int neighborCount = this.nodeEvaluator.getNeighbors(this.neighbors, current);
|
| 108 |
|
| 109 | for (int i = 0; i < neighborCount; i++) {
|
| 110 | Node neighbor = this.neighbors[i];
|
| 111 | float distance = this.distance(current, neighbor);
|
| 112 | neighbor.walkedDistance = current.walkedDistance + distance;
|
| 113 | float tentativeGScore = current.g + distance + neighbor.costMalus;
|
| 114 | if (neighbor.walkedDistance < maxPathLength && (!neighbor.inOpenSet() || tentativeGScore < neighbor.g)) {
|
| 115 | neighbor.cameFrom = current;
|
| 116 | neighbor.g = tentativeGScore;
|
| 117 | neighbor.h = this.getBestH(neighbor, targets) * 1.5F;
|
| 118 | if (neighbor.inOpenSet()) {
|
| 119 | this.openSet.changeCost(neighbor, neighbor.g + neighbor.h);
|
| 120 | } else {
|
| 121 | neighbor.f = neighbor.g + neighbor.h;
|
| 122 | this.openSet.insert(neighbor);
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | Optional<Path> optPath = !reachedTargets.isEmpty()
|
| 130 | ? reachedTargets.stream()
|
| 131 | .map(target -> this.reconstructPath(target.getBestNode(), targetMap.get(target), true))
|
| 132 | .min(Comparator.comparingInt(Path::getNodeCount))
|
| 133 | : targets.stream()
|
| 134 | .map(target -> this.reconstructPath(target.getBestNode(), targetMap.get(target), false))
|
| 135 | .min(Comparator.comparingDouble(Path::getDistToTarget).thenComparingInt(Path::getNodeCount));
|
| 136 | profiler.pop();
|
| 137 | if (optPath.isEmpty()) {
|
| 138 | return null;
|
| 139 | }
|
| 140 |
|
| 141 | Path path = optPath.get();
|
| 142 | if (captureDebug) {
|
| 143 | path.setDebug(List.of(this.openSet.getHeap()), List.copyOf(closedSet), targets);
|
| 144 | }
|
| 145 |
|
| 146 | return path;
|
| 147 | }
|
| 148 |
|
| 149 | protected float distance(final Node from, final Node to) {
|
| 150 | return from.distanceTo(to);
|
| 151 | }
|
| 152 |
|
| 153 | private float getBestH(final Node from, final Set<Target> targets) {
|
| 154 | float bestH = Float.MAX_VALUE;
|
| 155 |
|
| 156 | for (Target target : targets) {
|
| 157 | float h = from.distanceTo(target);
|
| 158 | target.updateBest(h, from);
|
| 159 | bestH = Math.min(h, bestH);
|
| 160 | }
|
| 161 |
|
| 162 | return bestH;
|
| 163 | }
|
| 164 |
|
| 165 | private Path reconstructPath(final Node closest, final BlockPos target, final boolean reached) {
|
| 166 | List<Node> nodes = Lists.newArrayList();
|
| 167 | Node node = closest;
|
| 168 | nodes.add(0, node);
|
| 169 |
|
| 170 | while (node.cameFrom != null) {
|
| 171 | node = node.cameFrom;
|
| 172 | nodes.add(0, node);
|
| 173 | }
|
| 174 |
|
| 175 | return new Path(nodes, target, reached);
|
| 176 | }
|
| 177 | }
|
| 178 |
|