Page 1 of 3

PHP "Autoattack" help

Posted: Mon Jun 07, 2010 1:34 am
by Rastan
Yeah.. I'm back with another noob question.

So I started with sort of a base attack system from the MMO Tuts here and stuck in some of the equations to get a result that functioned for the moment. and am trying to "automate" against another player in the player database that gets brought in by GET. I don't necessarily want them for the ease of play in the game(and maybe my brain) to require a response from another player.

I just want them to select the player come to this script and have the players duke it out auto-attack style with some unadded pre fight decisions created later to add a little diversity outside the raw stats. I have had some trouble trying to figure out what method i might use to accomplish this idea. I have gotten a do.. while... approach to mostly work but I am having 1 serious problem with it.

I'll try to explain here as best I can. What I'm attempting here is to get it to make a decision based on which characters agility is higher. "IF" player agi is higher i'm trying to have it DO player attack first enemy attack etc while a check on thier HP is greater than 0 checking each stage if anyones health has dropped below 0. and exiting the code in any outcome once exp and loot has been deposited.

if enemy agi is higher I'm letting enemy attack first then player pretty much the reverse of the other using an "else" before the do...while... (it seems before I added the reverse side it didn't have the problem I'm about to describe if that helps.)

The problem I'm having is that with some echos I've added it will read out like its doing correctly swapping blows until someones health crosses the 0 point.. then declaring them dead and depositing loot and exp correctly end the fight and exit ---- but then it will almost always update one of the players health either "player" or "enemy" back to the initial health they came to the page with in the database . It doesn't seem to have a rhyme or reason for which it will select or maybe it does and I haven't caught on to it. I have tried a good many things as far as variable name changes, different loops , trying to make each set a function(which if thats possible I don't know enough to make it happen yet ) etc to keep it from reverting back to possibly the initial player hp = "X" which was what I thought maybe it was doing but I've gotten nowhere on it or made the reverse of progress.

Sorry about my messy coding and odd comments I have refined nothing on here. Just kinda threw it down to see what I could make happen. It was sorta better organized before I started shredding it to see if I could fix it tho.
If someone can tell me maybe what I might need to do to fix it I would appreciate it. I may just be using the wrong approach to make this happen or missing something simple. Any advice would be helpful.

Code: Select all

<?php
include_once 'connect.php';
session_start( );
include_once 'logo.php';

?>
/////////////Setup---Session start and GET enemy/////////
 <link href="style.css" rel="stylesheet" type="text/css" />
  <body link="blue" alink="blue" vlink="blue">
 <div id = "General1">
        </div>
 <?php
 if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];

}
else
{
  echo "Not Logged in! <br><br> <A href='index.php'>Login?</a>";
}

    ?>
      
      
       <?php

 $playerinfo="SELECT * from player where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
  include_once 'attackstatpanel.php';

 ?>

  <?php
 if (isset($_GET['enemy']))
{
$enemyinfo=$_GET['enemy'];


 $enemyinfo1="SELECT * from player where name='$enemyinfo'";
$enemyinfo2=mysql_query($enemyinfo1) or die("could not get enemy stats!");
$enemyinfo3=mysql_fetch_array($enemyinfo2);
}
else
{
echo"<a href='index.php'>No enemy selected. Go Back!";
}

?>
///////END Setup session start and GET enemy //////////
 <?php
//////////stat setup///////////

$playerhp = $playerinfo3['health'];
$playerstr = $playerinfo3['strength'];
$playerdefense = $playerinfo3['defense'];
 $playeragi= $playerinfo3['agility'];
 $playerlvl = $playerinfo3['level'];
 $playerfirst = 0 ;

$enemy = $enemyinfo3['name'];
$enemyhp = $enemyinfo3['health'];
$enemystr = $enemyinfo3['strength'];
$enemydefense = $enemyinfo3['defense'];
 $enemyagi= $enemyinfo3['agility'];

$enemylvl= $enemyinfo3['level'] ;

   ?>
/////////END statsetup//////////
        <div id = "attackerror">
   <?php

 $hpcheck= $enemyhp >=0 and $playerhp >= 0 ;

