use ghast_data::v1_12::BlockState;
use super::blocks::{id, is_air, is_lava, is_water, slipperiness, state_at};
use super::body::{self, Body, Health, Water, nid};
use super::projectile::{ProjectileHit, ray, to_hit};
use crate::math::{Aabb, floor};
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),
}
}
}
#[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(pos, vel, w, h);
b.health_model = match kind {
DropKind::Item | DropKind::ExperienceOrb => Health::Five,
_ => Health::None,
};
b.water = match kind {
DropKind::Item => Water::Item,
DropKind::ExperienceOrb => Water::Orb,
_ => Water::Entity,
};
Drop {
kind,
b,
id: 0,
age: 0,
fuse: 80,
time: 0,
block: ghast_data::v1_12::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(self, _fire_immune: bool) -> Drop {
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(pos);
}
pub fn set_vel(&mut self, vel: [f64; 3]) {
self.b.vel = vel;
}
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;
}
#[must_use]
pub fn kind(&self) -> DropKind {
self.kind
}
#[must_use]
pub fn pos(&self) -> [f64; 3] {
self.b.pos
}
#[must_use]
pub fn vel(&self) -> [f64; 3] {
self.b.vel
}
#[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 {
let v = self.b.vel;
self.b.on_ground && v[0] * v[0] + v[2] * v[2] < 1.0e-12 && v[1].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),
}
d.b.end_tick();
}
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[0]), floor(b.bb.min_y) - 1, floor(b.pos[2]));
slipperiness(s) * 0.98
}
fn lava_hop(b: &mut Body) {
b.vel[1] = f64::from(0.2f32);
b.vel[0] = f64::from((b.rng.next_float() - b.rng.next_float()) * 0.2);
b.vel[2] = f64::from((b.rng.next_float() - b.rng.next_float()) * 0.2);
b.rng.next_float();
}
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;
if !b.no_gravity {
b.vel[1] -= f64::from(0.04f32);
}
b.no_clip = if server {
let (x, y, z) = (b.pos[0], (b.bb.min_y + b.bb.max_y) / 2.0, b.pos[2]);
body::push_out_of_blocks(b, w, x, y, z)
} else {
false
};
let v = b.vel;
body::entity_move(b, w, v);
let moved = (0..3).any(|a| b.old[a] as i32 != b.pos[a] as i32);
if (moved || b.age % 25 == 0)
&& is_lava(state_at(
w,
floor(b.pos[0]),
floor(b.pos[1]),
floor(b.pos[2]),
))
{
lava_hop(b);
}
let g = if b.on_ground {
ground_friction(b, w)
} else {
0.98
};
b.vel[0] *= f64::from(g);
b.vel[1] *= f64::from(0.98f32);
b.vel[2] *= f64::from(g);
if b.on_ground {
b.vel[1] *= -0.5;
}
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.no_gravity {
b.vel[1] -= f64::from(0.03f32);
}
if is_lava(state_at(
w,
floor(b.pos[0]),
floor(b.pos[1]),
floor(b.pos[2]),
)) {
lava_hop(b);
}
let (x, y, z) = (b.pos[0], (b.bb.min_y + b.bb.max_y) / 2.0, b.pos[2]);
body::push_out_of_blocks(b, w, x, y, z);
if let Some(t) = crate::orb::legacy_target(
&mut d.target,
&mut d.last_search,
d.clock,
d.id,
players,
b.pos,
) {
crate::orb::legacy_pull(&mut b.vel, b.pos, &t, true);
}
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[0] *= f64::from(g);
b.vel[1] *= f64::from(0.98f32);
b.vel[2] *= f64::from(g);
if b.on_ground {
b.vel[1] *= f64::from(-0.9f32);
}
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[1] -= f64::from(0.04f32);
}
let v = b.vel;
body::entity_move(b, w, v);
for v in &mut b.vel {
*v *= f64::from(0.98f32);
}
if b.on_ground {
b.vel[0] *= f64::from(0.7f32);
b.vel[2] *= f64::from(0.7f32);
b.vel[1] *= -0.5;
}
d.fuse -= 1;
if d.fuse <= 0 {
b.removed = true;
} else {
body::update_water_state(b, w);
}
}
fn can_fall_through(s: BlockState) -> bool {
s.block().id() == nid::FIRE || is_air(s) || is_water(s) || is_lava(s)
}
fn falling<W: BlockView<State = BlockState>>(d: &mut Drop, side: Side, world: &W) {
let server = side == Side::Server;
if is_air(d.block) {
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().id() == d.block.block().id() {
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[1] -= f64::from(0.04f32);
}
let v = b.vel;
body::entity_move(b, w, v);
if server {
let mut p = b.block_pos();
let concrete = d.block.block().id() == nid::CONCRETE_POWDER;
let mut in_water = concrete && is_water(state_at(w, p[0], p[1], p[2]));
let v = b.vel;
if concrete && v[0] * v[0] + v[1] * v[1] + v[2] * v[2] > 1.0 {
let hit = ray(w, b.old, b.pos, true, false);
d.last_hit = hit.map(to_hit);
if let Some(h) = hit
&& is_water(state_at(w, h.pos[0], h.pos[1], h.pos[2]))
{
p = h.pos;
in_water = true;
}
}
if !b.on_ground && !in_water {
if (d.time > 100 && (p[1] < 1 || p[1] > 256)) || d.time > 600 {
b.removed = true;
}
} else {
let here = state_at(w, p[0], p[1], p[2]);
let below = state_at(
w,
floor(b.pos[0]),
floor(b.pos[1] - f64::from(0.01f32)),
floor(b.pos[2]),
);
if !in_water && can_fall_through(below) {
b.on_ground = false;
return;
}
b.vel[0] *= f64::from(0.7f32);
b.vel[2] *= f64::from(0.7f32);
b.vel[1] *= -0.5;
if here.block().id() != id::PISTON_EXTENSION {
b.removed = true;
}
}
}
for v in &mut b.vel {
*v *= f64::from(0.98f32);
}
}