Display hp as a percent in score

Posted by SirRoughknight on Mon 11 Jan 2010 06:27 PM — 7 posts, 28,826 views.

#0
I'm using SmaugFUSS 1.9

Was wondering, I added in a new token from the prompts to display them as a "life" bar for the character pretty much from observing the C/c tokens.


            case 'J':
               if (ch->max_hit > 0)
                     percent =  (100 * ch->hit ) / ch->max_hit;
               else
                     percent = -1;
               if (percent >= 100)
			  sprintf (pbuf, "&wLife[&R|||&Y||||&G|||&w]" );
            else if (percent >= 90)
              sprintf (pbuf, "&wLife[&R|||&Y||||&G||-&w]" );
            else if (percent >= 80)
              sprintf (pbuf, "&wLife[&R|||&Y||||&G|--&w]" );
            else if (percent >= 70)
              sprintf (pbuf, "&wLife[&R|||&Y||||&G---&w]" );
            else if (percent >= 60)
              sprintf (pbuf, "&wLife[&R|||&Y|||-&G---&w]" );
            else if (percent >= 50)
              sprintf (pbuf, "&wLife[&R|||&Y||--&G---&w]" );
            else if (percent >= 40)
              sprintf (pbuf, "&wLife[&R|||&Y|---&G---&w]" );
            else if (percent >= 30)
              sprintf (pbuf, "&wLife[&R|||&Y----&G---&w]" );
            else if (percent >= 20)
              sprintf (pbuf, "&wLife[&R||-&Y----&G---&w]" );
            else if (percent >= 10)
              sprintf (pbuf, "&wLife[&R|--&Y----&G---&w]" );
			else
              sprintf (pbuf, "&wLife[&R-&r-&R-&r-&R-&r-&R-&r-&R-&r-&w]" );
            break;


The next step I wanted to take was displaying the character's hitpoints as "Life: ???% of 100%", in the score command but I must admit I'm a little lost.

Any help would be greatly appreciated in advance.
Amended on Mon 11 Jan 2010 06:39 PM by SirRoughknight
USA #1
Just take that formula you used for the int percent you have there and put it in score.
#2
I changed the following:

void do_score( CHAR_DATA* ch, const char* argument )
{
   char buf[MAX_STRING_LENGTH];
   char buf2[MAX_STRING_LENGTH];
   char buf3[MAX_STRING_LENGTH];
   char buf4[MAX_STRING_LENGTH];
   AFFECT_DATA *paf;
   int iLang;
   int percent;//added int percent;



And added in:

if (ch->max_hit > 0)
    percent =  (100 * ch->hit ) / ch->max_hit;
else
    percent = -1;
    snprintf( buf, "%3.3d%", percent ); //line 265

 pager_printf(ch, "Life: %s  Gold: %-13s     Save: %s\r",
	      buf, num_punct( ch->gold ),
	      ch->save_time ? ctime( &( ch->save_time ) ) : "None this session.\n" );


Compiled with the following errors:

Compiling o/player.o....
cc1plus: warnings being treated as errors
player.c: In function 'void do_score(CHAR_DATA*, const char*)':
player.c:265: warning: ' ' flag used with '%o' printf format
player.c:265: warning: too few arguments for format
make[1]: *** [o/player.o] Error 1
make: *** [all] Error 2

Amended on Tue 12 Jan 2010 12:53 AM by SirRoughknight
USA #3
SirRoughknight said:

player.c: In function 'void do_score(CHAR_DATA*, const char*)':
player.c:265: warning: ' ' flag used with '%o' printf format
player.c:265: warning: too few arguments for format


Those warnings brought me to look at printf-like calls. It sounded like you have a blank format wildcard, because it's expecting more parameters than you passed. Sure enough:

snprintf( buf, "%3.3d%", percent );


Change that to:

snprintf( buf, "%3.3d%%", percent );


A %% in printf() outputs one %.
#4
Changed it to %3.3d%%

Did a make clean, tried to compile again.
Got the same errors as before.
Australia Forum Administrator #5
See "man snprintf":


SYNOPSIS
       #include <stdio.h>

       int printf(const char *format, ...);
       int fprintf(FILE *stream, const char *format, ...);
       int sprintf(char *str, const char *format, ...);
       int snprintf(char *str, size_t size, const char *format, ...);


The second argument should be a size.
#6

   if (ch->max_hit > 0)
    percent =  (100 * ch->hit ) / ch->max_hit;
   else
    percent = -1;
    snprintf( buf, MAX_STRING_LENGTH, "%-3d", percent );

    pager_printf(ch, "Life:   %s of   100   Gold: %-13s     Save: %s\r",
		 buf, num_punct( ch->gold ),
		 ch->save_time ? ctime( &( ch->save_time ) ) : "None this session.\n" );


Displays perfectly how I wanted it.

Thank you very much Twisol, Zeno, and Nick. Ya'll are Aces.