pub use super::body::Side;
use super::body::{self, Body, InsideRules, LegacyRandom, MoveRules, with_env};
use super::fluid::kind;
use super::math::{V3, deflate, java_min, mv};
use super::rules::Rules;
use crate::math::Aabb;
pub use crate::orb::OrbPlayer;
use crate::world::BlockView;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DropKind {
Item,
ExperienceOrb,
PrimedTnt,
FallingBlock,
}
impl DropKind {
#[must_use]
pub const fn entity_name(self) -> &'static str {
match self {
DropKind::Item => "item",
DropKind::ExperienceOrb => "experience_orb",
DropKind::PrimedTnt => "tnt",
DropKind::FallingBlock => "falling_block",
}
}
#[must_use]
pub fn from_entity_name(name: &str) -> Option<DropKind> {
let name = name.strip_prefix("minecraft:").unwrap_or(name);
[
DropKind::Item,
DropKind::ExperienceOrb,
DropKind::PrimedTnt,
DropKind::FallingBlock,
]
.into_iter()
.find(|k| k.entity_name() == name)
}
#[must_use]
pub const fn dimensions(self) -> (f32, f32) {
match self {
DropKind::Item => (0.25, 0.25),
DropKind::ExperienceOrb => (0.5, 0.5),
DropKind::PrimedTnt | DropKind::FallingBlock => (0.98, 0.98),
}
}
fn eye_height(self) -> f32 {
match self {
DropKind::Item => 0.2125,
DropKind::PrimedTnt => 0.15,
_ => self.dimensions().1 * 0.85,
}
}
#[must_use]
pub const fn gravity(self) -> f64 {
match self {
DropKind::ExperienceOrb => 0.03,
_ => 0.04,
}
}
}
#[derive(Clone, Debug)]
pub struct Drop {
kind: DropKind,
b: Body,
id: i32,
age: i32,
fuse: i32,
time: i32,
target: Option<OrbPlayer>,
}
impl Drop {
#[must_use]
pub fn new(kind: DropKind, pos: [f64; 3], vel: [f64; 3]) -> Drop {
let (w, h) = kind.dimensions();
let mut b = Body::new(V3::of(pos), V3::of(vel), w, h, kind.eye_height());
if matches!(kind, DropKind::Item | DropKind::ExperienceOrb) {
b.below_offset = 0.999_999;
}
Drop {
kind,
b,
id: 0,
age: 0,
fuse: 80,
time: 0,
target: None,
}
}
#[must_use]
pub fn following(&self) -> Option<i32> {
self.target.map(|t| t.id)
}
#[must_use]
pub fn with_id(mut self, id: i32) -> Drop {
self.id = id;
self
}
#[must_use]
pub fn with_random_seed(mut self, seed: i64) -> Drop {
self.b.rng = LegacyRandom::new(seed);
self
}
#[must_use]
pub fn with_on_ground(mut self, on_ground: bool) -> Drop {
self.b.on_ground = on_ground;
self
}
#[must_use]
pub fn with_fall_distance(mut self, fall: f64) -> Drop {
self.b.fall = fall;
self
}
#[must_use]
pub fn with_fuse(mut self, fuse: i32) -> Drop {
self.fuse = fuse;
self
}
pub fn set_pos(&mut self, pos: [f64; 3]) {
self.b.set_pos(V3::of(pos));
}
pub fn set_vel(&mut self, vel: [f64; 3]) {
self.b.vel = V3::of(vel);
}
pub fn set_on_ground(&mut self, on_ground: bool) {
self.b.on_ground = on_ground;
}
pub fn set_interpolation_start(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) {
self.b.pending_old = Some((V3::of(pos), yaw, pitch));
}
#[must_use]
pub fn moved_last_tick(&self) -> &[[f64; 3]] {
&self.b.moved[..usize::from(self.b.moved_n)]
}
pub fn set_fuse(&mut self, fuse: i32) {
self.fuse = fuse;
}
#[must_use]
pub fn kind(&self) -> DropKind {
self.kind
}
#[must_use]
pub fn pos(&self) -> [f64; 3] {
self.b.pos.arr()
}
#[must_use]
pub fn vel(&self) -> [f64; 3] {
self.b.vel.arr()
}
#[must_use]
pub fn bounding_box(&self) -> Aabb {
self.b.bb
}
#[must_use]
pub fn on_ground(&self) -> bool {
self.b.on_ground
}
#[must_use]
pub fn in_water(&self) -> bool {
self.b.in_water
}
#[must_use]
pub fn horizontal_collision(&self) -> bool {
self.b.hcol
}
#[must_use]
pub fn vertical_collision(&self) -> bool {
self.b.vcol
}
#[must_use]
pub fn fall_distance(&self) -> f64 {
self.b.fall
}
#[must_use]
pub fn age(&self) -> i32 {
self.age
}
#[must_use]
pub fn fuse(&self) -> i32 {
self.fuse
}
#[must_use]
pub fn time(&self) -> i32 {
self.time
}
#[must_use]
pub fn no_physics(&self) -> bool {
self.b.no_physics
}
#[must_use]
pub fn removed(&self) -> bool {
self.b.removed
}
#[must_use]
pub fn at_rest(&self) -> bool {
self.b.on_ground && self.b.vel.horizontal_sqr() < 1.0e-12 && self.b.vel.y.abs() < 1.0e-12
}
}
const ENTITY_INSIDE: InsideRules = InsideRules {
projectile_bubbles: false,
no_bubbles: false,
};
pub fn tick_drop<R: Rules, W: BlockView<State = R::State>>(d: &mut Drop, side: Side, world: &W) {
tick_drop_near::<R, W>(d, side, world, &[]);
}
pub fn tick_drop_near<R: Rules, W: BlockView<State = R::State>>(
d: &mut Drop,
side: Side,
world: &W,
players: &[OrbPlayer],
) {
if d.b.removed {
return;
}
let server = side == Side::Server;
let mr = MoveRules {
authoritative: server,
simulate: server || R::SIMULATED_ON_CLIENT,
gravity: d.kind.gravity(),
air_drag: 0.98,
};
with_env::<R, W, _>(world, |w| match d.kind {
DropKind::Item => item::<R, W>(d, server, mr, w),
DropKind::ExperienceOrb => orb::<R, W>(d, server, mr, w, players),
DropKind::PrimedTnt => tnt::<R, W>(d, mr, w),
DropKind::FallingBlock => falling::<R, W>(d, mr, w),
});
}
pub fn run_drop<R: Rules, W: BlockView<State = R::State>>(
d: &mut Drop,
side: Side,
world: &W,
max_ticks: u32,
) -> u32 {
for t in 1..=max_ticks {
tick_drop::<R, W>(d, side, world);
if d.b.removed || d.at_rest() {
return t;
}
}
max_ticks
}
fn air_drag<R: Rules>() -> f64 {
if R::FLOAT_AIR_DRAG {
f64::from(0.98f32)
} else {
0.98
}
}
fn item<R: Rules, W: BlockView<State = R::State>>(
d: &mut Drop,
server: bool,
mr: MoveRules,
w: &body::Env<'_, R, W>,
) {
let b = &mut d.b;
b.start_tick();
body::base_tick::<R, W>(b, w, true, true);
b.old = b.pos;
if b.in_water && b.water_height > f64::from(0.1f32) {
fluid_movement(b, 0.99);
} else if b.in_lava() && b.lava_height > f64::from(0.1f32) {
fluid_movement(b, 0.95);
} else {
b.apply_gravity(0.04);
}
if server {
let probe = deflate(&b.bb, 1.0e-7);
let ctx = b.ctx();
b.no_physics = !w.no_collision(b.pos.arr(), &probe, &ctx);
if b.no_physics {
let y = (b.bb.min_y + b.bb.max_y) / 2.0;
let (x, z) = (b.pos.x, b.pos.z);
body::move_towards_closest_space::<R, W>(b, w, x, y, z);
}
} else {
b.no_physics = false;
}
let resting = b.on_ground
&& (b.vel.horizontal_sqr() <= f64::from(1.0e-5f32) || b.vel.horizontal_sqr().is_nan())
&& (b.tick_count.wrapping_add(d.id)) % 4 != 0;
if resting {
body::apply_effects_last::<R, W>(b, w, ENTITY_INSIDE);
} else {
let v = b.vel;
body::entity_move::<R, W>(b, w, v, mr);
body::apply_effects_from_blocks::<R, W>(b, w, ENTITY_INSIDE);
let drag = 0.98f32;
let ground = if b.on_ground {
let p = body::on_pos::<R, W>(b, w, b.below_offset);
drag * R::friction(w.state(p[0], p[1], p[2]))
} else {
drag
};
b.vel = b
.vel
.mul(f64::from(ground), air_drag::<R>(), f64::from(ground));
if b.on_ground && b.vel.y < 0.0 {
b.vel = b.vel.mul(1.0, -0.5, 1.0);
}
}
if d.age != -32768 {
d.age += 1;
}
body::update_fluids::<R, W>(b, w, true, true);
if server && d.age >= 6000 {
b.removed = true;
}
}
fn fluid_movement(b: &mut Body, k: f32) {
let v = b.vel;
let k = f64::from(k);
let up = if v.y < f64::from(0.06f32) {
f64::from(5.0e-4f32)
} else {
0.0
};
b.vel = V3::new(v.x * k, v.y + up, v.z * k);
}
fn orb<R: Rules, W: BlockView<State = R::State>>(
d: &mut Drop,
server: bool,
mr: MoveRules,
w: &body::Env<'_, R, W>,
players: &[OrbPlayer],
) {
let b = &mut d.b;
b.start_tick();
if b.first_tick && !server {
b.first_tick = false;
return;
}
body::base_tick::<R, W>(b, w, true, false);
let colliding = !body::no_collision_empty::<R, W>(w, &b.bb);
if b.eye_in_water {
let v = b.vel;
b.vel = V3::new(
v.x * f64::from(0.99f32),
java_min(v.y + f64::from(5.0e-4f32), f64::from(0.06f32)),
v.z * f64::from(0.99f32),
);
} else if !colliding {
b.apply_gravity(0.03);
}
let bp = b.block_pos();
if kind(w.fluid(bp[0], bp[1], bp[2])) == 2 {
let x = (b.rng.next_float() - b.rng.next_float()) * 0.2;
let z = (b.rng.next_float() - b.rng.next_float()) * 0.2;
b.vel = V3::new(f64::from(x), f64::from(0.2f32), f64::from(z));
}
follow_nearby_player(b, &mut d.target, players);
if server && d.target.is_none() && colliding {
let next = mv(&b.bb, b.vel.x, b.vel.y, b.vel.z);
if !body::no_collision_empty::<R, W>(w, &next) {
let y = (b.bb.min_y + b.bb.max_y) / 2.0;
let (x, z) = (b.pos.x, b.pos.z);
body::move_towards_closest_space::<R, W>(b, w, x, y, z);
}
}
let fall_speed = b.vel.y;
let v = b.vel;
body::entity_move::<R, W>(b, w, v, mr);
body::apply_effects_from_blocks::<R, W>(b, w, ENTITY_INSIDE);
let friction = if b.on_ground {
let p = body::on_pos::<R, W>(b, w, b.below_offset);
R::friction(w.state(p[0], p[1], p[2])) * 0.98f32
} else {
0.98f32
};
b.vel = b.vel.scale(f64::from(friction));
if b.vcol_below && fall_speed < -0.03 {
b.vel = V3::new(b.vel.x, -fall_speed * 0.4, b.vel.z);
}
d.age += 1;
if d.age >= 6000 {
b.removed = true;
}
}
fn follow_nearby_player(b: &mut Body, target: &mut Option<OrbPlayer>, players: &[OrbPlayer]) {
use crate::orb::{dist_sqr, nearest, refresh};
refresh(target, players);
let p = b.pos;
if target.is_none_or(|t| t.spectator || dist_sqr(&t, p.x, p.y, p.z) > 64.0) {
*target = nearest(players, p.x, p.y, p.z, 64.0).filter(|n| !n.dead);
}
let Some(t) = *target else {
return;
};
let (x, y, z) = (
t.pos[0] - p.x,
t.pos[1] + f64::from(t.eye_height) / 2.0 - p.y,
t.pos[2] - p.z,
);
let length = x * x + y * y + z * z;
let power = 1.0 - length.sqrt() / 8.0;
let d = (x * x + y * y + z * z).sqrt();
let (nx, ny, nz) = if d < f64::from(1.0e-5f32) {
(0.0, 0.0, 0.0)
} else {
(x / d, y / d, z / d)
};
let k = power * power * 0.1;
b.vel = V3::new(b.vel.x + nx * k, b.vel.y + ny * k, b.vel.z + nz * k);
}
fn tnt<R: Rules, W: BlockView<State = R::State>>(
d: &mut Drop,
mr: MoveRules,
w: &body::Env<'_, R, W>,
) {
let b = &mut d.b;
b.start_tick();
b.apply_gravity(0.04);
let v = b.vel;
body::entity_move::<R, W>(b, w, v, mr);
body::apply_effects_from_blocks::<R, W>(b, w, ENTITY_INSIDE);
b.vel = b.vel.scale(air_drag::<R>());
if b.on_ground {
b.vel = b.vel.mul(0.7, -0.5, 0.7);
}
d.fuse -= 1;
if d.fuse <= 0 {
b.removed = true;
} else {
body::update_fluids::<R, W>(b, w, true, true);
}
}
fn falling<R: Rules, W: BlockView<State = R::State>>(
d: &mut Drop,
mr: MoveRules,
w: &body::Env<'_, R, W>,
) {
let b = &mut d.b;
b.start_tick();
d.time += 1;
b.apply_gravity(0.04);
let v = b.vel;
body::entity_move::<R, W>(b, w, v, mr);
body::apply_effects_from_blocks::<R, W>(b, w, ENTITY_INSIDE);
b.vel = b.vel.scale(air_drag::<R>());
}