/*          Dice Game Snippet for the EmberMUD Codebase by Rahl            */
/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Merc Diku Mud, you must comply with   *
 *  both the original Diku license in 'license.doc' as well the Merc       *
 *  license in 'license.txt'.  In particular, you may not remove either of *
 *  these copyright notices.                                               *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 ***************************************************************************/

/***************************************************************************
 * A few really simple dice games. If you want to change the odds, simply  *
 * change the dice() numbers.                                              *
 *                                                                         *
 * To install: add dice_games.o to your makefile, add do_games to interp.c *
 * and interp.h, and add spec_gamemaster to special.c                      *
 *                                                                         *
 * If you choose to use this code, please retain my name in this file and  *
 * send me an email (dwa1844@rit.edu) saying you are using it. Suggestions *
 * for improvement are welcome.   -- Rahl (Daniel Anderson)                *
 ***************************************************************************/

#if defined(macintosh)
#include <types.h>
#include <time.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "merc.h"

void win( CHAR_DATA *ch, long amnt );
void lose( CHAR_DATA *ch, long amnt );

void win( CHAR_DATA *ch, long amnt )
{
    char buf[MAX_STRING_LENGTH];

    buf[0] = '\0';

    ch->gold += amnt;
    sprintf( buf, "You win %ld gold!\n\r", amnt );
    send_to_char( buf, ch );
}

void lose( CHAR_DATA *ch, long amnt )
{
    char buf[MAX_STRING_LENGTH];

    buf[0] = '\0';

    ch->gold -= amnt;
    sprintf( buf, "You lost %ld gold!\n\r", amnt );
    send_to_char( buf, ch );
}

