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