@yipe/dice 0.2.1 → 0.2.3
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/README.md +20 -30
- package/dist/builder/index.cjs +588 -552
- package/dist/builder/index.cjs.map +1 -1
- package/dist/builder/index.d.cts +15 -3
- package/dist/builder/index.d.ts +15 -3
- package/dist/builder/index.js +588 -552
- package/dist/builder/index.js.map +1 -1
- package/dist/index.cjs +63 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +56 -20
- package/dist/index.js.map +1 -1
- package/dist/{pmf-DIegvlyA.d.cts → pmf-D1p2Re-J.d.cts} +29 -12
- package/dist/{pmf-DIegvlyA.d.ts → pmf-D1p2Re-J.d.ts} +29 -12
- package/package.json +21 -18
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { P as PMF } from './pmf-
|
|
2
|
-
export { B as Bin, D as DamageDistribution,
|
|
1
|
+
import { P as PMF } from './pmf-D1p2Re-J.cjs';
|
|
2
|
+
export { B as Bin, 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-D1p2Re-J.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-
|
|
2
|
-
export { B as Bin, D as DamageDistribution,
|
|
1
|
+
import { P as PMF } from './pmf-D1p2Re-J.js';
|
|
2
|
+
export { B as Bin, 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-D1p2Re-J.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
|
@@ -46,6 +46,14 @@ var LRUCache = class {
|
|
|
46
46
|
|
|
47
47
|
// src/common/types.ts
|
|
48
48
|
var EPS = 1e-12;
|
|
49
|
+
var onAnyHit = ["hit", "crit"];
|
|
50
|
+
var onCritOnly = ["crit"];
|
|
51
|
+
var onHitOnly = ["hit"];
|
|
52
|
+
var onMissOnly = ["missNone", "missDamage"];
|
|
53
|
+
var onMissDamageOnly = ["missDamage"];
|
|
54
|
+
var onSaveHalfOnly = ["saveHalf"];
|
|
55
|
+
var onSaveFailOnly = ["saveFail"];
|
|
56
|
+
var onPotentCantripOnly = ["pc"];
|
|
49
57
|
|
|
50
58
|
// src/pmf/query.ts
|
|
51
59
|
var _DiceQuery = class _DiceQuery {
|
|
@@ -1385,40 +1393,68 @@ var _PMF = class _PMF {
|
|
|
1385
1393
|
* - A PMF representing its outcome (e.g., damage dice).
|
|
1386
1394
|
* - A weight representing its probability of being selected.
|
|
1387
1395
|
*
|
|
1388
|
-
* This is useful for modeling situations like:
|
|
1389
|
-
* - Sneak Attack: 6d6 if first hit is a crit, 3d6 if first hit is non-crit, 0 otherwise.
|
|
1390
|
-
* - Multiple exclusive spells: Fireball vs Cone of Cold vs nothing.
|
|
1391
|
-
*
|
|
1392
1396
|
* Notes:
|
|
1393
|
-
* - If
|
|
1394
|
-
*
|
|
1395
|
-
*
|
|
1396
|
-
*
|
|
1397
|
-
* @param options Array of `{ pmf, weight }` objects, each representing an outcome and its probability.
|
|
1398
|
-
* @param label Optional name for debugging or charting.
|
|
1399
|
-
* @param eps Optional tolerance for floating point rounding
|
|
1400
|
-
* @returns A single PMF representing the exclusive mixture of all options.
|
|
1397
|
+
* - If total weight < 1 (within eps), leftover mass is assumed to be PMF.zero()
|
|
1398
|
+
*
|
|
1399
|
+
* @param options Array of `{ pmf, weight }` or `[PMF, number]`.
|
|
1400
|
+
* @param eps Optional tolerance for floating point rounding.
|
|
1401
1401
|
*/
|
|
1402
1402
|
static exclusive(options, eps = EPS) {
|
|
1403
1403
|
const items = options.map(
|
|
1404
1404
|
(o) => Array.isArray(o) ? { pmf: o[0], weight: o[1] } : o
|
|
1405
1405
|
);
|
|
1406
|
-
|
|
1407
|
-
|
|
1406
|
+
for (const { weight } of items) {
|
|
1407
|
+
if (!Number.isFinite(weight) || weight < -eps) {
|
|
1408
|
+
throw new Error(`PMF.exclusive: invalid weight ${weight}.`);
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
let totalWeight = items.reduce((s, { weight }) => s + weight, 0);
|
|
1412
|
+
if (Math.abs(totalWeight) <= eps) totalWeight = 0;
|
|
1413
|
+
if (Math.abs(1 - totalWeight) <= eps) totalWeight = 1;
|
|
1414
|
+
if (totalWeight > 1 + eps) {
|
|
1408
1415
|
throw new Error(
|
|
1409
1416
|
`PMF.exclusive: total weight ${totalWeight.toFixed(6)} exceeds 1.`
|
|
1410
1417
|
);
|
|
1411
1418
|
}
|
|
1412
|
-
let out =
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
const leftover = 1 - totalWeight;
|
|
1419
|
+
let out = _PMF.empty(eps);
|
|
1420
|
+
for (const { pmf, weight } of items) {
|
|
1421
|
+
if (weight > eps) out = out.addScaled(pmf, weight);
|
|
1422
|
+
}
|
|
1423
|
+
const leftover = Math.max(0, 1 - totalWeight);
|
|
1417
1424
|
if (leftover > eps) {
|
|
1418
1425
|
out = out.addScaled(_PMF.zero(), leftover);
|
|
1419
1426
|
}
|
|
1420
1427
|
return out;
|
|
1421
1428
|
}
|
|
1429
|
+
/**
|
|
1430
|
+
* PMF.mix()
|
|
1431
|
+
*
|
|
1432
|
+
* Builds a PMF as a linear combination of input PMFs with the given weights.
|
|
1433
|
+
* Unlike `exclusive`, this does NOT:
|
|
1434
|
+
* - enforce that weights sum to 1
|
|
1435
|
+
* - add leftover probability to δ0 (PMF.zero())
|
|
1436
|
+
*
|
|
1437
|
+
* Use when outcomes are not mutually exclusive, or for interpolation/blending.
|
|
1438
|
+
*
|
|
1439
|
+
* @param options Array of `{ pmf, weight }` or `[PMF, number]`.
|
|
1440
|
+
* @param eps Optional tolerance for skipping tiny weights.
|
|
1441
|
+
*/
|
|
1442
|
+
static mix(options, eps = EPS) {
|
|
1443
|
+
const items = options.map(
|
|
1444
|
+
(o) => Array.isArray(o) ? { pmf: o[0], weight: o[1] } : o
|
|
1445
|
+
);
|
|
1446
|
+
for (const { weight } of items) {
|
|
1447
|
+
if (!Number.isFinite(weight)) {
|
|
1448
|
+
throw new Error(`PMF.mix: invalid weight ${weight}.`);
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
let out = _PMF.empty(eps);
|
|
1452
|
+
for (const { pmf, weight } of items) {
|
|
1453
|
+
if (Math.abs(weight) <= eps) continue;
|
|
1454
|
+
out = out.addScaled(pmf, weight);
|
|
1455
|
+
}
|
|
1456
|
+
return out;
|
|
1457
|
+
}
|
|
1422
1458
|
/**
|
|
1423
1459
|
* General-purpose N-way mixture.
|
|
1424
1460
|
* weights: Array of [weight, PMF].
|
|
@@ -3134,6 +3170,6 @@ var Mixture = class _Mixture {
|
|
|
3134
3170
|
}
|
|
3135
3171
|
};
|
|
3136
3172
|
|
|
3137
|
-
export { DiceQuery, EPS, LRUCache, Mixture, PMF, clearParserCache, getCachingEnabled, parse, pmfCache, setCachingEnabled };
|
|
3173
|
+
export { DiceQuery, EPS, LRUCache, Mixture, PMF, clearParserCache, getCachingEnabled, onAnyHit, onCritOnly, onHitOnly, onMissDamageOnly, onMissOnly, onPotentCantripOnly, onSaveFailOnly, onSaveHalfOnly, parse, pmfCache, setCachingEnabled };
|
|
3138
3174
|
//# sourceMappingURL=index.js.map
|
|
3139
3175
|
//# sourceMappingURL=index.js.map
|