Advertisement

[PHP] Miss & Critical Hit Chance formulas

Started by June 12, 2015 06:32 PM
9 comments, last by ferrous 9 years, 6 months ago

Trying to come up, and do, formula's for the players & mobs miss chance & critical hit chance when fighting.
My primary stats are Attack & Defense.
There is Accuracy & Dodge secondary stats which can increase by the equipment they are using.

This Accuracy & Dodge stats are numbers that adds up to the total number, not a percent, so I would need to come up with a formula to convert them to a percentage.

For example:
The players total Accuracy is 125 from his stat and the extra accuracy on his gear.
Then divide 45 (random number in my head) by 125 and the attackers Accuracy is 2.777%

I'd need to do the same thing with Dodge and then compare the attackers accuracy with the defenders dodge.

I would subtract the Accuracy percentage from the Base Miss, which I was thinking would be 5% by default.


The flow of the battle is:

Attacker attacks the Defender.
Defender has a 10% chance to dodge.
Attacker has a 5% base chance to miss.

Generate random number from 1-100.
If number is 1-5 then the attacker misses.
If the number is 6-15 then the defender dodges.
The remaining (16-100) is a hit.

Fairly easy, but the problem I am having is players can increase their Accuracy and Dodge chances with better gear.

Maybe I would have to cap those stats to make sure they don't go above 100% total?

Would be great if somebody could help me out with this.

Looks like more of a design question -- figuring out damage rules generally -- and not a programming question. Moving.

Advertisement

Now that it is in design, different game designs do different formulas.

For example, D&D uses a combination of armor class, level, and weapon modifiers. You add them all up and get a number. Then the person rolls a 20 sided die. If the rolled number is equal or greater, then you hit. If the number is less, you missed. There is no cap, it is possible for a high-level versus low-level to have a number greater than 20, or lower than 1. When it exceeds the boundary it is a guaranteed miss or hit. A high level player versus a kobold is likely a guaranteed hit (value is one or less). A low-level player versus an adult black dragon is likely a guaranteed miss (value is over 20).

Looks like more of a design question -- figuring out damage rules generally -- and not a programming question. Moving.


I first encountered this in Game Design, and I was thinking it looked more like a programming question.
But I agree that designers should be able to work out this sort of formula in the design process.

-- Tom Sloper -- sloperama.com

Are attack and defense used for damage and damage-absorption? How about adding a speed-stat, when checking for hit compare the attackers accuracy+speed against the defenders dodge, and when checking for critical compare the attacker's accuracy against the defender's dodge+speed.

Hello Draven,

Acc = Attacker's Accuracy

Eva = Defender's Evasion

fAcc = effective Accuracy (i.e., the chance of Attacker to hit Defender)


The players total Accuracy is 125 from his stat and the extra accuracy on his gear.
Then divide 45 (random number in my head) by 125 and the attackers Accuracy is 2.777%

I'd need to do the same thing with Dodge and then compare the attackers accuracy with the defenders dodge.

Converting Acc and Eva into percentages is superfluous. What matters is that fAcc falls between 0 and 1 (i.e., 0% and 100%). A simple formula Acc/(Acc+Eva) will for example always give a number between 0 and 1. If you use another formula like (Acc-Eva)*x where the result can be below 0 or above 1, you can simply determine that fAcc is 0 or 1 respectively if the result falls out of range.

You can similarly determine the solution set for fAcc to be between 0.25 and .95 for example (i.e., the minimum hit chance is 25%, the maximum hit chance is 95%).

I'm not a programmer, but that should be something as simple as if fAcc<0, then fAcc=0 and if fAcc>1, then fAcc=1.

Look at how other games solve fAcc for some inspiration, such as on http://finalfantasy.wikia.com/wiki/Accuracy.


How about adding a speed-stat, when checking for hit compare the attackers accuracy+speed against the defenders dodge, and when checking for critical compare the attacker's accuracy against the defender's dodge+speed.

A Speed stat like that does not solve the problem at hand.

Cheers,

Chris

Advertisement

Thank you very much Nyaanyaa. That all makes a lot of sense. Very helpful!

Why have critical hits in the game at all? Critical hits have always seemed odd to me. Is it just that players are excited to get the lucky hit?

