use crate::entity::Entity;
use crate::modern_state::{State, st};
use ghast_data::modern::{EntityTypeData, type_flags};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Kind {
Frame,
Painting,
Knot,
Shulker,
Interaction,
WindCharge,
}
#[inline]
pub(crate) fn kind(td: Option<&EntityTypeData>) -> Option<Kind> {
let t = td?;
if t.flags & type_flags::NO_BOX == 0 {
return None;
}
kind_of(t.name)
}
#[cold]
fn kind_of(name: &str) -> Option<Kind> {
Some(match name {
"item_frame" | "glow_item_frame" => Kind::Frame,
"painting" => Kind::Painting,
"leash_knot" => Kind::Knot,
"shulker" => Kind::Shulker,
"interaction" => Kind::Interaction,
"wind_charge" | "breeze_wind_charge" => Kind::WindCharge,
_ => return None,
})
}
const MAP: i32 = 1 << 24;
fn facing(m: &State) -> usize {
(m.slime_size & 7) as usize % 6
}
fn block(m: &State) -> [f64; 3] {
m.target
}
fn from_3d(v: i32) -> i32 {
(v % 6).abs()
}
const STEP: [[i32; 3]; 6] = [
[0, -1, 0],
[0, 1, 0],
[0, 0, -1],
[0, 0, 1],
[-1, 0, 0],
[1, 0, 0],
];
fn counter_clockwise(d: usize) -> usize {
match d {
2 => 4,
4 => 3,
3 => 5,
5 => 2,
other => other,
}
}
fn opposite(d: usize) -> usize {
d ^ 1
}
fn jmin(a: f64, b: f64) -> f64 {
if a.is_nan() {
return a;
}
if a == 0.0 && b == 0.0 && b.is_sign_negative() {
return b;
}
if a <= b { a } else { b }
}
fn jmax(a: f64, b: f64) -> f64 {
if a.is_nan() {
return a;
}
if a == 0.0 && b == 0.0 && a.is_sign_negative() {
return b;
}
if a >= b { a } else { b }
}
fn aabb(a: [f64; 3], b: [f64; 3]) -> [f64; 6] {
[
jmin(a[0], b[0]),
jmin(a[1], b[1]),
jmin(a[2], b[2]),
jmax(a[0], b[0]),
jmax(a[1], b[1]),
jmax(a[2], b[2]),
]
}
fn of_size(c: [f64; 3], s: [f64; 3]) -> [f64; 6] {
aabb(
[c[0] - s[0] / 2.0, c[1] - s[1] / 2.0, c[2] - s[2] / 2.0],
[c[0] + s[0] / 2.0, c[1] + s[1] / 2.0, c[2] + s[2] / 2.0],
)
}
fn center(b: [f64; 6]) -> [f64; 3] {
[
b[0] + 0.5 * (b[3] - b[0]),
b[1] + 0.5 * (b[4] - b[1]),
b[2] + 0.5 * (b[5] - b[2]),
]
}
fn relative(p: [f64; 3], d: usize, len: f64) -> [f64; 3] {
let s = STEP[d];
[
p[0] + len * f64::from(s[0]),
p[1] + len * f64::from(s[1]),
p[2] + len * f64::from(s[2]),
]
}
fn floor(v: f64) -> i32 {
let i = v as i32;
if v < f64::from(i) { i - 1 } else { i }
}
fn mth_sin(v: f64) -> f32 {
let i = (v * 10_430.378_350_470_453) as i64 & 0xFFFF;
(i as f64 * std::f64::consts::PI * 2.0 / 65536.0).sin() as f32
}
fn hanging_box(e: &Entity, k: Kind) -> [f64; 6] {
let m = &*e.m;
let b = block(m);
let d = facing(m);
let wall = relative([b[0] + 0.5, b[1] + 0.5, b[2] + 0.5], d, -0.46875);
let axis = STEP[d];
if k == Kind::Frame {
let s = if m.slime_size & MAP != 0 { 1.0 } else { 0.75 };
let size = |a: i32| if a != 0 { 0.0625 } else { s };
return of_size(wall, [size(axis[0]), size(axis[1]), size(axis[2])]);
}
let w = (m.slime_size >> 8) & 0xFF;
let h = (m.slime_size >> 16) & 0xFF;
let off = |n: i32| if n % 2 == 0 { 0.5 } else { 0.0 };
let p = relative(wall, counter_clockwise(d), off(w));
let p = relative(p, 1, off(h));
let (w, h) = (f64::from(w), f64::from(h));
of_size(
p,
[
if axis[0] != 0 { 0.0625 } else { w },
h,
if axis[2] != 0 { 0.0625 } else { w },
],
)
}
pub(crate) fn bounding_box(e: &Entity, k: Kind) -> Option<[f64; 6]> {
let [x, y, z] = e.pos;
let m = &*e.m;
Some(match k {
Kind::Frame | Kind::Painting => hanging_box(e, k),
Kind::Knot => {
let s = m.td?.size?.registered?;
let hw = f64::from(s.0) / 2.0;
let h = f64::from(s.1);
aabb([x - hw, y, z - hw], [x + hw, y + h, z + hw])
}
Kind::WindCharge => {
let s = m.td?.size?.registered?;
let w = f64::from(s.0 / 2.0);
let y0 = y - f64::from(0.15_f32);
aabb([x - w, y0, z - w], [x + w, y0 + f64::from(s.1), z + w])
}
Kind::Interaction => {
let w = f64::from(m.target[0] as f32 / 2.0);
let h = f64::from(m.target[1] as f32);
aabb([x - w, y, z - w], [x + w, y + h, z + w])
}
Kind::Shulker => {
let size = m.target[0] as f32;
let peek = m.target[1] as f32;
let phys = 0.5_f32 - mth_sin(f64::from((0.5_f32 + peek) * std::f32::consts::PI)) * 0.5;
let d = STEP[opposite(m.slime_size as usize % 6)];
let s = f64::from(size);
let mut lo = [-s * 0.5, 0.0, -s * 0.5];
let mut hi = [s * 0.5, s, s * 0.5];
let max = f64::from((-1.0_f32).max(phys));
for i in 0..3 {
let a = f64::from(d[i]) * max * s;
if a < 0.0 {
lo[i] += a;
} else if a > 0.0 {
hi[i] += a;
}
}
let b = aabb(lo, hi);
aabb(
[b[0] + x, b[1] + y, b[2] + z],
[b[3] + x, b[4] + y, b[5] + z],
)
}
})
}
const PAINTINGS: [(u8, u8); 51] = [
(1, 1), (1, 1), (1, 1), (3, 4), (2, 2), (1, 1), (3, 3), (4, 4), (2, 2), (3, 3), (4, 2), (3, 3), (2, 1), (2, 1), (3, 3), (4, 3), (2, 2), (3, 3), (3, 3), (4, 2), (4, 2), (2, 2), (1, 2), (2, 2), (1, 1), (4, 2), (2, 2), (1, 1), (4, 4), (3, 3), (4, 2), (4, 4), (1, 1), (4, 4), (3, 4), (2, 1), (1, 2), (2, 1), (4, 3), (2, 2), (2, 2), (3, 3), (2, 1), (3, 3), (4, 4), (2, 2), (1, 2), (1, 1), (2, 2), (2, 2), (2, 2), ];
pub(crate) fn painting_size(id: i32) -> Option<(u8, u8)> {
PAINTINGS.get(usize::try_from(id).ok()?).copied()
}
#[derive(Default, Clone, Copy)]
pub(crate) struct Keys {
pub(crate) direction: Option<i32>,
pub(crate) map: Option<bool>,
pub(crate) variant: Option<(u8, u8)>,
pub(crate) face: Option<i32>,
pub(crate) peek: Option<i8>,
pub(crate) width: Option<f32>,
pub(crate) height: Option<f32>,
}
impl Keys {
pub(crate) fn any(&self) -> bool {
self.direction.is_some()
|| self.map.is_some()
|| self.variant.is_some()
|| self.face.is_some()
|| self.peek.is_some()
|| self.width.is_some()
|| self.height.is_some()
}
}
#[cold]
#[inline(never)]
pub(crate) fn set_pos(e: &mut Entity, k: Kind, old: Option<[f64; 3]>) -> Option<[f64; 3]> {
match k {
Kind::Frame | Kind::Painting | Kind::Knot => {
let p = e.pos;
e.m.target = [
f64::from(floor(p[0])),
f64::from(floor(p[1])),
f64::from(floor(p[2])),
];
recalc(e, k)
}
Kind::Shulker => {
shulker_set_pos(e, old);
None
}
Kind::Interaction | Kind::WindCharge => None,
}
}
fn direction_rotation(e: &mut Entity, k: Kind) {
let d = facing(&e.m);
let horizontal = [None, None, Some(2), Some(0), Some(1), Some(3)][d];
match horizontal {
Some(h) => {
e.yaw = (h * 90) as f32;
if k == Kind::Frame {
e.pitch = 0.0;
}
}
None if k == Kind::Frame => {
e.pitch = if d == 1 { -90.0 } else { 90.0 };
e.yaw = 0.0;
}
None => {}
}
}
fn recalc(e: &mut Entity, k: Kind) -> Option<[f64; 3]> {
let b = block(&e.m);
if k == Kind::Knot {
e.pos = [b[0] + 0.5, b[1] + 0.375, b[2] + 0.5];
return None;
}
let c = center(hanging_box(e, k));
e.pos = c;
(k == Kind::Frame).then_some(c)
}
fn shulker_set_pos(e: &mut Entity, old: Option<[f64; 3]>) {
let raw = e.pos;
if e.vehicle().is_none() {
e.pos = [
f64::from(floor(raw[0])) + 0.5,
f64::from(floor(raw[1] + 0.5)),
f64::from(floor(raw[2])) + 0.5,
];
}
let block = |p: [f64; 3]| [floor(p[0]), floor(p[1]), floor(p[2])];
if let Some(o) = old
&& e.m.flag(st::TICKED)
&& block(o) != block(e.pos)
{
e.m.target[2] = 0.0;
}
e.m.target[0] = f64::from(e.m.scale);
}
#[cold]
#[inline(never)]
pub(crate) fn spawned(e: &mut Entity, k: Kind, data: i32) -> Option<[f64; 3]> {
match k {
Kind::Frame | Kind::Painting | Kind::Knot => {
let (w, h) = PAINTINGS[0];
e.m.slime_size = from_3d(data) | i32::from(w) << 8 | i32::from(h) << 16;
if k == Kind::Knot {
e.m.slime_size = 0;
} else {
if k == Kind::Painting {
e.yaw = 0.0;
e.pitch = 0.0;
}
direction_rotation(e, k);
}
}
Kind::Shulker => {
e.m.slime_size = 0; e.m.target = [f64::from(e.m.scale), 0.0, 0.0];
}
Kind::Interaction => e.m.target = [1.0, 1.0, 0.0],
Kind::WindCharge => {}
}
set_pos(e, k, None)
}
#[cold]
#[inline(never)]
pub(crate) fn metadata(e: &mut Entity, k: Kind, keys: &Keys, resize: bool) -> Option<[f64; 3]> {
match k {
Kind::Frame | Kind::Painting => {
let mut changed = false;
if let Some(d) = keys.direction {
e.m.slime_size = (e.m.slime_size & !7) | from_3d(d);
direction_rotation(e, k);
changed = true;
}
if let Some(m) = keys.map {
e.m.slime_size = (e.m.slime_size & !MAP) | if m { MAP } else { 0 };
changed = true;
}
if let Some((w, h)) = keys.variant {
e.m.slime_size =
(e.m.slime_size & !0x00FF_FF00) | i32::from(w) << 8 | i32::from(h) << 16;
changed = true;
}
if changed {
return recalc(e, k);
}
}
Kind::Shulker => {
if let Some(p) = keys.peek {
e.m.target[2] = f64::from(p);
}
if let Some(f) = keys.face {
e.m.slime_size = from_3d(f);
}
if keys.face.is_some() || resize {
shulker_set_pos(e, None);
}
}
Kind::Interaction => {
if let Some(w) = keys.width {
e.m.target[0] = f64::from(w);
}
if let Some(h) = keys.height {
e.m.target[1] = f64::from(h);
}
}
Kind::Knot | Kind::WindCharge => {}
}
None
}
#[cold]
#[inline(never)]
pub(crate) fn shulker_tick(e: &mut Entity) -> bool {
e.m.set_flag(st::TICKED, true);
let current = e.m.target[1] as f32;
let target = e.m.target[2] as f32 * 0.01;
if current == target {
return false;
}
let next = if current > target {
clamp_f(current - 0.05, target, 1.0)
} else {
clamp_f(current + 0.05, 0.0, target)
};
e.m.target[1] = f64::from(next);
shulker_set_pos(e, None);
true
}
fn clamp_f(v: f32, lo: f32, hi: f32) -> f32 {
if v < lo {
lo
} else if v.is_nan() || v <= hi {
v
} else {
hi
}
}
#[cold]
#[inline(never)]
pub(crate) fn rescaled(e: &mut Entity, k: Kind) {
if k == Kind::Shulker {
shulker_set_pos(e, None);
}
}