Implement the spear's left-click jab and the Lunge enchantment
This commit is contained in:
@@ -197,6 +197,106 @@ impl Game<'_> {
|
||||
}
|
||||
self.post_attack_bursts(&held, &kind);
|
||||
}
|
||||
/// `EnchantmentHelper.doPostPiercingAttackEffects`: the
|
||||
/// `minecraft:post_piercing_attack` effects of the weapon in hand after a
|
||||
/// jab (Lunge's dash).
|
||||
pub(super) fn post_piercing_attack(&mut self) {
|
||||
let Some(held) = self.held().cloned() else {
|
||||
return;
|
||||
};
|
||||
let mut effects = Vec::new();
|
||||
for (id, level) in held.enchantments() {
|
||||
let Some(def) = self.res.stations().enchantments.get(id) else {
|
||||
continue;
|
||||
};
|
||||
for entry in def.effects["minecraft:post_piercing_attack"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
effects.push((*level, entry["effect"].clone()));
|
||||
}
|
||||
}
|
||||
// Lunge's requirements: not riding, gliding or in water, and a
|
||||
// player outside creative with at least 7 food.
|
||||
let Some(p) = self.player.as_ref() else {
|
||||
return;
|
||||
};
|
||||
if effects.is_empty()
|
||||
|| self.riding.is_some()
|
||||
|| p.fall_flying
|
||||
|| p.fluids.was_touching_water
|
||||
|| !(self.creative || p.health.food.food >= 7)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (level, effect) in effects {
|
||||
self.apply_piercing_effect(&effect, level);
|
||||
}
|
||||
}
|
||||
/// One `post_piercing_attack` effect on the player: `AllOf`,
|
||||
/// `ChangeItemDamage`, `ApplyExhaustion`, `ApplyEntityImpulse` and
|
||||
/// `PlaySoundEffect`.
|
||||
fn apply_piercing_effect(&mut self, effect: &serde_json::Value, level: u32) {
|
||||
let amount = || level_value(&effect["amount"], level);
|
||||
match effect["type"].as_str() {
|
||||
Some("minecraft:all_of") => {
|
||||
for e in effect["effects"].as_array().into_iter().flatten() {
|
||||
self.apply_piercing_effect(e, level);
|
||||
}
|
||||
}
|
||||
Some("minecraft:change_item_damage") => self.damage_held(amount() as u32),
|
||||
Some("minecraft:apply_exhaustion") => {
|
||||
if let Some(p) = self.player.as_mut() {
|
||||
p.cause_food_exhaustion(amount());
|
||||
}
|
||||
}
|
||||
// The direction turned by the look (`Entity.getLookQuaternion`:
|
||||
// pitch about x, then yaw about y), scaled by coordinate_scale
|
||||
// and the magnitude, added to the velocity (the guest's client
|
||||
// is told of it by `guest_hit_by`).
|
||||
Some("minecraft:apply_impulse") => {
|
||||
let vec3 = |key: &str| [0, 1, 2].map(|i| effect[key][i].as_f64().unwrap_or(0.0));
|
||||
let (d, scale) = (vec3("direction"), vec3("coordinate_scale"));
|
||||
let magnitude = f64::from(level_value(&effect["magnitude"], level));
|
||||
let (yaw, pitch) = (
|
||||
f64::from(self.yaw).to_radians(),
|
||||
f64::from(self.pitch).to_radians(),
|
||||
);
|
||||
let (y1, z1) = (
|
||||
d[1] * pitch.cos() - d[2] * pitch.sin(),
|
||||
d[1] * pitch.sin() + d[2] * pitch.cos(),
|
||||
);
|
||||
let impulse = horizon_core::geometry::Vec3::new(
|
||||
(d[0] * yaw.cos() - z1 * yaw.sin()) * scale[0] * magnitude,
|
||||
y1 * scale[1] * magnitude,
|
||||
(d[0] * yaw.sin() + z1 * yaw.cos()) * scale[2] * magnitude,
|
||||
);
|
||||
if let Some(p) = self.player.as_mut() {
|
||||
p.velocity = p.velocity.add_vector(impulse);
|
||||
}
|
||||
}
|
||||
// The sound for the level (a list picks by level), heard by all.
|
||||
Some("minecraft:play_sound") => {
|
||||
let sounds: Vec<&str> = match &effect["sound"] {
|
||||
serde_json::Value::Array(list) => {
|
||||
list.iter().filter_map(|s| s.as_str()).collect()
|
||||
}
|
||||
one => one.as_str().into_iter().collect(),
|
||||
};
|
||||
let index = (level as usize).saturating_sub(1);
|
||||
if let Some(sound) = sounds.get(index.min(sounds.len().saturating_sub(1))) {
|
||||
let float = |key: &str| effect[key].as_f64().unwrap_or(1.0) as f32;
|
||||
let at = self
|
||||
.player
|
||||
.as_ref()
|
||||
.map(|p| [p.position.x, p.position.y, p.position.z]);
|
||||
self.audio.play(sound, at, float("volume"), float("pitch"));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
/// `minecraft:post_attack` explode effects on the attacker (wind burst):
|
||||
/// a burst at the player's feet that only knocks back and triggers
|
||||
/// blocks (`ExplodeEffect` with `block_interaction: trigger`).
|
||||
|
||||
@@ -331,6 +331,12 @@ struct GameHands<'g, 'a> {
|
||||
error: Option<Box<dyn std::error::Error>>,
|
||||
}
|
||||
impl GameHands<'_, '_> {
|
||||
/// The main-hand item's `kinetic_weapon` and `piercing_weapon`, a spear's.
|
||||
fn attack_kinetic(&self) -> Option<horizon_client::spear::Kinetic> {
|
||||
self.game
|
||||
.held()
|
||||
.and_then(|s| horizon_client::spear::Kinetic::of(&s.item))
|
||||
}
|
||||
fn keep(&mut self, r: Result<()>) {
|
||||
if let Err(e) = r
|
||||
&& self.error.is_none()
|
||||
@@ -398,18 +404,27 @@ impl HandWorld for GameHands<'_, '_> {
|
||||
self.game.creative
|
||||
}
|
||||
fn cannot_attack(&mut self) -> bool {
|
||||
false
|
||||
let ticker = self.game.hand_strength.ticker;
|
||||
self.game.cannot_attack_with_item(ticker, 0)
|
||||
}
|
||||
fn piercing_weapon(&mut self) -> bool {
|
||||
false
|
||||
self.attack_kinetic().is_some()
|
||||
}
|
||||
fn attack_animation(&mut self) -> SwingAnimation {
|
||||
SwingAnimation::DEFAULT
|
||||
self.attack_kinetic()
|
||||
.map_or(SwingAnimation::DEFAULT, |k| k.attack_animation())
|
||||
}
|
||||
fn interact_animation(&mut self, _: Hand) -> SwingAnimation {
|
||||
SwingAnimation::DEFAULT
|
||||
}
|
||||
fn piercing_attack(&mut self, _: SwingAnimation) {}
|
||||
fn piercing_attack(&mut self, _: SwingAnimation) {
|
||||
// The server's part (`PiercingWeapon.attack`), then the client's:
|
||||
// the sound `MultiPlayerGameMode.piercingAttack` plays.
|
||||
if let Some(sound) = self.game.jab() {
|
||||
self.game.play_at_feet(sound);
|
||||
}
|
||||
self.game.hand_strength.reset_attack();
|
||||
}
|
||||
fn attack_entity(&mut self) -> bool {
|
||||
self.game.attack()
|
||||
}
|
||||
|
||||
@@ -1027,6 +1027,8 @@ impl Game<'_> {
|
||||
g.player_on_equip("mainhand", offhand.as_ref(), main.as_ref());
|
||||
});
|
||||
}
|
||||
// STAB: `PiercingWeapon.attack` of a left click with a spear.
|
||||
8 if !spectator => self.guest_jab(host, conn),
|
||||
_ => {}
|
||||
},
|
||||
S::SetCarriedItem(p) => {
|
||||
@@ -1514,6 +1516,37 @@ impl Game<'_> {
|
||||
Some((centre, eye))
|
||||
}
|
||||
|
||||
/// `ServerGamePacketListenerImpl.handlePlayerAction` for STAB: the spear in
|
||||
/// the guest's main hand jabs once its charge allows it (with five ticks
|
||||
/// of tolerance). The guest's client is sent the dash Lunge gave them.
|
||||
fn guest_jab(&mut self, host: &mut LanHost, conn: u32) {
|
||||
let Some(g) = host.hub.guest(conn) else {
|
||||
return;
|
||||
};
|
||||
let at = g.position;
|
||||
// ServerPlayer.getKnownMovement: the move their client just sent.
|
||||
let movement = g
|
||||
.moves
|
||||
.last()
|
||||
.map_or(Vec3::ZERO, |m| Vec3::new(m.0[0], m.0[1], m.0[2]));
|
||||
let sound = self
|
||||
.guest_hit_by(host, conn, |g| {
|
||||
g.known_movement = movement;
|
||||
if g.cannot_attack_with_item(g.attack_ticks as i32, 5) {
|
||||
None
|
||||
} else {
|
||||
g.jab()
|
||||
}
|
||||
})
|
||||
.flatten();
|
||||
// PiercingWeapon.makeSound: `playSound(causer, ...)`, so the guest's
|
||||
// own client plays it.
|
||||
if let Some(name) = sound {
|
||||
let source = horizon_client::settings::sound_source(name) as i32;
|
||||
host.hub.sound(name, at, 1.0, 1.0, source, Some(conn));
|
||||
}
|
||||
}
|
||||
|
||||
fn guest_attack(&mut self, host: &mut LanHost, conn: u32, id: i32) {
|
||||
let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else {
|
||||
return;
|
||||
|
||||
@@ -4564,6 +4564,28 @@ impl Game<'_> {
|
||||
if self.spectator() {
|
||||
return true;
|
||||
}
|
||||
// A spear jabs (`MultiPlayerGameMode.piercingAttack`): the server
|
||||
// stabs, this client swings, plays the sound and starts its charge
|
||||
// again.
|
||||
if let Some(kinetic) = self
|
||||
.held()
|
||||
.and_then(|s| horizon_client::spear::Kinetic::of(&s.item))
|
||||
{
|
||||
if self.cannot_attack_with_item(self.hand_strength.ticker, 0) {
|
||||
return false;
|
||||
}
|
||||
// ServerboundPlayerActionPacket.Action.STAB.
|
||||
self.send(S::PlayerAction(play::PlayerAction {
|
||||
action: 8,
|
||||
pos: [0; 3],
|
||||
direction: 0,
|
||||
sequence: 0,
|
||||
}));
|
||||
self.hand_strength.reset_attack();
|
||||
self.swing_hand(Hand::Main, kinetic.attack_animation());
|
||||
self.play_at_feet(kinetic.attack_sound);
|
||||
return true;
|
||||
}
|
||||
let mut instant = false;
|
||||
// A ghast's fireball or a wind charge nearer than any mob: the
|
||||
// server turns it (game/deflect.rs).
|
||||
@@ -4614,7 +4636,13 @@ impl Game<'_> {
|
||||
if !down {
|
||||
self.clicks.miss_time = 0;
|
||||
}
|
||||
if self.clicks.miss_time > 0 || self.using.is_some() {
|
||||
// A piercing weapon neither breaks blocks nor swings on its own.
|
||||
if self.clicks.miss_time > 0
|
||||
|| self.using.is_some()
|
||||
|| self
|
||||
.held()
|
||||
.is_some_and(|s| horizon_client::spear::Kinetic::of(&s.item).is_some())
|
||||
{
|
||||
return;
|
||||
}
|
||||
let entity = self.entity_targeted();
|
||||
|
||||
@@ -5,10 +5,13 @@
|
||||
//! attacker's own speed, and each condition runs out after its time
|
||||
//! (`Item.use`, `ItemStack.onUseTick`, `KineticWeapon.damageEntities`,
|
||||
//! `Player.stabAttack`). The hit's feedback (`onKineticHit`) reaches the
|
||||
//! players around as entity event 2.
|
||||
//! players around as entity event 2. A left click jabs instead
|
||||
//! (`PiercingWeapon.attack`), which Lunge turns into a dash.
|
||||
use super::{Game, view_vector};
|
||||
use horizon_client::{
|
||||
attack_strength::delay,
|
||||
collisions::SceneCollisions,
|
||||
first_person::Hand,
|
||||
items::ItemStack,
|
||||
mobs::Damage,
|
||||
spear::{HIT_FEEDBACK_TICKS, Kinetic, REACH},
|
||||
@@ -115,6 +118,28 @@ impl Game<'_> {
|
||||
hits
|
||||
}
|
||||
|
||||
/// `ProjectileUtil.getHitEntitiesAlong` with the spear's `AttackRange`: the
|
||||
/// mobs along the view from the minimum reach to the maximum plus the
|
||||
/// attacker's own movement, none when a block is closer than the minimum.
|
||||
fn stab_targets(&self, eye: Vec3, look: Vec3, movement: Vec3) -> Vec<usize> {
|
||||
// AttackRange.effectiveMinRange and effectiveMaxRange for a player.
|
||||
let (min, max) = if self.creative {
|
||||
(REACH.min_creative, REACH.max_creative)
|
||||
} else {
|
||||
(REACH.min, REACH.max)
|
||||
};
|
||||
let from = eye.add_vector(look.scale(f64::from(min)));
|
||||
let mut to = eye.add_vector(look.scale(f64::from(max) + movement.dot(look).max(0.0)));
|
||||
// A block closer than the minimum reach takes the whole stab.
|
||||
if let Some(block) = self.clip_collider(eye, to) {
|
||||
to = block;
|
||||
if eye.distance_squared(to) < eye.distance_squared(from) {
|
||||
return Vec::new();
|
||||
}
|
||||
}
|
||||
self.mobs_along(from, to, f64::from(REACH.hitbox_margin))
|
||||
}
|
||||
|
||||
/// `KineticWeapon.damageEntities`: one tick of the spear in use, `ticks`
|
||||
/// ticks into the use.
|
||||
pub(super) fn spear_tick(&mut self, stack: &ItemStack, ticks: u32) {
|
||||
@@ -136,25 +161,10 @@ impl Game<'_> {
|
||||
let look = Vec3::new(look[0], look[1], look[2]);
|
||||
let movement = self.known_player_movement();
|
||||
let attacker_speed = look.dot(movement.scale(20.0));
|
||||
// AttackRange.effectiveMinRange and effectiveMaxRange for a player.
|
||||
let (min, max) = if self.creative {
|
||||
(REACH.min_creative, REACH.max_creative)
|
||||
} else {
|
||||
(REACH.min, REACH.max)
|
||||
};
|
||||
let from = eye.add_vector(look.scale(f64::from(min)));
|
||||
let mut to = eye.add_vector(look.scale(f64::from(max) + movement.dot(look).max(0.0)));
|
||||
// A block closer than the minimum reach takes the whole stab.
|
||||
if let Some(block) = self.clip_collider(eye, to) {
|
||||
to = block;
|
||||
if eye.distance_squared(to) < eye.distance_squared(from) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
let now = self.world.game_time;
|
||||
let actor = self.lan_acting;
|
||||
let mut affected = false;
|
||||
for i in self.mobs_along(from, to, f64::from(REACH.hitbox_margin)) {
|
||||
for i in self.stab_targets(eye, look, movement) {
|
||||
let key = (actor, self.mobs[i].id);
|
||||
let recent = self
|
||||
.stabbed
|
||||
@@ -204,9 +214,18 @@ impl Game<'_> {
|
||||
return false;
|
||||
};
|
||||
let from = [p.position.x, p.position.y, p.position.z];
|
||||
// The enchantments' share, with no attack strength scaling for a
|
||||
// weapon in use.
|
||||
// The enchantments' share, scaled by the attack strength unless the
|
||||
// weapon is in use.
|
||||
let magic_boost = self.enchant_damage_bonus(&kind);
|
||||
let (base_damage, magic_boost) = if self.using.is_none() {
|
||||
let scale = self.attack_strength_scale();
|
||||
(
|
||||
base_damage * (0.2 + scale * scale * 0.8),
|
||||
magic_boost * scale,
|
||||
)
|
||||
} else {
|
||||
(base_damage, magic_boost)
|
||||
};
|
||||
let total = if deals_damage {
|
||||
base_damage + magic_boost
|
||||
} else {
|
||||
@@ -275,6 +294,57 @@ impl Game<'_> {
|
||||
true
|
||||
}
|
||||
|
||||
/// `Player.cannotAttackWithItem` for the held item: a spear needs its
|
||||
/// `minimum_attack_charge` of 1, counting `tolerance` ticks more than the
|
||||
/// `ticker` ticks charged.
|
||||
pub(super) fn cannot_attack_with_item(&self, ticker: i32, tolerance: i32) -> bool {
|
||||
self.held().is_some_and(|s| Kinetic::of(&s.item).is_some())
|
||||
&& (ticker + tolerance) as f32 / delay(self.main_hand_attack_speed()) < 1.0
|
||||
}
|
||||
|
||||
/// `PiercingWeapon.attack` of the spear in the main hand, for a left
|
||||
/// click: every mob along the view is stabbed, then `postPiercingAttack`
|
||||
/// (Lunge), the hit sound and the swing. Gives the sound of the jab
|
||||
/// (`makeSound`), which everyone but the user hears from the server.
|
||||
pub(super) fn jab(&mut self) -> Option<&'static str> {
|
||||
let kinetic = self.held().and_then(|s| Kinetic::of(&s.item))?;
|
||||
let eye = self.eye_position()?;
|
||||
let eye = Vec3::new(eye[0], eye[1], eye[2]);
|
||||
let look = view_vector(self.yaw, self.pitch);
|
||||
let look = Vec3::new(look[0], look[1], look[2]);
|
||||
// The attack damage attribute.
|
||||
let damage = 1.0
|
||||
+ self.attack_item_info().map_or(0.0, |i| i.attack_damage)
|
||||
+ self.effect_attack_bonus();
|
||||
let movement = self.known_player_movement();
|
||||
let mut hit = false;
|
||||
for i in self.stab_targets(eye, look, movement) {
|
||||
hit |= self.stab_mob(i, damage, true, true, false);
|
||||
}
|
||||
// Player.onAttack, then postPiercingAttack.
|
||||
self.attack_ticks = 0;
|
||||
self.post_piercing_attack();
|
||||
if hit {
|
||||
self.play_at_feet(kinetic.hit_sound);
|
||||
}
|
||||
// swingAndResetAttackStrength (ServerPlayer): a swing that starts
|
||||
// resets the attack strength.
|
||||
let swings = self.swings_started;
|
||||
self.swing_hand(Hand::Main, kinetic.attack_animation());
|
||||
if self.swings_started != swings {
|
||||
self.reset_attack_strength();
|
||||
}
|
||||
Some(kinetic.attack_sound)
|
||||
}
|
||||
|
||||
/// A sound at the player's feet.
|
||||
pub(super) fn play_at_feet(&mut self, sound: &str) {
|
||||
if let Some(p) = &self.player {
|
||||
let at = [p.position.x, p.position.y, p.position.z];
|
||||
self.audio.play(sound, Some(at), 1.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// `LivingEntity.onKineticHit` for `entity`: the hit feels (the spear
|
||||
/// moves back, `kinetic_hit_ticks`) and sounds, at most once every ten
|
||||
/// ticks.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//! conditions of a stab, `Item.Properties.spear`'s numbers) and
|
||||
//! `SpearAnimations` (how the spear is held in first and third person while
|
||||
//! it charges).
|
||||
use crate::first_person::SwingAnimation;
|
||||
use glam::{Mat4, Vec3};
|
||||
use horizon_core::{geometry::trig::Mth, jmath::pow};
|
||||
use std::f32::consts::PI;
|
||||
@@ -39,6 +40,10 @@ pub struct Kinetic {
|
||||
pub damage_multiplier: f32,
|
||||
pub sound: &'static str,
|
||||
pub hit_sound: &'static str,
|
||||
/// The `attack_animation` duration, and the `piercing_weapon` sound of a
|
||||
/// jab.
|
||||
pub attack_ticks: i32,
|
||||
pub attack_sound: &'static str,
|
||||
}
|
||||
|
||||
/// `AttackRange` of a spear: the reach of its stabs.
|
||||
@@ -62,16 +67,17 @@ pub const REACH: Reach = Reach {
|
||||
impl Kinetic {
|
||||
/// The `kinetic_weapon` of a spear item (`Item.Properties.spear`).
|
||||
pub fn of(item: &str) -> Option<Self> {
|
||||
// (damage multiplier, delay, dismount time and speed, knockback time
|
||||
// and speed, damage time and speed): seconds and blocks per second.
|
||||
let (multiplier, delay, dismount, knockback, damage) = match item {
|
||||
"minecraft:wooden_spear" => (0.7, 0.75, (5.0, 14.0), (10.0, 5.1), (15.0, 4.6)),
|
||||
"minecraft:stone_spear" => (0.82, 0.7, (4.5, 13.0), (9.0, 5.1), (13.75, 4.6)),
|
||||
"minecraft:copper_spear" => (0.82, 0.65, (4.0, 12.0), (8.25, 5.1), (12.5, 4.6)),
|
||||
"minecraft:iron_spear" => (0.95, 0.6, (2.5, 11.0), (6.75, 5.1), (11.25, 4.6)),
|
||||
"minecraft:golden_spear" => (0.7, 0.7, (3.5, 13.0), (8.5, 5.1), (13.75, 4.6)),
|
||||
"minecraft:diamond_spear" => (1.075, 0.5, (3.0, 10.0), (6.5, 5.1), (10.0, 4.6)),
|
||||
"minecraft:netherite_spear" => (1.2, 0.4, (2.5, 9.0), (5.5, 5.1), (8.75, 4.6)),
|
||||
// (attack duration, damage multiplier, delay, dismount time and
|
||||
// speed, knockback time and speed, damage time and speed): seconds
|
||||
// and blocks per second.
|
||||
let (attack, multiplier, delay, dismount, knockback, damage) = match item {
|
||||
"minecraft:wooden_spear" => (0.65, 0.7, 0.75, (5.0, 14.0), (10.0, 5.1), (15.0, 4.6)),
|
||||
"minecraft:stone_spear" => (0.75, 0.82, 0.7, (4.5, 13.0), (9.0, 5.1), (13.75, 4.6)),
|
||||
"minecraft:copper_spear" => (0.85, 0.82, 0.65, (4.0, 12.0), (8.25, 5.1), (12.5, 4.6)),
|
||||
"minecraft:iron_spear" => (0.95, 0.95, 0.6, (2.5, 11.0), (6.75, 5.1), (11.25, 4.6)),
|
||||
"minecraft:golden_spear" => (0.95, 0.7, 0.7, (3.5, 13.0), (8.5, 5.1), (13.75, 4.6)),
|
||||
"minecraft:diamond_spear" => (1.05, 1.075, 0.5, (3.0, 10.0), (6.5, 5.1), (10.0, 4.6)),
|
||||
"minecraft:netherite_spear" => (1.15, 1.2, 0.4, (2.5, 9.0), (5.5, 5.1), (8.75, 4.6)),
|
||||
_ => return None,
|
||||
};
|
||||
let wood = item == "minecraft:wooden_spear";
|
||||
@@ -107,8 +113,23 @@ impl Kinetic {
|
||||
} else {
|
||||
"minecraft:item.spear.hit"
|
||||
},
|
||||
attack_ticks: ticks(attack),
|
||||
attack_sound: if wood {
|
||||
"minecraft:item.spear_wood.attack"
|
||||
} else {
|
||||
"minecraft:item.spear.attack"
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// The spear's `attack_animation`: a STAB (`SwingAnimationType` 2) as long
|
||||
/// as its attack duration.
|
||||
pub fn attack_animation(&self) -> SwingAnimation {
|
||||
SwingAnimation {
|
||||
kind: 2,
|
||||
duration: self.attack_ticks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `KineticWeapon.HIT_FEEDBACK_TICKS`.
|
||||
|
||||
Reference in New Issue
Block a user