Discuss this page on this page's [[extStringToDieRoll Talk Page|Talk Page]]. === extStringToDieRoll === See [[http://www.neurohack.com/transcendence/forums/viewtopic.php?p=27071#27071|forum post]] by [[..:users:alterecco]]. ^ Syntax | (extStringToDieRoll string) -> number | ^ Argument List | string: A string that can be interpreted as a number range or dice roll. | ^ Returns | number: A random number resulting from that dice roll, or selected from that range. | ^ Category | general helper functions | ^ Description | A function that converts string representations of die rolls to actual die rolls. | It accepts strings like "2d6", "8d4+5", "2-10", "1" "2d6" is two rolls of a six sided die "8d4+5" is eight rolls of a four sided die with five added to the final result "2-10" is a number between two and ten "1" is just one IMPORTANT: Don't pass it junk strings, as there is no error checking :) === Example === (setq diceroll (extStringToDieRoll "2d6")) (if diceroll 2) (plyMessage gPlayer "Snake Eyes!") (plyMessage gPlayer (cat "You rolled " diceroll)) ) Displays the result of rolling a pair of dice to the player. === Function code === (setq extStringToDieRoll (lambda (string) (block (pos) (switch ;; we actually got passed a number (isInt string) string ;; normal die roll, ie: 4d12 or 4d12+2 (setq pos (find string 'd)) (block (die sides bpos bonus) (setq die (subset string 0 pos)) (setq sides (subset string (add pos 1))) (if (or (setq bpos (find string "+")) (setq bpos (find string "-"))) (block Nil (setq sides (subset string (add pos 1) (subtract bpos (add pos 1)))) (setq bonus (subset string bpos)) )) (rollDice (int die) (int sides) (int bonus)) ) ;; range, ie: 1-4 (setq pos (find string '-)) (random (int (subset string 0 pos)) (int (subset string (add pos 1)))) ;; fixnum? (int string) ) ) )) Return to [[:Functions|functions list]]