Package Exports
- rpg-dice-roller
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rpg-dice-roller) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
rpg-dice-roller
A basic JS based dice roller that accepts typical dice notation.
Supported notation
The standard notation formats are accepted, such as 2d6+12, and also the use of L or H to represent the lowest or highest roll respectively.
ie. 4d6-L (A roll of 4 six-sided dice, dropping the lowest result)
Operators
You can also use multiply and divide mathematical operators; 1d6*5 or 2d10/d20.
However, the use of the mathematical symbols × and ÷ do not currently work, but are planned.
Percentile dice (d%)
Although percentile dice can be rolled by using a d100, you can also use d%, which will do the same thing, returning a number between 0 and 100.
Exploding dice
Exploding dice roll an additional die if the maximum, on that die, is rolled. If that die is also the maximum it is rolled again, and so forth, until a roll is made that isn't the maximum.
ie. Rolling a 6 on a d6, or a 10 on a d10.
To explode a dice, add an exclamation mark after the die sides: 4d10!
Each exploded die shows as a separate roll in the list, like so:
2d6!: [4, 6!, 6!, 2] = 20Where the second roll exploded, so we rolled again, which also exploded. The fourth role, however, did not, so we stop rolling.
You can even use L and H, which will look at exploded dice, as well as normal rolls.
i.e.
1d6!-L: [6!,6!,6!,3]-L = 18Compounding
Sometimes, you may want the exploded dice rolls to be added together under the same, original roll. In this situation, you can compound the dice by using two exclamation marks: 4d10!!
For example (using the examples of exploding dice above):
2d6!!: [4, 14!!] = 20 // the exploded dice rolls of [6, 6, 2] are added together
1d6!!-L: [21!!]-L = 18 // the exploded dice rolls of [6, 6, 6, 3] are added togetherPenetrating
Some exploding dice system use a penetrating rule.
Taken from the Hackmaster Basic rules:
Should you roll the maximum value on this particular die, you may re-roll and add the result of the extra die, less one point, to the total (penetration can actually result in simply the maximum die value if a 1 is subsequently rolled, since any fool knows that 1-1=0). This process continues indefinitely as long as the die in question continues to come up maximum (but there’s always only a –1 subtracted from the extra die, even if it’s, say, the third die of penetration)
So, if I rolled 1d6 (penetrating), and got a 6, I would roll another d6, subtracting 1 from the result. If that d6 rolled a 6 (before the -1) it would penetrate, and so on.
The syntax for penetrating is very similar to exploding, but with a lowercase 'p' appended: 2d6!p.
i.e. (Using the same example from exploding dice above):
2d6!p: [4, 6!p, 5, 1] = 20Where the second roll exploded, so we rolled again, which also exploded (rolled a 6). The fourth role, however, rolled a 2, so did not penetrate, so we stop rolling.
Remember that we subtract 1 from penetrated rolls, which is why we show '5' and '1', instead of '6', and '2'.
You can also compound penetrating dice, like so: 2d6!!p
Compare point
By default, Exploding and penetrating dice do so if you roll the highest number possible on the dice (ie. a 6 on a d6, a 1 on a Fudge die).
You can easily change the exploding compare point by adding a comparison after it.
ie. to explode only if you roll a 4:
2d6!=4Or exploding if you roll anything over a 4:
2d6!>4You can also use this with penetrating and compounding dice:
2d6!!<=4 // compound if you roll a 4 or lower
2d6!p!=4 // penetrate if you *don't* roll a 4There is an obvious issue here, wherein you can't do a normal explode if you don't roll a certain number. ie:
2d6!!=4This will actually tell it to compound if you roll a 4. Solutions are currently being looked in to.
Fudge dice
Fudge notation is also supported. It allows both dF.2 and less common dF.1.
You can also use it in conjunction with other operators and additions.
Examples:
dF // this is the same as `dF.2`
4dF.2 // roll 4 standard fudge dice
4dF.2-L // roll 4 standard fudge dice, subtracting the lowest result
dF.1*2 // roll non-standard fudge dice, multiplying the result by 2Dice pools
Some systems use dice pool, whereby the total is equal to the number of dice rolled that meet a fixed condition, rather than the total value of the rolls.
For example, a "pool" of 10 sided dice where you count the number of dice that roll an 8 or higher as "successes".
This can be achieved with:
5d10>=8You can define various success conditions, by simply adding number comparisons directly after the dice roll.
Because of this, you can not have a pool dice that also explodes.
Examples:
2d6=6: [4,6*] = 1 // only a roll of 6 is a success
4d3>1: [1,3*,2*,1] = 2 // higher than a 1 is a success
4d3<2: [1*,3,2,1*] = 2 // lower than a 2 is a success
5d8>=5: [2,4,6*,3,8*] = 2 // higher than or equal to 5 is a success
6d10<=4: [7,2*,10,3*,3*,4*] = 4 // less than or equal to 4 is a successYou can mix pool dice with other dice types or equations, and it will use the number of successes as the value in the equation:
2d6>4+3d5: [4,5*]+[3,1,1] = 6 // 1 success + the raw values of the other rolls
2d6>4*d6!: [6*,5*]*[6!,4] = 20 // 1 success * raw values of the other rolls
2d6>4+2: [3,5*]+2 = 3 // 1 success + 2
2d6>4+H: [3,5*]+H = 2 // Highest roll is 5, which is a success, so value of 1
2d6<4+H: [3*,5]+H = 1 // Highest roll is 5, which is a failure, so value of 0The getSuccesses() method on the DiceRoll object will return the number of successes for a roll.
However, if the roll is just dice pool, and does not contain any other additions, or dice rolls, then the value returned will be the same as calling getTotals().
Usage
You only need to include the dice-roller.js file in your project:
<script src="dice-roller.js"></script>You can use DiceRoller like so:
// create a new instance of the DiceRoller
var roller = new DiceRoller();
// roll the dice
roller.roll('4d20-L');
// get the latest dice rolls from the log
var latestRoll = roller.getLog().shift();
// output the latest roll - it has a toString method for nice output when converted to a string
document.write(latestRoll);
// roll several notations all at once, and store their DiceRoll objects
var rolls = roller.rollMany(['1d6', '2d4-H', '5d10!!']);
// roll a single notation without saving it to the log
var diceRoll = new DiceRoll('2d6-L');
// export the dice roll as JSON
var exportedData = diceRoll.export(DiceRoller.exportFormats.JSON);
// we can also import data either from a previous export, or built up manually
// Note that here we're calling `import` on the `DiceRoll` class, not an existing object
var importedDiceRoll = DiceRoll.import(exportedData);
// importing into a DiceRoller is just as easy
roller.import(exportedData); // appends roll data to the end of existing roll log
var roller2 = DiceRoller.import(exportedData); // creates a new DiceRoller and stores the roll dataUMD
You can also load the library using AMD and CommonJS.
Here is the above example in Node.js:
// require the dice-roller library
const dr = require("./dice-roller.js");
// create a new instance of the DiceRoller
var diceRoller = new dr.DiceRoller();
// roll the dice
diceRoller.roll('4d20-L');
// get the latest dice rolls from the log
var latestRoll = diceRoller.getLog().shift();
// output the latest roll - it has a toString method for nice output
console.log(latestRoll.toString());API
The Dice Roller provides a global DiceRoller class, of which you can have multiple instances:
// my first DiceRoller
var roller1 = new DiceRoller();
// my second DiceRoller
var roller2 = new DiceRoller();Each instance keeps it's own log of dice rolls, so it's handy if you're rolling for several completely unrelated things.
DiceRoller object
| Property | type | description |
|---|---|---|
clearLog |
function() |
Clears the roll history log. |
export |
function({DiceRoller.exportFormats} format) |
Exports the DiceRoller object to the specified format. Returns mixed |
getLog |
function() |
Returns the current roll log. Returns: Array<DiceRoll> |
import |
function({mixed} data) |
Imports the given data and appends it to the current roll log, returning the updated log. Returns Array<DiceRoll> |
getOutput |
function() |
Returns the String representation of the object, in the format of: 2d20+1d6: [20,2]+[2] = 24; 1d8: [6] = 6. Returns String |
roll |
function({String} notation) |
Rolls the given dice notation and returns the rolls. Returns DiceRoll |
rollMany |
function({Array<String>} notations) |
Rolls the given list of dice notations and returns them. Returns Array<DiceRoll> |
toString |
function() |
Returns the String representation of the object, in the format of: 2d20+1d6: [20,2]+[2] = 24; 1d8: [6] = 6. Returns String |
getNotation |
function() |
getOutput method instead. |
Static properties
Static properties can be called on the class itself, without instantiating an object, like so:
var diceRoller = DiceRoller.import(data); // returns a new DiceRoller instance with the given data| Property | type | description |
|---|---|---|
exportFormats |
Object |
List of available export / import formats |
import |
function({mixed} data) |
Imports the given data and creates a new dice roll. Note: This is called on the DiceRoller class, not an instantiated object. Returns DiceRoller |
notationPatterns |
object |
An object that contains a single get(name) method, which returns the regular expression for matching dice notation |
parseDie |
function({String} notation) |
Parses the given notation for a single die and returns a parsed object (Same as parseNotation, but with only 1 result). Returns object |
parseNotation |
function({String} notation) |
Parses the given notation and returns a parsed object (Used internally by the DiceRoll object). Returns Array |
DiceRoll object
A DiceRoll object takes a notation and parses it in to rolls.
It can be created like so:
var notation = '4d10';
var roll = new DiceRoll(notation);| Property | type | description |
|---|---|---|
export |
function({DiceRoller.exportFormats} format) |
Exports the DiceRoll object to the specified format. Returns mixed |
getOutput |
function() |
Returns the String representation of the object, in the format of: 2d20+1d6: [20,2]+[2] = 24. Returns String |
getSuccesses |
function() |
Returns the successes for the roll, if using pool dice. Returns Number |
getTotal |
function() |
Returns the roll total, generated from roll(). Returns Number |
notation |
String |
The dice notation passed |
rolls |
Array |
Roll log for the notation |
roll |
function() |
Rolls the dice for the existing notation and returns the rolls. Returns Array |
toString |
function() |
Returns the String representation of the object, in the format of: 2d20+1d6: [20,2]+[2] = 24. Returns String |
getNotation |
function() |
getOutput method instead. |
Static properties
Static properties can be called on the class itself, without instantiating an object, like so:
var diceRoll = DiceRoll.import(data);| Property | type | description |
|---|---|---|
import |
function({mixed} data) |
Imports the given data and creates a new dice roll. Returns DiceRoll |
Browser support
This dice roller only works on the latest browsers and will not work in versions of IE older than 10.
Demo
View the demo here: http://rpg.greenimp.co.uk/dice-roller
Licence
This dice roller is released under the MIT licence, meaning that you can do pretty much anything you like with it, so long as the original copyright remains in place.
You can use it in commercial products.
If the licence terminology in the licence.txt is confusing, check out this: https://www.tldrlegal.com/l/mit
Reference
Further information can be found here: https://en.wikipedia.org/wiki/Dice_notation