# D20 Style Combat in RPG Maker

**UPDATE: While the logic below is sound, a recent comment drew my attention to testing what I posted here to find a number of small but crucial JavaScript syntax errors that may cause the formula to result in always returning 0 (the universal RPG Maker sign that something broke). Here is the corrected formula for use in the Damage Formula:**

**b.def <= (Math.randomInt(20) + 1 + (Math.floor((MOD – 10)/2)) + (Math.floor(a.level/2))) ? Math.random(X*Y) + 1 + (Math.floor((a.atk – 10)/2)) : 0;**

**Substitute MOD for a.atk or a.mat or whatever parameter you want to use for the attack. Substitute X for the number of dice. Substitute Y for the dice size (4,6,8,10,12,20).**

Something I’ve often toyed with aside from my own game FateStone was the idea of re-creating a Dungeons & Dragons campaign in something like RPG Maker. Seems easy right? You’ve got dungeons, monsters, characters all there and ready to go! However, the big hurdle is quite simply that the way combat works does not overlap. Like at all. RPG Maker’s combat calculations are more inspired by Final Fantasy or Dragon Quest than anything you’d find in a Pen & Paper RPG tome. So I recently put my mind to work on figuring out how exactly you would be able to bring a d20 or D&D Style of combat to a RPG Maker game.

Now take in mind that this is a very basic version of what I started working with. While I have started working on versions to incorporate all the different D&D ability scores, I haven’t hammered out all the nitty gritty of using them. So for now I strictly went for Attack Roll (Attack) vs AC (Defense) and Spell Attack (Magic Attack) vs Saving Throw (Magic Defense).

First is the dice roll:

*1dX = Math.randomInt(X)+1*

*YdX = Math.randomInt((X*Y), Y) + 1*

For the YdX formula, it’s important to note that you’ll be setting the range of the random numbers, when it says X*Y you should replace that with the actual value of X * Y. In other words, for 3d6 don’t put (6 * 3) just put (18). These formulas will be used for everything from determining the attack to the damage, so they are pretty much the cornerstone of this whole thing. But another important one would be how to you get the Ability Modifier from the Ability Score. For that you’ll want to use the following calculation:

*Math.Floor((A – 10)/2) = M*

* A = Ability Score. M = Ability Modifier.*

In simple terms, you subtract 10 from the Score, divide that by 2 and round down (because you always round down in D&D) and that will give you the modifier. So an Attack (Strength) of 14 would result in a modifier of 2.

So how would this work for an actual skill? Well, let’s take a look at one. First, you’ll want to set the Skill in RPG Maker to be a ‘Certain Hit’. We are just going to skip the whole Accuracy/Evade cycle of the attack in favor of our own math. Then our damage formula will look something like this:

