Merge pull request #2376 from kacpherek/fix/trial-mob-equipment

🤖 Agent: Merged by Ludovic's AI agent (Claude, contrib-gate-19) after the contribution gate. Checked against vanilla 26.3 and proven in union/cg19-r2 (all round-2 PRs together: clippy, 1722 tests, 135 harnesses, lan_e2e in-frame and on the server thread) and union/cg19-r3 (the same PRs merged with today's main: 96/96 harnesses). Thanks @kacpherek.
This commit is contained in:
Ludovic Marie
2026-10-27 17:35:46 +01:00
committed by GitHub
5 changed files with 124 additions and 11 deletions
@@ -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<std::sync::Ar
}
impl Game<'_> {
/// `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, &params)
}
None => loot.roll_entity(&name, &mut self.world.random, &params),
};
let mut inserted: Vec<usize> = 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.
+17 -10
View File
@@ -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<Tag>),
/// A mob loaded from the spawn data (with its passengers) and its
/// `equipment`.
Loaded(Tag, Option<Tag>),
}
/// 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);
@@ -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(|| {
@@ -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<f32>,
) {
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
+28
View File
@@ -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<f32>; 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<i64> {
Some(match tag? {