///are players dead?///
    if ($enemyhp <=0)
    {echo " You cannot kill the dead!";
     echo "<br><br> <a href='index.php'>Go back!</a>";
  exit;
    }
    else if ($playerhp <=0)
     {echo " You are too badly injured to fight!<br><br>";
          echo " <a href='index.php'>Go back!</a>";
     exit;
     }
    else
    {
     ?> 
/////////END are players dead?/////////
       </div>
     <div style="position:absolute; left:500px; top:220px;overflow:auto;height:300px;width: 500px;">
     
<?php
//////////Agility check to see who goes first//////////
if ($playeragi >= $enemyagi)
{
$attackfirst = $player;
$attacksecond = $enemy;
} 
else
{
$attackfirst = $enemy;
$attacksecond = $player;
}

$i = 0;
// while both player healths are greater than 0

while( $playerhp > 0 && $enemyhp > 0 )
{
  if ($i == 0)
  {
///player agility higher block /////////////
 if ($attackfirst ==  $player)
     {

     $playerattack = (rand(1,200) + $playerstr) -  (rand (1,200) +$enemydefense);
       


if ($playerattack <=0)
{
$playerattack = 0 ;
 }
 
 $enemyhp = $enemyhp - $playerattack;
    
echo "$player hits $enemy for $playerattack damage. Tag 1 Enemy hp is $enemyhp<br>";
      
      $i++;
     }
     else
     {

$enemyattack = (rand(1,200) + $enemystr) -  (rand (1,200) +$playerdefense) ;

   if ($enemyattack <=0)
   {
   $enemyattack = 0;
   }
$playerhp = $playerhp - $enemyattack;

     echo    "$enemy hits $player for $enemyattack damage! tag 2 Player hp is $playerhp.<br>";
       $i++;
     }



  }
///////enemy agility higher block////////
else
{

   if ($attacksecond == $player)
   {

     $playerattack = (rand(1,200) + $playerstr) -  (rand (1,200) +$enemydefense);
       


if ($playerattack <=0)
{
$playerattack = 0 ;
}

 $enemyhp = $enemyhp - $playerattack;
    
echo "$player hits $enemy for $playerattack damage.  tag 3 Enemy hp is $enemyhp<br>";
$i = 0;
}
     else
     {

$enemyattack = (rand(1,200) + $enemystr) -  (rand (1,200) +$playerdefense) ;

   if ($enemyattack <=0)
   {
   $enemyattack = 0;
   }
$playerhp = $playerhp - $enemyattack;

     echo    "$enemy hits $player for $enemyattack damage! tag 4   Player hp is $playerhp.<br>";
     
     $i = 0;
     }

}
}

////////end of "while"/////////////

/////Checks if player is dead and updates healths/////
if ($playerhp <= 0)
{
  $playerhp = 0;
  
  $updateplayer="update player set health='$playerhp' where name='$player' limit 1";
  mysql_query($updateplayer) or die("could not update player hp!  ");
   
   $updateenemy="update player set health='$enemyhp' where name='$enemy' limit 1";
  mysql_query($updateenemy) or die("could not update enemy hp!");

 echo "You have been killed by $enemy!";
   /////////// EXP and loot/////////////////////////
         if ($playerinfo3['level'] > $enemyinfo3['level'])
        {
         $firstmod = $playerinfo3['level'] - $enemyinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $enemyinfo3['level'];
         $totalexper =($enemyinfo3['experience'] - $thirdmod) *4;
        } 
        else
        {
         $firstmod = $enemyinfo3['level'] - $playerinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $enemyinfo3['level'];
         $totalexper =($enemyinfo3['level'] + $thirdmod) *4;
        }

               $totalexper = (int)$totalexper;
         if($totalexper <1) 
         {
           $totalexper = 1;
         }

           echo "<br><b><big>$enemy  defeats you and gains " . $totalexper . " . experience</b></big><br>";
      $updateenemy1="update player set experience=experience +'$totalexper'where name='$enemy'";
  mysql_query($updateenemy1) or die("Could not update enemy");

      ////player--->enemyloot///
  
  $playerstone= $playerinfo3['stonesnow'];
  $loot= ($playerstone * .10) ;
  $loot= (int)$loot;
      if ($loot <= 0)
      {
        $loot = 0;
      }

           $updateenemystone="update player set stonesnow=stonesnow +'$loot'where name='$enemy'";
  mysql_query($updateenemystone) or die("Could not update enemy");
   
   $updateplayerstone="update player set stonesnow=stonesnow -'$loot'where name='$player'";
  mysql_query($updateplayerstone) or die("Could not update enemy");
        
        
  echo "<br><b><big>$enemy  loots " . $loot . " stone off your body !</b></big><br>";
   
    exit;
    }
///////////////end EXP and loot///////////


////////////////Checks if enemy is dead/////////////
 if ($enemyhp <=0)
  {
    $enemyhp = 0;

    $updateenemy="update player set health='0' where name='$enemy' limit 1";
  mysql_query($updateenemy) or die("could not update enemy hp!");

  $updateplayer="update player set health='$playerhp' where name='$player' limit 1";
  mysql_query($updateplayer) or die("could not update player hp!  ");
/////////////EXP and loot//////////////
    echo "You have killed $enemy!";

           ////exp/////
        if ($playerinfo3['level'] > $enemyinfo3['level'])
        {
         $firstmod = $playerinfo3['level'] - $enemyinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $enemyinfo3['level'];
         $totalexper =($enemyinfo3['experience'] - $thirdmod) *4;
        }
        else
        {
         $firstmod = $enemyinfo3['level'] - $playerinfo3['level'];
         $secondmod = $firstmod * 10 ;
         if ($secondmod > 90){$secondmod = 90;}
         $thirdmod = ($secondmod / 100) * $enemyinfo3['level'];
         $totalexper =($enemyinfo3['level'] + $thirdmod) *4;
        }
          $totalexper = (int)$totalexper;
         if($totalexper <1)
         {
           $totalexper = 1;
         }
          echo "<br><b><big>You gain " . $totalexper . " experience.</b></big><br>";
          $updateplayer="update player set experience=experience +'$totalexper'where name='$player'";
          mysql_query($updateplayer) or die("Could not update player");


  
  
  $enemystone= $enemyinfo3['stonesnow'];
  $loot= ($enemystone * .10) ;
  $loot= (int)$loot;
   if ($loot <= 0)
      {
        $loot = 0;
      }
           $updateplayerstone="update player set stonesnow=stonesnow +'$loot'where name='$player'";
           mysql_query($updateplayerstone) or die("Could not update enemy");
   
            $updateenemystone="update player set stonesnow=stonesnow -'$loot'where name='$enemy'";
             mysql_query($updateenemystone) or die("Could not update enemy");
  echo "<br><b><big>You find  " . $loot . " stone on $enemy !</b></big><br>";

         exit;
        }
   }
