Files
TheriapolisV3/Theriapolis.Core/Loot/LootRoller.cs
T
Christopher Wiebe b451f83174 Initial commit: Theriapolis baseline at port/godot branch point
Captures the pre-Godot-port state of the codebase. This is the rollback
anchor for the Godot port (M0 of theriapolis-rpg-implementation-plan-godot-port.md).
All Phase 0 through Phase 6.5 work is included; Phase 7 is in flight.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-30 20:40:51 -07:00

47 lines
1.6 KiB
C#

using Theriapolis.Core.Data;
using Theriapolis.Core.Util;
namespace Theriapolis.Core.Loot;
/// <summary>
/// Pure deterministic loot roller. Given a table id and an RNG (typically
/// the encounter's <see cref="Rules.Combat.Encounter.RollDie"/>), 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.
/// </summary>
public static class LootRoller
{
public sealed record DropResult(ItemDef Def, int Qty);
/// <summary>
/// Roll <paramref name="tableId"/> against the supplied RNG. Returns an
/// empty list when the table id is empty/unknown.
/// </summary>
public static List<DropResult> Roll(
string tableId,
IReadOnlyDictionary<string, LootTableDef> tables,
IReadOnlyDictionary<string, ItemDef> items,
SeededRng rng)
{
var results = new List<DropResult>();
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;
}
}