# RMMVChange the damage formula directly in the JS file...

#### Indinera

##### Indie Dev
... so that instead of taking just the damage formula (as per defined in the skill tab), it does:

Max(regular damage formula, a random number between 5-10)

Thanks in advance for any help!

#### werzaque

##### Canned Dinosaur
Just so I understand what's being asked here... Is there any particular reason why you do not want to do this simply by typing it in the damage formula for each skill?

#### caethyril

##### ^_^
JavaScript:
``````/*:
* @target MV MZ
* @plugindesc Minimum action damage is 5~10 (random).
* @author Caethyril
* @help Free to use and/or modify for any project, no credit required.
*/
void (function(alias) {
Game_Action.prototype.evalDamageFormula = function(target) {
return Math.max(alias.apply(this, arguments), 5 + Math.randomInt(6));
};
})(Game_Action.prototype.evalDamageFormula);``````
(`randomInt(6)` returns a uniformly-random integer in the range 0~5.)

#### JohnDoeNews

##### AFK TTYL
For a random nr between 5 and 10, I do it like this:

Math.random() * 5 + 5

Math.random() will choose random between 0 and 1.

Times 5 makes it random berween 0 and 5

Plus 5 will make it between 5 and 10, with 5 as minimum and 10 as maximum

#### Maliki79

##### Regular
You have to define the dam formula in the skill tab. That said, you can make it reference js code in a custom plugin. To do this, try something like:
JavaScript:
``b.defaultAttack(a, b)``
Then, in your plugin code, have a function:
JavaScript:
``Game_Battler.prototype.defaultAttack = function(a, b)``
And put whatever alterations you need in there.
You do need it to return a number and must be careful of the code in there, but this is an easy way to lengthen and customize the damage formula.

Hope this helps.

#### Indinera

##### Indie Dev
JavaScript:
``````/*:
* @target MV MZ
* @plugindesc Minimum action damage is 5~10 (random).
* @author Caethyril
* @help Free to use and/or modify for any project, no credit required.
*/
void (function(alias) {
Game_Action.prototype.evalDamageFormula = function(target) {
return Math.max(alias.apply(this, arguments), 5 + Math.randomInt(6));
};
})(Game_Action.prototype.evalDamageFormula);``````
(`randomInt(6)` returns a uniformly-random integer in the range 0~5.)

I think this worked, thank you.

#### caethyril

##### ^_^
Great! If you do run into problems with it, let me know~

@Maliki79's suggestion is good if you use similar formulae in many places and want to keep them consistent. Helps to have everything in one place if/when you want to change the formula, too.

Math.random() * 5 + 5
RMMV's core scripts round the final formula result to the nearest integer, rather than floor it. That's relevant because `Math.random()` returns a value in the range `[0, 1)` (i.e. excluding `1`), so:
• `Math.round(Math.random() * 5)` -> `0, 1, 2, 3, 4, 5`, but `0` and `5` are half as likely as the others.
• `Math.floor(Math.random() * 5)` -> `0, 1, 2, 3, 4`, all equally likely.
`Math.randomInt(n)` is an RMMV/Z shorthand for `Math.floor(Math.random() * n)`.

#### JohnDoeNews

##### AFK TTYL
RMMV's core scripts round the final formula result to the nearest integer, rather than floor it. That's relevant because `Math.random()` returns a value in the range `[0, 1)` (i.e. excluding `1`), so:
• `Math.round(Math.random() * 5)` -> `0, 1, 2, 3, 4, 5`, but `0` and `5` are half as likely as the others.
• `Math.floor(Math.random() * 5)` -> `0, 1, 2, 3, 4`, all equally likely.
`Math.randomInt(n)` is an RMMV/Z shorthand for `Math.floor(Math.random() * n)`.
Oh wow... I knew how Math.random works (take any value between 0 and 1, with unlimited decimals), but I never actually realized that 0 can only be reached by rounding down and the top number only by rounding up, while all numbers in between ban be reached by rounding up and rounding down.

So I guess I did it wrong all this time.

Thanks for the explanation.

#### Indinera

##### Indie Dev
@caethyril I have to report a glitch with:

Code:
``````void (function(alias) {
Game_Action.prototype.evalDamageFormula = function(target) {
return Math.max(alias.apply(this, arguments), 5 + Math.randomInt(6));
};
})(Game_Action.prototype.evalDamageFormula);``````

It seems to work fine with HP Damage, but if you put HP Recover, well it will still pick 5 + Math.randomInt(6)) over the recovery formula (considered a negative value I suppose), so will damage the target instead of recovering it.

#### Mac15001900

##### JavaScript wild sorcerer
@caethyril I have to report a glitch with:

Code:
``````void (function(alias) {
Game_Action.prototype.evalDamageFormula = function(target) {
return Math.max(alias.apply(this, arguments), 5 + Math.randomInt(6));
};
})(Game_Action.prototype.evalDamageFormula);``````

It seems to work fine with HP Damage, but if you put HP Recover, well it will still pick 5 + Math.randomInt(6)) over the recovery formula (considered a negative value I suppose), so will damage the target instead of recovering it.
The problem with computers is that they never do what you want them to do, and instead do exactly what you tell them to do. It's not really a glitch, the damage formula has been modified exactly in the way you asked it to be

Of course this can be changed so that negative values are not affected:
JavaScript:
``````void (function(alias) {
Game_Action.prototype.evalDamageFormula = function(target) {
let normalValue = alias.apply(this, arguments);
if(normalValue >= 0) return Math.max(normalValue, 5 + Math.randomInt(6));
else return normalValue;
};
})(Game_Action.prototype.evalDamageFormula)``````

#### caethyril

##### ^_^
Oops! Yep, Mac's edit should fix that.

### Latest Profile Posts

Looooooonnnnggg maaaaannnnn
Yknow what? Im seriously considering recruiting a manager to oversee my games development.
Because I cannot focus or complete these tasks by myself. I need someone to give me orders, without having them be my boss.

Remember my latest plugin for rpg maker mz:

Acknowledgement Window is now available!

Take a look here:

Got my focus back, 9/59 maps have the door fix in place now.