diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index c9fa56be9..45afd2e2d 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -246,6 +246,11 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { } map.insert("equipment".into(), Tag::Compound(equipment)); } + "minecraft:ravager" => { + map.insert("AttackTick".into(), Tag::Int(m.extra.counter)); + map.insert("StunTick".into(), Tag::Int(m.extra.counter2)); + map.insert("RoarTick".into(), Tag::Int(m.extra.anim_ticks)); + } "minecraft:vex" => { if let Some(h) = m.extra.home { map.insert("bound_pos".into(), Tag::IntArray(h.to_vec())); @@ -408,6 +413,11 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - .unwrap_or(0) as u8; } } + if m.species() == crate::mobs::Species::Ravager { + m.extra.counter = number(tag.get("AttackTick")).unwrap_or(0) as i32; + m.extra.counter2 = number(tag.get("StunTick")).unwrap_or(0) as i32; + m.extra.anim_ticks = number(tag.get("RoarTick")).unwrap_or(0) as i32; + } if m.species() == crate::mobs::Species::Vex { if let Some(Tag::IntArray(v)) = tag.get("bound_pos") && v.len() == 3 diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index fdf79841f..864aed213 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -695,6 +695,21 @@ impl Game<'_> { self.mob_hits_player(amount, from, kind) } MobEvent::ElderCurse { at } => self.elder_curse(at), + MobEvent::Push { target, velocity } => { + let v = Vec3::new(velocity[0], velocity[1], velocity[2]); + match target { + Who::Player => { + if let Some(p) = self.player.as_mut() { + p.velocity = p.velocity.add_vector(v); + } + } + Who::Mob(id) => { + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { + m.velocity = m.velocity.add_vector(v); + } + } + } + } MobEvent::Wololo { mob } => { if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) { // DyeColor.RED. diff --git a/crates/horizon-client/src/game/shield.rs b/crates/horizon-client/src/game/shield.rs index 243f7dbbd..eade37109 100644 --- a/crates/horizon-client/src/game/shield.rs +++ b/crates/horizon-client/src/game/shield.rs @@ -185,6 +185,32 @@ impl Game<'_> { .as_deref() .and_then(|item| self.res.items.get(item)) .map_or(0.0, |i| i.disable_blocking_seconds); + // Ravager.blockedByItem: stunned half the time (the defender pushed + // apart), else the defender flung away; no roar in progress. + if m.species() == horizon_client::mobs::Species::Ravager { + let mut events = Vec::new(); + let roaring = m.extra.anim_ticks != 0; + let stunned = self + .mobs + .iter_mut() + .find(|m| m.id == id) + .is_some_and(|m| horizon_client::mobs::ravager::blocked(m, &mut events)); + if !roaring && let Some(p) = self.player.as_mut() { + let (xd, zd) = (p.position.x - mx, p.position.z - mz); + let push = if stunned { + // Entity.push(entity): apart by 0.05 per block of overlap. + let d = (xd * xd + zd * zd).sqrt().max(0.01); + let k = (1.0 / d).min(1.0) * 0.05; + horizon_core::geometry::Vec3::new(xd / d * k, 0.0, zd / d * k) + } else { + let dd = (xd * xd + zd * zd).max(0.001); + horizon_core::geometry::Vec3::new(xd / dd * 4.0, 0.2, zd / dd * 4.0) + }; + p.velocity = p.velocity.add_vector(push); + } + let _ = self.handle_mob_events(events); + return; + } // LivingEntity.blockedByItem: defender.knockback(0.5, defender - attacker), // which moves the defender along -(xd, zd). if !fully && let Some(p) = self.player.as_mut() { diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 2395f3007..8bac62e6d 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -26,6 +26,7 @@ pub mod nether; pub mod path; pub mod phantom; pub mod piglin; +pub mod ravager; pub mod render; pub mod shulker; pub mod spawning; @@ -522,6 +523,11 @@ pub enum MobEvent { ElderCurse { at: [f64; 3], }, + /// `Entity.push`: add to a target's velocity. + Push { + target: Who, + velocity: [f64; 3], + }, /// `EvokerWololoSpellGoal`: a blue sheep turns red. Wololo { mob: u32, diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index fe3a7bb9a..3b2420781 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -83,6 +83,7 @@ pub enum Family { Guardian, Shulker, Vex, + Ravager, } pub fn family(s: Species) -> Family { use Species as S; @@ -113,6 +114,7 @@ pub fn family(s: Species) -> Family { S::IronGolem | S::SnowGolem => Family::Golem, S::Pillager | S::Vindicator | S::Evoker => Family::Illager, S::Vex => Family::Vex, + S::Ravager => Family::Ravager, S::Piglin | S::PiglinBrute | S::Hoglin | S::Zoglin => Family::Piglin, S::Horse | S::Donkey | S::Mule | S::SkeletonHorse | S::ZombieHorse => Family::Equine, S::Phantom => Family::Phantom, @@ -154,6 +156,7 @@ pub fn register(m: &mut Mob) { Family::Guardian => super::guardian::register(m), Family::Shulker => super::shulker::register(m), Family::Vex => super::vex::register(m), + Family::Ravager => super::ravager::register(m), } } /// Mob.tick additions that run before the entity tick (creeper fuse, puffing). @@ -397,7 +400,10 @@ fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { ); m.ai.xxa *= 0.98; m.ai.zza *= 0.98; - if m.alive() { + // LivingEntity.isImmobile: no goals or input (a biting, stunned or + // roaring ravager). + let immobile = m.species() == Species::Ravager && super::ravager::immobile(m); + if m.alive() && !immobile { server_ai_step(m, w, events); } else { m.ai.jumping = false; @@ -537,6 +543,7 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { Species::Phantom => super::phantom::flap_sound(m, events), Species::Guardian | Species::ElderGuardian => super::guardian::ai_step(m, w, events), Species::Vex => super::vex::ai_step(m, w), + Species::Ravager => super::ravager::ai_step(m, w, events), Species::Shulker => { super::shulker::tick(m, w, events); super::shulker::ai_step(m, w, events); @@ -673,6 +680,9 @@ pub fn do_hurt_target(m: &mut Mob, who: Who, cx: &mut Cx) { if matches!(m.species(), Species::Hoglin | Species::Zoglin) { return super::piglin::hoglin_hurt_target(m, who, cx); } + if m.species() == Species::Ravager { + super::ravager::on_attack(m, cx.events); + } 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); diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index d0db21426..067a7de06 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -9,6 +9,7 @@ use std::f32::consts::PI; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Anim { Vex, + Ravager, Guardian, Shulker, Quadruped, @@ -458,6 +459,7 @@ pub fn setup( Anim::Phantom => super::phantom::anim(m, s, p), Anim::Guardian => super::guardian::anim(m, s, p), Anim::Vex => super::vex::anim(m, s, p), + Anim::Ravager => super::ravager::anim(m, s, p), Anim::Shulker => super::shulker::anim(m, s, p), Anim::Horse => { super::equine::anim(m, s, p); diff --git a/crates/horizon-client/src/mobs/illager.rs b/crates/horizon-client/src/mobs/illager.rs index ad5237b12..155e9ea5e 100644 --- a/crates/horizon-client/src/mobs/illager.rs +++ b/crates/horizon-client/src/mobs/illager.rs @@ -16,7 +16,11 @@ use std::f32::consts::PI; fn raider(s: Species) -> bool { matches!( s, - Species::Pillager | Species::Vindicator | Species::Witch | Species::Evoker + Species::Pillager + | Species::Vindicator + | Species::Witch + | Species::Evoker + | Species::Ravager ) } fn targets(m: &mut Mob, villagers_seen: bool) { @@ -27,6 +31,7 @@ fn targets(m: &mut Mob, villagers_seen: bool) { Species::Witch, Species::Evoker, Species::Vex, + Species::Ravager, ]; m.ai.targets.add(1, hurt); m.ai.targets.add(2, NearestTarget::player(true)); diff --git a/crates/horizon-client/src/mobs/ravager.rs b/crates/horizon-client/src/mobs/ravager.rs new file mode 100644 index 000000000..0166a21c3 --- /dev/null +++ b/crates/horizon-client/src/mobs/ravager.rs @@ -0,0 +1,258 @@ +//! Ravagers (`Ravager`): charging beasts that trample leaves, bite for 12, +//! get stunned by a raised shield half the time and answer with a roar +//! that hurts and flings everything within 4 blocks. +use super::{ + Damage, Mob, MobEvent, MobWorld, Species, Who, + anim::{Poses, State}, + goals::{Float, HurtByTarget, LookAtPlayer, MeleeAttack, NearestTarget, Stroll}, + path::PathType, +}; +use horizon_core::random::RandomSource; +use std::f32::consts::PI; + +// Ravager state: `counter` attackTick, `counter2` stunnedTick, +// `anim_ticks` roarTick. + +pub fn register(m: &mut Mob) { + m.set_malus(PathType::Leaves, 0.0); + let g = &mut m.ai.goals; + g.add(0, Float); + g.add(4, MeleeAttack::new(1.0, true)); + g.add(5, Stroll::avoiding_water(0.4)); + g.add(6, LookAtPlayer::new(6.0)); + let mut hurt = HurtByTarget::new().alert(); + hurt.ignore = vec![ + Species::Pillager, + Species::Vindicator, + Species::Evoker, + Species::Witch, + Species::Ravager, + ]; + m.ai.targets.add(2, hurt); + m.ai.targets.add(3, NearestTarget::player(true)); + m.ai.targets.add( + 4, + NearestTarget::new( + super::goals::Prey::Filter(|s| { + matches!(s.species, Species::Villager | Species::WanderingTrader) && !s.baby + }), + true, + ), + ); + m.ai.targets + .add(4, NearestTarget::species(&[Species::IronGolem], true)); +} +/// `Ravager.isImmobile`: biting, stunned or roaring. +pub fn immobile(m: &Mob) -> bool { + m.extra.counter > 0 || m.extra.counter2 > 0 || m.extra.anim_ticks > 0 +} +/// `Ravager.doHurtTarget`: the bite animation. +pub fn on_attack(m: &mut Mob, events: &mut Vec) { + m.extra.counter = 10; + events.push(MobEvent::Sound( + "entity.ravager.attack", + [m.position.x, m.position.y, m.position.z], + 1.0, + 1.0, + )); +} +/// `Ravager.blockedByItem`: a raised shield stuns it half the time (else +/// flings the defender); returns whether it was stunned. +pub fn blocked(m: &mut Mob, events: &mut Vec) -> bool { + if m.extra.anim_ticks != 0 { + return false; + } + if m.random.next_double() < 0.5 { + m.extra.counter2 = 40; + events.push(MobEvent::Sound( + "entity.ravager.stunned", + [m.position.x, m.position.y, m.position.z], + 1.0, + 1.0, + )); + true + } else { + false + } +} +/// `Ravager.aiStep`: speed toward 0.35 while hunting, leaf trampling, and +/// the bite, stun and roar clocks. +pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + if !m.alive() { + return; + } + let max = if immobile(m) { + 0.0 + } else if m.ai.target.is_some() { + 0.35 + } else { + 0.3 + }; + m.attrs.speed = if max == 0.0 { + 0.0 + } else { + m.attrs.speed + (max - m.attrs.speed) * 0.1 + }; + if m.horizontal_collision && w.mob_griefing { + let b = m + .bounds() + .inflate(horizon_core::geometry::Vec3::new(0.2, 0.2, 0.2)); + let (lo, hi) = (b.min_position(), b.max_position()); + let mut destroyed = false; + for x in lo.x.floor() as i32..=hi.x.floor() as i32 { + for y in lo.y.floor() as i32..=hi.y.floor() as i32 { + for z in lo.z.floor() as i32..=hi.z.floor() as i32 { + let name = w.block_name(x, y, z); + if name.ends_with("_leaves") { + events.push(MobEvent::BreakBlock { + pos: [x, y, z], + drop: true, + }); + destroyed = true; + } + } + } + } + if !destroyed && m.on_ground { + m.ai.jumping = true; + } + } + if m.extra.anim_ticks > 0 { + m.extra.anim_ticks -= 1; + if m.extra.anim_ticks == 10 { + roar(m, w, events); + } + } + if m.extra.counter > 0 { + m.extra.counter -= 1; + } + if m.extra.counter2 > 0 { + m.extra.counter2 -= 1; + if m.random.next_int_bound(6).unwrap_or(1) == 0 { + let yaw = m.body_yaw.to_radians(); + let width = f64::from(m.width()); + events.push(MobEvent::Particles { + kind: "entity_effect", + at: [ + m.position.x - width * f64::from(yaw.sin()), + m.position.y + f64::from(m.height()) - 0.3, + m.position.z + width * f64::from(yaw.cos()), + ], + count: 0, + }); + } + if m.extra.counter2 == 0 { + events.push(MobEvent::Sound( + "entity.ravager.roar", + [m.position.x, m.position.y, m.position.z], + 1.0, + 1.0, + )); + m.extra.anim_ticks = 20; + } + } +} +/// `Ravager.roar`: 6 damage to everything but illagers within 4 blocks, +/// and a strong knockback (players too, through the client). +fn roar(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + let area = m + .bounds() + .inflate(horizon_core::geometry::Vec3::new(4.0, 4.0, 4.0)); + let from = [m.position.x, m.position.y, m.position.z]; + let fling = |p: horizon_core::geometry::Vec3| { + let (xd, zd) = (p.x - m.position.x, p.z - m.position.z); + let dd = (xd * xd + zd * zd).max(0.001); + [xd / dd * 4.0, 0.2, zd / dd * 4.0] + }; + if let Some(p) = &w.player + && p.alive + && p.bounds.intersects(area) + { + events.push(MobEvent::AttackPlayer(6.0, from)); + events.push(MobEvent::Push { + target: Who::Player, + velocity: fling(p.position), + }); + } + for s in w.others.iter() { + if s.id == m.id || !s.alive || s.species == Species::Ravager || !s.bounds.intersects(area) { + continue; + } + let illager = matches!( + s.species, + Species::Pillager | Species::Vindicator | Species::Evoker + ); + if !illager { + events.push(MobEvent::AttackMob { + target: s.id, + amount: 6.0, + source: Damage::mob(m.id, from), + }); + } + events.push(MobEvent::Push { + target: Who::Mob(s.id), + velocity: fling(s.position), + }); + } + // Entity event 69: a puff of smoke. + events.push(MobEvent::Particles { + kind: "poof", + at: [ + m.position.x, + m.position.y + f64::from(m.height()) / 2.0, + m.position.z, + ], + count: 40, + }); +} +/// `RavagerModel.setupAnim`. +pub fn anim(m: &Mob, s: &State, p: &mut Poses) { + let stunned = if m.extra.counter2 > 0 { + m.extra.counter2 as f32 - s.partial + } else { + 0.0 + }; + let attack = if m.extra.counter > 0 { + m.extra.counter as f32 - s.partial + } else { + 0.0 + }; + if attack > 0.0 { + // Mth.triangleWave(attack, 10). + let wave = ((attack % 10.0 - 5.0).abs() - 2.5) / 2.5; + let scaled = (1.0 + wave) * 0.5; + let head = scaled * scaled * scaled * 12.0; + let neck_x_rot = p.get("neck")[3]; + let neck = p.get("neck"); + neck[2] = -6.5 + head; + neck[1] = -7.0 - head * neck_x_rot.sin(); + let mouth = if attack > 5.0 { + ((-4.0 + attack) / 4.0).sin() * PI * 0.4 + } else { + PI / 20.0 * (PI * attack / 10.0).sin() + }; + p.xrot("mouth", mouth); + } else { + let is_stunned = stunned > 0.0; + let x_rot: f32 = if is_stunned { 0.219_911_49 } else { 0.0 }; + let neck = p.get("neck"); + neck[0] = 0.0; + neck[1] = -7.0 + x_rot.sin(); + neck[2] = 5.5; + neck[3] = x_rot; + p.xrot("mouth", PI * if is_stunned { 0.05 } else { 0.01 }); + if is_stunned { + p.get("neck")[0] = ((stunned / 40.0) * 10.0).sin() * 3.0; + } else if m.extra.anim_ticks > 0 { + let roar = (20.0 - m.extra.anim_ticks as f32 + s.partial) / 20.0; + p.xrot("mouth", PI / 2.0 * (roar * PI * 0.25).sin()); + } + } + p.xrot("head", s.x_rot.to_radians()); + p.yrot("head", s.y_rot.to_radians()); + let (pos, leg) = (s.pos, 0.4 * s.speed); + p.xrot("right_hind_leg", (pos * 0.6662).cos() * leg); + p.xrot("left_hind_leg", (pos * 0.6662 + PI).cos() * leg); + p.xrot("right_front_leg", (pos * 0.6662 + PI).cos() * leg); + p.xrot("left_front_leg", (pos * 0.6662).cos() * leg); +} diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index 7ade4fa58..2f1cbea81 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -78,6 +78,7 @@ pub enum Species { Shulker, Evoker, Vex, + Ravager, } /// Baby bounding boxes (each class's BABY_DIMENSIONS). #[derive(Clone, Copy, Debug)] @@ -881,6 +882,20 @@ pub const ALL: &[Def] = &[ None, Baby::None ), + def!( + Ravager, + "ravager", + "ravager#main", + "entity/illager/ravager", + A::Ravager, + [ + "entity.ravager.ambient", + "entity.ravager.hurt", + "entity.ravager.death" + ], + Some("entity.ravager.step"), + Baby::None + ), def!( Vex, "vex", @@ -1272,6 +1287,7 @@ impl Def { | S::Shulker | S::Evoker | S::Vex + | S::Ravager ) } /// The item tag Animal.isFood tests.