@yipe/dice 0.2.16 → 0.2.19

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.
package/dist/index.cjs CHANGED
@@ -62,6 +62,7 @@ var _DiceQuery = class _DiceQuery {
62
62
  constructor(singles, combined, eps = EPS) {
63
63
  __publicField(this, "singles");
64
64
  __publicField(this, "combined");
65
+ __publicField(this, "_combinedWithAttr");
65
66
  this.singles = Array.isArray(singles) ? singles : [singles];
66
67
  if (this.singles.some((s) => s === void 0)) {
67
68
  throw new Error("DiceQuery contains undefined singles");
@@ -69,6 +70,35 @@ var _DiceQuery = class _DiceQuery {
69
70
  const c = combined ?? PMF.convolveMany(this.singles);
70
71
  this.combined = Math.abs(c.mass() - 1) <= eps ? c : c.normalize();
71
72
  }
73
+ /**
74
+ * Returns a new PMF with damage attribution metadata populated.
75
+ *
76
+ * This method computes attribution on-demand for builder-generated PMFs,
77
+ * enabling them to work with damage attribution charts. The `attr` field
78
+ * tracks how much damage each outcome type contributes at each damage value.
79
+ *
80
+ * For each bin at damage D: sum(attr.values()) ≈ D × P(damage = D)
81
+ *
82
+ * Performance: Cached after first call. Adds minimal overhead vs `combined`.
83
+ *
84
+ * @returns PMF with attr field populated for damage attribution charts
85
+ *
86
+ * @example
87
+ * const attack = d20.plus(5).ac(15).onHit(d(2,6).plus(3)).onCrit(d(2,6))
88
+ * const query = attack.toQuery()
89
+ * const pmf = query.combinedWithAttribution()
90
+ * // Now pmf can be used with toDamageAttributionChartSeries()
91
+ */
92
+ combinedWithAttribution() {
93
+ if (this._combinedWithAttr) {
94
+ return this._combinedWithAttr;
95
+ }
96
+ const singlesWithAttr = this.singles.map((pmf) => pmf.withAttribution());
97
+ const combined = PMF.convolveMany(singlesWithAttr, this.combined.epsilon);
98
+ const normalized = Math.abs(combined.mass() - 1) <= this.combined.epsilon ? combined : combined.normalize();
99
+ this._combinedWithAttr = normalized;
100
+ return normalized;
101
+ }
72
102
  /**
73
103
  * Returns the expected damage across all possible outcomes.
74
104
  *
@@ -1413,9 +1443,9 @@ var _PMF = class _PMF {
1413
1443
  let totalWeight = items.reduce((s, { weight }) => s + weight, 0);
1414
1444
  if (Math.abs(totalWeight) <= eps) totalWeight = 0;
1415
1445
  if (Math.abs(1 - totalWeight) <= eps) totalWeight = 1;
1416
- if (totalWeight > 1 + eps) {
1446
+ if (totalWeight > 1 + EPS) {
1417
1447
  throw new Error(
1418
- `PMF.exclusive: total weight ${totalWeight.toFixed(6)} exceeds 1.`
1448
+ `PMF.exclusive: total weight ${totalWeight} exceeds 1. (epsilon: ${eps})`
1419
1449
  );
1420
1450
  }
1421
1451
  let out = _PMF.empty(eps);
@@ -1457,6 +1487,44 @@ var _PMF = class _PMF {
1457
1487
  }
1458
1488
  return out;
1459
1489
  }
1490
+ /**
1491
+ * Adds damage attribution metadata to this PMF based on existing count metadata.
1492
+ * For each bin, sets attr[outcome] = damage × count[outcome].
1493
+ *
1494
+ * This enables damage attribution charts to work with builder-generated PMFs.
1495
+ * The parser generates attr automatically, but builder PMFs only have count.
1496
+ *
1497
+ * @returns New PMF with attr field populated in each bin
1498
+ */
1499
+ withAttribution() {
1500
+ for (const [damage, bin] of this.map) {
1501
+ if (damage !== 0 && bin.attr && Object.keys(bin.attr).length > 0) {
1502
+ return this;
1503
+ }
1504
+ if (damage > 0) break;
1505
+ }
1506
+ const newMap = /* @__PURE__ */ new Map();
1507
+ for (const [damage, bin] of this.map) {
1508
+ const attr = {};
1509
+ for (const outcome in bin.count) {
1510
+ const probability = bin.count[outcome];
1511
+ if (probability > 0) {
1512
+ attr[outcome] = damage * probability;
1513
+ }
1514
+ }
1515
+ newMap.set(damage, {
1516
+ p: bin.p,
1517
+ count: { ...bin.count },
1518
+ attr: Object.keys(attr).length > 0 ? attr : void 0
1519
+ });
1520
+ }
1521
+ return new _PMF(
1522
+ newMap,
1523
+ this.epsilon,
1524
+ this.normalized,
1525
+ `${this.identifier}~attr`
1526
+ );
1527
+ }
1460
1528
  /**
1461
1529
  * General-purpose N-way mixture.
1462
1530
  * weights: Array of [weight, PMF].