From 7b84a7ae91fe62175c76024ef497eb7a1d83932a Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:25:43 +0100 Subject: [PATCH] Let a sweep attack hurt and push back the other players next to the target --- crates/horizon-client/src/game.rs | 35 +++-- crates/horizon-client/src/game/creatures.rs | 3 + crates/horizon-client/src/game/lan.rs | 2 + crates/horizon-client/src/game/lan_pvp.rs | 137 +++++++++++++++++++- 4 files changed, 163 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7699e83d7..5df9291fe 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -7899,18 +7899,9 @@ impl Game<'_> { self.audio .play("minecraft:entity.player.attack.sweep", from, 1.0, 1.0); } - /// The damage and knockback part of `Player.doSweepAttack` after a sweep - /// hit on the mob at `target`: every other living mob in its box grown by - /// (1, 0.25, 1) and closer than 3 blocks (`distanceToSqr < 9`, in creative - /// too), but the player's allies - /// (`isAlliedTo`), takes `1 + sweeping_damage_ratio * baseDamage` with the - /// weapon's enchantments, scaled by the attack strength, and is pushed 0.4 - /// along the player's yaw. - pub(super) fn sweep_damage(&mut self, target: usize, scale: f32) { - let Some(p) = self.player.as_ref() else { - return; - }; - let from = [p.position.x, p.position.y, p.position.z]; + /// What `Player.doSweepAttack` deals before the target's enchantment + /// bonus and the attack strength: `1 + sweeping_damage_ratio * baseDamage`. + pub(super) fn sweep_base_damage(&self, scale: f32) -> f32 { // `baseDamage` of `Player.attack` before a critical hit (a sweep has // none) and the enchantments: the attack damage attribute with the // strength and weakness effects, scaled by the attack strength. @@ -7926,7 +7917,21 @@ impl Game<'_> { "minecraft:sweeping_damage_ratio", ) .clamp(0.0, 1.0) as f32; - let sweep_damage = 1.0 + ratio * base_damage; + 1.0 + ratio * base_damage + } + /// The damage and knockback part of `Player.doSweepAttack` after a sweep + /// hit on the mob at `target`: every other living mob in its box grown by + /// (1, 0.25, 1) and closer than 3 blocks (`distanceToSqr < 9`, in creative + /// too), but the player's allies + /// (`isAlliedTo`), takes `1 + sweeping_damage_ratio * baseDamage` with the + /// weapon's enchantments, scaled by the attack strength, and is pushed 0.4 + /// along the player's yaw. + pub(super) fn sweep_damage(&mut self, target: usize, scale: f32) { + let Some(p) = self.player.as_ref() else { + return; + }; + let from = [p.position.x, p.position.y, p.position.z]; + let sweep_damage = self.sweep_base_damage(scale); let hit_box = self.mobs[target] .bounds() .inflate(horizon_core::geometry::Vec3::new(1.0, 0.25, 1.0)); @@ -7961,6 +7966,10 @@ impl Game<'_> { .insert(self.mobs[i].id, self.lan_acting); } } + // `getEntitiesOfClass(LivingEntity.class)` takes the other players too. + let mut sweep = self.player_sweep(sweep_damage, scale); + sweep.hit_box = hit_box; + self.sweep_other_players(sweep); } /// `Player.attack`'s sounds when a vehicle takes the hit (it always does, /// and can't be hit critically): knockback when sprinting at full diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2bc40a7af..51437944f 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -46,6 +46,9 @@ pub(super) struct Creatures { pub projectiles: Vec, /// Wind bursts from a guest's turn, waiting for it to end (game/wind.rs). pub wind_bursts: Vec, + /// Sweep attacks from a guest's turn, waiting for it to end + /// (game/lan_pvp.rs). + pub sweeps: Vec, /// Ticks left of the elder guardian's apparition (MobAppearanceParticle). pub elder_apparition: i32, /// The wandering trader spawner's clock. diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 28ec3fe8a..cba61960e 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -1166,6 +1166,8 @@ impl Game<'_> { self.lan = Some(host); // A mace's wind burst from a guest's attack. let _ = self.flush_wind_bursts(); + // A sweep attack's reach to the other players from a guest's attack. + self.flush_sweeps(); } fn lan_guest_joined( diff --git a/crates/horizon-client/src/game/lan_pvp.rs b/crates/horizon-client/src/game/lan_pvp.rs index 65520221a..614d23d79 100644 --- a/crates/horizon-client/src/game/lan_pvp.rs +++ b/crates/horizon-client/src/game/lan_pvp.rs @@ -12,6 +12,7 @@ use horizon_client::{ mobs::Who, projectiles::{Hit, Projectile, ProjectileKind}, }; +use horizon_core::geometry::{Aabb, Vec3}; /// The player projectiles that meet other players: arrows (their hurt), and /// snowballs and eggs, which break on them (`Player.hurtServer` takes no 0 @@ -24,7 +25,7 @@ pub(super) fn meets_players(kind: &ProjectileKind) -> bool { } /// Who a player's attack lands on. -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq)] pub(super) enum Victim { Host, Guest(u32), @@ -47,6 +48,26 @@ pub(super) struct Strike { knockback_attack: bool, /// The attacker's team, for the victim's `Player.canHarmPlayer`. team: Option, + /// The sweep's damage to the players around the victim. + sweep_hit: Option, +} + +/// A sweep attack's reach to the other players (`Player.doSweepAttack`), +/// worked out as the attacker. +#[derive(Clone)] +pub(super) struct Sweep { + /// The attacking guest (none: the host). + attacker: Option, + from: [f64; 3], + /// The damage after the enchantments and the attack strength. + damage: f32, + /// The push along the attacker's yaw. + knockback: (f64, f64), + team: Option, + /// The target's box grown by (1, 0.25, 1). + pub(super) hit_box: Aabb, + /// The player the sweep started from, which it spares. + skip: Option, } impl Strike { @@ -113,6 +134,10 @@ impl Game<'_> { let from = [p.position.x, p.position.y, p.position.z]; let (amount, scale, crit, magic) = self.melee_damage("minecraft:player"); let sweep = self.sweep_attack_position(scale, crit); + let sweep_hit = sweep.map(|_| { + let base = self.sweep_base_damage(scale); + self.player_sweep(base, scale) + }); let (strength, knockback_attack) = self.extra_knockback("minecraft:player", scale); let (xd, zd) = self.extra_knockback_direction(); self.attack_ticks = 0; @@ -133,6 +158,7 @@ impl Game<'_> { knockback: (strength, xd, zd), knockback_attack, team: self.player_team().cloned(), + sweep_hit, }) } @@ -300,9 +326,118 @@ impl Game<'_> { }; if let Some(at) = s.sweep.filter(|_| hurt) { self.sweep_attack(at, Some(s.from)); + // The other players next to the victim (Player.doSweepAttack). + let bounds = match victim { + Victim::Host => self.player.as_ref().map(|p| p.bounds()), + Victim::Guest(conn) => host.hub.guest(conn).map(guest_bounds), + }; + if let (Some(sweep), Some(bounds)) = (&s.sweep_hit, bounds) { + self.sweep_players( + host, + &Sweep { + hit_box: bounds.inflate(Vec3::new(1.0, 0.25, 1.0)), + skip: Some(victim), + ..sweep.clone() + }, + ); + } } else { self.audio.play(sound, Some(s.from), 1.0, 1.0); } hurt } + + /// The sweep of the player in place, for the players it reaches: the + /// damage (`1 + ratio * baseDamage`, plus the enchantments against a + /// player, scaled by the attack strength) and who hits. The box is set + /// by the caller. + pub(super) fn player_sweep(&mut self, base: f32, scale: f32) -> Sweep { + let from = self + .player + .as_ref() + .map_or([0.0; 3], |p| [p.position.x, p.position.y, p.position.z]); + let damage = (base + self.enchant_damage_bonus("minecraft:player")) * scale; + Sweep { + attacker: self.lan_acting, + from, + damage, + knockback: self.extra_knockback_direction(), + team: self.player_team().cloned(), + hit_box: Aabb::new(Vec3::ZERO, Vec3::ZERO), + skip: None, + } + } + + /// A sweep from a mob hit reaches the other players. Inside a guest's + /// turn the host holds the other players (`as_guest`), so it waits for + /// the turn to end. + pub(super) fn sweep_other_players(&mut self, sweep: Sweep) { + if self.lan_acting.is_some() { + self.creatures.sweeps.push(sweep); + } else if let Some(mut host) = self.lan.take() { + self.sweep_players(&mut host, &sweep); + self.lan = Some(host); + } + } + + /// The sweeps guests' turns put off. + pub(super) fn flush_sweeps(&mut self) { + for sweep in std::mem::take(&mut self.creatures.sweeps) { + self.sweep_other_players(sweep); + } + } + + /// `Player.doSweepAttack` on the players: each other player in the box, + /// closer than 3 blocks (`distanceToSqr < 9`) and not an ally + /// (`isAlliedTo`), is hurt (`ServerPlayer.canHarmPlayer`: the `pvp` rule) + /// and pushed 0.4 along the attacker's yaw. + pub(super) fn sweep_players(&mut self, host: &mut LanHost, sweep: &Sweep) { + let from = Vec3::new(sweep.from[0], sweep.from[1], sweep.from[2]); + let mut victims = Vec::new(); + if sweep.attacker.is_some() + && self.options.dedicated.is_none() + && !self.spectator() + && let Some(p) = self.player.as_ref() + && !p.health.dead_or_dying() + && p.bounds().intersects(sweep.hit_box) + && p.position.distance_squared(from) < 9.0 + { + victims.push((Victim::Host, self.player_team().cloned())); + } + for g in &host.hub.guests { + if host.hub.here(g) + && g.loaded + && !g.dead + && !g.spectator() + && sweep.attacker != Some(g.conn) + && guest_bounds(g).intersects(sweep.hit_box) + && Vec3::new(g.position[0], g.position[1], g.position[2]).distance_squared(from) + < 9.0 + { + let team = self.score.board.players_team(&g.name).cloned(); + victims.push((Victim::Guest(g.conn), team)); + } + } + let (xd, zd) = sweep.knockback; + let land = |g: &mut Self| { + let hurt = g.mob_hits_player(sweep.damage, sweep.from, "minecraft:player_attack"); + if hurt { + g.knock_back_player(0.4, xd, zd); + } + }; + for (victim, team) in victims { + if sweep.skip == Some(victim) + || horizon_client::scoreboard::teams_allied(team.as_ref(), sweep.team.as_ref()) + || !self.ui.rules.bool("pvp") + { + continue; + } + match victim { + Victim::Host => land(self), + Victim::Guest(conn) => { + self.guest_struck_by(host, conn, land); + } + } + } + } }