• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Need a formula for custom bar

Status
Not open for further replies.
Level 8
Joined
Mar 3, 2009
Messages
327
Im trying to make a custom health bar. Please dont point me to a Vjass system. Anyway, im trying to make a unit's percentage life scale to a bar made up of X characters. For example,

Unit is on 50% life
Bar is 10 characters long
Therefore, 5 characters are white, 5 have been coloured black

Unit is on 50% life
Bar is 20 characters long
Therefore, 10 characters are white, 10 have been coloured black

I just can't think of a formula to do this. Ive been trying for a while and have hit a few roadblocks. If someone could provide one for me that would be great. Also, heres a piece of code. I've been trying
Code:
%Life/Bar length = Amount of characters to colour
to no avail.

  • NTest
    • Events
      • Player - Player 1 (Red) types a chat message containing n as A substring
    • Conditions
    • Actions
      • Set tempstring = <Empty String>
      • Set Characters = (Integer((Substring((Entered chat string), 2, (Length of (Entered chat string))))))
      • -------- X --------
      • Set Tempreal = 80.00
      • -------- Y --------
      • Set Tempreal2 = (Tempreal / (Real(Characters)))
      • For each (Integer A) from 1 to Characters, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Tempreal2 Less than (Real((Integer A)))
            • Then - Actions
              • Set tempstring = (tempstring + (|C00000000 + (Character + |r)))
            • Else - Actions
              • Set tempstring = (tempstring + Character)
Anyway, "Character" Is a string that is used. It is an l. Characters is an integer for how long the bar is in this test instance. Tempreal is the percentage life of our imaginary unit. That code there ^ actually works, but only if the bar is 10 characters long..

Thanks in advance for any assistance.
 
Level 8
Joined
Mar 3, 2009
Messages
327
Hmm. It behaves a bit oddly when I change the char count to 40. Sometimes the bar will be 70% full when the bloodmage's health is at about 30%, and sometimes it shows the colour codes on the end of the bar..

EDIT: Oh wait, i see what you did there. Alrighty im testing the formula..

EDIT2: Ah I can't make it work. Any chance you could just post a version of my trigger with tempreal and tempreal2 being set theright way? thanks
 
Level 8
Joined
Mar 3, 2009
Messages
327
It still shows the colour codes at the end sometimes and I dont think the percentages in the bar are exactly right. For example, % is at 85 and I have 80 characters, except the bar is completely full.

EDIT: oh i got it to work! thanks maker. Wish i could rep you, but it always seems to be the same people helping me over and over again, lol.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
This must be a joke...

RoundToNearest(10.0*unithealthpercentage)

RoundToNearest takes a real and returns an integer representing that real rounded using standard rounding behaviour.
unithealthpercentage is the percentage of unit life in multiplicative form (100% = 1, 50% = 0.5, 0% = 0, etc which is basically X*100% = X).

In the case that a round to nearest is not available, it can be computed as such (using X as the number to round and z is the output).
Y = to integer X (this cuts all decimal places as if you covered with a finger)
if X is greater than or equal to y+0.5 then Z = X+1
else Z = X

Unit life multiple can be computed by getting the units current life and dividing by its total life. A unit with 5 health and 10 maximum life.
5/10 = 1/2 = 0.5 = 50% (not the number 50, percentage is just a way of representing a multiple)

This will provide a totally linear and even spread most of the time. The only problems are near the boundaries where it will only be full when over or equal to 95% HP and will be totally empty when under 5% HP. This could be corrected via offsets but would make the scale very odd.

Integers are 32 bit signed values (with range of +(2^31-1) to -(2^31))
Reals are some form of float. Not too sure if it is a 32 or 64 bit float but they behave as a float. This means that they have finite precision and are incapable of representing the majority of numbers (instead it stores only an approximation which for the majoratory of uses is accurate enough.
 
Status
Not open for further replies.
Top