Help cleaning out a script

Post all your tuts or request for tuts here.
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Help cleaning out a script

Post by Dizuki »

Ok i need some help cleaning out a script. I'm not quite sure whats needed and whats not and the scripts getting kinda long and its getting hard to debug so i want some pointers as to what i need to do to better my scripts. I don't want someone to do it for me because i want to learn. But simply point out some things i can/need to change and why.

*edited with more notes

Code: Select all

/////////startup///////
<?php
include_once 'connect.php';
session_start ();
    if  (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
   echo "Not Logged in <br><br> <a href='login.php'>Login<a/>";
   exit;
}

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

    if (isset($_GET['creature']))
    {
    $creature=$_GET['creature'];
    $creatureinfo="SELECT * from creatures where name = '$creature'";
    $creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!");
    $creatureinfo3=mysql_fetch_array($creatureinfo2);

    }
    else
    {
      echo "<a href='battle.php'>No Creature selected. Go Back!";
      exit;
    }

    /////Player stats equations/////

    $physicald = $playerinfo3['pattk']*3 / ($creatureinfo3['pdef']+1);
    $laserd = $playerinfo3['lattk']*3 / ($creatureinfo3['ldef']+1);
    $acidd = $playerinfo3['aattk']*3 / ($creatureinfo3['adef']+1);
    $explotiond = $playerinfo3['eattk']*3 / ($creatureinfo3['edef']+1);
    $playerattack = $physicald+$laserd+$acidd+$explotiond;
    $hitpower = (($playerinfo3['dexter']*10)+rand(1,20)) / ($creatureinfo3['tough']+rand(1,20));
    $attackpower = $playerattack * $hitpower;
    $playerhp= $playerinfo3['hpoints'];
    /////creature stat equations////
    $cphysicald = $creatureinfo3['pattk']*3 / ($playerinfo3['pdef']+1);
    $claserd = $creatureinfo3['lattk']*3 / ($playerinfo3['ldef']+1);
    $cacidd = $creatureinfo3['aattk']*3 / ($playerinfo3['adef']+1);
    $cexplotiond = $creatureinfo3['eattk']*3 / ($playerinfo3['edef']+1);
    $creatureattack = $physicald+$laserd+$acidd+$explotiond;
    $chitpower = (($creatureinfo3['dexter']*10)+rand(1,20)) / ($playerinfo3['tough']+rand(1,20));
    $cattackpower = $creatureattack * $hitpower;
    $creaturehp= $creatureinfo3['hpoints'];




 ///////////////////////players turn////////////////////
 /////Hit damage brakets///////
do{
$hittype = rand(1,100);
$hittype2=($hittype);
    
    if($hittype2<=5)
    {
    $playerdamage=0;
    }
    elseif($hittype2<=15)
    {
    $playerdamage=$attackpower * .1;
    }
    elseif($hittype2<=30)
    {
    $playerdamage=$attackpower * .3;
    }
    elseif($hittype2<=45)
    {
    $playerdamage=$attackpower * .5;
    }
    elseif($hittype2<=75)
    {
    $playerdamage=$attackpower * .7;
    }
    elseif($hittype2<=90)
    {
    $playerdamage=$attackpower * .9;
    }
    elseif($hittype2<=100)
    {
    $playerdamage=$attackpower * 1.2;
    }

   $playerdamage=(int)$playerdamage;

    
   ////////stat autherazation/////
    echo "<u> " . $playerinfo3['name'] . "'s Attack</u><br>";
     {
      echo $playerinfo3['name'] . " hits! <br>";
       $newcreaturehp = $creatureinfo3['hpoints'] - $playerdamage;
      echo "For " . $playerdamage . " points of damage. <br>";

       }
      $updatecreature="update creatures set hpoints='$newcreaturehp' where name='$creature' limit 1";
      mysql_query($updatecreature) or die("Could not update creature");
    //////////////////////creatures turn //////////////////

///////creature damage braket////////
$chittype = rand(1,100);
$chittype2=($chittype);
    
    if($chittype2<=5)
    {
    $creaturedamage=0;
    }
    elseif($chittype2<=15)
    {
    $creaturedamage=$cattackpower * .1;
    }
    elseif($chittype2<=30)
    {
    $creaturedamage=$cattackpower * .3;
    }
    elseif($chittype2<=45)
    {
    $creaturedamage=$cattackpower * .5;
    }
    elseif($chittype2<=75)
    {
    $creaturedamage=$cattackpower * .7;
    }
    elseif($chittype2<=90)
    {
    $creaturedamage=$cattackpower * .9;
    }
    elseif($chittype2<=100)
    {
    $creaturedamage=$cattackpower * 1.2;
    }

   $creaturedamage=(int)$creaturedamage;

 //////stat autherazation///////
       $newplayerhp = $playerinfo3['hpoints'] - $creaturedamage;
       echo "Creature hit for " . $creaturedamage . " points of damage. <br>";
       

      $updateplayer="update players set hpoints='$newplayerhp' where name='$player'";
      mysql_query($updateplayer) or die("Could not update player");
////////////////////////////////////////////////////////////////////////////////////////

  }while ($playerhp > 0 && $creaturehp > 0);
  
 
 //////////battle end text//////
  if ($newplayerhp < 1)
       {
         echo $playerinfo3['name'] . " has been killed<br>";
          echo "<a href='gameover.php'>Continue";
          exit;
       }

  elseif ($creaturehp < 1)
        {
        {
         echo "The " . $creature . " has been killed";


////////////rewards////////
       {
         $firstmod = rand(1,3);
         $totalexper =$firstmod + $creatureinfo3['exper'];
       }

         echo "<br><b><big> you gained " . $totalexper . "experiance.</big></b></br>";
       $updateplayer=" update players set exper=exper+'$totalexper' where name='$player'";
     mysql_query($updateplayer) or die ("Could not update player");

/////////monster reset//////
   $crmaxhp=$creatureinfo3['maxhpoints'];
       $updatecreature="update creatures set hpoints='$crmaxhp' where name='$creature' limit 1";
   mysql_query($updatecreature) or die("Could not update creature");

        echo "<a href='battle.php'>Go Back";
        exit;

        }
    echo "<br><br><a href='battle.php?creature=$creature'>Battle Again!";
        }
