# Combat formulas, divide by zero issue... AND MORE!

This topic is 3632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I am using a basic combat formula which takes the attackers weapon skill and checks it against their defenders weapon skill to figure out the chance to hit: chance = attacker.getWeapon().getBaseSkill() / (defender.getWeapon().getBaseSkill()*2); So basically if we both have the same skill, it is a 50/50 chance. If I am higher, I gain an advantage, if they are higher, I am at a disadvantage. This works well, EXCEPT, what if the users (or defenders) weapon skill is less than 1? What if they have ZERO ability, as in they just picked up a sword, and goes to kill 10 rats? Some situations: I have 50 skill, they have 0 = divide by zero (NaN) I have 0 skill, they have 50 skill = I never hit them. I have 0 skill, they have 0 skill = divide by zero (NaN) I have 0 skill, they have .1 skill = I never hit them. I have .5 skill, they have .1 skill = I have a 250% chance to hit... Anyone have any ideas for a peacewise type formula? I am thinking it would have to check: if(attackerSkill == 0)//do this formula if(defenderSkill == 0)//do this other formula if(attackerSkill>0 && defenderSkill>0)//do the standard formula Also just as a fyi, the chance to hit doesn't mean they score a hit, I also check for parry/dodge/block depending on what defence the defender has. Now for the defensive skills... //if defender is using a shield chanceToBlock = (((defender.getSkill(BLOCK)/4)+5)*.01) //the .01 is to use math.random() <= chanceToBlock So as you can see if a user has 100 block, their max chance to block is 30% (before bonuses are applied) I would like to use the attackers attackSkill level in this equation too, but again, what if the attackers skill is 0 or the defenders block skill is 0? How to overcome the divide by zero problem, and make it consistant? Thanks if you can help.

##### Share on other sites
Often problems like this are most easily solved by tweaking your game design. Is there a reason you can't just make the lowest skill level 1? That would seem the obvious solution to me.
Also, if you're concerned about the chance to hit being higher than 100%, note that this is also going to happen whenever the attacker's skill is more than double the defender's skill...

cheers,
metal

##### Share on other sites
Hey thanks for the reply. I might have already found a solution...

Just add + 1 to both skills before doing any formulas.

The problem with the example was that .5 / (.1*2) should not give me a 250% chance, it should be a 68% chance. And the solution of adding +1 fixes this.

((.5+1) / (.1+1)*2) = .68

As far as the >100% issue, I was just going to cap it, so it doesn't matter if it is 100% or 500%.

if(chance > Math.random()*100)

So anything 100% or higher is going to hit.

##### Share on other sites
wouldn't this work?

int totSkill=attacker.getWeapon().getBaseSkill() + defender.getWeapon().getBaseSkill() ;

chance=attacker.getWeapon().getBaseSkill()/totSkill;

This should give you the correct 0 to 1 distribution you're looking for without the divide by zero hassle.. but I am not sure about the 0/0 behaviour.

##### Share on other sites
Hey kunos thanks for the great idea. I can combine that with the +1 to get it working even with the divide by zero error.

Thanks guys.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×