#![allow(clippy::neg_cmp_op_on_partial_ord)]
use super::player::{PlayerState, is_climbing};
use crate::math::{Aabb, BoxOps};
use crate::world::BlockView;
use ghast_data::v1_12::BlockState;
#[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" | "parrot" | "armor_stand" | "ender_dragon" => None,
"player" => Some(PushKind::Living),
_ if living => Some(PushKind::Living),
_ => None,
}
}
#[must_use]
pub fn collides(name: &str) -> bool {
matches!(name, "boat" | "shulker")
}
pub fn lily_pads_broken_by_boat<W: BlockView<State = BlockState>>(
world: &W,
bb: &Aabb,
out: &mut Vec<[i32; 3]>,
) {
use crate::math::floor;
let (x0, y0, z0) = (
floor(bb.min_x + 0.001),
floor(bb.min_y + 0.001),
floor(bb.min_z + 0.001),
);
let (x1, y1, z1) = (
floor(bb.max_x - 0.001),
floor(bb.max_y - 0.001),
floor(bb.max_z - 0.001),
);
for x in x0..=x1 {
for y in y0..=y1 {
for z in z0..=z1 {
if super::blocks::state_at(world, x, y, z).block().id() == LILY_PAD {
out.push([x, y, z]);
}
}
}
}
}
const LILY_PAD: u8 = 111;
pub fn push_by<W: BlockView<State = BlockState>>(
p: &mut PlayerState,
world: &W,
other: &Pusher,
) -> bool {
let area = match other.kind {
PushKind::Living => other.bb,
PushKind::Boat => other
.bb
.grow(f64::from(0.2f32), f64::from(-0.01f32), f64::from(0.2f32)),
};
if !p.bb.intersects(&area) {
return false;
}
if p.spectator || p.no_clip || is_climbing(p, world) {
return false;
}
let (dx, dz) = match other.kind {
PushKind::Living => (other.pos[0] - p.pos[0], other.pos[2] - p.pos[2]),
PushKind::Boat => {
if !(p.bb.min_y <= other.bb.min_y) {
return false;
}
(p.pos[0] - other.pos[0], p.pos[2] - other.pos[2])
}
};
let Some((x, z)) = push_away(dx, dz) else {
return false;
};
match other.kind {
PushKind::Living => {
p.vel[0] += -x;
p.vel[2] += -z;
}
PushKind::Boat => {
p.vel[0] += x;
p.vel[2] += z;
}
}
p.velocity_dirty = true;
true
}
fn push_away(mut dx: f64, mut dz: f64) -> Option<(f64, f64)> {
let mut f = abs_max(dx, dz);
if !(f >= f64::from(0.01f32)) {
return None;
}
f = f64::from(f.sqrt() as f32);
dx /= f;
dz /= f;
let mut g = 1.0 / f;
if g > 1.0 {
g = 1.0;
}
dx *= g;
dz *= g;
dx *= f64::from(0.05f32);
dz *= f64::from(0.05f32);
Some((dx, dz))
}
fn abs_max(a: f64, b: f64) -> f64 {
let a = if a < 0.0 { -a } else { a };
let b = if b < 0.0 { -b } else { b };
if a > b { a } else { b }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn push_kinds() {
assert_eq!(push_kind("zombie", true), Some(PushKind::Living));
assert_eq!(push_kind("player", false), Some(PushKind::Living));
assert_eq!(push_kind("boat", false), Some(PushKind::Boat));
for n in ["bat", "parrot", "armor_stand", "ender_dragon"] {
assert_eq!(push_kind(n, true), None, "{n}");
}
assert_eq!(push_kind("minecart", false), None);
assert!(collides("boat") && collides("shulker") && !collides("minecart"));
}
}