| 1 | package net.minecraft.world.level.pathfinder;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Function3;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class Node {
|
| 13 | public static final StreamCodec<ByteBuf, Node> DEBUG_STREAM_CODEC = createDebugStreamCodec(Node::new);
|
| 14 | public final int x;
|
| 15 | public final int y;
|
| 16 | public final int z;
|
| 17 | private final int hash;
|
| 18 | public int heapIdx = -1;
|
| 19 | public float g;
|
| 20 | public float h;
|
| 21 | public float f;
|
| 22 | public @Nullable Node cameFrom;
|
| 23 | public boolean closed;
|
| 24 | public float walkedDistance;
|
| 25 | public float costMalus;
|
| 26 | public PathType type = PathType.BLOCKED;
|
| 27 |
|
| 28 | protected static <N extends Node> StreamCodec<ByteBuf, N> createDebugStreamCodec(final Function3<Integer, Integer, Integer, N> factory) {
|
| 29 | return StreamCodec.composite(
|
| 30 | ByteBufCodecs.INT,
|
| 31 | n -> n.x,
|
| 32 | ByteBufCodecs.INT,
|
| 33 | n -> n.y,
|
| 34 | ByteBufCodecs.INT,
|
| 35 | n -> n.z,
|
| 36 | ByteBufCodecs.FLOAT,
|
| 37 | n -> n.walkedDistance,
|
| 38 | ByteBufCodecs.FLOAT,
|
| 39 | n -> n.costMalus,
|
| 40 | ByteBufCodecs.BOOL,
|
| 41 | n -> n.closed,
|
| 42 | PathType.STREAM_CODEC,
|
| 43 | n -> n.type,
|
| 44 | ByteBufCodecs.FLOAT,
|
| 45 | n -> n.f,
|
| 46 | (x, y, z, walkedDistance, costMalus, closed, type, f) -> {
|
| 47 | N node = factory.apply(x, y, z);
|
| 48 | node.walkedDistance = walkedDistance;
|
| 49 | node.costMalus = costMalus;
|
| 50 | node.closed = closed;
|
| 51 | node.type = type;
|
| 52 | node.f = f;
|
| 53 | return node;
|
| 54 | }
|
| 55 | );
|
| 56 | }
|
| 57 |
|
| 58 | public Node(final int x, final int y, final int z) {
|
| 59 | this.x = x;
|
| 60 | this.y = y;
|
| 61 | this.z = z;
|
| 62 | this.hash = createHash(x, y, z);
|
| 63 | }
|
| 64 |
|
| 65 | public Node cloneAndMove(final int x, final int y, final int z) {
|
| 66 | Node node = new Node(x, y, z);
|
| 67 | node.heapIdx = this.heapIdx;
|
| 68 | node.g = this.g;
|
| 69 | node.h = this.h;
|
| 70 | node.f = this.f;
|
| 71 | node.cameFrom = this.cameFrom;
|
| 72 | node.closed = this.closed;
|
| 73 | node.walkedDistance = this.walkedDistance;
|
| 74 | node.costMalus = this.costMalus;
|
| 75 | node.type = this.type;
|
| 76 | return node;
|
| 77 | }
|
| 78 |
|
| 79 | public static int createHash(final int x, final int y, final int z) {
|
| 80 | return y & 0xFF | (x & 32767) << 8 | (z & 32767) << 24 | (x < 0 ? Integer.MIN_VALUE : 0) | (z < 0 ? 32768 : 0);
|
| 81 | }
|
| 82 |
|
| 83 | public float distanceTo(final Node to) {
|
| 84 | float xd = to.x - this.x;
|
| 85 | float yd = to.y - this.y;
|
| 86 | float zd = to.z - this.z;
|
| 87 | return Mth.sqrt(xd * xd + yd * yd + zd * zd);
|
| 88 | }
|
| 89 |
|
| 90 | public float distanceToXZ(final Node to) {
|
| 91 | float xd = to.x - this.x;
|
| 92 | float zd = to.z - this.z;
|
| 93 | return Mth.sqrt(xd * xd + zd * zd);
|
| 94 | }
|
| 95 |
|
| 96 | public float distanceTo(final BlockPos pos) {
|
| 97 | float xd = pos.getX() - this.x;
|
| 98 | float yd = pos.getY() - this.y;
|
| 99 | float zd = pos.getZ() - this.z;
|
| 100 | return Mth.sqrt(xd * xd + yd * yd + zd * zd);
|
| 101 | }
|
| 102 |
|
| 103 | public float distanceToSqr(final Node to) {
|
| 104 | float xd = to.x - this.x;
|
| 105 | float yd = to.y - this.y;
|
| 106 | float zd = to.z - this.z;
|
| 107 | return xd * xd + yd * yd + zd * zd;
|
| 108 | }
|
| 109 |
|
| 110 | public float distanceToSqr(final BlockPos pos) {
|
| 111 | float xd = pos.getX() - this.x;
|
| 112 | float yd = pos.getY() - this.y;
|
| 113 | float zd = pos.getZ() - this.z;
|
| 114 | return xd * xd + yd * yd + zd * zd;
|
| 115 | }
|
| 116 |
|
| 117 | public float distanceManhattan(final Node to) {
|
| 118 | float xd = Math.abs(to.x - this.x);
|
| 119 | float yd = Math.abs(to.y - this.y);
|
| 120 | float zd = Math.abs(to.z - this.z);
|
| 121 | return xd + yd + zd;
|
| 122 | }
|
| 123 |
|
| 124 | public float distanceManhattan(final BlockPos pos) {
|
| 125 | float xd = Math.abs(pos.getX() - this.x);
|
| 126 | float yd = Math.abs(pos.getY() - this.y);
|
| 127 | float zd = Math.abs(pos.getZ() - this.z);
|
| 128 | return xd + yd + zd;
|
| 129 | }
|
| 130 |
|
| 131 | public BlockPos asBlockPos() {
|
| 132 | return new BlockPos(this.x, this.y, this.z);
|
| 133 | }
|
| 134 |
|
| 135 | public Vec3 asVec3() {
|
| 136 | return new Vec3(this.x, this.y, this.z);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public boolean equals(final Object o) {
|
| 141 | return !(o instanceof Node no) ? false : this.hash == no.hash && this.x == no.x && this.y == no.y && this.z == no.z;
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public int hashCode() {
|
| 146 | return this.hash;
|
| 147 | }
|
| 148 |
|
| 149 | public boolean inOpenSet() {
|
| 150 | return this.heapIdx >= 0;
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public String toString() {
|
| 155 | return "Node{x=" + this.x + ", y=" + this.y + ", z=" + this.z + "}";
|
| 156 | }
|
| 157 | }
|
| 158 |
|