?>



Re: PHP "Autoattack" help

Posted: Tue Jun 08, 2010 10:14 pm
by Rastan
It's been like 3 hours since I peed in the pool..... safe to jump in lol Not looking for anyone to hold my hand even something general like... hey! trying to do it with do while is dumb try this method will help me. I'm stumped on it.

Re: PHP "Autoattack" help

Posted: Tue Jun 08, 2010 10:42 pm
by Jackolantern
I have been planning to pull up my sleeves and dig into it to see what is going on when I had enough time. Unfortunately, auto-attack processes in PHP are complex, the script is fairly long and mostly uncommented, so that may be scaring some people away from trying to fix it. If no one else has gotten a solution, I should be able to check it out this evening :)

Re: PHP "Autoattack" help

Posted: Tue Jun 08, 2010 10:49 pm
by Chris
You're taking the right approach, I'm not going to read your code, it's way too messy.

Here's an idea to point you in the right direction

Code: Select all

<?php
// Example variables
$yourHp = 2000;
$enemyHp = 2000;
$yourAgility = 50;
$enemyAgility = 49;
$yourId = 1;
$enemyId = 2;

// Check who has higher agility
if( $yourAgility >= $enemyAgility )
{
    $firstAttacker = $yourId;
    $secondAttacker = $enemyId;
}
else
{
    $firstAttacker = $enemyId;
    $secondAttacker = $yourId;
}

