use crate::math::{cos, sin, sqrt_f64_as_f32};
use crate::nonliving::{LegacyRandom, atan2, degrees};
pub const SPREAD: f64 = 0.0075f32 as f64;
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct LaunchSpec {
pub power: f32,
pub pitch_offset: f32,
pub uncertainty: f32,
}
impl LaunchSpec {
#[must_use]
pub const fn new(power: f32, pitch_offset: f32, uncertainty: f32) -> LaunchSpec {
LaunchSpec {
power,
pitch_offset,
uncertainty,
}
}
pub const THROWN: LaunchSpec = LaunchSpec::new(1.5, 0.0, 1.0);
pub const POTION: LaunchSpec = LaunchSpec::new(0.5, -20.0, 1.0);
pub const EXPERIENCE_BOTTLE: LaunchSpec = LaunchSpec::new(0.7, -20.0, 1.0);
#[must_use]
pub fn bow(ticks_held: i32) -> Option<LaunchSpec> {
let p = bow_power(ticks_held);
if f64::from(p) < 0.1 {
None
} else {
Some(LaunchSpec::new(p * 3.0, 0.0, 1.0))
}
}
}
#[must_use]
pub fn bow_power(ticks_held: i32) -> f32 {
let t = ticks_held as f32 / 20.0;
let p = (t * t + t * 2.0) / 3.0;
if p > 1.0 { 1.0 } else { p }
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Noise(pub [f64; 3]);
impl Noise {
pub const NONE: Noise = Noise([0.0; 3]);
#[must_use]
pub fn from_seed(seed: i64, uncertainty: f32) -> Noise {
let mut r = LegacyRandom::new(seed);
let k = f64::from(0.0075f32);
let u = f64::from(uncertainty);
let x = r.next_gaussian() * k * u;
let y = r.next_gaussian() * k * u;
let z = r.next_gaussian() * k * u;
Noise([x, y, z])
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct Launch {
pub vel: [f64; 3],
pub yaw: f32,
pub pitch: f32,
}
const DEG_TO_RAD: f32 = (core::f64::consts::PI / 180.0) as f32;
fn horizontal(v: [f64; 3]) -> f64 {
f64::from(sqrt_f64_as_f32(v[0] * v[0] + v[2] * v[2]))
}
fn launch(
d: [f32; 3],
spec: &LaunchSpec,
noise: Noise,
shooter_motion: [f64; 3],
shooter_on_ground: bool,
) -> Launch {
let v = d.map(f64::from);
let f = f64::from(sqrt_f64_as_f32(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]));
let mut m = [0.0; 3];
for a in 0..3 {
m[a] = (v[a] / f + noise.0[a]) * f64::from(spec.power);
}
let yaw = degrees(atan2(m[0], m[2]));
let pitch = degrees(atan2(m[1], horizontal(m)));
m[0] += shooter_motion[0];
m[2] += shooter_motion[2];
if !shooter_on_ground {
m[1] += shooter_motion[1];
}
Launch { vel: m, yaw, pitch }
}
#[must_use]
pub fn shoot(
yaw: f32,
pitch: f32,
spec: &LaunchSpec,
noise: Noise,
shooter_motion: [f64; 3],
shooter_on_ground: bool,
) -> Launch {
let d = [
-sin(yaw * DEG_TO_RAD) * cos(pitch * DEG_TO_RAD),
-sin((pitch + spec.pitch_offset) * DEG_TO_RAD),
cos(yaw * DEG_TO_RAD) * cos(pitch * DEG_TO_RAD),
];
launch(d, spec, noise, shooter_motion, shooter_on_ground)
}
#[must_use]
pub fn shoot_arrow(
yaw: f32,
pitch: f32,
spec: &LaunchSpec,
noise: Noise,
shooter_motion: [f64; 3],
shooter_on_ground: bool,
) -> Launch {
let d = [
-sin(yaw * DEG_TO_RAD) * cos(pitch * DEG_TO_RAD),
-sin(pitch * DEG_TO_RAD),
cos(yaw * DEG_TO_RAD) * cos(pitch * DEG_TO_RAD),
];
launch(d, spec, noise, shooter_motion, shooter_on_ground)
}
#[must_use]
pub fn launch_y(eye_y: f64) -> f64 {
eye_y - f64::from(0.1f32)
}
#[must_use]
pub fn bobber_cast(
yaw: f32,
pitch: f32,
prev_yaw: f32,
prev_pitch: f32,
pos: [f64; 3],
eye_height: f32,
gaussians: [f64; 3],
) -> ([f64; 3], Launch) {
let f = prev_pitch + (pitch - prev_pitch);
let g = prev_yaw + (yaw - prev_yaw);
let h = cos(-g * DEG_TO_RAD - core::f32::consts::PI);
let i = sin(-g * DEG_TO_RAD - core::f32::consts::PI);
let j = -cos(-f * DEG_TO_RAD);
let k = sin(-f * DEG_TO_RAD);
let start = [
pos[0] - f64::from(i) * 0.3,
pos[1] + f64::from(eye_height),
pos[2] - f64::from(h) * 0.3,
];
let d = [
f64::from(-i),
f64::from((-(k / j)).clamp(-5.0, 5.0)),
f64::from(-h),
];
let m = f64::from(sqrt_f64_as_f32(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]));
let mut v = [0.0; 3];
for a in 0..3 {
v[a] = d[a] * (0.6 / m + 0.5 + gaussians[a] * 0.0045);
}
let yaw = degrees(atan2(v[0], v[2]));
let pitch = degrees(atan2(v[1], horizontal(v)));
(start, Launch { vel: v, yaw, pitch })
}
#[must_use]
pub fn bobber_noise(seed: i64) -> [f64; 3] {
let mut r = LegacyRandom::new(seed);
[r.next_gaussian(), r.next_gaussian(), r.next_gaussian()]
}
#[must_use]
pub fn aimed_power(dir: [f64; 3], gaussians: [f64; 3]) -> [f64; 3] {
let d = [
dir[0] + gaussians[0] * 0.4,
dir[1] + gaussians[1] * 0.4,
dir[2] + gaussians[2] * 0.4,
];
let g = f64::from(sqrt_f64_as_f32(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]));
[d[0] / g * 0.1, d[1] / g * 0.1, d[2] / g * 0.1]
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DropNoise(pub [f32; 4]);
impl DropNoise {
pub const NONE: DropNoise = DropNoise([0.0; 4]);
#[must_use]
pub fn from_seed(seed: i64) -> DropNoise {
let mut r = LegacyRandom::new(seed);
DropNoise([
r.next_float(),
r.next_float(),
r.next_float(),
r.next_float(),
])
}
}
#[must_use]
pub fn drop_velocity(yaw: f32, pitch: f32, noise: DropNoise) -> [f64; 3] {
let [r1, r2, r3, r4] = noise.0;
let x = -sin(yaw * DEG_TO_RAD) * cos(pitch * DEG_TO_RAD) * 0.3;
let z = cos(yaw * DEG_TO_RAD) * cos(pitch * DEG_TO_RAD) * 0.3;
let y = -sin(pitch * DEG_TO_RAD) * 0.3 + 0.1;
let (cos_i, sin_i) = crate::jdk_trig::drop_cos_sin(r1);
let h = f64::from(0.02f32 * r2);
[
f64::from(x) + cos_i * h,
f64::from(y) + f64::from((r3 - r4) * 0.1),
f64::from(z) + sin_i * h,
]
}
#[must_use]
pub fn drop_velocity_random(noise: [f32; 2]) -> [f64; 3] {
let f = noise[0] * 0.5;
let g = noise[1] * core::f32::consts::TAU;
[
f64::from(-sin(g) * f),
f64::from(0.2f32),
f64::from(cos(g) * f),
]
}
#[must_use]
pub fn drop_y(y: f64, eye_height: f32) -> f64 {
y - f64::from(0.3f32) + f64::from(eye_height)
}