aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-03 18:03:46 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-03 18:03:46 +1200
commit25e144a02c3b285cec9f444d17033b6222a53e79 (patch)
treea8c410bd0b132ec8e9a57eb5a858fc254deb5b36
parentAdd rnds(1) (diff)
downloaddotfiles-25e144a02c3b285cec9f444d17033b6222a53e79.tar.gz
dotfiles-25e144a02c3b285cec9f444d17033b6222a53e79.zip
Add dr(6)
Tabletop dice rolling
-rwxr-xr-xgames/dr34
-rw-r--r--man/man6/dr.621
2 files changed, 55 insertions, 0 deletions
diff --git a/games/dr b/games/dr
new file mode 100755
index 00000000..785ec2bd
--- /dev/null
+++ b/games/dr
@@ -0,0 +1,34 @@
+#!/bin/sh
+# Roll D&D-style dice in a ndn+n formula, e.g. 10d6+2
+
+# Need exactly one argument
+[ "$#" -eq 1 ] || exit 2
+
+# Arcane string chopping
+n=1 a=0 nd=${1%+*} d=${nd#*d}
+[ "${nd%d*}" != "" ] && n=${nd%d*}
+[ "${1#*+}" = "$1" ] || a=${1#*+}
+
+# Check number of roles and addendum make sense
+[ "$((n > 0 && a >= 0))" -eq 1 ] || exit 2
+
+# Check this is a real die you can actually roll
+case $d in
+ 4|6|8|10|12|20) : ;;
+ *) exit 2 ;;
+esac
+
+# Roll the dice the appropriate number of times using rndi(1)
+i=0 t=0
+while [ "$i" -lt "$n" ] ; do
+ seed=$(rnds)
+ r=$(rndi 1 "$d" "$seed")
+ t=$((t + r))
+ i=$((i + 1))
+done
+
+# Add the addendum
+t=$((t + a))
+
+# Print it
+printf '%u\n' "$t"
diff --git a/man/man6/dr.6 b/man/man6/dr.6
new file mode 100644
index 00000000..fc586bb2
--- /dev/null
+++ b/man/man6/dr.6
@@ -0,0 +1,21 @@
+.TH DR 6 "August 2016" "Manual page for dr"
+.SH NAME
+.B dr
+\- roll D&D dice
+.SH USAGE
+.B dr
+d6
+.br
+.B dr
+2d12
+.br
+.B dr
+3d4+10
+.SH DESCRIPTION
+.B dr
+rolls dice according to the formulas used in D&D and other tabletop roleplaying
+games. It only allows d4, d6, d8, d10, d12, and d20 dice.
+.SH SEE ALSO
+rndi(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>