diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 98043ce0f..b6ca051eb 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -30,6 +30,8 @@ pub(super) struct Creatures { /// Player status effects applied by mobs: (effect, ticks left, amplifier). pub player_fire: i32, pub pearl_cooldown: u32, + /// Ticks left of the elder guardian's apparition (MobAppearanceParticle). + pub elder_apparition: i32, } /// MobCategory caps (maxInstancesPerChunk with the 17x17 spawnable chunk area). const CATEGORIES: [(&str, usize); 7] = [ @@ -489,7 +491,11 @@ impl Game<'_> { let light_ok = !kind.category.eq("monster") || matches!( kind.species, - Species::Slime | Species::Ghast | Species::MagmaCube + Species::Slime + | Species::Ghast + | Species::MagmaCube + | Species::Guardian + | Species::ElderGuardian ) || w.brightness(x, y, z) <= 0.5; let consumed = r; @@ -674,6 +680,10 @@ impl Game<'_> { MobEvent::AttackPlayer(amount, from) => { self.mob_hits_player(amount, from, "minecraft:mob_attack") } + MobEvent::DamagePlayer { amount, from, kind } => { + self.mob_hits_player(amount, from, kind) + } + MobEvent::ElderCurse { at } => self.elder_curse(at), MobEvent::Shoot(..) => {} MobEvent::Explode(at, radius) => { // The exploding mob causes the damage (Creeper.explodeCreeper). @@ -1019,6 +1029,30 @@ impl Game<'_> { let xp = 1 + self.world.random.next_int_bound(7).unwrap_or(0) as u32; self.spawn_orbs([p.position.x, p.position.y, p.position.z], xp); } + /// `MobEffectUtil.addEffectToPlayersAround` for an elder guardian: + /// mining fatigue III for five minutes unless a stronger or longer one + /// is on, with the curse sound and the elder's apparition. + fn elder_curse(&mut self, at: [f64; 3]) { + if self.creative || self.spectator() { + return; + } + let Some(p) = self.player.as_ref() else { + return; + }; + let d = p.position.subtract(Vec3::new(at[0], at[1], at[2])); + if d.x * d.x + d.y * d.y + d.z * d.z >= 2500.0 || p.health.dead_or_dying() { + return; + } + let active = self.potions.effects.get("minecraft:mining_fatigue"); + if active.is_some_and(|e| e.amplifier >= 2 && e.duration >= 1200) { + return; + } + let at = [p.position.x, p.position.y, p.position.z]; + self.give_player_effect("minecraft:mining_fatigue", 6000, 2); + self.audio + .play("minecraft:entity.elder_guardian.curse", Some(at), 1.0, 1.0); + self.creatures.elder_apparition = 30; + } fn mob_hits_player(&mut self, amount: f32, from: [f64; 3], damage_type: &str) { if self.creative { return; @@ -1889,6 +1923,30 @@ impl Game<'_> { } Ok(true) } + /// A beam target's centre (half its height), interpolated. + fn beam_target(&self, who: Option, alpha: f64) -> Option { + match who? { + Who::Player => { + let p = self.player.as_ref()?; + let (a, b) = (self.previous_position, p.position); + Some(glam::DVec3::new( + a.x + (b.x - a.x) * alpha, + a.y + (b.y - a.y) * alpha + f64::from(p.dimensions.height) * 0.5, + a.z + (b.z - a.z) * alpha, + )) + } + Who::Mob(id) => { + let m = self.mobs.iter().find(|m| m.id == id)?; + Some(glam::DVec3::new( + m.previous.x + (m.position.x - m.previous.x) * alpha, + m.previous.y + + (m.position.y - m.previous.y) * alpha + + f64::from(m.height()) * 0.5, + m.previous.z + (m.position.z - m.previous.z) * alpha, + )) + } + } + } /// Mob models and projectiles for the entity pass. pub(super) fn mob_geometry(&self, camera: glam::DVec3, alpha: f64, out: &mut Vec) { let atlas = &self.res.atlas; @@ -1916,6 +1974,26 @@ impl Game<'_> { &self.res.mob_data, out, ); + // GuardianRenderer: the laser to the locked target. + if let Some((time, scale)) = horizon_client::mobs::guardian::beam(mob, alpha as f32) + && let Some(target) = self.beam_target(mob.ai.target, alpha) + && let Some(uv) = lookup("entity/guardian/guardian_beam") + { + let a = alpha; + let eye = glam::DVec3::new( + mob.previous.x + (mob.position.x - mob.previous.x) * a, + mob.previous.y + (mob.position.y - mob.previous.y) * a + mob.eye_height(), + mob.previous.z + (mob.position.z - mob.previous.z) * a, + ) - camera; + horizon_client::mobs::guardian::beam_geometry( + eye.as_vec3(), + (target - camera).as_vec3(), + time, + scale, + uv, + out, + ); + } } // Projectiles: arrows as crossed quads, the rest as item billboards. let (yaw, pitch) = (self.yaw.to_radians(), self.pitch.to_radians()); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 2f79a0a67..250cd1d64 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -16,6 +16,7 @@ pub mod equine; pub mod farm; pub mod goals; pub mod golem; +pub mod guardian; pub mod hostile; pub mod illager; pub mod keyframes; @@ -319,6 +320,7 @@ pub enum DamageKind { Cactus, Generic, InWall, + Thorns, } /// A damage source: its type, the attacking entity and where it came from. #[derive(Clone, Copy, Debug)] @@ -508,6 +510,16 @@ pub enum MobEvent { /// A sound event id that is not a static string (variant sounds). SoundId(String, [f64; 3], f32, f32), AttackPlayer(f32, [f64; 3]), + /// Damage of a given type to the player (guardian lasers, thorns). + DamagePlayer { + amount: f32, + from: [f64; 3], + kind: &'static str, + }, + /// `ElderGuardian`'s curse on players within 50 blocks. + ElderCurse { + at: [f64; 3], + }, Shoot([f64; 3], [f64; 3]), Explode([f64; 3], f32), /// Melee or ranged damage to another mob. diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 4f2aadd28..0043c7436 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -80,6 +80,7 @@ pub enum Family { Phantom, /// Slimes and magma cubes. Cube, + Guardian, } pub fn family(s: Species) -> Family { use Species as S; @@ -114,6 +115,7 @@ pub fn family(s: Species) -> Family { S::Phantom => Family::Phantom, S::Enderman => Family::Enderman, S::Slime | S::MagmaCube => Family::Cube, + S::Guardian | S::ElderGuardian => Family::Guardian, _ => Family::Generic, } } @@ -144,6 +146,7 @@ pub fn register(m: &mut Mob) { }, Family::Enderman => super::enderman::register(m), Family::Cube => super::cube::register(m), + Family::Guardian => super::guardian::register(m), } } /// Mob.tick additions that run before the entity tick (creeper fuse, puffing). @@ -166,6 +169,9 @@ pub fn custom_move_control(m: &mut Mob, w: &MobWorld, events: &mut Vec if m.species() == Species::Phantom { return super::phantom::move_control(m); } + if family(m.species()) == Family::Guardian { + return super::guardian::move_control(m); + } match family(m.species()) { Family::Aquatic => aquatic::fish_move_control(m), Family::Cube => super::cube::move_control(m, events), @@ -352,6 +358,8 @@ fn play_ambient(m: &mut Mob, w: &MobWorld, events: &mut Vec) { Species::Cat if !m.extra.tame => "entity.cat.stray_ambient", Species::PolarBear if m.is_baby() => "entity.polar_bear.ambient_baby", Species::Dolphin if !m.fluids.in_water => "entity.dolphin.ambient", + Species::Guardian if !m.fluids.in_water => "entity.guardian.ambient_land", + Species::ElderGuardian if !m.fluids.in_water => "entity.elder_guardian.ambient_land", Species::Axolotl if !m.fluids.in_water => "entity.axolotl.idle_air", Species::Bat if m.extra.flag && m.random.next_int_bound(4).unwrap_or(1) != 0 => return, Species::ZombifiedPiglin if m.ai.target.is_some() => "entity.zombified_piglin.angry", @@ -487,6 +495,7 @@ fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) { | Species::ZombifiedPiglin => undead::custom_server_ai_step(m, cx), Species::Enderman => super::enderman::custom_server_ai_step(m, cx), Species::Blaze => super::nether::blaze_server_ai_step(m, cx), + Species::ElderGuardian => super::guardian::elder_server_ai_step(m, cx), _ => {} } } @@ -512,6 +521,7 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { Species::Zoglin if m.extra.anim_ticks > 0 => m.extra.anim_ticks -= 1, s if super::equine::is_equine(s) => super::equine::ai_step(m, w), Species::Phantom => super::phantom::flap_sound(m, events), + Species::Guardian | Species::ElderGuardian => super::guardian::ai_step(m, w, events), Species::SnowGolem => super::golem::snow_ai_step(m, w, events), Species::Slime | Species::MagmaCube => super::cube::ai_step(m, w, events), Species::Chicken => farm::chicken_ai_step(m, w, events), @@ -582,6 +592,10 @@ pub fn sound_volume(m: &Mob) -> f32 { /// PathfinderMob.getWalkTargetValue (Animal and Monster overrides). pub fn walk_target_value(m: &Mob, w: &MobWorld, p: [i32; 3]) -> f32 { let light = || w.brightness(p[0], p[1], p[2]) - 0.5; + // Guardian.getWalkTargetValue: water first. + if family(m.species()) == Family::Guardian && w.fluid(p[0], p[1], p[2]) == Fluid::Water { + return 10.0 + light(); + } if m.hostile() && !matches!( m.species(), @@ -694,6 +708,7 @@ pub fn ate(m: &mut Mob) { pub fn custom_travel(m: &mut Mob, w: &MobWorld, input: Vec3, events: &mut Vec) -> bool { match family(m.species()) { Family::Aquatic => aquatic::travel(m, w, input, events), + Family::Guardian => super::guardian::travel(m, w, input, events), _ => false, } } diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 77cecee00..486e5d4a8 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -8,6 +8,7 @@ use std::f32::consts::PI; /// The setupAnim implementation a model uses. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Anim { + Guardian, Quadruped, Sheep, Chicken, @@ -453,6 +454,7 @@ pub fn setup( Anim::Illager => super::illager::anim(m, s, p), Anim::Piglin => super::piglin::piglin_anim(m, s, p), Anim::Phantom => super::phantom::anim(m, s, p), + Anim::Guardian => super::guardian::anim(m, s, p), Anim::Horse => { super::equine::anim(m, s, p); // DonkeyModel: the chests show once fitted. diff --git a/crates/horizon-client/src/mobs/guardian.rs b/crates/horizon-client/src/mobs/guardian.rs new file mode 100644 index 000000000..9cca8419a --- /dev/null +++ b/crates/horizon-client/src/mobs/guardian.rs @@ -0,0 +1,447 @@ +//! Guardians and elder guardians (`Guardian`, `ElderGuardian`): the laser +//! charging for 80 (60) ticks before it strikes, spikes that hurt melee +//! attackers while still, flopping on land, the swimming move control, and +//! the elder's mining fatigue curse every minute. +use super::{ + Damage, DamageKind, Mob, MobEvent, MobWorld, Species, Who, + anim::{Poses, State}, + control::{MoveKind, Operation, rotlerp}, + goals::{ + Cx, Goal, LOOK, LookAtPlayer, MOVE, NearestTarget, RandomLookAround, Stroll, goal_clone, + }, + nav::NavKind, + path::PathType, +}; +use horizon_core::{geometry::Vec3, random::RandomSource}; +use std::f32::consts::PI; + +// Guardian state: `flag` an active beam target (DATA_ID_ATTACK_TARGET), +// `flag2` moving (DATA_ID_MOVING), `counter2` the client attack time. +// AnimState slots: tail animation, its speed, spikes animation. +const TAIL: usize = 0; +const TAIL_SPEED: usize = 1; +const SPIKES: usize = 2; +const EYE_X: usize = 3; +const EYE_Y: usize = 4; + +pub fn is_elder(m: &Mob) -> bool { + m.species() == Species::ElderGuardian +} +fn attack_duration(m: &Mob) -> i32 { + if is_elder(m) { 60 } else { 80 } +} +pub fn register(m: &mut Mob) { + m.ai.mov.kind = MoveKind::Custom; + m.ai.nav.kind = NavKind::Water { breach: false }; + m.set_malus(PathType::Water, 0.0); + let t = m.random.next_float(); + m.anim.v[TAIL] = t; + m.anim.vo[TAIL] = t; + let elder = is_elder(m); + if elder { + m.persistent = true; + } + let g = &mut m.ai.goals; + g.add(4, GuardianAttack { attack_time: 0 }); + g.add(7, Stroll::new(1.0).interval(if elder { 400 } else { 80 })); + g.add(8, LookAtPlayer::new(8.0)); + g.add(9, RandomLookAround::default()); + // GuardianAttackSelector: players, squid and axolotls beyond 3 blocks. + m.ai.targets.add( + 1, + NearestTarget::player(true).when(|m, w| { + w.player + .as_ref() + .is_some_and(|p| p.position.distance_squared(m.position) > 9.0) + }), + ); + m.ai.targets.add( + 1, + NearestTarget::species( + &[Species::Squid, Species::GlowSquid, Species::Axolotl], + true, + ), + ); +} +/// `GuardianAttackGoal`. +#[derive(Clone, Debug)] +struct GuardianAttack { + attack_time: i32, +} +impl Goal for GuardianAttack { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE | LOOK + } + fn every_tick(&self) -> bool { + true + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + m.target_info(cx.w).is_some_and(|t| t.alive) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + self.can_use(m, cx) + && (is_elder(m) + || m.target_info(cx.w) + .is_some_and(|t| t.position.distance_squared(m.position) > 9.0)) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + self.attack_time = -10; + m.ai.nav.stop(); + if let Some(t) = m.target_info(cx.w) { + m.look_at_speed([t.position.x, t.eye_y, t.position.z], 90.0, 90.0); + } + } + fn stop(&mut self, m: &mut Mob, _cx: &mut Cx) { + m.extra.flag = false; + m.ai.target = None; + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + let (Some(who), Some(t)) = (m.ai.target, m.target_info(cx.w)) else { + return; + }; + m.ai.nav.stop(); + m.look_at_speed([t.position.x, t.eye_y, t.position.z], 90.0, 90.0); + if !m.sees(cx.w, who) { + m.ai.target = None; + return; + } + self.attack_time += 1; + if self.attack_time == 0 { + m.extra.flag = true; + m.extra.counter2 = 0; + // Entity event 21: the attack sound loop starts. + let sound = "entity.guardian.attack"; + cx.events.push(MobEvent::Sound( + sound, + [m.position.x, m.position.y, m.position.z], + 1.0, + 1.0, + )); + } else if self.attack_time >= attack_duration(m) { + // Indirect magic (1, +2 for the elder; NORMAL difficulty), then + // doHurtTarget. + let magic = if is_elder(m) { 3.0 } else { 1.0 }; + let from = [m.position.x, m.position.y, m.position.z]; + match who { + Who::Player => cx.events.push(MobEvent::DamagePlayer { + amount: magic, + from, + kind: "minecraft:indirect_magic", + }), + Who::Mob(id) => cx.events.push(MobEvent::AttackMob { + target: id, + amount: magic, + source: Damage { + kind: DamageKind::Magic, + attacker: Some(Who::Mob(m.id)), + from: Some(from), + }, + }), + } + super::ai::do_hurt_target(m, who, cx); + m.ai.target = None; + } + } +} +/// `GuardianMoveControl`: swim straight at the wanted point with a wobble, +/// looking ahead. +pub fn move_control(m: &mut Mob) { + let moving = m.ai.mov.operation == Operation::MoveTo && !m.ai.nav.is_done(); + if !moving { + m.set_speed(0.0); + m.extra.flag2 = false; + return; + } + let [wx, wy, wz] = m.ai.mov.wanted; + let d = Vec3::new(wx - m.position.x, wy - m.position.y, wz - m.position.z); + let length = (d.x * d.x + d.y * d.y + d.z * d.z).sqrt(); + let (xd, yd, zd) = (d.x / length, d.y / length, d.z / length); + let y_rot = (d.z.atan2(d.x).to_degrees() as f32) - 90.0; + m.yaw = rotlerp(m.yaw, y_rot, 90.0); + m.body_yaw = m.yaw; + let target = (m.ai.mov.speed_modifier * m.movement_speed()) as f32; + let speed = m.ai.speed + (target - m.ai.speed) * 0.125; + m.set_speed(speed); + let t = f64::from(m.age as i32 + m.id as i32); + let push = (t * 0.5).sin() * 0.05; + let (cos, sin) = ( + f64::from(m.yaw.to_radians().cos()), + f64::from(m.yaw.to_radians().sin()), + ); + let y_push = (t * 0.75).sin() * 0.05; + m.velocity = m.velocity.add_vector(Vec3::new( + push * cos, + y_push * (sin + cos) * 0.25 + f64::from(speed) * yd * 0.1, + push * sin, + )); + let new_look = [ + m.position.x + xd * 2.0, + m.eye_y() + yd / length, + m.position.z + zd * 2.0, + ]; + let old = if m.ai.look.cooldown > 0 { + m.ai.look.wanted + } else { + new_look + }; + let lerp = |a: f64, b: f64| a + (b - a) * 0.125; + m.look_at_speed( + [ + lerp(old[0], new_look[0]), + lerp(old[1], new_look[1]), + lerp(old[2], new_look[2]), + ], + 10.0, + 40.0, + ); + m.extra.flag2 = true; +} +/// `Guardian.travelInWater`; on land the usual travel. +pub fn travel(m: &mut Mob, w: &MobWorld, input: Vec3, events: &mut Vec) -> bool { + if !m.fluids.in_water { + return false; + } + m.move_relative(0.1, input); + m.do_move(w, events); + m.velocity = m.velocity.scale(0.9); + if !m.extra.flag2 && m.ai.target.is_none() { + m.velocity.y -= 0.005; + } + true +} +/// `Guardian.aiStep`: the client animations, air, flopping on land, the +/// beam's clock, and the spikes' thorns. +pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + if !m.alive() { + return; + } + let moving = m.extra.flag2; + let in_water = m.fluids.in_water; + // Tail and spikes (clientSideTailAnimation / clientSideSpikesAnimation). + let a = &mut m.anim; + let speed = &mut a.v[TAIL_SPEED]; + if !in_water { + *speed = 2.0; + } else if moving { + if *speed < 0.5 { + *speed = 4.0; + } else { + *speed += (0.5 - *speed) * 0.1; + } + } else { + *speed += (0.125 - *speed) * 0.2; + } + a.v[TAIL] += a.v[TAIL_SPEED]; + if !in_water { + a.v[SPIKES] = m.random.next_float(); + } else if moving { + a.v[SPIKES] += (0.0 - a.v[SPIKES]) * 0.25; + } else { + a.v[SPIKES] += (1.0 - a.v[SPIKES]) * 0.06; + } + if m.extra.flag && m.extra.counter2 < attack_duration(m) { + m.extra.counter2 += 1; + } + if in_water { + m.air = 300; + } else if m.on_ground { + let r = &mut m.random; + let (x, z) = (r.next_float() * 2.0 - 1.0, r.next_float() * 2.0 - 1.0); + m.velocity = m + .velocity + .add_vector(Vec3::new(f64::from(x) * 0.4, 0.5, f64::from(z) * 0.4)); + m.yaw = m.random.next_float() * 360.0; + m.on_ground = false; + let flop = if is_elder(m) { + "entity.elder_guardian.flop" + } else { + "entity.guardian.flop" + }; + events.push(MobEvent::Sound( + flop, + [m.position.x, m.position.y, m.position.z], + 1.0, + 1.0, + )); + } + if m.extra.flag { + m.yaw = m.head_yaw; + } + // The eye follows the active target, else the player (the camera). + let look = match m.target_info(w).filter(|_| m.extra.flag) { + Some(t) => Some([t.position.x, t.eye_y, t.position.z]), + None => w.player.as_ref().map(|p| p.eye), + }; + if let Some(look) = look { + m.anim.v[EYE_Y] = if look[1] - m.eye_y() > 0.0 { 0.0 } else { 1.0 }; + let yaw = m.head_yaw.to_radians(); + let pitch = m.head_pitch.to_radians(); + let view = [-yaw.sin() * pitch.cos(), yaw.cos() * pitch.cos()]; + let d = [m.position.x - look[0], m.position.z - look[2]]; + let len = (d[0] * d[0] + d[1] * d[1]).sqrt().max(1e-9); + let (dx, dz) = ((d[0] / len) as f32, (d[1] / len) as f32); + // Vec3.yRot(pi / 2) of the direction from the target. + let side = [dz, -dx]; + let dot = view[0] * side[0] + view[1] * side[1]; + m.anim.v[EYE_X] = dot.abs().sqrt() * 2.0 * dot.signum(); + } + // Guardian.hurtServer: a still guardian's spikes hurt a melee attacker. + if m.hurt_time == 10 + && !moving + && let Some((d, _)) = m.last_damage + && matches!(d.kind, DamageKind::PlayerAttack | DamageKind::MobAttack) + { + let from = [m.position.x, m.position.y, m.position.z]; + match d.attacker { + Some(Who::Player) => events.push(MobEvent::DamagePlayer { + amount: 2.0, + from, + kind: "minecraft:thorns", + }), + Some(Who::Mob(id)) => events.push(MobEvent::AttackMob { + target: id, + amount: 2.0, + source: Damage { + kind: DamageKind::Thorns, + attacker: Some(Who::Mob(m.id)), + from: Some(from), + }, + }), + None => {} + } + } +} +/// `ElderGuardian.customServerAiStep`: every minute, mining fatigue III for +/// players within 50 blocks; a home within 16 blocks. +pub fn elder_server_ai_step(m: &mut Mob, cx: &mut Cx) { + if (m.age as i64 + i64::from(m.id)) % 1200 == 0 { + cx.events.push(MobEvent::ElderCurse { + at: [m.position.x, m.position.y, m.position.z], + }); + } + if m.extra.home.is_none() { + m.extra.home = Some([ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ]); + } +} +/// The beam's clock for rendering: (attack time with the partial tick, +/// attack scale), while a target is locked. +pub fn beam(m: &Mob, partial: f32) -> Option<(f32, f32)> { + if !m.extra.flag || m.ai.target.is_none() { + return None; + } + let time = m.extra.counter2 as f32 + partial; + Some((time, time / attack_duration(m) as f32)) +} +/// `GuardianRenderer.renderBeam` from `eye` to `target` (world minus +/// camera): the four rotating sides and the end cap. The texture's V +/// repeats along the beam, so the sides are cut at each repeat. +pub fn beam_geometry( + eye: glam::Vec3, + target: glam::Vec3, + time: f32, + scale: f32, + uv: [f32; 4], + out: &mut Vec, +) { + let v = target - eye; + let length = v.length() + 1.0; + let dir = v.normalize_or_zero(); + let x_rot = dir.y.clamp(-1.0, 1.0).acos(); + let y_rot = std::f32::consts::FRAC_PI_2 - dir.z.atan2(dir.x); + let pose = glam::Mat4::from_translation(eye) + * glam::Mat4::from_rotation_y(y_rot) + * glam::Mat4::from_rotation_x(x_rot); + let rot = time * 0.05 * -1.5; + let cs = scale * scale; + let (r, g, b) = ( + 64 + (cs * 191.0) as u32, + 32 + (cs * 191.0) as u32, + 128u32.saturating_sub((cs * 64.0) as u32), + ); + let color = 0xFF00_0000 | (b.min(255) << 16) | (g.min(255) << 8) | r.min(255); + let at = |a: f32, radius: f32| (a.cos() * radius, a.sin() * radius); + let quarter = std::f32::consts::FRAC_PI_4; + let (wx, wz) = at(rot + PI, 0.2); + let (ex, ez) = at(rot, 0.2); + let (nx, nz) = at(rot + PI / 2.0, 0.2); + let (sx, sz) = at(rot + PI * 1.5, 0.2); + let (wnx, wnz) = at(rot + quarter * 3.0, 0.282); + let (enx, enz) = at(rot + quarter, 0.282); + let (wsx, wsz) = at(rot + quarter * 5.0, 0.282); + let (esx, esz) = at(rot + quarter * 7.0, 0.282); + let tex_off = (time * 0.5) % 1.0; + let min_v = -1.0 + tex_off; + let max_v = min_v + length * 2.5; + let atlas = |u: f32, v: f32| [uv[0] + (uv[2] - uv[0]) * u, uv[1] + (uv[3] - uv[1]) * v]; + let mut push = |p: [f32; 3], t: [f32; 2]| { + out.push(crate::mesher::Vertex { + pos: pose.transform_point3(glam::Vec3::from(p)).to_array(), + uv: t, + layer: 0, + color, + light: 240 | 240 << 8, + }); + }; + // Sides: one quad per texture repeat. + let mut k = min_v.floor(); + while k < max_v { + let (v0, v1) = (min_v.max(k), max_v.min(k + 1.0)); + let (y0, y1) = ( + (v0 - min_v) / (max_v - min_v) * length, + (v1 - min_v) / (max_v - min_v) * length, + ); + let (t0, t1) = (v0 - k, v1 - k); + for (a, c) in [((wx, wz), (ex, ez)), ((nx, nz), (sx, sz))] { + push([a.0, y1, a.1], atlas(0.4999, t1)); + push([a.0, y0, a.1], atlas(0.4999, t0)); + push([c.0, y0, c.1], atlas(0.0, t0)); + push([c.0, y1, c.1], atlas(0.0, t1)); + } + k += 1.0; + } + let base = if (time.floor() as i32) % 2 == 0 { + 0.5 + } else { + 0.0 + }; + push([wnx, length, wnz], atlas(0.5, base + 0.5)); + push([enx, length, enz], atlas(1.0, base + 0.5)); + push([esx, length, esz], atlas(1.0, base)); + push([wsx, length, wsz], atlas(0.5, base)); +} +/// `GuardianModel.setupAnim`: head, spikes by withdrawal, the eye toward +/// what it looks at, and the tail. +pub fn anim(m: &Mob, s: &State, p: &mut Poses) { + p.yrot("head", s.y_rot.to_radians()); + p.xrot("head", s.x_rot.to_radians()); + let spikes = m.anim.lerp(SPIKES, s.partial); + let withdrawal = (1.0 - spikes) * 0.55; + const X: [f32; 12] = [ + 0.0, 0.0, 8.0, -8.0, -8.0, 8.0, 8.0, -8.0, 0.0, 0.0, 8.0, -8.0, + ]; + const Y: [f32; 12] = [ + -8.0, -8.0, -8.0, -8.0, 0.0, 0.0, 0.0, 0.0, 8.0, 8.0, 8.0, 8.0, + ]; + const Z: [f32; 12] = [ + 8.0, -8.0, 0.0, 0.0, -8.0, -8.0, 8.0, 8.0, 8.0, -8.0, 0.0, 0.0, + ]; + for i in 0..12 { + let offset = 1.0 + (s.age * 1.5 + i as f32).cos() * 0.01 - withdrawal; + let part = p.get(&format!("spike{i}")); + part[0] = X[i] * offset; + part[1] = 16.0 + Y[i] * offset; + part[2] = Z[i] * offset; + } + let e = p.get("eye"); + e[0] = m.anim.v[EYE_X]; + e[1] = m.anim.v[EYE_Y]; + let tail = m.anim.lerp(TAIL, s.partial); + p.yrot("tail0", tail.sin() * PI * 0.05); + p.yrot("tail1", tail.sin() * PI * 0.1); + p.yrot("tail2", tail.sin() * PI * 0.15); +} diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index 505fccb5a..072bcd259 100644 --- a/crates/horizon-client/src/mobs/spawning.rs +++ b/crates/horizon-client/src/mobs/spawning.rs @@ -76,6 +76,25 @@ pub fn spawn_rules_ok( let light = w.world.light(x, y, z); let raw = i32::from(light >> 4).max(i32::from(light & 15)); match species { + // Guardian.checkGuardianSpawnRules. + S::Guardian | S::ElderGuardian => { + let sky_from_below = || { + let sea = 63; + if y >= sea { + return w.can_see_sky(x, y, z); + } + if !w.can_see_sky(x, sea, z) { + return false; + } + ((y + 1)..sea).all(|yy| { + let f = w.facts.get(w.world.block(x, yy, z)); + !f.full_collision || f.fluid != Fluid::None + }) + }; + (random.next_int_bound(20).unwrap_or(1) == 0 || !sky_from_below()) + && w.fluid(x, y, z) == Fluid::Water + && w.fluid(x, y - 1, z) == Fluid::Water + } // Animal.checkAnimalSpawnRules: animals_spawnable_on below and bright. S::Pig | S::Cow diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index 6ac764e65..9139db90f 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -73,6 +73,8 @@ pub enum Species { Ghast, Blaze, Strider, + Guardian, + ElderGuardian, } /// Baby bounding boxes (each class's BABY_DIMENSIONS). #[derive(Clone, Copy, Debug)] @@ -862,6 +864,34 @@ pub const ALL: &[Def] = &[ None, Baby::None ), + def!( + Guardian, + "guardian", + "guardian#main", + "entity/guardian/guardian", + A::Guardian, + [ + "entity.guardian.ambient", + "entity.guardian.hurt", + "entity.guardian.death" + ], + None, + Baby::None + ), + def!( + ElderGuardian, + "elder_guardian", + "elder_guardian#main", + "entity/guardian/guardian_elder", + A::Guardian, + [ + "entity.elder_guardian.ambient", + "entity.elder_guardian.hurt", + "entity.elder_guardian.death" + ], + None, + Baby::None + ), def!( Pillager, "pillager", @@ -1147,6 +1177,8 @@ impl Def { S::Axolotl | S::Frog | S::Turtle + | S::Guardian + | S::ElderGuardian | S::IronGolem | S::SnowGolem | S::Ghast @@ -1194,6 +1226,8 @@ impl Def { | S::Zoglin | S::Ghast | S::Blaze + | S::Guardian + | S::ElderGuardian ) } /// The item tag Animal.isFood tests.