aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src/position.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-05-15 01:46:11 +0000
committerGitHub <noreply@github.com>2022-05-15 01:46:11 +0000
commitd0ac62d85276bc48e4f8e0e60afdc35840681622 (patch)
treeff4996b89d6f34c7c452d1b2950e53d512bce3c1 /azalea-core/src/position.rs
parentef3cbe27f2a7eed5c635924d6fa0401dd04eae77 (diff)
parentc16e958d0be671a17edf060aee9850faccbcfe14 (diff)
downloadazalea-drasl-d0ac62d85276bc48e4f8e0e60afdc35840681622.tar.xz
Merge pull request #6 from mat-1/chunk-decoding
Chunk decoding
Diffstat (limited to 'azalea-core/src/position.rs')
-rw-r--r--azalea-core/src/position.rs157
1 files changed, 157 insertions, 0 deletions
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs
new file mode 100644
index 00000000..9c7cd132
--- /dev/null
+++ b/azalea-core/src/position.rs
@@ -0,0 +1,157 @@
+use std::ops::Rem;
+
+#[derive(Clone, Copy, Debug, Default)]
+pub struct BlockPos {
+ pub x: i32,
+ pub y: i32,
+ pub z: i32,
+}
+
+impl BlockPos {
+ pub fn new(x: i32, y: i32, z: i32) -> Self {
+ BlockPos { x, y, z }
+ }
+}
+
+impl Rem<i32> for BlockPos {
+ type Output = Self;
+
+ fn rem(self, rhs: i32) -> Self {
+ BlockPos {
+ x: self.x % rhs,
+ y: self.y % rhs,
+ z: self.z % rhs,
+ }
+ }
+}
+
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub struct ChunkPos {
+ pub x: i32,
+ pub z: i32,
+}
+
+impl ChunkPos {
+ pub fn new(x: i32, z: i32) -> Self {
+ ChunkPos { x, z }
+ }
+}
+
+impl From<&BlockPos> for ChunkPos {
+ fn from(pos: &BlockPos) -> Self {
+ ChunkPos {
+ x: pos.x.div_floor(16),
+ z: pos.z.div_floor(16),
+ }
+ }
+}
+
+/// The coordinates of a chunk section in the world.
+#[derive(Clone, Copy, Debug, Default)]
+pub struct ChunkSectionPos {
+ pub x: i32,
+ pub y: i32,
+ pub z: i32,
+}
+
+impl ChunkSectionPos {
+ pub fn new(x: i32, y: i32, z: i32) -> Self {
+ ChunkSectionPos { x, y, z }
+ }
+}
+
+impl From<BlockPos> for ChunkSectionPos {
+ fn from(pos: BlockPos) -> Self {
+ ChunkSectionPos {
+ x: pos.x.div_floor(16),
+ y: pos.y.div_floor(16),
+ z: pos.z.div_floor(16),
+ }
+ }
+}
+
+impl From<ChunkSectionPos> for ChunkPos {
+ fn from(pos: ChunkSectionPos) -> Self {
+ ChunkPos { x: pos.x, z: pos.z }
+ }
+}
+
+/// The coordinates of a block inside a chunk.
+#[derive(Clone, Copy, Debug, Default, PartialEq)]
+pub struct ChunkBlockPos {
+ pub x: u8,
+ pub y: i32,
+ pub z: u8,
+}
+
+impl ChunkBlockPos {
+ pub fn new(x: u8, y: i32, z: u8) -> Self {
+ ChunkBlockPos { x, y, z }
+ }
+}
+
+impl From<&BlockPos> for ChunkBlockPos {
+ fn from(pos: &BlockPos) -> Self {
+ ChunkBlockPos {
+ x: pos.x.rem_euclid(16).abs() as u8,
+ y: pos.y,
+ z: pos.z.rem_euclid(16).abs() as u8,
+ }
+ }
+}
+
+/// The coordinates of a block inside a chunk section.
+#[derive(Clone, Copy, Debug, Default)]
+pub struct ChunkSectionBlockPos {
+ /// A number between 0 and 16.
+ pub x: u8,
+ /// A number between 0 and 16.
+ pub y: u8,
+ /// A number between 0 and 16.
+ pub z: u8,
+}
+
+impl ChunkSectionBlockPos {
+ pub fn new(x: u8, y: u8, z: u8) -> Self {
+ ChunkSectionBlockPos { x, y, z }
+ }
+}
+
+impl From<&BlockPos> for ChunkSectionBlockPos {
+ fn from(pos: &BlockPos) -> Self {
+ ChunkSectionBlockPos {
+ x: pos.x.rem(16).abs() as u8,
+ y: pos.y.rem(16).abs() as u8,
+ z: pos.z.rem(16).abs() as u8,
+ }
+ }
+}
+
+impl From<&ChunkBlockPos> for ChunkSectionBlockPos {
+ fn from(pos: &ChunkBlockPos) -> Self {
+ ChunkSectionBlockPos {
+ x: pos.x,
+ y: pos.y.rem(16).abs() as u8,
+ z: pos.z,
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_from_block_pos_to_chunk_pos() {
+ let block_pos = BlockPos::new(5, 78, -2);
+ let chunk_pos = ChunkPos::from(&block_pos);
+ assert_eq!(chunk_pos, ChunkPos::new(0, -1));
+ }
+
+ #[test]
+ fn test_from_block_pos_to_chunk_block_pos() {
+ let block_pos = BlockPos::new(5, 78, -2);
+ let chunk_block_pos = ChunkBlockPos::from(&block_pos);
+ assert_eq!(chunk_block_pos, ChunkBlockPos::new(5, 78, 14));
+ }
+}