Tables refer to other tables
If an entry in a table references <<another/table>>, that reference is replaced with a call to that other table. This allows building complex responses.
This commit is contained in:
parent
769fcc27d4
commit
b0c3601e4c
1 changed files with 16 additions and 4 deletions
|
|
@ -106,13 +106,25 @@ dice table (see `rpgdm-tables--choose-dice-table')."
|
||||||
((hash-table-p table) (rpgdm-tables--choose-freq-table table))
|
((hash-table-p table) (rpgdm-tables--choose-freq-table table))
|
||||||
((functionp table) (call-interactively table))
|
((functionp table) (call-interactively table))
|
||||||
((listp table) (rpgdm-tables--choose-list table))
|
((listp table) (rpgdm-tables--choose-list table))
|
||||||
(t "Error: Could choose anything from %s (internal bug?)" table-name)))
|
(t "Error: Could not choose anything from %s (internal bug?)" table-name)))
|
||||||
;; Replace any dice expression in the message with an roll:
|
;; Replace any dice expression in the message with an roll:
|
||||||
(dice-sum (lambda (dice-exp) (number-to-string (rpgdm-roll-sum dice-exp))))
|
(dice-sum (lambda (dice-exp) (number-to-string (rpgdm-roll-sum dice-exp))))
|
||||||
(no-dice-nums (replace-regexp-in-string rpgdm-roll-regexp dice-sum result))
|
(no-dice-nums (replace-regexp-in-string rpgdm-roll-regexp dice-sum result))
|
||||||
(no-alt-words (rpgdm-tables--choose-string-list no-dice-nums)))
|
(no-alt-words (rpgdm-tables--choose-string-list no-dice-nums))
|
||||||
(kill-new no-alt-words)
|
|
||||||
(rpgdm-message "%s" no-alt-words))))
|
;; Can we replace a <<tablename>> substring with the results from calling this function again?
|
||||||
|
(final-choice (replace-regexp-in-string
|
||||||
|
(rx "<<" (group (one-or-more (not ">"))) ">>")
|
||||||
|
'rpgdm-tables--choose-replacement no-alt-words)))
|
||||||
|
(kill-new final-choice)
|
||||||
|
(rpgdm-message "%s" final-choice))))
|
||||||
|
|
||||||
|
(defun rpgdm-tables--choose-replacement (str)
|
||||||
|
"Given STR like, <<foobar>>, return call to `rpgdm-tables-choose'.
|
||||||
|
However, the `<<...>>' characters are replaced when calling function."
|
||||||
|
(if (string-match (rx "<<" (group (one-or-more (not ">"))) ">>") str)
|
||||||
|
(rpgdm-tables-choose (match-string 1 str))
|
||||||
|
str))
|
||||||
|
|
||||||
(defun rpgdm-tables--choose-list (lst)
|
(defun rpgdm-tables--choose-list (lst)
|
||||||
"Randomly choose (equal chance for any) element in LST."
|
"Randomly choose (equal chance for any) element in LST."
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue