@tel #431=me
@edit #431
1 10000 del
i
(Header, 11 lines -- Dieroll.muf
Jordan Greywolf, 11 May 2001 - http://greywolf.critter.net
based on a program by Lynx
:
This is a die-roller program, used in the following example
format:
:
roll 1d6+2 <-- rolls 1 6-sided die, then adds 2
roll 3d6 <-- rolls 3 6-sided dice, adds results together
roll 2d4-1 <-- rolls 2 4-sided dice, subtracts 2
)
lvar qflag
lvar dietype
lvar bonus
lvar numdice
()
: tell ( s -- : sends message to user )
me @ swap notify
;
()
: tell-all ( s -- : sends message to room )
loc @ #-1 rot notify_except
;
()
: rollmessage ( i : shows result i of roll )
qflag @ not if
"
" me @ name " rolls " strcat strcat
else
"You roll "
then
numdice @ intostr "d" dietype @ intostr
strcat strcat strcat
bonus @ 0 > if
"+" bonus @ intostr strcat strcat
then
bonus @ 0 < if
"-" bonus @ -1 * intostr strcat strcat
then
", giving a result of " strcat
swap intostr strcat
qflag @ not if
" " strcat
tell-all
else
tell
then
;
()
: help
"-=-+-=-+-=-+-=-+-=-+-=- Dice Roller -=-+-=-+-=-+-=-+-=-+-=-"
tell
"- To roll dice for online role-play, indicate -"
tell
"- the number and type of dice in the following format: -"
tell
"- -"
tell
"- roll 3d6+2 <- roll 3 6-sided dice, and add 2 -"
tell
"- qroll 2d8 <- generates private result for GM -"
tell
"- -"
tell
"- The modifier at the end (e.g., '+2') is optional. -"
tell
"-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-"
tell
;
()
: die-roll ( -- i : returns random value from 1 to dietype )
random 1003 / ( division to avoid low-bit error )
dietype @ % ( divide by die type, take remainder )
1 +
;
()
: roll-dice ( -- i : returns result )
0 ( put total of 0 on stack )
1 ( put loop counter on stack )
begin
dup numdice @ > if
pop break
then
die-roll rot + swap
1 +
repeat
bonus @ + ( add bonus - or penalty - to end result )
;
()
: format-error ( generates proper usage format message )
"Use in the format of '" command @
" 3d8+2', for example to roll 3 8-sided dice and add 2."
strcat strcat tell
;
()
: parse-command ( s -- 0|1 : splits up input into local vars )
"" " " subst
dup "d" instring dup not if
"Error: no 'd' found to indicate die type." tell
pop pop 0 exit
then
dup 1 = if ( if no number of dice given, assume 1 )
pop 1 numdice !
else ( get number before 'd' and store )
1 - strcut swap atoi dup 1 < if
pop 1
then
dup 20 > if ( arbitrary maximum of 20 to limit looping )
"Error: number before 'd' must be from 1 to 20."
tell pop pop 0 exit
then
numdice !
then
1 strcut swap pop ( remove the 'd' )
0 bonus !
dup "+" instring dup if
1 - strcut 1 strcut swap pop ( split and remove '+' )
atoi bonus !
else
pop
then
dup "-" instring dup if
1 - strcut 1 strcut swap pop ( split and remove '-' )
atoi -1 * bonus !
else
pop
then
atoi dup 2 < if
"Error: die type must be d2 or greater." tell
pop 0 exit
then
dietype !
1
;
()
: main
command @ "qroll" stringcmp not if
1 qflag ! ( tells program this is in 'quiet' mode )
else
0 qflag !
then
dup not if ( if no parameters, print help)
pop help exit
then
dup 1 strcut pop "#" stringcmp not if
pop help exit ( if '#' command is found, print help )
then
dup 4 strcut pop "help" stringcmp not if
pop help exit ( if 'help' found, print help )
then
parse-command not if
format-error exit ( show 'proper usage' message if error )
then
roll-dice ( actually roll the dice )
rollmessage ( then show message to room or user )
;
.
compile
quit