# RMMV Damage Formula - ideas and help

#### Aesica

##### undefined
You forgot the question mark if you're gonna use ternary notation. I believe it should look like this...someone please correct me if I'm wrong btw.
`if (b.isStateAffected(47)) ? (b.addState(Math.floor(Math.random() * 9) + 60)) : (a.atk * 12) / (b.def * 5) * 0.2 `
You're trying to mix an if statement with the ternary operator in a way that I'd be legit surprised if it actually worked without throwing an error.

Code:
``````// ternary
x = condition ? resultIfTrue : resultIfFalse;

// if

// ternary as an if statement

The code I posted works perfectly...though maybe it’s a fluke . In addition to the math floor statement, I’m looking to do extra damage to the monster and add one tp to the actor who used the skill if the IF statement is true. (Enemy has state 47)

Hopefully that’s not too confusing. Thanks for replying!
At a glance, your code is just fine. Only thing I'd add is that you don't need the parentheses in your damage formula since it's all multiplication and division:

Code:
``a.atk * 12 / b.def * 5 * 0.2``

Will yield the exact same result as what you had and his less cluttery to look at. Edit: You could even remove the 5 * 0.2 part (since the result is 1) but I"m guessing you have it there because other skills of yours have other defense multipliers, so it's easier to track that way.

Last edited:

#### Frostorm

##### []D[][]V[][]D
You're trying to mix an if statement with the ternary operator in a way that I'd be legit surprised if it actually worked without throwing an error.
Whoops, didn't notice I left the "if" in there, that shouldn't be there lol. I've edited my earlier post now.

#### Kitsundere

Hi everyone,

Been digging through RPG Maker MV to see what I can see recently and hit an odd bump. Bear with me I spent a whole day putzing around with this. It's a long post...

First some background - I am not a coder. I know some code but I don't have the brain capacity for coding so I'm trying to keep things as simple as I can afford.

What the game is:
Mostly I'm trying to make a game to go along with a cast of characters I have, but me being me I need to do something special with the mechanics. What I settled on was a "momentum" system using Yanfly's ATB system and TP.

The way the game works is that the turn order is determined on the character's .agi and their current TP level. What happens is the more momentum (TP) they build up, the faster they get to attack until it's basically as fast as the player can input the commands, attacking three or four times before the enemy even has a chance.

The hitch is that I need to seriously control how TP is handled, and especially be able damage TP. Should be easy right?

The Problem:
I need a simple way for actors to lose TP and HP when hit with damage. I scoured the internet for hours and here's what I came up with.

For reference, a.atk is 7 and b.def is between 5 and 7
Type: HP Damage
Element: None
Variance: 20%

This code should work:
b.gainTp(-Math.randomInt(a.atk*3-b.def)); (a.atk*2+a.luk)-(b.def+b.agi)

