diff --git a/crates/horizon-client/src/game/mob_gear.rs b/crates/horizon-client/src/game/mob_gear.rs index 809b250dc..7dc529f17 100644 --- a/crates/horizon-client/src/game/mob_gear.rs +++ b/crates/horizon-client/src/game/mob_gear.rs @@ -5,11 +5,15 @@ use super::Game; use horizon_client::{ mobs::{ Species, + drops::{self, SLOTS}, + finalize::equip_stack, gear::{self, ARMOR, MAIN}, + spawner::equipment_table, }, raid::{self, RaiderType}, }; use horizon_core::random::RandomSource; +use horizon_world::{loot::EntityLootParams, nbt::Tag}; /// `EnchantmentsByCostWithDifficulty` of `mob_spawn_equipment`. const MIN_COST: i32 = 5; @@ -47,6 +51,58 @@ fn slot_item(m: &horizon_client::mobs::Mob, slot: usize) -> Option { + /// `Mob.equip(EquipmentTable)` for a spawner's `equipment`: the loot table + /// (the mob as `this`, its random sequence) is rolled and each stack goes + /// in the slot `EquipmentUser.resolveSlot` picks, with the table's drop + /// chance for that slot. + pub(super) fn equip_from_table(&mut self, id: u32, table: &Tag) { + let Some((name, chances)) = equipment_table(table) else { + return; + }; + let Some(loot) = horizon_client::structure_blocks::loot_data(&self.options.resources) + else { + return; + }; + let Some(m) = self.mobs.iter().find(|m| m.id == id) else { + return; + }; + if !loot.has_table(&name) { + return; + } + let params = EntityLootParams { + this: drops::loot_entity(m, None), + ..EntityLootParams::default() + }; + let seed = self.level.seed; + let stacks = match loot.random_sequence(&name) { + Some(sequence) => { + let random = self.rand_sequences.get(sequence, seed); + loot.roll_entity(&name, random, ¶ms) + } + None => loot.roll_entity(&name, &mut self.world.random, ¶ms), + }; + let mut inserted: Vec = Vec::new(); + for stack in stacks.iter().filter(|s| !s.is_empty()) { + // `resolveSlot`: an `equippable` item goes in its own slot, any + // other in the main hand, each slot once. + let slot = match self + .res + .items() + .get(&stack.item) + .and_then(|i| i.equip_slot.as_deref()) + { + Some(name) => SLOTS.iter().position(|s| *s == name), + None => Some(0), + }; + let Some(slot) = slot.filter(|s| !inserted.contains(s)) else { + continue; + }; + if let Some(m) = self.mobs.iter_mut().find(|m| m.id == id) { + equip_stack(m, slot, stack, chances[slot]); + } + inserted.push(slot); + } + } /// `Mob.populateDefaultEquipmentEnchantments`: the weapon one time in /// four and each piece of armour one time in two, scaled by the local /// difficulty, enchanted as if at an enchanting table of cost 5 to 22. diff --git a/crates/horizon-client/src/game/spawner.rs b/crates/horizon-client/src/game/spawner.rs index a3761cf7a..40085b7fa 100644 --- a/crates/horizon-client/src/game/spawner.rs +++ b/crates/horizon-client/src/game/spawner.rs @@ -21,10 +21,11 @@ type Pos = (i32, i32, i32); /// What a spawner tick decided to add, created once the level is free. pub(super) enum Planned { - /// `finalizeSpawn` ran on a fresh mob. - Finalized(Mob), - /// A mob loaded from the spawn data (with its passengers). - Loaded(Tag), + /// `finalizeSpawn` ran on a fresh mob; the spawn data's `equipment`. + Finalized(Mob, Option), + /// A mob loaded from the spawn data (with its passengers) and its + /// `equipment`. + Loaded(Tag, Option), } /// What the spawner code keeps between ticks. @@ -120,18 +121,24 @@ impl Game<'_> { for (i, plan) in planned.into_iter().enumerate() { let provisional = u32::MAX - i as u32; let id = match plan { - Planned::Finalized(mut mob) => { + Planned::Finalized(mut mob, equipment) => { self.next_entity += 1; mob.id = self.next_entity; let id = mob.id; self.mobs.push(mob); self.enchant_spawn_equipment(id); self.spawn_jockeys(id, true); - Some(id) + Some((id, equipment)) + } + Planned::Loaded(tag, equipment) => { + self.load_mob(tag, None).map(|id| (id, equipment)) } - Planned::Loaded(tag) => self.load_mob(tag, None), }; - if let Some(id) = id { + if let Some((id, equipment)) = id { + // `nextSpawnData.getEquipment().ifPresent(mob::equip)`. + if let Some(table) = equipment { + self.equip_from_table(id, &table); + } ids.insert(provisional, id); } } @@ -169,7 +176,7 @@ impl Game<'_> { mob.previous_head_yaw = 0.0; } let bounds = mob.bounds(); - planned.push(Planned::Finalized(mob)); + planned.push(Planned::Finalized(mob, r.equipment.cloned())); bounds } else { // A UUID already in the level is refused. @@ -197,7 +204,7 @@ impl Game<'_> { let tag = Tag::Compound(tag); let mob = horizon_client::entity_nbt::mob_from_nbt(&tag, self.res.mob_types(), 0)?; let bounds = mob.bounds(); - planned.push(Planned::Loaded(tag)); + planned.push(Planned::Loaded(tag, r.equipment.cloned())); bounds }; l.pending.borrow_mut().push(bounds); diff --git a/crates/horizon-client/src/game/trial_spawner.rs b/crates/horizon-client/src/game/trial_spawner.rs index 42a26c6e4..d076671ea 100644 --- a/crates/horizon-client/src/game/trial_spawner.rs +++ b/crates/horizon-client/src/game/trial_spawner.rs @@ -163,7 +163,7 @@ impl Game<'_> { let mut spawn = |l: &GameLevel, r: &SpawnerSpawn| { let added = l.game.plan_spawner_mob(l, r, &mut planned)?; let uuid = match planned.last_mut() { - Some(Planned::Loaded(Tag::Compound(tag))) => { + Some(Planned::Loaded(Tag::Compound(tag), _)) => { let tag_uuid = horizon_client::entity_nbt::uuid(&Tag::Compound(tag.clone())); tag_uuid.unwrap_or_else(|| { diff --git a/crates/horizon-client/src/mobs/finalize.rs b/crates/horizon-client/src/mobs/finalize.rs index b3532726e..ff819422a 100644 --- a/crates/horizon-client/src/mobs/finalize.rs +++ b/crates/horizon-client/src/mobs/finalize.rs @@ -898,6 +898,28 @@ pub fn slot_enchantments(m: &Mob, slot: usize) -> gear::Levels { pub fn set_slot_enchantments(m: &mut Mob, slot: usize, levels: gear::Levels) { gear::set(m, gear_slot(slot), levels); } +/// `EquipmentUser.equip` for one rolled stack: `setItemSlot` puts the item in +/// `slot` with its own enchantments, then the slot's drop chance is stored +/// when the table names one. +pub fn equip_stack( + m: &mut Mob, + slot: usize, + stack: &horizon_world::loot::LootStack, + drop_chance: Option, +) { + set_item(m, slot, &stack.item); + if slot <= HEAD { + let levels = stack + .enchantments + .iter() + .map(|(id, level)| (Arc::from(id.as_str()), (*level).max(0) as u32)) + .collect(); + gear::set(m, gear_slot(slot), levels); + } + if let Some(chance) = drop_chance { + set_drop_chance(m, slot, chance); + } +} /// `Mob.setDropChance`. pub fn set_drop_chance(m: &mut Mob, slot: usize, chance: f32) { let c = m diff --git a/crates/horizon-client/src/mobs/spawner.rs b/crates/horizon-client/src/mobs/spawner.rs index 78b091b7b..f5e633226 100644 --- a/crates/horizon-client/src/mobs/spawner.rs +++ b/crates/horizon-client/src/mobs/spawner.rs @@ -152,6 +152,34 @@ pub(crate) fn cube_scale(id: &str, entity: &Compound) -> f32 { .map_or(1, |size| (size as i32).saturating_add(1).clamp(1, 127)) as f32 } +/// `EquipmentTable.CODEC`: the loot table and the drop chance of each +/// `EquipmentSlot` (`drops::SLOTS` order) the table names, `slot_drop_chances` +/// being one number for every slot or a map by slot name. +pub fn equipment_table(tag: &Tag) -> Option<(String, [Option; 8])> { + let chance = |t: &Tag| match t { + Tag::Float(v) => Some(f32::from_bits(*v)), + Tag::Double(v) => Some(f64::from_bits(*v) as f32), + Tag::Byte(v) => Some(f32::from(*v)), + Tag::Short(v) => Some(f32::from(*v)), + Tag::Int(v) => Some(*v as f32), + Tag::Long(v) => Some(*v as f32), + _ => None, + }; + let Some(Tag::String(table)) = tag.get("loot_table") else { + return None; + }; + let mut chances = [None; 8]; + match tag.get("slot_drop_chances") { + Some(Tag::Compound(map)) => { + for (i, slot) in super::drops::SLOTS.iter().enumerate() { + chances[i] = map.get(&JavaString::from(*slot)).and_then(chance); + } + } + Some(all) => chances = [Some(chance(all)?); 8], + None => {} + } + Some((table.to_string_lossy(), chances)) +} /// A whole number from any numeric tag (`NumericTag.intValue`: floats floor). pub(crate) fn number(tag: Option<&Tag>) -> Option { Some(match tag? {