/*
 *  Programmer: Cameron Taylor, a.k.a. Shanyr
 *  Date: 3 May 2005
 *  Notes:  Put do_dice in where you think it fits; don't forget the DO_FUN
 *         entry; you may need to add an #include or two for is_number() and
 *         atoi().
 *          'dice' will roll two six-sided dice.  'dice' followed by a number
 *         will restrict the number to 1-10 and roll that number of six-sided
 *         dice.  'dice' followed by two numbers will restrict the first number
 *         to 1-10 and the second number to 2-20 and roll (first number) dice
 *         of (second number) sides.  The values of all dice rolled are listed,
 *         and the total.
 *          If you have a 'snippets' help file, I would appreciate an entry
 *         similar to:  Dice code by Cameron Taylor.
 */

/*
 *  This function allows a character to roll 1 or more dice.
 *  Programmer: Cameron Taylor
 */
void do_dice( CHAR_DATA *ch, char *argument )
{
    const int MAX_DICE = 10;
    const int MAX_SIDES = 20;
    int number=2, sides=6;
    int total = 0;
    int dice[10];
    char buf1[MAX_STRING_LENGTH];
    char buf2[MAX_STRING_LENGTH];
    char arg1 [MAX_INPUT_LENGTH];
    char arg2 [MAX_INPUT_LENGTH];
    int i;

    argument = one_argument( argument, arg1 );
    argument = one_argument( argument, arg2 );

    if(STR_HASDATA(arg1))
    {
        if(is_number(arg1))
            number = atoi(arg1);
        number = number > MAX_DICE ? MAX_DICE : (number < 1 ? 1 : number);

        if(STR_HASDATA(arg2))
        {
            if(is_number(arg2))
                sides = atoi(arg2);
            sides = sides > MAX_SIDES ? MAX_SIDES : (sides < 2 ? 2 : sides);
        }
    }

    for(i=0; i<number; i++)
        total += (dice[i]=number_range(1, sides));

    if(number == 1)
        sprintf(buf1, "%s up a %d-sided die and %s %d.", "%s", sides, "%s", dice[0]);
    else
    {
        sprintf(buf1, "%s up %d %d-sided dice and %s %d%s", "%s", number, sides, "%s", dice[0], "%s");
        for(i=1; i<number-1; i++)
        {
            sprintf(buf2, buf1, "%s", "%s", ", %d%s");
            sprintf(buf1, buf2, "%s", "%s", dice[i], "%s");
        }
        sprintf(buf2, buf1, "%s", "%s", " and %d, for a total of %d.");
        sprintf(buf1, buf2, "%s", "%s", dice[number-1], total);
    }

    sprintf(buf2, buf1, "You pick", "roll");
    act(buf2, ch, NULL, NULL, TO_CHAR);
    sprintf(buf2, buf1, "$n picks", "rolls");
    act(buf2, ch, NULL, NULL, TO_ROOM);
}