From c5a37c02de3101da524236010243244d278c5bd4 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 19:32:01 +0100 Subject: [PATCH 1/2] Push sonic boom targets only when hit, by their knockback resistance --- .../examples/probe/mob_ai_probe.rs | 8 ++ crates/horizon-client/src/game/creatures.rs | 115 ++++++++++++------ crates/horizon-client/src/game/lan_mobs.rs | 4 + crates/horizon-client/src/mobs.rs | 9 ++ crates/horizon-client/src/mobs/warden.rs | 31 +---- 5 files changed, 104 insertions(+), 63 deletions(-) diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs index ecdf02901..290d45371 100644 --- a/crates/horizon-client/examples/probe/mob_ai_probe.rs +++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs @@ -568,6 +568,10 @@ fn print_heart( fn player_damage(e: &MobEvent) -> f32 { match e { MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount, + MobEvent::SonicBoom { + target: Who::Player, + .. + } => 10.0, _ => 0.0, } } @@ -579,6 +583,10 @@ fn player_hurt(player: Option<&PlayerView>, e: &MobEvent) -> bool { }; match e { MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount > 0.0, + MobEvent::SonicBoom { + target: Who::Player, + .. + } => true, // ServerExplosion.hurtEntities: within twice the radius every hit does damage. MobEvent::Explode(at, radius) => { let d = Vec3::new(at[0], at[1], at[2]) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 1f560af0d..c7b033c03 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -838,6 +838,44 @@ impl Game<'_> { } } } + /// Melee or ranged damage to another mob; whether the hit landed. + fn mob_hits_mob(&mut self, target: u32, amount: f32, source: Damage) -> bool { + let mut killed = false; + let mut hurt = false; + if let Some(t) = self.mobs.iter_mut().find(|m| m.id == target) { + let hit = t.damage(amount, source); + hurt = hit; + if hit { + killed = !t.alive(); + // The hurt or death sound the mob raised. + let center = t.center(); + for event in std::mem::take(&mut t.outbox) { + if let MobEvent::Sound(s, _, volume, pitch) = event { + self.audio + .play(&format!("minecraft:{s}"), Some(center), volume, pitch); + } + } + } + } + if hurt + && let Some(Who::Mob(attacker)) = source.attacker + && let Some(m) = self + .mobs + .iter() + .find(|m| m.id == attacker && m.species() == Species::Axolotl) + { + self.audio.play( + "minecraft:entity.axolotl.attack", + Some([m.position.x, m.position.y, m.position.z]), + 1.0, + 1.0, + ); + } + if killed && let Some(Who::Mob(killer)) = source.attacker { + self.zombie_killed(killer, target); + } + hurt + } pub(super) fn handle_mob_events(&mut self, events: Vec) -> Result<()> { for e in events { match e { @@ -926,43 +964,48 @@ impl Game<'_> { amount, source, } => { - let mut killed = false; - let mut hurt = false; - if let Some(t) = self.mobs.iter_mut().find(|m| m.id == target) { - let hit = t.damage(amount, source); - hurt = hit; - if hit { - killed = !t.alive(); - // The hurt or death sound the mob raised. - let center = t.center(); - for event in std::mem::take(&mut t.outbox) { - if let MobEvent::Sound(s, _, volume, pitch) = event { - self.audio.play( - &format!("minecraft:{s}"), - Some(center), - volume, - pitch, - ); - } - } + self.mob_hits_mob(target, amount, source); + } + MobEvent::SonicBoom { + target, + attacker, + from, + direction, + } => { + // SonicBoom: the push only follows a hit that landed, scaled + // by the target's KNOCKBACK_RESISTANCE. + let (hurt, resistance) = match target { + Who::Player => { + let hurt = self.mob_hits_player(10.0, from, "minecraft:sonic_boom"); + let resistance = self.player.as_mut().map_or(0.0, |p| { + p.attribute("minecraft:knockback_resistance").unwrap_or(0.0) + }); + (hurt, resistance) } - } - if hurt - && let Some(Who::Mob(attacker)) = source.attacker - && let Some(m) = self - .mobs - .iter() - .find(|m| m.id == attacker && m.species() == Species::Axolotl) - { - self.audio.play( - "minecraft:entity.axolotl.attack", - Some([m.position.x, m.position.y, m.position.z]), - 1.0, - 1.0, - ); - } - if killed && let Some(Who::Mob(killer)) = source.attacker { - self.zombie_killed(killer, target); + Who::Mob(id) => { + let source = Damage { + kind: DamageKind::SonicBoom, + attacker: Some(Who::Mob(attacker)), + from: Some(from), + }; + let hurt = self.mob_hits_mob(id, 10.0, source); + let resistance = self + .mobs + .iter() + .find(|m| m.id == id) + .map_or(0.0, |m| m.attrs.knockback_resistance); + (hurt, resistance) + } + }; + if hurt { + let horizontal = 2.5 * (1.0 - resistance); + let vertical = 0.5 * (1.0 - resistance); + let velocity = [ + direction[0] * horizontal, + direction[1] * vertical, + direction[2] * horizontal, + ]; + self.handle_mob_events(vec![MobEvent::Push { target, velocity }])?; } } MobEvent::FinishAdmiring { mob } => self.finish_admiring(mob), diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 4f53cec60..88f98e3f6 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -66,6 +66,10 @@ pub(super) fn acts_on_player(e: &MobEvent) -> bool { e, MobEvent::AttackPlayer(..) | MobEvent::DamagePlayer { .. } + | MobEvent::SonicBoom { + target: Who::Player, + .. + } | MobEvent::Push { target: Who::Player, .. diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 4b67f738f..49a11e75a 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -961,6 +961,15 @@ pub enum MobEvent { target: Who, velocity: [f64; 3], }, + /// `SonicBoom`: 10 damage to the target, then a push along `direction` + /// (the unit vector from the warden's chest to the target's eyes) only + /// if the hit landed, less the target's knockback resistance. + SonicBoom { + target: Who, + attacker: u32, + from: [f64; 3], + direction: [f64; 3], + }, /// `EvokerWololoSpellGoal`: a blue sheep turns red. Wololo { mob: u32, diff --git a/crates/horizon-client/src/mobs/warden.rs b/crates/horizon-client/src/mobs/warden.rs index 11161ef7c..f6acb3d50 100644 --- a/crates/horizon-client/src/mobs/warden.rs +++ b/crates/horizon-client/src/mobs/warden.rs @@ -697,34 +697,11 @@ pub(super) fn tick_sonic_boom(m: &mut Mob, w: &MobWorld, events: &mut Vec events.push(MobEvent::DamagePlayer { - amount: 10.0, - from, - kind: "minecraft:sonic_boom", - }), - Who::Mob(id) => events.push(MobEvent::AttackMob { - target: id, - amount: 10.0, - source: Damage { - kind: DamageKind::SonicBoom, - attacker: Some(Who::Mob(m.id)), - from: Some(from), - }, - }), - } - let horizontal = 2.5 * (1.0 - resistance); - let vertical = 0.5 * (1.0 - resistance); - events.push(MobEvent::Push { + events.push(MobEvent::SonicBoom { target, - velocity: [ - normal[0] * horizontal, - normal[1] * vertical, - normal[2] * horizontal, - ], + attacker: m.id, + from: at(m), + direction: normal, }); } /// `Warden.tick` on the client: the heartbeat (pitched by the client's copy From d0df2389726563ee5d65efb184108c60f2de0df8 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 21:19:49 +0100 Subject: [PATCH 2/2] Include equipment modifiers in a mob's knockback resistance for the sonic boom push --- crates/horizon-client/src/game/creatures.rs | 2 +- .../horizon-client/src/game/lan_attributes.rs | 31 +++++++++++++++++++ crates/horizon-client/src/mobs/finalize.rs | 11 +++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index c7b033c03..b7fb75fb4 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -993,7 +993,7 @@ impl Game<'_> { .mobs .iter() .find(|m| m.id == id) - .map_or(0.0, |m| m.attrs.knockback_resistance); + .map_or(0.0, |m| self.mob_knockback_resistance(m)); (hurt, resistance) } }; diff --git a/crates/horizon-client/src/game/lan_attributes.rs b/crates/horizon-client/src/game/lan_attributes.rs index 73942e770..9909c88f6 100644 --- a/crates/horizon-client/src/game/lan_attributes.rs +++ b/crates/horizon-client/src/game/lan_attributes.rs @@ -24,6 +24,37 @@ fn slot_in_group(group: &str, slot: &str) -> bool { } impl Game<'_> { + /// `getAttributeValue(KNOCKBACK_RESISTANCE)` of a mob: its own value plus + /// the modifiers of the items it wears or holds in matching slots. + pub(super) fn mob_knockback_resistance(&self, m: &Mob) -> f64 { + let e = &m.extra; + let slots = [ + ("mainhand", &e.main_hand), + ("offhand", &e.off_hand), + ("feet", &e.feet), + ("legs", &e.legs), + ("chest", &e.chest), + ("head", &e.head), + ]; + let mut extra = Vec::new(); + for (slot, item) in slots { + let Some(info) = item.as_ref().and_then(|i| self.res.items().get(i)) else { + continue; + }; + for (attribute, _, amount, op, group) in &info.modifiers { + if attribute == "minecraft:knockback_resistance" && slot_in_group(group, slot) { + let op = match operation_named(op) { + 1 => Op::AddMultipliedBase, + 2 => Op::AddMultipliedTotal, + _ => Op::AddValue, + }; + extra.push((*amount, op)); + } + } + } + finalize::value_with(m, Attr::KnockbackResistance, &extra) + } + /// The registry id of a client-syncable attribute the entity type has /// (`AttributeMap.getInstance` is null for the others), by name. fn synced_attribute(&self, entity: &str, name: &str) -> Option { diff --git a/crates/horizon-client/src/mobs/finalize.rs b/crates/horizon-client/src/mobs/finalize.rs index 4eeed885b..be1ff952a 100644 --- a/crates/horizon-client/src/mobs/finalize.rs +++ b/crates/horizon-client/src/mobs/finalize.rs @@ -516,9 +516,13 @@ fn effect_modifiers(m: &Mob, a: Attr) -> impl Iterator + '_ { } /// `AttributeInstance.getValue`: the base, then the modifiers by operation. pub fn value(m: &Mob, a: Attr) -> f64 { - compute(m, a, true) + compute(m, a, true, &[]) } -fn compute(m: &Mob, a: Attr, with_transient: bool) -> f64 { +/// `value` with more modifiers, such as the ones worn equipment grants. +pub fn value_with(m: &Mob, a: Attr, extra: &[(f64, Op)]) -> f64 { + compute(m, a, true, extra) +} +fn compute(m: &Mob, a: Attr, with_transient: bool, extra: &[(f64, Op)]) -> f64 { let mut base = base_value(m, a); let empty = Vec::new(); let mods = m.extra.attributes.as_ref().map_or(&empty, |s| &s.modifiers); @@ -531,6 +535,7 @@ fn compute(m: &Mob, a: Attr, with_transient: bool) -> f64 { ops.extend(transient(m, a)); ops.extend(effect_modifiers(m, a)); } + ops.extend(extra.iter().copied()); for (amount, op) in &ops { if *op == Op::AddValue { base += amount; @@ -573,7 +578,7 @@ fn refresh(m: &mut Mob, a: Attr) { /// The value without the transient baby modifier (`speed_bonus` applies /// that one to movement speed). fn base_with_modifiers(m: &Mob, a: Attr) -> f64 { - compute(m, a, false) + compute(m, a, false, &[]) } /// `AttributeInstance.setBaseValue`. pub fn set_base(m: &mut Mob, a: Attr, v: f64) {