$i = 0;

// while both id's are greater than 0
while( $yourHp > 0 && $enemyHp > 0 )
{
    if( $i == 0 )
    {
        $damage = ceil(rand(0, 100));
        if( $firstAttacker == $yourId )
        {
            $enemyHp = ($enemyHp-$damage);
        }
        else
        {
            $yourHp = ($yourHp-$damage);
        }
        echo (( $firstAttacker == $yourId ) ? 'You' : 'You were' ) .
             " hit $damage damage, leaving" .
             (( $firstAttacker == $yourId ) ? 'your enemy' : 'you' ) .
             " with " .
             (( $firstAttacker == $yourId ) ? $enemyHp : $yourHp ) .
             " HP. <br />";
        $i++;
    }
    else
    {
        $damage = ceil(rand(0, 100));
        if( $secondAttacker == $yourId )
        {
            $enemyHp = ($enemyHp-$damage);
        }
        else
        {
            $yourHp = ($yourHp-$damage);
        }
        echo (( $secondAttacker == $yourId ) ? 'You' : 'You were' ) .
             " hit $damage damage, leaving" .
             (( $secondAttacker == $yourId ) ? 'your enemy' : 'you' ) .
             " with " .
             (( $secondAttacker == $yourId ) ? $enemyHp : $yourHp ) .
             " HP. <br />";
        $i = 0;
    }
}

// Set hp to 0 before putting into database to prevent negative numbers
if( $yourHp <= 0 )
{
    $yourHp = 0;
    echo "You lost the fight";
}
if( $enemyHp <= 0 )
{
    $enemyHp = 0;
    echo "You won the fight";
}

// put shit into database (remover the #)
# mysql_query("UPDATE `table` SET `hp` = '$yourHp' WHERE `id` = '$yourId'");
# mysql_query("UPDATE `table` SET `hp` = '$enemyHp' WHERE `id` = '$enemyId'");
?>
Here's the result

Code: Select all

