#![allow(clippy::neg_cmp_op_on_partial_ord)]
use super::ext::PlayerExt;
use super::math::{V3, intersects};
use super::player::{PlayerState, on_climbable_ext};
use super::rules::Rules;
use crate::math::Aabb;
use crate::world::BlockView;
#[derive(Clone, Copy, Debug, Default)]
#[non_exhaustive]
pub struct Surroundings<'a> {
pub entities: &'a [Aabb],
}
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
}
}
#[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_26_1(name: &str, living: bool) -> Option<PushKind> {
if is_boat(name) {
return Some(PushKind::Boat);
}
match name {
"bat" | "parrot" | "armor_stand" => None,
_ if living => Some(PushKind::Living),
_ => None,
}
}
#[must_use]
pub fn collides_26_1(name: &str) -> bool {
is_boat(name) || name == "shulker"
}
fn is_boat(name: &str) -> bool {
name.ends_with("_boat") || name.ends_with("_raft")
}
pub fn push_by<R: Rules, W: BlockView<State = R::State>>(
p: &mut PlayerState,
x: &PlayerExt,
world: &W,
other: &Pusher,
) -> bool {
let area = match other.kind {
PushKind::Living => other.bb,
PushKind::Boat => super::math::inflate(
&other.bb,
f64::from(0.2f32),
f64::from(-0.01f32),
f64::from(0.2f32),
),
};
let b = p.bb;
if !intersects(
&b, area.min_x, area.min_y, area.min_z, area.max_x, area.max_y, area.max_z,
) {
return false;
}
if other.kind == PushKind::Boat && !section_query_reaches(&area, p.pos) {
return false;
}
if p.spectator || on_climbable_ext::<R, W>(p, x, world) {
return false;
}
if other.kind == PushKind::Boat && !(b.min_y <= other.bb.min_y) {
return false;
}
let mut xa = p.pos[0] - other.pos[0];
let mut za = p.pos[2] - other.pos[2];
let mut dd = xa.abs().max(za.abs());
if !(dd >= f64::from(0.01f32)) {
return false;
}
dd = dd.sqrt();
xa /= dd;
za /= dd;
let pow = (1.0 / dd).min(1.0);
xa *= pow;
za *= pow;
xa *= f64::from(0.05f32);
za *= f64::from(0.05f32);
if !(xa.is_finite() && za.is_finite()) {
return false;
}
let v = V3::of(p.vel).add(V3::new(xa, 0.0, za));
if v.is_finite() {
p.vel = v.arr();
}
true
}
#[must_use]
pub fn section_query_reaches(area: &Aabb, pos: [f64; 3]) -> bool {
use crate::math::floor;
let sec = |v: f64| floor(v) >> 4;
let (sx, sy, sz) = (sec(pos[0]), sec(pos[1]), sec(pos[2]));
sec(area.min_x - 2.0) <= sx
&& sx <= sec(area.max_x + 2.0)
&& sec(area.min_y - 4.0) <= sy
&& sy <= sec(area.max_y + 0.0)
&& sec(area.min_z - 2.0) <= sz
&& sz <= sec(area.max_z + 2.0)
}