/*
Dice snippet
$dice(<parameters>).<properties>
Syntax: $dice(R [,DN] [,SN] [,CN] [,A<reroll rules>]) [.sum|success|failed|critical|botch|rerolls|string|rerollstring|r|d|s|c]
----- Parameters -----
All parameters are optional.
R = number of rolls (number of dice). If used, R has to be a positive integer and the first parameter.
All other parameters (D, S, C and A) don't have to be in order.
The D, S and C parameters consist of the indicating letter, followed by a positive integer N.
D = number of sides of the dice (type of dice)
S = lowest side of the dice to be regarded a "successful" roll
C = lowest side of the dice to be regarded a "critical" roll
A = matching dice will be rolled again (reroll).
The A paramter consists of the indicating letter "a/A", followed by either:
-- a string of integers, separated by dots
-- an integer N and "+": N and all higher sides will be rerolled
-- an integer N and "-": N and all lower sides will be rerolled
If you use "a" (lowercase), dice will be rerolled only once. If you use "A" (uppercase), dice will be rerolled till they don't match the reroll rule.
If you don't provide R, D, S, C, or A, the following defaults will be used:
-- if no R is given, a single dice is used (R = 1)
-- if no D is given, a 10-sided dice is used (D = 10)
-- if no S (with N <= D) is given, a roll is regarded successful if it's equal to or biger than [D / 2 + 1, rounded up to the next integer]
-- (the default S for D2 is 2, for D3-D4 it's 3, for D5-D6 it's 4 etc)
-- if no C (with N <= D) is given, a roll is regarded critical if it has the highest possible result (that is: D)
-- if no A is given, no reroll will occur
Examples:
$dice() will return the sum of a 1D10
$dice(5) will return the sum of a 5D10
$dice(2,D3) will return the sum of a 2D3
$dice(4,A5.6,D7) will return the sum of a 4D7 while rerolling 5es and 6es
$dice(4,a3-) will return the sum of a 4D10 with one-time rerolls for 3, 2, and 1
----- Properties -----
All properties are optional. Properties don't have to be in order.
sum = total of the rolls (same as using no property at all)
success = number of successful rolls (after rerolls)
failed = number of failed rolls (= total rolls - successful rolls)
critical = number of critical rolls (after rerolls)
botch = number of botch rolls (after rerolls)
rerolls = number of rerolls
string = space-delimited string of the individual dice results (after possible rerolls)
-- If you use STRING (uppercase), results will be zeropadded to the length of D.
-- Even though the script will calculate larger rolls, this string will hold no more than 50 dice.
rerollstring = space-delimited string of the dice results INCLUDING all rerolls
-- If you use REROLLSTRING (uppercase), results will be zeropadded by the length of D.
-- Even though the script will calculate larger rolls, this string will hold no more than 50 dice.
r = number of rolls performed (e.g. if no valid R specified). Note that it is NOT counting rerolls
d = number of sides of the dice used (e.g. if no valid D specified)
s = value used as success threshold (e.g. if no valid S specified)
c = value used as success threshold (e.g. if no valid C specified)
The r,d,s and c properties are most of all for convenience
Examples:
$dice() = sum of a default roll with a default dice (a 1D10)
$dice(8,D5).success = number of successful rolls of a 8D5 (using default success value where a roll of 4+ is regarded "success")
$dice(8,D5,S5).success = number of successful rolls of a 8D5, where a roll of 5+ is regarded "success"
$dice(4).failed = number of failed rolls of a 4D10 (using default values for D and success, where for a D10 a roll of 5- is regarded "failure")
$dice(3,D3).critical = number of critical rolls of a 3D3 (using default critical value = 3)
$dice(10,D9,C7).critical = number of critical rolls of a 10D9, where a roll of 7+ is regarded "critical"
$dice(5).botch = number of botches of a 5D10
$dice(7,D7,A5+).rerolls = number of rerolls of a 7D7, rerolling 5,6 and 7
$dice(10,D6).string = a string of the individual results of a 10D6
$dice(10,D6,a4).rerollstring = a string of the individual results after non-recurring rerolls of a 10D6, rerolling 4s
$dice().d = default D value (10)
$dice().s = default S value for a default D roll (= 6 for a default D10)
NOTE: The snippet gets versatility by allowing you to use multiple properties, separated by comma. The results will be separated by comma.
Examples:
"$dice(7).success,critical" will for a 7D10 return: <successful rolls>,<critical rolls>
"$dice(8,S7,D8).sum,failed,botch" will for a 8D8 return: <sum of rolls>,<failed rolls, where 6- is regarded "failure">,<botch rolls>
"$dice(3,D9,C8).critical,success" will for a 3D9 return:
<critical rolls, where 8+ is regarded "critical">,<successfull rolls, where for a D9 6+ is regarded "success")
"$dice(5,D12,a4-).STRING,rerolls,REROLLSTRING" will for a 5D12 return:
<zero-padded results string (after rerolls)>,<number of non-recurring rerolls>,<zero-padded string with rerolls>
"$dice(D4,C6).c,critical" will for a 1D4 return:
<default critical threshold of a D4 (as the invalid parameter "C6" is ignored for being bigger than D4)>,<critical rolls of the 1D4, using the 3+ threshold>
*/
alias dice {
if ($isid) {
var %sum = 0, %success = 0, %critical = 0, %botch = 0, %rerolls = 0, %loop, string, %rerollstring, %n = 1
; parse parameters
var %r = $iif(($1 isnum 1-),$int($v1),1)
var %d = $iif(($mid($wildtok($1-,D*,1,32),2-) isnum 1-),$int($v1),10)
var %s = $iif((($mid($wildtok($1-,S*,1,32),2-) isnum 1-) && ($int($v1) <= %d)),$v1,$ceil($calc(%d / 2 $iif((%d > 1),+ 1))))
var %c = $iif((($mid($wildtok($1-,C*,1,32),2-) isnum 1-) && ($int($v1) < %d)),$v1,%d)
var %a = $wildtok($1-,A*,1,32), %ap = $mid(%a,2-), %repeat = $iif(%ap,$iif(($left(%a,1) isupper),1,0))
if (%repeat != $null) {
if ($right(%ap,1) isin +-) && ($left(%ap,-1) isnum 1-) { var %a = $int($v1), %ap = $iif(($right(%ap,1) == +),2,1) }
else { var %a = %ap, %ap = 0 }
}
; %repeat: 1=loop, 0=single; %a = value; %ap: 2=and higher, 1=and lower, 0=tokens
; parse properties (1) - create (zeropadded) string of results?
var %mkstring = $iif(($wildtok($prop,string,1,44)),$iif(($v1 isupper),$len(%d),1))
var %mkrstring = $iif(($wildtok($prop,rerollstring,1,44)),$iif(($v1 isupper),$len(%d),1))
; roll the dice!
while (%n <= %r) {
:reroll
var %x = $rand(1,%d)
if (%repeat != $null) {
if ((%ap == 2) && (%x >= %a)) || ((%ap == 1) && (%x <= %a)) || ($istok(%a,%x,46)) {
if (!%loop) {
if (!%repeat) var %loop 1
if (%mkrstring) && ($numtok($replace(%rerollstring,-,$chr(32)),32) < 50) $&
var %rerollstring = $+(%rerollstring,$iif(($right(%rerollstring,1) isnum),$chr(32)),$iif((%mkrstring < 2),%x,$base(%x,10,10,$v1)),->)
inc %rerolls
goto reroll
}
}
unset %loop
}
if (%x == 1) inc %botch
if (%x >= %s) inc %success
if (%x >= %c) inc %critical
inc %sum %x
if (%mkstring) && ($numtok(%string,32) < 50) var %string = %string $iif((%mkstring < 2),%x,$base(%x,10,10,$v1))
if (%mkrstring) && ($numtok($replace(%rerollstring,-,$chr(32)),32) < 50) $&
var %rerollstring = $+(%rerollstring,$iif(($right(%rerollstring,1) isnum),$chr(32)),$iif((%mkrstring < 2),%x,$base(%x,10,10,$v1)))
inc %n
}
var %failed = %r - %success
; parse properties (2)
; every valid property is replaced with the value of the variable which has the name of the property
var %return = $regsubex($prop,/(?<=^|\54)(sum|success|failed|critical|botch|rerolls|string|rerollstring|r|d|s|c)(?=$|\54)/ig, $var(\1,1).value)
; return the result acc. to properties, or the sum if no property was used
return $iif($regml(0),%return,%sum)
}
}