summaryrefslogtreecommitdiff
path: root/.emacs.d/site-lisp
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2017-09-18 07:17:32 -0700
committerSean Whitton <spwhitton@spwhitton.name>2017-09-18 07:23:35 -0700
commit0d4de667b19170a1f1537ea4665d78845708fc9c (patch)
treec367f47d4537c47e73f3d2e51e35403919c256fe /.emacs.d/site-lisp
parentc0cf16fe385b29f1e937d7c1dc6112fad7dcccf1 (diff)
downloaddotfiles-0d4de667b19170a1f1537ea4665d78845708fc9c.tar.gz
restructure rolling function; seems to work now
Diffstat (limited to '.emacs.d/site-lisp')
-rw-r--r--.emacs.d/site-lisp/spwd20.el44
1 files changed, 23 insertions, 21 deletions
diff --git a/.emacs.d/site-lisp/spwd20.el b/.emacs.d/site-lisp/spwd20.el
index 009f150f..c508cac2 100644
--- a/.emacs.d/site-lisp/spwd20.el
+++ b/.emacs.d/site-lisp/spwd20.el
@@ -38,27 +38,29 @@
Accepts roll20's extension for rolling multiple dice and keeping
the best N of them, e.g., 4d6k3."
- (let* ((exps (s-slice-at "[+-]" exp))
- (ours (s-chop-prefixes (list "+" "-") (car exps)))
- (sign (if (string= (substring exp 0 1) "-")
- -1 1)))
- (+ (if (s-index-of "d" ours)
- (let* ((split (s-split "[dk]" ours))
- (times (string-to-int (seq-elt split 0)))
- (sides (string-to-int (seq-elt split 1)))
- (keep (if (> (length split) 2)
- (string-to-int (seq-elt split 2))
- nil))
- (rolls))
- (while (> times 0)
- (let ((roll (+ 1 (random (- sides 1)))))
- (push roll rolls)))
- (-sum
- (if keep
- (seq-drop (sort rolls) keep)
- rolls)))
- (string-to-int ours))
- (-sum (seq-map 'spwd20--roll (cdr exps))))))
+ (let ((exps (seq-map (lambda (s) (s-chop-prefix "+" s))
+ (s-slice-at "[+-]" exp))))
+ (-sum (seq-map 'spwd20--roll-inner exps))))
+
+(defun spwd20--roll-inner (exp)
+ (let* ((sign (if (s-prefix-p "-" exp) -1 1))
+ (ours (s-chop-prefix "-" exp))
+ (split (seq-map 'string-to-int (s-split "[dk]" ours)))
+ (times (seq-elt split 0))
+ (sides (ignore-errors (seq-elt split 1)))
+ (keep (ignore-errors (seq-elt split 2)))
+ (rolls))
+ (* sign
+ (if (not sides)
+ times
+ (while (> times 0)
+ (let ((roll (+ 1 (random (- sides 1)))))
+ (push roll rolls))
+ (setq times (- times 1)))
+ (-sum
+ (if keep
+ (seq-drop (sort rolls '<) (- times keep))
+ rolls))))))
;;;###autoload
(define-minor-mode spwd20-mode