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;
        }
   }
?>




 
   