Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Health messages rather than HP

Health messages rather than HP

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Valna   (15 posts)  Bio
Date Thu 25 Mar 2004 04:47 AM (UTC)
Message
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?
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 25 Mar 2004 05:50 AM (UTC)

Amended on Thu 25 Mar 2004 05:52 AM (UTC) by Nick Gammon

Message
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.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Meerclar   USA  (733 posts)  Bio
Date Reply #2 on Thu 25 Mar 2004 06:17 AM (UTC)
Message
Theres also the option of
hitpct = ((chhit % chmax) * 100);
if you want to do division first.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
Top

Posted by Valna   (15 posts)  Bio
Date Reply #3 on Thu 25 Mar 2004 07:05 AM (UTC)
Message
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.
Top

Posted by Valna   (15 posts)  Bio
Date Reply #4 on Thu 25 Mar 2004 08:17 AM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #5 on Thu 25 Mar 2004 08:39 PM (UTC)
Message
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.



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Valna   (15 posts)  Bio
Date Reply #6 on Sat 27 Mar 2004 01:39 AM (UTC)
Message
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.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


19,440 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.