using Theriapolis.Core.Data; using Theriapolis.Core.Util; namespace Theriapolis.Core.Loot; /// /// Pure deterministic loot roller. Given a table id and an RNG (typically /// the encounter's ), produces /// the list of (itemDef, qty) tuples to drop. /// /// Determinism: dice come from the encounter RNG so save+load round-trips /// produce identical drops — important for the autosave_combat retry slot. /// public static class LootRoller { public sealed record DropResult(ItemDef Def, int Qty); /// /// Roll against the supplied RNG. Returns an /// empty list when the table id is empty/unknown. /// public static List Roll( string tableId, IReadOnlyDictionary tables, IReadOnlyDictionary items, SeededRng rng) { var results = new List(); if (string.IsNullOrEmpty(tableId) || !tables.TryGetValue(tableId, out var table)) return results; foreach (var drop in table.Drops) { // Independent chance roll per drop. if (rng.NextFloat() > drop.Chance) continue; if (!items.TryGetValue(drop.ItemId, out var def)) continue; int qty; if (drop.QtyMax <= drop.QtyMin) qty = System.Math.Max(1, drop.QtyMin); else qty = rng.NextInt(drop.QtyMin, drop.QtyMax + 1); results.Add(new DropResult(def, qty)); } return results; } }