diff --git a/crates/horizon-client/src/effects.rs b/crates/horizon-client/src/effects.rs index bfa582781..9144d2ec1 100644 --- a/crates/horizon-client/src/effects.rs +++ b/crates/horizon-client/src/effects.rs @@ -95,6 +95,8 @@ pub enum EffectAction { Heal(f32), /// Damage with a damage type id (`minecraft:magic`, `minecraft:wither`). Hurt(&'static str, f32), + /// PoisonMobEffect: one magic damage while above one health. + Poison, Exhaust(f32), /// FoodData.eat(nutrition, saturation modifier). Eat(i32, f32), @@ -107,7 +109,7 @@ fn periodic(id: &str, tick: i32, amp: i32) -> Option { }; match id { "regeneration" if every(50) => Some(EffectAction::Heal(1.0)), - "poison" if every(25) => Some(EffectAction::Hurt("minecraft:magic", 1.0)), + "poison" if every(25) => Some(EffectAction::Poison), "wither" if every(40) => Some(EffectAction::Hurt("minecraft:wither", 1.0)), "hunger" => Some(EffectAction::Exhaust(0.005 * (amp + 1) as f32)), "saturation" if tick >= 1 => Some(EffectAction::Eat(amp + 1, 1.0)), diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index a1b441fa8..635296cd4 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -157,6 +157,10 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { } map.insert("powered".into(), Tag::Byte(i8::from(m.extra.charged))); } + "minecraft:slime" | "minecraft:magma_cube" => { + map.insert("Size".into(), Tag::Int(m.extra.size.max(1) - 1)); + map.insert("wasOnGround".into(), Tag::Byte(i8::from(m.extra.flag2))); + } // Skeletons are spawned holding a bow (finalizeSpawn); a mob loaded // from NBT keeps only its saved equipment. "minecraft:skeleton" | "minecraft:stray" | "minecraft:bogged" => { @@ -221,6 +225,15 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - if m.species() == crate::mobs::Species::Creeper { m.extra.charged = flag(tag, "powered"); } + if matches!( + m.species(), + crate::mobs::Species::Slime | crate::mobs::Species::MagmaCube + ) { + let health = m.health; + crate::mobs::cube::set_size(&mut m, number(tag.get("Size")).unwrap_or(0) as i32 + 1); + m.health = health; + m.extra.flag2 = flag(tag, "wasOnGround"); + } // A mob saved mid-death finishes dying; one saved dead is dropped. if m.health <= 0.0 && m.death_time == 0 { return None; diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 9aef30f12..d29710174 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -28,7 +28,6 @@ use std::sync::Arc; pub(super) struct Creatures { pub projectiles: Vec, /// Player status effects applied by mobs: (effect, ticks left, amplifier). - pub player_effects: Vec<(&'static str, i32, i32)>, pub player_fire: i32, pub pearl_cooldown: u32, } @@ -621,8 +620,7 @@ impl Game<'_> { if let Some(id) = self.spawn_mob(&m.kind.id, at, m.persistent) && let Some(c) = self.mobs.iter_mut().find(|c| c.id == id) { - c.extra.size = size / 2; - c.health = c.kind.max_health * (size / 2 * size / 2) as f32; + horizon_client::mobs::cube::set_size(c, size / 2); } } } @@ -755,12 +753,7 @@ impl Game<'_> { ticks, amplifier, } => { - if !self.creative { - self.creatures.player_effects.retain(|e| e.0 != effect); - self.creatures - .player_effects - .push((effect, ticks, amplifier)); - } + self.give_player_effect(effect, ticks, amplifier); } MobEvent::Ignite { target, seconds } => match target { Who::Player => { @@ -898,7 +891,19 @@ impl Game<'_> { self.after_block(outcome, amount, attacker, projectile); } } - /// Poison, wither, hunger and burning applied to the player by mobs. + /// A status effect from a mob, arrow or splash potion (`addEffect`). + pub(super) fn give_player_effect(&mut self, effect: &'static str, ticks: i32, amplifier: i32) { + if self.creative || self.spectator() { + return; + } + let id = effect.strip_prefix("minecraft:").unwrap_or(effect); + self.potions + .effects + .add(horizon_client::effects::EffectInstance::new( + id, ticks, amplifier, + )); + } + /// Burning applied to the player by mobs. fn tick_player_effects(&mut self) { let in_water = self.player.as_ref().is_some_and(|p| { let (x, y, z) = ( @@ -918,35 +923,6 @@ impl Game<'_> { self.creatures.player_fire -= 1; } } - let mut effects = std::mem::take(&mut self.creatures.player_effects); - for (effect, ticks, amp) in &mut effects { - // MobEffect.shouldApplyEffectTickThisTick intervals. - let interval = |base: i32| (base >> (*amp).max(0)).max(1); - match *effect { - "minecraft:poison" if *ticks % interval(25) == 0 => { - if self - .player - .as_ref() - .is_some_and(|p| p.health.health() > 1.0) - { - self.hurt_player_by("minecraft:magic", 1.0); - } - } - "minecraft:wither" if *ticks % interval(40) == 0 => { - self.hurt_player_by("minecraft:wither", 1.0) - } - "minecraft:hunger" => { - if let Some(p) = self.player.as_mut() { - p.health.food.exhaustion = - (p.health.food.exhaustion + 0.005 * (*amp + 1) as f32).min(40.0); - } - } - _ => {} - } - *ticks -= 1; - } - effects.retain(|e| e.1 > 0); - self.creatures.player_effects = effects; if self.creatures.pearl_cooldown > 0 { self.creatures.pearl_cooldown -= 1; } @@ -1045,7 +1021,7 @@ impl Game<'_> { "minecraft:arrow", ); if let Some((e, t)) = effect { - self.creatures.player_effects.push((e, *t, 0)); + self.give_player_effect(e, *t, 0); } self.audio.play( "minecraft:entity.arrow.hit_player", @@ -1207,12 +1183,15 @@ impl Game<'_> { if d2 < 16.0 && !self.creative { let scale = 1.0 - d2.sqrt() / 4.0; let t = (f64::from(*ticks) * scale + 0.5) as i32; + if let Some(owner) = self.combat.projectile_owner { + self.note_attacker("minecraft:indirect_magic", owner); + } match *effect { "minecraft:instant_damage" => self.hurt_player_by( "minecraft:indirect_magic", 6.0 * (1 << *amplifier) as f32 * scale as f32, ), - e if t > 20 => self.creatures.player_effects.push((e, t, *amplifier)), + e if t > 20 => self.give_player_effect(e, t, *amplifier), _ => {} } } diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index e03729568..ffa2c86da 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -199,6 +199,14 @@ impl Game<'_> { let _ = p.hurt(&self.motion.health, rules, id, amount); } } + EffectAction::Poison => { + if !creative + && p.health.health() > 1.0 + && let Some(id) = self.motion.health.damage.id("minecraft:magic") + { + let _ = p.hurt(&self.motion.health, rules, id, 1.0); + } + } EffectAction::Exhaust(amount) => { p.health.food.exhaustion = (p.health.food.exhaustion + amount).min(40.0); } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 66dde60cb..bafbed23f 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -9,7 +9,9 @@ pub mod ai; pub mod anim; pub mod aquatic; pub mod control; +pub mod cube; pub mod data; +pub mod enderman; pub mod farm; pub mod goals; pub mod hostile; @@ -21,6 +23,7 @@ pub mod render; pub mod spawning; pub mod species; pub mod undead; +pub mod witch; mod world; use crate::{blocks::BlockFacts, mesher::Vertex, world::World}; diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 6ce290fff..449a6609a 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -67,6 +67,10 @@ pub enum Family { Generic, /// No goals at all (Giant). Inert, + Witch, + Enderman, + /// Slimes and magma cubes. + Cube, } pub fn family(s: Species) -> Family { use Species as S; @@ -90,6 +94,9 @@ pub fn family(s: Species) -> Family { | S::Pufferfish | S::Dolphin => Family::Aquatic, S::Giant => Family::Inert, + S::Witch => Family::Witch, + S::Enderman => Family::Enderman, + S::Slime | S::MagmaCube => Family::Cube, _ => Family::Generic, } } @@ -101,6 +108,9 @@ pub fn register(m: &mut Mob) { Family::Aquatic => aquatic::register(m), Family::Generic => generic(m), Family::Inert => {} + Family::Witch => super::witch::register(m), + Family::Enderman => super::enderman::register(m), + Family::Cube => super::cube::register(m), } } /// Mob.tick additions that run before the entity tick (creeper fuse, puffing). @@ -116,9 +126,11 @@ fn pre_tick(m: &mut Mob, events: &mut Vec) { } } /// Move controls implemented by the species (MoveKind::Custom). -pub fn custom_move_control(m: &mut Mob) { - if family(m.species()) == Family::Aquatic { - aquatic::fish_move_control(m) +pub fn custom_move_control(m: &mut Mob, events: &mut Vec) { + match family(m.species()) { + Family::Aquatic => aquatic::fish_move_control(m), + Family::Cube => super::cube::move_control(m, events), + _ => {} } } /// A fallback for species without bespoke goals yet: animals stroll and @@ -413,7 +425,7 @@ fn server_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { let mut cx = Cx { w, events }; custom_server_ai_step(m, &mut cx); } - m.move_control_tick(w); + m.move_control_tick(w, events); m.look_control_tick(); m.jump_control_tick(); } @@ -428,6 +440,7 @@ fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) { | Species::Stray | Species::Bogged | Species::WitherSkeleton => undead::custom_server_ai_step(m, cx), + Species::Enderman => super::enderman::custom_server_ai_step(m, cx), _ => {} } } @@ -436,6 +449,14 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { aquatic::ai_step(m, w, events); } match m.species() { + Species::Witch => { + let far = m + .target_info(w) + .is_some_and(|t| t.position.distance_squared(m.position) > 121.0); + super::witch::ai_step(m, events, far) + } + Species::Enderman => super::enderman::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), Species::Sheep if m.extra.anim_ticks > 0 => { m.extra.anim_ticks -= 1; @@ -523,7 +544,7 @@ pub fn walk_target_value(m: &Mob, w: &MobWorld, p: [i32; 3]) -> f32 { /// Species damage filters (LivingEntity.hurtServer overrides). pub fn accepts_damage(m: &mut Mob, source: &Damage, _amount: f32) -> bool { match m.species() { - Species::Enderman if source.is_projectile() => false, + Species::Enderman => super::enderman::on_damage(m, source), Species::WitherSkeleton | Species::Blaze | Species::MagmaCube | Species::Strider if source.is_fire() => { @@ -533,8 +554,11 @@ pub fn accepts_damage(m: &mut Mob, source: &Damage, _amount: f32) -> bool { } } /// Reactions after taking damage (sounds, anger). -pub fn after_hurt(m: &mut Mob, _source: &Damage) { +pub fn after_hurt(m: &mut Mob, source: &Damage) { m.ai.no_action_time = 0; + if m.species() == Species::Enderman { + super::enderman::after_hurt(m, source); + } } /// Mob.doHurtTarget: attack damage (+ species extras) to the target. pub fn do_hurt_target(m: &mut Mob, who: Who, cx: &mut Cx) { diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index ad0889f81..4a703bad5 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -205,6 +205,10 @@ pub enum ArmPose { CrossbowHold, CrossbowCharge, } +/// HumanoidModel.setupAnim with empty hands (endermen). +pub fn humanoid_pub(p: &mut Poses, s: &State, _m: &Mob) { + humanoid(p, s, ArmPose::Empty, ArmPose::Empty, None, false); +} /// HumanoidModel.setupAnim up to (and including) the attack animation hook. fn humanoid( p: &mut Poses, @@ -413,6 +417,9 @@ pub fn setup( bob(p, "left_arm", s.age, -1.0); } } + Anim::Witch => super::witch::anim(m, s, p), + Anim::Enderman => super::enderman::anim(m, s, p), + Anim::Slime | Anim::MagmaCube => super::cube::anim(m, s, p), Anim::Creeper => { head(p, s); p.xrot("right_hind_leg", (s.pos * 0.6662).cos() * 1.4 * s.speed); diff --git a/crates/horizon-client/src/mobs/control.rs b/crates/horizon-client/src/mobs/control.rs index 10c42f5c4..bd31e1715 100644 --- a/crates/horizon-client/src/mobs/control.rs +++ b/crates/horizon-client/src/mobs/control.rs @@ -123,10 +123,10 @@ impl Mob { self.yaw = rotate_toward_max(self.yaw, y_rot, y_max); } /// MoveControl.tick (and the flying / smooth-swimming variants). - pub fn move_control_tick(&mut self, w: &MobWorld) { + pub fn move_control_tick(&mut self, w: &MobWorld, events: &mut Vec) { let attr_speed = self.movement_speed(); match self.ai.mov.kind { - MoveKind::Custom => super::ai::custom_move_control(self), + MoveKind::Custom => super::ai::custom_move_control(self, events), MoveKind::Ground => self.ground_move_tick(w, attr_speed), MoveKind::Flying { max_turn, hovers } => { let m = &mut self.ai.mov; diff --git a/crates/horizon-client/src/mobs/cube.rs b/crates/horizon-client/src/mobs/cube.rs index 6fef85290..63770987f 100644 --- a/crates/horizon-client/src/mobs/cube.rs +++ b/crates/horizon-client/src/mobs/cube.rs @@ -16,10 +16,26 @@ const TARGET_SQUISH: usize = 1; pub fn register(m: &mut Mob) { m.ai.goals.add(1, CubeFloat); m.ai.goals.add(2, CubeAttack { tired: 0 }); - m.ai.goals.add(4, CubeRandomDirection { degrees: 0.0, next: 0 }); + m.ai.goals.add( + 4, + CubeRandomDirection { + degrees: 0.0, + next: 0, + }, + ); m.ai.goals.add(5, CubeKeepJumping); - m.ai.targets.add(1, NearestTarget::new(Prey::Player, true).interval(10).when(|m, w| w.player.as_ref().is_some_and(|p| (p.position.y - m.position.y).abs() <= 4.0))); - m.ai.targets.add(3, NearestTarget::species(&[Species::IronGolem], true)); + m.ai.targets.add( + 1, + NearestTarget::new(Prey::Player, true) + .interval(10) + .when(|m, w| { + w.player + .as_ref() + .is_some_and(|p| (p.position.y - m.position.y).abs() <= 4.0) + }), + ); + m.ai.targets + .add(3, NearestTarget::species(&[Species::IronGolem], true)); m.ai.mov.kind = MoveKind::Custom; m.ai.nav.can_float = true; m.extra.vector[0] = f64::from(180.0 * m.yaw / std::f32::consts::PI); @@ -47,7 +63,11 @@ fn can_deal_damage(m: &Mob) -> bool { } fn jump_delay(m: &mut Mob) -> i32 { let base = m.random.next_int_bound(20).unwrap_or(0) + 10; - if m.species() == Species::MagmaCube { base * 4 } else { base } + if m.species() == Species::MagmaCube { + base * 4 + } else { + base + } } fn sound_pitch(m: &mut Mob) -> f32 { let adjust = if tiny(m) { 1.4 } else { 0.8 }; @@ -102,7 +122,11 @@ fn set_wanted(m: &mut Mob, speed: f64) { } /// AbstractCubeMob.jumpFromGround (and MagmaCube's size boost). pub fn jump(m: &mut Mob) { - let boost = if m.species() == Species::MagmaCube { m.extra.size as f32 * 0.1 } else { 0.0 }; + let boost = if m.species() == Species::MagmaCube { + m.extra.size as f32 * 0.1 + } else { + 0.0 + }; m.velocity.y = f64::from(m.jump_power() + boost); } /// AbstractCubeMob.tick squish and landing sounds, plus contact damage. @@ -125,22 +149,43 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { m.anim.v[TARGET_SQUISH] = 1.0; } m.extra.flag2 = m.on_ground; - m.anim.v[TARGET_SQUISH] *= if m.species() == Species::MagmaCube { 0.9 } else { 0.6 }; + m.anim.v[TARGET_SQUISH] *= if m.species() == Species::MagmaCube { + 0.9 + } else { + 0.6 + }; // playerTouch -> dealDamage if can_deal_damage(m) && m.alive() && let Some(p) = &w.player && p.alive && !p.creative - && m.bounds().intersects(p.bounds) + // Player.touch: entities within the player's box grown by (1, 0.5, 1). + && m.bounds().intersects(p.bounds.inflate(Vec3::new(1.0, 0.5, 1.0))) { let reach = 2.04f32.sqrt() as f64 - 0.6; - if m.bounds().inflate(Vec3::new(reach, 0.0, reach)).intersects(p.bounds) { - let damage = m.attrs.attack_damage + if m.species() == Species::MagmaCube { 2.0 } else { 0.0 }; - events.push(MobEvent::AttackPlayer(damage, [m.position.x, m.position.y, m.position.z])); + if m.bounds() + .inflate(Vec3::new(reach, 0.0, reach)) + .intersects(p.bounds) + { + let damage = m.attrs.attack_damage + + if m.species() == Species::MagmaCube { + 2.0 + } else { + 0.0 + }; + events.push(MobEvent::AttackPlayer( + damage, + [m.position.x, m.position.y, m.position.z], + )); if m.species() == Species::Slime { let pitch = (m.random.next_float() - m.random.next_float()) * 0.2 + 1.0; - events.push(MobEvent::Sound("entity.slime.attack", m.center(), 1.0, pitch)); + events.push(MobEvent::Sound( + "entity.slime.attack", + m.center(), + 1.0, + pitch, + )); } } } @@ -161,7 +206,8 @@ pub fn scale(m: &Mob, alpha: f32) -> glam::Mat4 { let w = 1.0 / (squish + 1.0); let mut t = glam::Mat4::IDENTITY; if m.species() == Species::Slime { - t = glam::Mat4::from_scale(glam::Vec3::splat(0.999)) * glam::Mat4::from_translation(glam::Vec3::new(0.0, 0.001, 0.0)); + t = glam::Mat4::from_scale(glam::Vec3::splat(0.999)) + * glam::Mat4::from_translation(glam::Vec3::new(0.0, 0.001, 0.0)); } t * glam::Mat4::from_scale(glam::Vec3::new(w * size, 1.0 / w * size, w * size)) } @@ -197,13 +243,17 @@ impl Goal for CubeAttack { LOOK } fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { - m.target_info(cx.w).is_some_and(|t| t.alive && !(t.is_player && t.creative)) + m.target_info(cx.w) + .is_some_and(|t| t.alive && !(t.is_player && t.creative)) } fn start(&mut self, _: &mut Mob, _: &mut Cx) { self.tired = reduced(300); } fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { - if !m.target_info(cx.w).is_some_and(|t| t.alive && !(t.is_player && t.creative)) { + if !m + .target_info(cx.w) + .is_some_and(|t| t.alive && !(t.is_player && t.creative)) + { return false; } self.tired -= 1; @@ -232,7 +282,8 @@ impl Goal for CubeRandomDirection { LOOK } fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool { - m.ai.target.is_none() && (m.on_ground || m.is_in_liquid() || m.has_effect("minecraft:levitation")) + m.ai.target.is_none() + && (m.on_ground || m.is_in_liquid() || m.has_effect("minecraft:levitation")) } fn tick(&mut self, m: &mut Mob, _: &mut Cx) { self.next -= 1; diff --git a/crates/horizon-client/src/mobs/enderman.rs b/crates/horizon-client/src/mobs/enderman.rs index 7f7a2ae81..13e6c8a44 100644 --- a/crates/horizon-client/src/mobs/enderman.rs +++ b/crates/horizon-client/src/mobs/enderman.rs @@ -4,7 +4,10 @@ use super::{ Damage, Mob, MobEvent, MobWorld, Species, Who, anim::{Poses, State}, - goals::{Cx, Float, Goal, HurtByTarget, JUMP, LookAtPlayer, MOVE, MeleeAttack, NearestTarget, RandomLookAround, Stroll, TARGET, goal_clone, reduced}, + goals::{ + Cx, Float, Goal, HurtByTarget, JUMP, LookAtPlayer, MOVE, MeleeAttack, NearestTarget, + RandomLookAround, Stroll, TARGET, goal_clone, reduced, + }, }; use crate::blocks::Fluid; use horizon_core::{ @@ -24,9 +27,19 @@ pub fn register(m: &mut Mob) { g.add(8, RandomLookAround::default()); g.add(10, LeaveBlock); g.add(11, TakeBlock); - m.ai.targets.add(1, LookForPlayer { pending: false, aggro: 0, teleport: 0 }); + m.ai.targets.add( + 1, + LookForPlayer { + pending: false, + aggro: 0, + teleport: 0, + }, + ); m.ai.targets.add(2, HurtByTarget::new()); - m.ai.targets.add(3, NearestTarget::species(&[Species::Endermite], true).reach()); + m.ai.targets.add( + 3, + NearestTarget::species(&[Species::Endermite], true).reach(), + ); m.set_malus(super::path::PathType::Water, -1.0); } /// Enderman.isBeingStaredBy: the player's look within a cone of 0.025 / distance @@ -37,16 +50,31 @@ pub fn stared_at(m: &Mob, w: &MobWorld) -> bool { return false; } let look = p.look; - let dir = [m.position.x - p.position.x, m.eye_y() - p.eye[1], m.position.z - p.position.z]; + let dir = [ + m.position.x - p.position.x, + m.eye_y() - p.eye[1], + m.position.z - p.position.z, + ]; let dist = (dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]).sqrt(); if dist < 1.0e-6 { return false; } let dot = (look[0] * dir[0] + look[1] * dir[1] + look[2] * dir[2]) / dist; - dot > 1.0 - 0.025 / dist && w.line_of_sight(Vec3::new(p.eye[0], p.eye[1], p.eye[2]), Vec3::new(m.position.x, m.eye_y(), m.position.z)) + dot > 1.0 - 0.025 / dist + && w.line_of_sight( + Vec3::new(p.eye[0], p.eye[1], p.eye[2]), + Vec3::new(m.position.x, m.eye_y(), m.position.z), + ) } /// Enderman.teleport(x, y, z): drop to standing ground, require free space without liquid. -pub fn teleport_to(m: &mut Mob, w: &MobWorld, x: f64, y: f64, z: f64, events: &mut Vec) -> bool { +pub fn teleport_to( + m: &mut Mob, + w: &MobWorld, + x: f64, + y: f64, + z: f64, + events: &mut Vec, +) -> bool { let (bx, bz) = (x.floor() as i32, z.floor() as i32); if !w.world.loaded(bx, bz) { return false; @@ -61,7 +89,10 @@ pub fn teleport_to(m: &mut Mob, w: &MobWorld, x: f64, y: f64, z: f64, events: &m } let ny = f64::from(by + 1); let half = f64::from(m.width()) / 2.0; - let b = Aabb::new(Vec3::new(x - half, ny, z - half), Vec3::new(x + half, ny + f64::from(m.height()), z + half)); + let b = Aabb::new( + Vec3::new(x - half, ny, z - half), + Vec3::new(x + half, ny + f64::from(m.height()), z + half), + ); if !w.no_collision(b) || w.fluids(b, ny + m.eye_height()).in_water || w.fluids(b, ny).in_lava { return false; } @@ -71,7 +102,12 @@ pub fn teleport_to(m: &mut Mob, w: &MobWorld, x: f64, y: f64, z: f64, events: &m m.velocity = Vec3::ZERO; m.ai.nav.stop(); events.push(MobEvent::Sound("entity.enderman.teleport", old, 1.0, 1.0)); - events.push(MobEvent::Sound("entity.enderman.teleport", m.center(), 1.0, 1.0)); + events.push(MobEvent::Sound( + "entity.enderman.teleport", + m.center(), + 1.0, + 1.0, + )); true } pub fn teleport_random(m: &mut Mob, w: &MobWorld, events: &mut Vec) -> bool { @@ -83,8 +119,18 @@ pub fn teleport_random(m: &mut Mob, w: &MobWorld, events: &mut Vec) -> let z = m.position.z + (m.random.next_double() - 0.5) * 64.0; teleport_to(m, w, x, y, z, events) } -fn teleport_towards(m: &mut Mob, w: &MobWorld, at: Vec3, eye_y: f64, events: &mut Vec) -> bool { - let mut d = [m.position.x - at.x, m.position.y + f64::from(m.height()) * 0.5 - eye_y, m.position.z - at.z]; +fn teleport_towards( + m: &mut Mob, + w: &MobWorld, + at: Vec3, + eye_y: f64, + events: &mut Vec, +) -> bool { + let mut d = [ + m.position.x - at.x, + m.position.y + f64::from(m.height()) * 0.5 - eye_y, + m.position.z - at.z, + ]; let len = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt().max(1.0e-9); d = d.map(|c| c / len); let x = m.position.x + (m.random.next_double() - 0.5) * 8.0 - d[0] * 16.0; @@ -96,8 +142,13 @@ fn teleport_towards(m: &mut Mob, w: &MobWorld, at: Vec3, eye_y: f64, events: &mu pub fn custom_server_ai_step(m: &mut Mob, cx: &mut Cx) { if cx.w.daylight && m.age as i32 >= m.extra.counter + 600 { let br = super::hostile::mob_brightness(m, cx.w); - let (x, y, z) = (m.position.x.floor() as i32, m.position.y.floor() as i32, m.position.z.floor() as i32); - if br > 0.5 && cx.w.can_see_sky(x, y, z) && m.random.next_float() * 30.0 < (br - 0.4) * 2.0 { + let (x, y, z) = ( + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ); + if br > 0.5 && cx.w.can_see_sky(x, y, z) && m.random.next_float() * 30.0 < (br - 0.4) * 2.0 + { m.ai.target = None; teleport_random(m, cx.w, cx.events); } @@ -132,7 +183,15 @@ pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { } } } - if (m.fluids.in_water || w.raining && w.can_see_sky(m.position.x.floor() as i32, m.eye_y().floor() as i32, m.position.z.floor() as i32)) && m.alive() { + if (m.fluids.in_water + || w.raining + && w.can_see_sky( + m.position.x.floor() as i32, + m.eye_y().floor() as i32, + m.position.z.floor() as i32, + )) + && m.alive() + { // Hurt by water: teleport away. m.extra.counter2 = m.extra.counter2.max(1); } @@ -195,7 +254,10 @@ impl Goal for LookForPlayer { } fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { let Some(p) = &cx.w.player else { return false }; - self.pending = p.alive && !p.creative && m.distance_sqr(p.position) <= m.attrs.follow_range.powi(2) && stared_at(m, cx.w); + self.pending = p.alive + && !p.creative + && m.distance_sqr(p.position) <= m.attrs.follow_range.powi(2) + && stared_at(m, cx.w); self.pending } fn start(&mut self, m: &mut Mob, _: &mut Cx) { @@ -209,7 +271,9 @@ impl Goal for LookForPlayer { m.ai.target = None; } fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { - let Some(p) = cx.w.player.clone() else { return false }; + let Some(p) = cx.w.player.clone() else { + return false; + }; if self.pending { if !stared_at(m, cx.w) { return false; @@ -217,7 +281,10 @@ impl Goal for LookForPlayer { m.turn_to(p.eye, 10.0, 10.0); return true; } - p.alive && !p.creative && m.ai.target == Some(Who::Player) && m.distance_sqr(p.position) <= m.attrs.follow_range.powi(2) + p.alive + && !p.creative + && m.ai.target == Some(Who::Player) + && m.distance_sqr(p.position) <= m.attrs.follow_range.powi(2) } fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { let Some(p) = cx.w.player.clone() else { return }; @@ -226,7 +293,12 @@ impl Goal for LookForPlayer { if self.aggro <= 0 { self.pending = false; m.ai.target = Some(Who::Player); - cx.events.push(MobEvent::Sound("entity.enderman.stare", m.center(), 2.5, 1.0)); + cx.events.push(MobEvent::Sound( + "entity.enderman.stare", + m.center(), + 2.5, + 1.0, + )); } } else if m.ai.target == Some(Who::Player) { if stared_at(m, cx.w) { @@ -240,7 +312,10 @@ impl Goal for LookForPlayer { if far { self.teleport += 1; } - if far && t >= reduced(30) && teleport_towards(m, cx.w, p.position, p.eye[1], cx.events) { + if far + && t >= reduced(30) + && teleport_towards(m, cx.w, p.position, p.eye[1], cx.events) + { self.teleport = 0; } } @@ -268,13 +343,30 @@ impl Goal for TakeBlock { return; } // Reachable: the clip from the mob's column hits this block first. - let from = Vec3::new(m.position.x.floor() + 0.5, f64::from(y) + 0.5, m.position.z.floor() + 0.5); + let from = Vec3::new( + m.position.x.floor() + 0.5, + f64::from(y) + 0.5, + m.position.z.floor() + 0.5, + ); let to = Vec3::new(f64::from(x) + 0.5, f64::from(y) + 0.5, f64::from(z) + 0.5); let dx = (x - m.position.x.floor() as i32).abs() + (z - m.position.z.floor() as i32).abs(); - if dx > 0 && !cx.w.clear_between(from, Vec3::new(to.x - (to.x - from.x) * 0.3, to.y, to.z - (to.z - from.z) * 0.3), false) { + if dx > 0 + && !cx.w.clear_between( + from, + Vec3::new( + to.x - (to.x - from.x) * 0.3, + to.y, + to.z - (to.z - from.z) * 0.3, + ), + false, + ) + { return; } - cx.events.push(MobEvent::SetBlock { pos: [x, y, z], state: cx.w.world.air }); + cx.events.push(MobEvent::SetBlock { + pos: [x, y, z], + state: cx.w.world.air, + }); m.extra.carried = Some(state); } } @@ -290,16 +382,31 @@ impl Goal for LeaveBlock { m.extra.carried.is_some() && m.random.next_int_bound(reduced(2000)).unwrap_or(1) == 0 } fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { - let Some(carried) = m.extra.carried else { return }; + let Some(carried) = m.extra.carried else { + return; + }; let x = (m.position.x - 1.0 + m.random.next_double() * 2.0).floor() as i32; let y = (m.position.y + m.random.next_double() * 2.0).floor() as i32; let z = (m.position.z - 1.0 + m.random.next_double() * 2.0).floor() as i32; let target = cx.w.world.block(x, y, z); let below = cx.w.world.block(x, y - 1, z); let bf = cx.w.facts.get(below); - let free = !cx.w.others.iter().any(|s| s.bounds.intersects(Aabb::new(Vec3::new(f64::from(x), f64::from(y), f64::from(z)), Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1))))); - if cx.w.facts.is_air(target) && !cx.w.facts.is_air(below) && &*bf.name != "minecraft:bedrock" && bf.full_collision && free { - cx.events.push(MobEvent::SetBlock { pos: [x, y, z], state: carried }); + let free = !cx.w.others.iter().any(|s| { + s.bounds.intersects(Aabb::new( + Vec3::new(f64::from(x), f64::from(y), f64::from(z)), + Vec3::new(f64::from(x + 1), f64::from(y + 1), f64::from(z + 1)), + )) + }); + if cx.w.facts.is_air(target) + && !cx.w.facts.is_air(below) + && &*bf.name != "minecraft:bedrock" + && bf.full_collision + && free + { + cx.events.push(MobEvent::SetBlock { + pos: [x, y, z], + state: carried, + }); m.extra.carried = None; } } diff --git a/crates/horizon-client/src/mobs/living.rs b/crates/horizon-client/src/mobs/living.rs index 090005cb9..327b0662b 100644 --- a/crates/horizon-client/src/mobs/living.rs +++ b/crates/horizon-client/src/mobs/living.rs @@ -410,6 +410,13 @@ impl Mob { self.velocity.y += if self.ai.nav.can_float { 0.04 } else { 0.3 }; } pub fn jump_from_ground(&mut self) { + if matches!( + self.species(), + super::Species::Slime | super::Species::MagmaCube + ) { + super::cube::jump(self); + return; + } let power = self.jump_power(); if power > 1.0e-5 { self.velocity.y = f64::from(power).max(self.velocity.y); diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index c5ba0de55..8a408bd01 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -254,6 +254,8 @@ pub fn layers(m: &Mob) -> Vec { }) }; match m.species() { + // SlimeOuterLayer: the translucent outer cube. + S::Slime => push(&mut v, "slime#outer", "entity/slime/slime", white, false), S::Sheep if !m.extra.sheared => { let color = abgr(sheep_wool_rgb(m.extra.color)); if !m.is_baby() && m.extra.color != 0 { @@ -471,6 +473,7 @@ fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 { let y = (1.0 + s4 * 0.1) / wobble; glam::Mat4::from_scale(glam::Vec3::new(xz, y, xz)) } + Species::Slime | Species::MagmaCube => super::cube::scale(m, alpha), _ => glam::Mat4::IDENTITY, } } diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index df47f0ac5..9c7cda3d2 100644 --- a/crates/horizon-client/src/mobs/spawning.rs +++ b/crates/horizon-client/src/mobs/spawning.rs @@ -204,6 +204,7 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts) { m.extra.variant = villager_type(biome); } S::TropicalFish => super::aquatic::finalize(m), + S::Slime | S::MagmaCube => super::cube::finalize(m), _ => {} } } diff --git a/crates/horizon-client/src/mobs/witch.rs b/crates/horizon-client/src/mobs/witch.rs index eee488303..59b4ecff7 100644 --- a/crates/horizon-client/src/mobs/witch.rs +++ b/crates/horizon-client/src/mobs/witch.rs @@ -3,7 +3,10 @@ use super::{ Mob, MobEvent, Species, Who, anim::{Poses, State}, - goals::{Cx, Float, Goal, HurtByTarget, LOOK, LookAtPlayer, MOVE, NearestTarget, RandomLookAround, Stroll, goal_clone}, + goals::{ + Cx, Float, Goal, HurtByTarget, LOOK, LookAtPlayer, MOVE, NearestTarget, RandomLookAround, + Stroll, goal_clone, + }, }; use crate::projectiles::{Projectile, ProjectileKind}; use horizon_core::{geometry::Vec3, random::RandomSource}; @@ -16,7 +19,8 @@ pub fn register(m: &mut Mob) { g.add(3, LookAtPlayer::new(8.0)); g.add(3, RandomLookAround::default()); m.ai.targets.add(1, HurtByTarget::new()); - m.ai.targets.add(3, NearestTarget::player(true).interval(10)); + m.ai.targets + .add(3, NearestTarget::player(true).interval(10)); } /// The callback a ranged mob uses to fire (`RangedAttackMob.performRangedAttack`). pub type Fire = fn(&mut Mob, Who, f32, &mut Cx); @@ -34,7 +38,15 @@ pub struct RangedAttack { } impl RangedAttack { pub fn new(speed: f64, min: i32, max: i32, radius: f32, fire: Fire) -> Self { - Self { speed, min, max, radius, fire, attack_time: -1, see_time: 0 } + Self { + speed, + min, + max, + radius, + fire, + attack_time: -1, + see_time: 0, + } } } impl Goal for RangedAttack { @@ -57,7 +69,9 @@ impl Goal for RangedAttack { } fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { let Some(who) = m.ai.target else { return }; - let Some(t) = cx.w.target_info(who) else { return }; + let Some(t) = cx.w.target_info(who) else { + return; + }; let d2 = m.distance_sqr(t.position); let sees = m.sees(cx.w, who); if sees { @@ -68,7 +82,12 @@ impl Goal for RangedAttack { if d2 <= f64::from(self.radius * self.radius) && self.see_time >= 5 { m.nav_stop(); } else { - m.nav_move_to_entity(cx.w, [t.position.x, t.position.y, t.position.z], 1, self.speed); + m.nav_move_to_entity( + cx.w, + [t.position.x, t.position.y, t.position.z], + 1, + self.speed, + ); } m.look_at_speed([t.position.x, t.eye_y, t.position.z], 30.0, 30.0); self.attack_time -= 1; @@ -79,10 +98,12 @@ impl Goal for RangedAttack { let dist = (d2.sqrt() as f32) / self.radius; let power = dist.clamp(0.1, 1.0); (self.fire)(m, who, power, cx); - self.attack_time = (dist * (self.max - self.min) as f32 + self.min as f32).floor() as i32; + self.attack_time = + (dist * (self.max - self.min) as f32 + self.min as f32).floor() as i32; } else if self.attack_time < 0 { let f = d2.sqrt() / f64::from(self.radius); - self.attack_time = (f64::from(self.min) + f * f64::from(self.max - self.min)).floor() as i32; + self.attack_time = + (f64::from(self.min) + f * f64::from(self.max - self.min)).floor() as i32; } } } @@ -92,12 +113,22 @@ fn witch_attack(m: &mut Mob, who: Who, _power: f32, cx: &mut Cx) { if m.extra.using_item > 0 { return; } - let Some(t) = cx.w.target_info(who) else { return }; + let Some(t) = cx.w.target_info(who) else { + return; + }; let xd = t.position.x - m.position.x; let yd = t.eye_y - 1.1 - m.position.y; let zd = t.position.z - m.position.z; let dist = (xd * xd + zd * zd).sqrt(); - let health = if t.is_player { 20.0 } else { cx.w.seen(match who { Who::Mob(id) => id, Who::Player => 0 }).map_or(20.0, |s| s.health) }; + let health = if t.is_player { + 20.0 + } else { + cx.w.seen(match who { + Who::Mob(id) => id, + Who::Player => 0, + }) + .map_or(20.0, |s| s.health) + }; let (effect, ticks, amplifier) = if dist >= 8.0 { ("minecraft:slowness", 1800, 0) } else if health >= 8.0 { @@ -108,12 +139,31 @@ fn witch_attack(m: &mut Mob, who: Who, _power: f32, cx: &mut Cx) { ("minecraft:instant_damage", 1, 0) }; let start = Vec3::new(m.position.x, m.eye_y() - 0.1, m.position.z); - let mut p = Projectile::new(ProjectileKind::Potion { effect, ticks, amplifier, color: 0 }, start, Some(Who::Mob(m.id))); + let mut p = Projectile::new( + ProjectileKind::Potion { + effect, + ticks, + amplifier, + color: 0, + }, + start, + Some(Who::Mob(m.id)), + ); // Splash potions throw with gravity 0.05. - p.shoot(&mut m.random, [xd, yd + dist * 0.2, zd], if dist <= 2.0 { 0.45 } else { 0.75 }, 8.0); + p.shoot( + &mut m.random, + [xd, yd + dist * 0.2, zd], + if dist <= 2.0 { 0.45 } else { 0.75 }, + 8.0, + ); cx.events.push(MobEvent::Projectile(p)); let pitch = 0.8 + m.random.next_float() * 0.4; - cx.events.push(MobEvent::Sound("entity.witch.throw", m.center(), 1.0, pitch)); + cx.events.push(MobEvent::Sound( + "entity.witch.throw", + m.center(), + 1.0, + pitch, + )); } /// Witch.aiStep: drink water breathing, fire resistance, healing or swiftness. pub fn ai_step(m: &mut Mob, cx_events: &mut Vec, target_far: bool) { @@ -126,8 +176,12 @@ pub fn ai_step(m: &mut Mob, cx_events: &mut Vec, target_far: bool) { m.extra.using_item = 0; if let Some(potion) = m.extra.main_hand.take() { match potion.as_ref() { - "minecraft:water_breathing" => m.add_effect("minecraft:water_breathing", 3600, 0), - "minecraft:fire_resistance" => m.add_effect("minecraft:fire_resistance", 3600, 0), + "minecraft:water_breathing" => { + m.add_effect("minecraft:water_breathing", 3600, 0) + } + "minecraft:fire_resistance" => { + m.add_effect("minecraft:fire_resistance", 3600, 0) + } "minecraft:healing" => m.health = (m.health + 4.0).min(m.attrs.max_health), "minecraft:swiftness" => m.add_effect("minecraft:speed", 3600, 0), _ => {} @@ -137,13 +191,23 @@ pub fn ai_step(m: &mut Mob, cx_events: &mut Vec, target_far: bool) { } return; } - let potion = if m.random.next_float() < 0.15 && m.fluids.eye_in_water && !m.has_effect("minecraft:water_breathing") { + let potion = if m.random.next_float() < 0.15 + && m.fluids.eye_in_water + && !m.has_effect("minecraft:water_breathing") + { Some("minecraft:water_breathing") - } else if m.random.next_float() < 0.15 && (m.fire_ticks > 0 || m.last_damage.is_some_and(|(d, _)| d.is_fire())) && !m.has_effect("minecraft:fire_resistance") { + } else if m.random.next_float() < 0.15 + && (m.fire_ticks > 0 || m.last_damage.is_some_and(|(d, _)| d.is_fire())) + && !m.has_effect("minecraft:fire_resistance") + { Some("minecraft:fire_resistance") } else if m.random.next_float() < 0.05 && m.health < m.attrs.max_health { Some("minecraft:healing") - } else if m.random.next_float() < 0.5 && m.ai.target.is_some() && !m.has_effect("minecraft:speed") && target_far { + } else if m.random.next_float() < 0.5 + && m.ai.target.is_some() + && !m.has_effect("minecraft:speed") + && target_far + { Some("minecraft:swiftness") } else { None @@ -154,7 +218,12 @@ pub fn ai_step(m: &mut Mob, cx_events: &mut Vec, target_far: bool) { m.extra.using_item = 32; m.extra.flag = true; let pitch = 0.8 + m.random.next_float() * 0.4; - cx_events.push(MobEvent::Sound("entity.witch.drink", m.center(), 1.0, pitch)); + cx_events.push(MobEvent::Sound( + "entity.witch.drink", + m.center(), + 1.0, + pitch, + )); } } /// WitchModel.setupAnim (VillagerModel base with a wobbling nose). @@ -162,10 +231,19 @@ 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()); p.xrot("right_leg", (s.pos * 0.6662).cos() * 1.4 * s.speed * 0.5); - p.xrot("left_leg", (s.pos * 0.6662 + std::f32::consts::PI).cos() * 1.4 * s.speed * 0.5); + p.xrot( + "left_leg", + (s.pos * 0.6662 + std::f32::consts::PI).cos() * 1.4 * s.speed * 0.5, + ); let speed = 0.01 * (m.id % 10) as f32; - p.xrot("nose", (s.age * speed).sin() * 4.5 * std::f32::consts::PI / 180.0); - p.zrot("nose", (s.age * speed).cos() * 2.5 * std::f32::consts::PI / 180.0); + p.xrot( + "nose", + (s.age * speed).sin() * 4.5 * std::f32::consts::PI / 180.0, + ); + p.zrot( + "nose", + (s.age * speed).cos() * 2.5 * std::f32::consts::PI / 180.0, + ); if m.extra.main_hand.is_some() { let n = p.get("nose"); n[0] = 0.0;