@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.
@@ -1,4 +1,4 @@
1
- import { P as PMF, C as CritConfig, i as DiceQuery, L as LRUCache } from '../pmf-CYaEbY7m.cjs';
1
+ import { P as PMF, C as CritConfig, i as DiceQuery, L as LRUCache } from '../pmf-BC1poIqF.cjs';
2
2
 
3
3
  type ExpressionNode = DieNode | ConstantNode | SumNode | AddNode | KeepNode | D20RollNode | HalfNode | MaxOfNode;
4
4
  type DieNode = {
@@ -1,4 +1,4 @@
1
- import { P as PMF, C as CritConfig, i as DiceQuery, L as LRUCache } from '../pmf-CYaEbY7m.js';
1
+ import { P as PMF, C as CritConfig, i as DiceQuery, L as LRUCache } from '../pmf-BC1poIqF.js';
2
2
 
3
3
  type ExpressionNode = DieNode | ConstantNode | SumNode | AddNode | KeepNode | D20RollNode | HalfNode | MaxOfNode;
4
4
  type DieNode = {
@@ -52,6 +52,7 @@ var _DiceQuery = class _DiceQuery {
52
52
  constructor(singles, combined, eps = EPS) {
53
53
  __publicField(this, "singles");
54
54
  __publicField(this, "combined");
55
+ __publicField(this, "_combinedWithAttr");
55
56
  this.singles = Array.isArray(singles) ? singles : [singles];
56
57
  if (this.singles.some((s) => s === void 0)) {
57
58
  throw new Error("DiceQuery contains undefined singles");
@@ -59,6 +60,35 @@ var _DiceQuery = class _DiceQuery {
59
60
  const c = combined ?? PMF.convolveMany(this.singles);
60
61
  this.combined = Math.abs(c.mass() - 1) <= eps ? c : c.normalize();
61
62
  }
63
+ /**
64
+ * Returns a new PMF with damage attribution metadata populated.
65
+ *
66
+ * This method computes attribution on-demand for builder-generated PMFs,
67
+ * enabling them to work with damage attribution charts. The `attr` field
68
+ * tracks how much damage each outcome type contributes at each damage value.
69
+ *
70
+ * For each bin at damage D: sum(attr.values()) ≈ D × P(damage = D)
71
+ *
72
+ * Performance: Cached after first call. Adds minimal overhead vs `combined`.
73
+ *
74
+ * @returns PMF with attr field populated for damage attribution charts
75
+ *
76
+ * @example
77
+ * const attack = d20.plus(5).ac(15).onHit(d(2,6).plus(3)).onCrit(d(2,6))
78
+ * const query = attack.toQuery()
79
+ * const pmf = query.combinedWithAttribution()
80
+ * // Now pmf can be used with toDamageAttributionChartSeries()
81
+ */
82
+ combinedWithAttribution() {
83
+ if (this._combinedWithAttr) {
84
+ return this._combinedWithAttr;
85
+ }
86
+ const singlesWithAttr = this.singles.map((pmf) => pmf.withAttribution());
87
+ const combined = PMF.convolveMany(singlesWithAttr, this.combined.epsilon);
88
+ const normalized = Math.abs(combined.mass() - 1) <= this.combined.epsilon ? combined : combined.normalize();
89
+ this._combinedWithAttr = normalized;
90
+ return normalized;
91
+ }
62
92
  /**
63
93
  * Returns the expected damage across all possible outcomes.
64
94
  *
@@ -1403,9 +1433,9 @@ var _PMF = class _PMF {
1403
1433
  let totalWeight = items.reduce((s, { weight }) => s + weight, 0);
1404
1434
  if (Math.abs(totalWeight) <= eps) totalWeight = 0;
1405
1435
  if (Math.abs(1 - totalWeight) <= eps) totalWeight = 1;
1406
- if (totalWeight > 1 + eps) {
1436
+ if (totalWeight > 1 + EPS) {
1407
1437
  throw new Error(
1408
- `PMF.exclusive: total weight ${totalWeight.toFixed(6)} exceeds 1.`
1438
+ `PMF.exclusive: total weight ${totalWeight} exceeds 1. (epsilon: ${eps})`
1409
1439
  );
1410
1440
  }
1411
1441
  let out = _PMF.empty(eps);
@@ -1447,6 +1477,44 @@ var _PMF = class _PMF {
1447
1477
  }
1448
1478
  return out;
1449
1479
  }
1480
+ /**
1481
+ * Adds damage attribution metadata to this PMF based on existing count metadata.
1482
+ * For each bin, sets attr[outcome] = damage × count[outcome].
1483
+ *
1484
+ * This enables damage attribution charts to work with builder-generated PMFs.
1485
+ * The parser generates attr automatically, but builder PMFs only have count.
1486
+ *
1487
+ * @returns New PMF with attr field populated in each bin
1488
+ */
1489
+ withAttribution() {
1490
+ for (const [damage, bin] of this.map) {
1491
+ if (damage !== 0 && bin.attr && Object.keys(bin.attr).length > 0) {
1492
+ return this;
1493
+ }
1494
+ if (damage > 0) break;
1495
+ }
1496
+ const newMap = /* @__PURE__ */ new Map();
1497
+ for (const [damage, bin] of this.map) {
1498
+ const attr = {};
1499
+ for (const outcome in bin.count) {
1500
+ const probability = bin.count[outcome];
1501
+ if (probability > 0) {
1502
+ attr[outcome] = damage * probability;
1503
+ }
1504
+ }
1505
+ newMap.set(damage, {
1506
+ p: bin.p,
1507
+ count: { ...bin.count },
1508
+ attr: Object.keys(attr).length > 0 ? attr : void 0
1509
+ });
1510
+ }
1511
+ return new _PMF(
1512
+ newMap,
1513
+ this.epsilon,
1514
+ this.normalized,
1515
+ `${this.identifier}~attr`
1516
+ );
1517
+ }
1450
1518
  /**
1451
1519
  * General-purpose N-way mixture.
1452
1520
  * weights: Array of [weight, PMF].