*If (b.def <= (Math.randomInt(20) + 1 + (Math.Floor((a.atk – 10)/2) + (Math.Floor(a.lvl / 2)) )) Math.random((X*Y), X) + 1 + (MOD – Math.Floor(a.level/2)); else 0*

Kind of crazy, right? Let’s break it down.

*If (b.def <=: *This First bit is essentially starting an ‘If-then’ clause that says if the following math results in something equal to or higher than our target’s defense (AC).

*(Math.randomInt(20) + 1:* This is our d20 roll.

*+ (Math.Floor((a.atk – 10)/2): *This is adding our attack modifier

*+ (Math.Floor(a.lvl / 2)) )):* This adds half our level to the math and finishes our If condition. So it’s a random number between 1-20, plus the modifier, plus half our level.

*Math.random((X*Y), X) + 1 + (Math.Floor((a.atk – 10)/2)); *This part is our damage calculation. Essentially, do this much damage (a random XdY dice amount) plus our Attack modifier damage.

*else 0 *And if the math DIDN’T equal or beat the Target’s Defense(AC), then deal zero damage due to it being a miss.

To summarize, the formula is basically:

*If (Target AC) <= 1d20 + Attack Modifier + Half Level; Deal XdY + Attack Modifier damage; else deal no damage.*

Naturally, you can probably imagine how this basic formula can be applied to a lot of different things. It forms the basic idea for skill checks, saving throws, and pretty much any Difficulty Check based roll. You could replace the target defense with a d20 roll on the enemy side as well and have an opposed check.

As I said at the top, this isn’t perfect. It doesn’t quite yet take into account D&D’s Ability Scores, which I’m still working on. Mostly just stuck on thinking of a way to make the Target Defense side of things work when b.def would simply be their Constitution score or something.

If I ever figure out a good solution to it, I will let you know.

In the mean time, you might find the following plug ins for RPG Maker MV to be handy when it comes to recreating the D&D experience:

Yanfly’s Weapon Unleash: Allows you to reassign a different attack skill to different weapons, thus being able to give daggers a different damage formula than a great axe.

Yanfly’s Limited Skill Usages: For those interested in bringing D&D 4th Edition’s system of At-Will, Encounter and Daily abilities to the game, this plugin can help. However, you might want to create a common event for sleeping that gets called when using an item like ‘Camping Set’ or something to reset the Daily uses.

Posted on November 27, 2017, in Gaming, Other Random Geekery and tagged 4e, 5e, D&D, d20 system, Damage Formulas, dungeons & dragons, Math, RPG Maker, RPG Maker MV. Bookmark the permalink. 8 Comments.

This is beautiful! The intricate and detailed math of “how to” is SUPER helpful to someone who is starting out, like me! I love the board games and RPG games, virtual and in real life! I am even creating a school project ton creating RPG style games! Math of how to fight is SO needed in creating those fight scenes!

Oh man, thanks for this. I’ve been banging my head against the wall thinking about solutions for similar. Did you work on more D20 stuff? I’m having a blast mucking with ideas and assets for it.

I’ve actually been working on a d20 style plug in for RPG Maker MV that uses D&D 4e as a jumping off point. It’s still in development, but so far you can target specific defenses with attacks, have states and items that grant bonuses or minuses to attack rolls, and I’ve just got weapon proficiency for attack rolls implemented.

Every time I test this out, my actors hit with 100% accuracy but deal zero damage..? What am I doing wrong? Also, would this be compatible if I substituted the above damage formula with values assigned via Hime’s Weapon Damage plugin..? Thanks in advance!

You are not doing anything wrong it turns out! Upon reviewing your error, I went back and ran through all this over again and for some reason (my bad) there was a bunch of syntax errors that caused the formula to break. (JavaScript is mighty picky about capitalization) I have posted an updated and corrected formula at the top of the post that should fix the issues.

As for using Hime’s Weapon Damage plugin, you sure can. Just replace the damage calculation in the second half with Hime’s weapon damage call like so:

b.def <= (Math.randomInt(20) + 1 + (Math.floor((MOD – 10)/2)) + (Math.floor(a.level/2))) ? a.weaponDamage(b) : 0;

Whew, thanks for the quick turn around on the reply – now it works like a charm! I actually shortened up the formula you posted, and ended up with:

b.def <= (Math.randomInt(20) + 1 + a.atk) ? a.weaponDamage(b) : 0;

I'm trying to replicate a D&D / Wizardry style of combat ala d20 rolls, and your help has been invaluable in that regard; I've spent more than the last week looking up different plugins, tutorials, etc. but yours did the trick (now I plan on just using atk/def for AB and AC, respectively – MUCH simpler)! So, in the event that I ever finish my game – you, sir, have earned yourself a spot in the credits! 🙂

One more thing, if you know of any formula and/or configuration tweak that would suit the need – how would I go about making AC a negative value, ala old-school D&D / Wizardry..? I don't need the actual formulas I use to be negative numbers (and as a scripting novice, I can't even wrap my head around how that'd even work – seems like it'd just explode lol), but would like to display AC as a descending value instead of a positive one, if possible.

Thanks again – and whether you can help with the above request or not, you've already made by day!

Hm. A descending AC value – but only in the display? It would almost certainly have to be some manner of Plugin to alter how the Defense displays on the status screen. I haven’t a TON of experience with altering the UI via Plugins unfortunately.

Most of my plug in work has been behind the scenes stuff. Mostly fleshing out the above concept into a full combat revamp in an easy to use plugin.

Sorry I can’t be of more help on that one.

Hey, like I said – no worries! You’ve already helped me establish a core part of what I was trying to achieve, and that’s invaluable in how much time/effort it’s going to save me.

I’ll just stick to normal ascending AC until I figure something else out, I just wanted the value displayed for nostalgia’s sake.

Besides that, I’m pretty psyched – I just figured out how to spawn random enemies in battles w/ turn 0 troop manipulation! ^_^

Thanks again, and I’ll certainly be keeping an eye out for that plugin! 🙂