aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/pathfinder/positions.rs
blob: c3c089d3c8a6d4ad97b19e1124baf4d0f4d11d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::{
    cmp,
    hash::{Hash, Hasher},
    mem::{self, transmute},
    ops::{Add, Mul},
};

use azalea_core::position::BlockPos;

/// An offset from a block position.
///
/// This fits in 64 bits, so it's more efficient than a BlockPos in some cases.
///
/// The X and Z are limited to ±32k.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct RelBlockPos {
    /// The actual non-relative Y coordinate of the block.
    pub y: i32,
    /// The X coordinate of the block, relative to some origin.
    pub x: i16,
    /// The Y coordinate of the block, relative to some origin.
    pub z: i16,
}

impl RelBlockPos {
    pub fn get_origin(origin: BlockPos) -> Self {
        Self::new(0, origin.y, 0)
    }

    #[inline]
    pub const fn new(x: i16, y: i32, z: i16) -> Self {
        Self { x, y, z }
    }

    #[inline]
    pub fn apply(self, origin: BlockPos) -> BlockPos {
        BlockPos::new(origin.x + self.x as i32, self.y, origin.z + self.z as i32)
    }

    /// Create a new [`RelBlockPos`] from a given origin and new position.
    #[inline]
    pub fn from_origin(origin: BlockPos, new: BlockPos) -> Self {
        Self {
            x: (new.x - origin.x) as i16,
            y: new.y,
            z: (new.z - origin.z) as i16,
        }
    }

    #[inline]
    pub fn up(&self, y: i32) -> Self {
        Self {
            x: self.x,
            y: self.y + y,
            z: self.z,
        }
    }
    #[inline]
    pub fn down(&self, y: i32) -> Self {
        Self {
            x: self.x,
            y: self.y - y,
            z: self.z,
        }
    }
    #[inline]
    pub fn north(&self, z: i16) -> Self {
        Self {
            x: self.x,
            y: self.y,
            z: self.z - z,
        }
    }
    #[inline]
    pub fn south(&self, z: i16) -> Self {
        Self {
            x: self.x,
            y: self.y,
            z: self.z + z,
        }
    }
    #[inline]
    pub fn east(&self, x: i16) -> Self {
        Self {
            x: self.x + x,
            y: self.y,
            z: self.z,
        }
    }
    #[inline]
    pub fn west(&self, x: i16) -> Self {
        Self {
            x: self.x - x,
            y: self.y,
            z: self.z,
        }
    }

    #[inline]
    pub fn as_u64(self) -> u64 {
        // SAFETY: RelBlockPos can be represented as a u64
        unsafe { transmute::<Self, u64>(self) }
    }
}

impl Add<RelBlockPos> for RelBlockPos {
    type Output = RelBlockPos;

    fn add(self, rhs: RelBlockPos) -> Self::Output {
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}
impl Mul<i16> for RelBlockPos {
    type Output = RelBlockPos;

    fn mul(self, rhs: i16) -> Self::Output {
        Self {
            x: self.x * rhs,
            y: self.y * rhs as i32,
            z: self.z * rhs,
        }
    }
}

impl Hash for RelBlockPos {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_u64().hash(state);
    }
}

/// Similar to [`ChunkSectionPos`] but fits in 64 bits.
///
/// [`ChunkSectionPos`]: azalea_core::position::ChunkSectionPos
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct SmallChunkSectionPos {
    pub y: i32,
    pub x: i16,
    pub z: i16,
}
impl SmallChunkSectionPos {
    pub fn as_u64(self) -> u64 {
        unsafe { mem::transmute::<_, u64>(self) }
    }
}
impl From<BlockPos> for SmallChunkSectionPos {
    #[inline]
    fn from(pos: BlockPos) -> Self {
        Self {
            x: (pos.x >> 4) as i16,
            y: pos.y >> 4,
            z: (pos.z >> 4) as i16,
        }
    }
}
impl PartialOrd for SmallChunkSectionPos {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for SmallChunkSectionPos {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.as_u64().cmp(&other.as_u64())
    }
}