Skip to main content

classicube_sys/
world.rs

1use core::slice;
2
3use crate::{
4    World,
5    bindings::{BlockID, BlockRaw},
6    std_types::c_int,
7};
8
9#[must_use]
10pub fn World_Pack(x: c_int, y: c_int, z: c_int) -> c_int {
11    (y * unsafe { World.Length } + z) * unsafe { World.Width } + x
12}
13
14/// # Panics
15///
16/// Panics if `(x, y, z)` lies outside the loaded world, producing a negative
17/// packed index or a masked id that does not fit in [`BlockID`].
18#[must_use]
19pub fn World_GetBlock(x: c_int, y: c_int, z: c_int) -> BlockID {
20    let i = usize::try_from(World_Pack(x, y, z)).expect("block index out of range");
21
22    let raw = (c_int::from(World_Blocks()[i]) | (c_int::from(World_Blocks2()[i]) << 8))
23        & unsafe { World.IDMask };
24    BlockID::try_from(raw).expect("masked block id exceeds BlockID range")
25}
26
27/// # Panics
28///
29/// Panics if `World.Volume` is negative.
30#[must_use]
31pub fn World_Blocks() -> &'static mut [BlockRaw] {
32    let volume = usize::try_from(unsafe { World.Volume }).expect("world volume out of range");
33    unsafe { slice::from_raw_parts_mut(World.Blocks, volume) }
34}
35
36/// # Panics
37///
38/// Panics if `World.Volume` is negative.
39#[must_use]
40pub fn World_Blocks2() -> &'static mut [BlockRaw] {
41    let volume = usize::try_from(unsafe { World.Volume }).expect("world volume out of range");
42    unsafe { slice::from_raw_parts_mut(World.Blocks2, volume) }
43}