/*

===========================================================================
This snippet was written by Erwin S. Andreasen, erwin@pip.dknet.dk. You may 
use this code freely, as long as you retain my name in all of the files. You
also have to mail me telling that you are using it. I am giving this,
hopefully useful, piece of source code to you for free, and all I require
from you is some feedback.

Please mail me if you find any bugs or have any new ideas or just comments.

All my snippets are publically available at:

http://pip.dknet.dk/~pip1773/

If you do not have WWW access, try ftp'ing to pip.dknet.dk and examine
the /pub/pip1773 directory.
===========================================================================


Multiple object buy routine
---------------------------

Last update:  Aug 20, 1995

Should work on : MERC2.2 ROM2.3

Fixed since last update:
 None

Know bugs and limitations yet to be fixed:
 None?

Comments:

 This change to do_buy allows the player to type e.g. buy 10 bread and get all
 the 10 pieces of bread at once.

 The code does check if the item is an item sold to the shopkeeper he only has
 one of.

For ROM 2.3, you have to uncomment a single line near the beginning.


*/

/* Insert this in the lower part of the do_buy routine, after the pet code */

    else /* object purchase code begins HERE */
    {

	/* char arg[MAX_INPUT_LENGTH]; */ /* Uncomment for ROM 2.3 */

        CHAR_DATA *keeper;
        OBJ_DATA *obj;
        int cost;
        char arg2[MAX_INPUT_LENGTH]; /* 2nd argument */
        int item_count = 1;          /* default: buy only 1 item */

    argument = one_argument (argument, arg2); /* get another argument, if any */

    if (arg2[0]) /* more than one argument specified? then arg2[0] <> 0 */
    {
        /* check if first of the 2 args really IS a number */

        if (!is_number(arg))
        {
            send_to_char ("Syntax for BUY is: BUY [number] <item>\n\r\"number\" is an optional number of items to buy.\n\r",ch);
            return;
        }

        item_count = atoi (arg); /* first argument is the optional count */
        strcpy (arg,arg2);       /* copy the item name to its right spot */
    }

    if ( ( keeper = find_keeper( ch ) ) == NULL ) /* is there a shopkeeper here? */
            return;

/* find the pointer to the object */
        obj  = get_obj_carry( keeper, arg );
        cost = get_cost( keeper, obj, TRUE );

    if ( cost <= 0 || !can_see_obj( ch, obj ) ) /* cant buy what you cant see */
        {
            act( "$n tells you 'I don't sell that -- try 'list''.",keeper, NULL, ch, TO_VICT );
            ch->reply = keeper;
            return;
        }

/* check for valid positive numeric value entered */
    if (!(item_count > 0))
    {
            send_to_char ("Buy how many? Number must be positive!\n\r",ch);
            return;
    }

/* can the character afford it ? */
    if ( ch->gold < (cost * item_count) )
    {
        if (item_count == 1) /* he only wanted to buy one */
        {
            act( "$n tells you 'You can't afford to buy $p'.",keeper, obj, ch, TO_VICT );
        }
        else
        {
            char buf[MAX_STRING_LENGTH]; /* temp buffer */
            if ( (ch->gold / cost) > 0) /* how many CAN he afford? */
                sprintf (buf, "$n tells you 'You can only afford %d of those!", (ch->gold / cost));
            else /* not even a single one! what a bum! */
                sprintf (buf, "$n tells you '%s? You must be kidding - you can't even afford a single one, let alone %d!'",capitalize(obj->short_descr), item_count);

            act(buf,keeper, obj, ch, TO_VICT );
            ch->reply = keeper; /* like the character really would reply to the shopkeeper... */
            return;
        }

        ch->reply = keeper; /* like the character really would reply to the shopkeeper... */
        return;
    }

/* Can the character use the item at all ? */
    if ( obj->level > ch->level )
    {
        act( "$n tells you 'You can't use $p yet'.",
            keeper, obj, ch, TO_VICT );
        ch->reply = keeper;
        return;
    }
/* can the character carry more items? */
    if ( ch->carry_number + (get_obj_number(obj)*item_count) > can_carry_n( ch ) )
        {
            send_to_char( "You can't carry that many items.\n\r", ch );
            return;
        }

/* can the character carry more weight? */
    if ( ch->carry_weight + item_count*get_obj_weight(obj) > can_carry_w( ch ) )
        {
            send_to_char( "You can't carry that much weight.\n\r", ch );
            return;
        }

/* check for objects sold to the keeper */
    if ( (item_count > 1) && !IS_SET (obj->extra_flags,ITEM_INVENTORY))
    {
        act( "$n tells you 'Sorry - $p is something I have only one of'.",keeper, obj, ch, TO_VICT );
        ch->reply = keeper;
        return;
    }

/* change this to reflect multiple items bought */
    if (item_count == 1)
    {
        act( "$n buys $p.", ch, obj, NULL, TO_ROOM );
        act( "You buy $p.", ch, obj, NULL, TO_CHAR );
    }
    else /* inform of multiple item purchase */
    {
        char buf[MAX_STRING_LENGTH]; /* temporary buffer */

/* "buys 5 * a piece of bread" seems to be the easiest and least gramatically incorerct solution. */
        sprintf (buf, "$n buys %d * $p.", item_count);
        act (buf, ch, obj, NULL, TO_ROOM); /* to char self */
        sprintf (buf, "You buy %d * $p.", item_count);
        act(buf, ch, obj, NULL, TO_CHAR ); /* to others */
    }

    ch->gold     -= cost*item_count;
    keeper->gold += cost*item_count;

    if ( IS_SET( obj->extra_flags, ITEM_INVENTORY ) ) /* 'permanent' item */
    {
        /* item_count of items */
        for ( ; item_count > 0; item_count--) /* create item_count objects */
        {
            obj = create_object( obj->pIndexData, obj->level );
            obj_to_char( obj, ch );
        }
    }
    else /* single item */
    {
        obj_from_char( obj );
        obj_to_char( obj, ch );
    }
    return;
    } /* else */
} /* do_buy */