Because this code works perfectly:
b.gainTp(-Math.randomInt(a.atk*3-b.def)

And this code works perfectly:
(a.atk*2+a.luk)-(b.def+b.agi)

AND THIS CODE WORKS PERFECTLY
b.gainMp(-Math.randomInt(a.atk*3-b.def)); (a.atk*2+a.luk)-(b.def+b.agi)

But the same code for TP does not.

This is the code I'm currently using:
b.gainTp(-(Math.floor(Math.random() * ((a.atk*3)-b.def)+1))); ((a.atk*2)+a.luk)-(b.def+b.agi);

It works half of the time, but seems to randomly assign negative or positive value to the amount of TP gained. I cannot for the life of me get it to consistently produce a negative result, even with -Math.abs()

What I've tried:
I tried flipping the TP formula to negative in-case my soapy brain got the math wrong. This just always doubles the amount of TP earned.

I tried a different formula that reportedly works using &&. Didn't work at all.

I thought maybe the 20% variance was pushing the result into positive, but turning that to zero didn't fix anything.

I thought that maybe the core system was causing a net positive from TP earned by receiving damage, so I tried Yanfly's enhanced TP plugin but it doesn't appear to work at all since some update.

Can anyone help me out with this?

#### Aesica

##### undefined
Edit: To clarify, this is directed at @Kitsundere

A few things:

1. Variance only affects the actual damage part. It has no effect on anything you do before the actual damage is calculated.

2. Your equations have a lot of unnecessary parentheses and a lack of spacing that make it uglier to read than they need to be. Let the order of operations do some of the work for you:

-(Math.floor(Math.random() * ((a.atk*3)-b.def)+1))

Is the same as:

-Math.floor(Math.random() * (a.atk * 3 - b.def + 1))

For any b with more def than 3x a's attack, you're going to get a negative value which is then flipped to positive, resulting in gained TP. In fact, any of your equations that add/subtract a target's stats from the attackers will have that result if the total defense is greater than the total attack.

You said you were using -Math.abs(), but where were you putting it? I'd actually suggest this approach instead of Math.abs() for situations where really high defense comes into play:

-Math.max(0, Math.floor(Math.random() * (a.atk * 3 - b.def + 1)))

Additionally, have you completely disabled the passive TP gain from taking damage? If a hit innately restores 10 TP, but your formula is set to knock off 4 TP, I'm still going to have a net gain of 6 TP.

#### Kitsundere

Edit: To clarify, this is directed at @Kitsundere

A few things:

1. Variance only affects the actual damage part. It has no effect on anything you do before the actual damage is calculated.

2. Your equations have a lot of unnecessary parentheses and a lack of spacing that make it uglier to read than they need to be. Let the order of operations do some of the work for you:

-(Math.floor(Math.random() * ((a.atk*3)-b.def)+1))

Is the same as:

-Math.floor(Math.random() * (a.atk * 3 - b.def + 1))

For any b with more def than 3x a's attack, you're going to get a negative value which is then flipped to positive, resulting in gained TP. In fact, any of your equations that add/subtract a target's stats from the attackers will have that result if the total defense is greater than the total attack.

You said you were using -Math.abs(), but where were you putting it? I'd actually suggest this approach instead of Math.abs() for situations where really high defense comes into play:

-Math.max(0, Math.floor(Math.random() * (a.atk * 3 - b.def + 1)))

Additionally, have you completely disabled the passive TP gain from taking damage? If a hit innately restores 10 TP, but your formula is set to knock off 4 TP, I'm still going to have a net gain of 6 TP.

Thanks for this!

The extra parenthesis were just paranoia on my part, thanks for clearing that up.
I was putting -Math.abs at the head of formula, again paranoia just trying anything to force a negative output. I was using Math.randomINT so I didn't need to floor it before.

I'm 1000% certain the damage isn't returning negative, but I'll keep that in mind. I don't think I'll need an enemy gaining momentum from being hit.

I didn't *completely* remove passive TP gain cause the plugin doesn't seem to work anymore. Thinking on this I realize why the TP damage formula worked alone because no health was being lost which mean no passive TP gained.

So it looks like passive TP gain from damage might be cause. I'll dig into the code and see if I can't cut that wire, unless there's a plugin someone might recommend.

#### Aesica

##### undefined
Try looking at Game_Battler.prototype.chargeTpByDamage

You can overwrite that with an empty function to switch tp gains from damage off.

Game_Battler.prototype.chargeTpByDamage = function(){};

Or, if you're feeling fancy, you could set it to remove TP instead of doing it through the formula box in what is presumably every damage-dealing skill you have so far.

#### Snarkyfork

##### Veteran
I need a spell that raises an ally but reduces the caster to 1 hp, how would I go about making that happen? Any advice would be greatly appreciated, thanks!

This is the formula I am using, it's the res from the Octopack Battler sample project.

Math.max(1, b.mhp / 2 * (a.bp + 1))

#### Kagemaru

##### Veteran
I sock at making formulas, so I might be wrong, but Im pretty sure you dont need a complicated formula to do that. If by "a spell that raises an ally", you mean raising one of their stats, then simply use the effects box to add a buff. If you meant raising an ally from the dead, in other words, reviving them, then just use the effects box to remove the death state and then heal that allys HP by a certain amount. Finally, for reducing the casters HP to 1, put
Code:
``a.setHp(1)``
in the formula box.

I also have a question of my own. Im trying to make a stat increasing system like in FF2 or the SaGa games without a plugin. In other words, a system that increase stats without relying on an actors level. For exemple, if Actor1 uses the attack command, they would deal damage to the enemy and afterwards, if the attack didnt miss, there would be a 25% chance that Actor1s attack stat would PERMANENTLY increase by 1~4. Then if Actor2 uses a spell that costs MP, the spell would deal damage to the enemy and afterwards, if the spell didnt miss, there would be a 25% chance that actor2s magic attack stat would PERMANENTLY increase by 1~4 and a 30% chance that their MP stat would PERMANENTLY increase by 1~3. Finally, if Enemy1 attacks Actor1 with a physical attack but Actor1 dodges the attack, Actor1s agility stat would PERMANENTLY increase by 2~3. Is there a way to do something like that with a damage formula or with a combination of a damage formula and the effects box?

EDIT: I found a way to do this thanks to Beregon.

Last edited:

#### FanrirRC

##### M.A.S.K
Hello everyone
I've been trying to make a formula for a skill where the damage is based on multiple factors.

Code:
``````dam = math.sqrt(a.atk/(2*b.def)); {
if (a.isStateAffected(17)) { dam *= 1.4 };
if (a.isStateAffected(18)) { dam *= 0.7 };
if (b.isStateAffected(19)) { dam *= 0.7 };
if (b.isStateAffected(20)) { dam *= 1.3 };
if (a.atk === 99) { dam *= Math.randomInt(10)+2 };
if (dam <= 0) { dam = 1 }
}``````

I tried to first calculate the base power with
Code:
``dam = math.sqrt(a.atk/(2*b.def)); {``

Then the base power would be multiplied based on whether the attacker or defender has an Atk up/Down, Def up/Down
Code:
``````if (a.isStateAffected(17)) { dam *= 1.4 };
if (a.isStateAffected(18)) { dam *= 0.7 };
if (b.isStateAffected(19)) { dam *= 0.7 };
if (b.isStateAffected(20)) { dam *= 1.3 };``````

After that it checks if the attacks atk stats is 99 (which is maximum in the game), if so it multiplies the damage with a random number that's between 2 - 10
Code:
``if (a.atk === 99) { dam *= Math.randomInt(10)+2 };``

The damage gets checked to see if its 0, if so it becomes 1
Code:
``if (dam <= 0) { dam = 1 }``

I posted a thread a few days had some help but for some reason, the damage keeps on coming up as 0

Does anyone know how I can fix it

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
If you've written everything that's in your formula, you forgot to indicate the damage value at the end of your code. The engine itself doesn't read the names of your variables to determine what you want the damage to be equal to. So you can put all the code you want, but the last thing to enter is the damage value without a `;` at the end.
Example: `dam = math.sqrt(a.atk/(2*b.def)); if (a.atk === 99) { dam *= Math.randomInt(10)+2 }; dam`

Another thing, is there a particular reason why you've put curly brackets before your 6 conditions?

Try this:
JavaScript:
``````dam = math.sqrt(a.atk/(2*b.def));
if (a.isStateAffected(17)) { dam *= 1.4 };
if (a.isStateAffected(18)) { dam *= 0.7 };
if (b.isStateAffected(19)) { dam *= 0.7 };
if (b.isStateAffected(20)) { dam *= 1.3 };
if (a.atk === 99) { dam *= Math.randomInt(10)+2 };
if (dam <= 0) { dam = 1 };
dam``````

If there's no particular reason, that could be why your code isn't working.

After that it checks if the attacks atk stats is 99 (which is maximum in the game), if so it multiplies the damage with a random number that's between 2 - 10
Code:
``if (a.atk === 99) { dam *= Math.randomInt(10)+2 };``
By the way, `Math.randomInt(10)+2` will return a number between 2 and 12. For a number between 2 and 10, it's `Math.randomInt(8)+2`.

#### FanrirRC

##### M.A.S.K
If you've written everything that's in your formula, you forgot to indicate the damage value at the end of your code. The engine itself doesn't read the names of your variables to determine what you want the damage to be equal to. So you can put all the code you want, but the last thing to enter is the damage value without a `;` at the end.
Example: `dam = math.sqrt(a.atk/(2*b.def)); if (a.atk === 99) { dam *= Math.randomInt(10)+2 }; dam`

Another thing, is there a particular reason why you've put curly brackets before your 6 conditions?

Try this:
JavaScript:
``````dam = math.sqrt(a.atk/(2*b.def));
if (a.isStateAffected(17)) { dam *= 1.4 };
if (a.isStateAffected(18)) { dam *= 0.7 };
if (b.isStateAffected(19)) { dam *= 0.7 };
if (b.isStateAffected(20)) { dam *= 1.3 };
if (a.atk === 99) { dam *= Math.randomInt(10)+2 };
if (dam <= 0) { dam = 1 };
dam``````

If there's no particular reason, that could be why your code isn't working.

By the way, `Math.randomInt(10)+2` will return a number between 2 and 12. For a number between 2 and 10, it's `Math.randomInt(8)+2`.

I just tried it but it still wouldn't work
The damage output is still 0

But I'm pretty sure the problem is in here
Code:
``dam = math.sqrt(a.atk/(2*b.def));``

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
But I'm pretty sure the problem is in here
Code:
``dam = math.sqrt(a.atk/(2*b.def));``
Oh, I completely missed that.
It's `Math.` with a capital M, JS is case-sensitive and this word has a capital M.
Try `dam = Math.sqrt(a.atk/(2*b.def));`

#### FanrirRC

##### M.A.S.K
Oh, I completely missed that.
It's `Math.` with a capital M, JS is case-sensitive and this word has a capital M.
Try `dam = Math.sqrt(a.atk/(2*b.def));`

Oh yea, It works perfectly now
Thank you so much

I also realized that when the formula checks this
``` if (a.atk === 99) { dam *= Math.randomInt(10)+2 }; ```

it checks the total atk of the actor, but I've been trying to make it check only the base atk of the class equipped on the actor without counting the weapon that's equipped.

Is that possible to do normally or do I need a plugin in order to so?

Last edited:

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
Oh yea, It works perfectly now
Thank you so much

I also realized that when the formula checks this
``` if (a.atk === 99) { dam *= Math.randomInt(10)+2 }; ```

it checks the total atk of the actor, but I've been trying to make it check only the base atk of the class equipped on the actor without counting the weapon that's equipped.

Is that possible to do normally or do I need a plugin in order to so?
Use `paramBase(2)` instead of `atk`.
`if (a.paramBase(2)=== 99) { dam *= Math.randomInt(10)+2 };`

#### FanrirRC

##### M.A.S.K
Use `paramBase(2)` instead of `atk`.
`if (a.paramBase(2)=== 99) { dam *= Math.randomInt(10)+2 };`

Yea, it works perfectly now!
Thank you so much for your help CHKNRAVE
I truly appreciate it

#### Ebontope

##### Plays video games too much to be productive.
For a project that I'm currently working on, I'm using a separate damage formula for enemy attacks that should gradually increase the effectiveness of the enemy's ATK as it goes higher. However, I seem to be consistently getting the wrong numbers, even when double-checked using an online equation calculator.

This is the formula that I'm using:
((a.atk * 4 - b.def) / 2,6) * (1 + (a.atk / 20))

In this formula, if the variables are a.atk = 10 and b.def = 18, the end result should be 12,69. However, when I use this formula in an attack, even if I change the corresponding variables into the static numbers 10 and 18, the result it gives is 9. I also set the Variance to 0%, so this is a consistent and specific value.

Does anybody know what could be causing this issue? Is there something I could be missing?

EDIT: I've figured out the problem, turns out all I had to do was change the decimal point from a comma into a dot. Good to know for future reference.

Last edited:

#### silver-silvera

##### Warper
For RPG Maker MV,
If:
There are two enemies, enemy A and enemy B,
There are two actors, actor 1 and actor 2,
and both enemies have skill X and skill Y,

And if, what I want to happen is that when skill X is used, it calls a common event that does stuff. AFTER that stuff is done, the enemy (whether it is A or B ) that just used skill X uses skill Y on actor 1 or actor 2 (whichever actor was they just targeted with skill X).

How would I go about doing that? I've searched this thread and Google and haven't been able to find a way.

#### TheGentlemanLoser

##### "And when we fall, we will fall together..."
I am trying to figure out what to type into the damage formula box to get a specific skill to have a minimum damage, i.e.

if dmg < 76, set dmg = 76

in this case. But you know with actual javascript? I want to learn JS I really do but I feel like every time I try to put even the slightest snippet of JS into a damage formula box or whatever my entire game explodes the next time I try to run it.

I know math.floor and math.min don't do this...I think...even though they both sound kind of like they should (yes this is how ******ed I am at trying to learn JS) it's an if else function but yeah my brain is NOT made for this lol

I am surprised that I'm having trouble with this, as it seems like a super basic function. I recall this being in a Yanfly script in VXA and it's probably somewhere and a and think I've probably overlooked something ominous again. I'm paging back through this thread trying to learn how to solve it on my own or if I find someone with the exact same problem.

#### CHKNRAVE

##### Surprisingly not sponsored by Yanfly.
I am trying to figure out what to type into the damage formula box to get a specific skill to have a minimum damage, i.e.

if dmg < 76, set dmg = 76

in this case. But you know with actual javascript? I want to learn JS I really do but I feel like every time I try to put even the slightest snippet of JS into a damage formula box or whatever my entire game explodes the next time I try to run it.

I know math.floor and math.min don't do this...I think...even though they both sound kind of like they should (yes this is how ******ed I am at trying to learn JS) it's an if else function but yeah my brain is NOT made for this lol

I am surprised that I'm having trouble with this, as it seems like a super basic function. I recall this being in a Yanfly script in VXA and it's probably somewhere and a and think I've probably overlooked something ominous again. I'm paging back through this thread trying to learn how to solve it on my own or if I find someone with the exact same problem.
Try `Math.min(76, FORMULA)`. Replace `FORMULA` by the usual damage value, and make sure the M in `Math` is uppercase.
`Math.floor(n)` will round a number down, so it wouldn't do the trick for what you're trying to do.

#### TheGentlemanLoser

##### "And when we fall, we will fall together..."
thanks I'll try that out!

### Latest Profile Posts

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
I'm Really proud of how my game over screen looks!
I felt inspired by a piece of art I saw. So I made this.

Thought i'd be cool to have like a company introduction. Check it out!