use ghast_data::v1_8::BlockState;
use super::blocks::{HistoryShapes, is_air, is_valid_pos, is_water, state_at};
use super::body::{self, Body};
use crate::math::{Aabb, sqrt_f64_as_f32};
pub use crate::nonliving::Side;
use crate::nonliving::{LegacyRandom, atan2, degrees_1_8 as degrees, lerp_rotation};
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,
Arrow,
SmallFireball,
LargeFireball,
WitherSkull,
FishingBobber,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Family {
Thrown,
Hurting,
Arrow,
Bobber,
}
impl ProjectileKind {
pub const ALL: [ProjectileKind; 10] = [
ProjectileKind::Snowball,
ProjectileKind::Egg,
ProjectileKind::EnderPearl,
ProjectileKind::ExperienceBottle,
ProjectileKind::SplashPotion,
ProjectileKind::Arrow,
ProjectileKind::SmallFireball,
ProjectileKind::LargeFireball,
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::Arrow => "arrow",
ProjectileKind::SmallFireball => "small_fireball",
ProjectileKind::LargeFireball => "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 => Family::Thrown,
ProjectileKind::SmallFireball
| ProjectileKind::LargeFireball
| ProjectileKind::WitherSkull => Family::Hurting,
ProjectileKind::Arrow => 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 => (0.5, 0.5),
ProjectileKind::LargeFireball => (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 => 0.05,
ProjectileKind::Arrow => 0.05,
_ => 0.0,
})
}
}
#[derive(Clone, Debug)]
pub struct Projectile {
kind: ProjectileKind,
b: Body,
power: [f64; 3],
dangerous: bool,
in_ground: bool,
life: i32,
stuck: [i32; 3],
stuck_block: Option<(u8, u8)>,
owner: Option<[f64; 3]>,
owner_holds_rod: bool,
lerp: Option<Box<BobberLerp>>,
}
#[derive(Clone, Debug, Default)]
struct BobberLerp {
steps: i32,
to: [f64; 5],
packet_vel: [f64; 3],
}
fn wrap_degrees(mut d: f64) -> f64 {
d %= 360.0;
if d >= 180.0 {
d -= 360.0;
}
if d < -180.0 {
d += 360.0;
}
d
}
#[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 mut b = Body::new(pos, vel, w, h);
b.climbs = kind == ProjectileKind::FishingBobber;
b.history = HistoryShapes::Constructor;
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,
in_ground: false,
life: 0,
stuck: [-1, -1, -1],
stuck_block: None,
owner: None,
owner_holds_rod: true,
lerp: None,
}
}
#[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(self, _tipped: bool) -> Projectile {
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(self, _no_gravity: bool) -> Projectile {
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_tracked_position<W: BlockView<State = BlockState>>(
&mut self,
pos: [f64; 3],
yaw: f32,
pitch: f32,
world: &W,
) {
match self.kind.family() {
Family::Bobber => {
let l = self.lerp.get_or_insert_with(Box::default);
l.steps = 3;
l.to = [pos[0], pos[1], pos[2], f64::from(yaw), f64::from(pitch)];
self.b.vel = l.packet_vel;
}
Family::Arrow => {
self.b.set_position(pos);
self.b.yaw = yaw % 360.0;
self.b.pitch = pitch % 360.0;
}
Family::Thrown | Family::Hurting => {
self.b.yaw = yaw % 360.0;
self.b.pitch = pitch % 360.0;
body::tracked_position(&mut self.b, world, pos);
}
}
}
pub fn set_velocity_client(&mut self, vel: [f64; 3]) {
let family = self.kind.family();
let b = &mut self.b;
b.vel = vel;
match family {
Family::Thrown | Family::Arrow if 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.yaw = degrees(atan2(vel[0], vel[2]));
b.pitch = degrees(atan2(vel[1], f64::from(f)));
b.yaw_o = b.yaw;
b.pitch_o = b.pitch;
if family == Family::Arrow {
let p = b.pos;
b.set_position(p);
self.life = 0;
}
}
Family::Bobber => {
self.lerp.get_or_insert_with(Box::default).packet_vel = vel;
}
_ => {}
}
}
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;
}
#[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 life(&self) -> i32 {
self.life
}
#[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
}
}
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
}
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 })
}
fn ray<W: BlockView<State = BlockState>>(
w: &W,
from: [f64; 3],
to: [f64; 3],
ignore_no_box: bool,
touched: u8,
) -> Option<BlockHit> {
crate::pick::legacy::trace(v3(from), v3(to), false, |p| {
let s = state_at(w, p[0], p[1], p[2]);
if ignore_no_box && !s.selection().has_collision_box {
return crate::pick::legacy::Shape::Skip;
}
match s.block().id() {
body::nid::BREWING_STAND if touched & 1 == 0 => {
crate::pick::legacy::Shape::One(Aabb::FULL)
}
body::nid::BREWING_STAND => {
crate::pick::legacy::Shape::One(Aabb::new(0.0, 0.0, 0.0, 1.0, 0.125, 1.0))
}
body::nid::END_PORTAL_FRAME if touched & 2 == 0 => {
crate::pick::legacy::Shape::One(Aabb::FULL)
}
body::nid::END_PORTAL_FRAME => {
crate::pick::legacy::Shape::One(Aabb::new(0.0, 0.0, 0.0, 1.0, 0.8125, 1.0))
}
_ => crate::pick::v1_8::shape(w, p, false),
}
})
}
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, b.touched);
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);
}
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
&& !(is_valid_pos(bp[0], bp[1], bp[2]) && 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, b.touched);
if hit.is_some() && side == Side::Server {
b.removed = true;
}
let next = add(b.pos, b.vel);
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);
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 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, b.history, 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 {
if p.stuck_block == Some((here.block().id(), here.meta())) {
p.life += 1;
if p.life >= 1200 {
b.removed = true;
}
} else {
p.in_ground = false;
for a in 0..3 {
b.vel[a] *= f64::from(b.rng.next_float() * 0.2);
}
p.life = 0;
}
} else {
let hit = ray(w, b.pos, add(b.pos, b.vel), true, b.touched);
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);
}
b.vel[1] -= f64::from(0.05f32);
let pos = b.pos;
b.set_position(pos);
body::check_block_collision(b, w);
}
result
}
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);
if let Some(l) = p.lerp.as_deref_mut()
&& l.steps > 0
{
let n = f64::from(l.steps);
let d = [
b.pos[0] + (l.to[0] - b.pos[0]) / n,
b.pos[1] + (l.to[1] - b.pos[1]) / n,
b.pos[2] + (l.to[2] - b.pos[2]) / n,
];
let g = wrap_degrees(l.to[3] - f64::from(b.yaw));
let yaw = (f64::from(b.yaw) + g / n) as f32;
let pitch = (f64::from(b.pitch) + (l.to[4] - f64::from(b.pitch)) / n) as f32;
l.steps -= 1;
b.set_position(d);
b.yaw = yaw % 360.0;
b.pitch = pitch % 360.0;
return None;
}
if side == Side::Server {
let gone = match p.owner {
None => true,
Some(o) => {
let (dx, dy, dz) = (b.pos[0] - o[0], b.pos[1] - o[1], b.pos[2] - o[2]);
!p.owner_holds_rod || dx * dx + dy * dy + dz * dz > 1024.0
}
};
if gone {
b.removed = true;
return None;
}
}
if p.in_ground {
p.in_ground = false;
for a in 0..3 {
b.vel[a] *= f64::from(b.rng.next_float() * 0.2);
}
p.life = 0;
}
let hit = ray(w, b.pos, add(b.pos, b.vel), false, b.touched);
if hit.is_some() {
p.in_ground = true;
}
if !p.in_ground {
let v = b.vel;
body::entity_move(b, w, v);
update_rotation(b);
let mut m: f32 = if b.on_ground || b.hcol { 0.5 } else { 0.92 };
let mut o = 0.0;
let bb = b.bb;
for i in 0..5 {
let q = bb.max_y - bb.min_y;
let r = bb.min_y + q * f64::from(i) / 5.0;
let s = bb.min_y + q * f64::from(i + 1) / 5.0;
if contains_water(w, &Aabb::new(bb.min_x, r, bb.min_z, bb.max_x, s, bb.max_z)) {
o += 1.0 / 5.0;
}
}
let aj = o * 2.0 - 1.0;
b.vel[1] += f64::from(0.04f32) * aj;
if o > 0.0 {
m = (f64::from(m) * 0.9) as f32;
b.vel[1] *= 0.8;
}
for v in &mut b.vel {
*v *= f64::from(m);
}
let pos = b.pos;
b.set_position(pos);
}
hit.map(to_hit)
}
fn contains_water<W: BlockView<State = BlockState>>(w: &W, bb: &Aabb) -> bool {
use crate::math::floor;
let end = |v: f64| floor(v + 1.0);
for x in floor(bb.min_x)..end(bb.max_x) {
for y in floor(bb.min_y)..end(bb.max_y) {
for z in floor(bb.min_z)..end(bb.max_z) {
let s = state_at(w, x, y, z);
if is_water(s) {
let level = i32::from(s.meta());
let top = if level < 8 {
f64::from(y + 1) - f64::from(level) / 8.0
} else {
f64::from(y + 1)
};
if top >= bb.min_y {
return true;
}
}
}
}
}
false
}