This is my very first snippet, so please don't be mad at me if it's odd and scary. Anyway. It's a dice roll function for Socketmud, ported from a function I wrote for ROM. It's a little different from the other dice-roll functions out there because you can pick the output color if you want to. (ie: R is bright red, r is normal red, G is bright green, etc.)
I use it for mostly RP purposes (D&D style), but it could be used for other things too, such as gambling or games, or whatever you find to do with it. I'm pretty sure I remembered everything (hope!). Thank for looking, if nothing else.
Please note that this snippet requires the "Randomize Functions" snippet located here (http://www.socketmud.dk/snippets.php) to be installed.
If you use this, please leave the comments in tact and send an email to faith_18_2000@hotmail.com to let me know you're using it, bug reports, suggestions, etc. Thank you.
STEP 1: Add the following line to the top of action_safe.c, with the other includes.
#include <stdlib.h>
STEP 2: Insert the following code to the bottom(or wherever) of action_safe.c.
/* Written and ported from ROM by Samira (faith_18_2000@hotmail.com) */
/* If used and/or ported to other codebases, please leave the comments */
/* in tact and send me an email at the above email address */
void cmd_roll(D_MOBILE *dMob, char *arg)
{
char arg1[MAX_BUFFER];
char buf[MAX_OUTPUT];
char color;
int result;
int sides;
/* Syntax: roll <sides of dice> [optional color code] */
/* Example: roll 20 R (this will print in bright red) */
arg = one_arg(arg, arg1);
if (arg1[0] == '\0')
{
text_to_mobile(dMob, "How many sides?\n\r");
return;
}
switch (arg[0])
{
default: color = 'n'; break;
case('R'): color = 'R'; break;
case('G'): color = 'G'; break;
case('Y'): color = 'Y'; break;
case('B'): color = 'B'; break;
case('M'): color = 'M'; break;
case('C'): color = 'C'; break;
case('W'): color = 'W'; break;
case('D'): color = 'D'; break;
case('r'): color = 'r'; break;
case('g'): color = 'g'; break;
case('y'): color = 'y'; break;
case('b'): color = 'b'; break;
case('m'): color = 'm'; break;
case('c'): color = 'c'; break;
case('w'): color = 'w'; break;
}
sides = atoi(arg1);
if ((sides <= 1) || (sides > 256)) /* 256 seemed like a good number to stop at at the time */
{
text_to_mobile(dMob, "The number of sides must be between 2 and 256.\n\r");
return;
}
result = random_range(1, sides);
if (sides == 2) /* Rolling 2 sided dice plays like a coin flip */
{
switch(result)
{
case(1): sprintf(buf, "#%cYou flip a coin. It reveals Heads!#n\n\r", color);
text_to_mobile(dMob, buf);
sprintf(buf, "#%c%s flips a coin. The coin reveals Heads!#n", color, dMob->name);
communicate(dMob, buf, COMM_ROOM);
break;
case(2): sprintf(buf, "#%cYou flip a coin! It reveals Tails!#n\n\r", color);
text_to_mobile(dMob, buf);
sprintf(buf, "#%c%s flips a coin. The coin reveals Tails!#n", color, dMob->name);
communicate(dMob, buf, COMM_ROOM);
break;
default: bug( "Do_Roll: bug in coin flip", 0 );
break;
}
}
else if( sides > 2 && sides <= 256 )
{
sprintf(buf,"#%cYou have rolled a %d on a %d-sided dice.#n\n\r", color, result, sides);
text_to_mobile(dMob, buf);
sprintf(buf,"#%c%s rolls a %d on a %d-sided dice.#n", color, dMob->name, result, sides);
communicate(dMob, buf, COMM_ROOM);
}
return;
}
STEP 3: Add the following line to interpret.c in the command table (where it should be alphabetically).
{ "roll", cmd_roll, LEVEL_GUEST },
STEP 4: Add the following to mud.h. Put it right underneath COMM_LOCAL.
#define COMM_ROOM 1 /* more like room-echo than 'say' */
STEP 5: Add the following to mud.h. Put it in the "Prototype function declerations", with the other "action_safe.c" declarations.
void cmd_roll ( D_M *dMob, char *arg );
STEP 6: Add the following to utils.c in the "void communicate" function. Put it at the bottom of the switch statment.
case COMM_ROOM: /* effectively a local echo - Samira */
sprintf(message, "%s\n\r", txt);
pIter = AllocIterator(dmobile_list);
while ((xMob = (D_MOBILE *) NextInList(pIter)) != NULL)
{
if (xMob == dMob) continue;
text_to_mobile(xMob, message);
}
FreeIterator(pIter);
break;