From 9d9f259f94a5a9482e3ea397bfc4a13d9a6e137b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:44:13 +0200 Subject: [PATCH] Combat: sprint and yaw knockback (Player.causeExtraKnockback); vanilla knockback on the player Player.attack adds causeExtraKnockback after the hurt: the weapon's knockback enchantment (halved) plus 0.5 for a sprinting full-strength hit, pushed along the attacker's yaw (sin, -cos) rather than away from it, which lets a player steer a hit target by turning while attacking (knockback displacement). Horizon had only the enchantment part, on mobs; sprinting hits added nothing and players (LAN PvP) got no extra knockback at all. Now: - player hits on mobs and players: one extra knockback of that strength with LivingEntity.knockback (knockback resistance, lift only from the ground), the attack.knockback sound, and the attacker's sprint stops; - the knockback a player takes (mob and PvP hits, dealDefaultKnockback with 0.4) reads its knockback resistance (netherite) and only lifts from the ground: an airborne player kept being thrown up by every hit. Fixes #504 Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 10 +++ crates/horizon-client/src/game/creatures.rs | 38 ++++++++-- .../src/game/enchant_effects.rs | 75 +++++++++++-------- crates/horizon-client/src/game/lan_pvp.rs | 47 +++++++++--- 4 files changed, 123 insertions(+), 47 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 4013bb04b..f3e429084 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6717,6 +6717,7 @@ impl Game<'_> { let target_kind = format!("minecraft:{}", target_kind.trim_start_matches("minecraft:")); let (amount, scale, crit) = self.melee_damage(&target_kind); let sweep = self.sweep_attack_position(scale, crit); + let (knockback, knockback_attack) = self.extra_knockback(&target_kind, scale); self.attack_ticks = 0; if let Some(p) = self.player.as_mut() && !self.creative @@ -6725,6 +6726,10 @@ impl Game<'_> { } let player = self.player.as_ref(); let from = player.map(|p| [p.position.x, p.position.y, p.position.z]); + if knockback_attack { + self.audio + .play("minecraft:entity.player.attack.knockback", from, 1.0, 1.0); + } self.swing_arm(); // LivingEntity.setLastHurtMob (tamed wolves join in). self.combat.last_hurt_mob = Some((self.mobs[index].id, self.world.game_time)); @@ -6734,6 +6739,11 @@ impl Game<'_> { None => horizon_client::mobs::Damage::of(horizon_client::mobs::DamageKind::Generic), }; if self.hurt_by_player(index, amount, source) { + if knockback > 0.0 { + let (xd, zd) = self.extra_knockback_direction(); + self.mobs[index].knockback(f64::from(knockback), xd, zd); + self.stop_sprint_after_extra_knockback(); + } let mob = &self.mobs[index]; // Player.damageStatsAndHearts: the damage the hit actually did. let actual = old_health - mob.health; diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index db793d993..113558e34 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1440,13 +1440,8 @@ impl Game<'_> { o.hurt } { - let (dx, dz) = (player.position.x - from[0], player.position.z - from[2]); - let l = (dx * dx + dz * dz).sqrt().max(1e-4); - player.velocity = Vec3::new( - player.velocity.x / 2.0 + dx / l * 0.4, - (player.velocity.y / 2.0 + 0.4).min(0.4), - player.velocity.z / 2.0 + dz / l * 0.4, - ); + let (xd, zd) = (from[0] - player.position.x, from[2] - player.position.z); + self.knock_back_player(f64::from(0.4_f32), xd, zd); self.hurt_armor(amount); self.thorns(from); } @@ -1455,6 +1450,35 @@ impl Game<'_> { } outcome.hurt } + /// `LivingEntity.knockback` on the player: pushed `power` away from the + /// direction (`xd`, `zd`), less its knockback resistance, and up only from + /// the ground. + pub(super) fn knock_back_player(&mut self, power: f64, mut xd: f64, mut zd: f64) { + let Some(p) = self.player.as_mut() else { + return; + }; + let resistance = p.attribute("minecraft:knockback_resistance").unwrap_or(0.0); + let power = power * (1.0 - resistance); + if power <= 0.0 { + return; + } + while xd * xd + zd * zd < f64::from(1.0e-5_f32) { + let r = &mut self.world.random; + xd = (r.next_double() - r.next_double()) * 0.01; + zd = (r.next_double() - r.next_double()) * 0.01; + } + let l = (xd * xd + zd * zd).sqrt(); + let v = p.velocity; + p.velocity = Vec3::new( + v.x / 2.0 - xd / l * power, + if p.ground.on_ground { + (v.y / 2.0 + power).min(0.4) + } else { + v.y + }, + v.z / 2.0 - zd / l * power, + ); + } /// A status effect from a mob, arrow or splash potion (`addEffect`). pub(super) fn give_player_effect(&mut self, effect: &'static str, ticks: i32, amplifier: i32) { if self.creative || self.spectator() { diff --git a/crates/horizon-client/src/game/enchant_effects.rs b/crates/horizon-client/src/game/enchant_effects.rs index 0aa3138f6..42b505152 100644 --- a/crates/horizon-client/src/game/enchant_effects.rs +++ b/crates/horizon-client/src/game/enchant_effects.rs @@ -6,7 +6,7 @@ //! the armor attribute effects (respiration, depth strider, ...). use super::Game; use horizon_client::{enchant::EffectContext, items::ItemStack}; -use horizon_core::{geometry::Vec3, random::RandomSource}; +use horizon_core::random::RandomSource; impl Game<'_> { /// `Player.getDestroySpeed` after the tool speed: mining efficiency, haste @@ -84,7 +84,50 @@ impl Game<'_> { - fx.amplifier("weakness") .map_or(0.0, |a| 4.0 * (a + 1) as f32) } - /// After a successful melee hit: knockback enchantment and fire aspect. + /// `Player.attack`'s extra knockback: `getKnockback` (the weapon's knockback + /// enchantment, halved) plus 0.5 for a sprinting full-strength hit, and + /// whether the hit is that knockback attack. + pub(super) fn extra_knockback(&mut self, target_kind: &str, scale: f32) -> (f32, bool) { + let knockback_attack = scale > 0.9 && self.player.as_ref().is_some_and(|p| p.sprinting); + let enchanted = match self.inventory[self.selected].clone() { + Some(held) => { + let ctx = EffectContext { + this: Some(target_kind), + attacker: Some("minecraft:player"), + direct_attacker: Some("minecraft:player"), + is_direct: true, + items: Some(self.res.items()), + ..Default::default() + }; + self.res.stations().enchantments.modify_value( + Some(&held), + "minecraft:knockback", + &ctx, + &mut self.world.random, + 0.0, + ) + } + None => 0.0, + }; + let bonus = if knockback_attack { 0.5 } else { 0.0 }; + (enchanted / 2.0 + bonus, knockback_attack) + } + /// `causeExtraKnockback`'s direction, `(sin yRot, -cos yRot)`: the target + /// flies where the attacker looks, not away from it. + pub(super) fn extra_knockback_direction(&self) -> (f64, f64) { + let yaw = self.yaw.to_radians(); + (f64::from(yaw.sin()), -f64::from(yaw.cos())) + } + /// The attacker's side of `causeExtraKnockback` as the attacking client + /// sees it: the server's player stops sprinting, and its flag reaches the + /// client. (The server's 0.6 speed cut never does: the client's own + /// `Player.attack` simulates no hurt, so it skips the knockback.) + pub(super) fn stop_sprint_after_extra_knockback(&mut self) { + if let Some(p) = self.player.as_mut() { + let _ = p.set_sprinting(false); + } + } + /// After a successful melee hit: fire aspect and wind burst. pub(super) fn post_attack(&mut self, mob: usize) { let Some(held) = self.inventory[self.selected].clone() else { return; @@ -99,34 +142,6 @@ impl Game<'_> { items: Some(self.res.items()), ..Default::default() }; - let knockback = self.res.stations().enchantments.modify_value( - Some(&held), - "minecraft:knockback", - &ctx, - &mut self.world.random, - 0.0, - ); - if knockback > 0.0 { - // LivingEntity.knockback(strength * 0.5, sin(yRot), -cos(yRot)). - let strength = f64::from(knockback) * 0.5; - let (s, c) = ( - f64::from(self.yaw.to_radians().sin()), - f64::from(self.yaw.to_radians().cos()), - ); - let (dx, dz) = (s, -c); - let l = (dx * dx + dz * dz).sqrt().max(1e-5); - let m = &mut self.mobs[mob]; - let v = m.velocity; - m.velocity = Vec3::new( - v.x / 2.0 - dx / l * strength, - if m.on_ground { - (v.y / 2.0 + strength).min(0.4) - } else { - v.y - }, - v.z / 2.0 - dz / l * strength, - ); - } // minecraft:post_attack ignite effects of the attacker's weapon (fire aspect). let mut ignite = 0.0f32; for (id, level) in held.enchantments() { diff --git a/crates/horizon-client/src/game/lan_pvp.rs b/crates/horizon-client/src/game/lan_pvp.rs index 01537cee6..a7347ab2a 100644 --- a/crates/horizon-client/src/game/lan_pvp.rs +++ b/crates/horizon-client/src/game/lan_pvp.rs @@ -20,6 +20,10 @@ pub(super) struct Strike { crit: bool, from: [f64; 3], sweep: Option<[f64; 3]>, + /// `causeExtraKnockback`: its strength and direction (the attacker's + /// yaw), and whether the hit is a sprinting knockback attack. + knockback: (f32, f64, f64), + knockback_attack: bool, /// The attacker's team, for the victim's `Player.canHarmPlayer`. team: Option, } @@ -51,8 +55,11 @@ impl Game<'_> { let Some(mut host) = self.lan.take() else { return false; }; - if let Some(strike) = self.pvp_strike() { - self.pvp_land(&mut host, Victim::Guest(conn), &strike); + if let Some(strike) = self.pvp_strike() + && self.pvp_land(&mut host, Victim::Guest(conn), &strike) + && strike.knockback.0 > 0.0 + { + self.stop_sprint_after_extra_knockback(); } self.lan = Some(host); true @@ -77,6 +84,8 @@ impl Game<'_> { let from = [p.position.x, p.position.y, p.position.z]; let (amount, scale, crit) = self.melee_damage("minecraft:player"); let sweep = self.sweep_attack_position(scale, crit); + let (strength, knockback_attack) = self.extra_knockback("minecraft:player", scale); + let (xd, zd) = self.extra_knockback_direction(); self.attack_ticks = 0; if let Some(p) = self.player.as_mut() && !self.creative @@ -90,12 +99,15 @@ impl Game<'_> { crit, from, sweep, + knockback: (strength, xd, zd), + knockback_attack, team: self.player_team().cloned(), }) } - /// The hit landing on the victim, then the attacker's attack sound. - pub(super) fn pvp_land(&mut self, host: &mut LanHost, victim: Victim, s: &Strike) { + /// The hit landing on the victim with its extra knockback, then the + /// attacker's attack sound. Whether the victim was hurt. + pub(super) fn pvp_land(&mut self, host: &mut LanHost, victim: Victim, s: &Strike) -> bool { // ServerPlayer.canHarmPlayer: the pvp game rule, then the victim's // team (Player.canHarmPlayer: allies only with friendly fire on). let team = match victim { @@ -106,15 +118,29 @@ impl Game<'_> { .and_then(|g| self.score.board.players_team(&g.name)), }; let harm = horizon_client::scoreboard::teams_allow_damage(team, s.team.as_ref()); + if s.knockback_attack { + self.audio.play( + "minecraft:entity.player.attack.knockback", + Some(s.from), + 1.0, + 1.0, + ); + } + // Player.attack: the hurt (with its knockback away from the + // attacker), then causeExtraKnockback along the attacker's yaw. + let land = |g: &mut Self| { + let hurt = g.mob_hits_player(s.amount, s.from, "minecraft:player_attack"); + let (strength, xd, zd) = s.knockback; + if hurt && strength > 0.0 { + g.knock_back_player(f64::from(strength), xd, zd); + } + hurt + }; let hurt = self.ui.rules.bool("pvp") && harm && match victim { - Victim::Host => self.mob_hits_player(s.amount, s.from, "minecraft:player_attack"), - Victim::Guest(conn) => self - .guest_hit_by(host, conn, |g| { - g.mob_hits_player(s.amount, s.from, "minecraft:player_attack") - }) - .unwrap_or(false), + Victim::Host => land(self), + Victim::Guest(conn) => self.guest_hit_by(host, conn, land).unwrap_or(false), }; let sound = match (hurt, s.crit, s.scale > 0.9) { (false, ..) => "minecraft:entity.player.attack.nodamage", @@ -127,5 +153,6 @@ impl Game<'_> { } else { self.audio.play(sound, Some(s.from), 1.0, 1.0); } + hurt } }