#![allow(clippy::neg_cmp_op_on_partial_ord)]
use ghast_data::FluidKind;
use ghast_data::PhysicsFlags;
use ghast_data::v1_16_5::{Block, BlockState};
use super::blocks::{
ShapeCtx, flags, fluid, is_bed, jump_multiplier, slipperiness, state_at, velocity_multiplier,
};
use super::collide::{BorderShape, Collider, NoEntities, bx, offset};
use super::fluid::{V3, ceil, region_loaded, update_in_fluid_as};
use crate::math::{self, Aabb, floor};
use crate::world::{BlockView, WorldBorder};
use crate::{Packet, Packets, TickInput};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MountKind {
Horse,
Donkey,
Mule,
SkeletonHorse,
ZombieHorse,
Llama,
TraderLlama,
Pig,
Strider,
}
impl MountKind {
#[must_use]
pub fn from_name(name: &str) -> Option<MountKind> {
Some(match name.strip_prefix("minecraft:").unwrap_or(name) {
"horse" => MountKind::Horse,
"donkey" => MountKind::Donkey,
"mule" => MountKind::Mule,
"skeleton_horse" => MountKind::SkeletonHorse,
"zombie_horse" => MountKind::ZombieHorse,
"llama" => MountKind::Llama,
"trader_llama" => MountKind::TraderLlama,
"pig" => MountKind::Pig,
"strider" => MountKind::Strider,
_ => return None,
})
}
#[must_use]
pub fn dimensions(self) -> (f32, f32) {
match self {
MountKind::Horse
| MountKind::Mule
| MountKind::SkeletonHorse
| MountKind::ZombieHorse => (1.396_484_4, 1.6),
MountKind::Donkey => (1.396_484_4, 1.5),
MountKind::Llama | MountKind::TraderLlama => (0.9, 1.87),
MountKind::Pig => (0.9, 0.9),
MountKind::Strider => (0.9, 1.7),
}
}
#[must_use]
pub fn is_horse_family(self) -> bool {
!matches!(self, MountKind::Pig | MountKind::Strider)
}
fn is_llama(self) -> bool {
matches!(self, MountKind::Llama | MountKind::TraderLlama)
}
#[must_use]
pub fn default_speed(self) -> f64 {
match self {
MountKind::Horse => f64::from(0.225f32),
MountKind::Donkey | MountKind::Mule | MountKind::Llama | MountKind::TraderLlama => {
f64::from(0.175f32)
}
MountKind::SkeletonHorse | MountKind::ZombieHorse => f64::from(0.2f32),
MountKind::Pig => 0.25,
MountKind::Strider => f64::from(0.175f32),
}
}
#[must_use]
pub fn default_jump(self) -> f64 {
match self {
MountKind::Donkey | MountKind::Mule | MountKind::Llama | MountKind::TraderLlama => 0.5,
_ => 0.7,
}
}
fn water_drag(self) -> f32 {
if self == MountKind::SkeletonHorse {
0.96
} else {
0.8
}
}
fn initial_step(self) -> f32 {
if self.is_horse_family() { 1.0 } else { 0.6 }
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Mount {
pub kind: MountKind,
pub pos: [f64; 3],
pub bb: Aabb,
pub vel: [f64; 3],
pub yaw: f32,
pub pitch: f32,
pub body_yaw: f32,
pub head_yaw: f32,
pub on_ground: bool,
pub horizontal_collision: bool,
pub vertical_collision: bool,
pub fall_distance: f32,
pub touching_water: bool,
pub step_height: f32,
pub flying_speed: f32,
pub movement_speed: f32,
pub limb_distance: f32,
pub limb_angle: f32,
pub speed_attribute: f64,
pub jump_attribute: f64,
pub saddled: bool,
pub rider_holds_stick: bool,
pub tracked: [f64; 3],
passenger: bool,
head_target: f64,
head_steps: i32,
floated: bool,
water_height: f64,
lava_height: f64,
first_update: bool,
movement_multiplier: [f64; 3],
prev: [f64; 3],
target: [f64; 3],
target_yaw: f64,
target_pitch: f64,
steps: i32,
last_head_yaw: f32,
body_ticks: i32,
horse: HorseState,
boost: Boost,
cold: bool,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct HorseState {
jump_strength: f32,
in_air: bool,
jumping: bool,
angry: bool,
angry_ticks: i32,
angry_progress: f32,
last_angry_progress: f32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct Boost {
boosted: bool,
ticks: i32,
time: i32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct HorseView {
pub jump_strength: f32,
pub in_air: bool,
pub jumping: bool,
pub angry: bool,
pub angry_ticks: i32,
pub angry_progress: f32,
pub last_angry_progress: f32,
}
#[derive(Clone, Copy, Debug)]
struct Rider {
yaw: f32,
pitch: f32,
sideways: f32,
forward: f32,
}
fn mount_box(kind: MountKind, x: f64, y: f64, z: f64) -> Aabb {
let (w, h) = kind.dimensions();
let r = f64::from(w / 2.0);
bx(x - r, y, z - r, x + r, y + f64::from(h), z + r)
}
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 step_angle(from: f32, to: f32, step: f32) -> f32 {
let f = crate::rotation::wrap_degrees(to - from);
let g = if f < -step {
-step
} else if f > step {
step
} else {
f
};
to - g
}
fn encode(v: f64) -> i64 {
(v * 4096.0).floor() as i64
}
impl Mount {
#[must_use]
pub fn new(kind: MountKind, pos: [f64; 3], yaw: f32, pitch: f32, head_yaw: f32) -> Mount {
let mut m = Mount {
kind,
pos,
bb: mount_box(kind, pos[0], pos[1], pos[2]),
vel: [0.0; 3],
yaw: yaw % 360.0,
pitch: pitch.clamp(-90.0, 90.0) % 360.0,
body_yaw: head_yaw,
head_yaw,
on_ground: false,
horizontal_collision: false,
vertical_collision: false,
fall_distance: 0.0,
touching_water: false,
step_height: kind.initial_step(),
flying_speed: 0.02,
movement_speed: 0.0,
limb_distance: 0.0,
limb_angle: 0.0,
speed_attribute: kind.default_speed(),
jump_attribute: kind.default_jump(),
saddled: false,
rider_holds_stick: false,
tracked: pos,
passenger: true,
head_target: 0.0,
head_steps: 0,
floated: false,
water_height: 0.0,
lava_height: 0.0,
first_update: true,
movement_multiplier: [0.0; 3],
prev: pos,
target: pos,
target_yaw: 0.0,
target_pitch: 0.0,
steps: 0,
last_head_yaw: 0.0,
body_ticks: 0,
horse: HorseState::default(),
boost: Boost::default(),
cold: false,
};
if !kind.is_horse_family() {
m.boost(0);
}
m
}
#[must_use]
pub fn watched(kind: MountKind, pos: [f64; 3], yaw: f32, pitch: f32, head_yaw: f32) -> Mount {
let mut m = Mount::new(kind, pos, yaw, pitch, head_yaw);
m.passenger = false;
m
}
pub fn board(&mut self) {
self.passenger = true;
}
pub fn leave(&mut self) {
self.passenger = false;
}
#[must_use]
pub fn ridden(&self) -> bool {
self.passenger
}
#[must_use]
pub fn head_steps(&self) -> i32 {
self.head_steps
}
pub fn server_head_yaw(&mut self, yaw: i8) {
self.head_target = f64::from(f32::from(yaw) * 360.0 / 256.0);
self.head_steps = 3;
}
pub fn server_horse_flags(&mut self, flags: u8) {
if self.kind.is_horse_family() {
self.saddled = flags & 4 != 0;
self.horse.angry = flags & 32 != 0;
}
}
#[must_use]
pub fn controlled(&self) -> bool {
if !self.passenger {
return false;
}
match self.kind {
MountKind::Llama | MountKind::TraderLlama => false,
MountKind::Pig | MountKind::Strider => self.rider_holds_stick,
_ => true,
}
}
#[must_use]
pub fn can_jump(&self) -> bool {
self.kind.is_horse_family() && self.saddled
}
#[must_use]
pub fn interpolation_steps(&self) -> i32 {
self.steps
}
#[must_use]
pub fn cold(&self) -> bool {
self.cold
}
#[must_use]
pub fn boost_state(&self) -> (bool, i32, i32) {
(self.boost.boosted, self.boost.ticks, self.boost.time)
}
#[must_use]
pub fn horse(&self) -> HorseView {
let h = self.horse;
HorseView {
jump_strength: h.jump_strength,
in_air: h.in_air,
jumping: h.jumping,
angry: h.angry,
angry_ticks: h.angry_ticks,
angry_progress: h.angry_progress,
last_angry_progress: h.last_angry_progress,
}
}
pub fn boost(&mut self, time: i32) {
self.boost = Boost {
boosted: true,
ticks: 0,
time,
};
}
pub fn server_velocity(&mut self, x: i16, y: i16, z: i16) {
self.vel = [
f64::from(x) / 8000.0,
f64::from(y) / 8000.0,
f64::from(z) / 8000.0,
];
}
pub fn spawn_velocity(&mut self, x: i16, y: i16, z: i16) {
self.vel = [x, y, z].map(|v| f64::from(f32::from(v) / 8000.0));
}
pub fn set_position(&mut self, x: f64, y: f64, z: f64) {
self.pos = [x, y, z];
self.bb = mount_box(self.kind, x, y, z);
}
pub fn server_move(&mut self, pos: [f64; 3], yaw: f32, pitch: f32) -> Option<Packet> {
if !self.controlled() {
return None;
}
self.yaw = yaw % 360.0;
self.pitch = pitch.clamp(-90.0, 90.0) % 360.0;
self.set_position(
pos[0].clamp(-3.0e7, 3.0e7),
pos[1],
pos[2].clamp(-3.0e7, 3.0e7),
);
Some(self.vehicle_move())
}
#[must_use]
pub fn vehicle_move(&self) -> Packet {
Packet::MoveVehicle {
pos: self.pos,
yaw: self.yaw,
pitch: self.pitch,
on_ground: self.on_ground,
}
}
pub fn server_teleport(&mut self, pos: [f64; 3], yaw: i8, pitch: i8, on_ground: bool) {
self.tracked = pos;
if !self.controlled() {
let yaw = f32::from(yaw) * 360.0 / 256.0;
let pitch = f32::from(pitch) * 360.0 / 256.0;
self.retarget(pos, yaw, pitch);
self.on_ground = on_ground;
}
}
pub fn server_update(
&mut self,
delta: Option<[i16; 3]>,
rotation: Option<(i8, i8)>,
on_ground: bool,
) {
if self.controlled() {
return;
}
let angles =
|(y, p): (i8, i8)| (f32::from(y) * 360.0 / 256.0, f32::from(p) * 360.0 / 256.0);
match delta {
Some(d) => {
if d != [0; 3] {
for k in 0..3 {
if d[k] != 0 {
self.tracked[k] =
(encode(self.tracked[k]) + i64::from(d[k])) as f64 / 4096.0;
}
}
}
let (yaw, pitch) = rotation.map_or((self.yaw, self.pitch), angles);
self.retarget(self.tracked, yaw, pitch);
}
None => {
if let Some(r) = rotation {
let (yaw, pitch) = angles(r);
self.retarget(self.pos, yaw, pitch);
}
}
}
self.on_ground = on_ground;
}
fn retarget(&mut self, target: [f64; 3], yaw: f32, pitch: f32) {
self.target = target;
self.target_yaw = f64::from(yaw);
self.target_pitch = f64::from(pitch);
self.steps = 3;
}
pub fn set_jump_strength(&mut self, strength: i32) {
if !self.kind.is_horse_family() || !self.saddled {
return;
}
if strength >= 0 {
self.horse.jumping = true;
if self.controlled() {
self.horse.angry_ticks = 1;
self.horse.angry = true;
}
}
let strength = strength.max(0);
self.horse.jump_strength = if strength >= 90 {
1.0
} else {
0.4 + 0.4 * strength as f32 / 90.0
};
}
#[must_use]
pub fn passenger_pos(&self) -> [f64; 3] {
let [x, y, z] = self.pos;
let (_, h) = self.kind.dimensions();
let base = f64::from(h) * 0.75;
match self.kind {
MountKind::Llama | MountKind::TraderLlama => {
let r = self.body_yaw * (std::f64::consts::PI / 180.0) as f32;
let (f, g) = (math::cos(r), math::sin(r));
let off = f64::from(h) * 0.67;
[
x + f64::from(0.3f32 * g),
y + off + -0.35,
z - f64::from(0.3f32 * f),
]
}
MountKind::Pig => [x, y + base + -0.35, z],
MountKind::Strider => {
let f = 0.25f32.min(self.limb_distance);
let g = self.limb_angle;
let off = f64::from(h) - 0.19 + f64::from(0.12f32 * math::cos(g * 1.5) * 2.0 * f);
[x, y + off + -0.35, z]
}
_ => {
let off = match self.kind {
MountKind::Donkey | MountKind::Mule => base - 0.25,
MountKind::SkeletonHorse => base - 0.1875,
_ => base,
};
let last = self.horse.last_angry_progress;
if last > 0.0 {
let r = self.body_yaw * (std::f64::consts::PI / 180.0) as f32;
let (f, g) = (math::sin(r), math::cos(r));
let hh = 0.7f32 * last;
let i = 0.15f32 * last;
[
x + f64::from(hh * f),
y + off + -0.35 + f64::from(i),
z - f64::from(hh * g),
]
} else {
[x, y + off + -0.35, z]
}
}
}
}
}
pub fn tick_mount<W: BlockView<State = BlockState>>(
p: &mut super::PlayerState,
x: &mut super::PlayerExt,
mount: &mut Mount,
around: &super::Surroundings<'_>,
input: &TickInput,
world: &W,
out: &mut Packets,
) -> bool {
let rider = Rider {
yaw: input.yaw,
pitch: input.pitch,
sideways: p.sideways_speed,
forward: p.forward_speed,
};
mount_tick(mount, world, x.ultrawarm, rider);
if p.input.sneaking {
p.age += 1;
p.yaw = input.yaw;
p.pitch = input.pitch;
return false;
}
let ride = super::player::RideCtx {
bb: mount.bb,
submerged: false,
boat: false,
jumping_mount: mount.can_jump(),
};
super::player::tick_passenger(p, x, around, ride, input, world, out);
if let Some(s) = x.take_mount_jump() {
mount.set_jump_strength(s);
}
if mount.controlled() {
out.push(mount.vehicle_move());
}
let [sx, sy, sz] = mount.passenger_pos();
p.set_position(sx, sy, sz);
p.fall_distance = 0.0;
true
}
pub fn tick_watched<W: BlockView<State = BlockState>>(m: &mut Mount, world: &W, ultrawarm: bool) {
let rider = Rider {
yaw: 0.0,
pitch: 0.0,
sideways: 0.0,
forward: 0.0,
};
mount_tick(m, world, ultrawarm, rider);
}
impl Mount {
pub fn pushed_player(&mut self, player: [f64; 2]) {
let f = if self.floated { 0.5 } else { 1.0 };
self.vel[0] += -player[0] * f;
self.vel[1] += 0.0;
self.vel[2] += -player[1] * f;
}
}
struct LavaFloor<'a, W> {
w: &'a W,
walk: bool,
min_y: f64,
}
impl<W: BlockView<State = BlockState>> BlockView for LavaFloor<'_, W> {
type State = BlockState;
#[inline]
fn block(&self, x: i32, y: i32, z: i32) -> BlockState {
let s = self.w.block(x, y, z);
if self.walk
&& s.block() == Block::LAVA
&& fluid(s).is_some_and(|f| f.amount == 8 && !f.falling)
&& self.min_y > f64::from(y) + 0.5 - f64::from(1.0e-5f32)
&& !fluid(state_at(self.w, x, y + 1, z)).is_some_and(|f| f.kind == FluidKind::Lava)
{
return Block::SMOOTH_STONE_SLAB.default_state();
}
s
}
#[inline]
fn is_chunk_loaded(&self, chunk_x: i32, chunk_z: i32) -> bool {
self.w.is_chunk_loaded(chunk_x, chunk_z)
}
#[inline]
fn world_border(&self) -> WorldBorder {
self.w.world_border()
}
}
fn mount_tick<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, ultrawarm: bool, r: Rider) {
m.prev = m.pos;
m.floated = false;
if m.kind == MountKind::Strider {
let at = state_at(w, floor(m.pos[0]), floor(m.pos[1]), floor(m.pos[2]));
let landing = landing_state(m, w);
let warm =
at.block() == Block::LAVA || landing.block() == Block::LAVA || m.lava_height > 0.0;
m.cold = !warm;
}
base_tick(m, w, ultrawarm);
tick_movement(m, w, r);
body_control(m);
if m.kind.is_horse_family() {
horse_tick(m);
}
if m.kind == MountKind::Strider {
if in_lava(m) {
let bp = [floor(m.pos[0]), floor(m.pos[1]), floor(m.pos[2])];
let above = m.pos[1] > f64::from(bp[1]) + 0.5 - f64::from(1.0e-5f32);
let lava_up = fluid(state_at(w, bp[0], bp[1] + 1, bp[2]))
.is_some_and(|f| f.kind == FluidKind::Lava);
m.floated = !(above && !lava_up);
if above && !lava_up {
m.on_ground = true;
} else {
m.vel = [m.vel[0] * 0.5, m.vel[1] * 0.5 + 0.05, m.vel[2] * 0.5];
}
}
block_contacts(m, w);
}
}
fn in_lava(m: &Mount) -> bool {
!m.first_update && m.lava_height > 0.0
}
fn landing_state<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> BlockState {
let (lx, ly, lz) = (
floor(m.pos[0]),
floor(m.pos[1] - f64::from(0.2f32)),
floor(m.pos[2]),
);
let s = state_at(w, lx, ly, lz);
if s.is_air() {
let below = state_at(w, lx, ly - 1, lz);
let f = flags(below);
if f.contains(PhysicsFlags::FENCE)
|| f.contains(PhysicsFlags::WALL)
|| f.contains(PhysicsFlags::FENCE_GATE)
{
return below;
}
}
s
}
fn check_water<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W) {
let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
let c = update_in_fluid_as::<W, false>(w, &m.bb, FluidKind::Water, 0.014, true, &mut v);
m.vel = [v.x, v.y, v.z];
m.water_height = c.height;
if c.touching {
m.fall_distance = 0.0;
}
m.touching_water = c.touching;
}
fn base_tick<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, ultrawarm: bool) {
m.water_height = 0.0;
m.lava_height = 0.0;
check_water(m, w);
let speed = if ultrawarm {
0.007
} else {
0.002_333_333_333_333_333_5
};
let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
let c = update_in_fluid_as::<W, false>(w, &m.bb, FluidKind::Lava, speed, true, &mut v);
m.vel = [v.x, v.y, v.z];
m.lava_height = c.height;
if in_lava(m) {
m.fall_distance *= 0.5;
}
m.first_update = false;
}
fn tick_movement<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, r: Rider) {
if m.controlled() {
m.steps = 0;
m.tracked = m.pos;
}
if m.steps > 0 {
let n = f64::from(m.steps);
let x = m.pos[0] + (m.target[0] - m.pos[0]) / n;
let y = m.pos[1] + (m.target[1] - m.pos[1]) / n;
let z = m.pos[2] + (m.target[2] - m.pos[2]) / n;
let g = wrap_degrees(m.target_yaw - f64::from(m.yaw));
m.yaw = (f64::from(m.yaw) + g / n) as f32;
m.pitch = (f64::from(m.pitch) + (m.target_pitch - f64::from(m.pitch)) / n) as f32;
m.steps -= 1;
m.set_position(x, y, z);
m.yaw %= 360.0;
m.pitch %= 360.0;
} else {
m.vel = [m.vel[0] * 0.98, m.vel[1] * 0.98, m.vel[2] * 0.98];
}
if m.head_steps > 0 {
let d = wrap_degrees(m.head_target - f64::from(m.head_yaw));
m.head_yaw = (f64::from(m.head_yaw) + d / f64::from(m.head_steps)) as f32;
m.head_steps -= 1;
}
for v in &mut m.vel {
if v.abs() < 0.003 {
*v = 0.0;
}
}
travel(m, w, r);
}
fn travel<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, r: Rider) {
match m.kind {
MountKind::Pig | MountKind::Strider => {
if m.kind == MountKind::Strider {
m.movement_speed = strider_speed(m);
}
if m.controlled() {
m.yaw = r.yaw;
m.pitch = r.pitch * 0.5;
m.yaw %= 360.0;
m.pitch %= 360.0;
m.body_yaw = m.yaw;
m.head_yaw = m.yaw;
m.step_height = 1.0;
m.flying_speed = m.movement_speed * 0.1;
if m.boost.boosted {
let t = m.boost.ticks;
m.boost.ticks += 1;
if t > m.boost.time {
m.boost.boosted = false;
}
}
let mut f = saddled_speed(m);
if m.boost.boosted {
let k = m.boost.ticks as f32 / m.boost.time as f32 * std::f32::consts::PI;
f += f * 1.15 * math::sin(k);
}
m.movement_speed = f;
living_travel(m, w, V3::new(0.0, 0.0, 1.0));
m.steps = 0;
} else {
m.step_height = 0.5;
m.flying_speed = 0.02;
living_travel(m, w, V3::ZERO);
}
}
_ if m.passenger && m.saddled && !m.kind.is_llama() => {
m.yaw = r.yaw;
m.pitch = r.pitch * 0.5;
m.yaw %= 360.0;
m.pitch %= 360.0;
m.body_yaw = m.yaw;
m.head_yaw = m.body_yaw;
let mut f = r.sideways * 0.5;
let mut g = r.forward;
if g <= 0.0 {
g *= 0.25;
}
let h = m.horse;
if m.on_ground && h.jump_strength == 0.0 && h.angry && !h.jumping {
f = 0.0;
g = 0.0;
}
if h.jump_strength > 0.0 && !h.in_air && m.on_ground {
let d = m.jump_attribute
* f64::from(h.jump_strength)
* f64::from(jump_velocity_multiplier(m, w));
m.vel[1] = d;
m.horse.in_air = true;
if g > 0.0 {
let rad = m.yaw * (std::f64::consts::PI / 180.0) as f32;
let (i, j) = (math::sin(rad), math::cos(rad));
m.vel[0] += f64::from(-0.4 * i * h.jump_strength);
m.vel[2] += f64::from(0.4 * j * h.jump_strength);
}
m.horse.jump_strength = 0.0;
}
m.flying_speed = m.movement_speed * 0.1;
m.movement_speed = m.speed_attribute as f32;
living_travel(m, w, V3::new(f64::from(f), 0.0, f64::from(g)));
if m.on_ground {
m.horse.jump_strength = 0.0;
m.horse.in_air = false;
}
limbs(m);
}
_ => {
m.flying_speed = 0.02;
living_travel(m, w, V3::ZERO);
}
}
}
fn strider_speed(m: &Mount) -> f32 {
m.speed_attribute as f32 * if m.cold { 0.66 } else { 1.0 }
}
fn saddled_speed(m: &Mount) -> f32 {
if m.kind == MountKind::Pig {
m.speed_attribute as f32 * 0.225
} else {
m.speed_attribute as f32 * if m.cold { 0.23 } else { 0.55 }
}
}
fn jump_velocity_multiplier<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> f32 {
let at = jump_multiplier(state_at(
w,
floor(m.pos[0]),
floor(m.pos[1]),
floor(m.pos[2]),
));
if at == 1.0 {
let [x, y, z] = velocity_pos(m);
jump_multiplier(state_at(w, x, y, z))
} else {
at
}
}
fn velocity_pos(m: &Mount) -> [i32; 3] {
[
floor(m.pos[0]),
floor(m.bb.min_y - 0.500_000_1),
floor(m.pos[2]),
]
}
fn limbs(m: &mut Mount) {
let dx = m.pos[0] - m.prev[0];
let dz = m.pos[2] - m.prev[2];
let mut g = ((dx * dx + 0.0 * 0.0 + dz * dz).sqrt() as f32) * 4.0;
if g > 1.0 {
g = 1.0;
}
m.limb_distance += (g - m.limb_distance) * 0.4;
m.limb_angle += m.limb_distance;
}
fn climbing<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> bool {
!m.kind.is_horse_family() && super::entity::climbing_at(w, m.pos)
}
fn living_travel<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, input: V3) {
if m.controlled() {
travel_physics(m, w, input);
}
limbs(m);
}
fn accelerate(m: &mut Mount, speed: f32, input: V3) {
let d = input.length_sq();
if d < 1.0e-7 {
return;
}
let v = if d > 1.0 { input.normalize() } else { input }.scale(f64::from(speed));
let r = m.yaw * (std::f64::consts::PI / 180.0) as f32;
let (f, g) = (f64::from(math::sin(r)), f64::from(math::cos(r)));
m.vel = [
m.vel[0] + (v.x * g - v.z * f),
m.vel[1] + v.y,
m.vel[2] + (v.z * g + v.x * f),
];
}
fn fluid_fall(gravity: f64, falling: bool, v: V3) -> V3 {
let y = if falling && (v.y - 0.005).abs() >= 0.003 && (v.y - gravity / 16.0).abs() < 0.003 {
-0.003
} else {
v.y - gravity / 16.0
};
V3::new(v.x, y, v.z)
}
fn walks_on<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> bool {
m.kind == MountKind::Strider
&& fluid(state_at(
w,
floor(m.pos[0]),
floor(m.pos[1]),
floor(m.pos[2]),
))
.is_some_and(|f| f.kind == FluidKind::Lava)
}
fn travel_physics<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, input: V3) {
let gravity = 0.08;
let falling = m.vel[1] <= 0.0;
let walks = walks_on(m, w);
if m.touching_water && !walks {
let start_y = m.pos[1];
let f = m.kind.water_drag();
accelerate(m, 0.02, input);
let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
mount_move(m, w, v);
let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
if m.horizontal_collision && climbing(m, w) {
v.y = 0.2;
}
let v = V3::new(
v.x * f64::from(f),
v.y * f64::from(0.8f32),
v.z * f64::from(f),
);
let v = fluid_fall(gravity, falling, v);
m.vel = [v.x, v.y, v.z];
jump_out(m, w, v, start_y);
} else if in_lava(m) && !walks {
let start_y = m.pos[1];
accelerate(m, 0.02, input);
let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
mount_move(m, w, v);
let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
let v = if m.lava_height <= 0.4 {
fluid_fall(
gravity,
falling,
V3::new(v.x * 0.5, v.y * f64::from(0.8f32), v.z * 0.5),
)
} else {
v.scale(0.5)
};
let v = V3::new(v.x, v.y + -gravity / 4.0, v.z);
m.vel = [v.x, v.y, v.z];
jump_out(m, w, v, start_y);
} else {
let [px, py, pz] = velocity_pos(m);
let t = slipperiness(state_at(w, px, py, pz));
let u = if m.on_ground { t * 0.91 } else { 0.91 };
let speed = if m.on_ground {
m.movement_speed * (0.216_000_02 / (t * t * t))
} else {
m.flying_speed
};
accelerate(m, speed, input);
if climbing(m, w) {
m.fall_distance = 0.0;
let lim = f64::from(0.15f32);
m.vel[0] = m.vel[0].clamp(-lim, lim);
m.vel[2] = m.vel[2].clamp(-lim, lim);
m.vel[1] = math::java_max(m.vel[1], -lim);
}
let v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
mount_move(m, w, v);
let mut v = V3::new(m.vel[0], m.vel[1], m.vel[2]);
if m.horizontal_collision && climbing(m, w) {
v.y = 0.2;
}
let u = f64::from(u);
m.vel = [v.x * u, (v.y - gravity) * f64::from(0.98f32), v.z * u];
}
}
fn collider<'a, W: BlockView<State = BlockState>>(
w: &'a LavaFloor<'a, W>,
border: &'a BorderShape,
) -> Collider<'a, LavaFloor<'a, W>, NoEntities> {
Collider {
w,
border,
ctx: ShapeCtx {
min_y: w.min_y,
descending: false,
},
entities: NoEntities,
}
}
fn floor_view<'a, W: BlockView<State = BlockState>>(m: &Mount, w: &'a W) -> LavaFloor<'a, W> {
LavaFloor {
w,
walk: m.kind == MountKind::Strider,
min_y: m.pos[1],
}
}
fn jump_out<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, v: V3, start_y: f64) {
if !m.horizontal_collision {
return;
}
let dy = v.y + f64::from(0.6f32) - m.pos[1] + start_y;
let probe = offset(&m.bb, v.x, dy, v.z);
let fw = floor_view(m, w);
let border = BorderShape::new(&w.world_border());
if collider(&fw, &border).is_space_empty(&m.bb, &probe) && !contains_fluid(w, &probe) {
m.vel[1] = f64::from(0.3f32);
}
}
fn contains_fluid<W: BlockView<State = BlockState>>(w: &W, b: &Aabb) -> bool {
let (x0, x1) = (floor(b.min_x), ceil(b.max_x));
let (y0, y1) = (floor(b.min_y), ceil(b.max_y));
let (z0, z1) = (floor(b.min_z), ceil(b.max_z));
(x0..x1).any(|x| (y0..y1).any(|y| (z0..z1).any(|z| fluid(state_at(w, x, y, z)).is_some())))
}
fn mount_move<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W, movement: V3) {
let mut mv = movement;
let mm = m.movement_multiplier;
if mm[0] * mm[0] + mm[1] * mm[1] + mm[2] * mm[2] > 1.0e-7 {
mv = V3::new(mv.x * mm[0], mv.y * mm[1], mv.z * mm[2]);
m.movement_multiplier = [0.0; 3];
m.vel = [0.0; 3];
}
let fw = floor_view(m, w);
let border = BorderShape::new(&w.world_border());
let v = collider(&fw, &border).adjust(mv, &m.bb, m.on_ground, m.step_height);
if v.length_sq() > 1.0e-7 {
m.bb = offset(&m.bb, v.x, v.y, v.z);
m.pos = [
(m.bb.min_x + m.bb.max_x) / 2.0,
m.bb.min_y,
(m.bb.min_z + m.bb.max_z) / 2.0,
];
}
let approx = |a: f64, b: f64| (b - a).abs() < f64::from(1.0e-5f32);
m.horizontal_collision = !approx(mv.x, v.x) || !approx(mv.z, v.z);
m.vertical_collision = mv.y != v.y;
m.on_ground = m.vertical_collision && mv.y < 0.0;
let landed = landing_state(m, w);
let skip = if m.kind == MountKind::Strider {
block_contacts(m, w);
if in_lava(m) {
m.fall_distance = 0.0;
true
} else {
false
}
} else {
false
};
if !skip {
if !m.touching_water {
check_water(m, w);
}
if m.on_ground {
m.fall_distance = 0.0;
} else if v.y < 0.0 {
m.fall_distance = (f64::from(m.fall_distance) - v.y) as f32;
}
}
let old = m.vel;
if mv.x != v.x {
m.vel = [0.0, old[1], old[2]];
}
if mv.z != v.z {
m.vel = [old[0], old[1], 0.0];
}
let block = landed.block();
if mv.y != v.y {
if block == Block::SLIME_BLOCK || is_bed(landed) {
if m.vel[1] < 0.0 {
let k = if block == Block::SLIME_BLOCK {
1.0
} else {
f64::from(0.66f32)
};
m.vel[1] = -m.vel[1] * k;
}
} else {
m.vel[1] *= 0.0;
}
}
if m.on_ground && block == Block::SLIME_BLOCK {
let d = m.vel[1].abs();
if d < 0.1 {
let e = 0.4 + d * 0.2;
m.vel[0] *= e;
m.vel[2] *= e;
}
}
block_contacts(m, w);
let k = f64::from(mount_velocity_multiplier(m, w));
m.vel[0] *= k;
m.vel[2] *= k;
}
fn mount_velocity_multiplier<W: BlockView<State = BlockState>>(m: &Mount, w: &W) -> f32 {
let here = state_at(w, floor(m.pos[0]), floor(m.pos[1]), floor(m.pos[2]));
let f = velocity_multiplier(here);
if here.block() == Block::WATER || here.block() == Block::BUBBLE_COLUMN || f != 1.0 {
f
} else {
let [x, y, z] = velocity_pos(m);
velocity_multiplier(state_at(w, x, y, z))
}
}
fn block_contacts<W: BlockView<State = BlockState>>(m: &mut Mount, w: &W) {
let b = m.bb;
let (x0, y0, z0) = (
floor(b.min_x + 0.001),
floor(b.min_y + 0.001),
floor(b.min_z + 0.001),
);
let (x1, y1, z1) = (
floor(b.max_x - 0.001),
floor(b.max_y - 0.001),
floor(b.max_z - 0.001),
);
if !region_loaded(w, x0, y0, z0, x1, y1, z1) {
return;
}
let (width, _) = m.kind.dimensions();
for x in x0..=x1 {
for y in y0..=y1 {
for z in z0..=z1 {
let s = state_at(w, x, y, z);
let block = s.block();
if block == Block::COBWEB {
m.fall_distance = 0.0;
m.movement_multiplier = [0.25, f64::from(0.05f32), 0.25];
} else if block == Block::SWEET_BERRY_BUSH {
m.fall_distance = 0.0;
m.movement_multiplier = [f64::from(0.8f32), 0.75, f64::from(0.8f32)];
} else if block == Block::HONEY_BLOCK {
let sliding = !m.on_ground
&& !(m.pos[1] > f64::from(y) + 0.9375 - 1.0e-7)
&& !(m.vel[1] >= -0.08)
&& {
let d = (f64::from(x) + 0.5 - m.pos[0]).abs();
let e = (f64::from(z) + 0.5 - m.pos[2]).abs();
let f = 0.4375 + f64::from(width / 2.0);
d + 1.0e-7 > f || e + 1.0e-7 > f
};
if sliding {
if m.vel[1] < -0.13 {
let d = -0.05 / m.vel[1];
m.vel = [m.vel[0] * d, -0.05, m.vel[2] * d];
} else {
m.vel[1] = -0.05;
}
m.fall_distance = 0.0;
}
} else if block == Block::BUBBLE_COLUMN {
let drag = s.get(ghast_data::v1_16_5::Property::Drag) == Some("true");
let v = m.vel[1];
if state_at(w, x, y + 1, z).is_air() {
m.vel[1] = if drag {
math::java_max(-0.9, v - 0.03)
} else {
math::java_min(1.8, v + 0.1)
};
} else {
m.vel[1] = if drag {
math::java_max(-0.3, v - 0.03)
} else {
math::java_min(0.7, v + 0.06)
};
m.fall_distance = 0.0;
}
}
}
}
}
}
fn body_control(m: &mut Mount) {
let dx = m.pos[0] - m.prev[0];
let dz = m.pos[2] - m.prev[2];
if dx * dx + dz * dz > f64::from(2.500_000_3e-7f32) {
m.body_yaw = m.yaw;
m.head_yaw = step_angle(m.head_yaw, m.body_yaw, 75.0);
m.last_head_yaw = m.head_yaw;
m.body_ticks = 0;
} else if (m.head_yaw - m.last_head_yaw).abs() > 15.0 {
m.body_ticks = 0;
m.last_head_yaw = m.head_yaw;
m.body_yaw = step_angle(m.body_yaw, m.head_yaw, 75.0);
} else {
m.body_ticks += 1;
if m.body_ticks > 10 {
let i = m.body_ticks - 10;
let f = (i as f32 / 10.0).clamp(0.0, 1.0);
let g = 75.0 * (1.0 - f);
m.body_yaw = step_angle(m.body_yaw, m.head_yaw, g);
}
}
}
fn horse_tick(m: &mut Mount) {
let controlled = m.controlled();
let h = &mut m.horse;
if controlled && h.angry_ticks > 0 {
h.angry_ticks += 1;
if h.angry_ticks > 20 {
h.angry_ticks = 0;
h.angry = false;
}
}
h.last_angry_progress = h.angry_progress;
if h.angry {
h.angry_progress += (1.0 - h.angry_progress) * 0.4 + 0.05;
if h.angry_progress > 1.0 {
h.angry_progress = 1.0;
}
} else {
h.jumping = false;
let a = h.angry_progress;
h.angry_progress += (0.8 * a * a * a - a) * 0.6 - 0.05;
if h.angry_progress < 0.0 {
h.angry_progress = 0.0;
}
}
}