Merge remote-tracking branch 'origin/main' into fix/sleeping-player-state

This commit is contained in:
Cosmic
2026-10-17 04:41:31 -04:00
3 changed files with 96 additions and 3 deletions
+43 -2
View File
@@ -5869,6 +5869,38 @@ impl Game<'_> {
let delay = 20.0 / (4.0 + speed).max(0.1);
((self.attack_ticks as f32 + 0.5) / delay).clamp(0.0, 1.0)
}
/// `Player.isSweepAttack`: a charged, grounded sword hit below the movement limit.
pub(super) fn sweep_attack_position(&self, scale: f32, crit: bool) -> Option<[f64; 3]> {
let p = self.player.as_ref()?;
let sword = self.held().is_some_and(|s| {
self.res
.items
.item_tags
.get("minecraft:swords")
.is_some_and(|tag| tag.contains(&*s.item))
});
if !p.is_sweep_attack(scale, crit, sword) {
return None;
}
let yaw = f64::from(self.yaw.to_radians());
Some([
p.position.x - yaw.sin(),
p.position.y + f64::from(p.dimensions.height) * 0.5,
p.position.z + yaw.cos(),
])
}
/// The visual and sound part of `Player.doSweepAttack` after a successful hit.
pub(super) fn sweep_attack(&mut self, at: [f64; 3], from: Option<[f64; 3]>) {
self.particles.add(
&horizon_client::particles::ParticleOptions::simple(
horizon_client::particles::ParticleType::SweepAttack,
),
at,
[0.0; 3],
);
self.audio
.play("minecraft:entity.player.attack.sweep", from, 1.0, 1.0);
}
/// `Player.attack`'s sounds when a vehicle takes the hit (it always does,
/// and can't be hit critically): knockback when sprinting at full
/// strength, then strong or weak. Before `swing_arm` resets the charge.
@@ -5885,7 +5917,11 @@ impl Game<'_> {
} else {
"minecraft:entity.player.attack.weak"
};
self.audio.play(sound, from, 1.0, 1.0);
if let Some(at) = self.sweep_attack_position(self.attack_strength_scale(), false) {
self.sweep_attack(at, from);
} else {
self.audio.play(sound, from, 1.0, 1.0);
}
}
/// Player.attack with the attack-strength charge, crits and knockback.
fn attack(&mut self) -> bool {
@@ -5937,6 +5973,7 @@ impl Game<'_> {
let target_kind = self.mobs[index].kind.id.clone();
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);
self.attack_ticks = 0;
if let Some(p) = self.player.as_mut()
&& !self.creative
@@ -5996,7 +6033,11 @@ impl Game<'_> {
"minecraft:entity.player.attack.weak"
};
// Player.playServerSideSound: at the player.
self.audio.play(sound, from, 1.0, 1.0);
if let Some(at) = sweep {
self.sweep_attack(at, from);
} else {
self.audio.play(sound, from, 1.0, 1.0);
}
// The hurt or death sound the mob raised (getHurtSound / getDeathSound,
// its voice pitch from its own random).
let queued = std::mem::take(&mut self.mobs[index].outbox);
+8 -1
View File
@@ -19,6 +19,7 @@ pub(super) struct Strike {
scale: f32,
crit: bool,
from: [f64; 3],
sweep: Option<[f64; 3]>,
}
impl Game<'_> {
@@ -73,6 +74,7 @@ impl Game<'_> {
let p = self.player.as_ref()?;
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);
self.attack_ticks = 0;
if let Some(p) = self.player.as_mut()
&& !self.creative
@@ -85,6 +87,7 @@ impl Game<'_> {
scale,
crit,
from,
sweep,
})
}
@@ -105,6 +108,10 @@ impl Game<'_> {
(true, false, true) => "minecraft:entity.player.attack.strong",
(true, false, false) => "minecraft:entity.player.attack.weak",
};
self.audio.play(sound, Some(s.from), 1.0, 1.0);
if let Some(at) = s.sweep.filter(|_| hurt) {
self.sweep_attack(at, Some(s.from));
} else {
self.audio.play(sound, Some(s.from), 1.0, 1.0);
}
}
}
+45
View File
@@ -281,6 +281,17 @@ fn attribute_error(error: impl std::fmt::Debug) -> io::Error {
io::Error::other(format!("movement attribute: {error:?}"))
}
impl PlayerMotion {
/// `Player.isSweepAttack`, using the resident player's known horizontal velocity.
pub fn is_sweep_attack(&self, strength: f32, critical: bool, sword: bool) -> bool {
let movement = self.velocity;
let limit = f64::from(self.speed) * 2.5;
strength > 0.9
&& !critical
&& !self.sprinting
&& self.ground.on_ground
&& movement.x * movement.x + movement.z * movement.z < limit * limit
&& sword
}
pub fn load_environment_once(
&mut self,
pending: &mut bool,
@@ -1986,6 +1997,40 @@ impl LivingMotionWorld for MotionWorld<'_, '_, '_> {
mod tests {
use super::*;
#[test]
fn sword_sweep_requires_charge_ground_and_slow_horizontal_movement() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3")
});
if !root.join("movement-properties.nbt").exists() {
eprintln!("skipped sword sweep regression: prepared resources unavailable");
return;
}
let assets = Assets::load(&root).unwrap();
let resources = MotionResources::load(&root, &assets).unwrap();
let mut p = PlayerMotion::new(&resources, Vec3::ZERO).unwrap();
p.ground.on_ground = true;
p.speed = 0.1;
assert!(p.is_sweep_attack(1.0, false, true));
assert!(!p.is_sweep_attack(0.9, false, true));
assert!(!p.is_sweep_attack(1.0, true, true));
assert!(!p.is_sweep_attack(1.0, false, false));
p.sprinting = true;
assert!(!p.is_sweep_attack(1.0, false, true));
p.sprinting = false;
p.ground.on_ground = false;
assert!(!p.is_sweep_attack(1.0, false, true));
p.ground.on_ground = true;
p.velocity = Vec3::new(0.2, 10.0, 0.0);
assert!(p.is_sweep_attack(1.0, false, true));
p.velocity.z = 0.2;
assert!(!p.is_sweep_attack(1.0, false, true));
p.velocity = Vec3::new(f64::from(p.speed) * 2.5, 0.0, 0.0);
assert!(!p.is_sweep_attack(1.0, false, true));
}
#[test]
fn player_stands_walks_sneaks_and_jumps_on_ghast() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")