+void Packet::Payload::Write(const glm::quat &val, size_t off) noexcept {
+ // find the largest component
+ float largest = 0.0f;
+ int largest_index = -1;
+ for (int i = 0; i < 4; ++i) {
+ float iabs = abs(val[i]);
+ if (iabs > largest) {
+ largest = iabs;
+ largest_index = i;
+ }
+ }
+ // make sure it's positive
+ const glm::quat q(val[largest_index] < 0.0f ? -val : val);
+ // move index to the two most significant bits
+ uint64_t packed = uint64_t(largest_index) << 62;
+ // we have to map from [-0.7072,0.7072] to [-524287,524287] and move into positive range
+ constexpr float conv = 524287.0f / 0.7072f;
+ // if largest is 1, the other three are 0
+ // precision of comparison is the interval of our mapping
+ if (abs(1.0 - largest) < (1.0f / conv)) {
+ packed |= 0x7FFFF7FFFF7FFFF;
+ } else {
+ // pack the three smaller components into 20bit ints each
+ int shift = 40;
+ for (int i = 0; i < 4; ++i) {
+ if (i != largest_index) {
+ packed |= uint64_t(int(q[i] * conv) + 524287) << shift;
+ shift -= 20;
+ }
+ }
+ }
+ // and write it out
+ Write(packed, off);
+}
+
+void Packet::Payload::Read(glm::quat &val, size_t off) const noexcept {
+ // extract the 8 byte packed value
+ uint64_t packed = 0;
+ Read(packed, off);
+ // two most significant bits are the index of the largest (omitted) component
+ int largest_index = packed >> 62;
+ // if all other three are 0, largest is 1 and we can omit the conversion
+ if ((packed & 0xFFFFFFFFFFFFFFF) == 0x7FFFF7FFFF7FFFF) {
+ val = { 0.0f, 0.0f, 0.0f, 0.0f };
+ val[largest_index] = 1.0f;
+ return;
+ }
+ // we have to map from [-524287,524287] to [-0.7072,0.7072]
+ constexpr float conv = 0.7072f / 524287.0f;
+ int shift = 40;
+ for (int i = 0; i < 4; ++i) {
+ if (i != largest_index) {
+ val[i] = float(int((packed >> shift) & 0xFFFFF) - 524287) * conv;
+ shift -= 20;
+ } else {
+ // set to zero so the length of the other three can be determined
+ val[i] = 0.0f;
+ }
+ }
+ // omitted component squared is 1 - length squared of others
+ val[largest_index] = sqrt(1.0f - glm::length2(val));
+ // and already normalized
+}
+
+void Packet::Payload::Write(const EntityState &state, size_t off) noexcept {
+ Write(state.pos.chunk, off);
+ WritePackU(state.pos.block * (1.0f / ExactLocation::fscale), off + 12);
+ Write(state.velocity, off + 18);
+ Write(state.orient, off + 30);
+ WritePackN(state.pitch * PI_0p5_inv, off + 38);
+ WritePackN(state.yaw * PI_inv, off + 40);
+}
+
+void Packet::Payload::Read(EntityState &state, size_t off) const noexcept {
+ Read(state.pos.chunk, off);
+ ReadPackU(state.pos.block, off + 12);
+ Read(state.velocity, off + 18);
+ Read(state.orient, off + 30);
+ ReadPackN(state.pitch, off + 38);
+ ReadPackN(state.yaw, off + 40);
+ state.pos.block *= ExactLocation::fscale;
+ state.pitch *= PI_0p5;
+ state.yaw *= PI;
+}
+
+void Packet::Payload::Write(const EntityState &state, const glm::ivec3 &base, size_t off) noexcept {
+ WritePackB(state.pos.chunk - base, off);
+ WritePackU(state.pos.block * (1.0f / ExactLocation::fscale), off + 3);
+ Write(state.velocity, off + 9);
+ Write(state.orient, off + 21);
+ WritePackN(state.pitch * PI_0p5_inv, off + 29);
+ WritePackN(state.yaw * PI_inv, off + 31);
+}
+
+void Packet::Payload::Read(EntityState &state, const glm::ivec3 &base, size_t off) const noexcept {
+ ReadPackB(state.pos.chunk, off);
+ ReadPackU(state.pos.block, off + 3);
+ Read(state.velocity, off + 9);
+ Read(state.orient, off + 21);
+ ReadPackN(state.pitch, off + 29);
+ ReadPackN(state.yaw, off + 31);
+ state.pos.chunk += base;
+ state.pos.block *= ExactLocation::fscale;
+ state.pitch *= PI_0p5;
+ state.yaw *= PI;
+}
+
+void Packet::Payload::WritePackB(const glm::ivec3 &val, size_t off) noexcept {
+ Write(int8_t(val.x), off);
+ Write(int8_t(val.y), off + 1);
+ Write(int8_t(val.z), off + 2);
+}
+
+void Packet::Payload::ReadPackB(glm::ivec3 &val, size_t off) const noexcept {
+ int8_t conv = 0;
+ Read(conv, off);
+ val.x = conv;
+ Read(conv, off + 1);
+ val.y = conv;
+ Read(conv, off + 2);
+ val.z = conv;
+}
+
+void Packet::Payload::WritePackN(float val, size_t off) noexcept {
+ int16_t raw = glm::clamp(glm::round(val * 32767.0f), -32767.0f, 32767.0f);
+ Write(raw, off);
+}
+
+void Packet::Payload::ReadPackN(float &val, size_t off) const noexcept {
+ int16_t raw = 0;
+ Read(raw, off);
+ val = raw * (1.0f/32767.0f);
+}
+
+void Packet::Payload::WritePackN(const glm::vec3 &val, size_t off) noexcept {
+ WritePackN(val.x, off);
+ WritePackN(val.y, off + 2);
+ WritePackN(val.z, off + 4);
+}
+
+void Packet::Payload::ReadPackN(glm::vec3 &val, size_t off) const noexcept {
+ ReadPackN(val.x, off);
+ ReadPackN(val.y, off + 2);
+ ReadPackN(val.z, off + 4);
+}
+
+void Packet::Payload::WritePackU(float val, size_t off) noexcept {
+ uint16_t raw = glm::clamp(glm::round(val * 65535.0f), 0.0f, 65535.0f);
+ Write(raw, off);
+}
+
+void Packet::Payload::ReadPackU(float &val, size_t off) const noexcept {
+ uint16_t raw = 0;
+ Read(raw, off);
+ val = raw * (1.0f/65535.0f);
+}
+
+void Packet::Payload::WritePackU(const glm::vec3 &val, size_t off) noexcept {
+ WritePackU(val.x, off);
+ WritePackU(val.y, off + 2);
+ WritePackU(val.z, off + 4);
+}
+
+void Packet::Payload::ReadPackU(glm::vec3 &val, size_t off) const noexcept {
+ ReadPackU(val.x, off);
+ ReadPackU(val.y, off + 2);
+ ReadPackU(val.z, off + 4);
+}
+