Hit / Miss formula, how does it work?

Hello 

I'm having trouble figuring out how the hit / miss formula work in Vx Ace. My problem is that I have 95% Hit and 5% Eva and yet both the party and the enemies miss almost half of the time!. I do know that it will calculate a miss and then an evade and then it will actually do the hit, but the formula doesn't make much sense to me:

#-------------------------------------------------------------------------- # * Apply Effect of Skill/Item #-------------------------------------------------------------------------- def item_apply(user, item) @result.clear @result.used = item_test(user, item) # <= check this skill/item is used or not @result.missed = (@result.used && rand >= item_hit(user, item)) # <= even if used, it's miss or not @result.evaded = ([email protected] && rand < item_eva(user, item)) # <= if not miss, the target evade or not if @result.hit? # <= Used + not missed, + not evaded unless item.damage.none? @result.critical = (rand < item_cri(user, item)) make_damage_value(user, item) execute_damage(user) end item.effects.each {|effect| item_effect_apply(user, item, effect) } item_user_effect(user, item) end endCan someone "debug" this one and tell me step by step what is it doing? What is that "rand"om number there standing for?

Thanks!
The way it works is it rolls to see if you miss first (5% of the time in the default), then it rolls to see if the enemy evades (5%) of the time. This maths out to you should get a hit 95% * 95% = 90.25% of the time with default settings.

There are three possibilities here:

1) Check for other scripts. If you have Yanfly's Extra parameters it messes with your to hit and evade, and that could be your problem.

2) Make sure everyone really does have +95% to hit, and that you didn't set that to something different.

3) Personally, I've seen the RNG roll a lot of 69% in my game when doing this calculation (I set it up to print it out, and that number came up way more than it should statistically). Maybe the RNG is stuck?
It's a bit of a stinker, IMO - the system by default checks if the attacker misses (the @result.missed part), then, IF the attacker hit, whether or not the defender dodges (the @result.evaded part), THEN if the attack neither missed or was evaded, calculates the hit result.
 
Personally, I rewrote the whole segment of script, from a bit higher up, to this:

Spoiler
#--------------------------------------------------------------------------
# * Calculate Hit Rate of Skill/Item
#--------------------------------------------------------------------------
def item_hit(user, item)
rate = (item.success_rate + user.hit) if item.physical? # Physical attack
return rate # Return calculated hit rate
end
#--------------------------------------------------------------------------
# * Calculate Evasion Rate for Skill/Item
#--------------------------------------------------------------------------
def item_eva(user, item)
return eva if item.physical? # Return evasion if physical attack
return mev if item.magical? # Return magic evasion if magic attack
return 0
end
#--------------------------------------------------------------------------
# * Calculate Critical Rate of Skill/Item
#--------------------------------------------------------------------------
def item_cri(user, item)
item.damage.critical ? user.cri * (1 - cev) : 0
end
#--------------------------------------------------------------------------
# * Apply Normal Attack Effects
#--------------------------------------------------------------------------
def attack_apply(attacker)
item_apply(attacker, $data_skills[attacker.attack_skill_id])
end
#--------------------------------------------------------------------------
# * Apply Effect of Skill/Item
#--------------------------------------------------------------------------
def item_apply(user, item)
@result.clear
@result.used = item_test(user, item)
@result.missed = (@result.used && (1+rand(item_hit(user, item))) - (1+rand(item_eva(user, item))) <= 0)
if @result.hit?
unless item.damage.none?
@result.critical = (rand < item_cri(user, item))
make_damage_value(user, item)
execute_damage(user)
end
item.effects.each {|effect| item_effect_apply(user, item, effect) }
item_user_effect(user, item)
end
end
The first part calculates Hit Rate by adding the skill/item's hit probability with the attacker's HIT stat, the second part defines the defender's Evasion Rate as either Magical or Physical, depending on attack type. The two next parts are not relevant to the adjustments, but are in those locations in the Battler script. Then in the last part, it checks whether the attack was a miss by seeing if a random number up to the Hit Rate is smaller than a random number up to the Evasion Rate (the @result.missed = (@result.used && (1+rand(item_hit(user, item))) - (1+rand(item_eva(user, item))) <= 0) line), then if it's a hit, it moves on to damage.

Simplifying the mechanics to a single roll-off instead of two separate, disconnected skill rolls. Yes, this means I have to adjust the accuracy of skills/items and characters' HIT stat down to, well, significantly less and increase the EVA/MEV scores a bit to make it even plausible to miss (seeing as the default numbers would make the roll 195 vs. 5), but that part's *easy*... 🙂
The way it works is it rolls to see if you miss first (5% of the time in the default), then it rolls to see if the enemy evades (5%) of the time. This maths out to you should get a hit 95% * 95% = 90.25% of the time with default settings.

There are three possibilities here:

1) Check for other scripts. If you have Yanfly's Extra parameters it messes with your to hit and evade, and that could be your problem.

2) Make sure everyone really does have +95% to hit, and that you didn't set that to something different.

3) Personally, I've seen the RNG roll a lot of 69% in my game when doing this calculation (I set it up to print it out, and that number came up way more than it should statistically). Maybe the RNG is stuck?
These are the scripts that I currently have:

[IMG]http://puu.sh/gqfgG/a3e7ed964f.png[/IMG]

Most of them are Yanfly's , and the rest doesn't deal with battle. Maybe the battle engine? ... but that wouldn't make sense if the hit formula is being handled in the extra param.

