# Damage Formulas 101

#### ThreeSixNine

##### Veteran
I think I may not have properly explained what the goal was, but I may have gotten the answer I needed.

If the attack hits, it's supposed to deal damage, then apply a debuff to evasion. However, I didn't think to use a state to lower evasion, which would make doing this a bit easier (unless I'm wrong, but I'm open to learning). I'm gonna try going with applying a state that changes the evasion rate of the affected enemy.
I actually misspoke when I said the state would be added to the target and then the damage would be calculated.

If you check out the flow chart above, you'll see how each step is processed and when. States are added/removed after the damage is done, but must be processed before the actual damage calculation.

The above code:
`b.addState(369); 200 + a.mat * 2 - b.mdf * 2`
should work, just replace 369 with the ID of your evasion reducing state.

#### UmbrotheUmbreon

##### Legendary Umbreon
I actually misspoke when I said the state would be added to the target and then the damage would be calculated.

If you check out the flow chart above, you'll see how each step is processed and when. States are added/removed after the damage is done, but must be processed before the actual damage calculation.

The above code:
`b.addState(369); 200 + a.mat * 2 - b.mdf * 2`
should work, just replace 369 with the ID of your evasion reducing state.
Yeah I used the code you provided and it works fine. Is there a way to add to it further, like including a state with a percentage chance to be applied?

#### Trihan

##### Speedy Scripter
Yeah I used the code you provided and it works fine. Is there a way to add to it further, like including a state with a percentage chance to be applied?
If you wanted to do that, you could include a check against a random number. For example say you wanted a 15% chance of applying the state:

JavaScript:
``if (Math.randomInt(100) >= 85) b.addState(369); 200 + a.mat * 2 - b.mdf * 2``

One thing to note about this method which people tend to not know/forget (and Yanfly wrote an article about it a long time ago) is that if you use Autobattle at all in your game, it's better to do it a different way. The reason being when a battler is determining which of their actions to use, they actually evaluate the damage formula to figure out what's best, meaning in this case that their "test" enemies would end up having the state applied even if that action ends up not being used.

#### UmbrotheUmbreon

##### Legendary Umbreon
If you wanted to do that, you could include a check against a random number. For example say you wanted a 15% chance of applying the state:

JavaScript:
``if (Math.randomInt(100) >= 85) b.addState(369); 200 + a.mat * 2 - b.mdf * 2``

One thing to note about this method which people tend to not know/forget (and Yanfly wrote an article about it a long time ago) is that if you use Autobattle at all in your game, it's better to do it a different way. The reason being when a battler is determining which of their actions to use, they actually evaluate the damage formula to figure out what's best, meaning in this case that their "test" enemies would end up having the state applied even if that action ends up not being used.

What I currently have going is the skill has two effects to it. A chance to stun, and a guaranteed evasion debuff. The stun effect is in the effects part, but I saw no way to apply an evasion debuff there. That's why I originally came here to figure out how to get my skill to apply an evasion debuff if it succeeds. I was curious if it was possible to include both the guaranteed evasion debuff and a chance to stun in the damage formula. As far as auto-battle goes, I don't know if the game I'm going for will include it, since a lot of the game will be based on choices you make throughout it, so not too concerned about that. I'll give that formula a try though, and see how it works.

#### ThreeSixNine

##### Veteran
You should be able to add both through the effects of the skill.
You can also add additional effects through linking a common event to the usage of the skill.

#### Trihan

##### Speedy Scripter

What I currently have going is the skill has two effects to it. A chance to stun, and a guaranteed evasion debuff. The stun effect is in the effects part, but I saw no way to apply an evasion debuff there. That's why I originally came here to figure out how to get my skill to apply an evasion debuff if it succeeds. I was curious if it was possible to include both the guaranteed evasion debuff and a chance to stun in the damage formula. As far as auto-battle goes, I don't know if the game I'm going for will include it, since a lot of the game will be based on choices you make throughout it, so not too concerned about that. I'll give that formula a try though, and see how it works.
Yeah, debuffs only apply to base parameters. You'll have to add a state that inflicts an evasion reduction.

#### UmbrotheUmbreon

##### Legendary Umbreon
I appreciate the help guys. I'm still so new to coding and such. It's quite intimidating but I really wanna learn

#### Solar_Flare

##### Veteran
If you wanted to do that, you could include a check against a random number. For example say you wanted a 15% chance of applying the state:

JavaScript:
``if (Math.randomInt(100) >= 85) b.addState(369); 200 + a.mat * 2 - b.mdf * 2``
I think for readability it's better to reverse that conditional:
JavaScript:
``if(Math.randomInt(100) < 15) b.addState(369); 200 + a.mat * 2 - b.mdf * 2``

#### mkrudesign

##### Veteran
Is there a way to set that the minimum amount of damage for a skill as a percent instead of a fixed amount?

For the standard attack skill, the formula I use is: Math.max(a.atk * 4 - b.def * 2,1) So the minimum this skill can do is 1 damage.

The thing is, it gets pretty dumb when enemies who are meant to be intimidating end up hitting for 1 HP if the player becomes over powered. Is there a formula that I can use to make the minimum damage for this skill be, let's say 5% of the target's total health? It would be way easier than having to rebalance the entire game, since mine is open ended.

#### ATT_Turan

##### Forewarner of the Black Wind
Is there a way to set that the minimum amount of damage for a skill as a percent instead of a fixed amount?
Just replace the "1" with whatever equation you want. So, mathematically, 5% of your target's total health is `b.mhp*.05`

