use crate::common::{EntityDimensions, EntityInfo, VanillaEntitySize, lookup_name};
use crate::generated::v26_1 as g;
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;
}
pub const LIVING_SLEEPING: EntityDimensions = EntityDimensions {
width: 0.2,
height: 0.2,
eye_height: 0.2,
};
pub const PLAYER: u16 = 155;
pub const ITEM: u16 = 71;
pub const EXPERIENCE_ORB: u16 = 49;
pub const PAINTING: u16 = 93;
pub const ARMOR_STAND: u16 = 5;
#[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)
}
}
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",
];
#[must_use]
pub fn entities() -> &'static [EntityInfo] {
&g::entities::ENTITIES
}
#[must_use]
pub fn entity(id: u16) -> Option<&'static EntityInfo> {
g::entities::ENTITIES.get(usize::from(id))
}
#[must_use]
pub fn entity_by_name(name: &str) -> Option<&'static EntityInfo> {
lookup_name(&g::entities::ENTITY_NAMES, name).map(|i| &g::entities::ENTITIES[usize::from(i)])
}
#[inline]
#[must_use]
pub fn entity_type(id: u16) -> Option<&'static EntityTypeData> {
g::entities::TYPES.get(usize::from(id))
}
#[must_use]
pub fn entity_types() -> &'static [EntityTypeData] {
&g::entities::TYPES
}
#[must_use]
pub fn vanilla_entity_sizes() -> &'static [VanillaEntitySize] {
&g::entities::VANILLA_ENTITY_SIZES
}
#[must_use]
pub fn vanilla_entity_size(name: &str) -> Option<&'static VanillaEntitySize> {
g::entities::VANILLA_ENTITY_SIZES
.binary_search_by(|e| e.name.cmp(name))
.ok()
.map(|i| &g::entities::VANILLA_ENTITY_SIZES[i])
}