I did check that, the actors don't have any parameters on them, only the classes. They all have 95% Hit, 5% Eva:

[IMG]http://puu.sh/gqfyt/ebb33dc8ed.png[/IMG]

Same for the monsters I'm testing against.

It's a bit of a stinker, IMO - the system by default checks if the attacker misses (the @result.missed part), then, IF the attacker hit, whether or not the defender dodges (the @result.evaded part), THEN if the attack neither missed or was evaded, calculates the hit result.

Personally, I rewrote the whole segment of script, from a bit higher up, to this:

Spoiler
#--------------------------------------------------------------------------# * Calculate Hit Rate of Skill/Item

#--------------------------------------------------------------------------

def item_hit(user, item)

rate = (item.success_rate + user.hit) if item.physical? # Physical attack

return rate # Return calculated hit rate

end

#--------------------------------------------------------------------------

# * Calculate Evasion Rate for Skill/Item

#--------------------------------------------------------------------------

def item_eva(user, item)

return eva if item.physical? # Return evasion if physical attack

return mev if item.magical? # Return magic evasion if magic attack

return 0

end

#--------------------------------------------------------------------------

# * Calculate Critical Rate of Skill/Item

#--------------------------------------------------------------------------

def item_cri(user, item)

item.damage.critical ? user.cri * (1 - cev) : 0

end

#--------------------------------------------------------------------------

# * Apply Normal Attack Effects

#--------------------------------------------------------------------------

def attack_apply(attacker)

item_apply(attacker, $data_skills[attacker.attack_skill_id])

end

#--------------------------------------------------------------------------

# * Apply Effect of Skill/Item

#--------------------------------------------------------------------------

def item_apply(user, item)

@result.clear

@result.used = item_test(user, item)

@result.missed = (@result.used && (1+rand(item_hit(user, item))) - (1+rand(item_eva(user, item))) <= 0)

if @result.hit?

unless item.damage.none?

@result.critical = (rand < item_cri(user, item))

make_damage_value(user, item)

execute_damage(user)

end

item.effects.each {|effect| item_effect_apply(user, item, effect) }

item_user_effect(user, item)

end

end
Spoiler
The first part calculates Hit Rate by adding the skill/item's hit probability with the attacker's HIT stat, the second part defines the defender's Evasion Rate as either Magical or Physical, depending on attack type. The two next parts are not relevant to the adjustments, but are in those locations in the Battler script. Then in the last part, it checks whether the attack was a miss by seeing if a random number up to the Hit Rate is smaller than a random number up to the Evasion Rate (the @result.missed = (@result.used && (1+rand(item_hit(user, item))) - (1+rand(item_eva(user, item))) <= 0) line), then if it's a hit, it moves on to damage.

Simplifying the mechanics to a single roll-off instead of two separate, disconnected skill rolls. Yes, this means I have to adjust the accuracy of skills/items and characters' HIT stat down to, well, significantly less and increase the EVA/MEV scores a bit to make it even plausible to miss (seeing as the default numbers would make the roll 195 vs. 5), but that part's *easy*... 🙂

It doesn't bother me the order of the calculations but, with the percentages as they are, they should not be missing that much. What I would like to understand is why the calculations on the vanilla code are not working as they should. I mean, with the values as they are I would understand it like:

1) I have 95% chance of hit, so it will eval a random number between 0 - 100 and if the result is between 0 and (100 - actor hit) (this case would be 0 - 5) then miss.

2) Lets say the random number was 45, which means it didn't miss. Now it should calculate eva pretty much like the way it did before. Rand between 0 - 100 and if the result is between 0 and Eva (again, 5) it will evade.

If not, then calculate if it actually does dmg, if crit and all that. Now, based on test results, it doesn't seem to work like that 🙁
You have Yanfly's extra params, which calculates the to hit and eva based on a formula in that script and ignores what you set on the actors. You will either need to go and comment out those lines, or remove the extra param's script. That is why you are missing so much, because with the default actors and stats, yanfly evaluates your to hit to be about 66%, and that is what it is using.
You have Yanfly's extra params, which calculates the to hit and eva based on a formula in that script and ignores what you set on the actors. You will either need to go and comment out those lines, or remove the extra param's script. That is why you are missing so much, because with the default actors and stats, yanfly evaluates your to hit to be about 66%, and that is what it is using.
Lol I'm an idiot ... no wonder when I added the extra param scirpt again at the bottom it gave me the stack too deep error ... let me do a quick test and I will reply back

EDIT: Yep, it noticeably improved the hit rate of both my actors and the enemy. I'll mark this as solved and play a little bit more with the extra params. Thanks a lot!
Glad it worked! If you wish to use the to hit part of extra params and not have as much missing, try this line:

:hit_formula => "(n / (100.0 + n)) * 0.20 + base_hit",

That's what I did in my game. It keeps the base to hit you set up (not 2/3's of it like Yanfly's does), then adds on a small bonus that can go up to another 20%. If you have 95% for to hit though and use the default calculation you might want to change that to 0.05 instead of 0.20, since values over 100% are useless with the default engine unless you have states that can lower it in battle.

I do the same thing with my evade:

:eva_formula => "(n / (100.0 + n)) * 0.20 + base_eva",

Just an idea if you want them to be able to change as stats increase.

Original thread: https://forums.rpgmakerweb.com/threads/hit-miss-formula-how-does-it-work.37549/