# Dice roller, by Kardafol
#rolldice off
on *:text:!roll*:#: {
;flood protection
if (%flood.roll) return
set %flood.roll 1
.timerflood.roll 1 3 unset %flood.roll
; syntax things, and error checking
if ((!$2) || ($2 == help)) {
msg # Syntax: <<dice>d<sides: 4/6/8/10/20/100>|<number>>[<+/->...]. <> = required, [] = optional. $&
... means you may repeat the expression. <number> is a number to add to the total without rolling a dice for it. The first part must be a roll. $&
Limit to <dice> is 100.
msg # Examples: !roll 2d6+5, !roll 1d20, !roll 2d6+1d8-3. Bad examples: !roll 2d2+4, !roll 5+2d6, !roll 101d4+5
return
}
var %x = $rolldice($2-)
if (-* iswm %x) {
msg # $nick $+ , your syntax is invalid. Possible errors: $gettok(%x,2-,32) $+ .
return
}
else {
describe # rolls dice for $nick $+ , and gets: %x
}
}
alias rolldice {
if (!$1) return -
; Syntax checking:
var %s = $1-, %st = 09, %f = 1
while ($regsub(%s,/^((\d+)d(?:4|6|8|10|20|100)(?:[-+]|$)|\d+(?:[-+]|$))/,$null,%s)) {
if ((%f == 1) && (*d* !iswm $regml(1))) {
return - $+(%st,04,$regml(1),%s)
}
if ($regml(2) > 100) {
return - $+(%st,04,$regml(1),%s)
}
var %st = %st $+ $regml(1)
inc %f
}
if (%s != $null) {
return - $+(%st,04,%s)
}
; The actual calculations and rolls
var %x = $1-, %rolls, %t, %total
while ($regsub(roll,%x,/^(?:(\d+)(d)(4|6|8|10|20|100)([-+]|$)|(\d+)([-+]|$))/,$null,%x)) {
if ($regml(roll,2) == d) {
var %d = $regml(roll,1), %s = $regml(roll,3)
var %o = $droll(%d,%s), %t = $gettok(%o,1,32), %rx = $gettok(%o,2-,32)
var %total = $calc(%t %m %total)
var %rolls = %rolls $iif(%m,%m) $+([,%rx,])
var %m = $regml(roll,4)
}
else {
if (!%m) %m = +
var %total = $iif(%total,$calc(%total %m $regml(roll,1)),$regml(roll,1))
var %rolls = %rolls %m $regml(roll,1)
var %m = $regml(roll,2)
}
}
if ($len(%rolls) > 150) %rolls = [hidden, too long to display.]
return %rolls = %total
}
alias droll {
if (($1 !isnum) || ($2 !isnum)) return
var %n = $1, %s = $2, %r, %rolls, %t
while (%n) {
%r = $rand(1,%s)
%t = $calc(%t + %r)
%rolls = $iif(%rolls,$+(%rolls,+,%r),%r)
dec %n
}
return %t $+(%rolls,=,%t)
}
#rolldice end
alias diceroll {
if (!$1) {
echo -ac info * Dice Roll is $iif($group(rolldice) == on,enabled,disabled) $+ . /diceroll <on/off> to enable, disable.
}
if ($1 == on) {
if ($group(#rolldice) == on) echo -ac info * Dice Roll is already enabled.
else {
.enable #rolldice
echo -ac info * Dice Roll has been enabled.
}
}
elseif ($1 == off) {
if ($group(#rolldice) == off) echo -ac info * Dice Roll is already disabled.
else {
.disable #rolldice
echo -ac info * Dice Roll has been disabled.
}
}
}
on *:load: {
echo -ac info * Dice Roll V1 by kardafol has been sucessfully loaded.
echo -ac info * Type /diceroll on to enable dice rolling. (Disabled by deafult.)
}