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.

 

 

 

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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']);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.