diff --git a/crates/horizon-client/src/entity_nbt.rs b/crates/horizon-client/src/entity_nbt.rs index b96038124..d0d50d2c0 100644 --- a/crates/horizon-client/src/entity_nbt.rs +++ b/crates/horizon-client/src/entity_nbt.rs @@ -715,6 +715,17 @@ pub fn mob_to_nbt(m: &Mob, uuid: [i32; 4], raw: Option<&Tag>) -> Tag { equipment.insert(slot.into(), crate::save::item_to_nbt(&stack)); continue; } + // A wolf's armor keeps the durability it lost. + if slot == "body" + && m.species() == crate::mobs::Species::Wolf + && let Some(id) = item + { + let mut stack = crate::items::ItemStack::new(id, 1); + stack.damage = m.extra.body_damage.max(0) as u32; + stack.extra = m.extra.body_extra.clone(); + equipment.insert(slot.into(), crate::save::item_to_nbt(&stack)); + continue; + } match item { Some(id) => { let keep = matches!( @@ -1121,6 +1132,12 @@ pub fn mob_from_nbt(tag: &Tag, types: &HashMap>, id: u32) - crate::mobs::tame::set_tame(&mut m, tame); m.health = health; m.extra.sitting = flag(tag, "Sitting"); + let armor = tag + .get("equipment") + .and_then(|e| e.get("body")) + .and_then(crate::save::item_from_nbt); + m.extra.body_damage = armor.as_ref().map_or(0, |s| s.damage as i32); + m.extra.body_extra = armor.and_then(|s| s.extra); m.extra.color = number(tag.get("CollarColor")).map_or(crate::mobs::tame::RED, |c| (c as u8) & 15); } diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index c133a0927..5cdec36fb 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -3328,12 +3328,18 @@ impl Game<'_> { .get(&item) .is_some_and(|i| i.max_damage > 0); let acting = self.acting_player_uuid(); + let sneaking = self.keys.contains(&winit::keyboard::KeyCode::ShiftLeft) + || self.keys.contains(&winit::keyboard::KeyCode::ShiftRight); + let held_damage = held.as_ref().map_or(0, |h| h.damage as i32); + let held_extra = held.as_ref().and_then(|h| h.extra.clone()); let m = &mut self.mobs[index]; if !m.alive() { return Ok(false); } let species = m.species(); let center = m.center(); + // Wolf armor the owner takes off with shears or leaves a dropped stack of. + let mut armor_drop: Option = None; let mut consume = false; let mut give: Option<&str> = None; let mut sound: Option<&str> = None; @@ -3418,6 +3424,23 @@ impl Game<'_> { handled = false; } } + // Entity.interact: shears take a tamed wolf's armor off for its owner. + Species::Wolf + if item == "minecraft:shears" + && m.extra.tame + && m.extra.owner == Some(acting) + && m.extra.head.is_some() + && !sneaking => + { + let mut stack = ItemStack::new("minecraft:wolf_armor", 1); + stack.damage = m.extra.body_damage.max(0) as u32; + stack.extra = m.extra.body_extra.take(); + armor_drop = Some(stack); + m.extra.head = None; + m.extra.body_damage = 0; + eat = Some(("item.armor.unequip_wolf", 1.0, 1.0)); + self.damage_held_tool(); + } // 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 => { @@ -3432,6 +3455,36 @@ impl Game<'_> { m.health = (m.health + heal).min(m.attrs.max_health); consume = true; } + // Wolf.mobInteract: the owner puts wolf armor on a tamed adult. + Species::Wolf + if item == "minecraft:wolf_armor" + && m.extra.tame + && m.extra.owner == Some(acting) + && m.extra.head.is_none() + && !m.is_baby() => + { + m.extra.head = Some(item.as_str().into()); + m.extra.body_damage = held_damage; + m.extra.body_extra = held_extra; + consume = true; + eat = Some(("item.armor.equip_wolf", 1.0, 1.0)); + } + // Wolf.mobInteract: an armadillo scute repairs a sitting wolf's + // armor by an eighth of its durability. + Species::Wolf + if item == "minecraft:armadillo_scute" + && m.extra.tame + && m.extra.owner == Some(acting) + && m.extra.sitting + && m.extra.head.is_some() + && m.extra.body_damage > 0 => + { + m.extra.body_damage = (m.extra.body_damage + - (horizon_client::mobs::WOLF_ARMOR_DURABILITY as f32 * 0.125) as i32) + .max(0); + consume = true; + eat = Some(("item.wolf_armor.repair", 1.0, 1.0)); + } Species::Wolf if m.extra.tame && m.extra.owner == Some(acting) @@ -3608,7 +3661,7 @@ impl Game<'_> { let tamed = m.random.next_int_bound(10).unwrap_or(1) == 0; if tamed { m.extra.tame = true; - m.extra.owner = self.level.player_uuid.or(Some([0, 0, 0, 1])); + m.extra.owner = Some(acting); } // TamableAnimal.spawnTamingParticles (entity event 7 or 6). self.mob_event_particles(self.mobs[index].id, if tamed { 7 } else { 6 }); @@ -3728,6 +3781,9 @@ impl Game<'_> { self.spawn_item(rest, at, [0.0, 0.0, 0.0], 40); } } + if let Some(stack) = armor_drop { + self.spawn_item(stack, [center[0], center[1] + 0.5, center[2]], [0.0; 3], 10); + } for (item, count) in drops { for _ in 0..count { let v = [ diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 7b687d8bc..1819f4e52 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -794,8 +794,17 @@ fn mob_equipment( let stack = ItemStack { item: item.clone(), count: 1, - damage: 0, - extra: None, + // A wolf's armor shows its cracks. + damage: if slot == 6 && m.species() == horizon_client::mobs::Species::Wolf { + e.body_damage.max(0) as u32 + } else { + 0 + }, + extra: if slot == 6 && m.species() == horizon_client::mobs::Species::Wolf { + e.body_extra.clone() + } else { + None + }, }; out.push((slot, hub.stack(&Some(stack)))); } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index fbe955dd0..86aa646a4 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -2191,7 +2191,11 @@ impl Game<'_> { 3 => e.legs = item, 4 => e.chest = item, 5 if !body => e.head = item, - 6 if body => e.head = item, + 6 if body => { + // A wolf's armor shows its cracks. + e.body_damage = stack.as_ref().map_or(0, |s| s.damage as i32); + e.head = item; + } 7 => m.set_saddle(stack), _ => {} } diff --git a/crates/horizon-client/src/mobs.rs b/crates/horizon-client/src/mobs.rs index 48df8b9a4..32ef6f7e5 100644 --- a/crates/horizon-client/src/mobs.rs +++ b/crates/horizon-client/src/mobs.rs @@ -666,6 +666,8 @@ pub struct Attrs { pub spawn_reinforcements: f64, pub jump_strength: f64, } +/// `minecraft:wolf_armor`'s maximum damage. +pub const WOLF_ARMOR_DURABILITY: i32 = 64; /// Species-specific persistent state (variants, colours, taming, ageing...). #[derive(Clone, Debug, Default)] pub struct Extra { @@ -699,6 +701,10 @@ pub struct Extra { /// `Mob.canPickUpLoot` (also lets dispensers equip it). pub pick_up_loot: bool, pub saddled: bool, + /// The durability a wolf's body armor (`extra.head`) has lost. + pub body_damage: i32, + /// The components (dye, name...) of that armor stack. + pub body_extra: Option>, /// The saddle's components (name, enchantments...) when it is not a bare /// one; `saddled` stays the gameplay flag. pub saddle_stack: Option, @@ -2046,18 +2052,29 @@ impl Mob { if epf > 0.0 { amount *= 1.0 - epf / 25.0; } + // Wolf.actuallyHurt: wolf armor takes the damage instead of the wolf. + let absorbed = self.wolf_armor_absorbs(&source); let mut knock = true; if self.invulnerable as f32 > 10.0 { if amount <= self.last_hurt { return false; } - self.health -= amount - self.last_hurt; + let dealt = amount - self.last_hurt; + if absorbed { + self.hurt_wolf_armor(dealt); + } else { + self.health -= dealt; + } self.last_hurt = amount; knock = false; } else { self.last_hurt = amount; self.invulnerable = 20; - self.health -= amount; + if absorbed { + self.hurt_wolf_armor(amount); + } else { + self.health -= amount; + } self.hurt_time = 10; } // Animal.actuallyHurt: damage taken ends love mode (`resetLove`). @@ -2088,7 +2105,12 @@ impl Mob { // playHurtSound (Mob: also resets the ambient sound clock) or the // death sound: makeSound draws the voice pitch whenever there is a sound. let dead = !self.alive(); - let sound = ai::voice_sounds(self)[if dead { 2 } else { 1 }]; + // Wolf.getHurtSound: the armor's own sound when it takes the hit. + let sound = if absorbed { + "item.wolf_armor.damage" + } else { + ai::voice_sounds(self)[if dead { 2 } else { 1 }] + }; if !dead { self.ambient_timer = -self.ambient_interval(); } @@ -2112,6 +2134,46 @@ impl Mob { } true } + /// `Wolf.canArmorAbsorb`: it wears wolf armor and the damage type is not + /// in `#bypasses_wolf_armor` (drowning, magic, thorns, in a wall, out of + /// the world and the untyped kills). + fn wolf_armor_absorbs(&self, source: &Damage) -> bool { + self.species() == Species::Wolf + && self.extra.head.as_deref() == Some("minecraft:wolf_armor") + && !matches!( + source.kind, + DamageKind::Drown + | DamageKind::Magic + | DamageKind::InWall + | DamageKind::Thorns + | DamageKind::OutOfWorld + | DamageKind::Generic + ) + } + /// `Wolf.actuallyHurt`: the armor loses `ceil(damage)` durability, cracks + /// at `Crackiness.WOLF_ARMOR` steps and breaks at its maximum. + fn hurt_wolf_armor(&mut self, damage: f32) { + let crack = |lost: i32| { + let left = (WOLF_ARMOR_DURABILITY - lost) as f32 / WOLF_ARMOR_DURABILITY as f32; + [0.95_f32, 0.69, 0.32].iter().filter(|t| left < **t).count() + }; + let before = self.extra.body_damage; + let after = before + damage.ceil() as i32; + let at = [self.position.x, self.position.y, self.position.z]; + if after >= WOLF_ARMOR_DURABILITY { + self.extra.head = None; + self.extra.body_damage = 0; + self.extra.body_extra = None; + self.outbox + .push(MobEvent::Sound("item.wolf_armor.break", at, 1.0, 1.0)); + } else { + self.extra.body_damage = after; + if crack(before) != crack(after) { + self.outbox + .push(MobEvent::Sound("item.wolf_armor.crack", at, 1.0, 1.0)); + } + } + } /// LivingEntity.knockback(power, xd, zd) towards the negative direction. pub fn knockback(&mut self, power: f64, mut xd: f64, mut zd: f64) { let power = power * (1.0 - self.attrs.knockback_resistance); diff --git a/crates/horizon-client/src/mobs/drops.rs b/crates/horizon-client/src/mobs/drops.rs index e20f991c1..d0159a619 100644 --- a/crates/horizon-client/src/mobs/drops.rs +++ b/crates/horizon-client/src/mobs/drops.rs @@ -251,6 +251,9 @@ pub fn slot_item(m: &Mob, slot: &str) -> Option { 1 }; let mut stack = loot_stack(item?, count as i32); + if slot == "body" && m.species() == Species::Wolf { + stack.damage = m.extra.body_damage; + } if let (Some(index), Some(g)) = (index, gear::gear(m)) { for (e, l) in &g.slots[index] { stack.enchantments.push((e.to_string(), *l as i32)); @@ -288,6 +291,8 @@ pub fn clear_slot(m: &mut Mob, slot: &str) { } "body" if body => { m.extra.head = None; + m.extra.body_damage = 0; + m.extra.body_extra = None; return; } "saddle" => { diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index 1d83488be..d02c3a172 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -439,13 +439,37 @@ pub fn layers(m: &Mob) -> Vec { }) }; match m.species() { - // WolfCollarLayer. + // WolfArmorLayer, then 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") }; + // The armor, then the cracks of its damage (`Crackiness.WOLF_ARMOR`). + if !m.is_baby() && m.extra.head.as_deref() == Some("minecraft:wolf_armor") { + let left = (super::WOLF_ARMOR_DURABILITY - m.extra.body_damage) as f32 + / super::WOLF_ARMOR_DURABILITY as f32; + push( + &mut v, + "wolf_armor#main", + "entity/equipment/wolf_body/armadillo_scute", + white, + false, + ); + let crack = [(0.32, "high"), (0.69, "medium"), (0.95, "low")] + .iter() + .find(|(t, _)| left < *t); + if let Some((_, level)) = crack { + push( + &mut v, + "wolf_armor#main", + &format!("entity/wolf/wolf_armor_crackiness_{level}"), + white, + false, + ); + } + } push( &mut v, model, diff --git a/crates/horizon-client/src/mobs/species.rs b/crates/horizon-client/src/mobs/species.rs index 64106574b..201632706 100644 --- a/crates/horizon-client/src/mobs/species.rs +++ b/crates/horizon-client/src/mobs/species.rs @@ -1699,10 +1699,15 @@ impl Def { } } /// Mobs whose `body` equipment slot Horizon keeps in `Extra::head` (a -/// happy ghast's harness, nautilus armour, a sulfur cube's swallowed block). +/// happy ghast's harness, nautilus armour, a sulfur cube's swallowed block, +/// a wolf's armor). pub fn body_slot(s: Species) -> bool { matches!( s, - Species::HappyGhast | Species::Nautilus | Species::ZombieNautilus | Species::SulfurCube + Species::HappyGhast + | Species::Nautilus + | Species::ZombieNautilus + | Species::SulfurCube + | Species::Wolf ) } diff --git a/tools/test_entity_death_loot_parity.py b/tools/test_entity_death_loot_parity.py index 2bb53ebbb..39aa6edda 100644 --- a/tools/test_entity_death_loot_parity.py +++ b/tools/test_entity_death_loot_parity.py @@ -31,7 +31,7 @@ SKIPPED = {'player', 'mannequin', 'armor_stand', 'ender_dragon', 'giant'} # Types Horizon keeps body armour for in the head slot: their corpus wears # body items instead of helmets. BODY = {'horse', 'donkey', 'mule', 'skeleton_horse', 'zombie_horse', 'camel', 'camel_husk', 'llama', 'trader_llama', - 'happy_ghast', 'nautilus', 'zombie_nautilus', 'sulfur_cube'} + 'happy_ghast', 'nautilus', 'zombie_nautilus', 'sulfur_cube', 'wolf'} WEAPONS = ['minecraft:iron_sword', 'minecraft:diamond_sword;looting:1', 'minecraft:diamond_sword;looting:2', 'minecraft:netherite_sword;looting:3', 'minecraft:golden_sword;fire_aspect:2', 'minecraft:diamond_axe;looting:3;fire_aspect:1'] ARMOUR = {'head': 'minecraft:iron_helmet', 'chest': 'minecraft:chainmail_chestplate', 'legs': 'minecraft:golden_leggings',