2010-01-17 04:24:36 +00:00
|
|
|
"""
|
|
|
|
dice.py: written by Scaevolus 2008, updated 2009
|
|
|
|
simulates dicerolls
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
import random
|
|
|
|
|
|
|
|
from util import hook
|
|
|
|
|
|
|
|
|
|
|
|
whitespace_re = re.compile(r'\s+')
|
2010-08-16 09:32:28 +00:00
|
|
|
valid_diceroll = r'^([+-]?(?:\d+|\d*d(?:\d+|F))(?:[+-](?:\d+|\d*d(?:\d+|F)))*)( .+)?$'
|
2010-08-14 21:27:09 +00:00
|
|
|
valid_diceroll_re = re.compile(valid_diceroll, re.I)
|
2010-08-14 21:07:17 +00:00
|
|
|
sign_re = re.compile(r'[+-]?(?:\d*d)?(?:\d+|F)', re.I)
|
|
|
|
split_re = re.compile(r'([\d+-]*)d?(F|\d*)', re.I)
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def nrolls(count, n):
|
|
|
|
"roll an n-sided die count times"
|
2010-08-17 17:00:41 +00:00
|
|
|
if n == "F":
|
2010-08-30 03:35:27 +00:00
|
|
|
return [random.randint(-1, 1) for x in xrange(min(count, 100))]
|
2010-03-01 02:32:41 +00:00
|
|
|
if n < 2: # it's a coin
|
2010-01-17 04:24:36 +00:00
|
|
|
if count < 5000:
|
2010-08-17 17:00:41 +00:00
|
|
|
return [random.randint(0, 1) for x in xrange(count)]
|
2010-03-01 02:32:41 +00:00
|
|
|
else: # fake it
|
2014-01-14 21:12:37 +00:00
|
|
|
return [int(random.normalvariate(.5 * count, (.75 * count) ** .5))]
|
2010-01-17 04:24:36 +00:00
|
|
|
else:
|
|
|
|
if count < 5000:
|
2010-08-17 17:00:41 +00:00
|
|
|
return [random.randint(1, n) for x in xrange(count)]
|
2010-03-01 02:32:41 +00:00
|
|
|
else: # fake it
|
2014-01-14 21:12:37 +00:00
|
|
|
return [int(random.normalvariate(.5 * (1 + n) * count,
|
|
|
|
(((n + 1) * (2 * n + 1) / 6. - (.5 * (1 + n)) ** 2) * count) ** .5))]
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
|
2010-02-24 16:39:00 +00:00
|
|
|
@hook.command('roll')
|
2010-08-24 22:14:55 +00:00
|
|
|
#@hook.regex(valid_diceroll, re.I)
|
2010-05-07 23:16:44 +00:00
|
|
|
@hook.command
|
2010-01-17 04:24:36 +00:00
|
|
|
def dice(inp):
|
|
|
|
".dice <diceroll> -- simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \
|
|
|
|
"D20s, subtract 1D5, add 4"
|
2010-08-30 03:35:27 +00:00
|
|
|
|
|
|
|
try: # if inp is a re.match object...
|
2010-08-16 09:32:28 +00:00
|
|
|
(inp, desc) = inp.groups()
|
2010-08-14 21:27:09 +00:00
|
|
|
except AttributeError:
|
2010-08-30 03:35:27 +00:00
|
|
|
(inp, desc) = valid_diceroll_re.match(inp).groups()
|
|
|
|
|
|
|
|
if "d" not in inp:
|
|
|
|
return
|
|
|
|
|
2010-01-17 04:24:36 +00:00
|
|
|
spec = whitespace_re.sub('', inp)
|
|
|
|
if not valid_diceroll_re.match(spec):
|
|
|
|
return "Invalid diceroll"
|
|
|
|
groups = sign_re.findall(spec)
|
2010-08-17 17:00:41 +00:00
|
|
|
|
2010-08-17 17:54:47 +00:00
|
|
|
total = 0
|
2010-08-17 17:00:41 +00:00
|
|
|
rolls = []
|
2010-08-17 17:54:47 +00:00
|
|
|
|
2010-01-17 04:24:36 +00:00
|
|
|
for roll in groups:
|
|
|
|
count, side = split_re.match(roll).groups()
|
2010-08-14 21:07:17 +00:00
|
|
|
count = int(count) if count not in " +-" else 1
|
2010-08-30 03:35:27 +00:00
|
|
|
if side.upper() == "F": # fudge dice are basically 1d3-2
|
2010-08-17 17:54:47 +00:00
|
|
|
for fudge in nrolls(count, "F"):
|
|
|
|
if fudge == 1:
|
|
|
|
rolls.append("\x033+\x0F")
|
|
|
|
elif fudge == -1:
|
|
|
|
rolls.append("\x034-\x0F")
|
|
|
|
else:
|
|
|
|
rolls.append("0")
|
|
|
|
total += fudge
|
2010-08-14 21:07:17 +00:00
|
|
|
elif side == "":
|
2010-08-17 17:54:47 +00:00
|
|
|
total += count
|
2010-01-17 04:24:36 +00:00
|
|
|
else:
|
|
|
|
side = int(side)
|
|
|
|
try:
|
|
|
|
if count > 0:
|
2010-08-17 17:54:47 +00:00
|
|
|
dice = nrolls(count, side)
|
|
|
|
rolls += map(str, dice)
|
|
|
|
total += sum(dice)
|
2010-01-17 04:24:36 +00:00
|
|
|
else:
|
2010-08-17 17:54:47 +00:00
|
|
|
dice = nrolls(-count, side)
|
|
|
|
rolls += [str(-x) for x in dice]
|
|
|
|
total -= sum(dice)
|
2010-01-17 04:24:36 +00:00
|
|
|
except OverflowError:
|
|
|
|
return "Thanks for overflowing a float, jerk >:["
|
|
|
|
|
2010-08-17 17:02:59 +00:00
|
|
|
if desc:
|
2010-08-17 17:54:47 +00:00
|
|
|
return "%s: %d (%s=%s)" % (desc.strip(), total, inp, ", ".join(rolls))
|
2010-08-17 17:02:59 +00:00
|
|
|
else:
|
2010-08-17 17:54:47 +00:00
|
|
|
return "%d (%s=%s)" % (total, inp, ", ".join(rolls))
|