Health messages rather than HP

Posted by Valna on Thu 25 Mar 2004 04:47 AM — 7 posts, 25,248 views.

#0
ok i'm workin on a swr 1.0 downloaded from this site. Going for something slightly more realistic than the stock code. anyhow i'm putting in a health command and taking HP out of the score command. this is the code that i wrote for health:


void do_health(CHAR_DATA * ch, char *argument)
{
   char *hpt;
   int hitpct;
   int chmax = ch->max_hit;
   int chhit = ch->hit;

   hitpct = ((chmax / chhit) * 100);

   if (hitpct > 100 && hitpct != 100) { hpt = "envigorated";    }
   else if ((hitpct = 100) || hitpct >= 95) { hpt = "healthy";  }
   else if (hitpct < 95 && hitpct >= 90) { hpt = "scuffed";     }
   else if (hitpct < 90 && hitpct >= 80) { hpt = "bruised";     }
   else if (hitpct < 80 && hitpct >= 70) { hpt = "scratched";   }
   else if (hitpct < 70 && hitpct >= 60) { hpt = "injured";     }
   else if (hitpct < 60 && hitpct >= 50) { hpt = "beat up";     }
   else if (hitpct < 50 && hitpct >= 40) { hpt = "wounded";     }
   else if (hitpct < 40 && hitpct >= 30) { hpt = "mauled";      }
   else if (hitpct < 30 && hitpct >= 20) { hpt = "maimed";      }
   else if (hitpct < 20 && hitpct >= 10) { hpt = "in bad shape";        }
   else if (hitpct < 10 && hitpct >= 0) { hpt = "at death's door";      }
   else { hpt = "DEAD"; }

   ch_printf(ch, "You currently feel %s\n\r", hpt);
   return;
}


anyway i'm sure it's obvious to someone out there what i'm doing wrong, the command will function but it always returns a "healthy" result even when badly wounded. Any ideas?
Australia Forum Administrator #1
Well, for a start this line is rather bizarre:

if (hitpct > 100 && hitpct != 100) ...

You are saying that if hitpct is greater than 100 *and* is not 100. Surely if it is greater than 100, it will not *be* 100.

Anyway, I think you have the percentage the wrong way around. Let's say you have 40 1-cent coins in your pocket, the percentage of $1 is 40/100 not 100/40.

So I would start by changing this line:

hitpct = ((chmax / chhit) * 100);

to

hitpct = (chhit * 100) / chmax;

I would do the multiply before the divide, because otherwise you are likely to get integer arithmetic which would be:

40 hp / 100 hp = 0
Multiply by 100 still gives 0.

Amended on Thu 25 Mar 2004 05:52 AM by Nick Gammon
USA #2
Theres also the option of
hitpct = ((chhit % chmax) * 100);
if you want to do division first.
#3
I hadn't even thought about the fact that if it was greater than 100 it would obviously not be 100. I was just insuring that 100 would still be only healthy, but yeah that makes a lot of sense, i'll try the changes you recommended. Thanks Nick.
#4
Ok i made the changes you suggested, but i'm still always returning a "healthy" response. i did a make clean and recompiled. set up the command with cedit and it was still calling reserved in commands.dat so i changed it there too. still nothing.
Australia Forum Administrator #5
Ah yes, here is your problem. This line:


else if ((hitpct = 100) || hitpct >= 95) { hpt = "healthy"; }


I know it is subtle if you are not looking closely but the first part (hitpct = 100) *sets* the hitpct to 100.

You mean:


else if ((hitpct == 100) || hitpct >= 95) { hpt = "healthy"; }


Because of that the hitpct will always be 100 and thus he will always be healthy.


#6
Heh, thanks a lot nick, i'm still pretty new to this.. i've been reading up on C and working on this code as my practice but i'm still prone to mistakes such as that. thanks for pointing it out, works fine now.