From 46f674f842a53d6c211d9189c79a8bd29ec97013 Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Tue, 27 Oct 2026 06:32:31 +0100 Subject: [PATCH] Give dolphins swim-with-player, jump and treasure goals and fish feeding --- .../examples/probe/mob_ai_probe.rs | 1 + crates/horizon-client/src/game/creatures.rs | 40 +++ crates/horizon-client/src/game/lan_mobs.rs | 1 + crates/horizon-client/src/mobs.rs | 9 + crates/horizon-client/src/mobs/aquatic.rs | 3 + crates/horizon-client/src/mobs/dolphin.rs | 263 ++++++++++++++++++ crates/horizon-client/src/mobs/goals.rs | 5 + 7 files changed, 322 insertions(+) create mode 100644 crates/horizon-client/src/mobs/dolphin.rs diff --git a/crates/horizon-client/examples/probe/mob_ai_probe.rs b/crates/horizon-client/examples/probe/mob_ai_probe.rs index 5b6c26d2f..31b9508dd 100644 --- a/crates/horizon-client/examples/probe/mob_ai_probe.rs +++ b/crates/horizon-client/examples/probe/mob_ai_probe.rs @@ -1045,6 +1045,7 @@ pub fn main() -> horizon_client::Result<()> { alive: true, creative: s.creative, sneaking: false, + swimming: false, look: [0.0, 0.0, 1.0], main_hand: s.hand.as_deref().map(Into::into), off_hand: None, diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 2bc40a7af..5c0d1aa11 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -182,6 +182,7 @@ impl Game<'_> { // EntitySelector.NO_CREATIVE_OR_SPECTATOR. creative: self.creative || self.spectator(), sneaking: self.keys.contains(&winit::keyboard::KeyCode::ShiftLeft), + swimming: p.swimming, look: view_vector(self.yaw, self.pitch), main_hand: self.held().map(|s| s.item.clone()), off_hand: None, @@ -1289,6 +1290,27 @@ impl Game<'_> { .list .retain(|e| e.id != "mining_fatigue"); } + MobEvent::DolphinTreasure { mob } => { + // ServerLevel.findNearestMapStructure(#dolphin_located, 50). + let Some(at) = self.mobs.iter().find(|m| m.id == mob).map(|m| { + [ + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ] + }) else { + continue; + }; + let names = self.structure_tag("minecraft:dolphin_located"); + let found = self + .end + .locator() + .filter(|_| !names.is_empty()) + .and_then(|t| t.locate(&names, at, 50)); + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == mob) { + m.extra.home = found.map(|(_, p)| p); + } + } MobEvent::EffectPlayer { effect, ticks, @@ -2881,6 +2903,12 @@ impl Game<'_> { .item_tags .get("minecraft:creeper_igniters") .is_some_and(|t| t.contains(&*item)); + let fish = self + .res + .items() + .item_tags + .get("minecraft:fishes") + .is_some_and(|t| t.contains(&*item)); let damageable = self .res .items() @@ -3168,6 +3196,18 @@ impl Game<'_> { self.trigger_entity("minecraft:tame_animal", "entity", &view); } } + // Dolphin.mobInteract: a fish makes a baby grow by a tenth and an + // adult remember it (`gotFish`), to swim to a treasure. + Species::Dolphin if fish => { + eat = Some(("entity.dolphin.eat", 1.0, 1.0)); + if m.extra.age < 0 { + let secs = ((-m.extra.age / 20) as f32 * 0.1) as i32; + horizon_client::mobs::ai::age_up(m, secs, true); + } else { + m.extra.flag = true; + } + consume = true; + } // HappyGhast.canFallInLove: never, so only a ghastling takes food. _ if is_food && m.kind.flags.animal diff --git a/crates/horizon-client/src/game/lan_mobs.rs b/crates/horizon-client/src/game/lan_mobs.rs index 6527f1015..a783cd093 100644 --- a/crates/horizon-client/src/game/lan_mobs.rs +++ b/crates/horizon-client/src/game/lan_mobs.rs @@ -358,6 +358,7 @@ mod tests { alive: true, creative, sneaking: false, + swimming: false, look: [0.0; 3], main_hand: None, off_hand: None, diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 2325c3f5b..bf84e5040 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -25,6 +25,7 @@ pub mod creaking; pub mod creaking_heart; pub mod cube; pub mod data; +pub mod dolphin; pub mod doors; pub mod drops; pub mod enderman; @@ -1087,6 +1088,12 @@ pub enum MobEvent { }, /// Regeneration support after an axolotl-assisted kill. AxolotlSupportPlayer, + /// A dolphin that was given a fish asks the game for the nearest + /// shipwreck or ocean ruin (`DolphinSwimToTreasureGoal.start`): the game + /// sets the dolphin's `extra.home` to it, or leaves it empty. + DolphinTreasure { + mob: u32, + }, /// Apply an effect to the player (witch potions, bee stings, cave spider poison). EffectPlayer { effect: &'static str, @@ -1540,6 +1547,8 @@ pub struct PlayerView { pub alive: bool, pub creative: bool, pub sneaking: bool, + /// `Entity.isSwimming`. + pub swimming: bool, pub look: [f64; 3], pub main_hand: Option>, pub off_hand: Option>, diff --git a/crates/horizon-client/src/mobs/aquatic.rs b/crates/horizon-client/src/mobs/aquatic.rs index f63a42239..ffc443b21 100644 --- a/crates/horizon-client/src/mobs/aquatic.rs +++ b/crates/horizon-client/src/mobs/aquatic.rs @@ -128,9 +128,12 @@ pub fn register(m: &mut Mob) { let g = &mut m.ai.goals; g.add(0, BreathAir); g.add(0, TryFindWater::default()); + g.add(1, super::dolphin::SwimToTreasure::default()); + g.add(2, super::dolphin::SwimWithPlayer::new(4.0)); g.add(4, Stroll::swimming(1.0, 10)); g.add(4, RandomLookAround::default()); g.add(5, LookAtPlayer::new(6.0)); + g.add(5, super::dolphin::Jump::new(10)); g.add(6, MeleeAttack::new(1.2, true)); m.ai.targets.add(1, HurtByTarget::new().alert()); m.ai.mov.kind = MoveKind::SmoothSwimming { diff --git a/crates/horizon-client/src/mobs/dolphin.rs b/crates/horizon-client/src/mobs/dolphin.rs new file mode 100644 index 000000000..2f9643cef --- /dev/null +++ b/crates/horizon-client/src/mobs/dolphin.rs @@ -0,0 +1,263 @@ +//! Dolphin goals: `DolphinSwimToTreasureGoal`, `DolphinSwimWithPlayerGoal` +//! and `DolphinJumpGoal`. `extra.flag` is `gotFish` and `extra.home` the +//! `treasurePos` the game found for it (`MobEvent::DolphinTreasure`). +use super::{ + Mob, MobEvent, Who, + goals::{Cx, Goal, JUMP, LOOK, MOVE, goal_clone, reduced}, +}; +use crate::blocks::Fluid; +use horizon_core::random::RandomSource; + +/// `Entity.getMotionDirection`: the horizontal step of the yaw +/// (`Direction.fromYRot`: south, west, north, east). +fn motion_step(yaw: f32) -> [i32; 2] { + match (yaw / 90.0 + 0.5).floor() as i32 & 3 { + 0 => [0, 1], + 1 => [-1, 0], + 2 => [0, -1], + _ => [1, 0], + } +} + +/// `DolphinSwimToTreasureGoal`: a dolphin that was fed a fish leads the way to +/// a shipwreck or an ocean ruin within 50 sectors. +#[derive(Clone, Debug, Default)] +pub struct SwimToTreasure { + stuck: bool, +} +impl SwimToTreasure { + /// `BlockPos.containing(x, dolphin.getY(), z).closerToCenterThan(position, 4)`. + fn arrived(m: &Mob) -> bool { + let Some(t) = m.extra.home else { return false }; + let d = [ + f64::from(t[0]) + 0.5 - m.position.x, + m.position.y.floor() + 0.5 - m.position.y, + f64::from(t[2]) + 0.5 - m.position.z, + ]; + d[0] * d[0] + d[1] * d[1] + d[2] * d[2] < 16.0 + } +} +impl Goal for SwimToTreasure { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE | LOOK + } + fn interruptable(&self) -> bool { + false + } + fn can_use(&mut self, m: &mut Mob, _: &mut Cx) -> bool { + m.extra.flag && m.air >= 100 + } + fn can_continue(&mut self, m: &mut Mob, _: &mut Cx) -> bool { + m.extra.home.is_some() && !Self::arrived(m) && !self.stuck && m.air >= 100 + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + self.stuck = false; + m.nav_stop(); + // The game looks for the structure and sets the position, or none. + m.extra.home = None; + cx.events.push(MobEvent::DolphinTreasure { mob: m.id }); + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + if m.extra.home.is_none() || Self::arrived(m) || self.stuck { + m.extra.flag = false; + } + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + let Some(t) = m.extra.home else { return }; + let close = m.ai.nav.target_pos.is_some_and(|p| { + let d = [ + f64::from(p[0]) + 0.5 - m.position.x, + f64::from(p[1]) + 0.5 - m.position.y, + f64::from(p[2]) + 0.5 - m.position.z, + ]; + d[0] * d[0] + d[1] * d[1] + d[2] * d[2] < 144.0 + }); + if !close && !m.ai.nav.is_done() { + return; + } + let treasure = [ + f64::from(t[0]) + 0.5, + f64::from(t[1]) + 0.5, + f64::from(t[2]) + 0.5, + ]; + let eighth = f64::from(std::f32::consts::FRAC_PI_8); + let half = f64::from(std::f32::consts::FRAC_PI_2); + let mut next = m.default_random_pos_towards(cx.w, 16, 1, treasure, eighth); + if next.is_none() { + next = m.default_random_pos_towards(cx.w, 8, 4, treasure, half); + } + // isPathfindable(WATER) is a water fluid state. + if let Some(n) = next + && cx.w.fluid( + n[0].floor() as i32, + n[1].floor() as i32, + n[2].floor() as i32, + ) != Fluid::Water + { + next = m.default_random_pos_towards(cx.w, 8, 5, treasure, half); + } + let Some(next) = next else { + self.stuck = true; + return; + }; + let (y, x) = (m.kind.max_head_y_rot + 20.0, m.kind.max_head_x_rot); + m.look_at_speed(next, y, x); + m.nav_move_to(cx.w, next, 1.3); + if m.random.next_int_bound(80).unwrap_or(1) == 0 { + // Entity event 38: happy villager particles about the dolphin. + cx.events.push(MobEvent::AroundSelf { + kind: "happy_villager", + feet: [m.position.x, m.position.y, m.position.z], + width: f64::from(m.width()), + height: f64::from(m.height()), + burst: crate::particles::Burst::DOLPHIN, + }); + } + } +} + +/// `DolphinSwimWithPlayerGoal`: swim up to a swimming player and give them +/// Dolphin's Grace. +#[derive(Clone, Debug)] +pub struct SwimWithPlayer { + speed: f64, +} +impl SwimWithPlayer { + pub fn new(speed: f64) -> Self { + Self { speed } + } +} +impl Goal for SwimWithPlayer { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE | LOOK + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + // getNearestPlayer(forNonCombat().range(10).ignoreLineOfSight()). + m.can_see_player_through_walls(cx.w, 10.0) + && cx.w.player.as_ref().is_some_and(|p| p.swimming) + && m.ai.target != Some(Who::Player) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + cx.w.player + .as_ref() + .is_some_and(|p| p.swimming && m.distance_sqr(p.position) < 256.0) + } + fn start(&mut self, _: &mut Mob, cx: &mut Cx) { + cx.events.push(grace()); + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.nav_stop(); + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + let Some(p) = cx.w.player.clone() else { return }; + let (y, x) = (m.kind.max_head_y_rot + 20.0, m.kind.max_head_x_rot); + m.look_at_speed(p.eye, y, x); + if m.distance_sqr(p.position) < 6.25 { + m.nav_stop(); + } else { + m.nav_move_to_entity( + cx.w, + [p.position.x, p.position.y, p.position.z], + 1, + self.speed, + ); + } + if p.swimming && m.random.next_int_bound(6).unwrap_or(1) == 0 { + cx.events.push(grace()); + } + } +} +/// `MobEffectInstance(DOLPHINS_GRACE, 100)`. +fn grace() -> MobEvent { + MobEvent::EffectPlayer { + effect: "minecraft:dolphins_grace", + ticks: 100, + amplifier: 0, + } +} + +/// `DolphinJumpGoal`: leap out of clear water and arc back in. +#[derive(Clone, Debug)] +pub struct Jump { + interval: i32, + breached: bool, +} +impl Jump { + pub fn new(interval: i32) -> Self { + Self { + interval: reduced(interval), + breached: false, + } + } +} +impl Goal for Jump { + goal_clone!(); + fn flags(&self) -> u8 { + JUMP | MOVE + } + fn interruptable(&self) -> bool { + false + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if m.random.next_int_bound(self.interval).unwrap_or(1) != 0 { + return false; + } + let [sx, sz] = motion_step(m.yaw); + let (bx, by, bz) = ( + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ); + let w = cx.w; + [0, 1, 4, 5, 6, 7].into_iter().all(|i| { + let (x, z) = (bx + sx * i, bz + sz * i); + // waterIsClear, then surfaceIsClear. + w.fluid(x, by, z) == Fluid::Water + && !w.has_tag("minecraft:blocks_dolphin_jump", w.block_name(x, by, z)) + && w.facts.is_air(w.world.block(x, by + 1, z)) + && w.facts.is_air(w.world.block(x, by + 2, z)) + }) + } + fn can_continue(&mut self, m: &mut Mob, _: &mut Cx) -> bool { + let yd = m.velocity.y; + (yd * yd >= f64::from(0.03f32) + || m.head_pitch == 0.0 + || m.head_pitch.abs() >= 10.0 + || !m.fluids.in_water) + && !m.on_ground + } + fn start(&mut self, m: &mut Mob, _: &mut Cx) { + let [sx, sz] = motion_step(m.yaw); + m.velocity.x += f64::from(sx) * 0.6; + m.velocity.y += 0.7; + m.velocity.z += f64::from(sz) * 0.6; + m.nav_stop(); + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.head_pitch = 0.0; + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + let already = self.breached; + if !already { + self.breached = cx.w.fluid( + m.position.x.floor() as i32, + m.position.y.floor() as i32, + m.position.z.floor() as i32, + ) == Fluid::Water; + } + if self.breached && !already { + m.sound("entity.dolphin.jump", cx.events); + } + let v = m.velocity; + if v.y * v.y < f64::from(0.03f32) && m.head_pitch != 0.0 { + // Mth.rotLerp(0.2, xRot, 0). + m.head_pitch += 0.2 * super::living::wrap(0.0 - m.head_pitch); + } else if v.length() > f64::from(1.0e-5f32) { + let horizontal = v.x.hypot(v.z); + m.head_pitch = ((-v.y).atan2(horizontal) * f64::from(180.0f32) + / f64::from(std::f32::consts::PI)) as f32; + } + } +} diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index d0b784166..b172597d9 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -595,6 +595,11 @@ impl Mob { pub fn can_see_player(&mut self, w: &MobWorld, range: f64) -> bool { self.player_in_range(w, range, true) } + /// `TargetingConditions.forNonCombat().range(range).ignoreLineOfSight()` + /// for the player. + pub fn can_see_player_through_walls(&mut self, w: &MobWorld, range: f64) -> bool { + self.player_in_range(w, range, false) + } fn player_in_range(&mut self, w: &MobWorld, range: f64, check_sight: bool) -> bool { let Some(p) = &w.player else { return false }; if !p.alive || p.spectator {