#![allow(clippy::neg_cmp_op_on_partial_ord)]
use ghast_data::v1_16_5::BlockState;
use super::player::{PlayerState, is_climbing};
use crate::math::{Aabb, java_max, java_min};
use crate::voxel::EPSILON;
use crate::world::BlockView;
#[derive(Clone, Copy, Debug, Default)]
#[non_exhaustive]
pub struct Surroundings<'a> {
pub entities: &'a [Aabb],
pub others: &'a [OtherEntity],
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct OtherEntity {
pub bb: Aabb,
pub living: bool,
}
impl OtherEntity {
#[must_use]
pub fn new(bb: Aabb, living: bool) -> OtherEntity {
OtherEntity { bb, living }
}
}
impl<'a> Surroundings<'a> {
#[must_use]
pub fn new() -> Surroundings<'a> {
Surroundings::default()
}
#[must_use]
pub fn with_entities(mut self, entities: &'a [Aabb]) -> Surroundings<'a> {
self.entities = entities;
self
}
#[must_use]
pub fn with_others(mut self, others: &'a [OtherEntity]) -> Surroundings<'a> {
self.others = others;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PushKind {
Living,
Boat,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Pusher {
pub pos: [f64; 3],
pub bb: Aabb,
pub kind: PushKind,
}
impl Pusher {
#[must_use]
pub fn new(pos: [f64; 3], bb: Aabb, kind: PushKind) -> Pusher {
Pusher { pos, bb, kind }
}
}
#[must_use]
pub fn push_kind(name: &str, living: bool) -> Option<PushKind> {
match name {
"boat" => Some(PushKind::Boat),
"bat" | "armor_stand" | "parrot" | "ender_dragon" | "vex" => None,
_ if living => Some(PushKind::Living),
_ => None,
}
}
#[must_use]
pub fn collides(name: &str) -> bool {
matches!(name, "boat" | "shulker")
}
#[must_use]
pub fn step_height(name: &str, living: bool) -> f32 {
match name {
"iron_golem" | "horse" | "donkey" | "mule" | "skeleton_horse" | "zombie_horse"
| "llama" | "trader_llama" | "enderman" | "drowned" | "turtle" | "ravager" => 1.0,
"pig" | "strider" => 0.5,
"armor_stand" => 0.0,
_ if living => 0.6,
_ => 0.0,
}
}
#[must_use]
pub fn climbing_at<W: BlockView<State = BlockState>>(world: &W, pos: [f64; 3]) -> bool {
super::blocks::is_climbable_at(
world,
crate::math::floor(pos[0]),
crate::math::floor(pos[1]),
crate::math::floor(pos[2]),
)
}
fn is_minecart(name: &str) -> bool {
name == "minecart" || name.ends_with("_minecart")
}
#[must_use]
pub fn pushable(name: &str, living: bool) -> bool {
match name {
"boat" => true,
"bat" | "armor_stand" => false,
_ if is_minecart(name) => true,
_ => living,
}
}
#[must_use]
pub fn hits_pushable(name: &str) -> bool {
name == "boat" || is_minecart(name)
}
#[inline]
fn overlaps(a: &Aabb, b: &Aabb) -> bool {
a.min_x < b.max_x
&& a.max_x > b.min_x
&& a.min_y < b.max_y
&& a.max_y > b.min_y
&& a.min_z < b.max_z
&& a.max_z > b.min_z
}
pub fn push_by<W: BlockView<State = BlockState>>(
p: &mut PlayerState,
filed: [f64; 3],
world: &W,
other: &Pusher,
) -> bool {
let area = match other.kind {
PushKind::Living => other.bb,
PushKind::Boat => {
let (h, v) = (f64::from(0.2f32), f64::from(-0.01f32));
super::collide::bx(
other.bb.min_x - h,
other.bb.min_y - v,
other.bb.min_z - h,
other.bb.max_x + h,
other.bb.max_y + v,
other.bb.max_z + h,
)
}
};
if !overlaps(&p.bb, &area) || !query_reaches(&area, filed) {
return false;
}
if p.spectator || is_climbing(p, world) {
return false;
}
if other.kind == PushKind::Boat && !(p.bb.min_y <= other.bb.min_y) {
return false;
}
let (mut d, mut e) = match other.kind {
PushKind::Living => (other.pos[0] - p.pos[0], other.pos[2] - p.pos[2]),
PushKind::Boat => (p.pos[0] - other.pos[0], p.pos[2] - other.pos[2]),
};
let mut f = d.abs().max(e.abs());
if !(f >= f64::from(0.01f32)) {
return false;
}
f = f64::from(f.sqrt() as f32);
d /= f;
e /= f;
let mut g = 1.0 / f;
if g > 1.0 {
g = 1.0;
}
d *= g;
e *= g;
d *= f64::from(0.05f32);
e *= f64::from(0.05f32);
if other.kind == PushKind::Living {
(d, e) = (-d, -e);
}
p.vel[0] += d;
p.vel[1] += 0.0;
p.vel[2] += e;
true
}
#[must_use]
pub fn query_reaches(area: &Aabb, filed: [f64; 3]) -> bool {
use crate::math::floor;
let chunk = |v: f64| floor(v / 16.0);
let section = |v: f64| floor(v / 16.0).clamp(0, 15);
let (cx, cz) = (chunk(filed[0]), chunk(filed[2]));
let sy = section(filed[1]);
chunk(area.min_x - 2.0) <= cx
&& cx <= chunk(area.max_x + 2.0)
&& chunk(area.min_z - 2.0) <= cz
&& cz <= chunk(area.max_z + 2.0)
&& section(area.min_y - 2.0) <= sy
&& sy <= section(area.max_y + 2.0)
}
#[inline]
pub(crate) fn average_side(b: &Aabb) -> f64 {
let x = b.max_x - b.min_x;
let y = b.max_y - b.min_y;
let z = b.max_z - b.min_z;
(x + y + z) / 3.0
}
#[inline]
pub(crate) fn collision_hit(query: &Aabb, e: &Aabb) -> bool {
if average_side(query) < EPSILON {
return false;
}
let q = super::collide::expand(query, EPSILON);
overlaps(&q, e)
}
#[inline]
fn lo(b: &Aabb, axis: usize) -> f64 {
[b.min_x, b.min_y, b.min_z][axis]
}
#[inline]
fn hi(b: &Aabb, axis: usize) -> f64 {
[b.max_x, b.max_y, b.max_z][axis]
}
#[inline]
pub(crate) fn cuboid_max_distance(s: &Aabb, axis: usize, b: &Aabb, max: f64) -> f64 {
if max.abs() < EPSILON {
return 0.0;
}
let (a2, a3) = ((axis + 1) % 3, (axis + 2) % 3);
let across = |a: usize| lo(b, a) + EPSILON < hi(s, a) && hi(b, a) - EPSILON >= lo(s, a);
if !(across(a2) && across(a3)) {
return max;
}
if max > 0.0 {
if hi(b, axis) - EPSILON < lo(s, axis) {
return java_min(max, lo(s, axis) - hi(b, axis));
}
max
} else if max < 0.0 {
if lo(b, axis) + EPSILON >= hi(s, axis) {
return java_max(max, hi(s, axis) - lo(b, axis));
}
max
} else {
max
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::voxel::{Points, VoxelShape};
#[test]
fn cuboid_matches_the_generic_shape() {
let mut seed = 0x1234_5678_9abc_def0_u64;
let mut next = || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
(seed >> 11) as f64 / (1u64 << 53) as f64
};
for _ in 0..200_000 {
let (x0, y0, z0) = (
(next() - 0.5) * 6.0,
(next() - 0.5) * 6.0 + 64.0,
(next() - 0.5) * 6.0,
);
let s = Aabb::new(
x0,
y0,
z0,
x0 + next() * 2.0 + 0.01,
y0 + next() * 2.0 + 0.01,
z0 + next() * 2.0 + 0.01,
);
let (bx0, by0, bz0) = (
(next() - 0.5) * 6.0,
(next() - 0.5) * 6.0 + 64.0,
(next() - 0.5) * 6.0,
);
let b = Aabb::new(bx0, by0, bz0, bx0 + 0.6, by0 + 1.8, bz0 + 0.6);
let xs = [s.min_x, s.max_x];
let ys = [s.min_y, s.max_y];
let zs = [s.min_z, s.max_z];
let shape = VoxelShape {
axes: [
Points::Explicit(&xs),
Points::Explicit(&ys),
Points::Explicit(&zs),
],
cells: &[1],
};
for axis in 0..3 {
for max in [
(next() - 0.5) * 6.0,
-((next() - 0.5) * 6.0).abs(),
((next() - 0.5) * 6.0).abs(),
0.0,
1e-8,
-1e-8,
] {
let want = shape.max_distance(axis, &b, max, None);
let got = cuboid_max_distance(&s, axis, &b, max);
assert_eq!(want.to_bits(), got.to_bits(), "{s:?} {b:?} {axis} {max}");
}
}
}
}
}