/***************************************************************************
 * This code may be used freely within any non-commercial MUD, all I ask   *
 * is that these comments remain in tact and that you give me any feedback *
 * or bug reports you come up with.                                        *
 *                                  -- Midboss (eclipsing.souls@gmail.com) *
 ***************************************************************************/
/***************************************************************************
 * Add this file to your source directory, and if applicable, makefile.    *
 * Add the DECLARE_DO_FUN() and cmd_table entry for do_dice, and compile.  *
 *                  This one was a suggestion from Samira.                 *
 ***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "merc.h"

/*
 * This is just a hack of one of the *_argument functions in interp.c,
 * modified to handle dice.
 */
int dice_argument (char *argument, char *arg)
{
    char *pdot;
    int number;

    for (pdot = argument; *pdot != '\0'; pdot++)
    {
        if (*pdot == 'd')
        {
            *pdot = '\0';
            number = atoi (argument);
            *pdot = 'd';
            strcpy (arg, pdot + 1);
            return number;
        }
    }

    strcpy (arg, argument);
    return -1;
}

/*
 * Simple dice rolling command that lets you specify number of die and sides.
 */
void do_dice (CHAR_DATA * ch, char * argument)
{
	char arg[MIL], buf[MSL];
	unsigned int result = 0;
	sh_int num = 0, size = 0;

	if (argument[0] == '\0')
	{
		send_to_char ("Roll how many dice with how many sides?  [roll <num>d<size>]\n\r", ch);
		return;
	}

	if ((num = dice_argument (argument, arg)) < 0)
	{
		send_to_char ("Roll HOW many?\n\r", ch);
		return;
	}

	if (num > 255)
	{
		send_to_char ("You may only roll as many as 255 dice.\n\r", ch);
		return;
	}

	if (!is_number (arg) || arg[0] == '\0')
	{
		send_to_char ("How many sides?\n\r", ch);
		return;
	}

	size = atoi (arg);

	if (size > 255)
	{
		send_to_char ("You can only find up to 255 sided dice...  And even that's a little absurd.\n\r", ch);
		return;
	}

	result = dice (num, size);

	sprintf (buf, "You roll %d on %d %d-sided dice.\n\r", result, num, size);
	send_to_char (buf, ch);
	sprintf (buf, "$n rolls %d on %d %d-sided dice.\n\r", result, num, size);
	act (buf, ch, NULL, NULL, TO_ROOM);
}