Today I’ve decided to talk a bit about AC and how it actually works. We recently did a dive through all the hit/ac code and I thought it might be interesting to share.
First, a scenario! A level 50 monster takes a swing at a level 50 player (knight, no buffs) with -70ac. Lets walk through what happens.
First, the game asks “Is the player frozen?” If he is, the attack misses
Next, the game asks “Is the monster a summon AND is the player in town?” If both are true, the attack misses
Next, the game asks “Did the player’s doll proc a dodge?” If this is true, the attack misses
At this point, the game decides the attack occurred in an environment that it can potentially succeed. It sets “hit rate” equal to the monster’s level. In this case, it’s 50.
If it’s a pet it adds bonuses based off pet armor to that “hit rate”, as well as any NPC hit bonuses. In this case, there are none.
Next, it creates an “attacker dice” value by rolling 1-20 and adding it to “hit rate”. This value is what will be compared later to determine if the attack was successful. The game has code that basically says if the monster rolled a 1 it automatically misses, and if it rolled a 20 it automatically hits, both regardless of the target’s AC. It does this by comparing “attacker dice” to “hit rate” (a miss/fumble) and “hit rate +20” (a hit/critical [the code calls it a critical but it is really just a normal hit]) later in the code. This means all players always have at least a 5% base chance to be hit and a 5% base chance to be missed no matter their ac or the monster attacking them. Our monster had a “hit rate” value of 50, so now he has an “attacker dice” between 50-70, but remember if he got a 50 he fumbles and a 70 he criticals.
The game takes that “attacker dice” value and modifies it by the target’s dodge and ndodge (subtracts dodge from “attacker dice”, adds ndodge). Dodge exists basically as two spells, uncanny dodge and mirror image. Both give “+5” dodge. ndodge is negative dodge, and it comes from resist fear. It gives “-5”. These abilities are incredibly powerful, because they impact the value of “attacker dice” BEFORE the game checks if the npc fumbled/critical. This means they influence how often a monster fumbles/criticals before it ever compares to AC (our monster’s 50-70 becomes a 45-65, but is still compared to <=50 or >=70). Uncanny Dodge and Mirror Image are essentially a flat 25% chance to avoid any melee attack, negates any chance the monster had to “critical” avoiding ac, and also provide some influence on attacks that make it to the AC check. On the flip side, Resist Fear does the opposite and makes it so no matter your ac, you will be hit by at least 25% MORE attacks. Since our player is a knight, he has no dodge abilities, so this doesn’t impact it.
Now, the game finally looks at the target’s AC. Everyone has a default “defender dice” value of 10. If your AC is positive (+10-+1, basically naked) it subtracts from 10 to get your “defender dice” value. If your AC is negative, it uses the formula below:
DefenderDice = 10 + _random.nextInt(“Target’s AC”) + 1
This essentially means your “Defender Dice” is between 1o and 1o+your ac. Our scenario, the player has defender dice between 10 and 80.
It’s at this point the game calculates fumble/crit (if the monster got a 50 or a 70), then if it was not either of those, compares “attacker dice” to “defender dice”. Our monster must be between 51-69, while our knight must be between 10-80. If they tied, the attack misses.
As you can see, odds favor the monster hitting. Our knight has about a 23.75% chance to avoid an attack that makes it to the AC check (26.25% overall factoring in crit/fumble) (58% of the time he rolls below the monster’s range and is hit and 12.5% of the time he rolls above the range and is missed. Of the 22.5% of the time he rolls within that range, he will only be higher roughly 50%, meaning 12.25% of the time he is missed and 12.25% he is hit. These are not exact since it doesn’t factor in ties, but it’s fairly close).
Because AC doesn’t ever increase the lower bound of “defender dice”, you pretty much always have some chance to be hit by any monster above level 10, in addition to the inherent 5% critical chance. All AC does is reduce the chance of being hit, without ever negating it. The higher level a monster is, the better chance it has to hit you. If a monster is 30 levels above your ac, it will always hit you unless it rolls the 5% fumble chance.
Dark Elves and Illusionists are the only classes who can completely negate the “possible crit”. As a result, they are the only classes who could in theory make a level 10 or lower monster miss every time. Their dodge ability not only grants them essentially a 30% chance to avoid any attack (25%+5%), it also makes their AC more effective by lowering the monster’s “attacker dice” before it’s compared to their “defender dice”, essentially giving them 5 more effective AC. A dark elf with uncanny dodge and identical AC to our knight has a 57.5% chance to avoid an attack from the same monster. That is an astonishingly high difference.
Note, all of this only applies to melee. If the target is being hit with a ranged attack, the game does a very easy calculation instead. It rolls between 1-100. If your ER is < the roll you get hit, otherwise you are missed. That’s it. Ranged attacks don’t factor in anything else other that ER (for monsters). This means every point of ER is 1% chance to avoid a ranged attack.
I found all of this to be very interesting, hopefully you all do to (and hopefully it’s not too hard to follow)! Add another reason Illu/DE are going to be good!