valence/src/block_pos.rs

133 lines
3.2 KiB
Rust
Raw Normal View History

2022-04-15 07:55:45 +10:00
use std::io::{Read, Write};
use anyhow::bail;
2022-07-03 03:27:54 +10:00
use vek::Vec3;
2022-04-15 07:55:45 +10:00
2022-07-11 22:08:02 +10:00
use crate::protocol_inner::{Decode, Encode};
2022-04-15 07:55:45 +10:00
2022-07-11 22:08:02 +10:00
/// Represents an absolute block position in a world.
2022-04-15 07:55:45 +10:00
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Debug)]
2022-04-29 17:48:41 +10:00
pub struct BlockPos {
pub x: i32,
pub y: i32,
pub z: i32,
2022-04-15 07:55:45 +10:00
}
2022-04-29 17:48:41 +10:00
impl BlockPos {
2022-07-11 22:08:02 +10:00
/// Constructs a new block position.
2022-04-29 17:48:41 +10:00
pub const fn new(x: i32, y: i32, z: i32) -> Self {
Self { x, y, z }
2022-04-15 07:55:45 +10:00
}
2022-07-03 03:27:54 +10:00
2022-07-11 22:08:02 +10:00
/// Returns the block position a point is contained within.
2022-07-03 03:27:54 +10:00
pub fn at(pos: impl Into<Vec3<f64>>) -> Self {
pos.into().floor().as_::<i32>().into()
}
2022-04-15 07:55:45 +10:00
}
impl Encode for BlockPos {
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
2022-04-29 17:48:41 +10:00
match (self.x, self.y, self.z) {
2022-04-15 07:55:45 +10:00
(-0x2000000..=0x1ffffff, -0x800..=0x7ff, -0x2000000..=0x1ffffff) => {
2022-04-29 17:48:41 +10:00
let (x, y, z) = (self.x as u64, self.y as u64, self.z as u64);
2022-04-15 07:55:45 +10:00
(x << 38 | z << 38 >> 26 | y & 0xfff).encode(w)
}
_ => bail!("out of range: {self:?}"),
2022-04-15 07:55:45 +10:00
}
}
}
impl Decode for BlockPos {
fn decode(r: &mut impl Read) -> anyhow::Result<Self> {
// Use arithmetic right shift to determine sign.
let val = i64::decode(r)?;
let x = val >> 38;
let z = val << 26 >> 38;
let y = val << 52 >> 52;
2022-04-29 17:48:41 +10:00
Ok(Self {
x: x as i32,
y: y as i32,
z: z as i32,
})
2022-04-15 07:55:45 +10:00
}
}
impl From<(i32, i32, i32)> for BlockPos {
fn from((x, y, z): (i32, i32, i32)) -> Self {
BlockPos::new(x, y, z)
}
}
2022-06-23 01:06:54 +10:00
impl From<BlockPos> for (i32, i32, i32) {
fn from(pos: BlockPos) -> Self {
(pos.x, pos.y, pos.z)
}
}
impl From<[i32; 3]> for BlockPos {
fn from([x, y, z]: [i32; 3]) -> Self {
BlockPos::new(x, y, z)
}
}
2022-06-23 01:06:54 +10:00
impl From<BlockPos> for [i32; 3] {
fn from(pos: BlockPos) -> Self {
[pos.x, pos.y, pos.z]
}
}
2022-07-03 03:27:54 +10:00
impl From<Vec3<i32>> for BlockPos {
fn from(pos: Vec3<i32>) -> Self {
Self::new(pos.x, pos.y, pos.z)
}
}
impl From<BlockPos> for Vec3<i32> {
fn from(pos: BlockPos) -> Self {
Vec3::new(pos.x, pos.y, pos.z)
}
}
2022-04-15 07:55:45 +10:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn position() {
let xzs = [
(-33554432, true),
(-33554433, false),
(33554431, true),
(33554432, false),
(0, true),
(1, true),
(-1, true),
];
let ys = [
(-2048, true),
(-2049, false),
(2047, true),
(2048, false),
(0, true),
(1, true),
(-1, true),
];
let mut buf = [0; 8];
for (x, x_valid) in xzs {
for (y, y_valid) in ys {
for (z, z_valid) in xzs {
2022-04-29 17:48:41 +10:00
let pos = BlockPos::new(x, y, z);
2022-04-15 07:55:45 +10:00
if x_valid && y_valid && z_valid {
pos.encode(&mut &mut buf[..]).unwrap();
assert_eq!(BlockPos::decode(&mut &buf[..]).unwrap(), pos);
} else {
assert!(pos.encode(&mut &mut buf[..]).is_err());
}
}
}
}
}
}