| 1 | package net.minecraft.world.level.pathfinder;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.util.HashSet;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.util.VisibleForDebug;
|
| 11 | import net.minecraft.world.entity.Entity;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public final class Path {
|
| 16 | public static final StreamCodec<ByteBuf, Path> DEBUG_STREAM_CODEC = StreamCodec.composite(
|
| 17 | ByteBufCodecs.BOOL,
|
| 18 | p -> p.reached,
|
| 19 | ByteBufCodecs.INT,
|
| 20 | p -> p.nextNodeIndex,
|
| 21 | BlockPos.STREAM_CODEC,
|
| 22 | p -> p.target,
|
| 23 | Node.DEBUG_STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 24 | p -> p.nodes,
|
| 25 | Path.DebugData.STREAM_CODEC,
|
| 26 | p -> {
|
| 27 | if (p.debugData != null && !p.debugData.targetNodes.isEmpty()) {
|
| 28 | return p.debugData;
|
| 29 | } else {
|
| 30 | throw new IllegalStateException("Missing debug data");
|
| 31 | }
|
| 32 | },
|
| 33 | (reached, nextNodeIndex, target, nodes, debugData) -> {
|
| 34 | Path path = new Path(nodes, target, reached);
|
| 35 | path.debugData = debugData;
|
| 36 | path.nextNodeIndex = nextNodeIndex;
|
| 37 | return path;
|
| 38 | }
|
| 39 | );
|
| 40 | private final List<Node> nodes;
|
| 41 | private Path.@Nullable DebugData debugData;
|
| 42 | private int nextNodeIndex;
|
| 43 | private final BlockPos target;
|
| 44 | private final float distToTarget;
|
| 45 | private final boolean reached;
|
| 46 |
|
| 47 | public Path(final List<Node> nodes, final BlockPos target, final boolean reached) {
|
| 48 | this.nodes = nodes;
|
| 49 | this.target = target;
|
| 50 | this.distToTarget = nodes.isEmpty() ? Float.MAX_VALUE : this.nodes.get(this.nodes.size() - 1).distanceManhattan(this.target);
|
| 51 | this.reached = reached;
|
| 52 | }
|
| 53 |
|
| 54 | public void advance() {
|
| 55 | this.nextNodeIndex++;
|
| 56 | }
|
| 57 |
|
| 58 | public boolean notStarted() {
|
| 59 | return this.nextNodeIndex <= 0;
|
| 60 | }
|
| 61 |
|
| 62 | public boolean isDone() {
|
| 63 | return this.nextNodeIndex >= this.nodes.size();
|
| 64 | }
|
| 65 |
|
| 66 | public @Nullable Node getEndNode() {
|
| 67 | return !this.nodes.isEmpty() ? this.nodes.get(this.nodes.size() - 1) : null;
|
| 68 | }
|
| 69 |
|
| 70 | public Node getNode(final int i) {
|
| 71 | return this.nodes.get(i);
|
| 72 | }
|
| 73 |
|
| 74 | public void truncateNodes(final int index) {
|
| 75 | if (this.nodes.size() > index) {
|
| 76 | this.nodes.subList(index, this.nodes.size()).clear();
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public void replaceNode(final int index, final Node replaceWith) {
|
| 81 | this.nodes.set(index, replaceWith);
|
| 82 | }
|
| 83 |
|
| 84 | public int getNodeCount() {
|
| 85 | return this.nodes.size();
|
| 86 | }
|
| 87 |
|
| 88 | public int getNextNodeIndex() {
|
| 89 | return this.nextNodeIndex;
|
| 90 | }
|
| 91 |
|
| 92 | public void setNextNodeIndex(final int nextNodeIndex) {
|
| 93 | this.nextNodeIndex = nextNodeIndex;
|
| 94 | }
|
| 95 |
|
| 96 | public Vec3 getEntityPosAtNode(final Entity entity, final int index) {
|
| 97 | Node node = this.nodes.get(index);
|
| 98 | double x = node.x + (int)(entity.getBbWidth() + 1.0F) * 0.5;
|
| 99 | double y = node.y;
|
| 100 | double z = node.z + (int)(entity.getBbWidth() + 1.0F) * 0.5;
|
| 101 | return new Vec3(x, y, z);
|
| 102 | }
|
| 103 |
|
| 104 | public BlockPos getNodePos(final int index) {
|
| 105 | return this.nodes.get(index).asBlockPos();
|
| 106 | }
|
| 107 |
|
| 108 | public Vec3 getNextEntityPos(final Entity entity) {
|
| 109 | return this.getEntityPosAtNode(entity, this.nextNodeIndex);
|
| 110 | }
|
| 111 |
|
| 112 | public BlockPos getNextNodePos() {
|
| 113 | return this.nodes.get(this.nextNodeIndex).asBlockPos();
|
| 114 | }
|
| 115 |
|
| 116 | public Node getNextNode() {
|
| 117 | return this.nodes.get(this.nextNodeIndex);
|
| 118 | }
|
| 119 |
|
| 120 | public @Nullable Node getPreviousNode() {
|
| 121 | return this.nextNodeIndex > 0 ? this.nodes.get(this.nextNodeIndex - 1) : null;
|
| 122 | }
|
| 123 |
|
| 124 | public boolean sameAs(final @Nullable Path path) {
|
| 125 | return path != null && this.nodes.equals(path.nodes);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public boolean equals(final Object obj) {
|
| 130 | return !(obj instanceof Path path)
|
| 131 | ? false
|
| 132 | : this.nextNodeIndex == path.nextNodeIndex
|
| 133 | && this.debugData == path.debugData
|
| 134 | && this.reached == path.reached
|
| 135 | && this.target.equals(path.target)
|
| 136 | && this.nodes.equals(path.nodes);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public int hashCode() {
|
| 141 | return this.nextNodeIndex + this.nodes.hashCode() * 31;
|
| 142 | }
|
| 143 |
|
| 144 | public boolean canReach() {
|
| 145 | return this.reached;
|
| 146 | }
|
| 147 |
|
| 148 | @VisibleForDebug
|
| 149 | void setDebug(final List<Node> openSet, final List<Node> closedSet, final Set<Target> targets) {
|
| 150 | this.debugData = new Path.DebugData(openSet, closedSet, targets);
|
| 151 | }
|
| 152 |
|
| 153 | public Path.@Nullable DebugData debugData() {
|
| 154 | return this.debugData;
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public String toString() {
|
| 159 | return "Path(length=" + this.nodes.size() + ")";
|
| 160 | }
|
| 161 |
|
| 162 | public BlockPos getTarget() {
|
| 163 | return this.target;
|
| 164 | }
|
| 165 |
|
| 166 | public float getDistToTarget() {
|
| 167 | return this.distToTarget;
|
| 168 | }
|
| 169 |
|
| 170 | public Path copy() {
|
| 171 | Path result = new Path(this.nodes, this.target, this.reached);
|
| 172 | result.debugData = this.debugData;
|
| 173 | result.nextNodeIndex = this.nextNodeIndex;
|
| 174 | return result;
|
| 175 | }
|
| 176 |
|
| 177 | public record DebugData(List<Node> openSet, List<Node> closedSet, Set<Target> targetNodes) {
|
| 178 | public static final StreamCodec<ByteBuf, Path.DebugData> STREAM_CODEC = StreamCodec.composite(
|
| 179 | Node.DEBUG_STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 180 | Path.DebugData::openSet,
|
| 181 | Node.DEBUG_STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 182 | Path.DebugData::closedSet,
|
| 183 | Target.DEBUG_STREAM_CODEC.apply(ByteBufCodecs.collection(HashSet::new)),
|
| 184 | Path.DebugData::targetNodes,
|
| 185 | Path.DebugData::new
|
| 186 | );
|
| 187 | }
|
| 188 | }
|
| 189 |
|