Jump to content
MakeWebGames

Attack Script Help


Coly010

Recommended Posts

I'm trying to do my own attack code, and honestly, I'm finding it very difficult, mainly the maths behind it. If someone has a general theory for how an attack script should go then that would be very much appreciated.

But at the moment I'm trying to have it so that there is a chance that when the user attacks, they miss. But i want this chance to depend on the user's agility.

I dont know how to go about this.

Obviously I cant have

if($user['agility'] > 100 ) { // attack }

 

I'm thinking I need to use the $user['agility'] to form a decimal or an odd(chance), but I dont know what I can use to compare it.

I know I could use something like

if(mt_rand(1,2) == 1) { // attack }

but it seems too trivial, means there is only a 50/50 (theoretically) chance and doesn't incorporate the users agility.

any help on this matter would be very much appreciated!

Thanks guys,

Colum

Link to comment
Share on other sites

Now that I've sat for a minute and thought about it, I was thinking, maybe I could use a randomisation, like you said, but in a different kind of way:

$max_rand = floor(1000 / $user['agility']);
$chance = ($max_rand / (floor(0.02 * $user['agility']));
if(mt_rand(0, $max_rand) < $chance ) {
  // attack
}

 

would this be plausible ? I know it would mean setting a cap on the agility stat :/

Link to comment
Share on other sites

$myAgi = (($ir['agility'] / 100) * 2.5);
$enemyAgi = (($r['agility'] / 100) * 2.5);

if($myAgi > $enemyAgi) {
$speed = 1;
} else {
$speed = 0;
}
if($speed > 0){
//you attak
} else {
//they attack
}

I think that can potentiall mean 1 of the 2 people will never be able to attack.

I was thinking more along the lines of:

$dodge = rand(1,6);
if($dodge == 3) {
  //Make the player dodge the attack
}
else {
   //Make them hit
}

You can potentiall use a switch to add more like give some sort of attack multipliers like a critical hit, miss, or some kind of guard increase or something

Link to comment
Share on other sites

I think that can potentiall mean 1 of the 2 people will never be able to attack.

I was thinking more along the lines of:

$dodge = rand(1,6);
if($dodge == 3) {
  //Make the player dodge the attack
}
else {
   //Make them hit
}

You can potentiall use a switch to add more like give some sort of attack multipliers like a critical hit, miss, or some kind of guard increase or something

 

i guess that would work, but i still need to incorporate agility, otherwise one of my only two stats only counts for making the user attack first

Link to comment
Share on other sites

I've had a different idea built off the code snippet i posted. the idea came from kyle mentioning dodge in his post.

 

$max_rand = floor((100*$user['agility'] )/ $opp['agility']);
$chance = floor(($opp['agility'] / (0.2 * $user['agility'])));
if(mt_rand(0, $max_rand) < $chance ) {
  // miss
}

 

right so basically, it works like this. the stronger your opponent is, the greater their agility is going to be, the bigger that agility, the smaller $chance is going to be.

ill give a few example values

EG

$u['agility'] = 20; $o['agility'] = 15;
$max_rand = 2000 / 20 = 100;
$chance = floor(15 / (20*0.2) ) =  3;
if(mt_rand(0, 100) < 3){
  // opponent dodges
}

 

basically this means the higher the users agility, the greater chance they have of attacking (because their agility increases the range for the random while also decreasing the range for the dodge), whilst the greater agility the opponent has, the greater chance they have of dodging (because their agility increases their range for their chance to dodge).

thank god i do maths and further maths at school...

Link to comment
Share on other sites

$max_rand = 2000 / 20 = 100;
$chance = floor(15 / (20*0.2) ) =  3;
if(mt_rand(0, 100) < 3){
  // opponent dodges
}

 

2% chance of dodging everytime?

nope, those values are coming from the users and the opponents agility stats, so it changes depending. i just subbed example values into the code

i have the attack code finished now, thanks for all the help guys

Link to comment
Share on other sites

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.   Paste as plain text instead

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