8839f0bc72
🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked against 26.3: `Chicken.aiStep` lays through `dropFromGiftLootTable(gameplay/chicken_lay)` with the chicken as `this`, which the table's conditions need, and snow and chorus flowers drop their loot when a player breaks them. One difference is left for later: an explosion's drops don't pass the exploding entity as `this` yet (snow broken by TNT). Gate: this wave's 55 PRs merged on main in one tree: clippy `-D warnings` clean, 1186 horizon-client and 163 horizon-world tests pass, and of the 101 harnesses `parity.py --changed-files` picked, 100 pass (`block_removal` failed on one spawner case from #2199's orb merging, fixed there). `lan_e2e.py` passes 24 checks with the host on its server thread and in-frame (chest, xp, pvp, doors, villagers, beds, frames, projectiles, arrows, fishing, damage, mobs, blast, drops, elytra, breath, predict, cracks, barter, kills, hurt, contacts, underwater, plates), plus forced-levels on the server thread. Thanks @kacpherek!
1546 lines
59 KiB
Rust
1546 lines
59 KiB
Rust
//! Loot tables: `LootTable.fill` for chest-style loot tables (pools,
|
|
//! weighted entries, nested tables, item modifiers and conditions, the
|
|
//! stack splitter and the slot shuffle) and `LootTable.getRandomItems` for
|
|
//! entity loot (the victim, its killer, the damage source and the last
|
|
//! player to hurt it), with vanilla's random draw order.
|
|
//!
|
|
//! Items keep their id, count, damage, enchantments and potion. Exploration
|
|
//! maps are kept as found (structure search is not run); conditions that
|
|
//! need an entity, tool or damage source fail when the context has none, as
|
|
//! they do for containers.
|
|
use horizon_core::random::{LegacyRandom, RandomSource};
|
|
use serde_json::Value;
|
|
use std::{
|
|
collections::{BTreeMap, HashMap},
|
|
fs, io,
|
|
path::Path,
|
|
};
|
|
|
|
/// One generated item stack.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct LootStack {
|
|
pub item: String,
|
|
pub count: i32,
|
|
pub damage: i32,
|
|
pub enchantments: Vec<(String, i32)>,
|
|
pub potion: Option<String>,
|
|
/// Set by exploration_map (the map_id component of a found map).
|
|
pub map: bool,
|
|
/// The search an exploration_map function asked for, left to the game
|
|
/// (it needs the level's structures and maps).
|
|
pub exploration: Option<Box<ExplorationMap>>,
|
|
/// Set by set_ominous_bottle_amplifier.
|
|
pub ominous_amplifier: Option<i32>,
|
|
/// Added by set_stew_effect (the `suspicious_stew_effects` component):
|
|
/// (effect id, duration in ticks).
|
|
pub stew_effects: Vec<(String, i32)>,
|
|
}
|
|
/// `ExplorationMapFunction`'s settings.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct ExplorationMap {
|
|
/// A structure tag, without `#`.
|
|
pub destination: String,
|
|
pub decoration: String,
|
|
pub zoom: u8,
|
|
pub search_radius: i32,
|
|
pub skip_existing_chunks: bool,
|
|
}
|
|
impl ExplorationMap {
|
|
/// The function's fields with their codec defaults (`DEFAULT_DESTINATION`
|
|
/// on_treasure_maps, `DEFAULT_DECORATION` mansion, zoom 2, radius 50,
|
|
/// skipping known structures).
|
|
pub fn from_json(f: &Value) -> Self {
|
|
Self {
|
|
destination: f["destination"]
|
|
.as_str()
|
|
.unwrap_or("#minecraft:on_treasure_maps")
|
|
.trim_start_matches('#')
|
|
.to_owned(),
|
|
decoration: f["decoration"]
|
|
.as_str()
|
|
.unwrap_or("minecraft:mansion")
|
|
.to_owned(),
|
|
zoom: f["zoom"].as_u64().unwrap_or(2) as u8,
|
|
search_radius: f["search_radius"].as_i64().unwrap_or(50) as i32,
|
|
skip_existing_chunks: f["skip_existing_chunks"].as_bool().unwrap_or(true),
|
|
}
|
|
}
|
|
}
|
|
impl LootStack {
|
|
fn new(item: &str) -> Self {
|
|
Self {
|
|
item: item.to_owned(),
|
|
count: 1,
|
|
damage: 0,
|
|
enchantments: Vec::new(),
|
|
potion: None,
|
|
map: false,
|
|
exploration: None,
|
|
ominous_amplifier: None,
|
|
stew_effects: Vec::new(),
|
|
}
|
|
}
|
|
pub fn is_empty(&self) -> bool {
|
|
self.count <= 0 || self.item == "minecraft:air"
|
|
}
|
|
/// `ItemStack.getCount`: 0 for an empty stack.
|
|
pub fn get_count(&self) -> i32 {
|
|
if self.is_empty() { 0 } else { self.count }
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
struct ItemFacts {
|
|
max_stack: i32,
|
|
max_damage: i32,
|
|
enchantable: Option<i32>,
|
|
}
|
|
#[derive(Debug)]
|
|
struct Enchantment {
|
|
name: String,
|
|
supported: Value,
|
|
primary: Option<Value>,
|
|
exclusive: Value,
|
|
max_level: i32,
|
|
weight: i32,
|
|
min_cost: (i32, i32),
|
|
max_cost: (i32, i32),
|
|
/// `slots`: the equipment slot groups the enchantment works in.
|
|
slots: Vec<String>,
|
|
/// `effects` (equipment_drops and others).
|
|
effects: Value,
|
|
}
|
|
impl Enchantment {
|
|
fn min_cost(&self, level: i32) -> i32 {
|
|
self.min_cost.0 + self.min_cost.1 * (level - 1)
|
|
}
|
|
fn max_cost(&self, level: i32) -> i32 {
|
|
self.max_cost.0 + self.max_cost.1 * (level - 1)
|
|
}
|
|
}
|
|
|
|
/// Loot tables, items, enchantments and the tags they reference.
|
|
pub struct LootData {
|
|
tables: HashMap<String, Value>,
|
|
modifiers: HashMap<String, Value>,
|
|
predicates: HashMap<String, Value>,
|
|
items: HashMap<String, ItemFacts>,
|
|
/// Enchantments in registry (identifier) order.
|
|
enchantments: Vec<Enchantment>,
|
|
/// Raw tag files by (registry, tag id).
|
|
tags: HashMap<(String, String), Vec<Value>>,
|
|
/// Smelting recipes in recipe id order: (ingredient, result, result count).
|
|
smelting: Vec<(Value, String, i32)>,
|
|
}
|
|
|
|
/// An entity a loot context names (`this`, `attacker`, `direct_attacker`,
|
|
/// `attacking_player`): the facts entity predicates read.
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct LootEntity {
|
|
/// Entity type id (`minecraft:zombie`).
|
|
pub kind: String,
|
|
/// A `LivingEntity` (has equipment and enchantment levels).
|
|
pub living: bool,
|
|
/// `Entity.isOnFire` (fire-immune entities never are).
|
|
pub on_fire: bool,
|
|
pub baby: bool,
|
|
/// The entity it rides (`getVehicle`).
|
|
pub vehicle: Option<Box<LootEntity>>,
|
|
/// `AbstractCubeMob.getSize`.
|
|
pub cube_size: Option<i32>,
|
|
/// A sheep's colour name and whether it is sheared.
|
|
pub sheep: Option<(String, bool)>,
|
|
/// A raider: whether it is a captain (wears the ominous banner).
|
|
pub captain: Option<bool>,
|
|
/// A frog's variant id (`minecraft:warm`).
|
|
pub frog_variant: Option<String>,
|
|
/// A chicken's variant id (`minecraft:warm`).
|
|
pub chicken_variant: Option<String>,
|
|
/// A charged creeper (`Creeper.isPowered`).
|
|
pub powered: bool,
|
|
/// Worn and held items by `EquipmentSlot` name (mainhand, offhand,
|
|
/// feet, legs, chest, head, body, saddle).
|
|
pub equipment: Vec<(String, LootStack)>,
|
|
}
|
|
impl LootEntity {
|
|
pub fn new(kind: &str) -> Self {
|
|
Self {
|
|
kind: kind.to_owned(),
|
|
living: true,
|
|
..Self::default()
|
|
}
|
|
}
|
|
pub fn item(&self, slot: &str) -> Option<&LootStack> {
|
|
self.equipment
|
|
.iter()
|
|
.find(|(s, i)| s == slot && !i.is_empty())
|
|
.map(|(_, i)| i)
|
|
}
|
|
}
|
|
/// A `DamageSource`: its damage type and the entities behind it.
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct LootDamage {
|
|
/// Damage type id (`minecraft:player_attack`).
|
|
pub kind: String,
|
|
/// `getEntity`: the attacker (the shooter of a projectile).
|
|
pub source: Option<LootEntity>,
|
|
/// `getDirectEntity`: the projectile, or the attacker itself.
|
|
pub direct: Option<LootEntity>,
|
|
}
|
|
/// The parameters of `LivingEntity.dropFromLootTable`.
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct EntityLootParams {
|
|
pub this: LootEntity,
|
|
pub damage: Option<LootDamage>,
|
|
/// `LAST_DAMAGE_PLAYER`: set when a player killed the entity.
|
|
pub player: Option<LootEntity>,
|
|
/// The killing player's luck.
|
|
pub luck: f32,
|
|
}
|
|
/// `EquipmentSlot.VALUES` order.
|
|
pub const EQUIPMENT_SLOTS: [&str; 8] = [
|
|
"mainhand", "offhand", "feet", "legs", "chest", "head", "body", "saddle",
|
|
];
|
|
/// `EquipmentSlotGroup.test`.
|
|
fn slot_in_group(slot: &str, group: &str) -> bool {
|
|
match group {
|
|
"any" => true,
|
|
"hand" => slot == "mainhand" || slot == "offhand",
|
|
"armor" => matches!(slot, "feet" | "legs" | "chest" | "head" | "body"),
|
|
_ => slot == group,
|
|
}
|
|
}
|
|
/// `LevelBasedValue.calculate`.
|
|
pub fn level_value(v: &Value, level: i32) -> f32 {
|
|
match v {
|
|
Value::Number(n) => n.as_f64().unwrap_or(0.) as f32,
|
|
Value::Object(_) => match v["type"].as_str().unwrap_or("") {
|
|
"minecraft:linear" => {
|
|
level_value(&v["base"], level)
|
|
+ level_value(&v["per_level_above_first"], level) * (level - 1) as f32
|
|
}
|
|
"minecraft:constant" => level_value(&v["value"], level),
|
|
"minecraft:clamped" => {
|
|
let value = level_value(&v["value"], level);
|
|
let min = v["min"].as_f64().unwrap_or(0.) as f32;
|
|
let max = v["max"].as_f64().unwrap_or(0.) as f32;
|
|
value.clamp(min, max)
|
|
}
|
|
"minecraft:fraction" => {
|
|
level_value(&v["numerator"], level) / level_value(&v["denominator"], level)
|
|
}
|
|
"minecraft:levels_squared" => {
|
|
(level * level) as f32 + v["added"].as_f64().unwrap_or(0.) as f32
|
|
}
|
|
"minecraft:lookup" => {
|
|
let values = v["values"].as_array().map_or(&[][..], Vec::as_slice);
|
|
match values.get((level - 1).max(0) as usize) {
|
|
Some(x) if level >= 1 => x.as_f64().unwrap_or(0.) as f32,
|
|
_ => level_value(&v["fallback"], level),
|
|
}
|
|
}
|
|
_ => 0.,
|
|
},
|
|
_ => 0.,
|
|
}
|
|
}
|
|
/// `MinMaxBounds.Ints.matches` (a number or `{min, max}`).
|
|
fn int_bounds(v: &Value, value: i32) -> bool {
|
|
match v {
|
|
Value::Null => true,
|
|
Value::Number(n) => n.as_i64() == Some(i64::from(value)),
|
|
Value::Object(o) => {
|
|
o.get("min")
|
|
.and_then(Value::as_i64)
|
|
.is_none_or(|m| i64::from(value) >= m)
|
|
&& o.get("max")
|
|
.and_then(Value::as_i64)
|
|
.is_none_or(|m| i64::from(value) <= m)
|
|
}
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// A JSON number read by an int codec (`Number.intValue`): `3.0`, which
|
|
/// 1.21.1 mods write for rolls and counts, is 3.
|
|
fn int_value(v: &Value) -> Option<i64> {
|
|
v.as_i64().or_else(|| v.as_f64().map(|f| f as i64))
|
|
}
|
|
|
|
/// `data/<ns>/<kind>/` of every namespace, as `ns:path`: vanilla's, and in
|
|
/// a content mods' root theirs (docs/modpacks.md), whose chests name their
|
|
/// own loot tables.
|
|
fn load_kind(data: &Path, kind: &str, out: &mut HashMap<String, Value>) -> io::Result<()> {
|
|
let Ok(entries) = fs::read_dir(data) else {
|
|
return Ok(());
|
|
};
|
|
let mut namespaces: Vec<String> = entries
|
|
.flatten()
|
|
.filter(|e| e.path().is_dir())
|
|
.filter_map(|e| e.file_name().to_str().map(str::to_owned))
|
|
.collect();
|
|
namespaces.sort();
|
|
for ns in namespaces {
|
|
load_dir(&data.join(&ns).join(kind), &ns, "", out)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn load_dir(
|
|
dir: &Path,
|
|
namespace: &str,
|
|
prefix: &str,
|
|
out: &mut HashMap<String, Value>,
|
|
) -> io::Result<()> {
|
|
let Ok(entries) = fs::read_dir(dir) else {
|
|
return Ok(());
|
|
};
|
|
for entry in entries.flatten() {
|
|
let path = entry.path();
|
|
let Some(stem) = path.file_stem().and_then(|s| s.to_str()) else {
|
|
continue;
|
|
};
|
|
if path.is_dir() {
|
|
load_dir(&path, namespace, &format!("{prefix}{stem}/"), out)?;
|
|
} else if path.extension().is_some_and(|e| e == "json") {
|
|
let value = serde_json::from_slice(&fs::read(&path)?)?;
|
|
out.insert(format!("{namespace}:{prefix}{stem}"), value);
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
fn cost(v: &Value) -> (i32, i32) {
|
|
(
|
|
v["base"].as_i64().unwrap_or(0) as i32,
|
|
v["per_level_above_first"].as_i64().unwrap_or(0) as i32,
|
|
)
|
|
}
|
|
|
|
impl LootData {
|
|
/// Load from a resource root holding `items.json` and `data/`: loot
|
|
/// tables, item modifiers, predicates, tags and recipes of every
|
|
/// namespace, vanilla's enchantments.
|
|
pub fn load(root: &Path) -> io::Result<Self> {
|
|
let all = root.join("data");
|
|
let data = all.join("minecraft");
|
|
let mut tables = HashMap::new();
|
|
load_kind(&all, "loot_table", &mut tables)?;
|
|
let mut modifiers = HashMap::new();
|
|
load_kind(&all, "item_modifier", &mut modifiers)?;
|
|
let mut predicates = HashMap::new();
|
|
load_kind(&all, "predicate", &mut predicates)?;
|
|
let raw: BTreeMap<String, Value> =
|
|
serde_json::from_slice(&fs::read(root.join("items.json"))?)?;
|
|
let items = raw
|
|
.iter()
|
|
.map(|(name, c)| {
|
|
(
|
|
name.clone(),
|
|
ItemFacts {
|
|
max_stack: c["minecraft:max_stack_size"].as_i64().unwrap_or(64) as i32,
|
|
max_damage: c["minecraft:max_damage"].as_i64().unwrap_or(0) as i32,
|
|
enchantable: c["minecraft:enchantable"]["value"]
|
|
.as_i64()
|
|
.map(|v| v as i32),
|
|
},
|
|
)
|
|
})
|
|
.collect();
|
|
let mut raw_enchantments = HashMap::new();
|
|
load_dir(
|
|
&data.join("enchantment"),
|
|
"minecraft",
|
|
"",
|
|
&mut raw_enchantments,
|
|
)?;
|
|
let mut names: Vec<&String> = raw_enchantments.keys().collect();
|
|
names.sort();
|
|
let enchantments = names
|
|
.into_iter()
|
|
.map(|name| {
|
|
let e = &raw_enchantments[name];
|
|
Enchantment {
|
|
name: name.clone(),
|
|
supported: e["supported_items"].clone(),
|
|
primary: e.get("primary_items").cloned(),
|
|
exclusive: e.get("exclusive_set").cloned().unwrap_or(Value::Null),
|
|
max_level: e["max_level"].as_i64().unwrap_or(1) as i32,
|
|
weight: e["weight"].as_i64().unwrap_or(1) as i32,
|
|
min_cost: cost(&e["min_cost"]),
|
|
max_cost: cost(&e["max_cost"]),
|
|
slots: e["slots"]
|
|
.as_array()
|
|
.into_iter()
|
|
.flatten()
|
|
.filter_map(Value::as_str)
|
|
.map(str::to_owned)
|
|
.collect(),
|
|
effects: e["effects"].clone(),
|
|
}
|
|
})
|
|
.collect();
|
|
let mut tags = HashMap::new();
|
|
for registry in [
|
|
"item",
|
|
"enchantment",
|
|
"instrument",
|
|
"entity_type",
|
|
"damage_type",
|
|
] {
|
|
let mut raw = HashMap::new();
|
|
load_kind(&all, &format!("tags/{registry}"), &mut raw)?;
|
|
for (name, value) in raw {
|
|
let values = value["values"].as_array().cloned().unwrap_or_default();
|
|
tags.insert((registry.to_owned(), name), values);
|
|
}
|
|
}
|
|
let mut recipes = HashMap::new();
|
|
load_kind(&all, "recipe", &mut recipes)?;
|
|
let mut ids: Vec<&String> = recipes.keys().collect();
|
|
ids.sort();
|
|
let smelting = ids
|
|
.into_iter()
|
|
.map(|id| &recipes[id])
|
|
.filter(|r| r["type"] == "minecraft:smelting")
|
|
.filter_map(|r| {
|
|
let result = r["result"]["id"].as_str()?.to_owned();
|
|
let count = r["result"]["count"].as_i64().unwrap_or(1) as i32;
|
|
Some((r["ingredient"].clone(), result, count))
|
|
})
|
|
.collect();
|
|
Ok(Self {
|
|
tables,
|
|
modifiers,
|
|
predicates,
|
|
items,
|
|
enchantments,
|
|
tags,
|
|
smelting,
|
|
})
|
|
}
|
|
pub fn has_table(&self, table: &str) -> bool {
|
|
self.tables.contains_key(table)
|
|
}
|
|
/// The table's `random_sequence` (where `getRandomItems` draws from).
|
|
pub fn random_sequence(&self, table: &str) -> Option<&str> {
|
|
self.tables.get(table)?["random_sequence"].as_str()
|
|
}
|
|
pub fn max_stack(&self, item: &str) -> i32 {
|
|
self.facts(item).max_stack
|
|
}
|
|
pub fn max_damage(&self, item: &str) -> i32 {
|
|
self.facts(item).max_damage
|
|
}
|
|
/// Whether a registry tag (`entity_type`, `damage_type`, `item`...) holds `id`.
|
|
pub fn in_tag(&self, registry: &str, tag: &str, id: &str) -> bool {
|
|
self.tag(registry, tag).iter().any(|m| m == id)
|
|
}
|
|
/// `EnchantmentHelper.getEnchantmentLevel(enchantment, entity)`: the best
|
|
/// level on the items in the enchantment's slots.
|
|
pub fn enchantment_level(&self, enchantment: &str, entity: &LootEntity) -> i32 {
|
|
let Some(e) = self.enchantments.iter().find(|e| e.name == enchantment) else {
|
|
return 0;
|
|
};
|
|
if !entity.living {
|
|
return 0;
|
|
}
|
|
EQUIPMENT_SLOTS
|
|
.iter()
|
|
.filter(|slot| e.slots.iter().any(|g| slot_in_group(slot, g)))
|
|
.filter_map(|slot| entity.item(slot))
|
|
.map(|stack| item_level(stack, enchantment))
|
|
.max()
|
|
.unwrap_or(0)
|
|
}
|
|
/// Whether an item carries an enchantment with `prevent_equipment_drop`.
|
|
pub fn prevents_equipment_drop(&self, stack: &LootStack) -> bool {
|
|
stack.enchantments.iter().any(|(name, _)| {
|
|
self.enchantments.iter().any(|e| {
|
|
&e.name == name && !e.effects["minecraft:prevent_equipment_drop"].is_null()
|
|
})
|
|
})
|
|
}
|
|
/// `EnchantmentHelper.processEquipmentDropChance`: the victim's and the
|
|
/// attacker's `equipment_drops` effects (Looting) applied to `chance`.
|
|
pub fn equipment_drop_chance(&self, params: &EntityLootParams, chance: f32) -> f32 {
|
|
// Enchantment.damageContext: no luck and no killing player.
|
|
let damage_params = EntityLootParams {
|
|
this: params.this.clone(),
|
|
damage: params.damage.clone(),
|
|
player: None,
|
|
luck: 0.0,
|
|
};
|
|
let mut c = Context {
|
|
data: self,
|
|
random: LegacyRandom::new(0),
|
|
biome: None,
|
|
visited: Vec::new(),
|
|
luck: 0.0,
|
|
open_water: None,
|
|
entity: Some(&damage_params),
|
|
};
|
|
let mut chance = chance;
|
|
c.equipment_drops(¶ms.this, "victim", &mut chance);
|
|
if let Some(attacker) = params
|
|
.damage
|
|
.as_ref()
|
|
.and_then(|d| d.source.as_ref())
|
|
.filter(|a| a.living)
|
|
{
|
|
c.equipment_drops(attacker, "attacker", &mut chance);
|
|
}
|
|
chance
|
|
}
|
|
/// Tag members in tag order (nested tags expanded in place, duplicates
|
|
/// dropped).
|
|
fn tag(&self, registry: &str, name: &str) -> Vec<String> {
|
|
fn visit(data: &LootData, registry: &str, name: &str, out: &mut Vec<String>, depth: u32) {
|
|
if depth > 16 {
|
|
return;
|
|
}
|
|
let Some(values) = data.tags.get(&(registry.to_owned(), name.to_owned())) else {
|
|
return;
|
|
};
|
|
for v in values {
|
|
let id = match v {
|
|
Value::String(s) => s.as_str(),
|
|
Value::Object(o) => o.get("id").and_then(Value::as_str).unwrap_or(""),
|
|
_ => continue,
|
|
};
|
|
match id.strip_prefix('#') {
|
|
Some(tag) => visit(data, registry, tag, out, depth + 1),
|
|
None if !out.iter().any(|x| x == id) => out.push(id.to_owned()),
|
|
None => {}
|
|
}
|
|
}
|
|
}
|
|
let mut out = Vec::new();
|
|
visit(self, registry, name, &mut out, 0);
|
|
out
|
|
}
|
|
/// A HolderSet: "#tag", "id" or a list of ids.
|
|
fn holder_set(&self, registry: &str, v: &Value) -> Vec<String> {
|
|
match v {
|
|
Value::String(s) => match s.strip_prefix('#') {
|
|
Some(tag) => self.tag(registry, tag),
|
|
None => vec![s.clone()],
|
|
},
|
|
Value::Array(a) => a
|
|
.iter()
|
|
.filter_map(Value::as_str)
|
|
.map(str::to_owned)
|
|
.collect(),
|
|
_ => Vec::new(),
|
|
}
|
|
}
|
|
fn item_in(&self, item: &str, set: &Value) -> bool {
|
|
self.holder_set("item", set).iter().any(|i| i == item)
|
|
}
|
|
fn facts(&self, item: &str) -> ItemFacts {
|
|
self.items.get(item).copied().unwrap_or(ItemFacts {
|
|
max_stack: 64,
|
|
..ItemFacts::default()
|
|
})
|
|
}
|
|
|
|
/// LootTable.getRandomItems with the given random (its state is
|
|
/// advanced), luck and, for fishing, whether the hook is in open water.
|
|
pub fn roll(
|
|
&self,
|
|
table: &str,
|
|
random: &mut LegacyRandom,
|
|
luck: f32,
|
|
biome: Option<&str>,
|
|
open_water: Option<bool>,
|
|
) -> Vec<LootStack> {
|
|
let Some(json) = self.tables.get(table) else {
|
|
return Vec::new();
|
|
};
|
|
let mut c = Context {
|
|
data: self,
|
|
random: random.clone(),
|
|
biome,
|
|
visited: Vec::new(),
|
|
luck,
|
|
open_water,
|
|
entity: None,
|
|
};
|
|
let mut items = Vec::new();
|
|
c.table_raw(table, json, &mut |_, stack| items.push(stack));
|
|
*random = c.random;
|
|
items
|
|
}
|
|
/// LootTable.getRandomItems for an entity context: the generated stacks
|
|
/// after the stack splitter, in order. `random` is the table's random
|
|
/// sequence (or the level random) and is advanced.
|
|
pub fn roll_entity<R: RandomSource + Clone>(
|
|
&self,
|
|
table: &str,
|
|
random: &mut R,
|
|
params: &EntityLootParams,
|
|
) -> Vec<LootStack> {
|
|
let Some(json) = self.tables.get(table) else {
|
|
return Vec::new();
|
|
};
|
|
let mut c = Context {
|
|
data: self,
|
|
random: random.clone(),
|
|
biome: None,
|
|
visited: Vec::new(),
|
|
luck: params.luck,
|
|
open_water: None,
|
|
entity: Some(params),
|
|
};
|
|
let mut items = Vec::new();
|
|
c.table_raw(table, json, &mut |c, stack| c.data.split(stack, &mut items));
|
|
*random = c.random;
|
|
items
|
|
}
|
|
/// LootTable.getRandomItems with an empty context (a trial spawner's
|
|
/// reward): the stacks after the stack splitter. `random` is the table's
|
|
/// random sequence (or the level random) and is advanced.
|
|
pub fn roll_split<R: RandomSource + Clone>(
|
|
&self,
|
|
table: &str,
|
|
random: &mut R,
|
|
) -> Vec<LootStack> {
|
|
let Some(json) = self.tables.get(table) else {
|
|
return Vec::new();
|
|
};
|
|
let mut c = Context {
|
|
data: self,
|
|
random: random.clone(),
|
|
biome: None,
|
|
visited: Vec::new(),
|
|
luck: 0.0,
|
|
open_water: None,
|
|
entity: None,
|
|
};
|
|
let mut items = Vec::new();
|
|
c.table_raw(table, json, &mut |c, stack| c.data.split(stack, &mut items));
|
|
*random = c.random;
|
|
items
|
|
}
|
|
/// LootTable.createStackSplitter (every item is enabled).
|
|
fn split(&self, stack: LootStack, items: &mut Vec<LootStack>) {
|
|
let max = self.facts(&stack.item).max_stack.max(1);
|
|
if stack.count < max {
|
|
items.push(stack);
|
|
} else {
|
|
let mut count = stack.count;
|
|
while count > 0 {
|
|
let mut copy = stack.clone();
|
|
copy.count = max.min(count);
|
|
count -= copy.count;
|
|
items.push(copy);
|
|
}
|
|
}
|
|
}
|
|
/// LootTable.fill into an empty container of `slots` slots with
|
|
/// `RandomSource.create(seed)`; `biome` is the container's biome for
|
|
/// location checks (None: no origin, checks fail).
|
|
pub fn fill(
|
|
&self,
|
|
table: &str,
|
|
seed: i64,
|
|
slots: usize,
|
|
biome: Option<&str>,
|
|
) -> Vec<Option<LootStack>> {
|
|
self.fill_with(table, &mut LegacyRandom::new(seed), slots, biome)
|
|
}
|
|
/// LootTable.fill drawing from `random` (a seeded random or the table's
|
|
/// random sequence), which is advanced.
|
|
pub fn fill_with<R: RandomSource + Clone>(
|
|
&self,
|
|
table: &str,
|
|
random: &mut R,
|
|
slots: usize,
|
|
biome: Option<&str>,
|
|
) -> Vec<Option<LootStack>> {
|
|
let mut out = vec![None; slots];
|
|
let Some(json) = self.tables.get(table) else {
|
|
return out;
|
|
};
|
|
let mut c = Context {
|
|
data: self,
|
|
random: random.clone(),
|
|
biome,
|
|
visited: Vec::new(),
|
|
luck: 0.0,
|
|
open_water: None,
|
|
entity: None,
|
|
};
|
|
let mut items = Vec::new();
|
|
c.table_raw(table, json, &mut |c, stack| c.data.split(stack, &mut items));
|
|
*random = c.random;
|
|
let mut available: Vec<usize> = (0..slots).collect();
|
|
shuffle(&mut available, random);
|
|
shuffle_and_split(&mut items, available.len(), random);
|
|
for stack in items {
|
|
let Some(slot) = available.pop() else { break };
|
|
out[slot] = (!stack.is_empty()).then_some(stack);
|
|
}
|
|
out
|
|
}
|
|
}
|
|
|
|
/// `MobEffect.isInstantaneous`.
|
|
fn is_instantaneous(effect: &str) -> bool {
|
|
matches!(
|
|
effect,
|
|
"minecraft:saturation" | "minecraft:instant_health" | "minecraft:instant_damage"
|
|
)
|
|
}
|
|
/// The level of one enchantment on a stack (its `enchantments` component).
|
|
fn item_level(stack: &LootStack, enchantment: &str) -> i32 {
|
|
stack
|
|
.enchantments
|
|
.iter()
|
|
.find(|(n, _)| n == enchantment)
|
|
.map_or(0, |(_, l)| *l)
|
|
}
|
|
/// Util.shuffle.
|
|
fn shuffle<T>(list: &mut [T], random: &mut impl RandomSource) {
|
|
for i in (2..=list.len()).rev() {
|
|
let j = random.next_int_bound(i as i32).unwrap_or(0) as usize;
|
|
list.swap(i - 1, j);
|
|
}
|
|
}
|
|
/// Mth.nextInt.
|
|
fn next_int(random: &mut impl RandomSource, min: i32, max: i32) -> i32 {
|
|
if min >= max {
|
|
min
|
|
} else {
|
|
random.next_int_bound(max - min + 1).unwrap_or(0) + min
|
|
}
|
|
}
|
|
/// LootTable.shuffleAndSplitItems.
|
|
fn shuffle_and_split(result: &mut Vec<LootStack>, slots: usize, random: &mut impl RandomSource) {
|
|
let mut splittable = Vec::new();
|
|
result.retain(|s| {
|
|
if s.is_empty() {
|
|
false
|
|
} else if s.count > 1 {
|
|
splittable.push(s.clone());
|
|
false
|
|
} else {
|
|
true
|
|
}
|
|
});
|
|
while slots as i64 - result.len() as i64 - splittable.len() as i64 > 0 && !splittable.is_empty()
|
|
{
|
|
let index = next_int(random, 0, splittable.len() as i32 - 1) as usize;
|
|
let mut stack = splittable.remove(index);
|
|
let remove = next_int(random, 1, stack.count / 2).min(stack.count);
|
|
let mut copy = stack.clone();
|
|
copy.count = remove;
|
|
stack.count -= remove;
|
|
if stack.count > 1 && random.next_boolean() {
|
|
splittable.push(stack);
|
|
} else {
|
|
result.push(stack);
|
|
}
|
|
if copy.count > 1 && random.next_boolean() {
|
|
splittable.push(copy);
|
|
} else {
|
|
result.push(copy);
|
|
}
|
|
}
|
|
result.append(&mut splittable);
|
|
shuffle(result, random);
|
|
}
|
|
|
|
struct Context<'a, R> {
|
|
data: &'a LootData,
|
|
random: R,
|
|
biome: Option<&'a str>,
|
|
visited: Vec<String>,
|
|
/// LootContext.getLuck.
|
|
luck: f32,
|
|
/// A fishing hook's `is_open_water` for `entity_properties` (None: no entity).
|
|
open_water: Option<bool>,
|
|
/// The entity, damage source and killer parameters.
|
|
entity: Option<&'a EntityLootParams>,
|
|
}
|
|
type Sink<'s, R> = dyn FnMut(&mut Context<'_, R>, LootStack) + 's;
|
|
|
|
impl<R: RandomSource> Context<'_, R> {
|
|
/// A `LootContext.EntityTarget` parameter.
|
|
fn target(&self, name: &str) -> Option<&LootEntity> {
|
|
let p = self.entity?;
|
|
match name {
|
|
"this" => Some(&p.this),
|
|
"attacker" => p.damage.as_ref()?.source.as_ref(),
|
|
"direct_attacker" => p.damage.as_ref()?.direct.as_ref(),
|
|
"attacking_player" => p.player.as_ref(),
|
|
_ => None,
|
|
}
|
|
}
|
|
/// The `ATTACKING_ENTITY` parameter when it is a living entity.
|
|
fn living_attacker(&self) -> Option<&LootEntity> {
|
|
self.target("attacker").filter(|a| a.living)
|
|
}
|
|
/// The `equipment_drops` effects on one entity's equipment
|
|
/// (`runIterationOnEquipment`), for the given `enchanted` target.
|
|
fn equipment_drops(&mut self, holder: &LootEntity, enchanted: &str, chance: &mut f32) {
|
|
let data = self.data;
|
|
for slot in EQUIPMENT_SLOTS {
|
|
let Some(stack) = holder.item(slot) else {
|
|
continue;
|
|
};
|
|
for (name, level) in &stack.enchantments {
|
|
let Some(e) = data.enchantments.iter().find(|e| &e.name == name) else {
|
|
continue;
|
|
};
|
|
if !e.slots.iter().any(|g| slot_in_group(slot, g)) {
|
|
continue;
|
|
}
|
|
for effect in e.effects["minecraft:equipment_drops"]
|
|
.as_array()
|
|
.into_iter()
|
|
.flatten()
|
|
{
|
|
if effect["enchanted"].as_str() != Some(enchanted)
|
|
|| effect["affected"].as_str().unwrap_or("victim") != "victim"
|
|
{
|
|
continue;
|
|
}
|
|
if !effect["requirements"].is_null() && !self.condition(&effect["requirements"])
|
|
{
|
|
continue;
|
|
}
|
|
let value = level_value(&effect["effect"]["value"], *level);
|
|
*chance = match effect["effect"]["type"].as_str().unwrap_or("") {
|
|
"minecraft:add" => *chance + value,
|
|
"minecraft:multiply" => *chance * value,
|
|
"minecraft:set" => value,
|
|
_ => *chance,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// EntityPredicate.matches (false without an entity).
|
|
fn entity_matches(&self, p: &Value, e: Option<&LootEntity>) -> bool {
|
|
let Some(e) = e else { return false };
|
|
let Some(parts) = p.as_object() else {
|
|
return true;
|
|
};
|
|
let data = self.data;
|
|
parts.iter().all(|(key, v)| match key.as_str() {
|
|
"minecraft:entity_type" => match v.as_str() {
|
|
Some(s) => match s.strip_prefix('#') {
|
|
Some(tag) => data.in_tag("entity_type", tag, &e.kind),
|
|
None => s == e.kind,
|
|
},
|
|
None => v
|
|
.as_array()
|
|
.is_some_and(|a| a.iter().any(|x| x.as_str() == Some(&e.kind))),
|
|
},
|
|
"minecraft:flags" => v.as_object().into_iter().flatten().all(|(flag, want)| {
|
|
let want = want.as_bool().unwrap_or(false);
|
|
match flag.as_str() {
|
|
"is_on_fire" => e.on_fire == want,
|
|
// EntityFlagsPredicate: only living entities have an age.
|
|
"is_baby" => !e.living || e.baby == want,
|
|
"is_fall_flying" => !e.living || !want,
|
|
_ => !want,
|
|
}
|
|
}),
|
|
"minecraft:equipment" => {
|
|
e.living
|
|
&& v.as_object().into_iter().flatten().all(|(slot, item)| {
|
|
e.item(slot)
|
|
.is_some_and(|stack| self.item_matches(item, stack))
|
|
})
|
|
}
|
|
"minecraft:components" => v.as_object().into_iter().flatten().all(|(c, want)| match c
|
|
.as_str()
|
|
{
|
|
"minecraft:sheep/color" => e
|
|
.sheep
|
|
.as_ref()
|
|
.is_some_and(|(color, _)| want.as_str() == Some(color)),
|
|
"minecraft:frog/variant" => {
|
|
e.frog_variant.is_some() && want.as_str() == e.frog_variant.as_deref()
|
|
}
|
|
"minecraft:chicken/variant" => {
|
|
e.chicken_variant.is_some() && want.as_str() == e.chicken_variant.as_deref()
|
|
}
|
|
_ => false,
|
|
}),
|
|
"minecraft:type_specific/sheep" => e
|
|
.sheep
|
|
.as_ref()
|
|
.is_some_and(|(_, sheared)| v["sheared"].as_bool().is_none_or(|w| w == *sheared)),
|
|
"minecraft:type_specific/cube_mob" => {
|
|
e.cube_size.is_some_and(|size| int_bounds(&v["size"], size))
|
|
}
|
|
// RaiderPredicate: no raid is running here.
|
|
"minecraft:type_specific/raider" => e.captain.is_some_and(|captain| {
|
|
!v["has_raid"].as_bool().unwrap_or(false)
|
|
&& captain == v["is_captain"].as_bool().unwrap_or(false)
|
|
}),
|
|
"minecraft:vehicle" => self.entity_matches(v, e.vehicle.as_deref()),
|
|
_ => false,
|
|
})
|
|
}
|
|
fn int(&mut self, v: &Value) -> i32 {
|
|
match v {
|
|
Value::Number(_) => int_value(v).unwrap_or(0) as i32,
|
|
Value::Object(_) => match v["type"].as_str().unwrap_or("") {
|
|
"minecraft:constant" => self.int(&v["value"]),
|
|
"minecraft:uniform" => {
|
|
let min = self.int(&v["min"]);
|
|
let max = self.int(&v["max"]);
|
|
next_int(&mut self.random, min, max)
|
|
}
|
|
"minecraft:binomial" => {
|
|
let n = self.int(&v["n"]);
|
|
let p = self.float(&v["p"]);
|
|
(0..n).filter(|_| self.random.next_float() < p).count() as i32
|
|
}
|
|
_ => 0,
|
|
},
|
|
_ => 0,
|
|
}
|
|
}
|
|
fn float(&mut self, v: &Value) -> f32 {
|
|
match v {
|
|
Value::Number(n) => n.as_f64().unwrap_or(0.) as f32,
|
|
Value::Object(_) => match v["type"].as_str().unwrap_or("") {
|
|
"minecraft:constant" => self.float(&v["value"]),
|
|
"minecraft:uniform" => {
|
|
let min = self.float(&v["min"]);
|
|
let max = self.float(&v["max"]);
|
|
if min >= max {
|
|
min
|
|
} else {
|
|
self.random.next_float() * (max - min) + min
|
|
}
|
|
}
|
|
_ => 0.,
|
|
},
|
|
_ => 0.,
|
|
}
|
|
}
|
|
fn condition(&mut self, c: &Value) -> bool {
|
|
let data = self.data;
|
|
match c {
|
|
Value::Null => true,
|
|
Value::Array(list) => list.iter().all(|c| self.condition(c)),
|
|
Value::String(id) => match data.predicates.get(id) {
|
|
Some(p) => self.condition(p),
|
|
None => false,
|
|
},
|
|
Value::Object(_) => match c["type"].as_str().unwrap_or("") {
|
|
"minecraft:inverted" => !self.condition(&c["term"]),
|
|
"minecraft:all_of" => c["terms"]
|
|
.as_array()
|
|
.into_iter()
|
|
.flatten()
|
|
.all(|t| self.condition(t)),
|
|
"minecraft:any_of" => c["terms"]
|
|
.as_array()
|
|
.into_iter()
|
|
.flatten()
|
|
.any(|t| self.condition(t)),
|
|
"minecraft:entity_properties" if self.entity.is_some() => {
|
|
let target = self.target(c["entity"].as_str().unwrap_or("this"));
|
|
c["predicate"].is_null() || self.entity_matches(&c["predicate"], target)
|
|
}
|
|
// FishingHookPredicate: the only entity property a hook has.
|
|
"minecraft:entity_properties" => {
|
|
let predicate = &c["predicate"];
|
|
let fishing = &predicate["minecraft:type_specific/fishing_hook"];
|
|
let only_hook = predicate.as_object().is_some_and(|o| o.len() == 1);
|
|
match self.open_water {
|
|
Some(open) if only_hook && fishing.is_object() => fishing["in_open_water"]
|
|
.as_bool()
|
|
.is_none_or(|want| want == open),
|
|
_ => false,
|
|
}
|
|
}
|
|
"minecraft:killed_by_player" => self.entity.is_some_and(|p| p.player.is_some()),
|
|
"minecraft:random_chance_with_enchanted_bonus" => {
|
|
let level = self.living_attacker().map_or(0, |a| {
|
|
data.enchantment_level(c["enchantment"].as_str().unwrap_or(""), a)
|
|
});
|
|
let chance = if level > 0 {
|
|
level_value(&c["enchanted_chance"], level)
|
|
} else {
|
|
c["unenchanted_chance"].as_f64().unwrap_or(0.) as f32
|
|
};
|
|
self.random.next_float() < chance
|
|
}
|
|
// DamageSourcePredicate (false without a damage source).
|
|
"minecraft:damage_source_properties" => {
|
|
let Some(damage) = self.entity.and_then(|p| p.damage.as_ref()) else {
|
|
return false;
|
|
};
|
|
let p = &c["predicate"];
|
|
let tags_ok = p["tags"].as_array().into_iter().flatten().all(|t| {
|
|
let id = t["id"].as_str().unwrap_or("");
|
|
let has = match id.strip_prefix('#') {
|
|
Some(tag) => data.in_tag("damage_type", tag, &damage.kind),
|
|
None => id == damage.kind,
|
|
};
|
|
has == t["expected"].as_bool().unwrap_or(false)
|
|
});
|
|
tags_ok
|
|
&& (p["direct_entity"].is_null()
|
|
|| self.entity_matches(&p["direct_entity"], damage.direct.as_ref()))
|
|
&& (p["source_entity"].is_null()
|
|
|| self.entity_matches(&p["source_entity"], damage.source.as_ref()))
|
|
&& p["is_direct"].as_bool().is_none_or(|want| {
|
|
let direct = match (&damage.source, &damage.direct) {
|
|
(Some(a), Some(b)) => a.kind == b.kind,
|
|
(None, None) => true,
|
|
_ => false,
|
|
};
|
|
direct == want
|
|
})
|
|
}
|
|
"minecraft:random_chance" => {
|
|
let chance = self.float(&c["chance"]);
|
|
self.random.next_float() < chance
|
|
}
|
|
"minecraft:location_check" => {
|
|
let Some(biome) = self.biome else {
|
|
return false;
|
|
};
|
|
match &c["predicate"]["biomes"] {
|
|
Value::Null => true,
|
|
Value::String(s) => match s.strip_prefix('#') {
|
|
// Biome tags are not loaded here.
|
|
Some(_) => false,
|
|
None => s == biome,
|
|
},
|
|
Value::Array(a) => a.iter().any(|b| b.as_str() == Some(biome)),
|
|
_ => false,
|
|
}
|
|
}
|
|
_ => false,
|
|
},
|
|
_ => false,
|
|
}
|
|
}
|
|
/// An item modifier (typed object, inline list or registry reference).
|
|
fn modify(&mut self, m: &Value, stack: LootStack) -> LootStack {
|
|
let data = self.data;
|
|
match m {
|
|
Value::Null => stack,
|
|
Value::Array(list) => list.iter().fold(stack, |s, f| self.modify(f, s)),
|
|
Value::String(id) => match data.modifiers.get(id) {
|
|
Some(f) => self.modify(f, stack),
|
|
None => stack,
|
|
},
|
|
Value::Object(_) => {
|
|
if !m["condition"].is_null() && !self.condition(&m["condition"]) {
|
|
return stack;
|
|
}
|
|
self.run(m, stack)
|
|
}
|
|
_ => stack,
|
|
}
|
|
}
|
|
fn run(&mut self, f: &Value, mut stack: LootStack) -> LootStack {
|
|
let data = self.data;
|
|
match f["type"].as_str().unwrap_or("") {
|
|
"minecraft:sequence" => {
|
|
for function in f["functions"].as_array().into_iter().flatten() {
|
|
stack = self.modify(function, stack);
|
|
}
|
|
}
|
|
"minecraft:set_count" => {
|
|
let base = if f["add"].as_bool() == Some(true) {
|
|
stack.get_count()
|
|
} else {
|
|
0
|
|
};
|
|
stack.count = base + self.int(&f["count"]);
|
|
}
|
|
"minecraft:limit_count" => {
|
|
let limit = &f["limit"];
|
|
let mut count = stack.get_count();
|
|
if let Some(min) = int_value(&limit["min"]) {
|
|
count = count.max(min as i32);
|
|
}
|
|
if let Some(max) = int_value(&limit["max"]) {
|
|
count = count.min(max as i32);
|
|
}
|
|
stack.count = count;
|
|
}
|
|
"minecraft:set_damage" => {
|
|
let max = data.facts(&stack.item).max_damage;
|
|
if max > 0 {
|
|
let base = if f["add"].as_bool() == Some(true) {
|
|
1. - stack.damage as f32 / max as f32
|
|
} else {
|
|
0.
|
|
};
|
|
let pct = 1. - (self.float(&f["damage"]) + base).clamp(0., 1.);
|
|
stack.damage = (pct * max as f32).floor() as i32;
|
|
}
|
|
}
|
|
"minecraft:enchant_randomly" => {
|
|
let book = stack.item == "minecraft:book";
|
|
let check = !book && f["only_compatible"].as_bool().unwrap_or(true);
|
|
let options: Vec<usize> = match f.get("options") {
|
|
Some(o) => data
|
|
.holder_set("enchantment", o)
|
|
.iter()
|
|
.filter_map(|n| data.enchantments.iter().position(|e| &e.name == n))
|
|
.collect(),
|
|
None => (0..data.enchantments.len()).collect(),
|
|
};
|
|
let options: Vec<usize> = options
|
|
.into_iter()
|
|
.filter(|&i| {
|
|
!check || data.item_in(&stack.item, &data.enchantments[i].supported)
|
|
})
|
|
.collect();
|
|
if !options.is_empty() {
|
|
let pick = self
|
|
.random
|
|
.next_int_bound(options.len() as i32)
|
|
.unwrap_or(0) as usize;
|
|
let e = &data.enchantments[options[pick]];
|
|
let level = next_int(&mut self.random, 1, e.max_level);
|
|
if book {
|
|
stack.item = "minecraft:enchanted_book".into();
|
|
}
|
|
enchant(&mut stack, &e.name, level);
|
|
}
|
|
}
|
|
"minecraft:enchant_with_levels" => {
|
|
let cost = self.int(&f["levels"]);
|
|
let source: Vec<usize> = match f.get("options") {
|
|
Some(o) => data
|
|
.holder_set("enchantment", o)
|
|
.iter()
|
|
.filter_map(|n| data.enchantments.iter().position(|e| &e.name == n))
|
|
.collect(),
|
|
None => (0..data.enchantments.len()).collect(),
|
|
};
|
|
let chosen = self.select_enchantments(&stack.item, cost, &source);
|
|
if stack.item == "minecraft:book" {
|
|
stack.item = "minecraft:enchanted_book".into();
|
|
}
|
|
for (i, level) in chosen {
|
|
enchant(&mut stack, &data.enchantments[i].name, level);
|
|
}
|
|
}
|
|
"minecraft:set_enchantments" => {
|
|
if let Some(map) = f["enchantments"].as_object() {
|
|
if stack.item == "minecraft:book" {
|
|
stack.item = "minecraft:enchanted_book".into();
|
|
}
|
|
for (name, level) in map {
|
|
let level = self.int(level);
|
|
enchant(&mut stack, name, level);
|
|
}
|
|
}
|
|
}
|
|
"minecraft:set_potion" => stack.potion = f["id"].as_str().map(str::to_owned),
|
|
"minecraft:set_stew_effect" => {
|
|
let effects = f["effects"].as_array().cloned().unwrap_or_default();
|
|
if stack.item == "minecraft:suspicious_stew" && !effects.is_empty() {
|
|
let pick = self
|
|
.random
|
|
.next_int_bound(effects.len() as i32)
|
|
.unwrap_or(0) as usize;
|
|
let effect = effects[pick]["type"].as_str().unwrap_or("");
|
|
let mut duration = self.int(&effects[pick]["duration"]);
|
|
// Seconds, unless the effect is instantaneous.
|
|
if !is_instantaneous(effect) {
|
|
duration *= 20;
|
|
}
|
|
// SuspiciousStewEffects.withEffectAdded
|
|
stack.stew_effects.push((effect.to_owned(), duration));
|
|
}
|
|
}
|
|
"minecraft:set_ominous_bottle_amplifier" => {
|
|
stack.ominous_amplifier = Some(self.int(&f["amplifier"]));
|
|
}
|
|
"minecraft:set_instrument" => {
|
|
let options = data.holder_set("instrument", &f["options"]);
|
|
if !options.is_empty() {
|
|
self.random
|
|
.next_int_bound(options.len() as i32)
|
|
.unwrap_or(0);
|
|
}
|
|
}
|
|
"minecraft:set_item" => {
|
|
if let Some(item) = f["item"].as_str() {
|
|
stack.item = item.to_owned();
|
|
}
|
|
}
|
|
// ExplorationMapFunction needs an origin; with one, the map is
|
|
// taken as found here and the game runs the structure search
|
|
// (it drops the map when nothing is found, as the filtered
|
|
// map_id check after it would).
|
|
"minecraft:exploration_map" => {
|
|
stack.map = self.biome.is_some();
|
|
if stack.map {
|
|
stack.exploration = Some(Box::new(ExplorationMap::from_json(f)));
|
|
}
|
|
}
|
|
"minecraft:filtered" => {
|
|
let pass = self.item_matches(&f["item_filter"], &stack);
|
|
let branch = if pass { &f["on_pass"] } else { &f["on_fail"] };
|
|
stack = self.modify(branch, stack);
|
|
}
|
|
"minecraft:discard" => stack.count = 0,
|
|
"minecraft:enchanted_count_increase" => {
|
|
if let Some(attacker) = self.living_attacker() {
|
|
let level =
|
|
data.enchantment_level(f["enchantment"].as_str().unwrap_or(""), attacker);
|
|
if level != 0 {
|
|
let addition = level as f32 * self.float(&f["count"]);
|
|
// ItemStack.grow(Math.round(addition)).
|
|
stack.count = stack.get_count() + (addition + 0.5).floor() as i32;
|
|
let limit = int_value(&f["limit"]).unwrap_or(0) as i32;
|
|
if limit > 0 && !stack.is_empty() && stack.count > limit {
|
|
stack.count = limit;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"minecraft:furnace_smelt" => {
|
|
if !stack.is_empty()
|
|
&& let Some((_, result, count)) = data
|
|
.smelting
|
|
.iter()
|
|
.find(|(ingredient, _, _)| data.item_in(&stack.item, ingredient))
|
|
{
|
|
let input = if f["use_input_count"].as_bool().unwrap_or(true) {
|
|
stack.count
|
|
} else {
|
|
1
|
|
};
|
|
let mut smelted = LootStack::new(result);
|
|
smelted.count = (input * count).min(data.facts(result).max_stack);
|
|
stack = smelted;
|
|
}
|
|
}
|
|
// Names, lore, components and other data without random draws.
|
|
_ => {}
|
|
}
|
|
stack
|
|
}
|
|
/// ItemPredicate subset used by filtered modifiers and equipment.
|
|
fn item_matches(&self, p: &Value, stack: &LootStack) -> bool {
|
|
if !p["items"].is_null() && !self.data.item_in(&stack.item, &p["items"]) {
|
|
return false;
|
|
}
|
|
if !int_bounds(&p["count"], stack.count) {
|
|
return false;
|
|
}
|
|
if p["predicates"].get("minecraft:map_id").is_some() && !stack.map {
|
|
return false;
|
|
}
|
|
// EnchantmentsPredicate: every entry must hold.
|
|
if let Some(list) = p["predicates"]["minecraft:enchantments"].as_array() {
|
|
let data = self.data;
|
|
let ok = list.iter().all(|e| {
|
|
let levels = &e["levels"];
|
|
match &e["enchantments"] {
|
|
Value::Null => stack
|
|
.enchantments
|
|
.iter()
|
|
.any(|(_, l)| int_bounds(levels, *l)),
|
|
set => data.holder_set("enchantment", set).iter().any(|name| {
|
|
let level = item_level(stack, name);
|
|
level != 0 && int_bounds(levels, level)
|
|
}),
|
|
}
|
|
});
|
|
if !ok {
|
|
return false;
|
|
}
|
|
}
|
|
true
|
|
}
|
|
/// EnchantmentHelper.selectEnchantment.
|
|
fn select_enchantments(
|
|
&mut self,
|
|
item: &str,
|
|
mut cost: i32,
|
|
source: &[usize],
|
|
) -> Vec<(usize, i32)> {
|
|
let data = self.data;
|
|
let mut results = Vec::new();
|
|
let Some(value) = data.facts(item).enchantable else {
|
|
return results;
|
|
};
|
|
cost += 1
|
|
+ self.random.next_int_bound(value / 4 + 1).unwrap_or(0)
|
|
+ self.random.next_int_bound(value / 4 + 1).unwrap_or(0);
|
|
let span = (self.random.next_float() + self.random.next_float() - 1.) * 0.15;
|
|
cost = ((cost as f32 + cost as f32 * span + 0.5).floor() as i32).max(1);
|
|
let book = item == "minecraft:book";
|
|
let mut available: Vec<(usize, i32)> = Vec::new();
|
|
for &i in source {
|
|
let e = &data.enchantments[i];
|
|
let primary = data.item_in(item, &e.supported)
|
|
&& e.primary.as_ref().is_none_or(|p| data.item_in(item, p));
|
|
if !primary && !book {
|
|
continue;
|
|
}
|
|
for level in (1..=e.max_level).rev() {
|
|
if cost >= e.min_cost(level) && cost <= e.max_cost(level) {
|
|
available.push((i, level));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if available.is_empty() {
|
|
return results;
|
|
}
|
|
let pick = |random: &mut R, list: &[(usize, i32)]| -> Option<(usize, i32)> {
|
|
let total: i32 = list.iter().map(|&(i, _)| data.enchantments[i].weight).sum();
|
|
if total <= 0 {
|
|
return None;
|
|
}
|
|
let mut selection = random.next_int_bound(total).unwrap_or(0);
|
|
for &entry in list {
|
|
selection -= data.enchantments[entry.0].weight;
|
|
if selection < 0 {
|
|
return Some(entry);
|
|
}
|
|
}
|
|
None
|
|
};
|
|
results.extend(pick(&mut self.random, &available));
|
|
while self.random.next_int_bound(50).unwrap_or(0) <= cost {
|
|
if let Some(&(last, _)) = results.last() {
|
|
available.retain(|&(i, _)| self.compatible(last, i));
|
|
}
|
|
if available.is_empty() {
|
|
break;
|
|
}
|
|
results.extend(pick(&mut self.random, &available));
|
|
cost /= 2;
|
|
}
|
|
results
|
|
}
|
|
/// Enchantment.areCompatible.
|
|
fn compatible(&self, a: usize, b: usize) -> bool {
|
|
let data = self.data;
|
|
let (ea, eb) = (&data.enchantments[a], &data.enchantments[b]);
|
|
a != b
|
|
&& !data
|
|
.holder_set("enchantment", &ea.exclusive)
|
|
.contains(&eb.name)
|
|
&& !data
|
|
.holder_set("enchantment", &eb.exclusive)
|
|
.contains(&ea.name)
|
|
}
|
|
/// LootTable.getRandomItemsRaw.
|
|
fn table_raw(&mut self, name: &str, table: &Value, sink: &mut Sink<'_, R>) {
|
|
if self.visited.iter().any(|v| v == name) {
|
|
return;
|
|
}
|
|
self.visited.push(name.to_owned());
|
|
let table_modifier = &table["modifier"];
|
|
for pool in table["pools"].as_array().into_iter().flatten() {
|
|
if !pool["condition"].is_null() && !self.condition(&pool["condition"]) {
|
|
continue;
|
|
}
|
|
let bonus = if pool["bonus_rolls"].is_null() {
|
|
0.0
|
|
} else {
|
|
self.float(&pool["bonus_rolls"])
|
|
};
|
|
let rolls = self.int(&pool["rolls"]) + (bonus * self.luck).floor() as i32;
|
|
for _ in 0..rolls {
|
|
self.add_random_item(pool, &mut |c, stack| {
|
|
let stack = c.modify(&pool["modifier"], stack);
|
|
let stack = c.modify(table_modifier, stack);
|
|
sink(c, stack);
|
|
});
|
|
}
|
|
}
|
|
self.visited.pop();
|
|
}
|
|
/// LootPool.addRandomItem.
|
|
fn add_random_item(&mut self, pool: &Value, sink: &mut Sink<'_, R>) {
|
|
let mut valid: Vec<Choice<'_>> = Vec::new();
|
|
for entry in pool["entries"].as_array().into_iter().flatten() {
|
|
self.expand(entry, &mut valid);
|
|
}
|
|
// EntryBase.getWeight: max(floor(weight + quality * luck), 0).
|
|
let luck = self.luck;
|
|
let weight = |c: &Choice<'_>| {
|
|
let e = c.entry;
|
|
let w = int_value(&e["weight"]).unwrap_or(1) as f32
|
|
+ int_value(&e["quality"]).unwrap_or(0) as f32 * luck;
|
|
w.floor().max(0.0) as i32
|
|
};
|
|
let total: i32 = valid.iter().map(&weight).sum();
|
|
if total == 0 || valid.is_empty() {
|
|
return;
|
|
}
|
|
let chosen = if valid.len() == 1 {
|
|
valid.swap_remove(0)
|
|
} else {
|
|
let mut index = self.random.next_int_bound(total).unwrap_or(0);
|
|
let Some(at) = valid.iter().position(|e| {
|
|
index -= weight(e);
|
|
index < 0
|
|
}) else {
|
|
return;
|
|
};
|
|
valid.swap_remove(at)
|
|
};
|
|
self.create(&chosen, sink);
|
|
}
|
|
/// LootPoolEntryContainer.expand: the singleton entries that can run.
|
|
fn expand<'v>(&mut self, entry: &'v Value, out: &mut Vec<Choice<'v>>) -> bool {
|
|
if !entry["condition"].is_null() && !self.condition(&entry["condition"]) {
|
|
return false;
|
|
}
|
|
let children = || entry["children"].as_array().into_iter().flatten();
|
|
match entry["type"].as_str().unwrap_or("") {
|
|
"minecraft:alternatives" => children().any(|c| self.expand(c, out)),
|
|
"minecraft:group" => {
|
|
for c in children() {
|
|
self.expand(c, out);
|
|
}
|
|
true
|
|
}
|
|
"minecraft:sequence" => {
|
|
for c in children() {
|
|
if !self.expand(c, out) {
|
|
return false;
|
|
}
|
|
}
|
|
true
|
|
}
|
|
"minecraft:item" | "minecraft:empty" | "minecraft:loot_table" => {
|
|
if int_value(&entry["weight"]).unwrap_or(1) > 0 {
|
|
out.push(Choice { entry, item: None });
|
|
}
|
|
true
|
|
}
|
|
// TagEntry: one choice per item when expanded, else one for all.
|
|
"minecraft:tag" => {
|
|
if entry["expand"].as_bool().unwrap_or(false) {
|
|
let items = self.data.holder_set("item", &entry["items"]);
|
|
for item in items {
|
|
if int_value(&entry["weight"]).unwrap_or(1) > 0 {
|
|
out.push(Choice {
|
|
entry,
|
|
item: Some(item),
|
|
});
|
|
}
|
|
}
|
|
true
|
|
} else {
|
|
if int_value(&entry["weight"]).unwrap_or(1) > 0 {
|
|
out.push(Choice { entry, item: None });
|
|
}
|
|
true
|
|
}
|
|
}
|
|
_ => false,
|
|
}
|
|
}
|
|
/// LootPoolEntry.createItemStack with the entry's modifier.
|
|
fn create(&mut self, choice: &Choice<'_>, sink: &mut Sink<'_, R>) {
|
|
let entry = choice.entry;
|
|
let modifier = &entry["modifier"];
|
|
let mut out = |c: &mut Context<'_, R>, stack: LootStack| {
|
|
let stack = c.modify(modifier, stack);
|
|
sink(c, stack);
|
|
};
|
|
match entry["type"].as_str().unwrap_or("") {
|
|
"minecraft:item" => {
|
|
let stack = LootStack::new(entry["name"].as_str().unwrap_or("minecraft:air"));
|
|
out(self, stack);
|
|
}
|
|
"minecraft:tag" => match &choice.item {
|
|
Some(item) => out(self, LootStack::new(item)),
|
|
None => {
|
|
for item in self.data.holder_set("item", &entry["items"]) {
|
|
out(self, LootStack::new(&item));
|
|
}
|
|
}
|
|
},
|
|
"minecraft:loot_table" => match &entry["value"] {
|
|
Value::String(name) => {
|
|
if let Some(table) = self.data.tables.get(name) {
|
|
self.table_raw(name, table, &mut out);
|
|
}
|
|
}
|
|
inline @ Value::Object(_) => self.table_raw("<inline>", inline, &mut out),
|
|
_ => {}
|
|
},
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
/// A pool entry that can be chosen (an expanded tag entry names its item).
|
|
struct Choice<'v> {
|
|
entry: &'v Value,
|
|
item: Option<String>,
|
|
}
|
|
fn enchant(stack: &mut LootStack, name: &str, level: i32) {
|
|
match stack.enchantments.iter_mut().find(|(n, _)| n == name) {
|
|
Some(slot) => slot.1 = level,
|
|
None => stack.enchantments.push((name.to_owned(), level)),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod namespace_tests {
|
|
use super::*;
|
|
|
|
/// A content mods' root: the mods' loot tables and tags load beside
|
|
/// vanilla's, under their own namespaces.
|
|
#[test]
|
|
fn loads_every_namespace() {
|
|
let root = std::env::temp_dir().join(format!("horizon-loot-ns-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&root);
|
|
let files = [
|
|
("items.json", r#"{"minecraft:bread": {}}"#),
|
|
(
|
|
"data/minecraft/loot_table/chests/village.json",
|
|
r#"{"pools": []}"#,
|
|
),
|
|
(
|
|
"data/kaisyn/loot_table/village/forest/house.json",
|
|
r#"{"pools": []}"#,
|
|
),
|
|
(
|
|
"data/kaisyn/tags/item/food.json",
|
|
r#"{"values": ["minecraft:bread"]}"#,
|
|
),
|
|
];
|
|
for (name, text) in files {
|
|
let path = root.join(name);
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
fs::write(path, text).unwrap();
|
|
}
|
|
let data = LootData::load(&root).unwrap();
|
|
assert!(data.has_table("minecraft:chests/village"));
|
|
assert!(data.has_table("kaisyn:village/forest/house"));
|
|
assert!(!data.has_table("minecraft:village/forest/house"));
|
|
assert!(data.in_tag("item", "kaisyn:food", "minecraft:bread"));
|
|
// Rolls and weights written as floats (Towns and Towers' tables).
|
|
let table = r#"{"pools": [{"rolls": {"type": "minecraft:uniform", "min": 3.0, "max": 3.0},
|
|
"entries": [{"type": "minecraft:item", "name": "minecraft:bread", "weight": 2.0}]}]}"#;
|
|
fs::write(
|
|
root.join("data/kaisyn/loot_table/village/forest/house.json"),
|
|
table,
|
|
)
|
|
.unwrap();
|
|
let data = LootData::load(&root).unwrap();
|
|
let filled = data.fill("kaisyn:village/forest/house", 1, 27, None);
|
|
let count: i32 = filled.iter().flatten().map(|s| s.count).sum();
|
|
assert_eq!(count, 3);
|
|
let _ = fs::remove_dir_all(&root);
|
|
}
|
|
}
|