@yipe/dice 0.2.16 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,6 +54,7 @@ var _DiceQuery = class _DiceQuery {
54
54
  constructor(singles, combined, eps = EPS) {
55
55
  __publicField(this, "singles");
56
56
  __publicField(this, "combined");
57
+ __publicField(this, "_combinedWithAttr");
57
58
  this.singles = Array.isArray(singles) ? singles : [singles];
58
59
  if (this.singles.some((s) => s === void 0)) {
59
60
  throw new Error("DiceQuery contains undefined singles");
@@ -61,6 +62,35 @@ var _DiceQuery = class _DiceQuery {
61
62
  const c = combined ?? PMF.convolveMany(this.singles);
62
63
  this.combined = Math.abs(c.mass() - 1) <= eps ? c : c.normalize();
63
64
  }
65
+ /**
66
+ * Returns a new PMF with damage attribution metadata populated.
67
+ *
68
+ * This method computes attribution on-demand for builder-generated PMFs,
69
+ * enabling them to work with damage attribution charts. The `attr` field
70
+ * tracks how much damage each outcome type contributes at each damage value.
71
+ *
72
+ * For each bin at damage D: sum(attr.values()) ≈ D × P(damage = D)
73
+ *
74
+ * Performance: Cached after first call. Adds minimal overhead vs `combined`.
75
+ *
76
+ * @returns PMF with attr field populated for damage attribution charts
77
+ *
78
+ * @example
79
+ * const attack = d20.plus(5).ac(15).onHit(d(2,6).plus(3)).onCrit(d(2,6))
80
+ * const query = attack.toQuery()
81
+ * const pmf = query.combinedWithAttribution()
82
+ * // Now pmf can be used with toDamageAttributionChartSeries()
83
+ */
84
+ combinedWithAttribution() {
85
+ if (this._combinedWithAttr) {
86
+ return this._combinedWithAttr;
87
+ }
88
+ const singlesWithAttr = this.singles.map((pmf) => pmf.withAttribution());
89
+ const combined = PMF.convolveMany(singlesWithAttr, this.combined.epsilon);
90
+ const normalized = Math.abs(combined.mass() - 1) <= this.combined.epsilon ? combined : combined.normalize();
91
+ this._combinedWithAttr = normalized;
92
+ return normalized;
93
+ }
64
94
  /**
65
95
  * Returns the expected damage across all possible outcomes.
66
96
  *
@@ -1405,9 +1435,9 @@ var _PMF = class _PMF {
1405
1435
  let totalWeight = items.reduce((s, { weight }) => s + weight, 0);
1406
1436
  if (Math.abs(totalWeight) <= eps) totalWeight = 0;
1407
1437
  if (Math.abs(1 - totalWeight) <= eps) totalWeight = 1;
1408
- if (totalWeight > 1 + eps) {
1438
+ if (totalWeight > 1 + EPS) {
1409
1439
  throw new Error(
1410
- `PMF.exclusive: total weight ${totalWeight.toFixed(6)} exceeds 1.`
1440
+ `PMF.exclusive: total weight ${totalWeight} exceeds 1. (epsilon: ${eps})`
1411
1441
  );
1412
1442
  }
1413
1443
  let out = _PMF.empty(eps);
@@ -1449,6 +1479,44 @@ var _PMF = class _PMF {
1449
1479
  }
1450
1480
  return out;
1451
1481
  }
1482
+ /**
1483
+ * Adds damage attribution metadata to this PMF based on existing count metadata.
1484
+ * For each bin, sets attr[outcome] = damage × count[outcome].
1485
+ *
1486
+ * This enables damage attribution charts to work with builder-generated PMFs.
1487
+ * The parser generates attr automatically, but builder PMFs only have count.
1488
+ *
1489
+ * @returns New PMF with attr field populated in each bin
1490
+ */
1491
+ withAttribution() {
1492
+ for (const [damage, bin] of this.map) {
1493
+ if (damage !== 0 && bin.attr && Object.keys(bin.attr).length > 0) {
1494
+ return this;
1495
+ }
1496
+ if (damage > 0) break;
1497
+ }
1498
+ const newMap = /* @__PURE__ */ new Map();
1499
+ for (const [damage, bin] of this.map) {
1500
+ const attr = {};
1501
+ for (const outcome in bin.count) {
1502
+ const probability = bin.count[outcome];
1503
+ if (probability > 0) {
1504
+ attr[outcome] = damage * probability;
1505
+ }
1506
+ }
1507
+ newMap.set(damage, {
1508
+ p: bin.p,
1509
+ count: { ...bin.count },
1510
+ attr: Object.keys(attr).length > 0 ? attr : void 0
1511
+ });
1512
+ }
1513
+ return new _PMF(
1514
+ newMap,
1515
+ this.epsilon,
1516
+ this.normalized,
1517
+ `${this.identifier}~attr`
1518
+ );
1519
+ }
1452
1520
  /**
1453
1521
  * General-purpose N-way mixture.
1454
1522
  * weights: Array of [weight, PMF].