Damage Formula Request (Damage Based On HP Situation)

Hi, guys...

I was trying to make a skill that deals more damage according to the target's HP situation, the lower its current HP, the higher is the skill's damage. The problem is that the formula isnt workin. Here's what I'm using (the whole code):

((b.hp / b.mhp) + 1) * a.atk * 3 - b.def * 2The reason I've used that:

"(b.hp / b.mhp)" is where I should get the health situation (ranging from 0.0000... to 1, the later being full HP). Then I add the valor 1 to it. So I can boost the a.atk.
 

So "((b.hp / b.mhp) + 1) * a.atk" means that the maximum boost is something near 100% of that damage.

The problem is that it is not working. In fact the first hit on an enemy is doing way more damage than the subsequent hits on the same enemy. Right after the first hit on the same target, the subsequent hits keep doing the same damage, like a bug.

Where am I doing wrong?
b.hp / b.mhp will always evaluate to 0 because two integer operands will always result in an integer.  And it truncates, rather than rounding.


Try

((b.hp.to_f / b.mhp) + 1).to_i * a.atk * 3 - b.def * 2 
It also follows the mathematical order of operations so it'll do all the multiplications, and then the subtraction.  That may or may not be what you want.  If it's not, you'll need to add some more brackets to force it to do the subtraction at the right point.


Edit: I'm surprised nobody picked up my mistake. Putting the .to_i where I did will pretty much give you what you originally had. Just wrap the whole lot in brackets and add .to_i at the end:

Code:
((b.hp.to_f / b.mhp) + 1 * a.atk * 3 - b.def * 2).to_i

Original thread: https://forums.rpgmakerweb.com/threads/damage-formula-request-damage-based-on-hp-situation.44915/