Package Exports
- @yipe/dice
- @yipe/dice/builder
- @yipe/dice/package.json
Readme
๐ฒ @yipe/dice
A TypeScript library for D&D 5e damage-per-round (DPR) calculations, designed for players, Dungeon Masters, and developers who want to analyze combat mathematically.
This library powers dprcalc.com and provides a precise, composable way to model dice rolls, attacks, and outcomes with probability mass functions (PMFs) โ not just averages. This allows for rich charting and statistics with full outcome attribution.
import { parse } from "@yipe/dice";
const attack = parse("(d20 + 8 AC 16) * (1d8 + 4) crit (2d8 + 4)");
console.log("DPR:", attack.mean());โจ Features
- D&D 5e Focused: Designed around 5e rules (2014 and 2024).
- Probability Mass Functions (PMF): Precise modeling of dice rolls and outcomes, not just averages.
- Complex Attack Expressions: Supports crit ranges, advantage/disadvantage, conditional damage, rerolls, minimum damage, and more.
- Composable API: Build dice expressions, run queries, and analyze results in just a few lines.
- TypeScript First: Full type safety and developer experience.
๐ Installation
# Install with npm or yarn
npm install @yipe/dice
# or
yarn add @yipe/dice๐ฆ Core Concepts
| Concept | Description |
|---|---|
| PMF | Probability Mass Function. The core mathematical representation of outcomes. |
| Query | Runs calculations and scenarios over one or more PMFs. |
| Parser | Parses text-based dice expressions like (d20 + 8 AC 16) * (1d4 + 4). |
| Dice | Represents the outcome of a single dice expression |
๐ง Basic Usage
Here's a simple example of calculating damage for a basic attack:
import { parse, DiceQuery } from "@yipe/dice";
const query = parse("(d20 + 8 AC 16) * (1d4 + 4) crit (2d4 + 4)").toQuery();
console.log("Hit chance:", query.probAtLeastOne(["hit", "crit"]));
console.log("Crit chance:", query.probAtLeastOne(["crit"]));
console.log("DPR:", query.mean());Output:
Hit chance: 0.65
Crit chance: 0.05
DPR: 4.35โ๏ธ Sneak Attack Example
Conditional damage ("once-per-turn damage riders") like Sneak Attack can be modeled easily:
import { parse, DiceQuery } from "@yipe/dice";
function damageRiderExample() {
const attack = parse("(d20 + 8 AC 16) * (1d4 + 4) crit (2d4 + 4)");
const sneakAttack = parse("3d6");
const sneakAttackCrit = parse("6d6");
// Determine the probability of a success (hit or crit) and a crit
const attacks = new DiceQuery([attack, attack]);
const [pSuccess, pCrit] = attacks.firstSuccessSplit(
["hit", "crit"],
["crit"]
);
// Calculate the expected damage of sneak attack in both scenarios
const sneak = PMF.exclusive([
[sneakAttack, pSuccess],
[sneakAttackCrit, pCrit],
]);
// Create a full round with all of those damage outcomes
const fullRound = new DiceQuery([attack, attack, sneak]);
const DPR = fullRound.mean();
console.log("full round DPR", DPR);
// Congrats, you can now chart and query this round!
return fullRound;
}๐ Statistics and Charts
You can generate full statistical distributions for visualization or reporting.
import { parse, DiceQuery } from "@yipe/dice";
const query2 = parse("(d20 + 8 AC 16) * (1d4 + 4) crit (2d4 + 4)").toQuery();
console.table(query2.toChartSeries());Output:
โโโโโโโโโโโฌโโโโโฌโโโโโโโโโโโ
โ (index) โ x โ y โ
โโโโโโโโโโโผโโโโโผโโโโโโโโโโโค
โ 0 โ 0 โ 0.35 โ
โ 1 โ 5 โ 0.15 โ
โ 2 โ 6 โ 0.153125 โ
โ 3 โ 7 โ 0.15625 โ
โ 4 โ 8 โ 0.159375 โ
โ 5 โ 9 โ 0.0125 โ
โ 6 โ 10 โ 0.009375 โ
โ 7 โ 11 โ 0.00625 โ
โ 8 โ 12 โ 0.003125 โ
โโโโโโโโโโโดโโโโโดโโโโโโโโโโโ๐ Project Structure
| File | Purpose |
|---|---|
src/ |
Core Dice class and logic |
examples/ |
Example scripts showing library usage |
tests/ |
Comprehensive library tests |
๐งช Running Examples
This repository includes example scripts.
yarn example basic
yarn example stats
yarn example sneakattack
yarn example miscHere is the basic example:
% yarn example basic
โโโโโโโโโโโโโโ
โ Summary โ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Expression: (d20 + 5 AC 15) * (1d6+2) crit (2d6 + 2) โ
โ Success Chance: 0.55 โ
โ Expected DPR: 3.20 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโ
โ PMF () โ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 0: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 45.00% โ
โ 3: โโโโโโโโโโโโโโ 8.33% โ
โ 4: โโโโโโโโโโโโโโ 8.47% โ
โ 5: โโโโโโโโโโโโโโ 8.61% โ
โ 6: โโโโโโโโโโโโโโโ 8.75% โ
โ 7: โโโโโโโโโโโโโโโ 8.89% โ
โ 8: โโโโโโโโโโโโโโโ 9.03% โ
โ 9: โโ 0.83% โ
โ 10: โโ 0.69% โ
โ 11: โ 0.56% โ
โ 12: โ 0.42% โ
โ 13: โ 0.28% โ
โ 14: โ 0.14% โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโ
โ CDF (): P(X โค x) โ
โโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 0: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 45.00% โ
โ 1: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 45.00% โ
โ 2: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 45.00% โ
โ 3: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 53.33% โ
โ 4: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 61.81% โ
โ 5: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 70.42% โ
โ 6: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 79.17% โ
โ 7: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 88.06% โ
โ 8: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 97.08% โ
โ 9: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 97.92% โ
โ 10: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 98.61% โ
โ 11: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 99.17% โ
โ 12: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 99.58% โ
โ 13: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 99.86% โ
โ 14: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 100.00% โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโ
โ Outcome Table () โ
โโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DAMAGE โ PERCENT โ Crit % โ Hit % โ Miss % โ
โโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโค
โ 0 โ 45.000% โ 0.000% โ 0.000% โ 45.000% โ
โ 3 โ 8.333% โ 0.000% โ 8.333% โ 0.000% โ
โ 4 โ 8.472% โ 0.139% โ 8.333% โ 0.000% โ
โ 5 โ 8.611% โ 0.278% โ 8.333% โ 0.000% โ
โ 6 โ 8.750% โ 0.417% โ 8.333% โ 0.000% โ
โ 7 โ 8.889% โ 0.556% โ 8.333% โ 0.000% โ
โ 8 โ 9.028% โ 0.694% โ 8.333% โ 0.000% โ
โ 9 โ 0.833% โ 0.833% โ 0.000% โ 0.000% โ
โ 10 โ 0.694% โ 0.694% โ 0.000% โ 0.000% โ
โ 11 โ 0.556% โ 0.556% โ 0.000% โ 0.000% โ
โ 12 โ 0.417% โ 0.417% โ 0.000% โ 0.000% โ
โ 13 โ 0.278% โ 0.278% โ 0.000% โ 0.000% โ
โ 14 โ 0.139% โ 0.139% โ 0.000% โ 0.000% โ
โโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโ
๐ฒ Sample Dice Expression Grammar
๐งฑ Roadmap
- Create a web playground with live examples
- Consider creating higher-level APIs:
Turn,Attack,DamageRider - Simplify and improve PMF and Query interface ergonomics
- Add more comprehensive 5e rule examples
- Performance improvements for DPR-only calculations
- Multi-round and sustained vs nova simulations
- Deeper integration with dprcalc.com
- Blog posts and documentation
- Grammar refinements and new YACC parsing
๐ฌ Discuss
Join our Discord to discuss this library and more!
๐ค Contributing
Clone the repo and install dependencies:
git clone https://github.com/yipe/dice.git
cd dice
yarn installRun tests
yarn testRun examples
yarn example๐ License
2025 MIT ยฉ Michael Margolis
โ๏ธ Legal / Trademarks
Wizards of the Coast, Dungeons & Dragons, and their logos are trademarks of Wizards of the Coast LLC in the United States and other countries.
ยฉ 2025 Wizards. All Rights Reserved.
โค๏ธ Credits
Portions of this code are inspired by dice.clockworkmod.com by Koushik Dutta (2013), licensed under the Apache License 2.0.
Initial TypeScript port expertly created by loginName1.