4 # hacked up by Tim Riker <Tim@Rikers.org> from Games::Dice
11 sub dice::roll_array ($) {
15 return @throws unless $line =~ m{
17 (\d+)? # optional count in $1
19 ( # type of dice in $2:
20 \d+ # either one or more digits
22 % # a percent sign for d% = d100
24 }x; # whitespace allowed
29 return @throws if $num > 100;
30 $type = 100 if $type eq '%';
31 return @throws if $type < 2;
34 push @throws, int (rand $type) + 1;
45 return '' unless $line =~ m{
48 (?:\d+)? # optional count
51 \d+ # either one or more digits
53 % # a percent sign for d% = d100
56 (?: # grouping-only parens
57 ([-+xX*/bB]) # a + - * / b(est) in $2
58 (\d+) # an offset in $3
59 )? # both of those last are optional
60 }x; # whitespace allowed in re
62 my($dice_string) = $1;
64 my($offset) = $3 || 0;
68 my(@throws) = roll_array( $dice_string );
69 return '' unless @throws > 0;
70 my($retval) = "rolled " . join(',', @throws);
74 $offset = 0 if $offset < 0;
75 $offset = @throws if $offset > @throws;
77 @throws = sort { $b <=> $a } @throws; # sort numerically, descending
78 @result = @throws[ 0 .. $offset-1 ]; # pick off the $offset first ones
79 $retval .= " best $offset";
82 $retval .= " $sign $offset" if $sign;
86 $sum += $_ foreach @result;
87 $sum += $offset if $sign eq '+';
88 $sum -= $offset if $sign eq '-';
89 $sum *= $offset if ($sign eq '*' || $sign eq 'x');
90 do { $sum /= $offset; $sum = int $sum; } if $sign eq '/';
92 return "$retval = $sum";
97 srand(); # fork seems to not change rand. force it here
98 my $retval = roll($message);
100 &::performStrictReply($retval);
103 #print "(q)uit or die combination, ex. 4d10/4\n";
104 #while (my $dice = <STDIN>) {
106 # if (! $dice || $dice =~ m/^q(?:uit)*$/i) {
110 # print roll($dice) . "\n";
122 dice.pl - simulate die rolls
130 The number and type of dice to roll is given in a style which should be
131 familiar to players of popular role-playing games: I<a>dI<b>[+-*/b]I<c>.
132 I<a> is optional and defaults to 1; it gives the number of dice to roll.
133 I<b> indicates the number of sides to each die; the most common,
134 cube-shaped die is thus a d6. % can be used instead of 100 for I<b>;
135 hence, rolling 2d% and 2d100 is equivalent. C<roll> simulates I<a> rolls
136 of I<b>-sided dice and adds together the results. The optional end,
137 consisting of one of +-*/b and a number I<c>, can modify the sum of the
138 individual dice. +-*/ are similar in that they take the sum of the rolls
139 and add or subtract I<c>, or multiply or divide the sum by I<c>. (x can
140 also be used instead of *.) Hence, 1d6+2 gives a number in the range
141 3..8, and 2d4*10 gives a number in the range 20..80. (Using / truncates
142 the result to an int after dividing.) Using b in this slot is a little
143 different: it's short for "best" and indicates "roll a number of dice,
144 but add together only the best few". For example, 5d6b3 rolls five six-
145 sided dice and adds together the three best rolls. This is sometimes
146 used, for example, in roll-playing to give higher averages.
150 Philip Newton, <pne@cpan.org>
152 Tim Riker <Tim@Rikers.org>
156 Copyright (C) 1999, 2002 Philip Newton - All rights reserved.
158 Copyright (C) 2005 Tim Riker - All rights reserved.
160 Redistribution and use in source and binary forms, with or without
161 modification, are permitted provided that the following conditions
168 Redistributions of source code must retain the above copyright notice,
169 this list of conditions and the following disclaimer.
173 Redistributions in binary form must reproduce the above copyright notice,
174 this list of conditions and the following disclaimer in the
175 documentation and/or other materials provided with the distribution.
179 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
180 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
181 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
182 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
183 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
184 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
185 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
186 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
187 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
188 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
189 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
190 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
191 POSSIBILITY OF SUCH DAMAGE.