use ghast_data::FluidKind;
use ghast_data::v1_16_5::{Block, BlockState};
use super::blocks::{fluid, slipperiness, state_at};
use super::body::{self, Body, Health, horizontal_sqr, mul};
use super::fluid::V3;
use super::projectile::ProjectileHit;
use crate::math::{Aabb, floor, java_min};
use crate::nonliving::LegacyRandom;
pub use crate::nonliving::Side;
pub use crate::orb::OrbPlayer;
use crate::world::{BlockView, WorldBorder};
#[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::PrimedTnt => 0.15,
_ => self.dimensions().1 * 0.85,
}
}
}
#[derive(Clone, Debug)]
pub struct Drop {
kind: DropKind,
b: Body,
id: i32,
age: i32,
fuse: i32,
time: i32,
block: BlockState,
cleared: Option<[i32; 3]>,
last_hit: Option<ProjectileHit>,
target: Option<OrbPlayer>,
clock: i32,
last_search: i32,
}
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::new(pos[0], pos[1], pos[2]),
V3::new(vel[0], vel[1], vel[2]),
w,
h,
kind.eye_height(),
);
b.health_model = match kind {
DropKind::Item => Health::Item,
DropKind::ExperienceOrb => Health::Orb,
_ => Health::None,
};
b.splashes = kind != DropKind::ExperienceOrb;
Drop {
kind,
b,
id: 0,
age: 0,
fuse: 80,
time: 0,
block: Block::SAND.default_state(),
cleared: None,
last_hit: None,
target: None,
clock: 0,
last_search: 0,
}
}
#[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: f32) -> Drop {
self.b.fall = fall;
self
}
#[must_use]
pub fn with_no_gravity(mut self, no_gravity: bool) -> Drop {
self.b.no_gravity = no_gravity;
self
}
#[must_use]
pub fn with_fire_immune(mut self, fire_immune: bool) -> Drop {
self.b.fire_immune = fire_immune;
self
}
#[must_use]
pub fn with_fuse(mut self, fuse: i32) -> Drop {
self.fuse = fuse;
self
}
#[must_use]
pub fn with_block(mut self, block: BlockState) -> Drop {
self.block = block;
self
}
pub fn set_pos(&mut self, pos: [f64; 3]) {
self.b.set_position(V3::new(pos[0], pos[1], pos[2]));
}
pub fn set_vel(&mut self, vel: [f64; 3]) {
self.b.vel = V3::new(vel[0], vel[1], vel[2]);
}
pub fn set_on_ground(&mut self, on_ground: bool) {
self.b.on_ground = on_ground;
}
pub fn set_fuse(&mut self, fuse: i32) {
self.fuse = fuse;
}
pub fn set_no_gravity(&mut self, no_gravity: bool) {
self.b.no_gravity = no_gravity;
}
pub fn set_fire_immune(&mut self, fire_immune: bool) {
self.b.fire_immune = fire_immune;
}
#[must_use]
pub fn kind(&self) -> DropKind {
self.kind
}
#[must_use]
pub fn pos(&self) -> [f64; 3] {
[self.b.pos.x, self.b.pos.y, self.b.pos.z]
}
#[must_use]
pub fn vel(&self) -> [f64; 3] {
[self.b.vel.x, self.b.vel.y, self.b.vel.z]
}
#[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) -> f32 {
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 cleared_block(&self) -> Option<[i32; 3]> {
self.cleared
}
#[must_use]
pub fn last_hit(&self) -> Option<ProjectileHit> {
self.last_hit
}
#[must_use]
pub fn no_physics(&self) -> bool {
self.b.no_clip
}
#[must_use]
pub fn removed(&self) -> bool {
self.b.removed
}
#[must_use]
pub fn at_rest(&self) -> bool {
self.b.on_ground && horizontal_sqr(self.b.vel) < 1.0e-12 && self.b.vel.y.abs() < 1.0e-12
}
}
struct Cleared<'a, W> {
w: &'a W,
cell: Option<[i32; 3]>,
}
impl<W: BlockView<State = BlockState>> BlockView for Cleared<'_, W> {
type State = BlockState;
#[inline]
fn block(&self, x: i32, y: i32, z: i32) -> BlockState {
if self.cell == Some([x, y, z]) {
BlockState::AIR
} else {
self.w.block(x, y, z)
}
}
#[inline]
fn is_chunk_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
self.w.is_chunk_loaded(chunk_x, chunk_z)
}
#[inline]
fn world_border(&self) -> WorldBorder {
self.w.world_border()
}
}
pub fn tick_drop<W: BlockView<State = BlockState>>(d: &mut Drop, side: Side, world: &W) {
tick_drop_near(d, side, world, &[]);
}
pub fn tick_drop_near<W: BlockView<State = BlockState>>(
d: &mut Drop,
side: Side,
world: &W,
players: &[OrbPlayer],
) {
if d.b.removed {
return;
}
d.last_hit = None;
match d.kind {
DropKind::Item => item(d, side, world),
DropKind::ExperienceOrb => orb(d, side, world, players),
DropKind::PrimedTnt => tnt(d, world),
DropKind::FallingBlock => falling(d, side, world),
}
}
pub fn run_drop<W: BlockView<State = BlockState>>(
d: &mut Drop,
side: Side,
world: &W,
max_ticks: u32,
) -> u32 {
for t in 1..=max_ticks {
tick_drop(d, side, world);
if d.b.removed || d.at_rest() {
return t;
}
}
max_ticks
}
fn ground_friction<W: BlockView<State = BlockState>>(b: &Body, w: &W) -> f32 {
let s = state_at(w, floor(b.pos.x), floor(b.pos.y - 1.0), floor(b.pos.z));
slipperiness(s) * 0.98
}
fn lava_at<W: BlockView<State = BlockState>>(w: &W, p: [i32; 3]) -> bool {
fluid(state_at(w, p[0], p[1], p[2])).is_some_and(|f| f.kind == FluidKind::Lava)
}
fn item<W: BlockView<State = BlockState>>(d: &mut Drop, side: Side, w: &W) {
let server = side == Side::Server;
let b = &mut d.b;
b.start_tick();
body::base_tick(b, w, side);
b.old = b.pos;
let f = f64::from(b.eye_height - 0.111_111_11f32);
let v = b.vel;
let up = |y: f64| {
y + f64::from(if y < f64::from(0.06f32) {
5.0e-4f32
} else {
0.0
})
};
if b.in_water && b.water_height > f {
let k = f64::from(0.99f32);
b.vel = V3::new(v.x * k, up(v.y), v.z * k);
} else if b.in_lava() && b.lava_height > f {
let k = f64::from(0.95f32);
b.vel = V3::new(v.x * k, up(v.y), v.z * k);
} else if !b.no_gravity {
b.vel = b.vel.add(V3::new(0.0, -0.04, 0.0));
}
if server {
b.no_clip = !body::space_empty_for(b, w);
if b.no_clip {
let (x, y, z) = (b.pos.x, (b.bb.min_y + b.bb.max_y) / 2.0, b.pos.z);
body::push_out_of_blocks(b, w, x, y, z);
}
} else {
b.no_clip = false;
}
if !b.on_ground
|| horizontal_sqr(b.vel) > f64::from(1.0e-5f32)
|| (b.age.wrapping_add(d.id)) % 4 == 0
{
let v = b.vel;
body::entity_move(b, w, v);
let g = if b.on_ground {
ground_friction(b, w)
} else {
0.98
};
b.vel = mul(b.vel, f64::from(g), 0.98, f64::from(g));
if b.on_ground && b.vel.y < 0.0 {
b.vel = mul(b.vel, 1.0, -0.5, 1.0);
}
}
let moved = floor(b.old.x) != floor(b.pos.x)
|| floor(b.old.y) != floor(b.pos.y)
|| floor(b.old.z) != floor(b.pos.z);
let i = if moved { 2 } else { 40 };
if b.age % i == 0 && lava_at(w, b.block_pos()) && !b.fire_immune {
b.rng.next_float();
}
if d.age != -32768 {
d.age += 1;
}
body::update_water_state(b, w);
if server && d.age >= 6000 {
b.removed = true;
}
}
fn orb<W: BlockView<State = BlockState>>(d: &mut Drop, side: Side, w: &W, players: &[OrbPlayer]) {
let b = &mut d.b;
b.start_tick();
body::base_tick(b, w, side);
b.old = b.pos;
if b.eye_in_water {
let v = b.vel;
let k = f64::from(0.99f32);
b.vel = V3::new(
v.x * k,
java_min(v.y + f64::from(5.0e-4f32), f64::from(0.06f32)),
v.z * k,
);
} else if !b.no_gravity {
b.vel = b.vel.add(V3::new(0.0, -0.03, 0.0));
}
if lava_at(w, b.block_pos()) {
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));
b.rng.next_float();
}
if !body::space_empty(w, &b.bb) {
let (x, y, z) = (b.pos.x, (b.bb.min_y + b.bb.max_y) / 2.0, b.pos.z);
body::push_out_of_blocks(b, w, x, y, z);
}
let pos = [b.pos.x, b.pos.y, b.pos.z];
if let Some(t) = crate::orb::legacy_target(
&mut d.target,
&mut d.last_search,
d.clock,
d.id,
players,
pos,
) {
let to = V3::new(
t.pos[0] - pos[0],
t.pos[1] + f64::from(t.eye_height) / 2.0 - pos[1],
t.pos[2] - pos[2],
);
let e = to.length_sq();
if e < 64.0 {
let f = 1.0 - e.sqrt() / 8.0;
b.vel = b.vel.add(to.normalize().scale(f * f * 0.1));
}
}
let v = b.vel;
body::entity_move(b, w, v);
let g = if b.on_ground {
ground_friction(b, w)
} else {
0.98
};
b.vel = mul(b.vel, f64::from(g), 0.98, f64::from(g));
if b.on_ground {
b.vel = mul(b.vel, 1.0, -0.9, 1.0);
}
d.clock = d.clock.wrapping_add(1);
d.age += 1;
if d.age >= 6000 {
b.removed = true;
}
}
fn tnt<W: BlockView<State = BlockState>>(d: &mut Drop, w: &W) {
let b = &mut d.b;
b.start_tick();
if !b.no_gravity {
b.vel = b.vel.add(V3::new(0.0, -0.04, 0.0));
}
let v = b.vel;
body::entity_move(b, w, v);
b.vel = b.vel.scale(0.98);
if b.on_ground {
b.vel = mul(b.vel, 0.7, -0.5, 0.7);
}
d.fuse -= 1;
if d.fuse <= 0 {
b.removed = true;
} else {
body::update_water_state(b, w);
}
}
fn falling<W: BlockView<State = BlockState>>(d: &mut Drop, side: Side, world: &W) {
let server = side == Side::Server;
if d.block.is_air() {
d.b.removed = true;
return;
}
let b = &mut d.b;
b.start_tick();
let first = d.time == 0;
d.time += 1;
if first {
let p = b.block_pos();
if state_at(world, p[0], p[1], p[2]).block() == d.block.block() {
d.cleared = Some(p);
} else if server {
b.removed = true;
return;
}
}
let w = &Cleared {
w: world,
cell: d.cleared,
};
if !b.no_gravity {
b.vel = b.vel.add(V3::new(0.0, -0.04, 0.0));
}
let v = b.vel;
body::entity_move(b, w, v);
if server {
let mut p = b.block_pos();
let concrete = d.block.block().name().ends_with("_concrete_powder");
let water_at = |p: [i32; 3]| {
fluid(state_at(w, p[0], p[1], p[2])).is_some_and(|f| f.kind == FluidKind::Water)
};
let mut in_water = concrete && water_at(p);
if concrete && b.vel.length_sq() > 1.0 {
let ctx = b.ctx();
let hit = body::clip(w, b.old, b.pos, &ctx, true);
d.last_hit = super::projectile::to_hit(hit);
if hit.is_block() && water_at(hit.pos) {
p = hit.pos;
in_water = true;
}
}
if b.on_ground || in_water {
let here = state_at(w, p[0], p[1], p[2]);
b.vel = mul(b.vel, 0.7, -0.5, 0.7);
if here.block() != Block::MOVING_PISTON {
b.removed = true;
}
} else if (d.time > 100 && (p[1] < 1 || p[1] > 256)) || d.time > 600 {
b.removed = true;
}
}
b.vel = b.vel.scale(0.98);
}