Calculating Accuracy in an RPG – Damage Calculation

In: Something Something Land|Tips

12 Apr 2010

I finally added the weapon’s accuracy into the battle calculation formula so you or the monster (but I haven’t made the monster fight back yet…) can miss when attacking.  Simply added one line:

if (Math.floor(Math.random()*100) > (acc-1)) dmg = 0;

This is if acc is a number from 0 to 100, 100 being sure to hit and 0 being sure not to.

Which now makes my entire damage calculation formula:

function dmgcalc(who, magic, stat, acc, element) //for who, 0 means you are attacking, 1 means monster is attacking; magic is 0 if its a weapon and 1 if its magic
{
	if (who == 0)
	{
		lvl = y_lvl;
		base = stat;
		if (magic == 0)
		{
			atk = y_atk_base;
			def = o_def_base;
		}
		else
		{
			atk = y_satk_base;
			def = o_sdef_base;
		}
	}
	else
	{
		lvl = o_lvl;
		base = stat;
		if (magic == 0)
		{
			atk = o_atk_base;
			def = y_def_base;
		}
		else
		{
			atk = o_satk_base;
			def = y_sdef_base;
		}
	}
	ch = 1;
	if (Math.floor(Math.random()*100) <= 5) ch = 2; //6% chance of critical hit, make it critical hit if this happens
	randnum = Math.floor(Math.random()*39) + 217;
	type = 1;
	dmg = Math.floor(((Math.floor(Math.floor(((Math.floor((lvl * 2) / 5) + 2) * base * atk) / 50) / def)) + 2) * ch * Math.floor((randnum * 100) / 255) / 100) * type;
	if (Math.floor(Math.random()*100) > (acc-1)) dmg = 0;
	return dmg;
}

Comment Form

About this blog

Sean Madigan is a Senior at Towson University majoring in Computer Science, currently working on a social adaption of the Pokemon franchise integrated into the Facebook platform.

  • radu: Hello, Is it something changed with FB API as I tried a lot of tutorials (also this) and I get: [...]
  • seanmadi: Well there are a ton of big Pokemon MMOs out there with a large player base that are still running. [...]
  • Travis: Interesting idea, but it sounds like a copyright infringement suit waiting to happen. [...]