| 1 | package net.minecraft.network;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 | import net.minecraft.world.phys.Vec3;
|
| 6 |
|
| 7 | public class LpVec3 {
|
| 8 | private static final int DATA_BITS = 15;
|
| 9 | private static final int DATA_BITS_MASK = 32767;
|
| 10 | private static final double MAX_QUANTIZED_VALUE = 32766.0;
|
| 11 | private static final int SCALE_BITS = 2;
|
| 12 | private static final int SCALE_BITS_MASK = 3;
|
| 13 | private static final int CONTINUATION_FLAG = 4;
|
| 14 | private static final int X_OFFSET = 3;
|
| 15 | private static final int Y_OFFSET = 18;
|
| 16 | private static final int Z_OFFSET = 33;
|
| 17 | public static final double ABS_MAX_VALUE = 1.7179869183E10;
|
| 18 | public static final double ABS_MIN_VALUE = 3.051944088384301E-5;
|
| 19 |
|
| 20 | public static boolean hasContinuationBit(final int in) {
|
| 21 | return (in & 4) == 4;
|
| 22 | }
|
| 23 |
|
| 24 | public static Vec3 read(final ByteBuf input) {
|
| 25 | int lowest = input.readUnsignedByte();
|
| 26 | if (lowest == 0) {
|
| 27 | return Vec3.ZERO;
|
| 28 | }
|
| 29 |
|
| 30 | int middle = input.readUnsignedByte();
|
| 31 | long highest = input.readUnsignedInt();
|
| 32 | long buffer = highest << 16 | middle << 8 | lowest;
|
| 33 | long scale = lowest & 3;
|
| 34 | if (hasContinuationBit(lowest)) {
|
| 35 | scale |= (VarInt.read(input) & 4294967295L) << 2;
|
| 36 | }
|
| 37 |
|
| 38 | return new Vec3(unpack(buffer >> 3) * scale, unpack(buffer >> 18) * scale, unpack(buffer >> 33) * scale);
|
| 39 | }
|
| 40 |
|
| 41 | public static void write(final ByteBuf output, final Vec3 value) {
|
| 42 | double x = sanitize(value.x);
|
| 43 | double y = sanitize(value.y);
|
| 44 | double z = sanitize(value.z);
|
| 45 | double chessboardLength = Mth.absMax(x, Mth.absMax(y, z));
|
| 46 | if (chessboardLength < 3.051944088384301E-5) {
|
| 47 | output.writeByte(0);
|
| 48 | } else {
|
| 49 | long scale = Mth.ceilLong(chessboardLength);
|
| 50 | boolean isPartial = (scale & 3L) != scale;
|
| 51 | long markers = isPartial ? scale & 3L | 4L : scale;
|
| 52 | long xn = pack(x / scale) << 3;
|
| 53 | long yn = pack(y / scale) << 18;
|
| 54 | long zn = pack(z / scale) << 33;
|
| 55 | long buffer = markers | xn | yn | zn;
|
| 56 | output.writeByte((byte)buffer);
|
| 57 | output.writeByte((byte)(buffer >> 8));
|
| 58 | output.writeInt((int)(buffer >> 16));
|
| 59 | if (isPartial) {
|
| 60 | VarInt.write(output, (int)(scale >> 2));
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | private static double sanitize(final double value) {
|
| 66 | return Double.isNaN(value) ? 0.0 : Math.clamp(value, -1.7179869183E10, 1.7179869183E10);
|
| 67 | }
|
| 68 |
|
| 69 | private static long pack(final double value) {
|
| 70 | return Math.round((value * 0.5 + 0.5) * 32766.0);
|
| 71 | }
|
| 72 |
|
| 73 | private static double unpack(final long value) {
|
| 74 | return Math.min(value & 32767L, 32766.0) * 2.0 / 32766.0 - 1.0;
|
| 75 | }
|
| 76 | }
|
| 77 |
|