Round instant potion effects and measure splash range from the body

Instant health and harm round to the nearest point instead of truncating. A splash potion measures the distance to the entity's box, not its centre, so a potion landing next to a player hits harder and reaches farther.
This commit is contained in:
Kacpherek
2026-10-26 15:14:30 +01:00
parent 8027cb4176
commit d2105fe66c
2 changed files with 44 additions and 22 deletions
+4 -3
View File
@@ -129,15 +129,16 @@ fn periodic(id: &str, tick: i32, amp: i32) -> Option<EffectAction> {
_ => None,
}
}
/// `InstantenousMobEffect.applyInstantenousEffect` (potions apply these directly).
/// `HealOrHarmMobEffect.applyInstantaneousEffect` (potions apply these directly;
/// the amount rounds to the nearest whole point).
pub fn instant(id: &str, amp: i32, scale: f64) -> Option<EffectAction> {
match id {
"instant_health" => Some(EffectAction::Heal(
(scale * f64::from(4 << amp.min(30))) as i32 as f32,
(scale * f64::from(4 << amp.min(30)) + 0.5) as i32 as f32,
)),
"instant_damage" => Some(EffectAction::Hurt(
"minecraft:magic",
(scale * f64::from(6 << amp.min(30))) as i32 as f32,
(scale * f64::from(6 << amp.min(30)) + 0.5) as i32 as f32,
)),
"saturation" => Some(EffectAction::Eat(amp + 1, 1.0)),
_ => None,
+40 -19
View File
@@ -13,7 +13,10 @@ use horizon_client::{
items::ItemStack,
mesher::Vertex,
};
use horizon_core::{geometry::Vec3, random::RandomSource};
use horizon_core::{
geometry::{Aabb, Vec3},
random::RandomSource,
};
/// A thrown splash or lingering potion in flight.
#[derive(Clone, Debug)]
@@ -44,6 +47,22 @@ fn mob_effect_id(id: &str) -> Option<&'static str> {
.get(id)
.copied()
}
/// `ThrownSplashPotion.onHitAsPotion`: the squared distance from the potion's
/// box at the hit point to an entity's box grown by the hit margin
/// (`ProjectileUtil.computeMargin`), for entities that meet the potion's box
/// grown by (4, 2, 4); none when it is 16 or more.
fn splash_distance_sqr(at: Vec3, age: u32, bounds: Aabb) -> Option<f64> {
let potion = Aabb::new(
Vec3::new(at.x - 0.125, at.y, at.z - 0.125),
Vec3::new(at.x + 0.125, at.y + 0.25, at.z + 0.125),
);
if !potion.inflate(Vec3::new(4.0, 2.0, 4.0)).intersects(bounds) {
return None;
}
let margin = ((f64::from(age) - 2.0) / 20.0).clamp(0.0, 0.3);
let d2 = potion.distance_squared_to_box(bounds.inflate(Vec3::new(margin, margin, margin)));
(d2 < 16.0).then_some(d2)
}
impl Game<'_> {
/// A splash potion, lingering potion or experience bottle in flight (the
/// player's throws and dispensers both end up here).
@@ -516,7 +535,7 @@ impl Game<'_> {
}
}
if let Some(p) = hit {
landed.push((t.stack.clone(), p));
landed.push((t.stack.clone(), p, t.age));
return false;
}
t.position = next;
@@ -535,8 +554,8 @@ impl Game<'_> {
t.age < 1200 && t.position.y > -128.0
});
self.potions.thrown = thrown;
for (stack, at) in landed {
self.splash(&stack, at);
for (stack, at, age) in landed {
self.splash(&stack, at, age);
}
}
/// `ThrownItemRenderer` for thrown splash and lingering potions and
@@ -589,17 +608,20 @@ impl Game<'_> {
&mut self,
effects: &'static [(&'static str, i32, i32)],
at: Vec3,
age: u32,
scale: f64,
) {
if let Some(p) = self.player.as_ref() {
let center = Vec3::new(
p.position.x,
p.position.y + f64::from(p.dimensions.height) / 2.0,
p.position.z,
let w = f64::from(p.dimensions.width) / 2.0;
let bounds = Aabb::new(
Vec3::new(p.position.x - w, p.position.y, p.position.z - w),
Vec3::new(
p.position.x + w,
p.position.y + f64::from(p.dimensions.height),
p.position.z + w,
),
);
let d2 =
(center.x - at.x).powi(2) + (center.y - at.y).powi(2) + (center.z - at.z).powi(2);
if d2 < 16.0 && (center.y - at.y).abs() <= 2.0 + f64::from(p.dimensions.height) {
if let Some(d2) = splash_distance_sqr(at, age, bounds) {
let intensity = 1.0 - d2.sqrt() / 4.0;
for (id, duration, amp) in effects {
let instant = brewing::effect(id).is_some_and(|e| e.instant);
@@ -620,7 +642,7 @@ impl Game<'_> {
}
}
/// `ThrownSplashPotion.onHitAsPotion` (lingering clouds apply as a weaker splash).
fn splash(&mut self, stack: &ItemStack, at: Vec3) {
fn splash(&mut self, stack: &ItemStack, at: Vec3, age: u32) {
if &*stack.item == "minecraft:experience_bottle" {
// ThrownExperienceBottle.onHit: the water-coloured splash (level
// event 2002) and 3 to 11 experience.
@@ -681,15 +703,14 @@ impl Game<'_> {
};
// Every player in range (`ThrownSplashPotion.applySplash` over
// `LivingEntity`s, the players among them).
self.for_each_player(|g| g.splash_on_player(def.effects, at, scale));
self.for_each_player(|g| g.splash_on_player(def.effects, at, age, scale));
// Mobs take the instant effects (undead invert healing and harming).
let undead = &self.res.stations().enchantments.entity_tags;
let mut hits = Vec::new();
for (i, m) in self.mobs.iter().enumerate() {
let d2 = (m.position.x - at.x).powi(2)
+ (m.position.y + m.kind.height / 2.0 - at.y).powi(2)
+ (m.position.z - at.z).powi(2);
if m.alive() && d2 < 16.0 {
if m.alive()
&& let Some(d2) = splash_distance_sqr(at, age, m.bounds())
{
let inverted = undead
.get("minecraft:inverted_healing_and_harm")
.is_some_and(|t| t.contains(&m.kind.id));
@@ -716,12 +737,12 @@ impl Game<'_> {
let m = &mut self.mobs[i];
if harm {
m.hurt(
(intensity * f64::from(6 << amp)) as i32 as f32,
(intensity * f64::from(6 << amp) + 0.5) as i32 as f32,
Some([at.x, at.y, at.z]),
&mut self.world.random,
);
} else {
m.health = (m.health + (intensity * f64::from(4 << amp)) as i32 as f32)
m.health = (m.health + (intensity * f64::from(4 << amp) + 0.5) as i32 as f32)
.min(m.kind.max_health);
}
}