diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index 635296cd4..535efe62b 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -157,6 +157,19 @@ 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:wolf" => { + if let Some(owner) = m.extra.owner { + map.insert("Owner".into(), Tag::IntArray(owner.to_vec())); + } else { + map.remove(&"Owner".into()); + } + map.insert("Sitting".into(), Tag::Byte(i8::from(m.extra.sitting))); + map.insert("CollarColor".into(), Tag::Byte(m.extra.color as i8)); + let variant = crate::mobs::tame::WOLF_VARIANTS[(m.extra.variant as usize).min(8)].0; + map.insert("variant".into(), string(&format!("minecraft:{variant}"))); + map.entry("sound_variant".into()) + .or_insert_with(|| string("minecraft:classic")); + } "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))); @@ -225,6 +238,24 @@ 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 m.species() == crate::mobs::Species::Wolf { + let health = m.health; + m.extra.owner = crate::save::uuid_from_tag(tag.get("Owner")); + let tame = m.extra.owner.is_some(); + crate::mobs::tame::set_tame(&mut m, tame); + m.health = health; + m.extra.sitting = flag(tag, "Sitting"); + m.extra.color = + number(tag.get("CollarColor")).map_or(crate::mobs::tame::RED, |c| (c as u8) & 15); + if let Some(Tag::String(v)) = tag.get("variant") { + let v = v.to_string().unwrap_or_default(); + let v = v.trim_start_matches("minecraft:"); + m.extra.variant = crate::mobs::tame::WOLF_VARIANTS + .iter() + .position(|(n, _)| *n == v) + .unwrap_or(0) as u8; + } + } if matches!( m.species(), crate::mobs::Species::Slime | crate::mobs::Species::MagmaCube diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0adfdf443..768d70620 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4064,6 +4064,8 @@ impl Game<'_> { let player = self.player.as_ref(); let from = player.map(|p| [p.position.x, p.position.y, p.position.z]); self.swing_arm(); + // LivingEntity.setLastHurtMob (tamed wolves join in). + self.combat.last_hurt_mob = Some((self.mobs[index].id, self.world.game_time)); let mob = &mut self.mobs[index]; let center = [ mob.position.x, diff --git a/crates/horizon-client/src/game/combat.rs b/crates/horizon-client/src/game/combat.rs index 857bf22b5..f83290b49 100644 --- a/crates/horizon-client/src/game/combat.rs +++ b/crates/horizon-client/src/game/combat.rs @@ -21,6 +21,8 @@ pub(super) struct CombatTracker { pending: Vec<(String, (u32, Text))>, /// `LivingEntity.lastHurtByMob` and its timestamp (the kill credit). last_hurt_by: Option<((u32, Text), i64)>, + /// The mob the player last hurt (`getLastHurtMob`) and when. + pub(super) last_hurt_mob: Option<(u32, i64)>, /// The mob whose projectile is hitting the player now. pub(super) projectile_owner: Option, /// The player's fall distance before this tick's movement (as of the @@ -36,6 +38,11 @@ impl Game<'_> { None => Text::translate(&format!("entity.{}", m.kind.id.replace(':', ".")), &[]), }) } + /// `getLastHurtByMob`: the last mob to hurt the player, for 100 ticks. + pub(super) fn player_hurt_by(&self) -> Option<(horizon_client::mobs::Who, i64)> { + let ((id, _), at) = self.combat.last_hurt_by.as_ref()?; + (self.world.game_time - at <= 100).then_some((horizon_client::mobs::Who::Mob(*id), *at)) + } /// Remember who deals the next `damage_type` damage to the player. pub(super) fn note_attacker(&mut self, damage_type: &str, mob: u32) { if let Some(name) = self.mob_display_name(mob) { diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index d29710174..95d2bc9c3 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -128,6 +128,9 @@ impl Game<'_> { off_hand: None, head: self.armor[0].as_ref().map(|s| s.item.clone()), armor_cover: self.armor.iter().filter(|a| a.is_some()).count() as f32 / 4.0, + spectator: self.spectator(), + hurt_by: self.player_hurt_by(), + last_hurt: self.combat.last_hurt_mob.map(|(id, at)| (Who::Mob(id), at)), }) } /// Biome facts at a position for finalizeSpawn and variant rules. @@ -156,6 +159,7 @@ impl Game<'_> { mushroom: b.name == "minecraft:mushroom_fields", desert: b.name == "minecraft:desert", jungle: has("minecraft:is_jungle"), + wolf: horizon_client::mobs::tame::wolf_variant_for(&b.name, &has), frog: if has("minecraft:spawns_warm_variant_frogs") { 1 } else if has("minecraft:spawns_cold_variant_frogs") { @@ -746,7 +750,26 @@ impl Game<'_> { } self.world.set(pos[0], pos[1], pos[2], self.world.air)?; } - MobEvent::Particles { .. } => {} + MobEvent::Particles { kind, at, count } => { + // Mob particle bursts (hearts, smoke, splashes) around a point. + if let Some(ty) = horizon_client::particles::ParticleType::from_name(kind) { + let options = horizon_client::particles::ParticleOptions::simple(ty); + for _ in 0..count { + let r = &mut self.world.random; + let p = [ + at[0] + (f64::from(r.next_float()) - 0.5), + at[1] + (f64::from(r.next_float()) - 0.5) * 0.5, + at[2] + (f64::from(r.next_float()) - 0.5), + ]; + let v = [ + r.next_gaussian() * 0.02, + r.next_gaussian() * 0.02, + r.next_gaussian() * 0.02, + ]; + self.particles.add(&options, p, v); + } + } + } MobEvent::Experience { at, amount } => self.spawn_orbs(at, amount), MobEvent::EffectPlayer { effect, @@ -812,11 +835,36 @@ impl Game<'_> { q.extra.age = 6000; q.extra.in_love = 0; } + let q = self.mobs.iter().find(|m| m.id == partner).cloned(); + let coin = self.world.random.next_int_bound(2).unwrap_or(0) == 0; + let coin2 = self.world.random.next_int_bound(2).unwrap_or(0) == 0; if let Some(id) = self.spawn_mob(&p.kind.id, [p.position.x, p.position.y, p.position.z], true) && let Some(child) = self.mobs.iter_mut().find(|m| m.id == id) { child.extra.age = -24000; + if p.species() == Species::Wolf { + // Wolf.getBreedOffspring: a parent's variant; tame parents + // give a tame baby with one of their collar colours. + let other = q.as_ref().unwrap_or(&p); + child.extra.variant = if coin { + p.extra.variant + } else { + other.extra.variant + }; + if p.extra.tame { + horizon_client::mobs::tame::set_tame(child, true); + child.extra.owner = p.extra.owner; + child.extra.color = if coin2 { + p.extra.color + } else { + other.extra.color + }; + } + 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); + return; + } // Offspring inherit a parent's variant (pigs/cows/chickens) or mixed wool. child.extra.variant = p.extra.variant; if p.species() == Species::Sheep { @@ -1339,6 +1387,83 @@ impl Game<'_> { handled = false; } } + // Wolf.mobInteract: heal, dye the collar or sit/stand when tame; + // a bone tames a wild one a third of the time. + Species::Wolf if m.extra.tame && is_food && m.health < m.attrs.max_health => { + let heal = self + .res + .items + .get(&item) + .and_then(|i| i.food.as_ref()) + .map_or(2.0, |f| f.0 as f32); + m.health = (m.health + heal).min(m.attrs.max_health); + consume = true; + sound = Some("entity.wolf.eat"); + } + Species::Wolf + if m.extra.tame + && item.ends_with("_dye") + && horizon_client::mobs::render::DYE_NAMES + .iter() + .position(|d| { + item.trim_start_matches("minecraft:") + .trim_end_matches("_dye") + == *d + }) + .is_some_and(|c| c as u8 != m.extra.color) => + { + let name = item + .trim_start_matches("minecraft:") + .trim_end_matches("_dye"); + if let Some(c) = horizon_client::mobs::render::DYE_NAMES + .iter() + .position(|d| *d == name) + { + m.extra.color = c as u8; + } + consume = true; + } + Species::Wolf + if m.extra.tame && !(is_food && m.extra.age == 0 && m.extra.in_love <= 0) => + { + m.extra.sitting = !m.extra.sitting; + m.ai.jumping = false; + m.nav_stop(); + m.ai.target = None; + self.world.modified.insert(( + m.position.x.floor() as i32 >> 4, + m.position.z.floor() as i32 >> 4, + )); + } + Species::Wolf + if !m.extra.tame + && item == "minecraft:bone" + && !horizon_client::mobs::tame::angry(m) => + { + consume = true; + let at = [ + m.position.x, + m.position.y + f64::from(m.height()) * 0.5 + 0.5, + m.position.z, + ]; + let tamed = m.random.next_int_bound(3).unwrap_or(1) == 0; + if tamed { + horizon_client::mobs::tame::set_tame(m, true); + m.extra.owner = self.level.player_uuid.or(Some([0, 0, 0, 1])); + m.persistent = true; + m.nav_stop(); + m.ai.target = None; + m.extra.sitting = true; + } + // TamableAnimal.spawnTamingParticles. + let kind = if tamed { "heart" } else { "smoke" }; + self.handle_mob_events(vec![MobEvent::Particles { kind, at, count: 7 }])?; + let m = &self.mobs[index]; + self.world.modified.insert(( + m.position.x.floor() as i32 >> 4, + m.position.z.floor() as i32 >> 4, + )); + } Species::Creeper if item == "minecraft:flint_and_steel" => { m.extra.flag = true; sound = Some("item.flintandsteel.use"); diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index bafbed23f..53df3cca1 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -22,6 +22,7 @@ pub mod path; pub mod render; pub mod spawning; pub mod species; +pub mod tame; pub mod undead; pub mod witch; mod world; @@ -410,6 +411,8 @@ pub struct Extra { pub chested: bool, pub tame: bool, pub sitting: bool, + /// The owner's UUID (tamed animals). + pub owner: Option<[i32; 4]>, /// Slime/magma cube size (0 = not sized). pub size: i32, /// Sheep eat-grass / iron golem attack / ravager animation ticks. @@ -610,6 +613,11 @@ pub struct PlayerView { pub off_hand: Option>, pub head: Option>, pub armor_cover: f32, + pub spectator: bool, + /// `getLastHurtByMob` (within 100 ticks) and when. + pub hurt_by: Option<(Who, i64)>, + /// `getLastHurtMob` and when. + pub last_hurt: Option<(Who, i64)>, } pub struct MobWorld<'a> { pub world: &'a World, diff --git a/crates/horizon-client/src/mobs/ai.rs b/crates/horizon-client/src/mobs/ai.rs index 449a6609a..73e110231 100644 --- a/crates/horizon-client/src/mobs/ai.rs +++ b/crates/horizon-client/src/mobs/ai.rs @@ -69,6 +69,7 @@ pub enum Family { Inert, Witch, Enderman, + Tame, /// Slimes and magma cubes. Cube, } @@ -95,6 +96,7 @@ pub fn family(s: Species) -> Family { | S::Dolphin => Family::Aquatic, S::Giant => Family::Inert, S::Witch => Family::Witch, + S::Wolf => Family::Tame, S::Enderman => Family::Enderman, S::Slime | S::MagmaCube => Family::Cube, _ => Family::Generic, @@ -109,6 +111,7 @@ pub fn register(m: &mut Mob) { Family::Generic => generic(m), Family::Inert => {} Family::Witch => super::witch::register(m), + Family::Tame => super::tame::register(m), Family::Enderman => super::enderman::register(m), Family::Cube => super::cube::register(m), } @@ -456,6 +459,7 @@ fn species_ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { super::witch::ai_step(m, events, far) } Species::Enderman => super::enderman::ai_step(m, w, events), + Species::Wolf => super::tame::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 => { @@ -556,8 +560,10 @@ 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) { m.ai.no_action_time = 0; - if m.species() == Species::Enderman { - super::enderman::after_hurt(m, source); + match m.species() { + Species::Enderman => super::enderman::after_hurt(m, source), + Species::Wolf => super::tame::after_hurt(m, source), + _ => {} } } /// Mob.doHurtTarget: attack damage (+ species extras) to the target. diff --git a/crates/horizon-client/src/mobs/anim.rs b/crates/horizon-client/src/mobs/anim.rs index 4a703bad5..c9318a880 100644 --- a/crates/horizon-client/src/mobs/anim.rs +++ b/crates/horizon-client/src/mobs/anim.rs @@ -418,6 +418,7 @@ pub fn setup( } } Anim::Witch => super::witch::anim(m, s, p), + Anim::Wolf => super::tame::anim(m, s, p), Anim::Enderman => super::enderman::anim(m, s, p), Anim::Slime | Anim::MagmaCube => super::cube::anim(m, s, p), Anim::Creeper => { diff --git a/crates/horizon-client/src/mobs/goals.rs b/crates/horizon-client/src/mobs/goals.rs index 021de51c5..dbb01ee8e 100644 --- a/crates/horizon-client/src/mobs/goals.rs +++ b/crates/horizon-client/src/mobs/goals.rs @@ -1411,10 +1411,15 @@ impl Goal for Breed { return false; } let species = m.species(); + // Wolf.canMate: both tame and standing. + let tamable = species == Species::Wolf; + if tamable && (!m.extra.tame || m.extra.sitting) { + return false; + } let id = m.id; self.partner = cx.w.nearby(m.position, 8.0, move |s| { - s.id != id && s.species == species && s.in_love && !s.baby + s.id != id && s.species == species && s.in_love && !s.baby && (!tamable || s.tame) }) .map(|s| s.id) .next(); diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index 8a408bd01..527ccc83e 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -103,6 +103,10 @@ pub fn body(m: &Mob) -> (String, String) { } }; match m.species() { + S::Wolf => ( + baby_or(m, "wolf#main", "wolf_baby#main"), + super::tame::texture(m), + ), S::Frog => ( "frog#main".into(), format!("entity/frog/frog_{}", climate(m)), @@ -234,8 +238,9 @@ pub fn body(m: &Mob) -> (String, String) { } } /// LivingEntityRenderer.getModelTint (tropical fish base colour). -pub fn body_color(m: &Mob) -> u32 { +pub fn body_color(m: &Mob, alpha: f32) -> u32 { match m.species() { + Species::Wolf => super::tame::tint(m, alpha), Species::TropicalFish => abgr(DYE[m.extra.variant2 as usize & 15]), _ => 0xFFFF_FFFF, } @@ -254,6 +259,21 @@ pub fn layers(m: &Mob) -> Vec { }) }; match m.species() { + // WolfCollarLayer. + S::Wolf if m.extra.tame => { + let (model, texture) = if m.is_baby() { + ("wolf_baby#main", "entity/wolf/wolf_collar_baby") + } else { + ("wolf#main", "entity/wolf/wolf_collar") + }; + push( + &mut v, + model, + texture, + abgr(DYE[m.extra.color as usize & 15]), + false, + ); + } // SlimeOuterLayer: the translucent outer cube. S::Slime => push(&mut v, "slime#outer", "entity/slime/slime", white, false), S::Sheep if !m.extra.sheared => { @@ -543,7 +563,16 @@ pub fn render( let mut poses = Poses::new(&model); anim::setup(m, &s, &mut poses, &data.animations); if let Some(uv) = atlas(&texture) { - draw(&model, base, &poses, uv, body_color(m), light, overlay, out); + draw( + &model, + base, + &poses, + uv, + body_color(m, alpha), + light, + overlay, + out, + ); } held_items(m, &model, base, &poses, atlas, light, out); for layer in layers(m) { diff --git a/crates/horizon-client/src/mobs/spawning.rs b/crates/horizon-client/src/mobs/spawning.rs index 9c7cda3d2..4ea244492 100644 --- a/crates/horizon-client/src/mobs/spawning.rs +++ b/crates/horizon-client/src/mobs/spawning.rs @@ -20,6 +20,8 @@ pub struct BiomeFacts { pub jungle: bool, /// Frog variant from `spawns_warm/cold_variant_frogs` (0 temperate, 1 warm, 2 cold). pub frog: u8, + /// Wolf variant index (`tame::WOLF_VARIANTS`). + pub wolf: u8, } /// SpawnPlacementTypes for the species. pub fn placement_ok(species: Species, w: &MobWorld, x: i32, y: i32, z: i32) -> bool { @@ -169,6 +171,7 @@ pub fn finalize(m: &mut Mob, biome: &BiomeFacts) { S::Sheep => m.extra.color = super::farm::sheep_color(biome.climate, &mut m.random), S::Mooshroom => m.extra.variant = 0, S::Frog => m.extra.variant = biome.frog, + S::Wolf => m.extra.variant = biome.wolf, S::Zombie | S::Husk | S::Drowned | S::ZombieVillager => { // Zombie.finalizeSpawn: 5% babies (normal difficulty). if m.random.next_float() < 0.05 { diff --git a/crates/horizon-client/src/mobs/tame.rs b/crates/horizon-client/src/mobs/tame.rs new file mode 100644 index 000000000..674b3f2c7 --- /dev/null +++ b/crates/horizon-client/src/mobs/tame.rs @@ -0,0 +1,674 @@ +//! Tamable animals (`TamableAnimal`): wolves with their variants, taming, +//! sitting, following and defending the owner, begging, anger and shaking +//! off water (`Wolf`, `WolfModel`). +use super::{ + Damage, Mob, MobEvent, MobWorld, Species, Who, + anim::{Poses, State}, + goals::{ + Breed, Cx, Float, Goal, HurtByTarget, JUMP, LOOK, LeapAtTarget, LookAtPlayer, MOVE, + MeleeAttack, NearestTarget, Panic, Prey, RandomLookAround, Stroll, TARGET, goal_clone, + reduced, + }, +}; +use horizon_core::{ + geometry::{Aabb, Vec3}, + random::RandomSource, +}; +use std::f32::consts::PI; + +/// Wolf variants: registry name and texture file (`WolfVariants`). +pub const WOLF_VARIANTS: [(&str, &str); 9] = [ + ("pale", "wolf"), + ("spotted", "wolf_spotted"), + ("snowy", "wolf_snowy"), + ("black", "wolf_black"), + ("ashen", "wolf_ashen"), + ("rusty", "wolf_rusty"), + ("woods", "wolf_woods"), + ("chestnut", "wolf_chestnut"), + ("striped", "wolf_striped"), +]; +/// The wolf variant a biome spawns (its high-priority `BiomeCheck`). +pub fn wolf_variant_for(biome: &str, has_tag: &dyn Fn(&str) -> bool) -> u8 { + if has_tag("minecraft:is_savanna") { + 1 + } else if biome == "minecraft:grove" { + 2 + } else if biome == "minecraft:old_growth_pine_taiga" { + 3 + } else if biome == "minecraft:snowy_taiga" { + 4 + } else if has_tag("minecraft:is_jungle") { + 5 + } else if biome == "minecraft:forest" { + 6 + } else if biome == "minecraft:old_growth_spruce_taiga" { + 7 + } else if has_tag("minecraft:is_badlands") { + 8 + } else { + 0 + } +} +/// The default collar: red. +pub const RED: u8 = 14; + +// AnimState slots: begging interest and the shake animation. +const INTEREST: usize = 0; +const SHAKE: usize = 1; + +pub fn register(m: &mut Mob) { + m.extra.color = RED; + let food = m.kind.food.clone(); + let g = &mut m.ai.goals; + g.add(1, Float); + g.add(1, TamePanic(Panic::new(1.5))); + g.add(2, SitWhenOrdered); + g.add(3, AvoidLlamas); + g.add(4, LeapAtTarget::new(0.4)); + g.add(5, MeleeAttack::new(1.0, true)); + g.add(6, FollowOwner::new(1.0, 10.0, 2.0)); + g.add(7, Breed::new(1.0)); + g.add(8, Stroll::avoiding_water(1.0)); + g.add(9, Beg { food, ticks: 0 }); + g.add(10, LookAtPlayer::new(8.0)); + g.add(10, RandomLookAround::default()); + m.ai.targets.add(1, OwnerHurtBy { timestamp: 0 }); + m.ai.targets.add(2, OwnerHurt { timestamp: 0 }); + m.ai.targets.add(3, HurtByTarget::new().alert()); + m.ai.targets + .add(4, NearestTarget::player(true).when(angry_at_player)); + m.ai.targets.add( + 5, + NearestTarget::new(Prey::Filter(wolf_prey), false).when(|m, _| !m.extra.tame), + ); + m.ai.targets.add( + 6, + NearestTarget::new(Prey::Filter(baby_turtle_on_land), false).when(|m, _| !m.extra.tame), + ); + m.ai.targets.add( + 7, + NearestTarget::species( + &[ + Species::Skeleton, + Species::Stray, + Species::Bogged, + Species::WitherSkeleton, + ], + false, + ), + ); +} +/// `Wolf.PREY_SELECTOR`: sheep, rabbits and foxes. +fn wolf_prey(s: &super::Seen) -> bool { + matches!(s.species, Species::Sheep | Species::Rabbit | Species::Fox) +} +fn baby_turtle_on_land(s: &super::Seen) -> bool { + s.species == Species::Turtle && s.baby +} +fn angry_at_player(m: &Mob, _: &MobWorld) -> bool { + m.extra.anger > 0 +} +/// `Wolf.isAngry`. +pub fn angry(m: &Mob) -> bool { + m.extra.anger > 0 +} +/// `TamableAnimal.applyTamingSideEffects` for wolves: 40 health when tame. +pub fn set_tame(m: &mut Mob, tame: bool) { + m.extra.tame = tame; + if tame { + m.attrs.max_health = 40.0; + m.health = 40.0; + } else { + m.attrs.max_health = 8.0; + } +} +/// The owner (the local player) when tame. +fn owner(m: &Mob, w: &MobWorld) -> Option<(Vec3, f64, bool)> { + if !m.extra.tame { + return None; + } + let p = w.player.as_ref()?; + Some((p.position, p.eye[1], p.spectator)) +} +/// `TamableAnimal.unableToMoveToOwner`. +fn unable_to_move_to_owner(m: &Mob, w: &MobWorld) -> bool { + m.extra.sitting || w.player.as_ref().is_some_and(|p| p.spectator) +} +/// `TamableAnimal.tryToTeleportToOwner`: ten tries around the owner. +fn teleport_to_owner(m: &mut Mob, w: &MobWorld) { + let Some((at, _, _)) = owner(m, w) else { + return; + }; + let base = [ + at.x.floor() as i32, + at.y.floor() as i32, + at.z.floor() as i32, + ]; + for _ in 0..10 { + let xd = m.random.next_int_bound(7).unwrap_or(0) - 3; + let zd = m.random.next_int_bound(7).unwrap_or(0) - 3; + if xd.abs() < 2 && zd.abs() < 2 { + continue; + } + let yd = m.random.next_int_bound(3).unwrap_or(0) - 1; + let p = [base[0] + xd, base[1] + yd, base[2] + zd]; + if can_teleport_to(m, w, p) { + m.position = Vec3::new( + f64::from(p[0]) + 0.5, + f64::from(p[1]), + f64::from(p[2]) + 0.5, + ); + m.previous = m.position; + m.nav_stop(); + return; + } + } +} +/// `TamableAnimal.canTeleportTo`: walkable, not on leaves, no collision. +fn can_teleport_to(m: &Mob, w: &MobWorld, p: [i32; 3]) -> bool { + let below = w.facts.get(w.world.block(p[0], p[1] - 1, p[2])); + if !below.full_collision || below.name.ends_with("_leaves") { + return false; + } + let here = w.facts.get(w.world.block(p[0], p[1], p[2])); + if here.fluid != crate::blocks::Fluid::None { + return false; + } + let half = f64::from(m.width()) / 2.0; + let (x, y, z) = ( + f64::from(p[0]) + 0.5, + f64::from(p[1]), + f64::from(p[2]) + 0.5, + ); + w.no_collision(Aabb::new( + Vec3::new(x - half, y, z - half), + Vec3::new(x + half, y + f64::from(m.height()), z + half), + )) +} +fn owner_distance_sqr(m: &Mob, w: &MobWorld) -> Option { + owner(m, w).map(|(at, _, _)| m.distance_sqr(at)) +} + +/// `TamableAnimal.TamableAnimalPanicGoal`: panic from fire and freezing, +/// teleporting to a far owner. +#[derive(Clone, Debug)] +struct TamePanic(Panic); +impl Goal for TamePanic { + goal_clone!(); + fn flags(&self) -> u8 { + self.0.flags() + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + // PanicGoal with PANIC_ENVIRONMENTAL_CAUSES: only burning or freezing. + m.fire_ticks > 0 && self.0.can_use(m, cx) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + self.0.can_continue(m, cx) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.start(m, cx) + } + fn stop(&mut self, m: &mut Mob, cx: &mut Cx) { + self.0.stop(m, cx) + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + if !unable_to_move_to_owner(m, cx.w) + && owner_distance_sqr(m, cx.w).is_some_and(|d| d >= 144.0) + { + teleport_to_owner(m, cx.w); + } + self.0.tick(m, cx); + } +} +/// `SitWhenOrderedToGoal`. +#[derive(Clone, Debug)] +struct SitWhenOrdered; +impl Goal for SitWhenOrdered { + goal_clone!(); + fn flags(&self) -> u8 { + JUMP | MOVE + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if !m.extra.sitting && !m.extra.tame { + return false; + } + if m.fluids.in_water || !m.on_ground { + return false; + } + match owner_distance_sqr(m, cx.w) { + None => true, + Some(d) => { + let attacked = cx.w.player.as_ref().is_some_and(|p| p.hurt_by.is_some()); + !(d < 144.0 && attacked) && m.extra.sitting + } + } + } + fn can_continue(&mut self, m: &mut Mob, _: &mut Cx) -> bool { + m.extra.sitting + } + fn start(&mut self, m: &mut Mob, _: &mut Cx) { + m.nav_stop(); + m.extra.flag = false; + } +} +/// `Wolf.WolfAvoidEntityGoal`: untamed wolves flee llamas. +#[derive(Clone, Debug)] +struct AvoidLlamas; +impl Goal for AvoidLlamas { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if m.extra.tame { + return false; + } + let pos = m.position; + let llama = + cx.w.nearby(pos, 24.0, |s| { + matches!(s.species, Species::Llama | Species::TraderLlama) && s.alive + }) + .map(|s| s.position) + .next(); + let Some(from) = llama else { + return false; + }; + // DefaultRandomPos.getPosAway(16, 7, from). + let away = Vec3::new(pos.x - from.x, 0.0, pos.z - from.z); + let len = (away.x * away.x + away.z * away.z).sqrt().max(1e-6); + let target = [ + pos.x + away.x / len * 16.0, + pos.y, + pos.z + away.z / len * 16.0, + ]; + m.nav_move_to(cx.w, target, 1.5) + } + fn can_continue(&mut self, m: &mut Mob, _: &mut Cx) -> bool { + !m.ai.nav.is_done() + } +} +/// `FollowOwnerGoal(speed, startDistance, stopDistance)`. +#[derive(Clone, Debug)] +struct FollowOwner { + speed: f64, + start: f64, + stop: f64, + recalc: i32, +} +impl FollowOwner { + fn new(speed: f64, start: f64, stop: f64) -> Self { + Self { + speed, + start, + stop, + recalc: 0, + } + } +} +impl Goal for FollowOwner { + goal_clone!(); + fn flags(&self) -> u8 { + MOVE | LOOK + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + let Some(d) = owner_distance_sqr(m, cx.w) else { + return false; + }; + !unable_to_move_to_owner(m, cx.w) && d >= self.start * self.start + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if m.ai.nav.is_done() || unable_to_move_to_owner(m, cx.w) { + return false; + } + owner_distance_sqr(m, cx.w).is_some_and(|d| d > self.stop * self.stop) + } + fn start(&mut self, m: &mut Mob, _: &mut Cx) { + self.recalc = 0; + m.set_malus(super::path::PathType::Water, 0.0); + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.nav_stop(); + let water = super::path::PathType::Water.malus(); + m.set_malus(super::path::PathType::Water, water); + } + fn every_tick(&self) -> bool { + true + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + let Some((at, eye, _)) = owner(m, cx.w) else { + return; + }; + let far = m.distance_sqr(at) >= 144.0; + if !far { + let max = max_head_x_rot(m); + m.look_at_speed([at.x, eye, at.z], 10.0, max); + } + self.recalc -= 1; + if self.recalc <= 0 { + self.recalc = reduced(10); + if far { + teleport_to_owner(m, cx.w); + } else { + m.nav_move_to_entity(cx.w, [at.x, at.y, at.z], 0, self.speed); + } + } + } +} +/// `OwnerHurtByTargetGoal`: attack what hurt the owner. +#[derive(Clone, Debug)] +struct OwnerHurtBy { + timestamp: i64, +} +impl Goal for OwnerHurtBy { + goal_clone!(); + fn flags(&self) -> u8 { + TARGET + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if !m.extra.tame || m.extra.sitting { + return false; + } + let Some((who, at)) = cx.w.player.as_ref().and_then(|p| p.hurt_by) else { + return false; + }; + at != self.timestamp && wants_to_attack(m, cx.w, who) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + m.ai.target + .and_then(|t| cx.w.target_info(t)) + .is_some_and(|t| t.alive) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + if let Some((who, at)) = cx.w.player.as_ref().and_then(|p| p.hurt_by) { + m.ai.target = Some(who); + self.timestamp = at; + } + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.ai.target = None; + } +} +/// `OwnerHurtTargetGoal`: attack what the owner attacks. +#[derive(Clone, Debug)] +struct OwnerHurt { + timestamp: i64, +} +impl Goal for OwnerHurt { + goal_clone!(); + fn flags(&self) -> u8 { + TARGET + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + if !m.extra.tame || m.extra.sitting { + return false; + } + let Some((who, at)) = cx.w.player.as_ref().and_then(|p| p.last_hurt) else { + return false; + }; + at != self.timestamp && wants_to_attack(m, cx.w, who) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + m.ai.target + .and_then(|t| cx.w.target_info(t)) + .is_some_and(|t| t.alive) + } + fn start(&mut self, m: &mut Mob, cx: &mut Cx) { + if let Some((who, at)) = cx.w.player.as_ref().and_then(|p| p.last_hurt) { + m.ai.target = Some(who); + self.timestamp = at; + } + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.ai.target = None; + } +} +/// `Wolf.wantsToAttack`: never creepers, ghasts or tame wolves and cats. +fn wants_to_attack(m: &Mob, w: &MobWorld, who: Who) -> bool { + let Who::Mob(id) = who else { + return false; + }; + if id == m.id { + return false; + } + let Some(s) = w.seen(id) else { + return false; + }; + match s.species { + Species::Creeper | Species::Ghast => false, + Species::Wolf | Species::Cat | Species::Parrot => !s.tame, + Species::Horse | Species::Donkey | Species::Mule | Species::Llama => !s.tame, + _ => true, + } +} +/// `BegGoal`: tilt the head at a player holding something interesting. +#[derive(Clone, Debug)] +struct Beg { + food: std::sync::Arc<[String]>, + ticks: i32, +} +impl Beg { + fn interesting(&self, m: &Mob, w: &MobWorld) -> bool { + let Some(p) = &w.player else { + return false; + }; + let held = |i: &Option>| { + i.as_deref().is_some_and(|i| { + (!m.extra.tame && i == "minecraft:bone") + || (m.extra.tame && self.food.iter().any(|f| f == i)) + }) + }; + p.alive && !p.spectator && (held(&p.main_hand) || held(&p.off_hand)) + } +} +impl Goal for Beg { + goal_clone!(); + fn flags(&self) -> u8 { + LOOK + } + fn can_use(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + cx.w.player + .as_ref() + .is_some_and(|p| m.distance_sqr(p.position) <= 64.0) + && self.interesting(m, cx.w) + } + fn can_continue(&mut self, m: &mut Mob, cx: &mut Cx) -> bool { + cx.w.player + .as_ref() + .is_some_and(|p| p.alive && m.distance_sqr(p.position) <= 64.0) + && self.ticks > 0 + && self.interesting(m, cx.w) + } + fn start(&mut self, m: &mut Mob, _: &mut Cx) { + m.extra.flag = true; + self.ticks = reduced(40 + m.random.next_int_bound(40).unwrap_or(0)); + } + fn stop(&mut self, m: &mut Mob, _: &mut Cx) { + m.extra.flag = false; + } + fn tick(&mut self, m: &mut Mob, cx: &mut Cx) { + if let Some(p) = &cx.w.player { + let max = max_head_x_rot(m); + m.look_at_speed([p.position.x, p.eye[1], p.position.z], 10.0, max); + } + self.ticks -= 1; + } +} +/// `Wolf.tick` / `aiStep`: anger countdown, begging tilt and shaking off water. +pub fn ai_step(m: &mut Mob, w: &MobWorld, events: &mut Vec) { + if m.extra.anger > 0 { + m.extra.anger -= 1; + if m.extra.anger == 0 && m.ai.target == Some(Who::Player) { + m.ai.target = None; + } + } + if !m.alive() { + return; + } + // interestedAngle eases toward 1 while begging. + let goal = if m.extra.flag { 1.0 } else { 0.0 }; + let v = &mut m.anim.v; + v[INTEREST] += (goal - v[INTEREST]) * 0.4; + // isWet (extra.flag2), isShaking (counter2 > 0). + let rain = w.raining + && w.can_see_sky( + m.position.x.floor() as i32, + (m.position.y + f64::from(m.height())).floor() as i32, + m.position.z.floor() as i32, + ); + let shaking = m.extra.counter2 > 0; + if m.fluids.in_water || rain { + m.extra.flag2 = true; + if shaking { + m.extra.counter2 = 0; + m.anim.v[SHAKE] = 0.0; + m.anim.vo[SHAKE] = 0.0; + } + } else if shaking { + if m.anim.v[SHAKE] == 0.0 { + let pitch = (m.random.next_float() - m.random.next_float()) * 0.2 + 1.0; + events.push(MobEvent::Sound("entity.wolf.shake", m.center(), 0.4, pitch)); + } + m.anim.v[SHAKE] += 0.05; + if m.anim.vo[SHAKE] >= 2.0 { + m.extra.flag2 = false; + m.extra.counter2 = 0; + m.anim.v[SHAKE] = 0.0; + m.anim.vo[SHAKE] = 0.0; + } + if m.anim.v[SHAKE] > 0.4 { + let count = (((m.anim.v[SHAKE] - 0.4) * PI).sin() * 7.0) as u32; + if count > 0 { + events.push(MobEvent::Particles { + kind: "splash", + at: [m.position.x, m.position.y + 0.8, m.position.z], + count, + }); + } + } + } else if m.extra.flag2 && m.ai.nav.is_done() && m.on_ground { + // Wolf.aiStep: start shaking when wet, standing and not pathing. + m.extra.counter2 = 1; + m.anim.v[SHAKE] = 0.0; + m.anim.vo[SHAKE] = 0.0; + } +} +/// `Wolf.hurtServer` and `NeutralMob` anger: stop sitting; untamed wolves +/// hit by the player stay angry for 20 to 39 seconds. +pub fn after_hurt(m: &mut Mob, source: &Damage) { + m.extra.sitting = false; + if source.attacker == Some(Who::Player) && !m.extra.tame { + m.extra.anger = 400 + m.random.next_int_bound(380).unwrap_or(0); + } +} +/// `Wolf.getTailAngle`. +fn tail_angle(m: &Mob) -> f32 { + if angry(m) { + 1.539_380_4 + } else if m.extra.tame { + let max = m.attrs.max_health; + let ratio = (max - m.health) / max; + (0.55 - ratio * 0.4) * PI + } else { + PI / 5.0 + } +} +/// `WolfRenderState.getBodyRollAngle`. +fn body_roll(shake: f32, offset: f32) -> f32 { + let p = ((shake + offset) / 1.8).clamp(0.0, 1.0); + (p * PI).sin() * (p * PI * 11.0).sin() * 0.15 * PI +} +/// `WolfModel.setupAnim` with the adult and baby overrides. +pub fn anim(m: &Mob, s: &State, p: &mut Poses) { + let (pos, speed) = (s.pos, s.speed); + let baby = m.is_baby(); + if angry(m) { + p.yrot("tail", 0.0); + } else { + p.yrot("tail", (pos * 0.6662).cos() * 1.4 * speed); + } + if m.extra.sitting { + let age = if baby { 0.5 } else { 1.0 }; + let add = |p: &mut Poses, part: &str, x: f32, y: f32, z: f32| { + let v = p.get(part); + v[0] += x; + v[1] += y; + v[2] += z; + }; + add(p, "body", 0.0, 4.0 * age, -2.0 * age); + p.xrot("body", PI / 4.0); + add(p, "tail", 0.0, 9.0 * age, -2.0 * age); + add(p, "right_hind_leg", 0.0, 6.7 * age, -5.0 * age); + p.xrot("right_hind_leg", PI * 3.0 / 2.0); + add(p, "left_hind_leg", 0.0, 6.7 * age, -5.0 * age); + p.xrot("left_hind_leg", PI * 3.0 / 2.0); + p.xrot("right_front_leg", 5.811_947); + add(p, "right_front_leg", 0.01 * age, 1.0 * age, 0.0); + p.xrot("left_front_leg", 5.811_947); + add(p, "left_front_leg", -0.01 * age, 1.0 * age, 0.0); + if baby { + let x = p.get("body")[3] - PI / 2.0; + p.xrot("body", x); + } else if p.has("upper_body") { + p.get("upper_body")[1] += 2.0; + p.xrot("upper_body", PI * 2.0 / 5.0); + p.yrot("upper_body", 0.0); + } + } else { + p.xrot("right_hind_leg", (pos * 0.6662).cos() * 1.4 * speed); + p.xrot("left_hind_leg", (pos * 0.6662 + PI).cos() * 1.4 * speed); + p.xrot("right_front_leg", (pos * 0.6662 + PI).cos() * 1.4 * speed); + p.xrot("left_front_leg", (pos * 0.6662).cos() * 1.4 * speed); + } + // shakeOffWater + let shake = m.anim.lerp(SHAKE, s.partial); + let roll = m.anim.lerp(INTEREST, s.partial) * 0.15 * PI; + p.zrot("body", body_roll(shake, -0.16)); + if baby { + p.zrot("head", roll + body_roll(shake, 0.0)); + p.zrot("tail", body_roll(shake, -0.2)); + } else { + if p.has("real_head") { + p.zrot("real_head", roll + body_roll(shake, 0.0)); + } + if p.has("upper_body") { + p.zrot("upper_body", body_roll(shake, -0.08)); + } + if p.has("real_tail") { + p.zrot("real_tail", body_roll(shake, -0.2)); + } + } + p.xrot("head", s.x_rot.to_radians()); + p.yrot("head", s.y_rot.to_radians()); + p.xrot("tail", tail_angle(m)); +} +/// `Wolf.getWetShade` as a vertex colour. +pub fn tint(m: &Mob, partial: f32) -> u32 { + if !m.extra.flag2 { + return 0xFFFF_FFFF; + } + let shade = (0.75 + m.anim.lerp(SHAKE, partial) / 2.0 * 0.25).min(1.0); + let c = (shade * 255.0) as u32; + 0xFF00_0000 | c << 16 | c << 8 | c +} +/// `Wolf.getTexture`. +pub fn texture(m: &Mob) -> String { + let file = WOLF_VARIANTS[(m.extra.variant as usize).min(8)].1; + let state = if m.extra.tame { + "_tame" + } else if angry(m) { + "_angry" + } else { + "" + }; + let baby = if m.is_baby() { "_baby" } else { "" }; + format!("entity/wolf/{file}{state}{baby}") +} +/// `Wolf.getMaxHeadXRot`: 20 degrees while sitting. +fn max_head_x_rot(m: &Mob) -> f32 { + if m.extra.sitting { + 20.0 + } else { + m.kind.max_head_x_rot + } +}