diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs index b9e9d7383..13149da3a 100644 --- a/crates/horizon-client/examples/probe/mob_ai_probe.rs +++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs @@ -567,7 +567,9 @@ fn print_heart( /// The damage of a mob's direct hit on the player (others count as none). fn player_damage(e: &MobEvent) -> f32 { match e { - MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount, + MobEvent::AttackPlayer(amount, _) + | MobEvent::AttackPlayerWithEffect { amount, .. } + | MobEvent::DamagePlayer { amount, .. } => *amount, _ => 0.0, } } @@ -578,7 +580,9 @@ fn player_hurt(player: Option<&PlayerView>, e: &MobEvent) -> bool { return false; }; match e { - MobEvent::AttackPlayer(amount, _) | MobEvent::DamagePlayer { amount, .. } => *amount > 0.0, + MobEvent::AttackPlayer(amount, _) + | MobEvent::AttackPlayerWithEffect { amount, .. } + | MobEvent::DamagePlayer { amount, .. } => *amount > 0.0, // 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 119f67fa7..db2ca7e16 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -1044,6 +1044,18 @@ impl Game<'_> { MobEvent::AttackPlayer(amount, from) => { self.mob_hits_player(amount, from, "minecraft:mob_attack"); } + // doHurtTarget: the effect only when hurtServer took damage. + MobEvent::AttackPlayerWithEffect { + amount, + from, + effect, + ticks, + amplifier, + } => { + if self.mob_hits_player(amount, from, "minecraft:mob_attack") { + self.give_player_effect(effect, ticks, amplifier); + } + } MobEvent::DamagePlayer { amount, from, kind } => { self.mob_hits_player(amount, from, kind); } diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 4b4efaaa8..86b9fd655 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -81,6 +81,7 @@ pub(super) fn acts_on_player(e: &MobEvent) -> bool { matches!( e, MobEvent::AttackPlayer(..) + | MobEvent::AttackPlayerWithEffect { .. } | MobEvent::DamagePlayer { .. } | MobEvent::Push { target: Who::Player, diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 389c6c7e0..6cfa8fa75 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -1011,6 +1011,16 @@ pub enum MobEvent { state: horizon_world::registry::BlockStateId, }, AttackPlayer(f32, [f64; 3]), + /// A melee hit whose effect comes only if the hit lands (husk hunger, + /// cave spider poison, wither skeleton wither, bee and pufferfish + /// poison): a shield-blocked hit gives nothing. + AttackPlayerWithEffect { + amount: f32, + from: [f64; 3], + effect: &'static str, + ticks: i32, + amplifier: i32, + }, /// Damage of a given type to the player (guardian lasers, thorns). DamagePlayer { amount: f32, diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 2af044209..e1627e29f 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -1602,6 +1602,7 @@ pub fn do_hurt_target(m: &mut Mob, who: Who, cx: &mut Cx) { let mut amount = m.attrs.attack_damage; let from = [m.position.x, m.position.y, m.position.z]; let source = Damage::mob(m.id, from); + let first_event = cx.events.len(); match m.species() { // CaveSpider.doHurtTarget: poison for 7 s on normal, 15 s on hard. Species::CaveSpider if who == Who::Player && cx.w.difficulty.base >= 2 => { @@ -1672,7 +1673,28 @@ pub fn do_hurt_target(m: &mut Mob, who: Who, cx: &mut Cx) { }); } match who { - Who::Player => cx.events.push(MobEvent::AttackPlayer(amount, from)), + // The effect picked above goes with the hit: it applies only when + // the player takes damage (a shield-blocked hit gives none). + Who::Player => { + let effect = cx.events[first_event..] + .iter() + .position(|e| matches!(e, MobEvent::EffectPlayer { .. })) + .map(|k| cx.events.remove(first_event + k)); + cx.events.push(match effect { + Some(MobEvent::EffectPlayer { + effect, + ticks, + amplifier, + }) => MobEvent::AttackPlayerWithEffect { + amount, + from, + effect, + ticks, + amplifier, + }, + _ => MobEvent::AttackPlayer(amount, from), + }); + } Who::Mob(id) => cx.events.push(MobEvent::AttackMob { target: id, amount, diff --git a/crates/horizon-client/src/mobs/aquatic.rs b/crates/horizon-client/src/mobs/aquatic.rs index 59a73c605..fbd09cd9f 100644 --- a/crates/horizon-client/src/mobs/aquatic.rs +++ b/crates/horizon-client/src/mobs/aquatic.rs @@ -244,11 +244,10 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { && m.ai.no_action_time % 10 == 0 { let amount = 1.0 + m.extra.size as f32; - events.push(MobEvent::AttackPlayer( + // Only when the sting hurt the player. + events.push(MobEvent::AttackPlayerWithEffect { amount, - [m.position.x, m.position.y, m.position.z], - )); - events.push(MobEvent::EffectPlayer { + from: [m.position.x, m.position.y, m.position.z], effect: "minecraft:poison", ticks: 60 * m.extra.size, amplifier: 0, diff --git a/crates/horizon-client/src/mobs/bee.rs b/crates/horizon-client/src/mobs/bee.rs index c759822f6..e8e3ae0ae 100644 --- a/crates/horizon-client/src/mobs/bee.rs +++ b/crates/horizon-client/src/mobs/bee.rs @@ -333,19 +333,23 @@ pub fn sting(m: &mut Mob, who: Who, cx: &mut Cx) { let amount = (m.attrs.attack_damage as i32) as f32; match who { Who::Player => { - cx.events.push(MobEvent::AttackPlayer(amount, from)); - // Poison only on normal (10 s) and hard (18 s). + // Bee.doHurtTarget: the poison only when the sting hurt, and + // only on normal (10 s) and hard (18 s). let seconds = match cx.w.difficulty.id { 2 => 10, 3 => 18, _ => 0, }; if seconds > 0 { - cx.events.push(MobEvent::EffectPlayer { + cx.events.push(MobEvent::AttackPlayerWithEffect { + amount, + from, effect: "minecraft:poison", ticks: seconds * 20, amplifier: 0, }); + } else { + cx.events.push(MobEvent::AttackPlayer(amount, from)); } } Who::Mob(id) => cx.events.push(MobEvent::AttackMob {