use crate::common::{Aabb, EntityDimensions, VanillaEntitySize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BlockKind {
Other = 0,
Cobweb,
SweetBerryBush,
PowderSnow,
HoneyBlock,
BubbleColumn,
SlimeBlock,
Scaffolding,
Ladder,
Water,
Lava,
MovingPiston,
}
pub mod block_flags {
pub const CLIMBABLE: u32 = 1;
pub const FENCES: u32 = 2;
pub const WALLS: u32 = 4;
pub const FENCE_GATE: u32 = 8;
pub const TRAPDOOR: u32 = 16;
pub const ICE: u32 = 32;
pub const BED: u32 = 64;
pub const FALL_DAMAGE_RESETTING: u32 = 128;
pub const SOUL_SPEED_BLOCKS: u32 = 256;
pub const FIRE: u32 = 512;
pub const CAN_GLIDE_THROUGH: u32 = 1024;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VoxelClass {
Simple,
Array,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VoxelShapeRef {
pub class: VoxelClass,
pub sizes: [u8; 3],
pub xs: &'static [f64],
pub ys: &'static [f64],
pub zs: &'static [f64],
pub cells: &'static [u64],
pub empty: bool,
pub boxes: &'static [Aabb],
}
impl VoxelShapeRef {
#[must_use]
pub fn filled(&self, x: usize, y: usize, z: usize) -> bool {
let [nx, ny, nz] = self.sizes.map(usize::from);
if x >= nx || y >= ny || z >= nz {
return false;
}
let i = (x * ny + y) * nz + z;
self.cells[i / 64] & (1 << (i % 64)) != 0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct StatePhys(pub u64);
pub mod state_flags {
pub const FULL_CUBE: u16 = 1;
pub const EMPTY: u16 = 2;
pub const LARGE: u16 = 4;
pub const SUFFOCATING: u16 = 8;
pub const AIR: u16 = 16;
pub const BLOCKS_MOTION: u16 = 32;
pub const STURDY_NORTH: u16 = 64;
pub const CONTEXT: u16 = 1 << 10;
pub const OFFSET: u16 = 1 << 11;
pub const MOVING_PISTON: u16 = 1 << 12;
}
impl StatePhys {
#[inline]
#[must_use]
pub const fn shape(self) -> u16 {
self.0 as u16
}
#[inline]
#[must_use]
pub const fn flags(self) -> u16 {
(self.0 >> 16) as u16
}
#[inline]
#[must_use]
pub const fn has(self, bits: u16) -> bool {
self.flags() & bits == bits
}
#[inline]
#[must_use]
pub const fn fluid(self) -> u8 {
(self.0 >> 32) as u8
}
#[inline]
#[must_use]
pub const fn aux(self) -> u8 {
(self.0 >> 40) as u8
}
#[inline]
#[must_use]
pub const fn kind(self) -> u8 {
(self.0 >> 48) as u8
}
}
pub mod type_flags {
pub const LIVING: u8 = 1;
pub const PICKABLE: u8 = 2;
pub const NO_BOX: u8 = 4;
pub const FIXED: u8 = 8;
pub const MOB: u8 = 16;
pub const DEFAULT_BODY: u8 = 32;
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct EntityTypeData {
pub id: u16,
pub name: &'static str,
pub flags: u8,
pub interpolation: Option<u8>,
pub max_head_y_rot: u8,
pub fixed_poses: u32,
pub baby_meta: Option<u8>,
pub size_meta: Option<u8>,
pub flags_meta: Option<u8>,
pub size: Option<&'static VanillaEntitySize>,
}
impl EntityTypeData {
#[must_use]
pub fn is_living(&self) -> bool {
self.flags & type_flags::LIVING != 0
}
#[must_use]
pub fn is_pickable(&self) -> bool {
self.flags & type_flags::PICKABLE != 0
}
#[must_use]
pub fn dimensions(
&self,
variant: &str,
slime_size: Option<i32>,
pose: i32,
) -> Option<EntityDimensions> {
if self.flags & type_flags::NO_BOX != 0 {
return None;
}
let s = self.size?;
if pose == 2 && self.is_living() {
return Some(LIVING_SLEEPING);
}
let pose_name = POSES.get(pose as usize).copied().unwrap_or("standing");
if let Some(n) = slime_size {
return Some(scale(s.default_dimensions()?, n as f32, false));
}
if variant == "default" {
if let Some(d) = s.pose(pose_name) {
return Some(d);
}
return s.default_dimensions();
}
let mut key = [0u8; 40];
let k = join(&mut key, variant, pose_name);
if let Some(d) = k.and_then(|k| s.pose(k)) {
return Some(d);
}
s.variant(variant)
.map(|v| v.dimensions)
.or_else(|| s.default_dimensions())
}
#[must_use]
pub fn scaled(d: EntityDimensions, f: f32, fixed: bool) -> EntityDimensions {
scale(d, f, fixed)
}
}
pub const LIVING_SLEEPING: EntityDimensions = EntityDimensions {
width: 0.2,
height: 0.2,
eye_height: 0.2,
};
fn scale(d: EntityDimensions, f: f32, fixed: bool) -> EntityDimensions {
if fixed || f == 1.0 {
d
} else {
EntityDimensions {
width: d.width * f,
height: d.height * f,
eye_height: d.eye_height * f,
}
}
}
fn join<'a>(buf: &'a mut [u8; 40], a: &str, b: &str) -> Option<&'a str> {
let n = a.len() + 1 + b.len();
if n > buf.len() {
return None;
}
buf[..a.len()].copy_from_slice(a.as_bytes());
buf[a.len()] = b'/';
buf[a.len() + 1..n].copy_from_slice(b.as_bytes());
core::str::from_utf8(&buf[..n]).ok()
}
pub const POSES: [&str; 18] = [
"standing",
"fall_flying",
"sleeping",
"swimming",
"spin_attack",
"crouching",
"long_jumping",
"dying",
"croaking",
"using_tongue",
"sitting",
"roaring",
"sniffing",
"emerging",
"digging",
"sliding",
"shooting",
"inhaling",
];