Context: At full HP, offensive skills should deal 100% damage, while at 1 HP their damage should be reduced to either 66% or 66,666% -- either value is fine to me if it's achievable by tinkering with the damage formula. Problem is i don't know how to go about it.
Assuming a skill with a damage formula of "a.atk - b.def", what should be added in there so it deals, say, 1% less damage for every 3% HP the user is below his max HP?
Multiply damage based on current hp % using a rule of 3 and incorporate it into the formula.
if your max hp is 5000 thats 100% then current hp is 3000, so 3k * 100/5k that gives us 60 as a result (as its 60% of hp) the formula right now is
(a.hp*100/a.mhp)
We divide this /100 to get a multiplier value for the damage as multiplying *60 is actually a boost, we now have:
(a.hp*100/a.mhp)/100
In this case the result is on 60% but for cases where the result is more than 66% you dont want potential damage to be 0, you specified the penalty to cap at 66% so we include a math.min which will make it pick the lowest between 66% or your current result, so at 67%+ it will pick your 66% cap as the "minimum value".
Now with the conditionals in place the final formula is:
a.atk-b.def *(math.min(((a.hp*100/a.mhp)/100), 0.66))
The first part replaced by your "true" damage formula, which will be then submitted to a multiplier based on hp, capped at 66% penalty.
Hope i didnt made any syntax mistakes there 😴
1% less damage for every 3% HP below max HP can be calculated as follows:
The current HP percentage is: hp / mhp. This gives you a number between 0 and 1, with 1 being 100%.
The HP percentage below max is as simple as: 1 - hp / mhp. This will reverse the above value, so 0 means no damage, and 1 means your character is already unconscious (0 HP).
Scaling down the 3% HP to 1% damage is as simple as: (1 - hp / mhp) / 3. You will have a number between 0 and 1/3.
To get the damage multiplier, you need to reverse the damage reduction: 1 - (1 - hp / mhp) / 3. This will give you a number between 2/3 to 1. All you need to do now is multiply the damage with this number (aka "damage multiplier"), for example: (a.atk - b.def) * (1 - (1 - a.hp / a.mhp) / 3). If the multiplier is 1, there will be no damage reduction (x * 1 = x), and if the multiplier is 2/3, your damage will be reduced by 1/3 (it will be 2/3 the intended damage).
Of course, there are multiple ways to write the same formula: 1 - (1 - hp / mhp) / 3 is the same as: 1 - 1/3 + hp / mhp / 3, which is the same as: 2/3 + hp / mhp / 3 which is the same as: (2 + hp / mhp) / 3. Choose whichever formula you think is easier to read.
Interesting idea. Are you sure you want damage to scale all the way down? Like it's impossible to fight back when you're at 1 hp?
You could consider using Math.max to set a second formula that makes sure the player at least does some damage. Like you could set it to a flat number like 10. Or another formula so you do 50% less damage or something instead of 99% less damage.
Oh, whoops. Sorry I skimmed too fast and got confused ^_^;;
Math.max should be something to keep in your back pocket, though. I would guess that this current formula you have might be difficult to tune around. Unless you have some sort of system where the players frequently find themselves on the brink of death. At 50% hp, you're only losing about 16% damage? So any less damage might be negligible. And and seeing the penalty ramp up might be imperceptible. Obviously I don't have the full picture, though.
Anyway, good luck!