void game_even_odd( CHAR_DATA *ch, char *argument )
{
    char arg[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    char buf[MAX_STRING_LENGTH];
    long amount;
    int roll;
    CHAR_DATA *gamemaster;

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

    buf[0] = '\0';

    if ( arg[0] == '\0' || arg2[0] == '\0' )
    {
        send_to_char( "Syntax: game even-odd <even|odd> <amount>\n\r", ch );
        return;
    }

    for ( gamemaster = ch->in_room->people; gamemaster != NULL; gamemaster = gamemaster->next_in_room )
    {
        if ( !IS_NPC( gamemaster ) )
            continue;
        if ( gamemaster->spec_fun == spec_lookup( "spec_gamemaster" ) )
            break;
    }

    if ( gamemaster == NULL || gamemaster->spec_fun != spec_lookup( "spec_gamemaster" ) )
    {
        send_to_char("You can't do that here.\n\r",ch);
        return;
    }

    if ( gamemaster->fighting != NULL)
    {
        send_to_char("Wait until the fighting stops.\n\r",ch);
        return;
    }

    if ( !is_number( arg2 ) )
    {
        send_to_char( "You must bet a number.\n\r", ch );
        return;
    }

    roll = dice( 2, 6 );

    amount = atol( arg2 );

    if ( amount < 1 )
    {
        send_to_char( "Bet SOMETHING, will you?\n\r", ch );
        return;
    }

    sprintf( buf, "%s rolls the dice.\n\rThe roll is %d.\n\r", gamemaster->short_descr, roll );
    send_to_char( buf, ch );
    
    if ( !str_cmp( arg, "odd" ) )
    {
        if ( roll %2 != 0 )     /* you win! */
            win( ch, amount );
        else
            lose( ch, amount );
        return;
    }
    else if ( !str_cmp( arg, "even" ) )
    {
        if ( roll %2 == 0 )
            win( ch, amount );
        else
            lose( ch, amount );
        return;
    }
    else
    {
        send_to_char( "Syntax: game even-odd <even|odd> <amount>\n\r", ch );
    }
    return;
}


void game_high_low( CHAR_DATA *ch, char *argument )
{
    char arg[MAX_INPUT_LENGTH];
    char arg2[MAX_INPUT_LENGTH];
    char buf[MAX_STRING_LENGTH];
    long amount;
    int roll;
    CHAR_DATA *gamemaster;

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

    buf[0] = '\0';

    if ( arg[0] == '\0' || arg2[0] == '\0' )
    {
        send_to_char( "Syntax: game high-low <high|low> <amount>\n\r", ch );
        return;
    }

    for ( gamemaster = ch->in_room->people; gamemaster != NULL; gamemaster = gamemaster->next_in_room )
    {
        if ( !IS_NPC( gamemaster ) )
            continue;
        if ( gamemaster->spec_fun == spec_lookup( "spec_gamemaster" ) )
            break;
    }

    if ( gamemaster == NULL || gamemaster->spec_fun != spec_lookup( "spec_gamemaster" ) )
    {
        send_to_char("You can't do that here.\n\r",ch);
        return;
    }

    if ( gamemaster->fighting != NULL)
    {
        send_to_char("Wait until the fighting stops.\n\r",ch);
        return;
    }

    if ( !is_number( arg2 ) )
    {
        send_to_char( "You must bet a number.\n\r", ch );
        return;
    }

    roll = dice( 1, 9 );

    amount = atol( arg2 );

    if ( amount < 1 )
    {
        send_to_char( "Bet SOMETHING, will you?\n\r", ch );
        return;
    }

    sprintf( buf, "%s rolls the dice.\n\rThe roll is %d.\n\r", gamemaster->short_descr, roll );
    send_to_char( buf, ch );
    
    if ( !str_cmp( arg, "high" ) )
    {
        if ( roll > 6 )     /* you win! */
            win( ch, amount * 1.5 );
        else
            lose( ch, amount );
        return;
    }
    else if ( !str_cmp( arg, "low" ) )
    {
        if ( roll < 6 )
            win( ch, amount * 1.5 );
        else
            lose( ch, amount );
        return;
    }
    else
    {
        send_to_char( "Syntax: game high-low <high|low> <amount>\n\r", ch );
    }
    return;
}


void game_higher_lower( CHAR_DATA *ch, char *argument )
{
    char arg[MAX_INPUT_LENGTH];
    char buf[MAX_STRING_LENGTH];
    long amount;
    int your_roll, his_roll;
    CHAR_DATA *gamemaster;

    argument = one_argument( argument, arg );

    buf[0] = '\0';

    if ( arg[0] == '\0' )
    {
        send_to_char( "Syntax: game higher-lower <amount>\n\r", ch );
        return;
    }

    for ( gamemaster = ch->in_room->people; gamemaster != NULL; gamemaster = gamemaster->next_in_room )
    {
        if ( !IS_NPC( gamemaster ) )
            continue;
        if ( gamemaster->spec_fun == spec_lookup( "spec_gamemaster" ) )
            break;
    }

    if ( gamemaster == NULL || gamemaster->spec_fun != spec_lookup( "spec_gamemaster" ) )
    {
        send_to_char("You can't do that here.\n\r",ch);
        return;
    }

    if ( gamemaster->fighting != NULL)
    {
        send_to_char("Wait until the fighting stops.\n\r",ch);
        return;
    }

    if ( !is_number( arg ) )
    {
        send_to_char( "You must bet a number.\n\r", ch );
        return;
    }

    your_roll = dice( 2, 6 );
    his_roll = dice( 2, 6 );

    amount = atol( arg );

    if ( amount < 1 )
    {
        send_to_char( "Bet SOMETHING, will you?\n\r", ch );
        return;
    }

    sprintf( buf, "%s rolls the dice and gets a %d.\n\rYour roll is %d.\n\r",                                gamemaster->short_descr, his_roll, your_roll );
    send_to_char( buf, ch );
    
    if ( your_roll > his_roll )     /* you win! */
        win( ch, amount * 2 );
    else
        lose( ch, amount );
    return;
}

void do_game( CHAR_DATA *ch, char *argument )
{
    char arg[MAX_INPUT_LENGTH];
    
    argument = one_argument( argument, arg );

    if ( !str_cmp( arg, "higher-lower" ) )
        game_higher_lower( ch, argument );
    else if ( !str_cmp( arg, "even-odd" ) )
        game_even_odd( ch, argument );
    else if ( !str_cmp( arg, "high-low" ) )
        game_high_low( ch, argument );
    else
    {
        send_to_char( "Current games are: higher-lower, even-odd, and high-low.\n\r", ch );
        return;
    }
}


/*
 =============================================================================
/   ______ _______ ____   _____   ___ __    _ ______    ____  ____   _____   /
\  |  ____|__   __|  _ \ / ____\ / _ \| \  / |  ____|  / __ \|  _ \ / ____\  \
/  | |__     | |  | |_| | |     | |_| | |\/| | |___   | |  | | |_| | |       /
/  | ___|    | |  | ___/| |   __|  _  | |  | | ____|  | |  | |  __/| |   ___ \
\  | |       | |  | |   | |___| | | | | |  | | |____  | |__| | |\ \| |___| | /
/  |_|       |_|  |_|  o \_____/|_| |_|_|  |_|______|o \____/|_| \_|\_____/  \
\                                                                            /
 ============================================================================

------------------------------------------------------------------------------
ftp://ftp.game.org/pub/mud      FTP.GAME.ORG      http://www.game.org/ftpsite/
------------------------------------------------------------------------------

  This file came from FTP.GAME.ORG, the ultimate source for MUD resources.

------------------------------------------------------------------------------

*/