Change coordinates, and add .get_block to ChunkBlocks

- Renamed ChunkRelativeScalar to ChunkRelativeOffset
- Added more traits to Height and ChunkRelativeOffset
- Added .get_block to ChunkBlocks
This commit is contained in:
lukas0008 2024-08-30 15:07:54 +02:00
parent 0c79d38de9
commit 5f05a1886b
5 changed files with 66 additions and 19 deletions

38
Cargo.lock generated
View File

@ -359,6 +359,15 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" 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]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.4" version = "0.9.4"
@ -604,6 +613,28 @@ dependencies = [
"powerfmt", "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]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -1974,6 +2005,7 @@ dependencies = [
name = "pumpkin-world" name = "pumpkin-world"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"derive_more",
"fastnbt", "fastnbt",
"fastsnbt", "fastsnbt",
"flate2", "flate2",
@ -2966,6 +2998,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-segmentation"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.13" version = "0.1.13"

View File

@ -16,3 +16,4 @@ log = "0.4"
tokio = { version = "1.39.2", features = ["net", "macros", "rt-multi-thread", "fs", "io-util", "sync"] } tokio = { version = "1.39.2", features = ["net", "macros", "rt-multi-thread", "fs", "io-util", "sync"] }
rayon = "1.10.0" rayon = "1.10.0"
uuid = { version = "1.10.0", features = ["serde", "v3"] } uuid = { version = "1.10.0", features = ["serde", "v3"] }
derive_more = { version = "1.0.0", features = ["full"] }

View File

@ -9,6 +9,7 @@ fastnbt = { git = "https://github.com/owengage/fastnbt.git" }
fastsnbt = "0.2" fastsnbt = "0.2"
tokio.workspace = true tokio.workspace = true
rayon.workspace = true rayon.workspace = true
derive_more.workspace = true
itertools = "0.13.0" itertools = "0.13.0"
thiserror = "1.0.63" thiserror = "1.0.63"
futures = "0.3.30" futures = "0.3.30"

View File

@ -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 /// Sets the given block in the chunk, returning the old block
pub fn set_block( pub fn set_block(
&mut self, &mut self,

View File

@ -1,15 +1,16 @@
use std::ops::Deref; use std::ops::Deref;
use derive_more::derive::{AsMut, AsRef, Display, Into};
use num_traits::{PrimInt, Signed, Unsigned}; use num_traits::{PrimInt, Signed, Unsigned};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{WORLD_LOWEST_Y, WORLD_MAX_Y}; 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)] #[serde(transparent)]
pub struct Height { pub struct Height(i16);
height: i16,
}
impl Height { impl Height {
pub fn from_absolute(height: u16) -> Self { pub fn from_absolute(height: u16) -> Self {
@ -19,7 +20,7 @@ impl Height {
/// Absolute height ranges from `0..WORLD_HEIGHT` /// Absolute height ranges from `0..WORLD_HEIGHT`
/// instead of `WORLD_LOWEST_Y..WORLD_MAX_Y` /// instead of `WORLD_LOWEST_Y..WORLD_MAX_Y`
pub fn get_absolute(self) -> u16 { 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<T: PrimInt + Signed> From<T> for Height {
assert!(height <= WORLD_MAX_Y); assert!(height <= WORLD_MAX_Y);
assert!(height >= WORLD_LOWEST_Y); assert!(height >= WORLD_LOWEST_Y);
Self { height } Self(height)
} }
} }
@ -37,29 +38,30 @@ impl Deref for Height {
type Target = i16; type Target = i16;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.height &self.0
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(
pub struct ChunkRelativeScalar { Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, AsRef, AsMut, Into, Display,
scalar: u8, )]
} #[repr(transparent)]
pub struct ChunkRelativeOffset(u8);
impl<T: PrimInt + Unsigned> From<T> for ChunkRelativeScalar { impl<T: PrimInt + Unsigned> From<T> for ChunkRelativeOffset {
fn from(scalar: T) -> Self { fn from(scalar: T) -> Self {
let scalar = scalar.to_u8().unwrap(); let scalar = scalar.to_u8().unwrap();
assert!(scalar < 16); assert!(scalar < 16);
Self { scalar } Self(scalar)
} }
} }
impl Deref for ChunkRelativeScalar { impl Deref for ChunkRelativeOffset {
type Target = u8; type Target = u8;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.scalar &self.0
} }
} }
@ -90,9 +92,9 @@ impl XZBlockCoordinates {
/// Coordinates of a block relative to a chunk /// Coordinates of a block relative to a chunk
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChunkRelativeBlockCoordinates { pub struct ChunkRelativeBlockCoordinates {
pub x: ChunkRelativeScalar, pub x: ChunkRelativeOffset,
pub y: Height, pub y: Height,
pub z: ChunkRelativeScalar, pub z: ChunkRelativeOffset,
} }
impl ChunkRelativeBlockCoordinates { impl ChunkRelativeBlockCoordinates {
@ -107,8 +109,8 @@ impl ChunkRelativeBlockCoordinates {
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChunkRelativeXZBlockCoordinates { pub struct ChunkRelativeXZBlockCoordinates {
pub x: ChunkRelativeScalar, pub x: ChunkRelativeOffset,
pub z: ChunkRelativeScalar, pub z: ChunkRelativeOffset,
} }
impl ChunkRelativeXZBlockCoordinates { impl ChunkRelativeXZBlockCoordinates {