diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index a729034bb..b93e8f6b5 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -378,7 +378,15 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { for (slot, item) in [ ("mainhand", &m.extra.main_hand), ("offhand", &m.extra.off_hand), - ("head", &m.extra.head), + ( + // A happy ghast wears its harness in the body slot. + if m.species() == crate::mobs::Species::HappyGhast { + "body" + } else { + "head" + }, + &m.extra.head, + ), ("chest", &m.extra.chest), ("legs", &m.extra.legs), ("feet", &m.extra.feet), @@ -444,7 +452,11 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - .map(Into::into); if !crate::mobs::equine::is_equine(m.species()) { let slot = |key| eq.get(key).and_then(|i| text(i.get("id"))).map(Into::into); - m.extra.head = slot("head"); + m.extra.head = slot(if m.species() == crate::mobs::Species::HappyGhast { + "body" + } else { + "head" + }); m.extra.chest = slot("chest"); m.extra.legs = slot("legs"); m.extra.feet = slot("feet"); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 8417ee935..8de66667b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -26,6 +26,7 @@ mod fishing; mod fov; mod frames; mod game_mode; +mod ghasts; mod growth; mod jukebox; mod leads; @@ -1370,6 +1371,11 @@ impl Game<'_> { kind: "hatch_sniffer", .. } => self.hatch_sniffer([pos.x, pos.y, pos.z]), + LevelEvent::BlockEvent { + pos, + state, + kind: "spawn_ghastling", + } => self.spawn_ghastling([pos.x, pos.y, pos.z], state), LevelEvent::BlockEvent { pos, state, diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index c6c6b3df5..c718b057e 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -702,6 +702,11 @@ impl Game<'_> { if m.species() == Species::Wither { gear.push("minecraft:nether_star"); } + if species_drops_harness(m) + && let Some(h) = m.extra.head.as_deref() + { + gear.push(h); + } if m.extra.chested && horizon_client::mobs::equine::is_equine(m.species()) { gear.push("minecraft:chest"); } @@ -2011,7 +2016,11 @@ impl Game<'_> { sound = Some("item.flintandsteel.use"); self.damage_held_tool(); } - _ if is_food && m.kind.flags.animal => { + // HappyGhast.canFallInLove: never, so only a ghastling takes food. + _ if is_food + && m.kind.flags.animal + && (species != Species::HappyGhast || m.is_baby()) => + { // Animal.mobInteract: breed adults, speed up babies. if m.extra.age == 0 && m.extra.in_love <= 0 { m.extra.in_love = 600; @@ -2393,6 +2402,9 @@ impl Game<'_> { } } } +fn species_drops_harness(m: &Mob) -> bool { + m.species() == Species::HappyGhast +} fn eat_sound(s: Species) -> &'static str { match s { Species::Pig => "entity.pig.eat", diff --git a/crates/horizon-client/src/game/ghasts.rs b/crates/horizon-client/src/game/ghasts.rs new file mode 100644 index 000000000..c324ecfef --- /dev/null +++ b/crates/horizon-client/src/game/ghasts.rs @@ -0,0 +1,111 @@ +//! Happy ghasts on the game side: putting a harness on (and shearing it +//! off), riding, getting off on top, and the ghastling a dried ghast block +//! hatches. +use super::Game; +use horizon_client::{ + items::ItemStack, + mobs::{MobEvent, happy_ghast}, +}; +use horizon_core::geometry::Vec3; +use horizon_world::registry::BlockStateId; + +impl Game<'_> { + /// `HappyGhast.mobInteract`. Returns whether the click was used. + pub(super) fn interact_happy_ghast(&mut self, index: usize, item: &str) -> bool { + let m = &self.mobs[index]; + if !m.alive() || Some(m.id) == self.riding { + return false; + } + if m.is_baby() { + // Animal.mobInteract: food grows it up. + return false; + } + let at = m.center(); + let sneaking = self.keys.contains(&winit::keyboard::KeyCode::ShiftLeft); + let harnessed = m.extra.head.is_some(); + // Equippable.equipOnInteract: a harness goes on a bare adult. + if item.ends_with("_harness") && item.starts_with("minecraft:") && !harnessed { + self.mobs[index].extra.head = Some(item.into()); + self.audio + .play("minecraft:entity.happy_ghast.equip", Some(at), 1.0, 1.0); + self.consume_held(); + return true; + } + // Equippable.canBeSheared: shears take the harness off again. + if item == "minecraft:shears" + && harnessed + && !sneaking + && let Some(harness) = self.mobs[index].extra.head.take() + { + self.audio + .play("minecraft:entity.happy_ghast.unequip", Some(at), 1.0, 1.0); + self.spawn_item(ItemStack::new(&harness, 1), at, [0.0, 0.2, 0.0], 10); + self.damage_held_tool(); + return true; + } + if harnessed && !sneaking { + // doPlayerRide. + let id = self.mobs[index].id; + self.break_leash(id, true, false); + let was_empty = self.mobs[index].rider.is_none(); + self.yaw = self.mobs[index].yaw; + self.riding = Some(id); + self.mount.reset_jump(); + if was_empty { + let down = happy_ghast::goggles(true, &self.mobs[index]); + let _ = self.handle_mob_events(vec![down]); + } + self.trigger_player("minecraft:started_riding"); + return true; + } + false + } + /// Getting off lands on top of it. + pub(super) fn dismount_happy_ghast(&mut self, id: u32) { + let Some(m) = self.mobs.iter().find(|m| m.id == id) else { + return; + }; + let top = Vec3::new( + m.position.x, + m.position.y + f64::from(m.height()), + m.position.z, + ); + let up: MobEvent = happy_ghast::goggles(false, m); + if let Some(p) = self.player.as_mut() { + p.position = top; + p.velocity = Vec3::ZERO; + } + let _ = self.handle_mob_events(vec![up]); + } + /// `DriedGhastBlock.spawnGhastling`: a ghastling facing the way the block did. + pub(super) fn spawn_ghastling(&mut self, pos: [i32; 3], state: BlockStateId) { + let at = [ + f64::from(pos[0]) + 0.5, + f64::from(pos[1]), + f64::from(pos[2]) + 0.5, + ]; + let facing = self + .res + .assets + .registry + .state(state) + .and_then(|s| s.properties.get("facing").cloned()); + let yaw = match facing.as_deref() { + Some("north") => 180.0, + Some("east") => 270.0, + Some("west") => 90.0, + _ => 0.0, + }; + if let Some(id) = self.spawn_mob("minecraft:happy_ghast", at, true) + && let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) + { + m.extra.age = -24000; + m.yaw = yaw; + m.body_yaw = yaw; + m.head_yaw = yaw; + let at = m.center(); + self.audio + .play("minecraft:entity.ghastling.spawn", Some(at), 1.0, 1.0); + } + } +} diff --git a/crates/horizon-client/src/game/mounts.rs b/crates/horizon-client/src/game/mounts.rs index f46e81b0c..43b51d398 100644 --- a/crates/horizon-client/src/game/mounts.rs +++ b/crates/horizon-client/src/game/mounts.rs @@ -106,6 +106,9 @@ impl Game<'_> { if horizon_client::mobs::llama::is_llama(species) { return self.interact_llama(index, item); } + if species == Species::HappyGhast { + return self.interact_happy_ghast(index, item); + } if !equine::is_equine(species) || !m.alive() || Some(m.id) == self.riding { return false; } @@ -290,6 +293,7 @@ impl Game<'_> { jump: if jump > 0.0 { jump } else { pending }, steer, sprint: self.keys.contains(&winit::keyboard::KeyCode::ControlLeft), + jumping: keys.jump, }); true } @@ -316,6 +320,15 @@ impl Game<'_> { m.position.z + f64::from(0.5 * scale * yaw.cos()), ); } + if m.species() == Species::HappyGhast { + // The first passenger attachment (0, 4, 1.7) of the harnessed back. + let yaw = f64::from(m.yaw.to_radians()); + at = Vec3::new( + m.position.x - 1.7 * yaw.sin(), + m.position.y + 4.0 - 0.6, + m.position.z + 1.7 * yaw.cos(), + ); + } if horizon_client::mobs::llama::is_llama(m.species()) { // The passenger attachment (0, 1.37, -0.3), half of it for a baby. let (y, z) = if m.is_baby() { @@ -346,6 +359,11 @@ impl Game<'_> { return; }; m.rider = None; + if m.species() == Species::HappyGhast { + let id = m.id; + self.dismount_happy_ghast(id); + return; + } let yaw = m.yaw.to_radians(); let side = Vec3::new(f64::from(yaw.cos()), 0.0, f64::from(yaw.sin())); let w = f64::from(m.width()) / 2.0 + 0.4; diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 9ccd4d3fa..f6d277f6c 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -22,6 +22,7 @@ pub mod farm; pub mod goals; pub mod golem; pub mod guardian; +pub mod happy_ghast; pub mod hostile; pub mod illager; pub mod keyframes; diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 0fdee8538..1816e86a8 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -90,6 +90,7 @@ pub enum Family { Trader, Breeze, Sniffer, + HappyGhast, } pub fn family(s: Species) -> Family { use Species as S; @@ -128,6 +129,7 @@ pub fn family(s: Species) -> Family { S::WanderingTrader => Family::Trader, S::Breeze => Family::Breeze, S::Sniffer => Family::Sniffer, + S::HappyGhast => Family::HappyGhast, S::Piglin | S::PiglinBrute | S::Hoglin | S::Zoglin => Family::Piglin, S::Horse | S::Donkey @@ -183,6 +185,7 @@ pub fn register(m: &mut Mob) { Family::Trader => super::trader::register(m), Family::Breeze => super::breeze::register(m), Family::Sniffer => super::sniffer::register(m), + Family::HappyGhast => super::happy_ghast::register(m), } } /// Mob.tick additions that run before the entity tick (creeper fuse, puffing). @@ -205,6 +208,9 @@ pub fn custom_move_control(m: &mut Mob, w: &MobWorld, events: &mut Vec if m.species() == Species::Ghast { return super::nether::ghast_move_control(m, w); } + if m.species() == Species::HappyGhast { + return super::happy_ghast::move_control(m, w); + } if m.species() == Species::Phantom { return super::phantom::move_control(m); } @@ -408,6 +414,17 @@ fn play_ambient(m: &mut Mob, w: &MobWorld, events: &mut Vec) { 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::Allay if m.extra.main_hand.is_some() => "entity.allay.ambient_with_item", + // HappyGhast: its own voice, never pitched up. + Species::HappyGhast => { + let sound = super::happy_ghast::sounds(m)[0]; + events.push(MobEvent::Sound( + sound, + m.center(), + super::happy_ghast::volume(m), + 1.0, + )); + return; + } // Sniffer.getAmbientSound. Species::Sniffer => match super::sniffer::ambient(m) { Some(s) => s, @@ -593,6 +610,7 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { Species::Allay => super::allay::ai_step(m), Species::Breeze => super::breeze::ai_step(m, events), Species::Sniffer => super::sniffer::ai_step(m, events), + Species::HappyGhast => super::happy_ghast::ai_step(m, w), Species::Villager => super::villager::ai_step(m, w), Species::WanderingTrader => { super::villager::ai_step(m, w); @@ -658,6 +676,7 @@ pub fn speed_bonus(m: &Mob) -> f64 { pub fn sound_volume(m: &Mob) -> f32 { match m.species() { Species::Ghast => 5.0, + Species::HappyGhast => super::happy_ghast::volume(m), Species::Slime | Species::MagmaCube => 0.4 * m.extra.size.max(1) as f32, Species::Cod | Species::Salmon diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 0c1c3a116..784853a45 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 { + HappyGhast, Sniffer, Breeze, Allay, @@ -349,6 +350,7 @@ pub fn setup( Anim::Camel => super::camel::anim(m, s, p, w_anims), Anim::Breeze => super::breeze::anim(m, s, p, w_anims), Anim::Sniffer => super::sniffer::anim(m, s, p, w_anims), + Anim::HappyGhast => super::happy_ghast::anim(m, s, p), Anim::Llama => super::llama::anim(m, s, p), Anim::Allay => super::allay::anim(m, s, p), Anim::Squid | Anim::Fish | Anim::Pufferfish | Anim::Dolphin => { diff --git a/crates/horizon-client/src/mobs/equine.rs b/crates/horizon-client/src/mobs/equine.rs index e87b1c0c0..67500509e 100644 --- a/crates/horizon-client/src/mobs/equine.rs +++ b/crates/horizon-client/src/mobs/equine.rs @@ -41,6 +41,8 @@ pub struct Rider { pub steer: bool, /// Sprinting (camels run faster). pub sprint: bool, + /// Holding the jump key (a happy ghast rises). + pub jumping: bool, } pub fn is_equine(s: Species) -> bool { @@ -155,6 +157,7 @@ pub fn controlled(m: &Mob) -> bool { match m.species() { // Pig/Strider.getControllingPassenger: saddled, the rider holding the stick. Species::Pig | Species::Strider => m.extra.saddled && r.steer, + Species::HappyGhast => super::happy_ghast::controlled(m), _ => m.extra.saddled || m.species() == Species::SkeletonHorse && m.extra.tame, } } @@ -306,6 +309,9 @@ pub fn ridden_step(m: &mut Mob) { m.body_yaw = r.yaw; m.head_yaw = r.yaw; m.ai.nav.stop(); + if m.species() == Species::HappyGhast { + return super::happy_ghast::ridden(m); + } if matches!(m.species(), Species::Pig | Species::Strider) { // Pig/Strider: always forward along the rider's look, at a fraction // of the movement speed, boosted by the stick. diff --git a/crates/horizon-client/src/mobs/happy_ghast.rs b/crates/horizon-client/src/mobs/happy_ghast.rs new file mode 100644 index 000000000..a2da803f2 --- /dev/null +++ b/crates/horizon-client/src/mobs/happy_ghast.rs @@ -0,0 +1,470 @@ +//! Happy ghasts and ghastlings (`HappyGhast`, `HappyGhastAi`): the adult +//! floats about following torchlight of snowballs and harnesses, the +//! ghastling follows players and adults, a harnessed adult can be ridden and +//! flown, healing is slow (fast in the rain) and `HappyGhastModel`. +use super::{ + Mob, MobEvent, MobWorld, Species, + anim::{Poses, State}, + control::{MoveKind, Operation}, + goals::{Cx, Float, Goal, LOOK, MOVE, Panic, Stroll, Tempt, goal_clone}, + nav::NavKind, +}; +use horizon_core::{ + geometry::{Aabb, Vec3}, + random::RandomSource, +}; + +// Happy ghast state: `head` the harness, `scratch[0]` 1 once set up as a +// baby and 2 as an adult, `home` and `ints[4]` the restriction, `counter` +// the end tick of the follow-an-adult range pick. +pub const BABY_SCALE: f64 = 0.2375; +/// `Attributes.FLYING_SPEED` and the speed `travel` uses (`* 5 / 3`). +pub const FLYING_SPEED: f64 = 0.05; +pub const AIR_SPEED: f32 = 0.05 * 5.0 / 3.0; + +pub fn register(m: &mut Mob) { + setup(m, m.is_baby()); +} +/// `adultGhastSetup` and `babyGhastSetup`. +fn setup(m: &mut Mob, baby: bool) { + m.extra.scratch[0] = if baby { 1 } else { 2 }; + m.ai.goals.clear(); + m.ai.nav.stop(); + m.ai.mov.operation = Operation::Wait; + if baby { + m.ai.mov.kind = MoveKind::Flying { + max_turn: 180.0, + hovers: true, + }; + m.ai.nav.kind = NavKind::Flying; + m.ai.nav.can_float = true; + m.ai.nav.required_path_length = 48.0; + let tempt = m.kind.food.clone(); + let g = &mut m.ai.goals; + g.add(0, Float); + g.add(0, Panic::new(2.0)); + let mut t = Tempt::new(1.25, tempt, false); + t.stop_distance = 3.0; + g.add(1, t); + g.add(2, FollowAdult::default()); + g.add(4, Stroll::flying(1.0)); + return; + } + m.ai.mov.kind = MoveKind::Custom; + m.ai.nav.kind = NavKind::Ground; + let g = &mut m.ai.goals; + g.add(3, Float); + g.add(4, GhastTempt::default()); + g.add(5, FloatAround { blocks: 16 }); +} +fn wearing_harness(m: &Mob) -> bool { + m.extra.head.is_some() +} +/// `getControllingPassenger`: a player on a harnessed adult. +pub fn controlled(m: &Mob) -> bool { + m.rider.is_some() && wearing_harness(m) && !m.is_baby() +} +/// `getHappyGhastRestrictionRadius`. +fn restriction_radius(m: &Mob) -> i32 { + if !m.is_baby() && m.extra.head.is_none() { + 64 + } else { + 32 + } +} +/// `Mob.isWithinHome` for the ghast's restriction. +fn within_home(m: &Mob, p: Vec3) -> bool { + let Some(h) = m.extra.home else { return true }; + let r = f64::from(m.extra.ints[4]); + let (dx, dy, dz) = ( + f64::from(h[0]) + 0.5 - p.x, + f64::from(h[1]) + 0.5 - p.y, + f64::from(h[2]) + 0.5 - p.z, + ); + dx * dx + dy * dy + dz * dz < r * r +} +/// `Ghast.GhastMoveControl` with the careful block checks: a push toward +/// the wanted point every few ticks while the way is clear. +pub fn move_control(m: &mut Mob, w: &MobWorld) { + if m.ai.mov.operation != Operation::MoveTo { + return; + } + m.extra.counter2 -= 1; + if m.extra.counter2 > 0 { + return; + } + m.extra.counter2 += m.random.next_int_bound(5).unwrap_or(0) + 2; + let [wx, wy, wz] = m.ai.mov.wanted; + let travel = Vec3::new(wx - m.position.x, wy - m.position.y, wz - m.position.z); + let len = travel.length(); + if len < 1.0e-6 { + return; + } + let b = m.bounds(); + let steps = len.ceil() as i32; + let clear = (1..=steps).all(|i| { + let f = f64::from(i) / f64::from(steps); + let d = Vec3::new(travel.x * f, travel.y * f, travel.z * f); + let at = Aabb::new( + b.min_position().add_vector(d), + b.max_position().add_vector(d), + ); + w.no_collision(at) && !touches_fluid(w, at) + }); + if clear { + let speed = AIR_SPEED; + m.velocity = m.velocity.add_vector(Vec3::new( + travel.x / len * f64::from(speed), + travel.y / len * f64::from(speed), + travel.z / len * f64::from(speed), + )); + } else { + m.ai.mov.operation = Operation::Wait; + } +} +/// A careful ghast does not float into water or lava. +fn touches_fluid(w: &MobWorld, b: Aabb) -> bool { + let (lo, hi) = (b.min_position(), b.max_position()); + 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 { + if w.fluid(x, y, z) != crate::blocks::Fluid::None { + return true; + } + } + } + } + false +} +/// `TemptGoal.ForNonPathfinders`: drift toward a player holding food, or +/// food and harnesses for a bare adult. +#[derive(Clone, Debug, Default)] +struct GhastTempt { + calm_down: i32, +} +impl GhastTempt { + fn wants(m: &Mob, item: &Option>) -> bool { + let Some(i) = item else { return false }; + let harness = !wearing_harness(m) && !m.is_baby() && i.ends_with("_harness"); + harness || m.kind.food.iter().any(|f| f.as_str() == &**i) + } +} +impl Goal for GhastTempt { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE | LOOK + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if self.calm_down > 0 { + self.calm_down -= 1; + return false; + } + let Some(p) = &cx.w.player else { return false }; + // Attributes.TEMPT_RANGE 16. + p.alive + && !p.creative + && (Self::wants(m, &p.main_hand) || Self::wants(m, &p.off_hand)) + && m.distance_sqr(p.position) <= 256.0 + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.ai.mov.operation = Operation::Wait; + self.calm_down = 50; + } + fn every_tick(&self) -> bool { + true + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + let Some(p) = cx.w.player.clone() else { return }; + m.look_at(p.eye); + if m.distance_sqr(p.position) < 49.0 { + m.ai.mov.operation = Operation::Wait; + } else { + let f = m.random.next_double(); + let at = [ + (p.eye[0] - m.position.x) * f + m.position.x, + (p.eye[1] - m.position.y) * f + m.position.y, + (p.eye[2] - m.position.z) * f + m.position.z, + ]; + m.ai.mov.set_wanted(at[0], at[1], at[2], 1.0); + } + } +} +/// `Ghast.RandomFloatAroundGoal` with a restriction and a wish for open +/// air next to something solid. +#[derive(Clone, Debug)] +struct FloatAround { + blocks: i32, +} +impl FloatAround { + fn good(&self, w: &MobWorld, p: Vec3) -> bool { + if self.blocks <= 0 { + return true; + } + let at = [p.x.floor() as i32, p.y.floor() as i32, p.z.floor() as i32]; + if !w.facts.is_air(w.world.block(at[0], at[1], at[2])) { + return false; + } + for d in [ + [1, 0, 0], + [-1, 0, 0], + [0, 1, 0], + [0, -1, 0], + [0, 0, 1], + [0, 0, -1], + ] { + for i in 1..self.blocks { + if !w.facts.is_air(w.world.block( + at[0] + d[0] * i, + at[1] + d[1] * i, + at[2] + d[2] * i, + )) { + return true; + } + } + } + false + } +} +impl Goal for FloatAround { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE + } + fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool { + if !m.ai.mov.has_wanted() { + return true; + } + let [x, y, z] = m.ai.mov.wanted; + let d = + (x - m.position.x).powi(2) + (y - m.position.y).powi(2) + (z - m.position.z).powi(2); + !(1.0..=3600.0).contains(&d) + } + fn can_continue(&mut self, _: &mut Mob, _: &mut Cx) -> bool { + false + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + let c = m.position; + let mut result: Option = None; + for _ in 0..64 { + let r = &mut m.random; + let t = Vec3::new( + c.x + f64::from(r.next_float() * 2.0 - 1.0) * 16.0, + c.y + f64::from(r.next_float() * 2.0 - 1.0) * 16.0, + c.z + f64::from(r.next_float() * 2.0 - 1.0) * 16.0, + ); + result = Some(t); + if within_home(m, t) && self.good(cx.w, t) { + break; + } + result = None; + } + let mut t = result.unwrap_or_else(|| { + let r = &mut m.random; + Vec3::new( + c.x + f64::from(r.next_float() * 2.0 - 1.0) * 16.0, + c.y + f64::from(r.next_float() * 2.0 - 1.0) * 16.0, + c.z + f64::from(r.next_float() * 2.0 - 1.0) * 16.0, + ) + }); + let top = + cx.w.motion_blocking_height(t.x.floor() as i32, t.z.floor() as i32); + if top < t.y.floor() as i32 && top > crate::chunk::MIN_Y { + t.y = c.y - (c.y - t.y).abs(); + } + m.ai.mov.set_wanted(t.x, t.y, t.z, 1.0); + } +} +/// `BabyFollowAdult`: a ghastling drifts toward the nearest player or adult +/// until it is a few blocks away. +#[derive(Clone, Debug, Default)] +struct FollowAdult { + close: f64, +} +impl FollowAdult { + fn target(m: &Mob, cx: &Cx) -> Option { + let player = + cx.w.player + .as_ref() + .filter(|p| p.alive && !p.spectator && m.distance_sqr(p.position) <= 32.0 * 32.0) + .map(|p| p.position); + player.or_else(|| { + cx.w.nearby(m.position, 32.0, |s| { + s.species == Species::HappyGhast && !s.baby + }) + .next() + .map(|s| s.position) + }) + } +} +impl Goal for FollowAdult { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + m.is_baby() + && Self::target(m, cx).is_some_and(|t| { + let close = if self.close > 0.0 { self.close } else { 16.0 }; + m.position.distance_squared(t) > close * close + }) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + Self::target(m, cx).is_some_and(|t| m.position.distance_squared(t) > 9.0) + } + fn start(&mut self, m: &mut Mob, _: &mut Cx) { + // UniformInt.of(3, 16). + self.close = f64::from(3 + m.random.next_int_bound(14).unwrap_or(0)); + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + if let Some(t) = Self::target(m, cx) { + m.nav_move_to(cx.w, [t.x, t.y, t.z], 1.1); + } + } +} +/// What a rider asks (`getRiddenInput`, `tickRidden`). +pub fn ridden(m: &mut Mob) { + let Some(r) = m.rider else { return }; + // tickRidden: turn 8% of the way toward the rider's yaw; pitch half of theirs. + let diff = super::living::wrap(r.yaw - m.yaw); + m.yaw += diff * 0.08; + m.head_pitch = r.pitch * 0.5; + m.body_yaw = m.yaw; + m.head_yaw = m.yaw; + m.ai.nav.stop(); + let (mut forward, mut up) = (0.0, 0.0); + if r.forward != 0.0 { + let mut f = r.pitch.to_radians().cos(); + let mut u = -r.pitch.to_radians().sin(); + if r.forward < 0.0 { + f *= -0.5; + u *= -0.5; + } + forward = f; + up = u; + } + if r.jumping { + up += 0.5; + } + let scale = 3.9 * FLYING_SPEED as f32; + m.ai.xxa = r.strafe * scale; + m.ai.yya = up * scale; + m.ai.zza = forward * scale; + m.ai.mov.operation = Operation::Wait; +} +/// `HappyGhast.aiStep`, `tick` and `checkRestriction`: the adult/baby +/// setup at the age boundary, the home, face-the-motion and slow healing. +pub fn ai_step(m: &mut Mob, w: &MobWorld) { + let baby = m.is_baby(); + if m.extra.scratch[0] != if baby { 1 } else { 2 } { + setup(m, baby); + } + // checkRestriction. + if m.extra.leash.is_none() && m.rider.is_none() { + let radius = restriction_radius(m); + let here = [ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ]; + let stale = match m.extra.home { + Some(h) => { + (h[0] - here[0]).pow(2) + (h[1] - here[1]).pow(2) + (h[2] - here[2]).pow(2) + >= (radius + 16).pow(2) + || radius != m.extra.ints[4] + } + None => true, + }; + if stale { + m.extra.home = Some(here); + m.extra.ints[4] = radius; + } + } + // LookControl: face where it is going. + if !baby && m.rider.is_none() { + let v = m.velocity; + if m.ai.target.is_none() && v.x * v.x + v.z * v.z > 1.0e-6 { + m.yaw = -(v.x.atan2(v.z) as f32).to_degrees(); + m.body_yaw = m.yaw; + m.head_yaw = m.yaw; + } + } + // continuousHeal: a point every 30 seconds, every second in rain. + if m.alive() && m.death_time == 0 && m.health < m.attrs.max_health { + let (x, y, z) = ( + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ); + let wet = w.raining && w.can_see_sky(x, y, z); + let interval = if wet { 20 } else { 600 }; + if m.age.is_multiple_of(interval) { + m.health = (m.health + 1.0).min(m.attrs.max_health); + } + } +} +/// Sounds: the ghastling has its own. +pub fn sounds(m: &Mob) -> [&'static str; 3] { + if m.is_baby() { + [ + "entity.ghastling.ambient", + "entity.ghastling.hurt", + "entity.ghastling.death", + ] + } else { + [ + "entity.happy_ghast.ambient", + "entity.happy_ghast.hurt", + "entity.happy_ghast.death", + ] + } +} +/// `getSoundVolume`: 4 for adults, 1 for ghastlings. +pub fn volume(m: &Mob) -> f32 { + if m.is_baby() { 1.0 } else { 4.0 } +} +/// `HappyGhastModel.setupAnim`: the tentacles, and the body squeezed under a harness. +pub fn anim(m: &Mob, s: &State, p: &mut Poses) { + if wearing_harness(m) { + let b = p.get("body"); + b[6] = 0.9375; + b[7] = 0.9375; + b[8] = 0.9375; + } + super::nether::ghast_anim(s, p); +} +/// Texture of the harness layer for the worn item. +pub fn harness_texture(item: &str) -> Option { + let name = item.strip_prefix("minecraft:")?; + name.ends_with("_harness") + .then(|| format!("entity/equipment/happy_ghast_body/{name}")) +} +/// Events for the player getting on or off. +pub fn goggles(down: bool, m: &Mob) -> MobEvent { + MobEvent::Sound( + if down { + "entity.happy_ghast.harness_goggles_down" + } else { + "entity.happy_ghast.harness_goggles_up" + }, + m.center(), + 1.0, + 1.0, + ) +} +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_harness_names_its_texture() { + assert_eq!( + harness_texture("minecraft:red_harness").as_deref(), + Some("entity/equipment/happy_ghast_body/red_harness") + ); + assert_eq!(harness_texture("minecraft:saddle"), None); + } + #[test] + fn travel_speed_is_five_thirds_of_the_flying_speed() { + assert!((f64::from(AIR_SPEED) - FLYING_SPEED * 5.0 / 3.0).abs() < 1e-7); + } +} diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index c6a56449b..cef64d66c 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -128,6 +128,14 @@ pub fn body(m: &Mob) -> (String, String) { baby_or(m, "wolf#main", "wolf_baby#main"), super::tame::texture(m), ), + S::HappyGhast => ( + baby_or(m, "happy_ghast#main", "happy_ghast_baby#main"), + if baby { + "entity/ghast/happy_ghast_baby".into() + } else { + "entity/ghast/happy_ghast".into() + }, + ), S::Sniffer => ( baby_or(m, "sniffer#main", "sniffer_baby#main"), if baby { @@ -321,6 +329,17 @@ pub fn layers(m: &Mob) -> Vec { false, ); } + // SimpleEquipmentLayer: the harness over the body. + S::HappyGhast if !m.is_baby() => { + if let Some(texture) = m + .extra + .head + .as_deref() + .and_then(super::happy_ghast::harness_texture) + { + push(&mut v, "happy_ghast_harness#main", &texture, white, false); + } + } // BreezeWindLayer (the scroll is not reproduced: atlas sprites do // not wrap) and BreezeEyesLayer. S::Breeze => { diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index 8a3a4365a..2001f2fca 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -85,6 +85,7 @@ pub enum Species { Allay, Breeze, Sniffer, + HappyGhast, } /// Baby bounding boxes (each class's BABY_DIMENSIONS). #[derive(Clone, Copy, Debug)] @@ -930,6 +931,20 @@ pub const ALL: &[Def] = &[ Some("entity.camel.step"), Baby::Abs(0.95, 1.4, 1.38) ), + def!( + HappyGhast, + "happy_ghast", + "happy_ghast#main", + "entity/ghast/happy_ghast", + A::HappyGhast, + [ + "entity.happy_ghast.ambient", + "entity.happy_ghast.hurt", + "entity.happy_ghast.death" + ], + None, + Baby::Scale(0.2375, Some(0.46875)) + ), def!( Sniffer, "sniffer", @@ -1211,6 +1226,11 @@ impl Def { Species::Fox => &["fox_baby"], Species::Wolf => &["wolf_baby"], Species::Sniffer => &["sniffer_baby"], + Species::HappyGhast => &[ + "happy_ghast_baby", + "happy_ghast_harness", + "happy_ghast_ropes", + ], Species::Cat => &["cat_baby"], Species::Ocelot => &["ocelot_baby"], Species::Bee => &["bee_baby"], @@ -1283,6 +1303,7 @@ impl Def { | S::Parrot | S::Bee | S::Ghast + | S::HappyGhast | S::Blaze | S::Phantom | S::IronGolem @@ -1294,7 +1315,7 @@ impl Def { | S::Wither | S::Allay ), - flying: matches!(s, S::Ghast | S::Phantom), + flying: matches!(s, S::Ghast | S::Phantom | S::HappyGhast), climber: matches!(s, S::Spider | S::CaveSpider), lava_walker: s == S::Strider, omnidirectional: matches!(s, S::Bat | S::Bee | S::Parrot), @@ -1402,6 +1423,7 @@ impl Def { S::Llama | S::TraderLlama => "llama_food", S::Goat => "goat_food", S::Sniffer => "sniffer_food", + S::HappyGhast => "happy_ghast_food", S::Panda => "panda_food", S::Fox => "fox_food", S::Wolf => "wolf_food", @@ -1436,6 +1458,7 @@ impl Def { pub fn air_speed(&self) -> f32 { match self.species { Species::Phantom => 0.2, + Species::HappyGhast => super::happy_ghast::AIR_SPEED, _ => 0.02, } }