use ghast_data::FluidKind;
use ghast_data::v1_16_5::BlockState;
use super::blocks::{fluid, state_at};
use super::body::{self, Body, horizontal_len, mul};
use super::fluid::{V3, height};
use crate::math::Aabb;
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,
Trident,
SmallFireball,
LargeFireball,
DragonFireball,
WitherSkull,
FishingBobber,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Family {
Thrown,
Hurting,
Arrow,
Bobber,
}
impl ProjectileKind {
pub const ALL: [ProjectileKind; 14] = [
ProjectileKind::Snowball,
ProjectileKind::Egg,
ProjectileKind::EnderPearl,
ProjectileKind::ExperienceBottle,
ProjectileKind::SplashPotion,
ProjectileKind::LingeringPotion,
ProjectileKind::Arrow,
ProjectileKind::SpectralArrow,
ProjectileKind::Trident,
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::Trident => "trident",
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 | ProjectileKind::Trident => {
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 | ProjectileKind::Trident => {
(0.5, 0.5)
}
ProjectileKind::LargeFireball | ProjectileKind::DragonFireball => (1.0, 1.0),
_ => (0.25, 0.25),
}
}
fn eye_height(self) -> f32 {
match self.family() {
Family::Arrow => 0.13,
_ => self.dimensions().1 * 0.85,
}
}
#[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 | ProjectileKind::Trident => 0.05,
ProjectileKind::FishingBobber => 0.03,
_ => 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)]
struct Fish {
uuid_lsb: i64,
game_time: i64,
sync: LegacyRandom,
biting: bool,
hooked: Option<HookedEntity>,
}
impl Default for Fish {
fn default() -> Fish {
Fish {
uuid_lsb: 0,
game_time: 0,
sync: LegacyRandom::new(0),
biting: false,
hooked: None,
}
}
}
#[derive(Clone, Debug)]
pub struct Projectile {
kind: ProjectileKind,
b: Body,
power: V3,
dangerous: bool,
tipped: bool,
in_ground: bool,
in_ground_time: i32,
life: i32,
last_state: Option<u16>,
bobber: BobberState,
owner: Option<[f64; 3]>,
owner_holds_rod: bool,
fish: Option<Box<Fish>>,
}
#[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 p = V3::new(pos[0], pos[1], pos[2]);
let v = V3::new(vel[0], vel[1], vel[2]);
let b = Body::new(p, v, w, h, kind.eye_height());
let power = if kind.family() == Family::Hurting {
let d = f64::from(crate::math::sqrt_f64_as_f32(v.length_sq()));
if d == 0.0 {
V3::ZERO
} else {
V3::new(v.x / d * 0.1, v.y / d * 0.1, v.z / d * 0.1)
}
} else {
V3::ZERO
};
Projectile {
kind,
b,
power,
dangerous: false,
tipped: false,
in_ground: false,
in_ground_time: 0,
life: 0,
last_state: None,
bobber: BobberState::Flying,
owner: None,
owner_holds_rod: true,
fish: None,
}
}
fn fish(&mut self) -> &mut Fish {
self.fish.get_or_insert_with(Box::default)
}
#[must_use]
pub fn with_uuid_lsb(mut self, lsb: i64) -> Projectile {
self.fish().uuid_lsb = lsb;
self
}
pub fn set_game_time(&mut self, time: i64) {
self.fish().game_time = time;
}
pub fn set_biting(&mut self, biting: bool) {
let f = self.fish();
f.biting = biting;
if biting {
let r = f.sync.next_float();
let y = -0.4f32 * (r * (1.0f32 - 0.6f32) + 0.6f32);
self.b.vel = V3::new(self.b.vel.x, f64::from(y), self.b.vel.z);
}
}
pub fn set_hooked(&mut self, hooked: Option<HookedEntity>) {
if hooked.is_none() && self.fish.is_none() {
return;
}
self.fish().hooked = hooked;
}
#[must_use]
pub fn hooked(&self) -> Option<HookedEntity> {
self.fish.as_ref().and_then(|f| f.hooked)
}
#[must_use]
pub fn biting(&self) -> bool {
self.fish.as_ref().is_some_and(|f| f.biting)
}
#[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 = V3::new(power[0], power[1], power[2]);
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(v3(pos));
}
pub fn set_vel(&mut self, vel: [f64; 3]) {
self.b.vel = v3(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 skip_tick(&mut self) {
let b = &mut self.b;
b.old = b.pos;
b.yaw_o = b.yaw;
b.pitch_o = b.pitch;
}
pub fn set_velocity_client(&mut self, vel: [f64; 3]) {
let b = &mut self.b;
let v = v3(vel);
b.vel = v;
if b.pitch_o == 0.0 && b.yaw_o == 0.0 {
let f = horizontal_len(v);
b.pitch = degrees(atan2(v.y, f));
b.yaw = degrees(atan2(v.x, v.z));
b.pitch_o = b.pitch;
b.yaw_o = b.yaw;
let p = b.pos;
b.set_position(p);
}
if self.kind.family() == Family::Arrow {
self.life = 0;
}
}
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.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 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.x, self.power.y, self.power.z]
}
#[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]),
];
fluid(state_at(w, bp[0], bp[1], bp[2]))
.filter(|f| f.kind == FluidKind::Water)
.map_or(0.0, |f| height(w, bp[0], bp[1], bp[2], f))
}
pub fn tick_projectile<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
world: &W,
) -> Option<ProjectileHit> {
if p.b.removed {
return None;
}
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),
}
}
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) -> Option<ProjectileHit> {
h.is_block().then_some(ProjectileHit {
pos: h.pos,
face: h.face,
location: h.location,
inside: h.inside,
})
}
#[inline]
fn v3(a: [f64; 3]) -> V3 {
V3::new(a[0], a[1], a[2])
}
fn update_rotation(b: &mut Body) {
let v = b.vel;
let f = horizontal_len(v);
b.pitch = lerp_rotation(b.pitch_o, degrees(atan2(v.y, f)));
b.yaw = lerp_rotation(b.yaw_o, degrees(atan2(v.x, v.z)));
}
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 ctx = b.ctx();
let hit = body::clip(w, b.pos, b.pos.add(b.vel), &ctx, false);
if hit.is_block() && side == Side::Server {
b.removed = true;
}
body::check_block_collision(b, w);
let v = b.vel;
let next = b.pos.add(v);
update_rotation(b);
let k: f32 = if b.in_water { 0.8 } else { 0.99 };
b.vel = v.scale(f64::from(k));
if !b.no_gravity {
b.vel = V3::new(b.vel.x, b.vel.y - gravity, b.vel.z);
}
b.set_position(next);
to_hit(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 ctx = b.ctx();
let hit = body::clip(w, b.pos, b.pos.add(b.vel), &ctx, false);
if hit.is_block() && side == Side::Server {
b.removed = true;
}
body::check_block_collision(b, w);
let v = b.vel;
let next = b.pos.add(v);
rotate_towards_movement(b);
let g = if b.in_water { 0.8 } else { drag };
b.vel = v.add(power).scale(f64::from(g));
b.set_position(next);
to_hit(hit)
}
fn rotate_towards_movement(b: &mut Body) {
let v = b.vel;
if v.length_sq() == 0.0 {
return;
}
let f = horizontal_len(v);
let yaw = degrees(atan2(v.z, v.x)) + 90.0;
let pitch = degrees(atan2(f, v.y)) - 90.0;
while pitch - b.pitch_o < -180.0 {
b.pitch_o -= 360.0;
}
while pitch - b.pitch_o >= 180.0 {
b.pitch_o += 360.0;
}
while yaw - b.yaw_o < -180.0 {
b.yaw_o -= 360.0;
}
while yaw - b.yaw_o >= 180.0 {
b.yaw_o += 360.0;
}
b.pitch = b.pitch_o + 0.2 * (pitch - b.pitch_o);
b.yaw = b.yaw_o + 0.2 * (yaw - b.yaw_o);
}
fn arrow<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
w: &W,
) -> Option<ProjectileHit> {
let water_drag: f32 = if p.kind == ProjectileKind::Trident {
0.99
} else {
0.6
};
let b = &mut p.b;
b.start_tick();
body::base_tick(b, w, side);
let mut v = b.vel;
if b.pitch_o == 0.0 && b.yaw_o == 0.0 {
let f = horizontal_len(v);
b.yaw = degrees(atan2(v.x, v.z));
b.pitch = degrees(atan2(v.y, f));
b.yaw_o = b.yaw;
b.pitch_o = b.pitch;
}
let bp = b.block_pos();
let bs = state_at(w, bp[0], bp[1], bp[2]);
if !bs.is_air() && body::collision_holds(bs, bp, b.pos) {
p.in_ground = true;
}
let mut result = None;
if p.in_ground {
let probe = Aabb::new(
b.pos.x - 0.06,
b.pos.y - 0.06,
b.pos.z - 0.06,
b.pos.x + 0.06,
b.pos.y + 0.06,
b.pos.z + 0.06,
);
if p.last_state != Some(bs.id()) && body::space_empty(w, &probe) {
p.in_ground = false;
let (fx, fy, fz) = (
b.rng.next_float() * 0.2,
b.rng.next_float() * 0.2,
b.rng.next_float() * 0.2,
);
b.vel = mul(b.vel, f64::from(fx), f64::from(fy), f64::from(fz));
p.life = 0;
} else if side == Side::Server {
p.life += 1;
if p.life >= 1200 {
b.removed = true;
}
}
p.in_ground_time += 1;
} else {
p.in_ground_time = 0;
let from = b.pos;
let ctx = b.ctx();
let hit = body::clip(w, from, from.add(v), &ctx, false);
if hit.is_block() && !b.removed {
p.last_state = Some(state_at(w, hit.pos[0], hit.pos[1], hit.pos[2]).id());
let d = V3::new(
hit.location[0] - b.pos.x,
hit.location[1] - b.pos.y,
hit.location[2] - b.pos.z,
);
b.vel = d;
let off = d.normalize().scale(f64::from(0.05f32));
b.pos = V3::new(b.pos.x - off.x, b.pos.y - off.y, b.pos.z - off.z);
b.rng.next_float();
p.in_ground = true;
result = to_hit(hit);
}
v = b.vel;
let next = b.pos.add(v);
let f = horizontal_len(v);
let yaw = degrees(atan2(v.x, v.z));
let pitch = degrees(atan2(v.y, f));
b.pitch = lerp_rotation(b.pitch_o, pitch);
b.yaw = lerp_rotation(b.yaw_o, yaw);
let m = if b.in_water { water_drag } else { 0.99f32 };
b.vel = v.scale(f64::from(m));
if !b.no_gravity {
b.vel = V3::new(b.vel.x, b.vel.y - f64::from(0.05f32), b.vel.z);
}
b.set_position(next);
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 bobber<W: BlockView<State = BlockState>>(
p: &mut Projectile,
side: Side,
w: &W,
) -> Option<ProjectileHit> {
if let Some(f) = p.fish.as_deref_mut() {
f.sync = LegacyRandom::new(f.uuid_lsb ^ f.game_time);
}
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.x - owner[0], b.pos.y - owner[1], b.pos.z - owner[2]);
if !p.owner_holds_rod || dx * dx + dy * dy + dz * dz > 1024.0 {
b.removed = true;
return None;
}
}
if b.on_ground {
p.life += 1;
if p.life >= 1200 {
b.removed = true;
return None;
}
} else {
p.life = 0;
}
let bp = b.block_pos();
let water = fluid(state_at(w, bp[0], bp[1], bp[2])).filter(|f| f.kind == FluidKind::Water);
let f = water.map_or(0.0, |f| height(w, bp[0], bp[1], bp[2], f));
let in_water = f > 0.0;
let hooked = p.fish.as_ref().and_then(|f| f.hooked);
let mut result = None;
match p.bobber {
BobberState::Flying => {
if hooked.is_some() {
b.vel = V3::ZERO;
p.bobber = BobberState::Hooked;
return None;
}
if in_water {
b.vel = mul(b.vel, 0.3, 0.2, 0.3);
p.bobber = BobberState::Bobbing;
return None;
}
let ctx = b.ctx();
let hit = body::clip(w, b.pos, b.pos.add(b.vel), &ctx, false);
if hit.is_block() {
let l = v3(hit.location);
let (dx, dy, dz) = (l.x - b.pos.x, l.y - b.pos.y, l.z - b.pos.z);
let d = dx * dx + dy * dy + dz * dz;
b.vel = b.vel.normalize().scale(d);
result = to_hit(hit);
}
}
BobberState::Bobbing => {
let v = b.vel;
let mut d = b.pos.y + v.y - 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 = V3::new(v.x * 0.9, v.y - d * r * 0.2, v.z * 0.9);
if in_water
&& let Some(f) = p.fish.as_deref_mut()
&& f.biting
{
let r1 = f64::from(f.sync.next_float());
let r2 = f64::from(f.sync.next_float());
b.vel = b.vel.add(V3::new(0.0, -0.1 * r1 * r2, 0.0));
}
}
BobberState::Hooked => {
if let Some(h) = hooked {
if h.removed {
if let Some(f) = p.fish.as_deref_mut() {
f.hooked = None;
}
p.bobber = BobberState::Flying;
} else {
let y = h.pos[1] + f64::from(h.height) * 0.8;
b.set_position(V3::new(h.pos[0], y, h.pos[2]));
}
}
return None;
}
}
if water.is_none() {
b.vel = b.vel.add(V3::new(0.0, -0.03, 0.0));
}
let v = b.vel;
body::entity_move(b, w, v);
update_rotation(b);
if p.bobber == BobberState::Flying && (b.on_ground || b.hcol) {
b.vel = V3::ZERO;
}
b.vel = b.vel.scale(0.92);
let pos = b.pos;
b.set_position(pos);
result
}