Jump to content

[SOLVED] Calulating max hits with strength vs defence - online game


slyte33

Recommended Posts

I have an online game, and I'm currently making real-time PvP battles, but I cannot seem to figure out a decent formula to calculate how much damage the the players deal based on the defence of the opponent.

 

Example:

 

Player A - 1,000 strength, 500 defence

Player B - 1,500 strength, 1,000 defence

 

Player A's max damage would be 1,000 VS 1 defence, and the minimum damage.. well I already have a good formula to calculate that.

 

What I want Player A's max damage to equal is around 750, due to the opponent having 1,000 defence.

 

Formulas that would not work is subtracting the opponents defence from the attackers strength, else that would mean you'd always hit 1 IF their defence was greater than the strength.

 

I have been trying for 4 hours to calculate this correctly, and will keep going until I get it right, but I was wondering if any geniuses on here could give me a hand with this :)

 

All feedback is much much appreciated :)

 

Thanks.

 

 

 

<?php
function calibrateDamage($attack, $defence) {
  $atkDifference = $attack-$defence;
  if($atkDifference>0) $atkDifference = ($atkDifference/7)*2;
  elseif($atkDifference==0) $atkDifference = ($attack*-.25);
  elseif($atkDifference<0) $atkDifference = ($atkDifference/3)*2;
  echo $atkDifference.' - ';
  return $attack+$atkDifference;
}

echo calibrateDamage(1000, 1000);
echo("<br/>");
echo calibrateDamage(1500, 1000);
?>

 

That's as close as I can get it for you. You can adjust the figures as needed, its pretty simple.

With just a simple addition of a query string we can adjust them as we want to test. I also fixed the <0 issue.

 

<?php
function calibrateDamage($attack, $defence) {
  $atkDifference = $attack-$defence;
  if($atkDifference>0) $atkDifference = ($atkDifference/7)*2;
  elseif($atkDifference==0) $atkDifference = ($attack*-.25);
  elseif($atkDifference<0) $atkDifference = ($atkDifference/3)*2;
  echo $atkDifference.' - ';
  return (($attack+$atkDifference)<0)?0:$attack+$atkDifference;
}

echo calibrateDamage($_GET['atk'], $_GET['def']);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.