@yipe/dice 0.8.1 → 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/index.cjs +668 -6
- 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 +665 -7
- package/dist/builder/index.js.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)
|
|
@@ -5137,11 +5190,11 @@ var AttackBuilder = class _AttackBuilder {
|
|
|
5137
5190
|
pMiss: pmiss
|
|
5138
5191
|
} = this.resolveProbabilities(this.check, eps);
|
|
5139
5192
|
const hitPMF = this.hitEffect ? this.hitEffect instanceof ParsedRollBuilder ? this.hitEffect.toPMF(eps) : pmfFromRollBuilder(this.hitEffect, eps) : PMF.delta(0, eps);
|
|
5140
|
-
let
|
|
5193
|
+
let critPMF2 = null;
|
|
5141
5194
|
let phit = pHit;
|
|
5142
5195
|
let pcrit = pCrit;
|
|
5143
5196
|
if (this.critEffect === null) {
|
|
5144
|
-
|
|
5197
|
+
critPMF2 = null;
|
|
5145
5198
|
phit += pcrit;
|
|
5146
5199
|
pcrit = 0;
|
|
5147
5200
|
} else {
|
|
@@ -5149,7 +5202,7 @@ var AttackBuilder = class _AttackBuilder {
|
|
|
5149
5202
|
if (this.critEffect) {
|
|
5150
5203
|
critBuilder = this.critEffect;
|
|
5151
5204
|
} else if (this.hitEffect instanceof ParsedRollBuilder) {
|
|
5152
|
-
|
|
5205
|
+
critPMF2 = null;
|
|
5153
5206
|
phit += pcrit;
|
|
5154
5207
|
pcrit = 0;
|
|
5155
5208
|
critBuilder = void 0;
|
|
@@ -5157,20 +5210,20 @@ var AttackBuilder = class _AttackBuilder {
|
|
|
5157
5210
|
critBuilder = this.hitEffect?.copy().doubleDice();
|
|
5158
5211
|
}
|
|
5159
5212
|
if (critBuilder) {
|
|
5160
|
-
|
|
5213
|
+
critPMF2 = critBuilder instanceof ParsedRollBuilder ? critBuilder.toPMF(eps) : pmfFromRollBuilder(critBuilder, eps);
|
|
5161
5214
|
}
|
|
5162
5215
|
}
|
|
5163
5216
|
const missPMF = this.missEffect ? this.missEffect instanceof ParsedRollBuilder ? this.missEffect.toPMF(eps) : pmfFromRollBuilder(this.missEffect, eps) : PMF.delta(0, eps);
|
|
5164
5217
|
const mix = new Mixture(eps);
|
|
5165
5218
|
if (phit > 0) mix.add("hit", hitPMF, phit);
|
|
5166
|
-
if (
|
|
5219
|
+
if (critPMF2 && pcrit > 0) mix.add("crit", critPMF2, pcrit);
|
|
5167
5220
|
if (pmiss > 0)
|
|
5168
5221
|
mix.add(this.missEffect ? "missDamage" : "missNone", missPMF, pmiss);
|
|
5169
5222
|
return {
|
|
5170
5223
|
pmf: mix.buildPMF(eps) ?? PMF.delta(0, eps),
|
|
5171
5224
|
check: this.check.toPMF(eps) ?? PMF.delta(0, eps),
|
|
5172
5225
|
hit: hitPMF ?? PMF.delta(0, eps),
|
|
5173
|
-
crit:
|
|
5226
|
+
crit: critPMF2 ?? PMF.delta(0, eps),
|
|
5174
5227
|
miss: missPMF ?? PMF.delta(0, eps),
|
|
5175
5228
|
weights: { hit: phit, crit: pcrit, miss: pmiss }
|
|
5176
5229
|
};
|
|
@@ -5498,6 +5551,611 @@ RollBuilder.prototype.dc = function(saveDC) {
|
|
|
5498
5551
|
return new DCBuilder(this).dc(saveDC);
|
|
5499
5552
|
};
|
|
5500
5553
|
|
|
5501
|
-
|
|
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 };
|
|
5502
6160
|
//# sourceMappingURL=index.js.map
|
|
5503
6161
|
//# sourceMappingURL=index.js.map
|