Damage Formula

I'm posting some questions in regards to the hit formula box.  One, what is the proper syntax for "rounding up" or "rounding down" to the nearest whole number?  What is the syntax for "if 0 or less, then 1?"

Basically, how does this formula translate:

(x - 10/2, rounded down + random number 1-20) + (a stat defined by N.A.S.T.Y. Extra Stats) - y

x = Attacker's Strength

y = Defender's Defense

For those of you in the know, it is sort of a variation of the D20 hit and damage formula.
Hmmm, lot of comments here, hopefully you can make sense of them.


10/2 is 5, so x - 10/2 will always be x-5. If that's what you mean, you should use 5 to avoid an unnecessary calculation. If you mean to take 10 away first and then divide, you need to use parentheses: (x - 10)/2.


To ensure anything below 1 is replaced by 1, you just use the max function: [x, 1].max means if x is 0 or lower, the result will be 1, but if x is more than 1, the result will be x.


Since the numbers you are working with are all integers, there is no rounding down or rounding up needed. However, if you did want to use floats (which you could do by specifying 10.0 instead of 10), you would do ((x - 10) / 2).round to round up or down, or ((x - 10) / 2).to_i to truncate the decimal. But don't convert it to a float and then back to an integer, since it will give you an integer by default.


The rand function will give you a random integer between 0 and that number. Since you want a minimum of 1, you'd have to do rand(19) to get something between 0-19, then add 1 to it.


With all of your description, I'm guessing you'll be wanting something like this:


[((a.str - 10) / 2), 1].max + 1 + rand(19) + nasty_stat - b.def
I did indeed mean (x-2)/5.  Which would create decimals.
No, it won't. If x is an integer then your result is an integer.


Rule of mathematics in RGSS (or Ruby?) - if your operands are integers, your result will be integer, regardless of the operations you're performing. So 5/2 would give you 2, not 2.5


Once you introduce a float to the equation, the result from that point will be a float. So to FORCE your result to have decimals, you would have to force one of the operands to be a float: 5.0 / 2 would give you 2.5


But don't do ((x - 10.0) / 5).to_i to convert it to a decimal and then back to an integer, as it's wasteful since you'll end up with the same result.
[((a.str - 10) / 2), 1].max + 1 + rand(19) + nasty_stat - b.def
Thanks, I'll let you know how it works

Edit:

I ended up streamlining the formula thus:

(a.xstat.str + (rand(19) + 1)) - b.xstat.con

with xstat.str being an approximation of the attacker's str modifier and xstat.con being a representation of the defender's AC

xstat.str being defined as (atk - 10)/5 in the actual NASTY script

The results were absolutely satisfactory.

Original thread: https://forums.rpgmakerweb.com/threads/damage-formula.16442/