buy.snippet.c
snippets · 5.9 KB · 2010-03-30
Multiple-object buy code for Merc 2.2 by Erwin Andreasen (20 Aug 95).
Download « Back to the snippets list
else /* object purchase code begins HERE */
/*
* 20 Aug 95 - added multiple object purchase
* Works perfectly with Merc2.2.
* By Erwin Andreasen, 4u2@aarhues.dk
* IF you use this code, *PLEASE* *SEND* *ME* *FEEDBACK*!!
*/
{
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 */
=============================================================================
/ ______ _______ ____ _____ ___ __ _ ______ ____ ____ _____ /
\ | ____|__ __| _ \ / ____\ / _ \| \ / | ____| / __ \| _ \ / ____\ \
/ | |__ | | | |_| | | | |_| | |\/| | |___ | | | | |_| | | /
/ | ___| | | | ___/| | __| _ | | | | ____| | | | | __/| | ___ \
\ | | | | | | | |___| | | | | | | | |____ | |__| | |\ \| |___| | /
/ |_| |_| |_| o \_____/|_| |_|_| |_|______|o \____/|_| \_|\_____/ \
\ /
============================================================================
------------------------------------------------------------------------------
ftp://ftp.game.org/pub/mud FTP.GAME.ORG http://www.game.org/ftpsite/
------------------------------------------------------------------------------
This archive came from FTP.GAME.ORG, the ultimate source for MUD resources.
------------------------------------------------------------------------------