use super::math::{self as m, DEG_TO_RAD, V3};
use super::player::{PlayerState, Pose};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct PlayerExt {
pub fall_flying: bool,
pub glider: bool,
pub rocket_arm_left: bool,
pub spin_attack: bool,
pub auto_spin_ticks: i32,
pub sleeping: bool,
rocket_count: u8,
rockets: [Rocket; MAX_ROCKETS],
pub(super) pending_move: Option<super::inside::Movement>,
pub attributes: MovementAttributes,
pub(super) jump_riding_ticks: i32,
pub(super) jump_riding_scale: f32,
pub(super) jump_bar_age: i32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct MovementAttributes {
pub friction_modifier: super::attr::Attribute,
pub air_drag_modifier: super::attr::Attribute,
pub bounciness: super::attr::Attribute,
}
impl Default for MovementAttributes {
fn default() -> Self {
MovementAttributes {
friction_modifier: super::attr::Attribute::new(1.0, 0.0, 2048.0),
air_drag_modifier: super::attr::Attribute::new(1.0, 0.0, 2048.0),
bounciness: super::attr::Attribute::new(0.0, 0.0, 1.0),
}
}
}
impl MovementAttributes {
pub fn get_mut(&mut self, name: &str) -> Option<&mut super::attr::Attribute> {
Some(match name.strip_prefix("minecraft:").unwrap_or(name) {
"friction_modifier" => &mut self.friction_modifier,
"air_drag_modifier" => &mut self.air_drag_modifier,
"bounciness" => &mut self.bounciness,
_ => return None,
})
}
}
pub const MAX_ROCKETS: usize = 16;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Rocket {
id: i32,
x: f64,
y: f64,
z: f64,
}
impl PlayerExt {
#[must_use]
pub fn new() -> PlayerExt {
PlayerExt::default()
}
#[must_use]
pub fn shared_flags(&self, p: &PlayerState) -> u8 {
(u8::from(p.sprinting) << 3)
| (u8::from(p.swimming) << 4)
| (u8::from(self.fall_flying) << 7)
}
#[must_use]
pub fn jump_riding(&self) -> (i32, f32) {
(self.jump_riding_ticks, self.jump_riding_scale)
}
#[must_use]
pub fn visually_crawling(&self, p: &PlayerState) -> bool {
(p.pose == Pose::Swimming || !self.fall_flying && p.pose == Pose::FallFlying)
&& !p.touching_water
}
pub fn attach_rocket(&mut self, id: i32, pos: [f64; 3]) -> bool {
self.remove_rocket(id);
let n = usize::from(self.rocket_count);
if n == MAX_ROCKETS {
return false;
}
self.rockets[n] = Rocket {
id,
x: pos[0],
y: pos[1],
z: pos[2],
};
self.rocket_count += 1;
true
}
pub fn remove_rocket(&mut self, id: i32) {
let n = usize::from(self.rocket_count);
if let Some(i) = self.rockets[..n].iter().position(|r| r.id == id) {
self.rockets.copy_within(i + 1..n, i);
self.rocket_count -= 1;
}
}
#[must_use]
pub fn rocket_count(&self) -> usize {
usize::from(self.rocket_count)
}
pub(super) fn tick_rockets(&mut self, p: &mut PlayerState, loaded: impl Fn(i32, i32) -> bool) {
let count = usize::from(self.rocket_count);
if count == 0 {
return;
}
let (yaw, pitch) = (p.yaw, p.pitch);
let below = f64::from(p.level.min_y - 64);
let mut discarded = [0i32; MAX_ROCKETS];
let mut n = 0;
for r in &mut self.rockets[..count] {
let cx = m::floor(r.x) >> 4;
let cz = m::floor(r.z) >> 4;
if !loaded(cx, cz) {
continue;
}
if r.y < below {
discarded[n] = r.id;
n += 1;
}
let hand = if self.fall_flying {
let v = rocket_boost(V3::of(p.vel), yaw, pitch);
if v.is_finite() {
p.vel = v.arr();
}
let a = if self.rocket_arm_left { -80.0 } else { 80.0 };
look_angle(yaw + a, 0.0).scale(0.5)
} else {
V3::ZERO
};
r.x = p.pos[0] + hand.x;
r.y = p.pos[1] + hand.y;
r.z = p.pos[2] + hand.z;
}
for &id in &discarded[..n] {
self.remove_rocket(id);
}
}
#[must_use]
pub fn living_flags(&self) -> u8 {
u8::from(self.spin_attack) << 2
}
pub fn apply_living_flags(&mut self, flags: u8) {
self.spin_attack = flags & 4 != 0;
}
pub fn apply_shared_flags(&mut self, p: &mut PlayerState, flags: u8) {
p.sprinting = flags & 0x08 != 0;
p.swimming = flags & 0x10 != 0;
self.fall_flying = flags & 0x80 != 0;
}
}
pub(super) fn look_angle(yaw: f32, pitch: f32) -> V3 {
let x_rot = pitch * DEG_TO_RAD;
let y_rot = -yaw * DEG_TO_RAD;
let y_cos = m::cos(f64::from(y_rot));
let y_sin = m::sin(f64::from(y_rot));
let x_cos = m::cos(f64::from(x_rot));
let x_sin = m::sin(f64::from(x_rot));
V3::new(
f64::from(y_sin * x_cos),
f64::from(-x_sin),
f64::from(y_cos * x_cos),
)
}
pub(super) fn glide(mv: V3, yaw: f32, pitch: f32, gravity: f64) -> V3 {
let look = look_angle(yaw, pitch);
let lean = pitch * DEG_TO_RAD;
let look_h = (look.x * look.x + look.z * look.z).sqrt();
let move_h = (mv.x * mv.x + mv.z * mv.z).sqrt();
let c = super::jdk_math::cos(lean);
let lift = c * c;
let mut v = mv.add(V3::new(0.0, gravity * (-1.0 + lift * 0.75), 0.0));
if v.y < 0.0 && look_h > 0.0 {
let k = v.y * -0.1 * lift;
v = v.add(V3::new(look.x * k / look_h, k, look.z * k / look_h));
}
if lean < 0.0 && look_h > 0.0 {
let k = move_h * f64::from(-m::sin(f64::from(lean))) * 0.04;
v = v.add(V3::new(-look.x * k / look_h, k * 3.2, -look.z * k / look_h));
}
if look_h > 0.0 {
v = v.add(V3::new(
(look.x / look_h * move_h - v.x) * 0.1,
0.0,
(look.z / look_h * move_h - v.z) * 0.1,
));
}
v.mul(f64::from(0.99f32), f64::from(0.98f32), f64::from(0.99f32))
}
pub(super) fn rocket_boost(v: V3, yaw: f32, pitch: f32) -> V3 {
let look = look_angle(yaw, pitch);
v.add(V3::new(
look.x * 0.1 + (look.x * 1.5 - v.x) * 0.5,
look.y * 0.1 + (look.y * 1.5 - v.y) * 0.5,
look.z * 0.1 + (look.z * 1.5 - v.z) * 0.5,
))
}