116 lines
3.9 KiB
Rust
116 lines
3.9 KiB
Rust
//! Living-entity fall damage calculation, impulse exemption and callback order.
|
|
use horizon_core::geometry::Vec3;
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
pub struct ImpulseContext {
|
|
pub reset_grace_time: i32,
|
|
pub impact_position: Option<Vec3>,
|
|
}
|
|
impl ImpulseContext {
|
|
pub fn set_ignore_fall_damage(&mut self, ignore: bool, position: Option<Vec3>) {
|
|
if ignore {
|
|
self.apply_grace_time(40);
|
|
self.impact_position = position;
|
|
} else {
|
|
self.reset_grace_time = 0;
|
|
}
|
|
}
|
|
pub fn apply_grace_time(&mut self, ticks: i32) {
|
|
self.reset_grace_time = self.reset_grace_time.max(ticks);
|
|
}
|
|
pub fn reset(&mut self) {
|
|
self.reset_grace_time = 0;
|
|
self.impact_position = None;
|
|
}
|
|
pub fn try_reset(&mut self) {
|
|
if self.reset_grace_time == 0 {
|
|
self.reset();
|
|
}
|
|
}
|
|
pub fn is_in_grace_time(&self) -> bool {
|
|
self.reset_grace_time > 0
|
|
}
|
|
pub fn ignores_fall_damage(&self) -> bool {
|
|
self.impact_position.is_some()
|
|
}
|
|
}
|
|
pub trait LivingFallWorld {
|
|
type Error;
|
|
type Sound: Copy;
|
|
fn impact_position(&self) -> Option<Vec3>;
|
|
fn y(&self) -> f64;
|
|
fn reset_impulse(&mut self) -> Result<(), Self::Error>;
|
|
fn try_reset_impulse(&mut self) -> Result<(), Self::Error>;
|
|
fn immune_to_fall_damage(&mut self) -> Result<bool, Self::Error>;
|
|
fn propagate_fall_to_passengers(
|
|
&mut self,
|
|
distance: f64,
|
|
multiplier: f32,
|
|
) -> Result<(), Self::Error>;
|
|
fn safe_fall_distance(&mut self) -> Result<f64, Self::Error>;
|
|
fn fall_damage_multiplier(&mut self) -> Result<f64, Self::Error>;
|
|
/// Select the entity type's small/big fall sound. Big applies above four.
|
|
fn fall_sound(&mut self, big: bool) -> Result<Self::Sound, Self::Error>;
|
|
fn play_fall_sound(&mut self, sound: Self::Sound) -> Result<(), Self::Error>;
|
|
fn play_block_fall_sound(&mut self) -> Result<(), Self::Error>;
|
|
/// Dispatch native Entity.hurt semantics (server-only), preserving its source.
|
|
fn hurt(&mut self, damage: f32) -> Result<(), Self::Error>;
|
|
}
|
|
pub fn calculate_fall_damage<W: LivingFallWorld>(
|
|
world: &mut W,
|
|
distance: f64,
|
|
multiplier: f32,
|
|
) -> Result<i32, W::Error> {
|
|
if world.immune_to_fall_damage()? {
|
|
return Ok(0);
|
|
}
|
|
let power = distance + 1.0e-6 - world.safe_fall_distance()?;
|
|
Ok((power * f64::from(multiplier) * world.fall_damage_multiplier()?).floor() as i32)
|
|
}
|
|
fn java_min(a: f64, b: f64) -> f64 {
|
|
if a.is_nan() {
|
|
a
|
|
} else if b.is_nan() {
|
|
b
|
|
} else if a == 0.0 && b == 0.0 {
|
|
f64::from_bits(a.to_bits() | b.to_bits())
|
|
} else {
|
|
a.min(b)
|
|
}
|
|
}
|
|
/// Default LivingEntity and Entity fall behavior. Entity-type overrides of the
|
|
/// calculation/parent method are a separate dispatch layer. The same damage
|
|
/// source must be retained by the provider for riders and the final hurt call.
|
|
pub fn cause_living_fall_damage<W: LivingFallWorld>(
|
|
world: &mut W,
|
|
distance: f64,
|
|
multiplier: f32,
|
|
) -> Result<bool, W::Error> {
|
|
let distance = if let Some(impact) = world.impact_position() {
|
|
let distance = java_min(distance, impact.y - world.y());
|
|
if distance <= 0.0 {
|
|
world.reset_impulse()?;
|
|
} else {
|
|
world.try_reset_impulse()?;
|
|
}
|
|
distance
|
|
} else {
|
|
distance
|
|
};
|
|
// Base Entity.causeFallDamage returns false, even after notifying riders.
|
|
if !world.immune_to_fall_damage()? {
|
|
world.propagate_fall_to_passengers(distance, multiplier)?;
|
|
}
|
|
let damage = calculate_fall_damage(world, distance, multiplier)?;
|
|
if damage > 0 {
|
|
world.reset_impulse()?;
|
|
let sound = world.fall_sound(damage > 4)?;
|
|
world.play_fall_sound(sound)?;
|
|
world.play_block_fall_sound()?;
|
|
world.hurt(damage as f32)?;
|
|
Ok(true)
|
|
} else {
|
|
Ok(false)
|
|
}
|
|
}
|