From 5f05a1886b8ac832407cf2fbb47f5684ee2a886e Mon Sep 17 00:00:00 2001 From: lukas0008 Date: Fri, 30 Aug 2024 15:07:54 +0200 Subject: [PATCH] Change coordinates, and add .get_block to ChunkBlocks - Renamed ChunkRelativeScalar to ChunkRelativeOffset - Added more traits to Height and ChunkRelativeOffset - Added .get_block to ChunkBlocks --- Cargo.lock | 38 ++++++++++++++++++++++++++++++ Cargo.toml | 1 + pumpkin-world/Cargo.toml | 1 + pumpkin-world/src/chunk.rs | 5 ++++ pumpkin-world/src/coordinates.rs | 40 +++++++++++++++++--------------- 5 files changed, 66 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7ce270de..1b8d17cb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -359,6 +359,15 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -604,6 +613,28 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "syn 2.0.76", + "unicode-xid", +] + [[package]] name = "digest" version = "0.10.7" @@ -1974,6 +2005,7 @@ dependencies = [ name = "pumpkin-world" version = "0.1.0" dependencies = [ + "derive_more", "fastnbt", "fastsnbt", "flate2", @@ -2966,6 +2998,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" + [[package]] name = "unicode-width" version = "0.1.13" diff --git a/Cargo.toml b/Cargo.toml index 2306175fd..3fbea597f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ log = "0.4" tokio = { version = "1.39.2", features = ["net", "macros", "rt-multi-thread", "fs", "io-util", "sync"] } rayon = "1.10.0" uuid = { version = "1.10.0", features = ["serde", "v3"] } +derive_more = { version = "1.0.0", features = ["full"] } diff --git a/pumpkin-world/Cargo.toml b/pumpkin-world/Cargo.toml index 516711d8d..4008eaa84 100644 --- a/pumpkin-world/Cargo.toml +++ b/pumpkin-world/Cargo.toml @@ -9,6 +9,7 @@ fastnbt = { git = "https://github.com/owengage/fastnbt.git" } fastsnbt = "0.2" tokio.workspace = true rayon.workspace = true +derive_more.workspace = true itertools = "0.13.0" thiserror = "1.0.63" futures = "0.3.30" diff --git a/pumpkin-world/src/chunk.rs b/pumpkin-world/src/chunk.rs index 8bf95e63c..2e7ba0d2c 100644 --- a/pumpkin-world/src/chunk.rs +++ b/pumpkin-world/src/chunk.rs @@ -131,6 +131,11 @@ impl ChunkBlocks { } } + /// Gets the given block in the chunk + pub fn get_block(&self, position: ChunkRelativeBlockCoordinates) -> BlockId { + self.blocks[Self::convert_index(position)] + } + /// Sets the given block in the chunk, returning the old block pub fn set_block( &mut self, diff --git a/pumpkin-world/src/coordinates.rs b/pumpkin-world/src/coordinates.rs index 93056f531..450734429 100644 --- a/pumpkin-world/src/coordinates.rs +++ b/pumpkin-world/src/coordinates.rs @@ -1,15 +1,16 @@ use std::ops::Deref; +use derive_more::derive::{AsMut, AsRef, Display, Into}; use num_traits::{PrimInt, Signed, Unsigned}; use serde::{Deserialize, Serialize}; use crate::{WORLD_LOWEST_Y, WORLD_MAX_Y}; -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] +#[derive( + Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, AsRef, AsMut, Into, Display, +)] #[serde(transparent)] -pub struct Height { - height: i16, -} +pub struct Height(i16); impl Height { pub fn from_absolute(height: u16) -> Self { @@ -19,7 +20,7 @@ impl Height { /// Absolute height ranges from `0..WORLD_HEIGHT` /// instead of `WORLD_LOWEST_Y..WORLD_MAX_Y` pub fn get_absolute(self) -> u16 { - (self.height + WORLD_LOWEST_Y.abs()) as u16 + (self.0 + WORLD_LOWEST_Y.abs()) as u16 } } @@ -29,7 +30,7 @@ impl From for Height { assert!(height <= WORLD_MAX_Y); assert!(height >= WORLD_LOWEST_Y); - Self { height } + Self(height) } } @@ -37,29 +38,30 @@ impl Deref for Height { type Target = i16; fn deref(&self) -> &Self::Target { - &self.height + &self.0 } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct ChunkRelativeScalar { - scalar: u8, -} +#[derive( + Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, AsRef, AsMut, Into, Display, +)] +#[repr(transparent)] +pub struct ChunkRelativeOffset(u8); -impl From for ChunkRelativeScalar { +impl From for ChunkRelativeOffset { fn from(scalar: T) -> Self { let scalar = scalar.to_u8().unwrap(); assert!(scalar < 16); - Self { scalar } + Self(scalar) } } -impl Deref for ChunkRelativeScalar { +impl Deref for ChunkRelativeOffset { type Target = u8; fn deref(&self) -> &Self::Target { - &self.scalar + &self.0 } } @@ -90,9 +92,9 @@ impl XZBlockCoordinates { /// Coordinates of a block relative to a chunk #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct ChunkRelativeBlockCoordinates { - pub x: ChunkRelativeScalar, + pub x: ChunkRelativeOffset, pub y: Height, - pub z: ChunkRelativeScalar, + pub z: ChunkRelativeOffset, } impl ChunkRelativeBlockCoordinates { @@ -107,8 +109,8 @@ impl ChunkRelativeBlockCoordinates { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct ChunkRelativeXZBlockCoordinates { - pub x: ChunkRelativeScalar, - pub z: ChunkRelativeScalar, + pub x: ChunkRelativeOffset, + pub z: ChunkRelativeOffset, } impl ChunkRelativeXZBlockCoordinates {