You hit 26 damage, leavingyour enemy with 1974 HP. 
You were hit 26 damage, leavingyou with 1974 HP. 
You hit 3 damage, leavingyour enemy with 1971 HP. 
You were hit 1 damage, leavingyou with 1973 HP. 
You hit 77 damage, leavingyour enemy with 1894 HP. 
You were hit 56 damage, leavingyou with 1917 HP. 
You hit 93 damage, leavingyour enemy with 1801 HP. 
You were hit 86 damage, leavingyou with 1831 HP. 
You hit 67 damage, leavingyour enemy with 1734 HP. 
You were hit 22 damage, leavingyou with 1809 HP. 
You hit 63 damage, leavingyour enemy with 1671 HP. 
You were hit 4 damage, leavingyou with 1805 HP. 
You hit 67 damage, leavingyour enemy with 1604 HP. 
You were hit 59 damage, leavingyou with 1746 HP. 
You hit 61 damage, leavingyour enemy with 1543 HP. 
You were hit 68 damage, leavingyou with 1678 HP. 
You hit 100 damage, leavingyour enemy with 1443 HP. 
You were hit 51 damage, leavingyou with 1627 HP. 
You hit 85 damage, leavingyour enemy with 1358 HP. 
You were hit 35 damage, leavingyou with 1592 HP. 
You hit 37 damage, leavingyour enemy with 1321 HP. 
You were hit 33 damage, leavingyou with 1559 HP. 
You hit 79 damage, leavingyour enemy with 1242 HP. 
You were hit 15 damage, leavingyou with 1544 HP. 
You hit 1 damage, leavingyour enemy with 1241 HP. 
You were hit 89 damage, leavingyou with 1455 HP. 
You hit 42 damage, leavingyour enemy with 1199 HP. 
You were hit 68 damage, leavingyou with 1387 HP. 
You hit 63 damage, leavingyour enemy with 1136 HP. 
You were hit 53 damage, leavingyou with 1334 HP. 
You hit 21 damage, leavingyour enemy with 1115 HP. 
You were hit 2 damage, leavingyou with 1332 HP. 
You hit 45 damage, leavingyour enemy with 1070 HP. 
You were hit 9 damage, leavingyou with 1323 HP. 
You hit 12 damage, leavingyour enemy with 1058 HP. 
You were hit 78 damage, leavingyou with 1245 HP. 
You hit 19 damage, leavingyour enemy with 1039 HP. 
You were hit 94 damage, leavingyou with 1151 HP. 
You hit 62 damage, leavingyour enemy with 977 HP. 
You were hit 4 damage, leavingyou with 1147 HP. 
You hit 7 damage, leavingyour enemy with 970 HP. 
You were hit 90 damage, leavingyou with 1057 HP. 
You hit 69 damage, leavingyour enemy with 901 HP. 
You were hit 40 damage, leavingyou with 1017 HP. 
You hit 81 damage, leavingyour enemy with 820 HP. 
You were hit 31 damage, leavingyou with 986 HP. 
You hit 78 damage, leavingyour enemy with 742 HP. 
You were hit 96 damage, leavingyou with 890 HP. 
You hit 62 damage, leavingyour enemy with 680 HP. 
You were hit 2 damage, leavingyou with 888 HP. 
You hit 87 damage, leavingyour enemy with 593 HP. 
You were hit 30 damage, leavingyou with 858 HP. 
You hit 23 damage, leavingyour enemy with 570 HP. 
You were hit 39 damage, leavingyou with 819 HP. 
You hit 42 damage, leavingyour enemy with 528 HP. 
You were hit 52 damage, leavingyou with 767 HP. 
You hit 86 damage, leavingyour enemy with 442 HP. 
You were hit 25 damage, leavingyou with 742 HP. 
You hit 41 damage, leavingyour enemy with 401 HP. 
You were hit 22 damage, leavingyou with 720 HP. 
You hit 21 damage, leavingyour enemy with 380 HP. 
You were hit 94 damage, leavingyou with 626 HP. 
You hit 31 damage, leavingyour enemy with 349 HP. 
You were hit 49 damage, leavingyou with 577 HP. 
You hit 51 damage, leavingyour enemy with 298 HP. 
You were hit 30 damage, leavingyou with 547 HP. 
You hit 8 damage, leavingyour enemy with 290 HP. 
You were hit 92 damage, leavingyou with 455 HP. 
You hit 49 damage, leavingyour enemy with 241 HP. 
You were hit 69 damage, leavingyou with 386 HP. 
You hit 19 damage, leavingyour enemy with 222 HP. 
You were hit 60 damage, leavingyou with 326 HP. 
You hit 35 damage, leavingyour enemy with 187 HP. 
You were hit 95 damage, leavingyou with 231 HP. 
You hit 61 damage, leavingyour enemy with 126 HP. 
You were hit 13 damage, leavingyou with 218 HP. 
You hit 83 damage, leavingyour enemy with 43 HP. 
You were hit 41 damage, leavingyou with 177 HP. 
You hit 82 damage, leavingyour enemy with -39 HP. 
You won the fight
Not bad for something wrote in 10 minutes.

Re: PHP "Autoattack" help

Posted: Wed Jun 09, 2010 12:18 am
by Rastan
Thanks a lot Chris! I agree, my code does look like hamburger after trying different things to get it to work out for me and moving/adding/removing things lol.

I'll give the way you did it a try. I was tryin to think of a way to do what you did there but my newbness at coding left me short of seeing a way like this here. I did try while but I think I was tryin to be too complex with it.


@Jackolantern: Thanks for all the help you have given me already. I appreciate ya lookin out for me.

I'd like to say I won't have a lot of questions but once I : research it a bit, trial and error write it to get it to work, find a problem, research the problem, try numerous things to fix the problem, and still fall short I usually ask here. I've unfortunately found with my learning that as I go I kinda take a long time to put things all together. Once they do click tho I'm golden and the whole package is visible to me. Until then tho, thanks a lot for the help!

Re: PHP "Autoattack" help

