958c4ff

Branches > main > 958c4ff > files

Add angle-> and lpvec3-> writers

Grahp, 2026-08-24

Commit: 958c4ffbe79b8f843ce52a4390c9a85aa6fb2ad6
Parent: d655594

1 changed file:

src/net/bowmap/transport/writers.clj modified (view file)

@@ -281,3 +281,34 @@             :empty-block-light-mask bitset->             :sky-light-arrays (list-> byte->)             :block-light-arrays (list-> byte->)]))++(defn angle-> [buf angle]+  (byte-> buf (Math/round (* (/ angle 360) 256))))++(def ^:const max-quantized-value 32766.0)+(def ^:const continuation-flag 0x04)+(def ^:const scale-bits 0x03)++(defn- pack [value]+  (Math/round (* (+ (* value 0.5) 0.5) max-quantized-value)))++(defn lpvec3-> [buf {:keys! [x y z]}]+  (let [max-coordinate (max (abs x) (abs y) (abs z))]+    (if (or (NaN? max-coordinate) (< max-coordinate (/ 1 max-quantized-value)))+      (byte-> buf 0)+      (let [scale-factor (long (Math/ceil max-coordinate))+            needs-continuation? (not= (bit-and scale-factor scale-bits) scale-factor)+            packed-scale (if needs-continuation?+                           (bit-or (bit-and scale-factor scale-bits) continuation-flag)+                           scale-factor)+            packed (bit-or+                     (bit-shift-left (pack (/ x scale-factor)) 3)+                     (bit-shift-left (pack (/ y scale-factor)) 18)+                     (bit-shift-left (pack (/ z scale-factor)) 33)+                     packed-scale)]+        (byte-> buf (unchecked-byte packed))+        (byte-> buf (unchecked-byte (bit-shift-right packed 8)))+        (int-> buf (unchecked-int (bit-shift-right packed 16)))+        (when needs-continuation?+          (varint-> buf (bit-shift-right scale-factor 2)))+        buf))))