?>
Last edited by Dizuki on Wed Jun 30, 2010 6:08 am, edited 1 time in total.
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Help cleaning out a script

Post by Dizuki »

Oh ps. I know its long and i don't expect anyone to read it the whole way through (very messy) but if you could find one error it would be very helpful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Help cleaning out a script

Post by Jackolantern »

One thing you desperately need to do is go through the comment everything. It is hard to make heads or tails of it at the moment if you aren't familiar with it, and you will be in the same position in a couple of months when you go back and try to read it later. You could also likely break discrete sections of code that perform a single task, or things that could be helpful for other scripts into functions. That will also help organize your code.
The indelible lord of tl;dr
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Help cleaning out a script

Post by Dizuki »

Ok, thanks for the help. I'll go do that.
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: Help cleaning out a script

Post by Rastan »

The first thing I notice is that it looks like you are giving some variables at the top and then giving them again down in creatures turn.You "should" be able to do without giving them again. I think possibly you have given a few attack variables that aren't coming into play. I could be wrong (I looked but not for a long time lol) or you may be wanting to add those in.

The other thing that I notice is that it looks like you could have something like a dead creature attacking a player in a turn before it reaches the health check. Maybe intended?
This problem is why I decided to go with something closer to the script Chris Gave me in the coding---> PHP Autoattack help post on this forum. It checks each individual turn for health and then initiates the next combatants turn \

Also, I noticed is that if the creature was able to take a player below 0 health it would record that negative number. you could fix that by just adding:

Code: Select all

if ($newplayerhp <= 0)
{
  $newplayerhp = 0;
}
This script doesn't look too long to me. My similar "auto-attack" script is 427 lines. I have a lot of checks before combat to see if the player is actually alive and has the required points to do combat and it figures up the enemy gear worth to add to defense and such. . It looks like you have a lot of interesting ideas to make the combat more varied. Good luck with it!
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Help cleaning out a script

Post by Dizuki »

Well the reason why i did this is because for some reason it just keeps cycling non-stop till the server times out. I was hopping it was a simple clutter error tripping up the script. I've done the math and a creature with lest say 30 health will take well over 300 damage. It doesn't happen if i take out the do-wile statement because each turn acts through, but its like the script is missing something, or its misplaced.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Help cleaning out a script

Post by Jackolantern »

It has to be a logic error somewhere, because your code can be as clear or as cluttered as you want. Not that it is good for it to be cluttered, but the PHP engine doesn't care.
The indelible lord of tl;dr
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Help cleaning out a script

Post by Dizuki »

I'm pretty sure it has something to do with the location of the squiggly brackets "{" of my script, i know i have a few misplaced ones from when i added some features and reorganized the code.
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: Help cleaning out a script

Post by Rastan »

Code: Select all

while ($playerhp > 0 && $creaturehp > 0);
I think you may never be changing $playerhp only updating the stats with $newplayerhp. So you announce $player hp = 100 then you change player hp to $newplayerhp and it goes to 0 but $playerhp still = 100. I hope that makes sense.

You may need to just use $playerhp the entire time so that the initial check works as well since telling it that $newplayerhp = $playerhp seems a bit redundant.
Dizuki
Posts: 217
Joined: Tue Jun 22, 2010 9:10 pm

Re: Help cleaning out a script

Post by Dizuki »

Ok it might not seem like it but this code is the mutated brother of the attack script from the tutorial that got edited a little at a time into this, but i guess some of it still remains. But that does make sense. just use 1 hp script. but that still doesn't fix the creature issue. Its probably a similar problem. I need to change $newcreaturehp and just use $creaturehp across the bored, would that work?
Post Reply

Return to “Tutorials”