Posted: Wed Jun 09, 2010 1:18 am
by Callan S.
Rastan wrote:The problem I'm having is that with some echos I've added it will read out like its doing correctly swapping blows until someones health crosses the 0 point.. then declaring them dead and depositing loot and exp correctly end the fight and exit ---- but then it will almost always update one of the players health either "player" or "enemy" back to the initial health they came to the page with in the database . It doesn't seem to have a rhyme or reason for which it will select or maybe it does and I haven't caught on to it.
It might be too simple, but are you updating the HP stat in the database, or are you just updating the HP variable in the session?

Re: PHP "Autoattack" help

Posted: Wed Jun 09, 2010 2:05 am
by Rastan
It is supposed to be updating healths in the database. I have an sql update set to do it. I am currently trying out the advice I got from Chris. I have rewritten most of it now and am testing. It seems to be working most of the time but I have had a few hiccups where it is not updating and leaving health at the initial set health, so I'm trying to pin down when and why . Will update this again if I find a pattern with new ( maybe better noted) source if I can't figure it out again.

Re: PHP "Autoattack" help

Posted: Wed Jun 09, 2010 6:00 am
by Rastan
Ok so I tried something like what Chris told me and it works as far as the parsing like mine did. Still not updating database correctly. I re-posted the code I am using now in the original post and deleted the other. I also added notes for every major transaction on the page.. so i hope that helps someone figure out what my jumble of code says. The longest part is the exp/loot distribution placeholders can probably ignore them.

It has no rhyme or reason when it will or wont write the correct information the the database still. I've documented a case of every possibility from the agility check and who wins. 1 was correct out of all of them. Interestingly when I have my wife connect up and run that part of the game from her computer it does seem to update more.... but not all the time. Might be dumb luck.

I can make it spit out correct numbers with "echo" just after the queries... Am I doing something wrong there?

Re: PHP "Autoattack" help

Posted: Thu Jun 10, 2010 6:55 am
by Callan S.
but then it will almost always update one of the players health either "player" or "enemy" back to the initial health they came to the page with in the database .
Your using

Code: Select all

if ($playerhp <= 0)
and

Code: Select all

if ($enemyhp <=0)
as the requirements for an update to either player or enemy, since the update is within the brackets of these if statements.

If they aren't at zero, then they will not do any database update and the database will remain at the value it was before (ie, at full hp). It's not updating the database to full hp, it's just not updating the database at all!!

You need to move the update for both parties to the end of the page and not only that, get rid of the exit statments that are contained within the two if statements (otherwise it wont get to the update sometimes). They don't need to exit anyway, AFAICT.

You also, and this is a side issue, need to check if a player has any hitpoints before they attack, otherwise your going to get double kills sometimes as a dude with no hp attacks, gets lucky, and puts down the other guy - then both dudes, as dead bodies, will loot each other! Unless you intended this, in which case that sounds sick in a cool sort of way! I'm all for new ways of doing things, as long as it wasn't an error.

Hope this helps - I've been wracking my brain over your code like it was a hard seduku puzzle, and I'm kind of in a boxing mood now! :twisted: :lol:

Re: PHP "Autoattack" help

Posted: Thu Jun 10, 2010 7:22 am
by Chris
Callan S. wrote:Edit: Ooops, I was reading Chris' code by accident (damn ctrl-f!), will edit shortly!

Well, looking at what I use (and this is essentially duplicating Halls code) I use code like this

Code: Select all

 $updateplayer="update players set hpoints='$newplayerhp' where name='$player'";
mysql_query($updateplayer) or die("Could not update player");
I guess putting the command in a string doesn't matter much, but...

Code: Select all

mysql_query("UPDATE `table` SET `hp` = '$yourHp' WHERE `id` = '$yourId'");
you've put little talking marks like ' around table, hp and id. In my code, it doesn't have that. Try stripping out those extra ', perhaps?

Code: Select all

mysql_query("UPDATE table SET hp = '$yourHp' WHERE id = '$yourId'");
mysql_query("UPDATE table SET hp = '$enemyHp' WHERE id = '$enemyId'");
I'm assuming "table" is where your keeping the player data and the temporary data for the players current enemy?
Those quotations are actually the proper way of doing it.