use ghast_data::v1_12::BlockState;
use super::ext::PlayerExt;
use super::player::PlayerState;
use crate::input::{Packets, TickInput};
use crate::math::Aabb;
use crate::world::BlockView;
pub const MINECART_WIDTH: f32 = 0.98;
pub const MINECART_HEIGHT: f32 = 0.7;
#[derive(Clone, Debug, PartialEq)]
pub struct Minecart {
pub pos: [f64; 3],
pub bb: Aabb,
pub yaw: f32,
pub pitch: f32,
pub tracked: [i64; 3],
target: [f64; 3],
target_yaw: f64,
target_pitch: f64,
steps: i32,
}
fn cart_box(p: [f64; 3]) -> Aabb {
super::player::box_at(p[0], p[1], p[2], MINECART_WIDTH, MINECART_HEIGHT)
}
fn wrap_degrees(d: f64) -> f64 {
let mut e = d % 360.0;
if e >= 180.0 {
e -= 360.0;
}
if e < -180.0 {
e += 360.0;
}
e
}
fn fixed(v: f64) -> i64 {
(v * 4096.0).floor() as i64
}
fn angle(b: i8) -> f32 {
(i32::from(b) * 360) as f32 / 256.0
}
impl Minecart {
#[must_use]
pub fn new(pos: [f64; 3], yaw: f32, pitch: f32) -> Minecart {
Minecart {
pos,
bb: cart_box(pos),
yaw,
pitch,
tracked: pos.map(fixed),
target: pos,
target_yaw: f64::from(yaw),
target_pitch: f64::from(pitch),
steps: 0,
}
}
#[must_use]
pub fn interpolation_steps(&self) -> i32 {
self.steps
}
fn retarget(&mut self, target: [f64; 3], yaw: f32, pitch: f32, steps: i32) {
self.target = target;
self.target_yaw = f64::from(yaw);
self.target_pitch = f64::from(pitch);
self.steps = steps + 2;
}
pub fn server_teleport(&mut self, pos: [f64; 3], yaw: i8, pitch: i8) {
self.tracked = pos.map(fixed);
let (yaw, pitch) = (angle(yaw), angle(pitch));
#[allow(clippy::neg_cmp_op_on_partial_ord)]
let near = !((self.pos[0] - pos[0]).abs() >= 0.03125)
&& !((self.pos[1] - pos[1]).abs() >= 0.015625)
&& !((self.pos[2] - pos[2]).abs() >= 0.03125);
if near {
self.retarget(self.pos, yaw, pitch, 0);
} else {
self.retarget(pos, yaw, pitch, 3);
}
}
pub fn server_update(&mut self, delta: [i16; 3], rotation: Option<(i8, i8)>) {
for (t, d) in self.tracked.iter_mut().zip(delta) {
*t += i64::from(d);
}
let target = self.tracked.map(|v| v as f64 / 4096.0);
let (yaw, pitch) = rotation.map_or((self.yaw, self.pitch), |(y, p)| (angle(y), angle(p)));
self.retarget(target, yaw, pitch, 3);
}
pub fn tick(&mut self) {
if self.steps > 0 {
let n = f64::from(self.steps);
let x = self.pos[0] + (self.target[0] - self.pos[0]) / n;
let y = self.pos[1] + (self.target[1] - self.pos[1]) / n;
let z = self.pos[2] + (self.target[2] - self.pos[2]) / n;
let g = wrap_degrees(self.target_yaw - f64::from(self.yaw));
self.yaw = (f64::from(self.yaw) + g / n) as f32;
self.pitch =
(f64::from(self.pitch) + (self.target_pitch - f64::from(self.pitch)) / n) as f32;
self.steps -= 1;
self.pos = [x, y, z];
}
self.bb = cart_box(self.pos);
self.yaw %= 360.0;
self.pitch %= 360.0;
}
#[must_use]
pub fn passenger_pos(&self) -> [f64; 3] {
[self.pos[0], self.pos[1] + 0.0 + -0.35, self.pos[2]]
}
}
pub fn tick_minecart<W: BlockView<State = BlockState>>(
p: &mut PlayerState,
x: &mut PlayerExt,
cart: &mut Minecart,
entities: &[Aabb],
input: &TickInput,
world: &W,
out: &mut Packets,
) {
cart.tick();
super::player::tick_cart_passenger(p, x, entities, input, world, out);
let [sx, sy, sz] = cart.passenger_pos();
p.set_position(sx, sy, sz);
p.fall_distance = 0.0;
}