module Rattletrap.Encode.Vector
  ( putVector
  )
where

import Rattletrap.Encode.CompressedWord
import Rattletrap.Type.CompressedWord
import Rattletrap.Type.Vector

import qualified Data.Binary.Bits.Put as BinaryBits

putVector :: Vector -> BinaryBits.BitPut ()
putVector :: Vector -> BitPut ()
putVector Vector
vector = do
  let
    bitSize :: Word
bitSize =
      Float -> Word
forall a b. (RealFrac a, Integral b) => a -> b
round (Float -> Float -> Float
forall a. Floating a => a -> a -> a
logBase (Float
2 :: Float) (Word -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector -> Word
vectorBias Vector
vector))) Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1 :: Word
    dx :: Word
dx =
      Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector -> Int
vectorX Vector
vector Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector -> Word
vectorBias Vector
vector)) :: Word
    dy :: Word
dy =
      Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector -> Int
vectorY Vector
vector Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector -> Word
vectorBias Vector
vector)) :: Word
    dz :: Word
dz =
      Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector -> Int
vectorZ Vector
vector Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector -> Word
vectorBias Vector
vector)) :: Word
    limit :: Word
limit = Word
2 Word -> Word -> Word
forall a b. (Num a, Integral b) => a -> b -> a
^ (Word
bitSize Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Word
2) :: Word
  CompressedWord -> BitPut ()
putCompressedWord (Vector -> CompressedWord
vectorSize Vector
vector)
  CompressedWord -> BitPut ()
putCompressedWord (Word -> Word -> CompressedWord
CompressedWord Word
limit Word
dx)
  CompressedWord -> BitPut ()
putCompressedWord (Word -> Word -> CompressedWord
CompressedWord Word
limit Word
dy)
  CompressedWord -> BitPut ()
putCompressedWord (Word -> Word -> CompressedWord
CompressedWord Word
limit Word
dz)