Thus, `Math.max(a.atk*4-b.def*2, b.mph*.05)`

#### ScorchedGround

##### Blizzards most disappointed fan (More than ever)
Is there a formula that I can use to make the minimum damage for this skill be, let's say 5% of the target's total health?

You basically just do the exact thing as before, but replace the "1" with the minimum damage you wish for.

e.g.
Math.max(a.atk * 4 - b.def * 2, b.mhp*0.05)

Now, if you asked me, I wouldn't use a % amount because that could get wonky at some points, but rather a flat amount of the attackers stats.
Something like:
Math.max(a.atk * 4 - b.def * 2, a.atk * 0.5)

Edit: Ninja'd

#### mkrudesign

##### Veteran
Thank you! I wasn't sure if it was that easy!

#### mkrudesign

##### Veteran
You basically just do the exact thing as before, but replace the "1" with the minimum damage you wish for.

e.g.
Math.max(a.atk * 4 - b.def * 2, b.mhp*0.05)

Now, if you asked me, I wouldn't use a % amount because that could get wonky at some points, but rather a flat amount of the attackers stats.
Something like:
Math.max(a.atk * 4 - b.def * 2, a.atk * 0.5)

Edit: Ninja'd
Could you explain the downside to using a % amount? You mentioned things could get wonky. Is there something I'm missing here?

#### Quack

##### Villager
Could you explain the downside to using a % amount? You mentioned things could get wonky. Is there something I'm missing here?
I don't think he was objecting to using a percentage amount, but that it was a percentage of the target's hp.
Sometimes bosses have very high hp so you could potentially end up in a situation where 5% of the target is actually more than the normal attack value before being reduced by target's defense.
If you just want to prevent attack value from getting too low, it might be better to set your "minimum" damage value to a percentage of the normal attack value before it is reduced by defense rather than a percentage of the target's hp.
JavaScript:
``Math.max(a.ath * 4 - b.def, a.atk * 4 * 0.05)``

#### ATT_Turan

##### Forewarner of the Black Wind
I don't think he was objecting to using a percentage amount, but that it was a percentage of the target's hp.
Sometimes bosses have very high hp so you could potentially end up in a situation where 5% of the target is actually more than the normal attack value
I don't know what ScorchedGround's objection was, but the original post for this formula did indicate it's for an enemy skill, so killing bosses too quickly wouldn't be a concern

#### lolotron6

##### Villager
Thank you for all this great info, Skunk!

#### Nox_Aeternae

##### Villager
Hey! So, I wanted to write a spell that does a percentage of MaxHP as damage both to the enemy and the caster. My initial formula was this:

a.mhp* 20 / 100 && a.gainHp(0 - a.mhp* 20 / 100)

and it didn't work. The part after the "&&" worked but the part before didn't.

So, I thought that maybe the problem was with the "&&" symbol and changed my formula to

a.mhp* 20 / 100 ; a.gainHp(0 - a.mhp* 20 / 100)

and the result was the same. The caster would take damage but the enemy would take 0.

I've tried a bunch of things but what ended up working was to reverse the order:

a.gainHp(0 - a.mhp* 20 / 100) ; a.mhp* 20 / 100

The above formula worked just as I wanted, but as it stands I'm not sure why it worked while the previous ones didn't. And because of that I don't feel like I've actually learned anything. If anyone could explain the reasoning behind it, I'd be grateful!

#### Andar

##### Veteran
the eval for the damage formula expects a number as a result, and that number will always be the last one processed. in your first variants the number was calculated first, but never used anywhere.

it works the other way around because the number first calculated there is used in the gainhp function instead of just being ignored, while the second calculation is taken as the result of the eval and used by the engine itself.

#### Nox_Aeternae

##### Villager
@Andar, Thanks a lot! That makes perfect sense now. So, the calculation for the damage the ability deals has to always be last.

#### Trihan

##### Speedy Scripter
@Andar, Thanks a lot! That makes perfect sense now. So, the calculation for the damage the ability deals has to always be last.
To expand on what Andar said, when you used && in your first formula, you actually turned it into a *boolean condition*.

a.mhp* 20 / 100 && a.gainHp(0 - a.mhp* 20 / 100)

This would evaluate to *true* if both sides are truthy, and *false* if either side is falsy.

Because a.hp * 20 / 100 just returns itself, that always returns truthy. But the gainHp function doesn't return a value, so it will never evaluate to a truthy value in a condition.

Therefore, your formula was evaluating to *false*, but booleans aren't numbers so it was unable to deal any damage using it.

### Latest Profile Posts

New Boss Attacks for Soul Hero Adventure:
1,Aim and shoot
2,Boulders falling frenzy
3,Throwing Fireballs
4,Chasing Magical Traps
5,Lasers attack of 360-degree
Pokemon AI Logic: "Enemy is weak to Fire, I better use Rain Dance to make fire type moves less effective. I should just always cast Rain Dance first no matter what, even though it's a niche move that I'm not set up to exploit."
After a little test with the parallax mapping, found it lags like a cat on a leash. Instead of the original idea to parallax map the entirety of the ship, I guess I'll settle for editing the tilesets. Probably should have done that first.
So I kinda came back and learned how to mod other RPG Maker games so... Hello everyone!
Version 1.0.1 done! Now finally free time... oh

123,286
Messages
1,155,503
Members
161,694
Latest member
naylith