Skip to content

Commit

Permalink
Change coordinates, and add .get_block to ChunkBlocks
Browse files Browse the repository at this point in the history
- Renamed ChunkRelativeScalar to ChunkRelativeOffset
- Added more traits to Height and ChunkRelativeOffset
- Added .get_block to ChunkBlocks
  • Loading branch information
lukas0008 committed Aug 30, 2024
1 parent 0c79d38 commit 5f05a18
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 1 addition & 0 deletions pumpkin-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions pumpkin-world/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 21 additions & 19 deletions pumpkin-world/src/coordinates.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
}

Expand All @@ -29,37 +30,38 @@ impl<T: PrimInt + Signed> From<T> for Height {

assert!(height <= WORLD_MAX_Y);
assert!(height >= WORLD_LOWEST_Y);
Self { height }
Self(height)
}
}

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<T: PrimInt + Unsigned> From<T> for ChunkRelativeScalar {
impl<T: PrimInt + Unsigned> From<T> 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
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 5f05a18

Please sign in to comment.