@yipe/dice 0.2.9 → 0.2.11
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/builder/index.cjs +49 -5
- package/dist/builder/index.cjs.map +1 -1
- package/dist/builder/index.d.cts +25 -10
- package/dist/builder/index.d.ts +25 -10
- package/dist/builder/index.js +49 -6
- package/dist/builder/index.js.map +1 -1
- package/package.json +1 -1
package/dist/builder/index.cjs
CHANGED
|
@@ -2698,7 +2698,7 @@ var RollBuilder = class _RollBuilder {
|
|
|
2698
2698
|
let rootD20Group;
|
|
2699
2699
|
if (rootConfig && rootConfig.sides === 20) {
|
|
2700
2700
|
const rootIndex = groupedConfigs.findIndex(
|
|
2701
|
-
({ config }) => config
|
|
2701
|
+
({ config }) => rollConfigsEqual(config, rootConfig) && JSON.stringify(config.keep) === JSON.stringify(rootConfig.keep)
|
|
2702
2702
|
);
|
|
2703
2703
|
if (rootIndex !== -1) {
|
|
2704
2704
|
rootD20Group = groupedConfigs.splice(rootIndex, 1)[0];
|
|
@@ -2737,13 +2737,19 @@ var RollBuilder = class _RollBuilder {
|
|
|
2737
2737
|
const expression = diceExpressions[i];
|
|
2738
2738
|
if (i === 0) {
|
|
2739
2739
|
result = (config.isSubtraction ? "-" : "") + expression;
|
|
2740
|
+
if (config.sides === 20 && totalModifier !== 0) {
|
|
2741
|
+
if (totalModifier > 0) result += ` + ${totalModifier}`;
|
|
2742
|
+
else result += ` - ${Math.abs(totalModifier)}`;
|
|
2743
|
+
}
|
|
2740
2744
|
} else {
|
|
2741
2745
|
const operator = config.isSubtraction ? " - " : " + ";
|
|
2742
2746
|
result += operator + expression;
|
|
2743
2747
|
}
|
|
2744
2748
|
}
|
|
2745
|
-
if (
|
|
2746
|
-
|
|
2749
|
+
if (diceConfigs.length === 0 || diceConfigs[0].sides !== 20) {
|
|
2750
|
+
if (totalModifier > 0) result += ` + ${totalModifier}`;
|
|
2751
|
+
else if (totalModifier < 0) result += ` - ${Math.abs(totalModifier)}`;
|
|
2752
|
+
}
|
|
2747
2753
|
return result.replace(/\+ -/g, "-");
|
|
2748
2754
|
}
|
|
2749
2755
|
toPMF(eps = 0) {
|
|
@@ -2869,6 +2875,37 @@ var RollBuilder = class _RollBuilder {
|
|
|
2869
2875
|
const rootConfig = this.getRootDieConfig();
|
|
2870
2876
|
return rootConfig?.reroll || 0;
|
|
2871
2877
|
}
|
|
2878
|
+
half() {
|
|
2879
|
+
return new HalfRollBuilder(this);
|
|
2880
|
+
}
|
|
2881
|
+
};
|
|
2882
|
+
var HalfRollBuilder = class _HalfRollBuilder extends RollBuilder {
|
|
2883
|
+
constructor(innerRoll) {
|
|
2884
|
+
super(0);
|
|
2885
|
+
this.innerRoll = innerRoll;
|
|
2886
|
+
}
|
|
2887
|
+
get lastConfig() {
|
|
2888
|
+
return this.innerRoll.lastConfig;
|
|
2889
|
+
}
|
|
2890
|
+
getSubRollConfigs() {
|
|
2891
|
+
return this.innerRoll.getSubRollConfigs();
|
|
2892
|
+
}
|
|
2893
|
+
toExpression() {
|
|
2894
|
+
const innerExpression = this.innerRoll.toExpression();
|
|
2895
|
+
return `(${innerExpression}) // 2`;
|
|
2896
|
+
}
|
|
2897
|
+
toAST() {
|
|
2898
|
+
return {
|
|
2899
|
+
type: "half",
|
|
2900
|
+
child: this.innerRoll.toAST()
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
toPMF(eps = 0) {
|
|
2904
|
+
return pmfFromRollBuilder(this, eps);
|
|
2905
|
+
}
|
|
2906
|
+
copy() {
|
|
2907
|
+
return new _HalfRollBuilder(this.innerRoll.copy());
|
|
2908
|
+
}
|
|
2872
2909
|
};
|
|
2873
2910
|
|
|
2874
2911
|
// src/builder/factory.ts
|
|
@@ -3036,14 +3073,17 @@ function resolve(node, eps = defaultEps) {
|
|
|
3036
3073
|
const rerollOne = !!childDie && (childDie.reroll || 0) >= 1;
|
|
3037
3074
|
return d20RollPMF(node.rollType, rerollOne);
|
|
3038
3075
|
}
|
|
3076
|
+
case "half": {
|
|
3077
|
+
const childPMF = resolve(node.child, eps);
|
|
3078
|
+
return childPMF.scaleDamage(0.5, "floor");
|
|
3079
|
+
}
|
|
3039
3080
|
}
|
|
3040
3081
|
})();
|
|
3041
3082
|
builderPMFCache.set(cacheKey, result);
|
|
3042
3083
|
return result;
|
|
3043
3084
|
}
|
|
3044
3085
|
function pmfFromRollBuilder(rb, eps = defaultEps) {
|
|
3045
|
-
const
|
|
3046
|
-
const ast = astFromRollConfigs(configs) || { type: "constant", value: 0 };
|
|
3086
|
+
const ast = rb.toAST();
|
|
3047
3087
|
return resolve(ast, eps);
|
|
3048
3088
|
}
|
|
3049
3089
|
function resolveSingleDie(die, eps = defaultEps) {
|
|
@@ -3106,6 +3146,7 @@ function findDie(node) {
|
|
|
3106
3146
|
return void 0;
|
|
3107
3147
|
case "sum":
|
|
3108
3148
|
case "d20Roll":
|
|
3149
|
+
case "half":
|
|
3109
3150
|
return findDie(node.child);
|
|
3110
3151
|
case "keep":
|
|
3111
3152
|
return findDie(node.child.child);
|
|
@@ -3171,6 +3212,8 @@ function getASTSignature(node) {
|
|
|
3171
3212
|
return `keep{c:${node.count},m:${node.mode},ch:${getASTSignature(
|
|
3172
3213
|
node.child
|
|
3173
3214
|
)}}`;
|
|
3215
|
+
case "half":
|
|
3216
|
+
return `half{ch:${getASTSignature(node.child)}}`;
|
|
3174
3217
|
case "add": {
|
|
3175
3218
|
let constantValue = 0;
|
|
3176
3219
|
const otherChildrenSigs = [];
|
|
@@ -3542,6 +3585,7 @@ RollBuilder.prototype.dc = function(saveDC) {
|
|
|
3542
3585
|
|
|
3543
3586
|
exports.ACBuilder = ACBuilder;
|
|
3544
3587
|
exports.DCBuilder = DCBuilder;
|
|
3588
|
+
exports.HalfRollBuilder = HalfRollBuilder;
|
|
3545
3589
|
exports.RollBuilder = RollBuilder;
|
|
3546
3590
|
exports.SaveBuilder = SaveBuilder;
|
|
3547
3591
|
exports.builderPMFCache = builderPMFCache;
|