@yipe/dice 0.8.0 → 0.9.0
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/CHANGELOG.md +456 -0
- package/README.md +169 -19
- package/dist/builder/dc.d.ts +8 -0
- package/dist/builder/dc.d.ts.map +1 -1
- package/dist/builder/index.cjs +735 -10
- package/dist/builder/index.cjs.map +1 -1
- package/dist/builder/index.d.ts +1 -0
- package/dist/builder/index.d.ts.map +1 -1
- package/dist/builder/index.js +729 -11
- package/dist/builder/index.js.map +1 -1
- package/dist/builder/roll.d.ts +2 -0
- package/dist/builder/roll.d.ts.map +1 -1
- package/dist/builder/save.d.ts +9 -0
- package/dist/builder/save.d.ts.map +1 -1
- package/dist/index.cjs +114 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +113 -1
- package/dist/index.js.map +1 -1
- package/dist/parser/rollType.d.ts +58 -0
- package/dist/parser/rollType.d.ts.map +1 -0
- package/dist/pmf/query.d.ts +26 -0
- package/dist/pmf/query.d.ts.map +1 -1
- package/dist/turn/index.d.ts +3 -0
- package/dist/turn/index.d.ts.map +1 -0
- package/dist/turn/plan.d.ts +55 -0
- package/dist/turn/plan.d.ts.map +1 -0
- package/dist/turn/state.d.ts +23 -0
- package/dist/turn/state.d.ts.map +1 -0
- package/dist/turn/turn.d.ts +149 -0
- package/dist/turn/turn.d.ts.map +1 -0
- package/dist/turn/types.d.ts +99 -0
- package/dist/turn/types.d.ts.map +1 -0
- package/package.json +2 -2
package/dist/builder/index.js
CHANGED
|
@@ -115,6 +115,10 @@ var _DiceQuery = class _DiceQuery {
|
|
|
115
115
|
if (this._combinedWithAttr) {
|
|
116
116
|
return this._combinedWithAttr;
|
|
117
117
|
}
|
|
118
|
+
if (this._combinedProvided) {
|
|
119
|
+
this._combinedWithAttr = this.combined.withAttribution();
|
|
120
|
+
return this._combinedWithAttr;
|
|
121
|
+
}
|
|
118
122
|
if (this.singles.every((pmf) => pmf.hasAttribution())) {
|
|
119
123
|
this._combinedWithAttr = this.combined;
|
|
120
124
|
return this._combinedWithAttr;
|
|
@@ -885,6 +889,55 @@ var _DiceQuery = class _DiceQuery {
|
|
|
885
889
|
}
|
|
886
890
|
return out;
|
|
887
891
|
}
|
|
892
|
+
/**
|
|
893
|
+
* Per-outcome probabilities and damage ranges, aggregated over the individual
|
|
894
|
+
* singles rather than read off the combined distribution.
|
|
895
|
+
*
|
|
896
|
+
* `damageRange` is the sum, over every single that can produce the outcome, of
|
|
897
|
+
* that single's own conditional damage range: "what this outcome contributes
|
|
898
|
+
* across the whole turn when every attack that can produce it does". Linear in
|
|
899
|
+
* the number of attacks by construction.
|
|
900
|
+
*
|
|
901
|
+
* Prefer this over {@link DiceQuery.snapshot} for a multi-attack query.
|
|
902
|
+
* `snapshot` reads `damageRange` off the combined PMF's `count`, which the
|
|
903
|
+
* convolution accumulates as an expected count, so its `avg` is size-biased
|
|
904
|
+
* for N≥2 (its own doc comment says so). The two agree for a single attack.
|
|
905
|
+
*
|
|
906
|
+
* Only outcomes that actually occur appear in the result.
|
|
907
|
+
*
|
|
908
|
+
* Like every `singles`-based helper on this class, it describes the singles
|
|
909
|
+
* and not an explicitly provided `combined`. `Turn.toQuery()` supplies one whose
|
|
910
|
+
* distribution also contains rider attacks that are absent from `singles`
|
|
911
|
+
* (an `otherwise([unarmed, unarmed])` flurry, say), so those attacks do not
|
|
912
|
+
* appear here. For rider-inclusive figures read the combined distribution
|
|
913
|
+
* directly: {@link DiceQuery.outcomeTotals}, {@link DiceQuery.outcomeDamageRanges}.
|
|
914
|
+
*
|
|
915
|
+
* @param outcomes Which outcomes to consider; defaults to every canonical one.
|
|
916
|
+
*/
|
|
917
|
+
outcomeStats(outcomes = ALL_OUTCOME_TYPES) {
|
|
918
|
+
const stats = /* @__PURE__ */ new Map();
|
|
919
|
+
const perSingle = this.singles.map((pmf) => new _DiceQuery([pmf], void 0, this._eps));
|
|
920
|
+
for (const outcome of outcomes) {
|
|
921
|
+
const atLeastOneProbability = this.probAtLeastOne(outcome);
|
|
922
|
+
if (atLeastOneProbability <= 0) continue;
|
|
923
|
+
const damageRange = { min: 0, avg: 0, max: 0 };
|
|
924
|
+
let contributors = 0;
|
|
925
|
+
for (const single of perSingle) {
|
|
926
|
+
if (single.probAtLeastOne(outcome) <= 0) continue;
|
|
927
|
+
contributors++;
|
|
928
|
+
const stat = single.damageStatsFrom(outcome);
|
|
929
|
+
damageRange.min += stat.min;
|
|
930
|
+
damageRange.avg += stat.avg;
|
|
931
|
+
damageRange.max += stat.max;
|
|
932
|
+
}
|
|
933
|
+
stats.set(outcome, {
|
|
934
|
+
atLeastOneProbability,
|
|
935
|
+
allProbability: contributors > 0 ? this.probExactlyK(outcome, contributors) : 0,
|
|
936
|
+
damageRange
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
return stats;
|
|
940
|
+
}
|
|
888
941
|
/**
|
|
889
942
|
* Snapshot of the distribution in the exact shape the UI consumes.
|
|
890
943
|
* - outcome probabilities are "at least one" (and equal to "all" for a single PMF)
|
|
@@ -3452,6 +3505,10 @@ function d20PMF(rerollOne) {
|
|
|
3452
3505
|
}
|
|
3453
3506
|
|
|
3454
3507
|
// src/builder/roll.ts
|
|
3508
|
+
var rollPMFCache = new LRUCache(4e3);
|
|
3509
|
+
function clearRollCache() {
|
|
3510
|
+
rollPMFCache.clear();
|
|
3511
|
+
}
|
|
3455
3512
|
var defaultConfig = {
|
|
3456
3513
|
count: 1,
|
|
3457
3514
|
sides: 0,
|
|
@@ -3864,8 +3921,16 @@ var RollBuilder = class _RollBuilder {
|
|
|
3864
3921
|
}
|
|
3865
3922
|
return result.replace(/\+ -/g, "-");
|
|
3866
3923
|
}
|
|
3924
|
+
// Main AST entry point. Cached by the cheap config key across identical rebuilds; see `rollPMFCache`.
|
|
3867
3925
|
toPMF(eps = 0) {
|
|
3868
|
-
|
|
3926
|
+
const key = this.cacheKey();
|
|
3927
|
+
if (key === null) return pmfFromRollBuilder(this, eps);
|
|
3928
|
+
const fullKey = `${key}*e${eps}`;
|
|
3929
|
+
const cached = rollPMFCache.get(fullKey);
|
|
3930
|
+
if (cached) return cached;
|
|
3931
|
+
const pmf = pmfFromRollBuilder(this, eps);
|
|
3932
|
+
rollPMFCache.set(fullKey, pmf);
|
|
3933
|
+
return pmf;
|
|
3869
3934
|
}
|
|
3870
3935
|
get pmf() {
|
|
3871
3936
|
return this.toPMF();
|
|
@@ -5125,11 +5190,11 @@ var AttackBuilder = class _AttackBuilder {
|
|
|
5125
5190
|
pMiss: pmiss
|
|
5126
5191
|
} = this.resolveProbabilities(this.check, eps);
|
|
5127
5192
|
const hitPMF = this.hitEffect ? this.hitEffect instanceof ParsedRollBuilder ? this.hitEffect.toPMF(eps) : pmfFromRollBuilder(this.hitEffect, eps) : PMF.delta(0, eps);
|
|
5128
|
-
let
|
|
5193
|
+
let critPMF2 = null;
|
|
5129
5194
|
let phit = pHit;
|
|
5130
5195
|
let pcrit = pCrit;
|
|
5131
5196
|
if (this.critEffect === null) {
|
|
5132
|
-
|
|
5197
|
+
critPMF2 = null;
|
|
5133
5198
|
phit += pcrit;
|
|
5134
5199
|
pcrit = 0;
|
|
5135
5200
|
} else {
|
|
@@ -5137,7 +5202,7 @@ var AttackBuilder = class _AttackBuilder {
|
|
|
5137
5202
|
if (this.critEffect) {
|
|
5138
5203
|
critBuilder = this.critEffect;
|
|
5139
5204
|
} else if (this.hitEffect instanceof ParsedRollBuilder) {
|
|
5140
|
-
|
|
5205
|
+
critPMF2 = null;
|
|
5141
5206
|
phit += pcrit;
|
|
5142
5207
|
pcrit = 0;
|
|
5143
5208
|
critBuilder = void 0;
|
|
@@ -5145,20 +5210,20 @@ var AttackBuilder = class _AttackBuilder {
|
|
|
5145
5210
|
critBuilder = this.hitEffect?.copy().doubleDice();
|
|
5146
5211
|
}
|
|
5147
5212
|
if (critBuilder) {
|
|
5148
|
-
|
|
5213
|
+
critPMF2 = critBuilder instanceof ParsedRollBuilder ? critBuilder.toPMF(eps) : pmfFromRollBuilder(critBuilder, eps);
|
|
5149
5214
|
}
|
|
5150
5215
|
}
|
|
5151
5216
|
const missPMF = this.missEffect ? this.missEffect instanceof ParsedRollBuilder ? this.missEffect.toPMF(eps) : pmfFromRollBuilder(this.missEffect, eps) : PMF.delta(0, eps);
|
|
5152
5217
|
const mix = new Mixture(eps);
|
|
5153
5218
|
if (phit > 0) mix.add("hit", hitPMF, phit);
|
|
5154
|
-
if (
|
|
5219
|
+
if (critPMF2 && pcrit > 0) mix.add("crit", critPMF2, pcrit);
|
|
5155
5220
|
if (pmiss > 0)
|
|
5156
5221
|
mix.add(this.missEffect ? "missDamage" : "missNone", missPMF, pmiss);
|
|
5157
5222
|
return {
|
|
5158
5223
|
pmf: mix.buildPMF(eps) ?? PMF.delta(0, eps),
|
|
5159
5224
|
check: this.check.toPMF(eps) ?? PMF.delta(0, eps),
|
|
5160
5225
|
hit: hitPMF ?? PMF.delta(0, eps),
|
|
5161
|
-
crit:
|
|
5226
|
+
crit: critPMF2 ?? PMF.delta(0, eps),
|
|
5162
5227
|
miss: missPMF ?? PMF.delta(0, eps),
|
|
5163
5228
|
weights: { hit: phit, crit: pcrit, miss: pmiss }
|
|
5164
5229
|
};
|
|
@@ -5294,6 +5359,10 @@ RollBuilder.prototype.ac = function(targetAC) {
|
|
|
5294
5359
|
};
|
|
5295
5360
|
|
|
5296
5361
|
// src/builder/save.ts
|
|
5362
|
+
var savePMFCache = new LRUCache(4e3);
|
|
5363
|
+
function clearSaveCache() {
|
|
5364
|
+
savePMFCache.clear();
|
|
5365
|
+
}
|
|
5297
5366
|
var SaveBuilder = class _SaveBuilder {
|
|
5298
5367
|
constructor(check, failureEffect, saveOutcome = "normal") {
|
|
5299
5368
|
this.check = check;
|
|
@@ -5330,9 +5399,32 @@ var SaveBuilder = class _SaveBuilder {
|
|
|
5330
5399
|
weights: { success: psuccess, fail: pfail }
|
|
5331
5400
|
};
|
|
5332
5401
|
}
|
|
5333
|
-
|
|
5402
|
+
/**
|
|
5403
|
+
* A cheap, complete key for this save's resolved PMF, or `null` when it can't be cached soundly.
|
|
5404
|
+
* {@link resolve} reads exactly three things: the DC check (via `resolveProbabilities`), the failure
|
|
5405
|
+
* effect's PMF, and the save outcome — so composing their keys pins it. A `ParsedRollBuilder` failure
|
|
5406
|
+
* effect returns `null`, which correctly forces this uncached.
|
|
5407
|
+
*/
|
|
5408
|
+
cacheKey(eps) {
|
|
5409
|
+
const checkKey = this.check.cacheKey();
|
|
5410
|
+
if (checkKey === null) return null;
|
|
5411
|
+
let failKey = "";
|
|
5412
|
+
if (this.failureEffect) {
|
|
5413
|
+
const k = this.failureEffect.cacheKey();
|
|
5414
|
+
if (k === null) return null;
|
|
5415
|
+
failKey = k;
|
|
5416
|
+
}
|
|
5417
|
+
return `${checkKey}*F${failKey}*O${this.saveOutcome}*e${eps}`;
|
|
5418
|
+
}
|
|
5419
|
+
// By default, create PMF with no pruning. Cached by the cheap config key across identical rebuilds.
|
|
5334
5420
|
toPMF(eps = 0) {
|
|
5335
|
-
|
|
5421
|
+
const key = this.cacheKey(eps);
|
|
5422
|
+
if (key === null) return this.resolve(eps).pmf;
|
|
5423
|
+
const cached = savePMFCache.get(key);
|
|
5424
|
+
if (cached) return cached;
|
|
5425
|
+
const pmf = this.resolve(eps).pmf;
|
|
5426
|
+
savePMFCache.set(key, pmf);
|
|
5427
|
+
return pmf;
|
|
5336
5428
|
}
|
|
5337
5429
|
get pmf() {
|
|
5338
5430
|
return this.toPMF();
|
|
@@ -5368,6 +5460,10 @@ function resolveProbabilities(check) {
|
|
|
5368
5460
|
}
|
|
5369
5461
|
|
|
5370
5462
|
// src/builder/dc.ts
|
|
5463
|
+
var dcPMFCache = new LRUCache(4e3);
|
|
5464
|
+
function clearDCCache() {
|
|
5465
|
+
dcPMFCache.clear();
|
|
5466
|
+
}
|
|
5371
5467
|
var DCBuilder = class _DCBuilder extends RollBuilder {
|
|
5372
5468
|
constructor(baseRoll, saveConfig) {
|
|
5373
5469
|
super(baseRoll.getSubRollConfigs());
|
|
@@ -5408,7 +5504,22 @@ var DCBuilder = class _DCBuilder extends RollBuilder {
|
|
|
5408
5504
|
const expression = new RollBuilder(allConfigs).toExpression();
|
|
5409
5505
|
return `(${expression} DC ${this.saveConfig.dc})`;
|
|
5410
5506
|
}
|
|
5507
|
+
/**
|
|
5508
|
+
* The DC check's PMF is fully determined by the save DC plus everything `toPMF` below reads off the roll
|
|
5509
|
+
* configs (`rollType`, `baseReroll`, `modifier`, bonus dice) — all of which `super.cacheKey()` already
|
|
5510
|
+
* serializes. So extend the base key with the DC, mirroring {@link AlwaysHitBuilder.cacheKey}.
|
|
5511
|
+
*/
|
|
5512
|
+
cacheKey() {
|
|
5513
|
+
const base = super.cacheKey();
|
|
5514
|
+
return base === null ? null : `DC|${this.saveConfig.dc}|${base}`;
|
|
5515
|
+
}
|
|
5411
5516
|
toPMF(eps = 0) {
|
|
5517
|
+
const key = this.cacheKey();
|
|
5518
|
+
const fullKey = key === null ? null : `${key}*e${eps}`;
|
|
5519
|
+
if (fullKey !== null) {
|
|
5520
|
+
const cached = dcPMFCache.get(fullKey);
|
|
5521
|
+
if (cached) return cached;
|
|
5522
|
+
}
|
|
5412
5523
|
const saveDC = this.saveDC;
|
|
5413
5524
|
const rollType = this.rollType;
|
|
5414
5525
|
const rerollOne = this.baseReroll > 0;
|
|
@@ -5430,7 +5541,9 @@ var DCBuilder = class _DCBuilder extends RollBuilder {
|
|
|
5430
5541
|
[0, psuccess > 0 ? psuccess : 0],
|
|
5431
5542
|
[1, pfail > 0 ? pfail : 0]
|
|
5432
5543
|
]);
|
|
5433
|
-
|
|
5544
|
+
const pmf = PMF.fromMap(m, eps);
|
|
5545
|
+
if (fullKey !== null) dcPMFCache.set(fullKey, pmf);
|
|
5546
|
+
return pmf;
|
|
5434
5547
|
}
|
|
5435
5548
|
};
|
|
5436
5549
|
RollBuilder.prototype.dc = function(saveDC) {
|
|
@@ -5438,6 +5551,611 @@ RollBuilder.prototype.dc = function(saveDC) {
|
|
|
5438
5551
|
return new DCBuilder(this).dc(saveDC);
|
|
5439
5552
|
};
|
|
5440
5553
|
|
|
5441
|
-
|
|
5554
|
+
// src/turn/types.ts
|
|
5555
|
+
var TurnSpecError = class extends Error {
|
|
5556
|
+
constructor(code, id, message) {
|
|
5557
|
+
super(message);
|
|
5558
|
+
this.code = code;
|
|
5559
|
+
this.id = id;
|
|
5560
|
+
this.name = "TurnSpecError";
|
|
5561
|
+
}
|
|
5562
|
+
};
|
|
5563
|
+
var MAX_TRIGGER_GROUPS = 4;
|
|
5564
|
+
|
|
5565
|
+
// src/turn/plan.ts
|
|
5566
|
+
var IS_HIT_TRIGGER = {
|
|
5567
|
+
"first-hit": true,
|
|
5568
|
+
"any-crit": true,
|
|
5569
|
+
"any-miss": true,
|
|
5570
|
+
"every-hit": true
|
|
5571
|
+
};
|
|
5572
|
+
function toPMF(damage, eps, id = "") {
|
|
5573
|
+
const parts = Array.isArray(damage) ? damage : [damage];
|
|
5574
|
+
if (parts.length === 0) return PMF.delta(0, eps);
|
|
5575
|
+
const pmfs = parts.map((part) => {
|
|
5576
|
+
if (part instanceof PMF) return part;
|
|
5577
|
+
if (typeof part.toPMF !== "function") {
|
|
5578
|
+
throw new TurnSpecError(
|
|
5579
|
+
"not-an-attack",
|
|
5580
|
+
id,
|
|
5581
|
+
`"${id}" is neither a PMF nor a builder with toPMF().`
|
|
5582
|
+
);
|
|
5583
|
+
}
|
|
5584
|
+
const resolved = part.toPMF(eps);
|
|
5585
|
+
if (!(resolved instanceof PMF)) {
|
|
5586
|
+
throw new TurnSpecError(
|
|
5587
|
+
"not-an-attack",
|
|
5588
|
+
id,
|
|
5589
|
+
`"${id}" has a toPMF() that did not return a PMF.`
|
|
5590
|
+
);
|
|
5591
|
+
}
|
|
5592
|
+
return resolved;
|
|
5593
|
+
});
|
|
5594
|
+
return PMF.convolveMany(pmfs, eps);
|
|
5595
|
+
}
|
|
5596
|
+
function critPMF(rider, base, eps) {
|
|
5597
|
+
if (rider.critDamage !== void 0) return toPMF(rider.critDamage, eps);
|
|
5598
|
+
const parts = Array.isArray(rider.damage) ? rider.damage : [rider.damage];
|
|
5599
|
+
const doubled = [];
|
|
5600
|
+
for (const part of parts) {
|
|
5601
|
+
const doublable = part;
|
|
5602
|
+
if (part instanceof PMF || typeof doublable.doubleDice !== "function") {
|
|
5603
|
+
return base;
|
|
5604
|
+
}
|
|
5605
|
+
try {
|
|
5606
|
+
doubled.push(toPMF(doublable.doubleDice(), eps));
|
|
5607
|
+
} catch {
|
|
5608
|
+
return base;
|
|
5609
|
+
}
|
|
5610
|
+
}
|
|
5611
|
+
if (doubled.length === 0) return base;
|
|
5612
|
+
return PMF.convolveMany(doubled, eps);
|
|
5613
|
+
}
|
|
5614
|
+
function sliceSource(pmf) {
|
|
5615
|
+
const labels = pmf.outcomes();
|
|
5616
|
+
if (!labels.includes("hit") && !labels.includes("crit")) return null;
|
|
5617
|
+
const missParts = ["missNone", "missDamage"].filter((label) => labels.includes(label)).map((label) => pmf.filterOutcome(label));
|
|
5618
|
+
return {
|
|
5619
|
+
hit: labels.includes("hit") ? pmf.filterOutcome("hit") : PMF.emptyMass(),
|
|
5620
|
+
crit: labels.includes("crit") ? pmf.filterOutcome("crit") : PMF.emptyMass(),
|
|
5621
|
+
miss: missParts.length ? missParts.reduce((all, part) => all.add(part)) : PMF.emptyMass()
|
|
5622
|
+
};
|
|
5623
|
+
}
|
|
5624
|
+
function buildPlan(spec, eps = EPS) {
|
|
5625
|
+
const fail = (code, id, message) => {
|
|
5626
|
+
throw new TurnSpecError(code, id, message);
|
|
5627
|
+
};
|
|
5628
|
+
const attackIds = [];
|
|
5629
|
+
const attackPMFs = [];
|
|
5630
|
+
const attackSlices = [];
|
|
5631
|
+
spec.attacks.forEach((entry, index) => {
|
|
5632
|
+
const named = entry;
|
|
5633
|
+
const hasWrapper = typeof named.id === "string" && named.source !== void 0;
|
|
5634
|
+
const id = hasWrapper ? named.id : `attack ${index + 1}`;
|
|
5635
|
+
const source = hasWrapper ? named.source : entry;
|
|
5636
|
+
const pmf = toPMF(source, eps, id);
|
|
5637
|
+
attackIds.push(id);
|
|
5638
|
+
attackPMFs.push(pmf);
|
|
5639
|
+
attackSlices.push(sliceSource(pmf));
|
|
5640
|
+
});
|
|
5641
|
+
const riders = spec.riders ?? [];
|
|
5642
|
+
const riderIds = riders.map((rider, index) => rider.id ?? `rider ${index + 1}`);
|
|
5643
|
+
const seen = /* @__PURE__ */ new Set();
|
|
5644
|
+
for (const id of [...attackIds, ...riderIds]) {
|
|
5645
|
+
if (seen.has(id)) fail("duplicate-id", id, `Duplicate id "${id}".`);
|
|
5646
|
+
seen.add(id);
|
|
5647
|
+
}
|
|
5648
|
+
const attackIndexById = new Map(attackIds.map((id, index) => [id, index]));
|
|
5649
|
+
const riderIndexById = new Map(riderIds.map((id, index) => [id, index]));
|
|
5650
|
+
const sourceIdsByRider = riders.map((rider, index) => {
|
|
5651
|
+
const id = riderIds[index];
|
|
5652
|
+
if (rider.on === "not-fired") {
|
|
5653
|
+
const target = rider.of;
|
|
5654
|
+
if (target === id) {
|
|
5655
|
+
fail("self-reference", id, `Rider "${id}" cannot depend on itself.`);
|
|
5656
|
+
}
|
|
5657
|
+
const targetIndex = riderIndexById.get(target);
|
|
5658
|
+
if (targetIndex === void 0) {
|
|
5659
|
+
fail(
|
|
5660
|
+
"unknown-id",
|
|
5661
|
+
target,
|
|
5662
|
+
`Rider "${id}" negates "${target}", which is not a rider in this turn.`
|
|
5663
|
+
);
|
|
5664
|
+
}
|
|
5665
|
+
if (riders[targetIndex].on === "every-hit") {
|
|
5666
|
+
fail(
|
|
5667
|
+
"not-an-attack",
|
|
5668
|
+
target,
|
|
5669
|
+
`Rider "${id}" negates "${target}", an every-hit rider, which can fire more than once and so has no single "did not fire" branch.`
|
|
5670
|
+
);
|
|
5671
|
+
}
|
|
5672
|
+
return [target];
|
|
5673
|
+
}
|
|
5674
|
+
const of = [...new Set(rider.of ?? attackIds)];
|
|
5675
|
+
if (of.length === 0) {
|
|
5676
|
+
fail("unknown-id", id, `Rider "${id}" has no sources.`);
|
|
5677
|
+
}
|
|
5678
|
+
for (const sourceId of of) {
|
|
5679
|
+
if (sourceId === id) {
|
|
5680
|
+
fail("self-reference", id, `Rider "${id}" cannot depend on itself.`);
|
|
5681
|
+
}
|
|
5682
|
+
const riderIndex = riderIndexById.get(sourceId);
|
|
5683
|
+
const isAttack = attackIndexById.has(sourceId);
|
|
5684
|
+
if (!isAttack && riderIndex === void 0) {
|
|
5685
|
+
fail(
|
|
5686
|
+
"unknown-id",
|
|
5687
|
+
sourceId,
|
|
5688
|
+
`Rider "${id}" depends on "${sourceId}", which is not in this turn.`
|
|
5689
|
+
);
|
|
5690
|
+
}
|
|
5691
|
+
if (riderIndex !== void 0 && riders[riderIndex].on === "every-hit") {
|
|
5692
|
+
fail(
|
|
5693
|
+
"not-an-attack",
|
|
5694
|
+
sourceId,
|
|
5695
|
+
`Rider "${id}" triggers on "${sourceId}", an every-hit rider. Those are folded into their own sources rather than resolved separately, so they cannot be triggered on \u2014 point at the attacks instead.`
|
|
5696
|
+
);
|
|
5697
|
+
}
|
|
5698
|
+
const slices = isAttack ? attackSlices[attackIndexById.get(sourceId)] : sliceSource(
|
|
5699
|
+
toPMF(riders[riderIndex].damage, eps, sourceId)
|
|
5700
|
+
);
|
|
5701
|
+
if (!slices) {
|
|
5702
|
+
fail(
|
|
5703
|
+
"not-an-attack",
|
|
5704
|
+
sourceId,
|
|
5705
|
+
`Rider "${id}" triggers on "${sourceId}", which has no hit/crit outcomes.`
|
|
5706
|
+
);
|
|
5707
|
+
}
|
|
5708
|
+
}
|
|
5709
|
+
return [...of];
|
|
5710
|
+
});
|
|
5711
|
+
const order = [];
|
|
5712
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
5713
|
+
const done = /* @__PURE__ */ new Set();
|
|
5714
|
+
const visit = (index) => {
|
|
5715
|
+
if (done.has(index)) return;
|
|
5716
|
+
const id = riderIds[index];
|
|
5717
|
+
if (visiting.has(index)) {
|
|
5718
|
+
fail("cycle", id, `Rider "${id}" is part of a dependency cycle.`);
|
|
5719
|
+
}
|
|
5720
|
+
visiting.add(index);
|
|
5721
|
+
for (const sourceId of sourceIdsByRider[index]) {
|
|
5722
|
+
const dependency = riderIndexById.get(sourceId);
|
|
5723
|
+
if (dependency !== void 0) visit(dependency);
|
|
5724
|
+
}
|
|
5725
|
+
visiting.delete(index);
|
|
5726
|
+
done.add(index);
|
|
5727
|
+
order.push(index);
|
|
5728
|
+
};
|
|
5729
|
+
riders.forEach((_, index) => visit(index));
|
|
5730
|
+
const groupIndexByKey = /* @__PURE__ */ new Map();
|
|
5731
|
+
const groupSources = [];
|
|
5732
|
+
const groupOf = (sourceIds) => {
|
|
5733
|
+
const key = JSON.stringify([...sourceIds].sort());
|
|
5734
|
+
const existing = groupIndexByKey.get(key);
|
|
5735
|
+
if (existing !== void 0) return existing;
|
|
5736
|
+
if (groupSources.length >= MAX_TRIGGER_GROUPS) {
|
|
5737
|
+
fail(
|
|
5738
|
+
"too-many-groups",
|
|
5739
|
+
key,
|
|
5740
|
+
`A turn may track at most ${MAX_TRIGGER_GROUPS} distinct trigger source sets.`
|
|
5741
|
+
);
|
|
5742
|
+
}
|
|
5743
|
+
const index = groupSources.length;
|
|
5744
|
+
groupIndexByKey.set(key, index);
|
|
5745
|
+
groupSources.push([...sourceIds]);
|
|
5746
|
+
return index;
|
|
5747
|
+
};
|
|
5748
|
+
const readsByRider = /* @__PURE__ */ new Map();
|
|
5749
|
+
const perHitGroups = /* @__PURE__ */ new Map();
|
|
5750
|
+
for (const index of order) {
|
|
5751
|
+
const rider = riders[index];
|
|
5752
|
+
if (!IS_HIT_TRIGGER[rider.on]) continue;
|
|
5753
|
+
const group = groupOf(sourceIdsByRider[index]);
|
|
5754
|
+
readsByRider.set(index, group);
|
|
5755
|
+
if (rider.on === "every-hit") perHitGroups.set(riderIds[index], group);
|
|
5756
|
+
}
|
|
5757
|
+
const perHitBySource = /* @__PURE__ */ new Map();
|
|
5758
|
+
for (const index of order) {
|
|
5759
|
+
const rider = riders[index];
|
|
5760
|
+
if (rider.on !== "every-hit") continue;
|
|
5761
|
+
const hit = toPMF(rider.damage, eps, riderIds[index]);
|
|
5762
|
+
const payload = { hit, crit: critPMF(rider, hit, eps) };
|
|
5763
|
+
for (const sourceId of sourceIdsByRider[index]) {
|
|
5764
|
+
const existing = perHitBySource.get(sourceId);
|
|
5765
|
+
if (existing) existing.push(payload);
|
|
5766
|
+
else perHitBySource.set(sourceId, [payload]);
|
|
5767
|
+
}
|
|
5768
|
+
}
|
|
5769
|
+
const updatesById = /* @__PURE__ */ new Map();
|
|
5770
|
+
groupSources.forEach((sourceIds, groupIndex) => {
|
|
5771
|
+
for (const sourceId of sourceIds) {
|
|
5772
|
+
const existing = updatesById.get(sourceId);
|
|
5773
|
+
if (existing) existing.push(groupIndex);
|
|
5774
|
+
else updatesById.set(sourceId, [groupIndex]);
|
|
5775
|
+
}
|
|
5776
|
+
});
|
|
5777
|
+
const withPerHit = (slices, id) => {
|
|
5778
|
+
const payloads = perHitBySource.get(id);
|
|
5779
|
+
if (!payloads) return slices;
|
|
5780
|
+
let hit = slices.hit;
|
|
5781
|
+
let crit = slices.crit;
|
|
5782
|
+
for (const payload of payloads) {
|
|
5783
|
+
hit = hit.convolve(payload.hit, eps, true);
|
|
5784
|
+
crit = crit.convolve(payload.crit, eps, true);
|
|
5785
|
+
}
|
|
5786
|
+
return { hit, crit, miss: slices.miss };
|
|
5787
|
+
};
|
|
5788
|
+
const steps = attackIds.map((id, index) => ({
|
|
5789
|
+
id,
|
|
5790
|
+
trigger: null,
|
|
5791
|
+
slices: withPerHit(
|
|
5792
|
+
attackSlices[index] ?? {
|
|
5793
|
+
hit: attackPMFs[index],
|
|
5794
|
+
crit: PMF.emptyMass(),
|
|
5795
|
+
miss: PMF.emptyMass()
|
|
5796
|
+
},
|
|
5797
|
+
id
|
|
5798
|
+
),
|
|
5799
|
+
damage: null,
|
|
5800
|
+
updates: updatesById.get(id) ?? [],
|
|
5801
|
+
reads: -1,
|
|
5802
|
+
negates: -1
|
|
5803
|
+
}));
|
|
5804
|
+
const stepIndexByRider = /* @__PURE__ */ new Map();
|
|
5805
|
+
const riderSteps = /* @__PURE__ */ new Map();
|
|
5806
|
+
for (const index of order) {
|
|
5807
|
+
const rider = riders[index];
|
|
5808
|
+
if (rider.on === "every-hit") continue;
|
|
5809
|
+
const id = riderIds[index];
|
|
5810
|
+
const hit = toPMF(rider.damage, eps, id);
|
|
5811
|
+
const slices = sliceSource(hit);
|
|
5812
|
+
if (slices && rider.critDamage !== void 0) {
|
|
5813
|
+
fail(
|
|
5814
|
+
"unused-crit-damage",
|
|
5815
|
+
id,
|
|
5816
|
+
`Rider "${id}" rolls its own attack, so its critDamage would never be used. Remove it, or pass plain damage dice instead.`
|
|
5817
|
+
);
|
|
5818
|
+
}
|
|
5819
|
+
const negatedRider = rider.on === "not-fired" ? riderIndexById.get(rider.of) : void 0;
|
|
5820
|
+
const step = {
|
|
5821
|
+
id,
|
|
5822
|
+
trigger: rider,
|
|
5823
|
+
slices: slices ? withPerHit(slices, id) : null,
|
|
5824
|
+
damage: slices ? null : { hit, crit: critPMF(rider, hit, eps) },
|
|
5825
|
+
updates: updatesById.get(id) ?? [],
|
|
5826
|
+
reads: readsByRider.get(index) ?? -1,
|
|
5827
|
+
negates: negatedRider === void 0 ? -1 : stepIndexByRider.get(negatedRider)
|
|
5828
|
+
};
|
|
5829
|
+
steps.push(step);
|
|
5830
|
+
stepIndexByRider.set(index, steps.length - 1);
|
|
5831
|
+
riderSteps.set(id, steps.length - 1);
|
|
5832
|
+
}
|
|
5833
|
+
return {
|
|
5834
|
+
steps,
|
|
5835
|
+
groupCount: groupSources.length,
|
|
5836
|
+
attackPMFs,
|
|
5837
|
+
attackIds,
|
|
5838
|
+
riderIds,
|
|
5839
|
+
riderSteps,
|
|
5840
|
+
perHitGroups
|
|
5841
|
+
};
|
|
5842
|
+
}
|
|
5843
|
+
|
|
5844
|
+
// src/turn/state.ts
|
|
5845
|
+
var FIRST_NONE = 0;
|
|
5846
|
+
var FIRST_HIT = 1;
|
|
5847
|
+
var FIRST_CRIT = 2;
|
|
5848
|
+
var CRIT_BIT = 2;
|
|
5849
|
+
var MISS_BIT = 1;
|
|
5850
|
+
var START_CODE = FIRST_NONE << 2;
|
|
5851
|
+
function advance(code, outcome) {
|
|
5852
|
+
if (outcome === "miss") return code | MISS_BIT;
|
|
5853
|
+
const first = code >> 2;
|
|
5854
|
+
const withCrit = outcome === "crit" ? code | CRIT_BIT : code;
|
|
5855
|
+
if (first !== FIRST_NONE) return withCrit;
|
|
5856
|
+
const nextFirst = outcome === "crit" ? FIRST_CRIT : FIRST_HIT;
|
|
5857
|
+
return nextFirst << 2 | withCrit & (CRIT_BIT | MISS_BIT);
|
|
5858
|
+
}
|
|
5859
|
+
|
|
5860
|
+
// src/turn/turn.ts
|
|
5861
|
+
var OUTCOMES = ["hit", "crit", "miss"];
|
|
5862
|
+
function fireMode(step, codes, firedByStep) {
|
|
5863
|
+
const trigger = step.trigger;
|
|
5864
|
+
if (!trigger) return "hit";
|
|
5865
|
+
if (trigger.on === "not-fired") {
|
|
5866
|
+
return firedByStep[step.negates] === null ? "hit" : null;
|
|
5867
|
+
}
|
|
5868
|
+
const code = codes[step.reads];
|
|
5869
|
+
const first = code >> 2;
|
|
5870
|
+
switch (trigger.on) {
|
|
5871
|
+
case "first-hit":
|
|
5872
|
+
if (first === FIRST_NONE) return null;
|
|
5873
|
+
return first === FIRST_CRIT ? "crit" : "hit";
|
|
5874
|
+
case "any-crit":
|
|
5875
|
+
return (code & CRIT_BIT) !== 0 ? "crit" : null;
|
|
5876
|
+
case "any-miss":
|
|
5877
|
+
return (code & MISS_BIT) !== 0 ? "hit" : null;
|
|
5878
|
+
default:
|
|
5879
|
+
return null;
|
|
5880
|
+
}
|
|
5881
|
+
}
|
|
5882
|
+
var Turn = class _Turn {
|
|
5883
|
+
constructor(attacks, riders, eps) {
|
|
5884
|
+
this.declaredAttacks = [...attacks];
|
|
5885
|
+
this.riders = [...riders];
|
|
5886
|
+
this.eps = eps;
|
|
5887
|
+
this.plan = buildPlan({ attacks: this.declaredAttacks, riders: this.riders }, eps);
|
|
5888
|
+
}
|
|
5889
|
+
/**
|
|
5890
|
+
* Builds a turn from plain data, throwing {@link TurnSpecError} if it is
|
|
5891
|
+
* malformed. Use this from a UI, where `error.code` maps to the field state to
|
|
5892
|
+
* show.
|
|
5893
|
+
*/
|
|
5894
|
+
static from(spec, eps = EPS) {
|
|
5895
|
+
return new _Turn(spec.attacks, spec.riders ?? [], eps);
|
|
5896
|
+
}
|
|
5897
|
+
/**
|
|
5898
|
+
* Appends an attack, throwing {@link TurnSpecError} if that makes the turn
|
|
5899
|
+
* invalid.
|
|
5900
|
+
*
|
|
5901
|
+
* A rider with no explicit `of` watches every declared attack *including ones
|
|
5902
|
+
* appended after it*, because `of` is resolved when the plan is built rather
|
|
5903
|
+
* than when the rider is added. Pass an explicit `of` to pin a rider to the
|
|
5904
|
+
* attacks it already saw. One attack must exist before a rider with a default
|
|
5905
|
+
* `of` is added, or the build fails `unknown-id`.
|
|
5906
|
+
*/
|
|
5907
|
+
attack(source, id) {
|
|
5908
|
+
const entry = id === void 0 ? source : { id, source };
|
|
5909
|
+
return new _Turn([...this.declaredAttacks, entry], this.riders, this.eps);
|
|
5910
|
+
}
|
|
5911
|
+
/**
|
|
5912
|
+
* Appends `count` copies of the same attack — the Extra Attack case, which is
|
|
5913
|
+
* most of 5e. Argument order mirrors `roll(count, die)`.
|
|
5914
|
+
*
|
|
5915
|
+
* ```ts
|
|
5916
|
+
* turn().attacks(4, greatsword).onEveryHit(d6); // fighter 20 + hunter's mark
|
|
5917
|
+
* ```
|
|
5918
|
+
*
|
|
5919
|
+
* @throws {RangeError} if `count` is not a positive integer.
|
|
5920
|
+
*/
|
|
5921
|
+
attacks(count, source) {
|
|
5922
|
+
if (!Number.isInteger(count) || count < 1) {
|
|
5923
|
+
throw new RangeError(
|
|
5924
|
+
`attacks(count) needs a positive integer, got ${count}.`
|
|
5925
|
+
);
|
|
5926
|
+
}
|
|
5927
|
+
const added = new Array(count).fill(source);
|
|
5928
|
+
return new _Turn([...this.declaredAttacks, ...added], this.riders, this.eps);
|
|
5929
|
+
}
|
|
5930
|
+
/**
|
|
5931
|
+
* Appends a rider, throwing {@link TurnSpecError} if that makes the turn
|
|
5932
|
+
* invalid. The `onX` methods below are the readable way to call this.
|
|
5933
|
+
*/
|
|
5934
|
+
rider(rider) {
|
|
5935
|
+
return new _Turn(this.declaredAttacks, [...this.riders, rider], this.eps);
|
|
5936
|
+
}
|
|
5937
|
+
/**
|
|
5938
|
+
* Fires once, on the first source that lands, in that source's mode — so a
|
|
5939
|
+
* crit on the first landing attack doubles the rider's dice. Sneak Attack.
|
|
5940
|
+
*/
|
|
5941
|
+
onFirstHit(damage, options = {}) {
|
|
5942
|
+
return this.rider({ ...options, damage, on: "first-hit" });
|
|
5943
|
+
}
|
|
5944
|
+
/**
|
|
5945
|
+
* Fires once if any source crit, always in crit mode. Divine Smite: nothing is
|
|
5946
|
+
* lost by holding it for a crit, so this is "any", not "first".
|
|
5947
|
+
*/
|
|
5948
|
+
onAnyCrit(damage, options = {}) {
|
|
5949
|
+
return this.rider({ ...options, damage, on: "any-crit" });
|
|
5950
|
+
}
|
|
5951
|
+
/**
|
|
5952
|
+
* Fires once if any source missed. The reroll gate: a reroll is a fresh attack,
|
|
5953
|
+
* so pass one as the damage. Kensei's Unerring Accuracy, Lucky.
|
|
5954
|
+
*/
|
|
5955
|
+
onAnyMiss(damage, options = {}) {
|
|
5956
|
+
return this.rider({ ...options, damage, on: "any-miss" });
|
|
5957
|
+
}
|
|
5958
|
+
/**
|
|
5959
|
+
* Fires once per source that lands, in that hit's mode — so it can fire several
|
|
5960
|
+
* times in a turn. Hunter's Mark, Hex, Rage.
|
|
5961
|
+
*/
|
|
5962
|
+
onEveryHit(damage, options = {}) {
|
|
5963
|
+
return this.rider({ ...options, damage, on: "every-hit" });
|
|
5964
|
+
}
|
|
5965
|
+
/**
|
|
5966
|
+
* Damage for the turns where the rider added just before this one did *not*
|
|
5967
|
+
* fire: "flurry of blows if I didn't smite".
|
|
5968
|
+
*
|
|
5969
|
+
* ```ts
|
|
5970
|
+
* turn([dagger, dagger])
|
|
5971
|
+
* .onAnyCrit(roll(2, d8)) // smite
|
|
5972
|
+
* .otherwise([flurry, flurry]) // ... or two more attacks
|
|
5973
|
+
* ```
|
|
5974
|
+
*
|
|
5975
|
+
* Always binds to the *immediately* preceding rider, so the two are branches of
|
|
5976
|
+
* one decision and can never both land. Note that chaining it therefore
|
|
5977
|
+
* alternates rather than laddering: `a.otherwise(b).otherwise(c)` makes `c`
|
|
5978
|
+
* fire whenever `b` did not, which is exactly when `a` did. For a genuine
|
|
5979
|
+
* three-way priority chain, name the riders and use explicit `not-fired`
|
|
5980
|
+
* triggers against the right one.
|
|
5981
|
+
*/
|
|
5982
|
+
otherwise(damage, options = {}) {
|
|
5983
|
+
const index = this.riders.length - 1;
|
|
5984
|
+
if (index < 0) {
|
|
5985
|
+
throw new TurnSpecError(
|
|
5986
|
+
"unknown-id",
|
|
5987
|
+
"",
|
|
5988
|
+
"otherwise() needs a preceding rider to negate."
|
|
5989
|
+
);
|
|
5990
|
+
}
|
|
5991
|
+
const previous = this.riders[index];
|
|
5992
|
+
if (previous.on === "every-hit") {
|
|
5993
|
+
throw new TurnSpecError(
|
|
5994
|
+
"not-an-attack",
|
|
5995
|
+
previous.id ?? `rider ${index + 1}`,
|
|
5996
|
+
"otherwise() cannot negate an every-hit rider: it can fire more than once."
|
|
5997
|
+
);
|
|
5998
|
+
}
|
|
5999
|
+
const target = previous.id ?? `rider ${index + 1}`;
|
|
6000
|
+
const riders = [...this.riders];
|
|
6001
|
+
riders[index] = { ...previous, id: target };
|
|
6002
|
+
riders.push({ ...options, damage, on: "not-fired", of: target });
|
|
6003
|
+
return new _Turn(this.declaredAttacks, riders, this.eps);
|
|
6004
|
+
}
|
|
6005
|
+
/**
|
|
6006
|
+
* The exact joint distribution: mass 1, outcome-labelled. Resolved once and
|
|
6007
|
+
* cached.
|
|
6008
|
+
*
|
|
6009
|
+
* There is no `toPMF(eps)` to match the builders: a turn's epsilon is fixed
|
|
6010
|
+
* when it is constructed, because the plan is validated and its sources are
|
|
6011
|
+
* resolved at that point.
|
|
6012
|
+
*/
|
|
6013
|
+
get pmf() {
|
|
6014
|
+
return this.resolve().pmf;
|
|
6015
|
+
}
|
|
6016
|
+
/** Mean damage for the turn. */
|
|
6017
|
+
mean() {
|
|
6018
|
+
return this.pmf.mean();
|
|
6019
|
+
}
|
|
6020
|
+
/**
|
|
6021
|
+
* A query whose `singles` are the **declared attacks** and whose combined
|
|
6022
|
+
* distribution is the exact turn PMF.
|
|
6023
|
+
*
|
|
6024
|
+
* Riders are inside the combined PMF, not in `singles`, so singles-based
|
|
6025
|
+
* helpers (`probAtLeastOne`, `countSinglesWith`, `outcomeStats`) describe the
|
|
6026
|
+
* attacks only. Read rider-inclusive statistics off the combined PMF —
|
|
6027
|
+
* `outcomeTotals`, `outcomeDamageRanges`, `damageAttributionChartModel`.
|
|
6028
|
+
*/
|
|
6029
|
+
toQuery() {
|
|
6030
|
+
return new DiceQuery([...this.plan.attackPMFs], this.pmf, this.eps);
|
|
6031
|
+
}
|
|
6032
|
+
/**
|
|
6033
|
+
* Attack ids in declaration order, including the `attack 1`, `attack 2`, …
|
|
6034
|
+
* defaults given to bare sources. These are the names `of` accepts.
|
|
6035
|
+
*/
|
|
6036
|
+
get attackIds() {
|
|
6037
|
+
return this.plan.attackIds;
|
|
6038
|
+
}
|
|
6039
|
+
/**
|
|
6040
|
+
* Rider ids in declaration order, including the `rider 1`, `rider 2`, …
|
|
6041
|
+
* defaults. These are the names {@link Turn.fireProbability} accepts.
|
|
6042
|
+
*/
|
|
6043
|
+
get riderIds() {
|
|
6044
|
+
return this.plan.riderIds;
|
|
6045
|
+
}
|
|
6046
|
+
/**
|
|
6047
|
+
* P(this rider fired). For an `every-hit` rider it is P(at least one source
|
|
6048
|
+
* hit), since that rider can fire more than once in a turn.
|
|
6049
|
+
*
|
|
6050
|
+
* @throws {TurnSpecError} `unknown-id` if `id` is not a rider — attack ids
|
|
6051
|
+
* included, since attacks always happen and have no firing probability.
|
|
6052
|
+
*/
|
|
6053
|
+
fireProbability(id) {
|
|
6054
|
+
const mass = this.resolve().fireMass.get(id);
|
|
6055
|
+
if (mass === void 0) {
|
|
6056
|
+
throw new TurnSpecError(
|
|
6057
|
+
"unknown-id",
|
|
6058
|
+
id,
|
|
6059
|
+
`"${id}" is not a rider in this turn. Riders: ${this.plan.riderIds.map((each) => `"${each}"`).join(", ")}.`
|
|
6060
|
+
);
|
|
6061
|
+
}
|
|
6062
|
+
return mass;
|
|
6063
|
+
}
|
|
6064
|
+
resolve() {
|
|
6065
|
+
if (this.resolved) return this.resolved;
|
|
6066
|
+
const plan = this.plan;
|
|
6067
|
+
const eps = this.eps;
|
|
6068
|
+
const width = plan.groupCount;
|
|
6069
|
+
const start = {
|
|
6070
|
+
codes: new Array(width).fill(START_CODE),
|
|
6071
|
+
pmf: PMF.delta(0, eps),
|
|
6072
|
+
fired: new Array(plan.steps.length).fill(null)
|
|
6073
|
+
};
|
|
6074
|
+
let states = /* @__PURE__ */ new Map([[String.fromCharCode(), start]]);
|
|
6075
|
+
plan.steps.forEach((step, stepIndex) => {
|
|
6076
|
+
const next = /* @__PURE__ */ new Map();
|
|
6077
|
+
const merge = (state) => {
|
|
6078
|
+
const key = String.fromCharCode(...state.codes) + "" + state.fired.map((mode) => mode === null ? "-" : "+").join("");
|
|
6079
|
+
const existing = next.get(key);
|
|
6080
|
+
if (existing) existing.pmf = existing.pmf.add(state.pmf);
|
|
6081
|
+
else next.set(key, state);
|
|
6082
|
+
};
|
|
6083
|
+
for (const state of states.values()) {
|
|
6084
|
+
const mode = fireMode(step, state.codes, state.fired);
|
|
6085
|
+
const fired = [...state.fired];
|
|
6086
|
+
fired[stepIndex] = mode;
|
|
6087
|
+
if (mode === null) {
|
|
6088
|
+
merge({ codes: state.codes, pmf: state.pmf, fired });
|
|
6089
|
+
continue;
|
|
6090
|
+
}
|
|
6091
|
+
if (!step.slices) {
|
|
6092
|
+
const payload = step.damage;
|
|
6093
|
+
merge({
|
|
6094
|
+
codes: state.codes,
|
|
6095
|
+
pmf: state.pmf.convolve(
|
|
6096
|
+
mode === "crit" ? payload.crit : payload.hit,
|
|
6097
|
+
eps,
|
|
6098
|
+
true
|
|
6099
|
+
),
|
|
6100
|
+
fired
|
|
6101
|
+
});
|
|
6102
|
+
continue;
|
|
6103
|
+
}
|
|
6104
|
+
for (const outcome of OUTCOMES) {
|
|
6105
|
+
const slice = step.slices[outcome];
|
|
6106
|
+
const sliceMass = slice.mass();
|
|
6107
|
+
if (sliceMass <= eps) continue;
|
|
6108
|
+
const codes = [...state.codes];
|
|
6109
|
+
for (const group of step.updates) {
|
|
6110
|
+
codes[group] = advance(codes[group], outcome);
|
|
6111
|
+
}
|
|
6112
|
+
merge({
|
|
6113
|
+
codes,
|
|
6114
|
+
pmf: state.pmf.convolve(slice, eps, true),
|
|
6115
|
+
fired
|
|
6116
|
+
});
|
|
6117
|
+
}
|
|
6118
|
+
}
|
|
6119
|
+
states = next;
|
|
6120
|
+
});
|
|
6121
|
+
const fireMass = /* @__PURE__ */ new Map();
|
|
6122
|
+
for (const id of plan.riderSteps.keys()) fireMass.set(id, 0);
|
|
6123
|
+
for (const id of plan.perHitGroups.keys()) fireMass.set(id, 0);
|
|
6124
|
+
let total;
|
|
6125
|
+
for (const state of states.values()) {
|
|
6126
|
+
total = total ? total.add(state.pmf) : state.pmf;
|
|
6127
|
+
const mass = state.pmf.mass();
|
|
6128
|
+
for (const [id, stepIndex] of plan.riderSteps) {
|
|
6129
|
+
if (state.fired[stepIndex] !== null) {
|
|
6130
|
+
fireMass.set(id, fireMass.get(id) + mass);
|
|
6131
|
+
}
|
|
6132
|
+
}
|
|
6133
|
+
for (const [id, group] of plan.perHitGroups) {
|
|
6134
|
+
if (state.codes[group] >> 2 !== FIRST_NONE) {
|
|
6135
|
+
fireMass.set(id, fireMass.get(id) + mass);
|
|
6136
|
+
}
|
|
6137
|
+
}
|
|
6138
|
+
}
|
|
6139
|
+
const pmf = total ?? PMF.delta(0, eps);
|
|
6140
|
+
const totalMass = pmf.mass();
|
|
6141
|
+
const needsNormalizing = Math.abs(totalMass - 1) > eps && totalMass > 0;
|
|
6142
|
+
if (needsNormalizing) {
|
|
6143
|
+
for (const [id, mass] of fireMass) fireMass.set(id, mass / totalMass);
|
|
6144
|
+
}
|
|
6145
|
+
this.resolved = {
|
|
6146
|
+
pmf: needsNormalizing ? pmf.normalize() : pmf,
|
|
6147
|
+
fireMass
|
|
6148
|
+
};
|
|
6149
|
+
return this.resolved;
|
|
6150
|
+
}
|
|
6151
|
+
};
|
|
6152
|
+
function turn(attacks = [], eps = EPS) {
|
|
6153
|
+
return Turn.from(
|
|
6154
|
+
{ attacks: Array.isArray(attacks) ? attacks : [attacks] },
|
|
6155
|
+
eps
|
|
6156
|
+
);
|
|
6157
|
+
}
|
|
6158
|
+
|
|
6159
|
+
export { ACBuilder, AlwaysCritBuilder, AlwaysHitBuilder, AttackBuilder, DCBuilder, HalfRollBuilder, MAX_TRIGGER_GROUPS, MaxOfRollBuilder, ParsedRollBuilder, PooledRollBuilder, RollBuilder, SaveBuilder, ScaleRollBuilder, Turn, TurnSpecError, builderPMFCache, clearAttackCache, clearDCCache, clearRollCache, clearSaveCache, d, d10, d100, d12, d20, d4, d6, d8, defaultConfig, flat, hd20, roll, sumRolls, turn };
|
|
5442
6160
|
//# sourceMappingURL=index.js.map
|
|
5443
6161
|
//# sourceMappingURL=index.js.map
|