use ghast_data::v1_12::BlockState;
use super::blocks::{is_air, is_water, state_at};
use super::body::{self, Body};
use crate::math::{Aabb, BoxOps, sqrt_f64_as_f32};
pub use crate::nonliving::Side;
use crate::nonliving::{LegacyRandom, atan2, degrees, lerp_rotation, signum};
use crate::pick::{BlockHit, Direction};
use crate::world::BlockView;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ProjectileKind {
Snowball,
Egg,
EnderPearl,
ExperienceBottle,
SplashPotion,
LingeringPotion,
Arrow,
SpectralArrow,
SmallFireball,
LargeFireball,
DragonFireball,
WitherSkull,
FishingBobber,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Family {
Thrown,
Hurting,
Arrow,
Bobber,
}
impl ProjectileKind {
pub const ALL: [ProjectileKind; 13] = [
ProjectileKind::Snowball,
ProjectileKind::Egg,
ProjectileKind::EnderPearl,
ProjectileKind::ExperienceBottle,
ProjectileKind::SplashPotion,
ProjectileKind::LingeringPotion,
ProjectileKind::Arrow,
ProjectileKind::SpectralArrow,
ProjectileKind::SmallFireball,
ProjectileKind::LargeFireball,
ProjectileKind::DragonFireball,
ProjectileKind::WitherSkull,
ProjectileKind::FishingBobber,
];
#[must_use]
pub const fn entity_name(self) -> &'static str {
match self {
ProjectileKind::Snowball => "snowball",
ProjectileKind::Egg => "egg",
ProjectileKind::EnderPearl => "ender_pearl",
ProjectileKind::ExperienceBottle => "experience_bottle",
ProjectileKind::SplashPotion => "splash_potion",
ProjectileKind::LingeringPotion => "lingering_potion",
ProjectileKind::Arrow => "arrow",
ProjectileKind::SpectralArrow => "spectral_arrow",
ProjectileKind::SmallFireball => "small_fireball",
ProjectileKind::LargeFireball => "fireball",
ProjectileKind::DragonFireball => "dragon_fireball",
ProjectileKind::WitherSkull => "wither_skull",
ProjectileKind::FishingBobber => "fishing_bobber",
}
}
#[must_use]
pub fn from_entity_name(name: &str) -> Option<ProjectileKind> {
let name = name.strip_prefix("minecraft:").unwrap_or(name);
ProjectileKind::ALL
.into_iter()
.find(|k| k.entity_name() == name)
}
fn family(self) -> Family {
match self {
ProjectileKind::Snowball
| ProjectileKind::Egg
| ProjectileKind::EnderPearl
| ProjectileKind::ExperienceBottle
| ProjectileKind::SplashPotion
| ProjectileKind::LingeringPotion => Family::Thrown,
ProjectileKind::SmallFireball
| ProjectileKind::LargeFireball
| ProjectileKind::DragonFireball
| ProjectileKind::WitherSkull => Family::Hurting,
ProjectileKind::Arrow | ProjectileKind::SpectralArrow => Family::Arrow,
ProjectileKind::FishingBobber => Family::Bobber,
}
}
#[must_use]
pub const fn dimensions(self) -> (f32, f32) {
match self {
ProjectileKind::SmallFireball | ProjectileKind::WitherSkull => (0.3125, 0.3125),
ProjectileKind::Arrow | ProjectileKind::SpectralArrow => (0.5, 0.5),
ProjectileKind::LargeFireball | ProjectileKind::DragonFireball => (1.0, 1.0),
_ => (0.25, 0.25),
}
}
#[must_use]
pub fn gravity(self) -> f64 {
f64::from(match self {
ProjectileKind::Snowball | ProjectileKind::Egg | ProjectileKind::EnderPearl => 0.03f32,
ProjectileKind::ExperienceBottle => 0.07,
ProjectileKind::SplashPotion | ProjectileKind::LingeringPotion => 0.05,
ProjectileKind::Arrow | ProjectileKind::SpectralArrow => 0.05,
_ => 0.0,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum BobberState {
#[default]
Flying,
Bobbing,
Hooked,
}
pub use crate::nonliving::HookedEntity;
#[derive(Clone, Debug)]
pub struct Projectile {
kind: ProjectileKind,
b: Body,
power: [f64; 3],
dangerous: bool,
tipped: bool,
in_ground: bool,
in_ground_time: i32,
life: i32,
stuck: [i32; 3],
stuck_block: Option<(u8, u8)>,
bobber: BobberState,
owner: Option<[f64; 3]>,
owner_holds_rod: bool,
hooked: Option<HookedEntity>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct ProjectileHit {
pub pos: [i32; 3],
pub face: Direction,
pub location: [f64; 3],
pub inside: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Impact {
pub tick: u32,
pub hit: ProjectileHit,
}
impl Projectile {
#[must_use]
pub fn new(kind: ProjectileKind, pos: [f64; 3], vel: [f64; 3]) -> Projectile {
let (w, h) = kind.dimensions();
let b = Body::new(pos, vel, w, h);
let power = if kind.family() == Family::Hurting {
let d = f64::from(sqrt_f64_as_f32(
vel[0] * vel[0] + vel[1] * vel[1] + vel[2] * vel[2],
));
if d == 0.0 {
[0.0; 3]
} else {
[vel[0] / d * 0.1, vel[1] / d * 0.1, vel[2] / d * 0.1]
}
} else {
[0.0; 3]
};
Projectile {
kind,
b,
power,
dangerous: false,
tipped: false,
in_ground: false,
in_ground_time: 0,
life: 0,
stuck: [-1, -1, -1],
stuck_block: None,
bobber: BobberState::Flying,
owner: None,
owner_holds_rod: true,
hooked: None,
}
}
pub fn set_hooked(&mut self, hooked: Option<HookedEntity>) {
self.hooked = hooked;
}
#[must_use]
pub fn hooked(&self) -> Option<HookedEntity> {
self.hooked
}
#[must_use]
pub fn with_rotation(mut self, yaw: f32, pitch: f32) -> Projectile {
self.b.yaw = yaw;
self.b.pitch = pitch;
self.b.yaw_o = yaw;
self.b.pitch_o = pitch;
self
}
#[must_use]
pub fn with_power(mut self, power: [f64; 3]) -> Projectile {
self.power = power;
self
}
#[must_use]
pub fn with_dangerous(mut self, dangerous: bool) -> Projectile {
self.dangerous = dangerous;
self
}
#[must_use]
pub fn with_tipped(mut self, tipped: bool) -> Projectile {
self.tipped = tipped;
self
}
#[must_use]
pub fn with_random_seed(mut self, seed: i64) -> Projectile {
self.b.rng = LegacyRandom::new(seed);
self
}
#[must_use]
pub fn with_on_ground(mut self, on_ground: bool) -> Projectile {
self.b.on_ground = on_ground;
self
}
#[must_use]
pub fn with_fall_distance(mut self, fall: f32) -> Projectile {
self.b.fall = fall;
self
}
#[must_use]
pub fn with_no_gravity(mut self, no_gravity: bool) -> Projectile {
self.b.no_gravity = no_gravity;
self
}
#[must_use]
pub fn with_owner(mut self, pos: Option<[f64; 3]>, holds_rod: bool) -> Projectile {
self.owner = pos;
self.owner_holds_rod = holds_rod;
self
}
pub fn set_owner_pos(&mut self, pos: Option<[f64; 3]>) {
self.owner = pos;
}
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_rotation(&mut self, yaw: f32, pitch: f32) {
self.b.yaw = yaw;
self.b.pitch = pitch;
}
pub fn set_on_ground(&mut self, on_ground: bool) {
self.b.on_ground = on_ground;
}
pub fn set_velocity_client(&mut self, vel: [f64; 3]) {
let family = self.kind.family();
let b = &mut self.b;
b.vel = vel;
if matches!(family, Family::Thrown | Family::Arrow) && b.pitch_o == 0.0 && b.yaw_o == 0.0 {
let f = sqrt_f64_as_f32(vel[0] * vel[0] + vel[2] * vel[2]);
b.pitch = degrees(atan2(vel[1], f64::from(f)));
b.yaw = degrees(atan2(vel[0], vel[2]));
b.pitch_o = b.pitch;
b.yaw_o = b.yaw;
if family == Family::Arrow {
let p = b.pos;
b.set_position(p);
self.life = 0;
}
}
}
pub fn skip_tick(&mut self) {
let b = &mut self.b;
b.old = b.pos;
b.tick_start = b.pos;
b.yaw_o = b.yaw;
b.pitch_o = b.pitch;
}
pub fn set_dangerous(&mut self, dangerous: bool) {
self.dangerous = dangerous;
}
pub fn set_tipped(&mut self, tipped: bool) {
self.tipped = tipped;
}
pub fn set_no_gravity(&mut self, no_gravity: bool) {
self.b.no_gravity = no_gravity;
}
#[must_use]
pub fn kind(&self) -> ProjectileKind {
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 yaw(&self) -> f32 {
self.b.yaw
}
#[must_use]
pub fn pitch(&self) -> f32 {
self.b.pitch
}
#[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 in_ground(&self) -> bool {
self.in_ground
}
#[must_use]
pub fn in_ground_time(&self) -> i32 {
self.in_ground_time
}
#[must_use]
pub fn life(&self) -> i32 {
self.life
}
#[must_use]
pub fn bobbing(&self) -> BobberState {
self.bobber
}
#[must_use]
pub fn power(&self) -> [f64; 3] {
self.power
}
#[must_use]
pub fn removed(&self) -> bool {
self.b.removed
}
#[must_use]
pub fn age(&self) -> i32 {
self.b.age
}
}
#[must_use]
pub fn bobber_liquid_height<W: BlockView<State = BlockState>>(w: &W, pos: [f64; 3]) -> f32 {
let bp = [
crate::math::floor(pos[0]),
crate::math::floor(pos[1]),
crate::math::floor(pos[2]),
];
let s = state_at(w, bp[0], bp[1], bp[2]);
if is_water(s) {
water_surface(w, bp, s)
} else {
0.0
}
}
pub fn tick_projectile<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
world: &W,
) -> Option<ProjectileHit> {
if p.b.removed {
return None;
}
let hit = match p.kind.family() {
Family::Thrown => thrown(p, side, world),
Family::Hurting => hurting(p, side, world),
Family::Arrow => arrow(p, side, world),
Family::Bobber => bobber(p, side, world),
};
p.b.end_tick();
hit
}
pub fn run_projectile<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
world: &W,
max_ticks: u32,
) -> Option<Impact> {
for t in 1..=max_ticks {
if p.b.removed {
return None;
}
if let Some(hit) = tick_projectile(p, side, world) {
return Some(Impact { tick: t, hit });
}
}
None
}
pub(super) fn to_hit(h: BlockHit) -> ProjectileHit {
ProjectileHit {
pos: h.pos,
face: h.face,
location: h.location,
inside: false,
}
}
#[inline]
fn v3(v: [f64; 3]) -> [f64; 3] {
v.map(|c| if c == 0.0 { 0.0 } else { c })
}
pub(super) fn ray<W: BlockView<State = BlockState>>(
w: &W,
from: [f64; 3],
to: [f64; 3],
liquids: bool,
ignore_no_box: bool,
) -> Option<BlockHit> {
crate::pick::legacy::trace(v3(from), v3(to), false, |p| {
if ignore_no_box && !state_at(w, p[0], p[1], p[2]).selection().has_collision_box {
return crate::pick::legacy::Shape::Skip;
}
crate::pick::v1_12::shape(w, p, liquids)
})
}
fn add(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
[a[0] + b[0], a[1] + b[1], a[2] + b[2]]
}
fn update_rotation(b: &mut Body) {
let v = b.vel;
let f = f64::from(sqrt_f64_as_f32(v[0] * v[0] + v[2] * v[2]));
b.pitch = lerp_rotation(b.pitch_o, degrees(atan2(v[1], f)));
b.yaw = lerp_rotation(b.yaw_o, degrees(atan2(v[0], v[2])));
}
fn thrown<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
w: &W,
) -> Option<ProjectileHit> {
let gravity = p.kind.gravity();
let b = &mut p.b;
b.start_tick();
body::base_tick(b, w, side);
let hit = ray(w, b.pos, add(b.pos, b.vel), false, false);
if hit.is_some() && side == Side::Server {
b.removed = true;
}
let next = add(b.pos, b.vel);
update_rotation(b);
let g: f32 = if b.in_water { 0.8 } else { 0.99 };
for v in &mut b.vel {
*v *= f64::from(g);
}
if !b.no_gravity {
b.vel[1] -= gravity;
}
b.set_position(next);
hit.map(to_hit)
}
fn hurting<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
w: &W,
) -> Option<ProjectileHit> {
let drag: f32 = if p.kind == ProjectileKind::WitherSkull && p.dangerous {
0.73
} else {
0.95
};
let power = p.power;
let b = &mut p.b;
b.start_tick();
let bp = b.block_pos();
if side == Side::Server && !w.is_chunk_loaded(bp[0] >> 4, bp[2] >> 4) {
b.removed = true;
return None;
}
body::base_tick(b, w, side);
let hit = ray(w, b.pos, add(b.pos, b.vel), false, true);
if hit.is_some() && side == Side::Server {
b.removed = true;
}
let next = add(b.pos, b.vel);
rotate_towards_movement(b);
let f = if b.in_water { 0.8 } else { drag };
for a in 0..3 {
b.vel[a] = (b.vel[a] + power[a]) * f64::from(f);
}
b.set_position(next);
hit.map(to_hit)
}
fn rotate_towards_movement(b: &mut Body) {
let v = b.vel;
let g = f64::from(sqrt_f64_as_f32(v[0] * v[0] + v[2] * v[2]));
let yaw = degrees(atan2(v[2], v[0])) + 90.0;
let pitch = degrees(atan2(g, v[1])) - 90.0;
b.pitch = lerp_rotation(b.pitch_o, pitch);
b.yaw = lerp_rotation(b.yaw_o, yaw);
}
fn arrow<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
w: &W,
) -> Option<ProjectileHit> {
let b = &mut p.b;
b.start_tick();
body::base_tick(b, w, side);
if b.pitch_o == 0.0 && b.yaw_o == 0.0 {
let v = b.vel;
let f = f64::from(sqrt_f64_as_f32(v[0] * v[0] + v[2] * v[2]));
b.yaw = degrees(atan2(v[0], v[2]));
b.pitch = degrees(atan2(v[1], f));
b.yaw_o = b.yaw;
b.pitch_o = b.pitch;
}
let sp = p.stuck;
let here = state_at(w, sp[0], sp[1], sp[2]);
if !is_air(here)
&& let Some(bx) = body::collision_bounds(w, sp[0], sp[1], sp[2])
{
let q = b.pos;
if q[0] > bx.min_x
&& q[0] < bx.max_x
&& q[1] > bx.min_y
&& q[1] < bx.max_y
&& q[2] > bx.min_z
&& q[2] < bx.max_z
{
p.in_ground = true;
}
}
let mut result = None;
if p.in_ground {
let same = p.stuck_block == Some((here.block().id(), here.meta()));
if !same && !body::collides_any(w, &b.bb.grow(0.05, 0.05, 0.05)) {
p.in_ground = false;
for a in 0..3 {
b.vel[a] *= f64::from(b.rng.next_float() * 0.2);
}
p.life = 0;
} else {
p.life += 1;
if p.life >= 1200 {
b.removed = true;
}
}
p.in_ground_time += 1;
} else {
p.in_ground_time = 0;
let hit = ray(w, b.pos, add(b.pos, b.vel), false, true);
if let Some(h) = hit {
p.stuck = h.pos;
let s = state_at(w, h.pos[0], h.pos[1], h.pos[2]);
p.stuck_block = Some((s.block().id(), s.meta()));
for a in 0..3 {
b.vel[a] = f64::from((h.location[a] - b.pos[a]) as f32);
}
let v = b.vel;
let len = f64::from(sqrt_f64_as_f32(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]));
for a in 0..3 {
b.pos[a] -= v[a] / len * f64::from(0.05f32);
}
b.rng.next_float();
p.in_ground = true;
if !is_air(s) {
body::block_touch(b, s);
}
result = Some(to_hit(h));
}
b.pos = add(b.pos, b.vel);
update_rotation(b);
let h: f32 = if b.in_water { 0.6 } else { 0.99 };
for v in &mut b.vel {
*v *= f64::from(h);
}
if !b.no_gravity {
b.vel[1] -= f64::from(0.05f32);
}
let pos = b.pos;
b.set_position(pos);
body::check_block_collision(b, w);
}
if p.tipped && side == Side::Client {
let n = if p.in_ground {
i32::from(p.in_ground_time % 5 == 0)
} else {
2
};
for _ in 0..n * 3 {
b.rng.next_double();
}
}
result
}
fn water_surface<W: BlockView<State = BlockState>>(w: &W, p: [i32; 3], s: BlockState) -> f32 {
let level = s.meta();
if level & 7 == 0 && is_water(state_at(w, p[0], p[1] + 1, p[2])) {
return 1.0;
}
let l = if level >= 8 { 0 } else { level };
1.0 - (f32::from(l) + 1.0) / 9.0
}
fn bobber<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
w: &W,
) -> Option<ProjectileHit> {
let b = &mut p.b;
b.start_tick();
body::base_tick(b, w, side);
let Some(owner) = p.owner else {
b.removed = true;
return None;
};
if side == Side::Server {
let (dx, dy, dz) = (
b.pos[0] - owner[0],
b.pos[1] - owner[1],
b.pos[2] - owner[2],
);
if !p.owner_holds_rod || dx * dx + dy * dy + dz * dz > 1024.0 {
b.removed = true;
return None;
}
}
if p.in_ground {
p.life += 1;
if p.life >= 1200 {
b.removed = true;
return None;
}
}
let bp = b.block_pos();
let s = state_at(w, bp[0], bp[1], bp[2]);
let water = is_water(s);
let f = if water { water_surface(w, bp, s) } else { 0.0 };
let mut result = None;
match p.bobber {
BobberState::Flying => {
if p.hooked.is_some() {
b.vel = [0.0; 3];
p.bobber = BobberState::Hooked;
return None;
}
if f > 0.0 {
b.vel[0] *= 0.3;
b.vel[1] *= 0.2;
b.vel[2] *= 0.3;
p.bobber = BobberState::Bobbing;
return None;
}
if side == Side::Server
&& let Some(h) = ray(w, b.pos, add(b.pos, b.vel), false, true)
{
p.in_ground = true;
result = Some(to_hit(h));
}
if p.in_ground || b.on_ground || b.hcol {
b.vel = [0.0; 3];
}
}
BobberState::Bobbing => {
b.vel[0] *= 0.9;
b.vel[2] *= 0.9;
let mut d = b.pos[1] + b.vel[1] - f64::from(bp[1]) - f64::from(f);
if d.abs() < 0.01 {
d += signum(d) * 0.1;
}
let r = f64::from(b.rng.next_float());
b.vel[1] -= d * r * 0.2;
}
BobberState::Hooked => {
if let Some(h) = p.hooked {
if h.removed {
p.hooked = None;
p.bobber = BobberState::Flying;
} else {
let y = h.pos[1] + f64::from(h.height) * 0.8;
b.set_position([h.pos[0], y, h.pos[2]]);
}
}
return None;
}
}
if !water {
b.vel[1] -= 0.03;
}
let v = b.vel;
body::entity_move(b, w, v);
update_rotation(b);
for v in &mut b.vel {
*v *= 0.92;
}
let pos = b.pos;
b.set_position(pos);
result
}