From 0ca1b0a8d9ab0a7d687a9f2edb6eae2e57851129 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Fri, 28 Jun 2019 14:56:07 +0100 Subject: loop->cl-loop --- org-d20.el | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'org-d20.el') diff --git a/org-d20.el b/org-d20.el index 55f05eb..59d7f4f 100644 --- a/org-d20.el +++ b/org-d20.el @@ -4,7 +4,7 @@ ;; Author: Sean Whitton ;; URL: https://spwhitton.name/tech/code/org-d20/ -;; Version: 0.3 +;; Version: 0.4 ;; Package-Requires: ((s "1.11.0") (seq "2.19") (dash "2.12.0") (emacs "24")) ;; Keywords: outlines games @@ -47,6 +47,7 @@ (require 's) (require 'seq) (require 'dash) +(require 'cl-lib) (require 'org-table) (defgroup org-d20 nil @@ -224,7 +225,7 @@ the best N of them, e.g., 4d6k3." (interactive "*") (let ((rows)) (let (name-input init-input hd-input num-input (monster 1)) - (loop + (cl-loop do (setq name-input (read-string "Monster/NPC name (blank when done): ")) (when (> (length name-input) 0) (setq init-input (read-string (concat name-input "'s init modifier: ")) @@ -276,7 +277,7 @@ the best N of them, e.g., 4d6k3." "Advance the turn tracker in an initiative table." (interactive "*") (when (org-at-table-p) - (loop + (cl-loop do (let* ((back (search-backward ">>>>" (org-table-begin) t)) (forward (search-forward ">>>>" (org-table-end) t)) (cur (if back back forward))) -- cgit v1.2.3 From 7ede5726d640c4f81146585f6eb907f51a0f6a19 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Thu, 16 Apr 2020 13:10:39 -0700 Subject: add TODO --- org-d20.el | 3 +++ 1 file changed, 3 insertions(+) (limited to 'org-d20.el') diff --git a/org-d20.el b/org-d20.el index 59d7f4f..05b7510 100644 --- a/org-d20.el +++ b/org-d20.el @@ -107,6 +107,9 @@ Org-mode document." ;;; Dice rolling +;; TODO: Also support '2d20kl1' to drop all but the lowest roll' +;; (spw 2020-04-16) + (defun org-d20--roll (exp) "Evaluate dice roll expression EXP. -- cgit v1.2.3 From e39134094c6384f0f09346208546137f4fc36e5e Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 7 Nov 2020 13:55:00 -0700 Subject: improve readability of org-d20--rolls-bracket Signed-off-by: Sean Whitton --- org-d20.el | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'org-d20.el') diff --git a/org-d20.el b/org-d20.el index 05b7510..dcb6c27 100644 --- a/org-d20.el +++ b/org-d20.el @@ -429,23 +429,15 @@ the best N of them, e.g., 4d6k3." ;; Bracket a number so it looks a bit like a dice roll result (defun org-d20--rolls-bracket (sides roll) - (let ((roll* (int-to-string roll))) - (cond ((= sides 4) - (concat "‹" roll* "›")) - ((= sides 6) - (concat "|" roll* "|")) - ((= sides 8) - (concat "/" roll* "/")) - ((= sides 10) - (concat "{" roll* "}")) - ((= sides 12) - (concat "⟨" roll* "⟩")) - ((= sides 20) - (concat "(" roll* ")")) - ((= sides 100) - (concat "«" roll* "»")) - (t - (concat "[" roll* "]"))))) + (let ((brackets (or (assoc sides '((4 "‹" "›") + (6 "|" "|") + (8 "/" "/") + (10 "{" "}") + (12 "⟨" "⟩") + (20 "(" ")") + (100 "«" "»"))) + '(nil "[" "]")))) + (concat (cadr brackets) (int-to-string roll) (caddr brackets)))) (defun org-d20--org-table-end-of-current-cell-content () "Move point to the end of the content of the current Org table cell." -- cgit v1.2.3 From cfbd4ae7343733b508feaf453b91facd1d0fb44d Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 7 Nov 2020 13:57:29 -0700 Subject: use nth rather than seq-elt, and use a static quoted list Signed-off-by: Sean Whitton --- org-d20.el | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'org-d20.el') diff --git a/org-d20.el b/org-d20.el index dcb6c27..1aa4b7c 100644 --- a/org-d20.el +++ b/org-d20.el @@ -411,10 +411,8 @@ the best N of them, e.g., 4d6k3." ;; Return the number or letter with which a monster name should be suffixed (defun org-d20--monster-number (n) (if (and org-d20-letter-monsters (>= 26 n)) - (seq-elt - (list "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" - "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z") - (1- n)) + (nth (1- n) '("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" + "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z")) (int-to-string n))) ;; Concat b onto a as a signed term, where a is possibly empty -- cgit v1.2.3 From 84b50eb38f6ffe2c454998318ca6e36a3123a3d5 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 7 Nov 2020 15:55:38 -0700 Subject: avoid using s-blank? Signed-off-by: Sean Whitton --- org-d20.el | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'org-d20.el') diff --git a/org-d20.el b/org-d20.el index 1aa4b7c..29bc283 100644 --- a/org-d20.el +++ b/org-d20.el @@ -417,13 +417,17 @@ the best N of them, e.g., 4d6k3." ;; Concat b onto a as a signed term, where a is possibly empty (defun org-d20--rolls-concat (sign a b) - (if (>= sign 0) - (if (s-blank? a) - b - (concat a " + " b)) - (if (s-blank? a) - (concat "- " b) - (concat a " - " b)))) + (let (strings) + (if (or (null a) (string= "" a)) + (unless (>= sign 0) + (push " - " strings)) + (push a strings) + (push (if (>= sign 0) + " + " + " - ") + strings)) + (push b strings) + (apply #'concat (nreverse strings)))) ;; Bracket a number so it looks a bit like a dice roll result (defun org-d20--rolls-bracket (sides roll) -- cgit v1.2.3 From a081171e216efd9fcc913026f74e32a681f0f76f Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 7 Nov 2020 17:06:02 -0700 Subject: tweak use of new page character Signed-off-by: Sean Whitton --- org-d20.el | 3 --- 1 file changed, 3 deletions(-) (limited to 'org-d20.el') diff --git a/org-d20.el b/org-d20.el index 29bc283..9c01e58 100644 --- a/org-d20.el +++ b/org-d20.el @@ -104,7 +104,6 @@ Org-mode document." :lighter " d20") - ;;; Dice rolling ;; TODO: Also support '2d20kl1' to drop all but the lowest roll' @@ -220,7 +219,6 @@ the best N of them, e.g., 4d6k3." (play-sound-file org-d20-dice-sound))) - ;;; Combat tracking (defun org-d20-initiative () @@ -398,7 +396,6 @@ the best N of them, e.g., 4d6k3." (org-d20-initiative))) - ;;; helper functions ;; Convert a signed integer to a string term -- cgit v1.2.3 From a64af30bd4e5ffe4d49813abe7776a0676c3c74f Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 7 Nov 2020 17:10:30 -0700 Subject: implement displaying recent dice rolls in a split window Signed-off-by: Sean Whitton --- NEWS.md | 1 + TODO.md | 2 -- org-d20.el | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 6 deletions(-) (limited to 'org-d20.el') diff --git a/NEWS.md b/NEWS.md index 279d58c..e28052b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ 0.4 (unreleased) ---------------- +- New defcustom, `org-d20-display-rolls-buffer`. - Replace some calls to `loop` with calls to `cl-loop`. - Other code cleanup. diff --git a/TODO.md b/TODO.md index 4dc9be5..d9ad189 100644 --- a/TODO.md +++ b/TODO.md @@ -3,5 +3,3 @@ Features - defcustom to roll each monster's initiative separately (as in 3e), rather than having type of monster act at the same time (as in 5e) - -- small window with trail of recent rolls diff --git a/org-d20.el b/org-d20.el index 9c01e58..ed3dc7f 100644 --- a/org-d20.el +++ b/org-d20.el @@ -82,6 +82,11 @@ Rather than starting again for each type." :type 'boolean :group 'org-d20) +(defcustom org-d20-display-rolls-buffer nil + "Non-nil means split the window and display history of dice rolls." + :type 'boolean + :group 'org-d20) + (defvar org-d20-mode-map (let ((map (make-sparse-keymap))) (define-key map (kbd "C-c , i") #'org-d20-initiative-dwim) @@ -186,8 +191,8 @@ the best N of them, e.g., 4d6k3." (frame-width)) (s-replace " " "" rolls) rolls))) - (message "%s = %s = %s" exp rolls-display result*)) - (message "%s = %s" exp (int-to-string result)))) + (org-d20--record-roll "%s = %s = %s" exp rolls-display result*)) + (org-d20--record-roll "%s = %s" exp (int-to-string result)))) (when org-d20-dice-sound (play-sound-file org-d20-dice-sound))) @@ -213,8 +218,8 @@ the best N of them, e.g., 4d6k3." (disadv (if (<= fst snd) (concat (propertize fst* 'face 'bold) " " snd*) (concat fst* " " (propertize snd* 'face 'bold))))) - (message "No adv./disadv.: %s\tAdv.: %s\tDisadv.: %s" - fst* adv disadv)) + (org-d20--record-roll "No adv./disadv.: %s\tAdv.: %s\tDisadv.: %s" + fst* adv disadv)) (when org-d20-dice-sound (play-sound-file org-d20-dice-sound))) @@ -448,5 +453,30 @@ the best N of them, e.g., 4d6k3." (defun org-d20--d20-plus (&optional mod) (+ 1 mod (random 20))) +;; Record and display a new dice roll result +(defun org-d20--record-roll (&rest args) + (let ((roll (apply #'format args))) + (with-current-buffer (get-buffer-create "*Dice Trail*") + (setq-local require-final-newline nil) + (goto-char (point-max)) + (unless (bolp) + (insert "\n")) + (insert " " roll)) + (cl-flet ((scroll (window) + (with-selected-window window + (goto-char (point-max)) + (beginning-of-line) + (recenter -1) + (redisplay)))) + (if-let ((window (get-buffer-window "*Dice Trail*"))) + (when (window-parameter window 'org-d20--dice-trail-split) + (scroll window)) + (if (and org-d20-mode org-d20-display-rolls-buffer) + (let ((window (split-window nil -8 'below))) + (set-window-buffer window "*Dice Trail*") + (set-window-parameter window 'org-d20--dice-trail-split t) + (scroll window)) + (message roll)))))) + (provide 'org-d20) ;;; org-d20.el ends here -- cgit v1.2.3 From af5e2afb8c862f348da75c25cb0a71fb444fe2a7 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sun, 8 Nov 2020 11:20:11 -0700 Subject: also redraw the frame Signed-off-by: Sean Whitton --- org-d20.el | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'org-d20.el') diff --git a/org-d20.el b/org-d20.el index ed3dc7f..198b585 100644 --- a/org-d20.el +++ b/org-d20.el @@ -467,7 +467,12 @@ the best N of them, e.g., 4d6k3." (goto-char (point-max)) (beginning-of-line) (recenter -1) - (redisplay)))) + ;; without the following line, the dice roll doesn't show up + ;; until `play-sound-file' has finished + (redisplay) + ;; and without this line, the split window appears selected + ;; even when it isn't + (redraw-frame)))) (if-let ((window (get-buffer-window "*Dice Trail*"))) (when (window-parameter window 'org-d20--dice-trail-split) (scroll window)) -- cgit v1.2.3 From a3734368cd5a36541dab2fa8f7404cb850991b5b Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Thu, 12 Nov 2020 21:53:08 -0700 Subject: org-d20-d20: show d4 in case Bless spell is active Signed-off-by: Sean Whitton --- NEWS.md | 1 + org-d20.el | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'org-d20.el') diff --git a/NEWS.md b/NEWS.md index e28052b..8ba0aef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ ---------------- - New defcustom, `org-d20-display-rolls-buffer`. +- `org-d20-d20` shows the result of rolling a d4 in case /Bless/ is active. - Replace some calls to `loop` with calls to `cl-loop`. - Other code cleanup. diff --git a/org-d20.el b/org-d20.el index 198b585..28ab294 100644 --- a/org-d20.el +++ b/org-d20.el @@ -210,16 +210,25 @@ the best N of them, e.g., 4d6k3." (interactive) (let* ((fst (cdr (org-d20--roll "1d20"))) (snd (cdr (org-d20--roll "1d20"))) + (bls (cdr (org-d20--roll "1d4"))) (fst* (int-to-string fst)) (snd* (int-to-string snd)) + (bls* (int-to-string bls)) (adv (if (>= fst snd) (concat (propertize fst* 'face 'bold) " " snd*) (concat fst* " " (propertize snd* 'face 'bold)))) (disadv (if (<= fst snd) (concat (propertize fst* 'face 'bold) " " snd*) (concat fst* " " (propertize snd* 'face 'bold))))) - (org-d20--record-roll "No adv./disadv.: %s\tAdv.: %s\tDisadv.: %s" - fst* adv disadv)) + (org-d20--record-roll + "No adv./disadv.: %s%sAdv.: %s%sDisadv.: %s%sBless: %s" + fst* + (make-string (- 4 (length fst*)) ?\ ) + adv + (make-string (- 8 (length adv)) ?\ ) + disadv + (make-string (- 10 (length disadv)) ?\ ) + bls*)) (when org-d20-dice-sound (play-sound-file org-d20-dice-sound))) -- cgit v1.2.3