PHP "Autoattack" help

C++, C#, Java, PHP, ect...
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP "Autoattack" help

Post by Callan S. »

Chris wrote:Those quotations are actually the proper way of doing it.
I was wondering if you might say that, :)

Anyway, edited my post, think I found the issue, listed above!
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: PHP "Autoattack" help

Post by Rastan »

Thank you guys for looking into this so hard for me. I really appreciate it.
I'm working what Callan has said into it now. I had removed the exits soon after posting that code so was set there. I am looking at a way to get the updates to the bottom of the page now. I'm not sure exactly what is meant there but I have an idea so I'll go with it for the moment

It will be a short while before I can test much because I am incorporating weapons/armor and whatnot into the game right now and the code won't actually even run in the state it is in. Hopefully this will go easier on me lol

Thanks again for the help. I'll update with the result once I get it in there.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP "Autoattack" help

Post by Callan S. »

It's just a few changes so I'll give an example of what I meant by changing your code a bit

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;
      // commenting out these updates as they aren't used in my version
      //$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;
      // commenting out these updates as they aren't used in my version
      //  $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;
            }
       }


      // inserting the health update here in my version, to wrap up the whole thing
      $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!");

    ?>
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: PHP "Autoattack" help

Post by Rastan »

ok cool. That's what I did. I haven't got to test it yet cause still putting in the stuff I mentioned earlier. Thanks again for the help!
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP "Autoattack" help

Post by Callan S. »

Glad it helped! Good luck!
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: PHP "Autoattack" help

Post by Rastan »

It is acting better now. It was still failing to write every once in a while. I put another set of updates after it and that hasn't failed yet even if it is not what I really wanted (seems sorta dirty lol ) . I dunno if it is something specifically in my setup or what. I been having some... odd occurrences lately(all characters turning to russian all "$"'s turning to little pairs of dots etc.... maybe I did something bad unintentionally or the kid decided to play with it) so I'll just see what it does in a different environment later because I feel like it should be working now. Thanks a ton for the help you guys.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP "Autoattack" help

Post by Callan S. »

I think it's good to try and break new design into smaller steps that can be verified as working, one at a time, rather than write a big while statement, then write multiple if statements in it, then test the whole thing at once.
it is not what I really wanted (seems sorta dirty lol )
Could you describe what you wanted - I or someone might be able to give an outline for it?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP "Autoattack" help

Post by Jackolantern »

You could also check out Codelobster PHP/JS IDE. You can connect WAMP right into it and get Visual Studio style line-by-line debugging. That is very rare for PHP, and worth its weight in gold when trying to get complex scripts to work.
The indelible lord of tl;dr
Rastan
Posts: 126
Joined: Tue Apr 13, 2010 1:48 am

Re: PHP "Autoattack" help

Post by Rastan »

I just wanted it to update correctly as it is it seems like its just skipping that whole ordeal half the time with just a regular "UPDATE X WHERE Y" statement. Doing that statement 2 times back to back has failed 0 times now . Seems kinda dumb to me but that's how its working at the moment. It has me thinking it may just be the environment I have it running in. I have a hard time believing that it just.... didn't feel like it the first time and that's working properly. lol I may further refine it but atm i have it distributing loot and exp and sending an event message and that's all clockwork.

This has me more boggled then the time Jackolantern sent me an identical line to my code and i could copy paste it in and it would work but if i took my line and pasted it back in it wouldn't lol Even one right on top of the other they were identical. I sometimes suspect that somewhere int he language translation its adding some type of hidden characters.... that's my 1 and only guess lol

I have code lobster. this leads me back to the language thing in what I'm running. It will just up and jump into german.... whenever it wants lol. I do like it but I def don't have the knowledge to use its features to my advantage yet I did once have it change all my "$" to some odd symbol not used in english on several pages. It makes me think it may have installed wrong.

Also... my reguser type code from the tutorials... to log in after it md5 encrypts It swaps letters to numbers. like apple has to be typed in 4ppl3 on login.
For this... I really don't have any ideas.

In conclusion my computer speaks "1337-speak" and it "pwns" me lol. I'll just code it all out get it running kinda like I want and adjust once it's up and running on a host that isn't my confusing mess. Surprisingly I have 0 problems with my computer doing anything else odd. Maybe I need to save all my php files wipe the newer stuff for coding like wamp and text editors and start over fresh. Other alternatives are welcome
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: PHP "Autoattack" help

Post by Callan S. »

At about that point I start putting in echo"*Swear words*"; at various points, to show me where it's actually going and also echo the values that if statements are using, just before the if statement. Also I'd consider making up a new fresh php page and just put in a while statement to a certain value and inside increment that value, then run it at that point. Because atleast then I know that works - indeed I broke down to doing that today when working with a google advert, only to find the advert code had this weird need for a line break (not <BR>) in a certain spot.

You could post the code here again if you want me to have a look again?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Post Reply

Return to “Coding”