azalea_entity/
vec_delta_codec.rs

1use azalea_core::position::Vec3;
2
3#[derive(Debug, Clone, Default)]
4pub struct VecDeltaCodec {
5    base: Vec3,
6}
7
8impl VecDeltaCodec {
9    pub fn new(base: Vec3) -> Self {
10        Self { base }
11    }
12
13    pub fn decode(&self, x: i64, y: i64, z: i64) -> Vec3 {
14        if x == 0 && y == 0 && z == 0 {
15            return self.base;
16        }
17
18        let new_x = if x == 0 {
19            self.base.x
20        } else {
21            decode(encode(self.base.x) + x)
22        };
23        let new_y = if y == 0 {
24            self.base.y
25        } else {
26            decode(encode(self.base.y) + y)
27        };
28        let new_z = if z == 0 {
29            self.base.z
30        } else {
31            decode(encode(self.base.z) + z)
32        };
33
34        Vec3::new(new_x, new_y, new_z)
35    }
36
37    pub fn encode_x(&self, pos: Vec3) -> i64 {
38        encode(pos.x) - encode(self.base.x)
39    }
40    pub fn encode_y(&self, pos: Vec3) -> i64 {
41        encode(pos.y) - encode(self.base.y)
42    }
43    pub fn encode_z(&self, pos: Vec3) -> i64 {
44        encode(pos.z) - encode(self.base.z)
45    }
46
47    pub fn set_base(&mut self, pos: Vec3) {
48        self.base = pos;
49    }
50    pub fn base(&self) -> Vec3 {
51        self.base
52    }
53}
54
55fn encode(value: f64) -> i64 {
56    (value * 4096.).round() as i64
57}
58fn decode(value: i64) -> f64 {
59    (value as f64) / 4096.
60}