I think the hit and miss math in my game will be really simple. Accuracy - Protection/Dodge = % to hit. So you might want an accuracy above 100% to hit enemies with really high protection. For a little more realism, a game might make attacks respond differently to dodge. Like gun accuracy being reduced by cover and distance, while melee accuracy is reduced by the opponents dodge or melee skill. So maybe gun accuracy would be reduced by Dodge/5, or is only reduced by dodge if dodge is over 50%.

I'd do a separate roll for crit percentage if the attack hit. A board game would benefit from putting the hit chance and crit chance into a single roll to reduce the amount of dice rolling required, but with a computer, just make the math easier and calculate crit chance separately if there's a hit.

Radiant Verge is a Turn-Based Tactical RPG where your movement determines which abilities you can use.


Why have critical hits in the game at all? Critical hits have always seemed odd to me. Is it just that players are excited to get the lucky hit?

It also adds uncertainty that affects decision making. Like do I use this sword with 4 base damage; or do I use this axe with 3 base damage, and a 40% chance to deal critical damage? If we say a critical hit deals twice as much damage, the axe would effectively deal 4.2 damage per hit, but the sword would be more reliable. But player excitement is certainly a big reason for implementing critical hits. They tend to come with big numbers and satisfying sound effects, too. This reinforces the behavior (i.e., attacking enemies). In psychology, this is called a variable ratio schedule of reinforcement. It's similar to using accuracy/evasion instead of making every hit connect.

Edit: Interestingly, Persona 4 (PS2) uses lookup tables in some way. You can test this using an emulator like PCSX2. Save the game state at the beginning of a battle, then fight for a few turns. Load the game state from the beginning of the battle, and do the same actions in the same order. You will see the exact same numbers, misses, critical hits, AI behavior ... Try another order of commands, load the game state and repeat, and it will be the exact same.

The reason for crits is it also widens the potential "build" options for players. Traditionally speaking it means you can begin building a character that is your "sneaky rogue" focused on low damage but high crit/crit% - You certainly could get that same impact with other mechanics, but the point is more that in most RPG settings you want to give the apparent option to build the character how you want it. The end result of using random mechanics that have large impacts is a much sharper random curve... or to put it bluntly in D&D terms:

You never want to fight anyone holding a Heavy Pick. Not because it's the best weapon, but because there's basically a 5% chance it will outright kill you... :D

Anyway to go back to the original question:

I say go for a contested dice roll rather than the somewhat convuluted formula you seem to be working on. It works as this:

The "to hit" die is rolled by the attacker, adding in the accuracy. This die can be based on the "Attack" value (or, instead, you could use the attack value as a bonus to the randomness of the die).

The enemy then rolls a defense die, adding in the defense value, same as what you do above you either increase the die value or add to it more for higher defense/dodge.

Higher number wins, the attack hits.

2 examples are as follows:

Version 1- My base attack is 10. I have +2 accuracy from equipment (not + attack!). I roll 1d10+2 (That +2 is a really good item!). The enemy is base defense 8 but has no equipment for bonus dodge. They roll 1d8. Whomever gets the higher number wins the contest. The end result is 3-12 vs.1-8.

In this scenario misses will be very frequent due to a very flat set of numbers, however, if its a game where you can use thing like "multiple attacks decrease defense" or "flanking rules" or other tactical scenarios then you can make each of those factors become a major source of impact. When you're rolling 1d10+6 vs 1d8 you're suddenly hitting an awful lot!

Version 2- My base attack is 20, with +4 from equipment. My opponent is base defense 12. I roll 1d10 (or whatever die you pick) + 24, they roll 1d10+12. (25-34 vs. 13-22)

In this version the die you select is static, so you'll end up with "impossible to hit" or "impossible to miss" scenarios a lot quicker than version 1. Also note in this version accuracy is worth less than attack, since it impacts other things, whereas in version 1 it is worth more (but only impacts attack).

After a hit you then use some kind of random fomula (% chance modified by Attack power, or even another contested roll...) for crit.

And so on, you can come up with other things that interrupt before the attack roll like dodge or rules for what is a "Dodge" vs "Deflect" based on how close the conteste roll is. You win a defense roll by 10 you dodge, by 2 the attack hits your shield, etc ... all kinds of interested mechanics can flow out of contested rolls. They're often avoided in Pen and Paper because they take longer, but PCs dont have this limit!

This topic is closed to new replies.

Advertisement