Re: your mail

From: Rasmus R\xnlev (raro94ab@hp4.econ.cbs.dk)
Date: 02/22/96


On Wed, 21 Feb 1996, Ben Leibig wrote:

> I wrote the following 29 lines of code inplace of do_gold.  Basicly what 
> it does is spilts your coinage into 3 diffrent units and desplays it 
> that way.  What i want to do is make it so whever you see gold in units 
> it goes this way.  What this means is i have to change a LOT of code and 
> i dont want to have to put this in every place.  What i am woundering is 
> can i set this up so i can do somthing like,
> sprintf(buf, "That will be %s", GET_SPLITGOLD(amount)
> and have GET_SPLITGOLD take a number of normal coins and output a the 
> results of this in a string.  Any ideas?
> 
> P.s. Sorry bout the code but last time no one gave me a real anwser cuz i 
> dont think they understood what i was going.  ALSO i WOULD like to make 
> it so there are 10 copper in a silver, 20 silver in a gold.  Right now 
> its 10, 10..  Any ideas how i could modify the first 3 lines to acheve that?
> 
> 
> int  cgold=GET_GOLD(ch)/100; 
> int  csilver=(GET_GOLD(ch)%100)/10; 
> int  ccopper=GET_GOLD(ch)%10;
>   if (GET_GOLD(ch) == 0)
>     send_to_char("You're broke!\r\n", ch);
>   else if (GET_GOLD(ch) == 1)
>     send_to_char("You have one miserable copper penny", ch);
>   else {
>    sprintf(buf, "You have ");  
>    if (cgold > 1)
>      sprintf(buf, "%s%d gold coins", buf,cgold);
>    if (cgold == 1)
>      sprintf(buf, "%s1 gold coin", buf, cgold);
>    if (cgold > 0 && csilver > 0)
>      sprintf(buf, "%s, ", buf)
>    if (csilver > 1){
>      sprintf(buf, "%s%d silver pennies", csilver);
>    if (csilver == 1)
>      sprintf(buf, "%s1 silver pennie", buf);
>    if (cgold > 0 || csilver > 0)
>      sprintf(buf, "%s and ", buf)
>    if (ccopper == 0)
>      sprintf(buf, "%s.\r\n", buf)
>    if (ccopper >1)
>      sprintf(buf, "%s %d copper pennies. \r\n",buf, ccopper);
>    if (ccoper = 1)
>      sprintf(buf, "%s 1 copper penny.\r\n",buf)
>      send_to_char(buf,ch); 
> }
> 

Well, I allready read about the sugestion to make a function that returns 
a string with the ammount of gold/silver/copper.
This seems to be the best idea to me too, so here goes.
I would like to add that this is FREELY COMPOSED directly out of my head, 
no guaranties and no watanties here. Use at your own risk of floating 
your diskquota and drinking a couple of extra coke's.

char *convert_gold(int ammount)
{
  int gold = 0, silver = 0, copper = 0;
  char returnbuf[50];

  char returnbuf[0] = '\0'; /* Init char filed for security */

  /* check if this is a positive amount */

  if(amount < 0)
    return "ERROR: negative amount in convert_gold()";
  if(amount == 0)
    return "?!?!?! amount = 0";
  if(amount == 1)
    return "One miserable copper penny";

  /* I'm not sure those return's work, but should be easy to modify anyways*/

  /* 20 silver = 1 gold , 10 copper = 1 silver */

  /* Thus 1 gold = 20*10 copper (= 200) */

  #define	COP_PER_SILVER	10
  #define	SIL_PER_GOLD	20
  #define	SC		(10 * 20)

/* You could have SC be SIL_PER_GOLD * COP_PER_SILVER for better readbility*/

  gold 		= ammount/(SC);
  silver 	= (ammount%(SC))/COP_PER_SILVER;
  copper 	= ((amouunt%(SC))/COP_PER_SILVER)%COP_PER_SILVER;

  /* We've now converted the ammount to gold/silver/copper coins */

/*
Well, here you can put in the various if's and stuff, to build up the right
string ... remember to put all output in resultbuf, and then simply use
*/

return resultbuf;
}

Hope this can help you a little ... if not.. well .-p go see your doctor 
or something.

d.
--
***************************************************************************
*                            *    Visit my homepage:                      *
*   Rasmus Rønlev DOEK'94    *    http://www.econ.cbs.dk/people/raro94ab  *
*   Student instructor       *    Visit my Multi User Dungeon (MUD):      *
*                            *    ns3.ptd.net 5000 or 198.80.46.3 5000    *   
***************************************************************************
*                                                                         *
*      Student, B.Sc in Computer Science and Business Administration      *
*                                                                         *
***************************************************************************



This archive was generated by hypermail 2b30 : 12/07/00 PST