RMMV Change the damage formula directly in the JS file...

Indinera

Indie Dev
Regular
Joined
Mar 13, 2012
Messages
2,547
Reaction score
1,281
First Language
French
... 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
Regular
Joined
May 30, 2023
Messages
440
Reaction score
264
First Language
EN/JP/NL
Primarily Uses
RMMZ
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

^_^
Global Mod
Joined
Feb 21, 2018
Messages
5,259
Reaction score
4,734
First Language
EN
Primarily Uses
RMMZ
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Minimum action damage is 5~10 (random).
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/161247/
 * @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
Regular
Joined
Apr 25, 2017
Messages
2,264
Reaction score
1,781
First Language
Dutch
Primarily Uses
RMMV
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
Regular
Joined
Mar 13, 2012
Messages
1,045
Reaction score
527
First Language
English
Primarily Uses
RMMV
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
Regular
Joined
Mar 13, 2012
Messages
2,547
Reaction score
1,281
First Language
French
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Minimum action damage is 5~10 (random).
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/161247/
 * @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

^_^
Global Mod
Joined
Feb 21, 2018
Messages
5,259
Reaction score
4,734
First Language
EN
Primarily Uses
RMMZ
Great! If you do run into problems with it, let me know~ :kaohi:

@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
Regular
Joined
Apr 25, 2017
Messages
2,264
Reaction score
1,781
First Language
Dutch
Primarily Uses
RMMV
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. :p

Thanks for the explanation.
 

Indinera

Indie Dev
Regular
Joined
Mar 13, 2012
Messages
2,547
Reaction score
1,281
First Language
French
@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
Regular
Joined
Aug 7, 2022
Messages
392
Reaction score
524
First Language
English
Primarily Uses
RMMV
@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

^_^
Global Mod
Joined
Feb 21, 2018
Messages
5,259
Reaction score
4,734
First Language
EN
Primarily Uses
RMMZ
Oops! Yep, Mac's edit should fix that. :kaoblush:
 

Latest Posts

Latest Profile Posts

Help, I can't stop! :kaohi:

alice_ornament.png
I'm happy to join this community.
about this argument. I expressed myself badly, I did it on my own, my English was mixed with Google Translate. And I believe chatGPT didn't even exist in 2016
I have to take sleeping pills :rtear:
Now that the forum is running smoothly, I can run around and react to posts the millisecond they're posted.
patrick-star-spongebob.gif

Forum statistics

Threads
136,812
Messages
1,270,316
Members
180,574
Latest member
PastorGary
Top