| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.world.entity.PositionPath;
|
| 6 | import net.minecraft.world.entity.PositionStep;
|
| 7 | import net.minecraft.world.phys.Vec3;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | public class VecDeltaCodec {
|
| 11 | private static final double TRUNCATION_STEPS = 4096.0;
|
| 12 | private Vec3 base = Vec3.ZERO;
|
| 13 |
|
| 14 | @VisibleForTesting
|
| 15 | static long encode(final double input) {
|
| 16 | return Math.round(input * 4096.0);
|
| 17 | }
|
| 18 |
|
| 19 | @VisibleForTesting
|
| 20 | static double decode(final long v) {
|
| 21 | return v / 4096.0;
|
| 22 | }
|
| 23 |
|
| 24 | public static double encodingPrecisionLoss(final double d) {
|
| 25 | return decode(encode(d)) - d;
|
| 26 | }
|
| 27 |
|
| 28 | public static boolean isDeltaTooBig(final long xa, final long ya, final long za) {
|
| 29 | return xa < -32768L || xa > 32767L || ya < -32768L || ya > 32767L || za < -32768L || za > 32767L;
|
| 30 | }
|
| 31 |
|
| 32 | public Vec3 decode(final long xa, final long ya, final long za) {
|
| 33 | if (xa == 0L && ya == 0L && za == 0L) {
|
| 34 | return this.base;
|
| 35 | }
|
| 36 |
|
| 37 | double x = xa == 0L ? this.base.x : decode(encode(this.base.x) + xa);
|
| 38 | double y = ya == 0L ? this.base.y : decode(encode(this.base.y) + ya);
|
| 39 | double z = za == 0L ? this.base.z : decode(encode(this.base.z) + za);
|
| 40 | return new Vec3(x, y, z);
|
| 41 | }
|
| 42 |
|
| 43 | public @Nullable VecDelta tryEncode(final PositionPath position) {
|
| 44 | return switch (position) {
|
| 45 | case PositionPath.Linear(Vec3 pos) -> this.tryEncode(pos);
|
| 46 | case PositionPath.Stepped var6 -> {
|
| 47 | PositionPath.Stepped var10000 = var6;
|
| 48 |
|
| 49 | try {
|
| 50 | var10000.endPosition();
|
| 51 | } catch (Throwable var10) {
|
| 52 | throw new MatchException(var10.toString(), var10);
|
| 53 | }
|
| 54 |
|
| 55 | var10000 = var6;
|
| 56 |
|
| 57 | try {
|
| 58 | var14 = var10000.steps();
|
| 59 | } catch (Throwable var9) {
|
| 60 | throw new MatchException(var9.toString(), var9);
|
| 61 | }
|
| 62 |
|
| 63 | List var8 = var14;
|
| 64 | List<PositionStep> steps = var8;
|
| 65 | yield VecDelta.Stepped.tryEncode(this, steps);
|
| 66 | }
|
| 67 | default -> throw new MatchException(null, null);
|
| 68 | };
|
| 69 | }
|
| 70 |
|
| 71 | public @Nullable VecDelta tryEncode(final Vec3 pos) {
|
| 72 | long xa = this.encodeX(pos);
|
| 73 | long ya = this.encodeY(pos);
|
| 74 | long za = this.encodeZ(pos);
|
| 75 | return isDeltaTooBig(xa, ya, za) ? null : new VecDelta.Linear((short)xa, (short)ya, (short)za);
|
| 76 | }
|
| 77 |
|
| 78 | public long encodeX(final Vec3 pos) {
|
| 79 | return encode(pos.x) - encode(this.base.x);
|
| 80 | }
|
| 81 |
|
| 82 | public long encodeY(final Vec3 pos) {
|
| 83 | return encode(pos.y) - encode(this.base.y);
|
| 84 | }
|
| 85 |
|
| 86 | public long encodeZ(final Vec3 pos) {
|
| 87 | return encode(pos.z) - encode(this.base.z);
|
| 88 | }
|
| 89 |
|
| 90 | public Vec3 delta(final Vec3 pos) {
|
| 91 | return pos.subtract(this.base);
|
| 92 | }
|
| 93 |
|
| 94 | public void setBase(final Vec3 base) {
|
| 95 | this.base = base;
|
| 96 | }
|
| 97 |
|
| 98 | public Vec3 getBase() {
|
| 99 | return this.base;
|
| 100 | }
|
| 101 | }
|
| 102 |
|