use std::f64::consts::{PI, TAU};
use crate::ModernMap;
use ghast_entity::{Entity, EntityClass, EntityType, MetaValue};
use crate::protocol::{Looks, NO_COLOR, kind, look};
#[must_use]
pub fn yaw_rad(deg: f32) -> f32 {
(PI - f64::from(deg).to_radians()).rem_euclid(TAU) as f32
}
#[must_use]
pub fn pitch_rad(deg: f32) -> f32 {
((-f64::from(deg)).to_radians() + PI).rem_euclid(TAU) as f32 - PI as f32
}
const OBJECTS: &[(u8, &str, f32, f32)] = &[
(1, "Boat", 1.5, 0.6),
(2, "Item", 0.25, 0.25),
(10, "MinecartRideable", 0.98, 0.7),
(50, "PrimedTnt", 0.98, 0.98),
(51, "EnderCrystal", 2.0, 2.0),
(60, "Arrow", 0.5, 0.5),
(61, "Snowball", 0.25, 0.25),
(62, "ThrownEgg", 0.25, 0.25),
(63, "Fireball", 1.0, 1.0),
(64, "SmallFireball", 0.3125, 0.3125),
(65, "ThrownEnderpearl", 0.25, 0.25),
(66, "WitherSkull", 0.3125, 0.3125),
(70, "FallingSand", 0.98, 0.98),
(71, "ItemFrame", 0.5, 0.5),
(72, "EyeOfEnderSignal", 0.25, 0.25),
(73, "ThrownPotion", 0.25, 0.25),
(75, "ThrownExpBottle", 0.25, 0.25),
(76, "FireworksRocketEntity", 0.25, 0.25),
(77, "LeashKnot", 0.5, 0.5),
(78, "ArmorStand", 0.5, 1.975),
(90, "FishingHook", 0.25, 0.25),
];
const MINECARTS: [&str; 7] = [
"MinecartRideable",
"MinecartChest",
"MinecartFurnace",
"MinecartTNT",
"MinecartSpawner",
"MinecartHopper",
"MinecartCommandBlock",
];
#[derive(Debug, Clone, PartialEq)]
pub struct ObjectInfo {
pub name: String,
pub width: f32,
pub height: f32,
pub ty: u8,
pub data: i32,
}
#[must_use]
pub fn object_info(e: &Entity) -> Option<ObjectInfo> {
let EntityType::LegacyObject(ty) = e.entity_type() else {
return None;
};
let data = e.data();
Some(match OBJECTS.iter().find(|o| o.0 == ty) {
Some(&(_, name, w, h)) => ObjectInfo {
name: if ty == 10 {
usize::try_from(data)
.ok()
.and_then(|i| MINECARTS.get(i))
.copied()
.unwrap_or(name)
.to_string()
} else {
name.to_string()
},
width: w,
height: h,
ty,
data,
},
None => ObjectInfo {
name: format!("object{ty}"),
width: 0.5,
height: 0.5,
ty,
data,
},
})
}
fn default_modern() -> Option<&'static ModernMap> {
crate::modern_map(ghast_client::GameVersion::V26_1)
}
#[must_use]
pub fn name_of(e: &Entity) -> String {
name_of_in(e, default_modern())
}
pub(crate) fn name_of_in(e: &Entity, m: Option<&ModernMap>) -> String {
if let Some(o) = object_info(e) {
return o.name;
}
match (e.entity_type(), modern(e, m)) {
(EntityType::Player, _) => "player".into(),
(EntityType::ExperienceOrb, _) => "XPOrb".into(),
(EntityType::Painting, _) => "Painting".into(),
(t @ EntityType::Registry(_), Some(m)) => {
(m.info)(t).map_or_else(String::new, |i| (m.entity_name)(i.name))
}
(t, _) => ghast_entity::v1_8::info(t).map_or_else(String::new, |i| i.name.to_string()),
}
}
fn modern<'m>(e: &Entity, m: Option<&'m ModernMap>) -> Option<&'m ModernMap> {
m.filter(|_| matches!(e.entity_type(), EntityType::Registry(_)))
}
fn page_item(e: &Entity, m: Option<&ModernMap>, id: i32, damage: i16) -> (u16, u16) {
if let Some(m) = modern(e, m) {
return (m.item)(id, damage);
}
(id as u16, damage as u16)
}
#[must_use]
pub fn display_name(e: &Entity) -> &'static str {
display_name_in(e, default_modern())
}
pub(crate) fn display_name_in(e: &Entity, m: Option<&ModernMap>) -> &'static str {
match (e.entity_type(), modern(e, m)) {
(EntityType::LegacyMob(_), _) => {
ghast_entity::v1_8::info(e.entity_type()).map_or("", |i| i.display_name)
}
(EntityType::Registry(_), Some(m)) if e.class() == EntityClass::Mob => {
(m.info)(e.entity_type()).map_or("", |i| i.display_name)
}
_ => "",
}
}
#[must_use]
pub fn kind_of(e: &Entity) -> u8 {
match e.class() {
EntityClass::Player => kind::PLAYER,
EntityClass::ExperienceOrb => kind::ORB,
EntityClass::Item => kind::ITEM,
EntityClass::Mob => kind::MOB,
EntityClass::Object => kind::OBJECT,
EntityClass::Painting => kind::OTHER,
_ => kind::OTHER,
}
}
#[must_use]
pub fn size_of_entity(e: &Entity) -> (f32, f32) {
size_of_entity_in(e, default_modern())
}
pub(crate) fn size_of_entity_in(e: &Entity, m: Option<&ModernMap>) -> (f32, f32) {
if let Some(o) = object_info(e) {
return (o.width, o.height);
}
if e.class() == EntityClass::Player {
return (0.6, 1.8);
}
e.size()
.or_else(|| {
if let Some(m) = modern(e, m) {
return (m.info)(e.entity_type()).and_then(|i| Some((i.width?, i.height?)));
}
ghast_entity::v1_8::info(e.entity_type()).and_then(|i| Some((i.width?, i.height?)))
})
.unwrap_or((0.6, 1.8))
}
fn num(v: Option<&MetaValue>) -> i32 {
match v {
Some(MetaValue::Byte(b)) => i32::from(*b),
Some(MetaValue::Short(s)) => i32::from(*s),
Some(MetaValue::Int(i) | MetaValue::VarInt(i)) => *i,
Some(MetaValue::Float(f)) if f.is_finite() => *f as i32,
Some(MetaValue::Bool(b)) => i32::from(*b),
_ => 0,
}
}
#[must_use]
pub fn stack_of(e: &Entity) -> (u16, u16) {
stack_of_in(e, default_modern())
}
pub(crate) fn stack_of_in(e: &Entity, m: Option<&ModernMap>) -> (u16, u16) {
match e.class() {
EntityClass::ExperienceOrb => return (0, e.data().clamp(0, 0xffff) as u16),
EntityClass::Item => {
if let Some(m) = modern(e, m) {
return (m.item_stack)(e).map_or((0, 0), |st| (m.item)(st.id, 0));
}
return e
.item()
.filter(|it| it.id > 0)
.map_or((0, 0), |it| page_item(e, m, it.id, it.damage));
}
_ => {}
}
if let Some(o) = object_info(e) {
match o.ty {
70 => return ((o.data & 0xfff) as u16, ((o.data >> 12) & 15) as u16),
50 => return (46, 0),
_ => {}
}
}
e.equipment(0)
.filter(|it| it.id > 0)
.map_or((0, 0), |it| page_item(e, m, it.id, it.damage))
}
fn cart_block(name: &str) -> u16 {
match name {
"MinecartChest" => (54 << 4) | 2,
"MinecartFurnace" => (61 << 4) | 2,
"MinecartTNT" => 46 << 4,
"MinecartSpawner" => 52 << 4,
"MinecartHopper" => 154 << 4,
"MinecartCommandBlock" => 137 << 4,
_ => 0,
}
}
#[must_use]
pub fn mob_variant(name: &str, at: impl Fn(u8) -> i32) -> u16 {
let v = match name {
"Slime" | "LavaSlime" | "Villager" => at(16).clamp(0, 255),
"Sheep" => at(16) & 31,
"EntityHorse" => ((at(19) & 7) << 8) | (at(20) & 0xff),
"Skeleton" | "Zombie" => at(13) & 1,
"Wolf" => (at(16) & 7) | ((at(20) & 15) << 4),
"Ozelot" => at(18) & 3,
"Rabbit" => {
if at(18) == 99 {
99
} else {
at(18) & 7
}
}
"Bat" | "Ghast" => at(16) & 1,
"Guardian" => i32::from(at(16) & 4 != 0),
"ArmorStand" => at(10) & 31,
_ if name.starts_with("Minecart") => {
if at(22) != 0 {
let s = at(20);
(((s & 0xfff) << 4) | ((s >> 12) & 15)) & 0xffff
} else if name == "MinecartFurnace" && at(16) & 1 != 0 {
(62 << 4) | 2
} else {
i32::from(cart_block(name))
}
}
_ => 0,
};
v as u16
}
#[must_use]
pub fn variant_of(e: &Entity) -> u16 {
if matches!(e.entity_type(), EntityType::Registry(_))
|| (!matches!(e.class(), EntityClass::Mob) && object_info(e).is_none())
{
return 0;
}
let m = e.metadata();
mob_variant(&name_of(e), |i| num(m.get(i)))
}
const AGEABLE: [&str; 10] = [
"Pig",
"Sheep",
"Cow",
"MushroomCow",
"Chicken",
"Wolf",
"Ozelot",
"EntityHorse",
"Rabbit",
"Villager",
];
#[must_use]
pub fn leather_color(nbt: Option<&bytes::Bytes>) -> u32 {
nbt.and_then(|b| ghast_protocol::nbt::RawNbt::from_bytes(b.clone()).ok())
.map_or(NO_COLOR, |n| leather_color_nbt(&n))
}
#[must_use]
pub fn leather_color_nbt(nbt: &ghast_protocol::nbt::RawNbt) -> u32 {
use ghast_protocol::nbt::Tag;
let Ok(Some(root)) = nbt.parse() else {
return NO_COLOR;
};
let color = root
.tag
.as_compound()
.and_then(|c| c.get("display"))
.and_then(Tag::as_compound)
.and_then(|d| d.get("color"));
match color {
Some(Tag::Int(c)) => (*c as u32) & 0xff_ffff,
_ => NO_COLOR,
}
}
const STAND_POSE: [[f32; 3]; 6] = [
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[-10.0, 0.0, -10.0],
[-15.0, 0.0, 10.0],
[-1.0, 0.0, -1.0],
[1.0, 0.0, 1.0],
];
#[must_use]
pub fn looks_of(e: &Entity) -> Option<Looks> {
looks_of_in(e, default_modern())
}
pub(crate) fn looks_of_in(e: &Entity, mm: Option<&ModernMap>) -> Option<Looks> {
let name = name_of_in(e, mm);
let m = e.metadata();
let at = |i: u8| num(m.get(i));
if modern(e, mm).is_some() {
let mut armour = Vec::new();
for slot in 1..=4u8 {
if let Some(it) = e.equipment(usize::from(slot) + 1).filter(|it| it.id > 0) {
let (id, meta) = page_item(e, mm, it.id, it.damage);
armour.push((slot, id, meta, NO_COLOR));
}
}
let l = Looks {
flags: 0,
armour,
pose: None,
};
return (!l.is_plain()).then_some(l);
}
let baby = if AGEABLE.contains(&name.as_str()) {
at(12) < 0
} else if name == "Zombie" || name == "PigZombie" {
at(12) == 1
} else {
name == "ArmorStand" && at(10) & 1 != 0
};
let mut armour = Vec::new();
for slot in 1..=4u8 {
if let Some(it) = e.equipment(usize::from(slot)).filter(|it| it.id > 0) {
armour.push((
slot,
it.id as u16,
it.damage as u16,
leather_color(it.nbt.as_ref()),
));
}
}
let pose = (name == "ArmorStand").then(|| {
let mut p = [0f32; 18];
for (i, def) in STAND_POSE.iter().enumerate() {
let r = match m.get(11 + i as u8) {
Some(MetaValue::Rotation(r)) => *r,
_ => *def,
};
p[i * 3..i * 3 + 3].copy_from_slice(&r);
}
p
});
let l = Looks {
flags: if baby { look::BABY } else { 0 },
armour,
pose,
};
(!l.is_plain()).then_some(l)
}
#[must_use]
pub fn estate_flags(e: &Entity, sleeping: bool) -> u8 {
estate_flags_in(e, default_modern(), sleeping)
}
pub(crate) fn estate_flags_in(e: &Entity, m: Option<&ModernMap>, sleeping: bool) -> u8 {
use crate::protocol::eflag;
if let Some(m) = modern(e, m) {
let meta = (m.flags)(e);
return (meta & (eflag::FIRE | eflag::SNEAK | eflag::SPRINT | eflag::INVISIBLE))
| if sleeping { eflag::SLEEP } else { 0 };
}
let meta = ghast_entity::v1_8::flags(e);
(meta & (eflag::FIRE | eflag::SNEAK | eflag::SPRINT | eflag::USE | eflag::INVISIBLE))
| if sleeping { eflag::SLEEP } else { 0 }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn angles_match_the_page_convention() {
assert!((yaw_rad(0.0) - PI as f32).abs() < 1e-6);
assert!((yaw_rad(90.0) - (PI / 2.0) as f32).abs() < 1e-6);
assert!((yaw_rad(180.0)).abs() < 1e-6);
assert!((pitch_rad(90.0) + (PI / 2.0) as f32).abs() < 1e-6);
assert!((pitch_rad(-30.0) - (PI / 6.0) as f32).abs() < 1e-6);
}
}