@dice-roller/rpg-dice-roller
Version:
An advanced JS based dice roller that can roll various types of dice and modifiers, along with mathematical equations.
11 lines (9 loc) • 49.2 kB
JavaScript
/**
* @dice-roller/rpg-dice-roller - An advanced JS based dice roller that can roll various types of dice and modifiers, along with mathematical equations.
*
* @version 5.5.1
* @license MIT
* @author GreenImp Media <info@greenimp.co.uk> (https://greenimp.co.uk)
* @link https://dice-roller.github.io/documentation
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("mathjs"),require("random-js")):"function"==typeof define&&define.amd?define(["exports","mathjs","random-js"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).rpgDiceRoller={},t.math,t.Random)}(this,(function(t,e,r){"use strict";class CompareOperatorError extends TypeError{constructor(t){super(`Operator "${t}" is invalid`),TypeError.captureStackTrace&&TypeError.captureStackTrace(this,CompareOperatorError),this.name="CompareOperatorError",this.operator=t}}class DataFormatError extends Error{constructor(t){super(`Invalid data format: ${t}`),Error.captureStackTrace&&Error.captureStackTrace(this,DataFormatError),this.name="ImportError",this.data=t}}class DieActionValueError extends Error{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;super(`Die "${t}" must have more than 1 possible value to ${e||"do this action"}`),Error.captureStackTrace&&Error.captureStackTrace(this,DieActionValueError),this.name="DieActionValueError",this.action=e,this.die=t}}class NotationError extends Error{constructor(t){super(`Notation "${t}" is invalid`),Error.captureStackTrace&&Error.captureStackTrace(this,NotationError),this.name="NotationError",this.notation=t}}class RequiredArgumentError extends Error{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;super("Missing argument"+(t?` "${t}"`:"")),Error.captureStackTrace&&Error.captureStackTrace(this,RequiredArgumentError),this.argumentName=t}}var n=Object.freeze({__proto__:null,CompareOperatorError:CompareOperatorError,DataFormatError:DataFormatError,DieActionValueError:DieActionValueError,NotationError:NotationError,RequiredArgumentError:RequiredArgumentError});const o=t=>e.evaluate(t),i=t=>("number"==typeof t||"string"==typeof t)&&(!Number.isNaN(t)&&Number.isFinite(Number(t))),s=t=>{if(!i(t))return!1;const e=Number(t);return e<=Number.MAX_SAFE_INTEGER&&e>=Number.MIN_SAFE_INTEGER},a=Symbol("engine"),u=Symbol("random"),l={range:[],next(){return this.range[1]-this.range[0]}},c={next:()=>0},h={browserCrypto:r.browserCrypto,nodeCrypto:r.nodeCrypto,MersenneTwister19937:r.MersenneTwister19937,nativeMath:r.nativeMath,min:c,max:l};const f=new class NumberGenerator{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:r.nativeMath;this.engine=t||r.nativeMath}get engine(){return this[a]}set engine(t){if(t&&"function"!=typeof t.next)throw new TypeError("engine must have function `next()`");this[a]=t||r.nativeMath,this[u]=new r.Random(this[a])}integer(t,e){return this[a].range=[t,e],this[u].integer(t,e)}real(t,e){let r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return this[a].range=[t,e],this[u].real(t,e,r)}};var p=Object.freeze({__proto__:null,engines:h,generator:f});const d=Symbol("text"),m=Symbol("type");class Description{static types={MULTILINE:"multiline",INLINE:"inline"};constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.constructor.types.INLINE;this.text=t,this.type=e}get text(){return this[d]}set text(t){if("object"==typeof t)throw new TypeError("Description text is invalid");if(!t&&0!==t||""===`${t}`.trim())throw new TypeError("Description text cannot be empty");this[d]=`${t}`.trim()}get type(){return this[m]}set type(t){const e=Object.values(this.constructor.types);if("string"!=typeof t)throw new TypeError("Description type must be a string");if(!e.includes(t))throw new RangeError(`Description type must be one of; ${e.join(", ")}`);this[m]=t}toJSON(){const{text:t,type:e}=this;return{text:t,type:e}}toString(){return this.type===this.constructor.types.INLINE?`# ${this.text}`:`[${this.text}]`}}const g=Symbol("description");class HasDescription{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;this.description=t}get description(){return this[g]||null}set description(t){if(t||0===t)if(t instanceof Description)this[g]=t;else{if("string"!=typeof t)throw new TypeError("description must be of type Description, string or null. Received "+typeof t);this[g]=new Description(t)}else this[g]=null}toJSON(){const{description:t}=this;return{description:t}}toString(){return this.description?`${this.description}`:""}}class Modifier{static order=999;constructor(){this.order=this.constructor.order}get name(){return"modifier"}get notation(){return""}get maxIterations(){return 1e3}defaults(t){return{}}useDefaultsIfNeeded(t){Object.entries(this.defaults(t)).forEach((t=>{let[e,r]=t;void 0===this[e]&&(this[e]=r)}))}run(t,e){return this.useDefaultsIfNeeded(e),t}toJSON(){const{notation:t,name:e}=this;return{name:e,notation:t,type:"modifier"}}toString(){return this.notation}}const y={compound:"!",explode:"!","critical-failure":"__","critical-success":"**",drop:"d",max:"v",min:"^",penetrate:"p","re-roll":"r","re-roll-once":"ro","target-failure":"_","target-success":"*",unique:"u","unique-once":"uo"},v=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return[...e].reduce(((t,e)=>{let r;return r=e instanceof Modifier?e.name:e,t+(y[r]||r)}),"")},b=Symbol("calculation-value"),w=Symbol("modifiers"),E=Symbol("initial-value"),S=Symbol("use-in-total"),R=Symbol("value");class RollResult{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];if(i(t))this[E]=Number(t),this.modifiers=e||[],this.useInTotal=r;else{if(!t||"object"!=typeof t||Array.isArray(t))throw t===1/0?new RangeError("Result value must be a finite number"):new TypeError(`Result value is invalid: ${t}`);{const n=i(t.initialValue)?t.initialValue:t.value;if(!i(n))throw new TypeError(`Result value is invalid: ${n}`);this[E]=Number(n),i(t.value)&&Number(t.value)!==this[E]&&(this.value=t.value),i(t.calculationValue)&&parseFloat(`${t.calculationValue}`)!==this.value&&(this.calculationValue=t.calculationValue),this.modifiers=t.modifiers||e||[],this.useInTotal="boolean"==typeof t.useInTotal?t.useInTotal:r||!1}}}get calculationValue(){return i(this[b])?parseFloat(this[b]):this.value}set calculationValue(t){const e=i(t);if(t===1/0)throw new RangeError("Result calculation value must be a finite number");if(t&&!e)throw new TypeError(`Result calculation value is invalid: ${t}`);this[b]=e?parseFloat(`${t}`):null}get initialValue(){return this[E]}get modifierFlags(){return v(...this.modifiers)}get modifiers(){return this[w]}set modifiers(t){if((Array.isArray(t)||t instanceof Set)&&[...t].every((t=>"string"==typeof t)))this[w]=new Set([...t]);else{if(t||0===t)throw new TypeError(`modifiers must be a Set or array of modifier names: ${t}`);this[w]=new Set}}get useInTotal(){return!!this[S]}set useInTotal(t){this[S]=!!t}get value(){return i(this[R])?this[R]:this[E]}set value(t){if(t===1/0)throw new RangeError("Result value must be a finite number");if(!i(t))throw new TypeError(`Result value is invalid: ${t}`);this[R]=Number(t)}toJSON(){const{calculationValue:t,initialValue:e,modifierFlags:r,modifiers:n,useInTotal:o,value:i}=this;return{calculationValue:t,initialValue:e,modifierFlags:r,modifiers:[...n],type:"result",useInTotal:o,value:i}}toString(){return this.value+this.modifierFlags}}const x=Symbol("rolls");class RollResults{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];this.rolls=t}get length(){return this.rolls.length||0}get rolls(){return[...this[x]]}set rolls(t){if(!t||!Array.isArray(t))throw new TypeError(`rolls must be an array: ${t}`);this[x]=[],t.forEach((t=>{this.addRoll(t)}))}get value(){return this.rolls.reduce(((t,e)=>t+(e.useInTotal?e.calculationValue:0)),0)}addRoll(t){const e=t instanceof RollResult?t:new RollResult(t);this[x].push(e)}toJSON(){const{rolls:t,value:e}=this;return{rolls:t,type:"roll-results",value:e}}toString(){return`[${this.rolls.join(", ")}]`}}const A=Symbol("modifiers"),C=Symbol("qty"),M=Symbol("sides"),T=Symbol("min-value"),N=Symbol("max-value");class StandardDice extends HasDescription{constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:1,o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:null;if(super(arguments.length>5&&void 0!==arguments[5]?arguments[5]:null),!t&&0!==t)throw new RequiredArgumentError("sides");if(t===1/0)throw new RangeError("numerical sides must be finite number");if(i(t)){if(t<1||!s(t))throw new RangeError("numerical sides must be a positive finite number")}else if("string"!=typeof t)throw new TypeError("non-numerical sides must be a string");if(!i(e))throw new TypeError("qty must be a positive finite integer");if(e<1||e>999)throw new RangeError("qty must be between 1 and 999");let a=n;if(null==a)a=1;else{if(!i(a))throw new TypeError("min must a finite number");if(!s(a))throw new RangeError("min must a finite number")}if(o&&!i(o))throw new TypeError("max must a finite number");if(o&&!s(o))throw new RangeError("max must a finite number");this[C]=parseInt(`${e}`,10),this[M]=t,r&&(this.modifiers=r),this[T]=parseInt(a,10),this[N]=o?parseInt(`${o}`,10):t}get average(){return(this.min+this.max)/2}get modifiers(){return this[A]?new Map([...this[A]].sort(((t,e)=>t[1].order-e[1].order))):null}set modifiers(t){let e;if(t instanceof Map)e=t;else if(Array.isArray(t))e=new Map(t.map((t=>[t.name,t])));else{if("object"!=typeof t)throw new TypeError("modifiers should be a Map, array, or an Object containing Modifiers");e=new Map(Object.entries(t))}if(e.size&&[...e.entries()].some((t=>!(t[1]instanceof Modifier))))throw new TypeError("modifiers must only contain Modifier instances");this[A]=e}get max(){return this[N]}get min(){return this[T]}get name(){return"standard"}get notation(){let t=`${this.qty}d${this.sides}`;return this.modifiers&&this.modifiers.size&&(t+=[...this.modifiers.values()].reduce(((t,e)=>t+e.notation),"")),t}get qty(){return this[C]}get sides(){return this[M]}roll(){const t=new RollResults;for(let e=0;e<this.qty;e++)t.addRoll(this.rollOnce());return(this.modifiers||[]).forEach((e=>{e.run(t,this)})),t}rollOnce(){return new RollResult(f.integer(this.min,this.max))}toJSON(){const{average:t,max:e,min:r,modifiers:n,name:o,notation:i,qty:s,sides:a}=this;return Object.assign(super.toJSON(),{average:t,max:e,min:r,modifiers:n,name:o,notation:i,qty:s,sides:a,type:"die"})}toString(){return`${this.notation}${this.description?` ${this.description}`:""}`}}class FudgeDice extends StandardDice{constructor(){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:2;if(n||0===n){if(1!==n&&2!==n)throw new RangeError("nonBlanks must be 1 or 2")}else n=2;super(n,t,e,-1,1,r)}get name(){return"fudge"}get nonBlanks(){return super.sides}get sides(){return`F.${this.nonBlanks}`}rollOnce(){let t=0;if(2===this.nonBlanks)t=f.integer(1,3)-2;else if(1===this.nonBlanks){const e=f.integer(1,6);1===e?t=-1:6===e&&(t=1)}return new RollResult(t)}}class PercentileDice extends StandardDice{constructor(){let t=arguments.length>2&&void 0!==arguments[2]&&arguments[2];super(100,arguments.length>0&&void 0!==arguments[0]?arguments[0]:1,arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,null,null,arguments.length>3&&void 0!==arguments[3]?arguments[3]:null),this.sidesAsNumber=!!t}get name(){return"percentile"}get sides(){return this.sidesAsNumber?super.sides:"%"}}var O=Object.freeze({__proto__:null,FudgeDice:FudgeDice,PercentileDice:PercentileDice,StandardDice:StandardDice});const $=Symbol("operator"),D=Symbol("value");class ComparePoint{constructor(t,e){if(!t)throw new RequiredArgumentError("operator");if(!e&&0!==e)throw new RequiredArgumentError("value");this.operator=t,this.value=e}static isValidOperator(t){return"string"==typeof t&&/^(?:[<>!]?=|[<>]|<>)$/.test(t)}set operator(t){if(!this.constructor.isValidOperator(t))throw new CompareOperatorError(t);this[$]=t}get operator(){return this[$]}set value(t){if(!i(t))throw new TypeError("value must be a finite number");this[D]=Number(t)}get value(){return this[D]}isMatch(t){return((t,e,r)=>{const n=Number(t),o=Number(e);let i;if(Number.isNaN(n)||Number.isNaN(o))return!1;switch(r){case"=":case"==":i=n===o;break;case"<":i=n<o;break;case">":i=n>o;break;case"<=":i=n<=o;break;case">=":i=n>=o;break;case"!":case"!=":case"<>":i=n!==o;break;default:i=!1}return i})(t,this.value,this.operator)}toJSON(){const{operator:t,value:e}=this;return{operator:t,type:"compare-point",value:e}}toString(){return`${this.operator}${this.value}`}}const P=Symbol("compare-point");class ComparisonModifier extends Modifier{constructor(t){super(),t&&(this.comparePoint=t)}get comparePoint(){return this[P]}set comparePoint(t){if(!(t instanceof ComparePoint))throw new TypeError("comparePoint must be instance of ComparePoint");this[P]=t}get name(){return"comparison"}get notation(){return`${this.comparePoint||""}`}defaultComparePoint(t){return{}}defaults(t){const e=this.defaultComparePoint(t);return"object"==typeof e&&2===e.length?{comparePoint:new ComparePoint(...e)}:{}}isComparePoint(t){return!!this.comparePoint&&this.comparePoint.isMatch(t)}toJSON(){const{comparePoint:t}=this;return Object.assign(super.toJSON(),{comparePoint:t})}}class CriticalFailureModifier extends ComparisonModifier{static order=10;constructor(t){super(t)}get name(){return"critical-failure"}get notation(){return`cf${super.notation}`}defaultComparePoint(t){return["=",t.min]}run(t,e){return super.run(t,e),t.rolls.forEach((t=>(this.isComparePoint(t.value)&&t.modifiers.add("critical-failure"),t))),t}}class CriticalSuccessModifier extends ComparisonModifier{static order=9;constructor(t){super(t)}get name(){return"critical-success"}get notation(){return`cs${super.notation}`}defaultComparePoint(t){return["=",t.max]}run(t,e){return super.run(t,e),t.rolls.forEach((t=>(this.isComparePoint(t.value)&&t.modifiers.add("critical-success"),t))),t}}const j=Symbol("calculation-value"),F=Symbol("is-roll-group"),I=Symbol("modifiers"),J=Symbol("results"),q=Symbol("use-in-total");class ResultGroup{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=!(arguments.length>3&&void 0!==arguments[3])||arguments[3];this.isRollGroup=r,this.modifiers=e,this.results=t,this.useInTotal=n}get calculationValue(){return i(this[j])?parseFloat(this[j]):this.value}set calculationValue(t){const e=i(t);if(t===1/0)throw new RangeError("Results calculation value must be a finite number");if(t&&!e)throw new TypeError(`Results calculation value is invalid: ${t}`);this[j]=e?parseFloat(`${t}`):null}get isRollGroup(){return this[F]}set isRollGroup(t){this[F]=!!t}get length(){return this.results.length||0}get modifierFlags(){return v(...this.modifiers)}get modifiers(){return this[I]}set modifiers(t){if((Array.isArray(t)||t instanceof Set)&&[...t].every((t=>"string"==typeof t)))this[I]=new Set([...t]);else{if(t||0===t)throw new TypeError(`modifiers must be a Set or array of modifier names: ${t}`);this[I]=new Set}}get results(){return[...this[J]]}set results(t){if(!t||!Array.isArray(t))throw new TypeError(`results must be an array: ${t}`);this[J]=[],t.forEach((t=>{this.addResult(t)}))}get useInTotal(){return!!this[q]}set useInTotal(t){this[q]=!!t}get value(){if(!this.results.length)return 0;const t=this.results.reduce(((t,e)=>{let r=e;return e instanceof ResultGroup?r=e.useInTotal?e.calculationValue:0:e instanceof RollResults&&(r=e.value),t+r}),"string"==typeof this.results[0]?"":0);return"string"==typeof t?o(t):t}addResult(t){let e;if(t instanceof ResultGroup||t instanceof RollResults)e=t;else{if("string"!=typeof t&&!i(t))throw new TypeError("value must be one of ResultGroup, RollResults, string, or number");e=t}this[J].push(e)}toJSON(){const{calculationValue:t,isRollGroup:e,modifierFlags:r,modifiers:n,results:o,useInTotal:i,value:s}=this;return{calculationValue:t,isRollGroup:e,modifierFlags:r,modifiers:[...n],results:o,type:"result-group",useInTotal:i,value:s}}toString(){let t;return t=this.isRollGroup?`{${this.results.join(", ")}}`:this.results.join(""),this.modifierFlags&&(t=`(${t})${this.modifierFlags}`),t}}const G=Symbol("end"),V=Symbol("qty");class KeepModifier extends Modifier{static order=6;constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"h",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1;super(),this.end=t,this.qty=e}get end(){return this[G]}set end(t){if("h"!==t&&"l"!==t)throw new RangeError('End must be "h" or "l"');this[G]=t}get name(){return`keep-${this.end}`}get notation(){return`k${this.end}${this.qty}`}get qty(){return this[V]}set qty(t){if(t===1/0)throw new RangeError("qty must be a finite number");if(!i(t)||t<1)throw new TypeError("qty must be a positive finite integer");this[V]=Math.floor(t)}rangeToDrop(t){return"h"===this.end?[0,t.length-this.qty]:[this.qty,t.length]}run(t,e){let r,n;return t instanceof ResultGroup?(r=t.results,n=1===r.length&&r[0]instanceof ResultGroup?r[0].results.map(((t,e)=>t instanceof RollResults?t.rolls.map(((t,r)=>({value:t.value,index:[e,r]}))):null)).flat().filter(Boolean):[...r].map(((t,e)=>({value:t.value,index:e})))):(r=t.rolls,n=[...r].map(((t,e)=>({value:t.value,index:e})))),n=n.sort(((t,e)=>t.value-e.value)).map((t=>t.index)).slice(...this.rangeToDrop(n)),n.forEach((t=>{let e;e=Array.isArray(t)?r[0].results[t[0]].rolls[t[1]]:r[t],e.modifiers.add("drop"),e.useInTotal=!1})),t}toJSON(){const{end:t,qty:e}=this;return Object.assign(super.toJSON(),{end:t,qty:e})}}class DropModifier extends KeepModifier{static order=7;constructor(){super(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"l",arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)}get name(){return`drop-${this.end}`}get notation(){return`d${this.end}${this.qty}`}rangeToDrop(t){return"h"===this.end?[t.length-this.qty,t.length]:[0,this.qty]}}const _=Symbol("compound"),k=Symbol("penetrate");class ExplodeModifier extends ComparisonModifier{static order=3;constructor(){let t=arguments.length>1&&void 0!==arguments[1]&&arguments[1],e=arguments.length>2&&void 0!==arguments[2]&&arguments[2];super(arguments.length>0&&void 0!==arguments[0]?arguments[0]:null),this[_]=!!t,this[k]=!!e}get compound(){return this[_]}get name(){return"explode"}get notation(){return`!${this.compound?"!":""}${this.penetrate?"p":""}${super.notation}`}get penetrate(){return this[k]}defaultComparePoint(t){return["=",t.max]}run(t,e){if(super.run(t,e),e.min===e.max)throw new DieActionValueError(e,"explode");const r=t;return r.rolls=t.rolls.map((t=>{const r=[t];let n=t.value;for(let t=0;t<this.maxIterations&&this.isComparePoint(n);t++){const t=r[r.length-1],o=e.rollOnce();n=o.value,t.modifiers.add("explode"),this.penetrate&&(t.modifiers.add("penetrate"),o.value-=1),r.push(o)}return this.compound&&r.length>1?(t.value=(o=r.map((t=>t.value)),Array.isArray(o)?o.reduce(((t,e)=>t+(i(e)?parseFloat(`${e}`):0)),0):0),t.modifiers=["explode","compound"],this.penetrate&&t.modifiers.add("penetrate"),t):r;var o})).flat(),r}toJSON(){const{compound:t,penetrate:e}=this;return Object.assign(super.toJSON(),{compound:t,penetrate:e})}}const B=Symbol("max");class MaxModifier extends Modifier{static order=2;constructor(t){super(),this.max=t}get max(){return this[B]}set max(t){if(!i(t))throw new TypeError("max must be a number");this[B]=parseFloat(`${t}`)}get name(){return"max"}get notation(){return`max${this.max}`}run(t,e){const r=t;return r.rolls=t.rolls.map((t=>{const e=t;return t.value>this.max&&(e.value=this.max,e.modifiers.add("max")),e})),r}toJSON(){const{max:t}=this;return Object.assign(super.toJSON(),{max:t})}}const L=Symbol("min");class MinModifier extends Modifier{static order=1;constructor(t){super(),this.min=t}get min(){return this[L]}set min(t){if(!i(t))throw new TypeError("min must be a number");this[L]=parseFloat(`${t}`)}get name(){return"min"}get notation(){return`min${this.min}`}run(t,e){const r=t;return r.rolls=t.rolls.map((t=>{const e=t;return t.value<this.min&&(e.value=this.min,e.modifiers.add("min")),e})),r}toJSON(){const{min:t}=this;return Object.assign(super.toJSON(),{min:t})}}const z=Symbol("once");class ReRollModifier extends ComparisonModifier{static order=4;constructor(){let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];super(arguments.length>1&&void 0!==arguments[1]?arguments[1]:null),this.once=!!t}get name(){return"re-roll"}get notation(){return`r${this.once?"o":""}${super.notation}`}get once(){return!!this[z]}set once(t){this[z]=!!t}defaultComparePoint(t){return["=",t.min]}run(t,e){if(super.run(t,e),e.min===e.max)throw new DieActionValueError(e,"re-roll");return t.rolls.map((t=>{for(let r=0;r<this.maxIterations&&this.isComparePoint(t.value);r++){const r=e.rollOnce();if(t.value=r.value,t.modifiers.add("re-roll"+(this.once?"-once":"")),this.once)break}return t})),t}toJSON(){const{once:t}=this;return Object.assign(super.toJSON(),{once:t})}}const U=Symbol("direction");class SortingModifier extends Modifier{static order=11;constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"a";super(),this.direction=t}get direction(){return this[U]}set direction(t){if("a"!==t&&"d"!==t)throw new RangeError('Direction must be "a" (Ascending) or "d" (Descending)');this[U]=t}get name(){return"sorting"}get notation(){return`s${this.direction}`}run(t,e){let r;return r=t instanceof ResultGroup?"results":"rolls",t[r]=t[r].sort(((t,e)=>"d"===this.direction?e.value-t.value:t.value-e.value)),t instanceof ResultGroup&&(t[r]=t[r].map((t=>t instanceof ResultGroup||t instanceof RollResults?this.run(t,e):t))),t}toJSON(){const{direction:t}=this;return Object.assign(super.toJSON(),{direction:t})}}const K=Symbol("failure-cp");class TargetModifier extends ComparisonModifier{static order=8;constructor(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;super(t),this.failureComparePoint=e}get failureComparePoint(){return this[K]}set failureComparePoint(t){if(t&&!(t instanceof ComparePoint))throw new TypeError("failure comparePoint must be instance of ComparePoint or null");this[K]=t||null}get name(){return"target"}get notation(){return`${super.notation}${this.failureComparePoint?`f${this.failureComparePoint}`:""}`}get successComparePoint(){return this.comparePoint}set successComparePoint(t){super.comparePoint=t}getStateValue(t){return this.isSuccess(t)?1:this.isFailure(t)?-1:0}isFailure(t){return!!this.failureComparePoint&&this.failureComparePoint.isMatch(t)}isNeutral(t){return!this.isSuccess(t)&&!this.isFailure(t)}isSuccess(t){return this.isComparePoint(t)}run(t,e){let r;return r=t instanceof ResultGroup?t.results:t.rolls,r.forEach((t=>{this.isSuccess(t.value)?t.modifiers.add("target-success"):this.isFailure(t.value)&&t.modifiers.add("target-failure"),t.calculationValue=this.getStateValue(t.value)})),t}toJSON(){const{failureComparePoint:t,successComparePoint:e}=this,r=super.toJSON();return delete r.comparePoint,Object.assign(r,{failureComparePoint:t,successComparePoint:e})}}const H=Symbol("once"),X=function(t,e,r){let n=arguments.length>3&&void 0!==arguments[3]&&arguments[3];const o=r.map((t=>t.value)).indexOf(t.value);return n?o<e:o!==e};class UniqueModifier extends ComparisonModifier{static order=5;constructor(){let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];super(arguments.length>1&&void 0!==arguments[1]?arguments[1]:null),this.once=!!t}get name(){return"unique"}get notation(){return`u${this.once?"o":""}${super.notation}`}get once(){return!!this[H]}set once(t){this[H]=!!t}run(t,e){if(e.min===e.max)throw new DieActionValueError(e,"re-roll");return t.rolls.forEach(((t,r,n)=>{if(0!==r)for(let o=0;o<this.maxIterations&&(!this.comparePoint||this.isComparePoint(t.value))&&X(t,r,n,!0);o++){const r=e.rollOnce();if(t.value=r.value,t.modifiers.add("unique"+(this.once?"-once":"")),this.once)break}})),t}toJSON(){const{once:t}=this;return Object.assign(super.toJSON(),{once:t})}}var Q=Object.freeze({__proto__:null,ComparisonModifier:ComparisonModifier,CriticalFailureModifier:CriticalFailureModifier,CriticalSuccessModifier:CriticalSuccessModifier,DropModifier:DropModifier,ExplodeModifier:ExplodeModifier,KeepModifier:KeepModifier,MaxModifier:MaxModifier,MinModifier:MinModifier,Modifier:Modifier,ReRollModifier:ReRollModifier,SortingModifier:SortingModifier,TargetModifier:TargetModifier,UniqueModifier:UniqueModifier}),W=Object.freeze({__proto__:null,ResultGroup:ResultGroup,RollResult:RollResult,RollResults:RollResults});const Y=t=>{try{return!(!t||btoa(atob(t))!==t)}catch(t){return!1}},Z=t=>{try{const e=!!t&&JSON.parse(t);return!(!e||"object"!=typeof e)}catch(t){return!1}},tt=Symbol("expressions"),et=Symbol("modifiers");class RollGroup extends HasDescription{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[];super(arguments.length>2&&void 0!==arguments[2]?arguments[2]:null),this.expressions=t,this.modifiers=e}get expressions(){return[...this[tt]||[]]}set expressions(t){if(!t)throw new RequiredArgumentError("expressions");if(!Array.isArray(t))throw new TypeError(`expressions must be an array: ${t}`);this[tt]=[],t.forEach((e=>{if(!e||!Array.isArray(e))throw new TypeError(`Expressions must be an array of arrays: ${t}`);if(0===e.length)throw new TypeError(`Sub expressions cannot be empty: ${t}`);if(!e.every((t=>t instanceof StandardDice||"string"==typeof t||"number"==typeof t)))throw new TypeError("Sub expression items must be Dice, numbers, or strings");this[tt].push(e)}))}get modifiers(){return this[et]?new Map([...this[et]].sort(((t,e)=>t[1].order-e[1].order))):null}set modifiers(t){let e;if(t instanceof Map)e=t;else if(Array.isArray(t))e=new Map(t.map((t=>[t.name,t])));else{if("object"!=typeof t)throw new TypeError("modifiers should be a Map, array, or an Object containing Modifiers");e=new Map(Object.entries(t))}if(e.size&&[...e.entries()].some((t=>!(t[1]instanceof Modifier))))throw new TypeError("modifiers must only contain Modifier instances");this[et]=e}get notation(){let t=this.expressions.map((t=>t.reduce(((t,e)=>t+e),""))).join(", ");return t=`{${t}}`,this.modifiers&&this.modifiers.size&&(t+=[...this.modifiers.values()].reduce(((t,e)=>t+e.notation),"")),t}roll(){const t=new ResultGroup(this.expressions.map((t=>{const e=t.map((t=>t instanceof StandardDice?t.roll():t));return new ResultGroup(e)})));return t.isRollGroup=!0,(this.modifiers||[]).forEach((e=>{e.run(t,this)})),t}toJSON(){const{modifiers:t,notation:e,expressions:r}=this;return Object.assign(super.toJSON(),{expressions:r,modifiers:t,notation:e,type:"group"})}toString(){return`${this.notation}${this.description?` ${this.description}`:""}`}}function rt(t,e,r,n){var o=Error.call(this,t);return Object.setPrototypeOf&&Object.setPrototypeOf(o,rt.prototype),o.expected=e,o.found=r,o.location=n,o.name="SyntaxError",o}function nt(t,e,r){return r=r||" ",t.length>e?t:(e-=t.length,t+(r+=r.repeat(e)).slice(0,e))}function ot(t,e){var r,n,i,s,a={},u=(e=void 0!==e?e:{}).grammarSource,l={Main:Er},c=Er,h="{",f=",",p="}",d="d",m="d%",g="dF",y=".",v="!",b="p",w="k",E="max",S="min",R="r",x="o",A="u",C="cs",M="cf",T="s",N="a",O="f",$="!=",D="<=",P=">=",j="=",F="<>",I=">",J="<",q="(",G=")",V="abs",_="ceil",k="cos",B="exp",L="floor",z="log",U="round",K="sign",H="sin",X="sqrt",Q="tan",W="pow",Y="-",Z="**",tt="*",et="^",nt="%",ot="/",it="+",st="/*",at="*/",ut="[",lt="]",ct="//",ht="#",ft=/^[12]/,pt=/^[lh]/,dt=/^[.]/,mt=/^[1-9]/,gt=/^[0-9]/,yt=/^[^\]]/,vt=/^[\n\r\u2028\u2029]/,bt=/^[ \t\n\r]/,wt=mr("{",!1),Et=mr(",",!1),St=mr("}",!1),Rt=mr("d",!1),xt=mr("d%",!1),At=mr("dF",!1),Ct=mr(".",!1),Mt=gr(["1","2"],!1,!1),Tt=mr("!",!1),Nt=mr("p",!1),Ot=gr(["l","h"],!1,!1),$t=mr("k",!1),Dt=mr("max",!1),Pt=mr("min",!1),jt=mr("r",!1),Ft=mr("o",!1),It=mr("u",!1),Jt=mr("cs",!1),qt=mr("cf",!1),Gt=mr("s",!1),Vt=mr("a",!1),_t=mr("f",!1),kt=mr("!=",!1),Bt=mr("<=",!1),Lt=mr(">=",!1),zt=mr("=",!1),Ut=mr("<>",!1),Kt=mr(">",!1),Ht=mr("<",!1),Xt=mr("(",!1),Qt=mr(")",!1),Wt=mr("abs",!1),Yt=mr("ceil",!1),Zt=mr("cos",!1),te=mr("exp",!1),ee=mr("floor",!1),re=mr("log",!1),ne=mr("round",!1),oe=mr("sign",!1),ie=mr("sin",!1),se=mr("sqrt",!1),ae=mr("tan",!1),ue=mr("pow",!1),le=mr("-",!1),ce=gr(["."],!1,!1),he=gr([["1","9"]],!1,!1),fe=gr([["0","9"]],!1,!1),pe=mr("**",!1),de=mr("*",!1),me=mr("^",!1),ge=mr("%",!1),ye=mr("/",!1),ve=mr("+",!1),be=yr("comment"),we=mr("/*",!1),Ee=mr("*/",!1),Se={type:"any"},Re=mr("[",!1),xe=gr(["]"],!0,!1),Ae=mr("]",!1),Ce=mr("//",!1),Me=mr("#",!1),Te=gr(["\n","\r","\u2028","\u2029"],!1,!1),Ne=yr("whitespace"),Oe=gr([" ","\t","\n","\r"],!1,!1),$e=yr("whitespace or comment"),De=function(t,e,r,n){return new RollGroup([t,...e.map((t=>t[3]))],Object.assign({},...r.map((t=>({[t.name]:t})))),n.find((t=>t instanceof Description)))},Pe=function(t,e,r){return t.modifiers=Object.assign({},...e.map((t=>({[t.name]:t})))),t.description=r.find((t=>t instanceof Description)),t},je=function(t,e){return new StandardDice(e,t||1)},Fe=function(t){return new PercentileDice(t||1)},Ie=function(t,e){return new FudgeDice(e?parseInt(e[1],10):2,t||1)},Je=function(t,e,r){return new ExplodeModifier(r,!!t,!!e)},qe=function(t,e){return new TargetModifier(t,e)},Ge=function(t,e){return new DropModifier(t||"l",e)},Ve=function(t,e){return new KeepModifier(t||"h",e)},_e=function(t){return new MaxModifier(t)},ke=function(t){return new MinModifier(t)},Be=function(t,e){return new ReRollModifier(!!t,e)},Le=function(t,e){return new UniqueModifier(!!t,e)},ze=function(t){return new CriticalSuccessModifier(t)},Ue=function(t){return new CriticalFailureModifier(t)},Ke=function(t){return new SortingModifier(t||"a")},He=function(t){return t},Xe=function(t,e){return new ComparePoint(t,e)},Qe=function(t,e,r){return o(dr())},We=function(t,e){return t=Array.isArray(t)?t:[t],[...t,...e.map((t=>{let[,e,,r]=t;return[e,r]})).flat(2)]},Ye=function(t,e,r){return[t,...e,r]},Ze=function(t,e){return[`${t}(`,...e,")"]},tr=function(t,e,r){return[`${t}(`,...e,",",...r,")"]},er=function(){return parseFloat(dr())},rr=function(){return parseInt(dr(),10)},nr=function(){return parseInt(dr(),10)},or=function(){return"^"},ir=function(t){return new Description(t.flat().join(""),Description.types.MULTILINE)},sr=function(t){return new Description(t.flat().join(""),Description.types.MULTILINE)},ar=function(t){return new Description(t.flat().join(""),Description.types.INLINE)},ur=0,lr=0,cr=[{line:1,column:1}],hr=0,fr=[],pr=0;if("startRule"in e){if(!(e.startRule in l))throw new Error("Can't start parsing from rule \""+e.startRule+'".');c=l[e.startRule]}function dr(){return t.substring(lr,ur)}function mr(t,e){return{type:"literal",text:t,ignoreCase:e}}function gr(t,e,r){return{type:"class",parts:t,inverted:e,ignoreCase:r}}function yr(t){return{type:"other",description:t}}function vr(e){var r,n=cr[e];if(n)return n;for(r=e-1;!cr[r];)r--;for(n={line:(n=cr[r]).line,column:n.column};r<e;)10===t.charCodeAt(r)?(n.line++,n.column=1):n.column++,r++;return cr[e]=n,n}function br(t,e,r){var n=vr(t),o=vr(e),i={source:u,start:{offset:t,line:n.line,column:n.column},end:{offset:e,line:o.line,column:o.column}};return r&&u&&"function"==typeof u.offset&&(i.start=u.offset(i.start),i.end=u.offset(i.end)),i}function wr(t){ur<hr||(ur>hr&&(hr=ur,fr=[]),fr.push(t))}function Er(){return Cr()}function Sr(){var e,r,n,o;if(e=ur,r=function(){var e,r,n,o;e=ur,r=Ar(),r===a&&(r=null);100===t.charCodeAt(ur)?(n=d,ur++):(n=a,0===pr&&wr(Rt));n!==a&&(o=Ar())!==a?(lr=e,e=je(r,o)):(ur=e,e=a);return e}(),r===a&&(r=function(){var e,r,n;e=ur,r=Ar(),r===a&&(r=null);t.substr(ur,2)===m?(n=m,ur+=2):(n=a,0===pr&&wr(xt));n!==a?(lr=e,e=Fe(r)):(ur=e,e=a);return e}(),r===a&&(r=function(){var e,r,n,o,i,s;e=ur,r=Ar(),r===a&&(r=null);t.substr(ur,2)===g?(n=g,ur+=2):(n=a,0===pr&&wr(At));n!==a?(o=ur,46===t.charCodeAt(ur)?(i=y,ur++):(i=a,0===pr&&wr(Ct)),i!==a?(ft.test(t.charAt(ur))?(s=t.charAt(ur),ur++):(s=a,0===pr&&wr(Mt)),s!==a?o=i=[i,s]:(ur=o,o=a)):(ur=o,o=a),o===a&&(o=null),lr=e,e=Ie(r,o)):(ur=e,e=a);return e}())),r!==a){for(n=[],o=Rr();o!==a;)n.push(o),o=Rr();o=Ir(),lr=e,e=Pe(r,n,o)}else ur=e,e=a;return e}function Rr(){var e;return(e=function(){var e,r,n,o,i;e=ur,33===t.charCodeAt(ur)?(r=v,ur++):(r=a,0===pr&&wr(Tt));r!==a?(33===t.charCodeAt(ur)?(n=v,ur++):(n=a,0===pr&&wr(Tt)),n===a&&(n=null),112===t.charCodeAt(ur)?(o=b,ur++):(o=a,0===pr&&wr(Nt)),o===a&&(o=null),(i=xr())===a&&(i=null),lr=e,e=Je(n,o,i)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,(r=xr())!==a?(n=function(){var e,r,n;e=ur,102===t.charCodeAt(ur)?(r=O,ur++):(r=a,0===pr&&wr(_t));r!==a&&(n=xr())!==a?(lr=e,e=He(n)):(ur=e,e=a);return e}(),n===a&&(n=null),lr=e,e=qe(r,n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,o;e=ur,100===t.charCodeAt(ur)?(r=d,ur++):(r=a,0===pr&&wr(Rt));r!==a?(pt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&wr(Ot)),n===a&&(n=null),(o=Nr())!==a?(lr=e,e=Ge(n,o)):(ur=e,e=a)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,o;e=ur,107===t.charCodeAt(ur)?(r=w,ur++):(r=a,0===pr&&wr($t));r!==a?(pt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&wr(Ot)),n===a&&(n=null),(o=Nr())!==a?(lr=e,e=Ve(n,o)):(ur=e,e=a)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,o;e=ur,114===t.charCodeAt(ur)?(r=R,ur++):(r=a,0===pr&&wr(jt));r!==a?(111===t.charCodeAt(ur)?(n=x,ur++):(n=a,0===pr&&wr(Ft)),n===a&&(n=null),(o=xr())===a&&(o=null),lr=e,e=Be(n,o)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,o;e=ur,117===t.charCodeAt(ur)?(r=A,ur++):(r=a,0===pr&&wr(It));r!==a?(111===t.charCodeAt(ur)?(n=x,ur++):(n=a,0===pr&&wr(Ft)),n===a&&(n=null),(o=xr())===a&&(o=null),lr=e,e=Le(n,o)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,2)===C?(r=C,ur+=2):(r=a,0===pr&&wr(Jt));r!==a?((n=xr())===a&&(n=null),lr=e,e=ze(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,2)===M?(r=M,ur+=2):(r=a,0===pr&&wr(qt));r!==a?((n=xr())===a&&(n=null),lr=e,e=Ue(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,115===t.charCodeAt(ur)?(r=T,ur++):(r=a,0===pr&&wr(Gt));r!==a?(97===t.charCodeAt(ur)?(n=N,ur++):(n=a,0===pr&&wr(Vt)),n===a&&(100===t.charCodeAt(ur)?(n=d,ur++):(n=a,0===pr&&wr(Rt))),n===a&&(n=null),lr=e,e=Ke(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,3)===E?(r=E,ur+=3):(r=a,0===pr&&wr(Dt));r!==a&&(n=Tr())!==a?(lr=e,e=_e(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,3)===S?(r=S,ur+=3):(r=a,0===pr&&wr(Pt));r!==a&&(n=Tr())!==a?(lr=e,e=ke(n)):(ur=e,e=a);return e}()),e}function xr(){var e,r,n;return e=ur,r=function(){var e;t.substr(ur,2)===$?(e=$,ur+=2):(e=a,0===pr&&wr(kt));e===a&&(t.substr(ur,2)===D?(e=D,ur+=2):(e=a,0===pr&&wr(Bt)),e===a&&(t.substr(ur,2)===P?(e=P,ur+=2):(e=a,0===pr&&wr(Lt)),e===a&&(61===t.charCodeAt(ur)?(e=j,ur++):(e=a,0===pr&&wr(zt)),e===a&&(t.substr(ur,2)===F?(e=F,ur+=2):(e=a,0===pr&&wr(Ut)),e===a&&(62===t.charCodeAt(ur)?(e=I,ur++):(e=a,0===pr&&wr(Kt)),e===a&&(60===t.charCodeAt(ur)?(e=J,ur++):(e=a,0===pr&&wr(Ht))))))));return e}(),r!==a&&(n=Tr())!==a?(lr=e,e=Xe(r,n)):(ur=e,e=a),e}function Ar(){var e,r,n,o,i,s,u,l,c,h;if((e=Nr())===a)if(e=ur,40===t.charCodeAt(ur)?(r=q,ur++):(r=a,0===pr&&wr(Xt)),r!==a){if(Fr(),n=ur,(o=Tr())!==a){if(i=[],s=ur,u=Fr(),(l=$r())!==a?(c=Fr(),(h=Tr())!==a?s=u=[u,l,c,h]:(ur=s,s=a)):(ur=s,s=a),s!==a)for(;s!==a;)i.push(s),s=ur,u=Fr(),(l=$r())!==a?(c=Fr(),(h=Tr())!==a?s=u=[u,l,c,h]:(ur=s,s=a)):(ur=s,s=a);else i=a;i!==a?n=o=[o,i]:(ur=n,n=a)}else ur=n,n=a;n!==a?(o=Fr(),41===t.charCodeAt(ur)?(i=G,ur++):(i=a,0===pr&&wr(Qt)),i!==a?(lr=e,e=Qe()):(ur=e,e=a)):(ur=e,e=a)}else ur=e,e=a;return e}function Cr(){var t,e,r,n,o,i,s,u;if(t=ur,(e=Mr())!==a){for(r=[],n=ur,o=Fr(),(i=$r())!==a?(s=Fr(),(u=Mr())!==a?n=o=[o,i,s,u]:(ur=n,n=a)):(ur=n,n=a);n!==a;)r.push(n),n=ur,o=Fr(),(i=$r())!==a?(s=Fr(),(u=Mr())!==a?n=o=[o,i,s,u]:(ur=n,n=a)):(ur=n,n=a);lr=t,t=We(e,r)}else ur=t,t=a;return t}function Mr(){var e,r,n,o;return e=function(){var e,r,n,o,i,s,u;e=ur,t.substr(ur,3)===V?(r=V,ur+=3):(r=a,0===pr&&wr(Wt));r===a&&(t.substr(ur,4)===_?(r=_,ur+=4):(r=a,0===pr&&wr(Yt)),r===a&&(t.substr(ur,3)===k?(r=k,ur+=3):(r=a,0===pr&&wr(Zt)),r===a&&(t.substr(ur,3)===B?(r=B,ur+=3):(r=a,0===pr&&wr(te)),r===a&&(t.substr(ur,5)===L?(r=L,ur+=5):(r=a,0===pr&&wr(ee)),r===a&&(t.substr(ur,3)===z?(r=z,ur+=3):(r=a,0===pr&&wr(re)),r===a&&(t.substr(ur,5)===U?(r=U,ur+=5):(r=a,0===pr&&wr(ne)),r===a&&(t.substr(ur,4)===K?(r=K,ur+=4):(r=a,0===pr&&wr(oe)),r===a&&(t.substr(ur,3)===H?(r=H,ur+=3):(r=a,0===pr&&wr(ie)),r===a&&(t.substr(ur,4)===X?(r=X,ur+=4):(r=a,0===pr&&wr(se)),r===a&&(t.substr(ur,3)===Q?(r=Q,ur+=3):(r=a,0===pr&&wr(ae))))))))))));r!==a?(40===t.charCodeAt(ur)?(n=q,ur++):(n=a,0===pr&&wr(Xt)),n!==a?(Fr(),(o=Cr())!==a?(Fr(),41===t.charCodeAt(ur)?(i=G,ur++):(i=a,0===pr&&wr(Qt)),i!==a?(lr=e,e=Ze(r,o)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a);e===a&&(e=ur,t.substr(ur,3)===W?(r=W,ur+=3):(r=a,0===pr&&wr(ue)),r===a&&(t.substr(ur,3)===E?(r=E,ur+=3):(r=a,0===pr&&wr(Dt)),r===a&&(t.substr(ur,3)===S?(r=S,ur+=3):(r=a,0===pr&&wr(Pt)))),r!==a?(40===t.charCodeAt(ur)?(n=q,ur++):(n=a,0===pr&&wr(Xt)),n!==a?(Fr(),(o=Cr())!==a?(Fr(),44===t.charCodeAt(ur)?(i=f,ur++):(i=a,0===pr&&wr(Et)),i!==a?(Fr(),(s=Cr())!==a?(Fr(),41===t.charCodeAt(ur)?(u=G,ur++):(u=a,0===pr&&wr(Qt)),u!==a?(lr=e,e=tr(r,o,s)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a));return e}(),e===a&&(e=Sr())===a&&(e=Tr())===a&&(e=ur,40===t.charCodeAt(ur)?(r=q,ur++):(r=a,0===pr&&wr(Xt)),r!==a?(Fr(),(n=Cr())!==a?(Fr(),41===t.charCodeAt(ur)?(o=G,ur++):(o=a,0===pr&&wr(Qt)),o!==a?(lr=e,e=Ye(r,n,o)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a),e===a&&(e=function(){var e,r,n,o,i,s,u,l,c;if(e=ur,123===t.charCodeAt(ur)?(r=h,ur++):(r=a,0===pr&&wr(wt)),r!==a)if(Fr(),(n=Cr())!==a){for(o=[],i=ur,s=Fr(),44===t.charCodeAt(ur)?(u=f,ur++):(u=a,0===pr&&wr(Et)),u!==a?(l=Fr(),(c=Cr())!==a?i=s=[s,u,l,c]:(ur=i,i=a)):(ur=i,i=a);i!==a;)o.push(i),i=ur,s=Fr(),44===t.charCodeAt(ur)?(u=f,ur++):(u=a,0===pr&&wr(Et)),u!==a?(l=Fr(),(c=Cr())!==a?i=s=[s,u,l,c]:(ur=i,i=a)):(ur=i,i=a);if(i=Fr(),125===t.charCodeAt(ur)?(s=p,ur++):(s=a,0===pr&&wr(St)),s!==a){for(u=[],l=Rr();l!==a;)u.push(l),l=Rr();l=Ir(),lr=e,e=De(n,o,u,l)}else ur=e,e=a}else ur=e,e=a;else ur=e,e=a;return e}())),e}function Tr(){var e,r,n,o;return e=ur,45===t.charCodeAt(ur)?ur++:0===pr&&wr(le),Or()!==a?(r=ur,dt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&wr(ce)),n!==a&&(o=Or())!==a?r=n=[n,o]:(ur=r,r=a),r===a&&(r=null),lr=e,e=er()):(ur=e,e=a),e}function Nr(){var e,r,n,o;if(e=ur,mt.test(t.charAt(ur))?(r=t.charAt(ur),ur++):(r=a,0===pr&&wr(he)),r!==a){for(n=[],gt.test(t.charAt(ur))?(o=t.charAt(ur),ur++):(o=a,0===pr&&wr(fe));o!==a;)n.push(o),gt.test(t.charAt(ur))?(o=t.charAt(ur),ur++):(o=a,0===pr&&wr(fe));lr=e,e=rr()}else ur=e,e=a;return e}function Or(){var e,r,n;if(e=ur,r=[],gt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&wr(fe)),n!==a)for(;n!==a;)r.push(n),gt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&wr(fe));else r=a;return r!==a&&(lr=e,r=nr()),e=r}function $r(){var e,r;return e=ur,t.substr(ur,2)===Z?(r=Z,ur+=2):(r=a,0===pr&&wr(pe)),r!==a&&(lr=e,r=or()),(e=r)===a&&(42===t.charCodeAt(ur)?(e=tt,ur++):(e=a,0===pr&&wr(de)),e===a&&(94===t.charCodeAt(ur)?(e=et,ur++):(e=a,0===pr&&wr(me)),e===a&&(37===t.charCodeAt(ur)?(e=nt,ur++):(e=a,0===pr&&wr(ge)),e===a&&(47===t.charCodeAt(ur)?(e=ot,ur++):(e=a,0===pr&&wr(ye)),e===a&&(43===t.charCodeAt(ur)?(e=it,ur++):(e=a,0===pr&&wr(ve)),e===a&&(45===t.charCodeAt(ur)?(e=Y,ur++):(e=a,0===pr&&wr(le)))))))),e}function Dr(){var e;return pr++,(e=function(){var e,r,n,o,i,s;e=ur,t.substr(ur,2)===st?(r=st,ur+=2):(r=a,0===pr&&wr(we));if(r!==a){for(n=[],o=ur,i=ur,pr++,t.substr(ur,2)===at?(s=at,ur+=2):(s=a,0===pr&&wr(Ee)),pr--,s===a?i=void 0:(ur=i,i=a),i!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&wr(Se)),s!==a?o=i=[i,s]:(ur=o,o=a)):(ur=o,o=a);o!==a;)n.push(o),o=ur,i=ur,pr++,t.substr(ur,2)===at?(s=at,ur+=2):(s=a,0===pr&&wr(Ee)),pr--,s===a?i=void 0:(ur=i,i=a),i!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&wr(Se)),s!==a?o=i=[i,s]:(ur=o,o=a)):(ur=o,o=a);t.substr(ur,2)===at?(o=at,ur+=2):(o=a,0===pr&&wr(Ee)),o!==a?(lr=e,e=ir(n)):(ur=e,e=a)}else ur=e,e=a;if(e===a)if(e=ur,91===t.charCodeAt(ur)?(r=ut,ur++):(r=a,0===pr&&wr(Re)),r!==a){for(n=[],yt.test(t.charAt(ur))?(o=t.charAt(ur),ur++):(o=a,0===pr&&wr(xe));o!==a;)n.push(o),yt.test(t.charAt(ur))?(o=t.charAt(ur),ur++):(o=a,0===pr&&wr(xe));93===t.charCodeAt(ur)?(o=lt,ur++):(o=a,0===pr&&wr(Ae)),o!==a?(lr=e,e=sr(n)):(ur=e,e=a)}else ur=e,e=a;return e}())===a&&(e=function(){var e,r,n,o,i,s;e=ur,t.substr(ur,2)===ct?(r=ct,ur+=2):(r=a,0===pr&&wr(Ce));r===a&&(35===t.charCodeAt(ur)?(r=ht,ur++):(r=a,0===pr&&wr(Me)));if(r!==a){for(n=[],o=ur,i=ur,pr++,s=Pr(),pr--,s===a?i=void 0:(ur=i,i=a),i!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&wr(Se)),s!==a?o=i=[i,s]:(ur=o,o=a)):(ur=o,o=a);o!==a;)n.push(o),o=ur,i=ur,pr++,s=Pr(),pr--,s===a?i=void 0:(ur=i,i=a),i!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&wr(Se)),s!==a?o=i=[i,s]:(ur=o,o=a)):(ur=o,o=a);lr=e,e=ar(n)}else ur=e,e=a;return e}()),pr--,e===a&&0===pr&&wr(be),e}function Pr(){var e;return vt.test(t.charAt(ur))?(e=t.charAt(ur),ur++):(e=a,0===pr&&wr(Te)),e}function jr(){var e;return pr++,bt.test(t.charAt(ur))?(e=t.charAt(ur),ur++):(e=a,0===pr&&wr(Oe)),pr--,e===a&&0===pr&&wr(Ne),e}function Fr(){var t,e;for(t=[],e=jr();e!==a;)t.push(e),e=jr();return t}function Ir(){var t,e;for(pr++,t=[],(e=jr())===a&&(e=Dr());e!==a;)t.push(e),(e=jr())===a&&(e=Dr());return pr--,e=a,0===pr&&wr($e),t}if((r=c())!==a&&ur===t.length)return r;throw r!==a&&ur<t.length&&wr({type:"end"}),n=fr,i=hr<t.length?t.charAt(hr):null,s=hr<t.length?br(hr,hr+1):br(hr,hr),new rt(rt.buildMessage(n,i),n,i,s)}!function(t,e){function r(){this.constructor=t}r.prototype=e.prototype,t.prototype=new r}(rt,Error),rt.prototype.format=function(t){var e="Error: "+this.message;if(this.location){var r,n=null;for(r=0;r<t.length;r++)if(t[r].source===this.location.source){n=t[r].text.split(/\r\n|\n|\r/g);break}var o=this.location.start,i=this.location.source&&"function"==typeof this.location.source.offset?this.location.source.offset(o):o,s=this.location.source+":"+i.line+":"+i.column;if(n){var a=this.location.end,u=nt("",i.line.toString().length," "),l=n[o.line-1],c=(o.line===a.line?a.column:l.length+1)-o.column||1;e+="\n --\x3e "+s+"\n"+u+" |\n"+i.line+" | "+l+"\n"+u+" | "+nt("",o.column-1," ")+nt("",c,"^")}else e+="\n at "+s}return e},rt.buildMessage=function(t,e){var r={literal:function(t){return'"'+o(t.text)+'"'},class:function(t){var e=t.parts.map((function(t){return Array.isArray(t)?i(t[0])+"-"+i(t[1]):i(t)}));return"["+(t.inverted?"^":"")+e.join("")+"]"},any:function(){return"any character"},end:function(){return"end of input"},other:function(t){return t.description}};function n(t){return t.charCodeAt(0).toString(16).toUpperCase()}function o(t){return t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,(function(t){return"\\x0"+n(t)})).replace(/[\x10-\x1F\x7F-\x9F]/g,(function(t){return"\\x"+n(t)}))}function i(t){return t.replace(/\\/g,"\\\\").replace(/\]/g,"\\]").replace(/\^/g,"\\^").replace(/-/g,"\\-").replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,(function(t){return"\\x0"+n(t)})).replace(/[\x10-\x1F\x7F-\x9F]/g,(function(t){return"\\x"+n(t)}))}function s(t){return r[t.type](t)}return"Expected "+function(t){var e,r,n=t.map(s);if(n.sort(),n.length>0){for(e=1,r=1;e<n.length;e++)n[e-1]!==n[e]&&(n[r]=n[e],r++);n.length=r}switch(n.length){case 1:return n[0];case 2:return n[0]+" or "+n[1];default:return n.slice(0,-1).join(", ")+", or "+n[n.length-1]}}(t)+" but "+function(t){return t?'"'+o(t)+'"':"end of input"}(e)+" found."};class Parser{static parse(t){if(!t)throw new RequiredArgumentError("notation");if("string"!=typeof t)throw new TypeError("Notation must be a string");return ot(t)}}const it=Object.freeze({BASE_64:1,JSON:0,OBJECT:2}),st=Symbol("notation"),at=Symbol("maxTotal"),ut=Symbol("minTotal"),lt=Symbol("expressions"),ct=Symbol("roll-method"),ht=Symbol("rolls"),ft=Symbol("set-rolls"),pt=Symbol("total"),dt=t=>function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return parseFloat(parseFloat(`${t}`).toFixed(e||0))}(t.calculationValue,2);class DiceRoll{constructor(t){if(!t)throw new RequiredArgumentError("notation");if(this[lt]=[],t instanceof Object&&!Array.isArray(t)){if(!t.notation)throw new RequiredArgumentError("notation");if("string"!=typeof t.notation)throw new NotationError(t.notation);t.rolls&&this[ft](t.rolls),this[st]=t.notation,this[lt]=Parser.parse(this.notation),this.hasRolls()||this.roll()}else{if("string"!=typeof t)throw new NotationError(t);this[st]=t,this[lt]=Parser.parse(this.notation),this.roll()}}get averageTotal(){return(this.maxTotal+this.minTotal)/2}get maxTotal(){if(!this.hasExpressions())return 0;if(!this[at]){const t=this[ct](h.max);this[at]=dt(t)}return this[at]}get minTotal(){if(!this.hasExpressions())return 0;if(!this[ut]){const t=this[ct](h.min);this[ut]=dt(t)}return this[ut]}get notation(){return this[st]}get output(){let t=`${this.notation}: `;return this.hasRolls()?t+=`${this[ht]} = ${this.total}`:t+="No dice rolled",t}get rolls(){return this[ht]?this[ht].results:[]}get total(){return!this[pt]&&this.hasRolls()&&(this[pt]=dt(this[ht])),this[pt]||0}export(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:it.JSON;switch(t){case it.BASE_64:return btoa(this.export(it.JSON));case it.JSON:return JSON.stringify(this);case it.OBJECT:return JSON.parse(this.export(it.JSON));default:throw new TypeError(`Invalid export format "${t}"`)}}hasExpressions(){return this[lt]&&this[lt].length>0}hasRolls(){return this.hasExpressions()&&this.rolls.length>0}roll(){return this[pt]=0,this[ht]=this[ct](),this.rolls}toJSON(){const{averageTotal:t,maxTotal:e,minTotal:r,notation:n,output:o,rolls:i,total:s}=this;return{averageTotal:t,maxTotal:e,minTotal:r,notation:n,output:o,rolls:i,total:s,type:"dice-roll"}}toString(){return this.output}static import(t){if(t){if(Z(t))return DiceRoll.import(JSON.parse(t));if(Y(t))return DiceRoll.import(atob(t));if("object"==typeof t)return new DiceRoll(t);throw new DataFormatError(t)}throw new RequiredArgumentError("data")}[ct](t){let e;t&&(e=f.engine,f.engine=t);const r=new ResultGroup(this[lt].map((t=>t instanceof StandardDice||t instanceof RollGroup?t.roll():t)).filter((t=>!!t||0===t)));return t&&(f.engine=e),r}[ft](t){if(t instanceof ResultGroup)this[ht]=t;else if(t instanceof RollResults)this[ht]=new ResultGroup([t]);else{if(!Array.isArray(t))throw new TypeError("Rolls must be a valid result object, or an array");this[ht]=new ResultGroup(t.map((t=>{if(t instanceof ResultGroup||t instanceof RollResults)return t;if(Array.isArray(t))return new RollResults(t);if("object"==typeof t){if(Array.isArray(t.results))return new ResultGroup(t.results,t.modifiers||[],t.isRollGroup||!1,"boolean"!=typeof t.useInTotal||t.useInTotal);if(Array.isArray(t.rolls))return new RollResults(t.rolls)}return t})))}}}const mt=Symbol("log");class DiceRoller{constructor(t){this[mt]=[],t&&this.import(t)}get log(){return this[mt]||[]}get output(){return this.log.join("; ")}get total(){return this.log.reduce(((t,e)=>t+e.total),0)}clearLog(){this[mt].length=0}export(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:it.JSON;switch(t){case it.BASE_64:return btoa(this.export(it.JSON));case it.JSON:return JSON.stringify(this);case it.OBJECT:return JSON.parse(this.export(it.JSON));default:throw new TypeError(`Invalid export format "${t}"`)}}import(t){if(t){if(Z(t))return this.import(JSON.parse(t));if(Y(t))return this.import(atob(t));if("object"==typeof t){let e=t.log||null;if(!t.log&&Array.isArray(t)&&t.length&&(e=t),e&&Array.isArray(e))e.forEach((t=>{this[mt].push(DiceRoll.import(t))}));else if(e)throw new TypeError("log must be an array");return this.log}throw new DataFormatError(t)}throw new RequiredArgumentError("data")}roll(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];const n=e.filter(Boolean);if(0===n.length)throw new RequiredArgumentError("notations");const o=n.map((t=>{const e=new DiceRoll(t);return this[mt].push(e),e}));return o.length>1?o:o[0]}toJSON(){const{log:t,output:e,total:r}=this;return{log:t,output:e,total:r,type:"dice-roller"}}toString(){return this.output}static import(t){const e=new DiceRoller;return e.import(t),e}}t.ComparePoint=ComparePoint,t.Dice=O,t.DiceRoll=DiceRoll,t.DiceRoller=DiceRoller,t.Exceptions=n,t.Modifiers=Q,t.NumberGenerator=p,t.Parser=Parser,t.Results=W,t.RollGroup=RollGroup,t.exportFormats=it,Object.defineProperty(t,"__esModule",{value:!0})}));