Heres a dousey. Been at this for hours - getting 505 error


#1

Alright, so I’ve been coding a battle system for my game and here is what I know: Everything works up until the while loop. I also took everything out after the while loop, the while loop is broken. Also took the while loop and left the stuff after, same error. I don’t know which line because it doesn’t tell me, sadly.

class Module_Battle extends Base_Module
{
    /*
      Function: start

    */
    public function start()
    {
        // You may call the requireLogin() function if this module is only available to players who are logged in.
        requireLogin();

        if (isset($_POST['battle_mob']))
        {
          $this->battleMob();
        }

        else
        {
          $query = $this->db->execute('SELECT * FROM `<ezrpg>mobs`');
          $mobs = $this->db->fetchAll($query);
          $this->tpl->assign('mobs', $mobs);
          $this->tpl->display('battle.tpl');
        }
    }

    private function battleMob()
    {

      /* Player variables */
      $player = $this->player;
      $playerhp = $player->max_hp * ceil($players->vitality / 2);
      $playermhp = $player->max_hp * ceil($players->vitality / 2);
      $wepmin = ceil($player->wep1_level * 5 / 2);
      $wepmax = ceil($player->wep1_level * 10 / 2);
      $helmdef = ceil($player->helm_level + 5 * 5 / 3);
      $chestdef = ceil($player->chest_level + 10 * 6 / 3);
      $bootsdef = ceil($player->boots_level + 3 * 4 / 3);
      $playerdmg = ceil($playerstr + rand($wepmin, $wepmax));
      $playerdef = ceil($player->strength + $player->dexterity / 2) + ceil($helmdef + $chestdef + $bootsdef / 3);

      /* Mob variables */
      $mobid = $_POST['selection'];
      $mobhp = ceil(5 * $mobid * 100 / 2);
      $mobmhp = ceil(5 * $mobid * 100 / 2);
      $mobstr = ceil(5 * $mobid);
      $mobagil = ceil(5 * $mobid);
      $mobdex = ceil(5 * $mobid);
      $mobdmg = ceil(pow($mobstr, $mobstr) * pow($mobid, $mobid));
      $mobdef = ceil(($mobstr + $mobdex / 2) + (5 * $mobid / 2));

      /* Drop calculation and value variables */
      #$expgain = $player->exp + 5 * $mobid;
      #$goldgain = $player->money + 2 * $mobid;
      $crychance = rand(1, 100);
      $amechance = rand(1, 200);
      $diachance = rand(1, 400);
      $orbchance = rand(1, 400);
      $rubychance = rand(1, 500);
      $medalchance = rand(1, 1000);



      /*stats and other calculations */
      $playerhits = 0;
      $mobhits = 0;
      $playermiss = 0;
      $mobmiss = 0;
      $playerdamage = 0;
      $mobdamage = 0;
      $rounds = 0;
      $hitfirstrigger = true;


      # START BATTLE LOOP #
      while ($playerhp <= 0 && $mobhp <= 0)
      {
        /* miss, dodge calculations */
        $missmob = rand($player->agility - $mobagi, $player->agility);
        $missplayer = rand($mobagi, $player->agility);


        /* who hits first */
        if ($hitfirsttrigger == true)
        {
          if ($player->agility > $mobagi)
              { $playerturn = true; $hitfirsttrigger = false; }
              else { $mobturn = true; $hitfirsttrigger = false; }
        }

        /* Battle Time! */
        if ($playerturn == true)
          {
            if ($missmob > $player->agility || $missmob <= 0)
            {
              $playermiss += 1;
              $rounds += 1;
              $playerturn = false;
              $mobturn = true;
            }
            else
            {
              $dmgdeltmob = ceil($playerdmg - $mobdef);
              $mobhp = $mobhp - $dmgdeltmob;
              $playerhits += 1;
              $rounds += 1;
              $playerdamage = $playerdamage + $damgdeltmob;
              $playerturn = false;
              $mobturn = true;
            }
        if ($mobturn == true)
          {
            if ($playermiss > $missplayer)
            {
              $mobmiss += 1;
              $rounds += 1;
              $mobturn = false;
              $playerturn = true;
            }
            else
            {
              $dmgdeltplayer = ceil($mobdmg - $playerdef);
              $playerhp = $playerhp - $dmgdeltplayer;
              $mobhits++;
              $round++;
              $mobdamage = $mobdamage + $dmgdeltplayer;
              $mobturn = false;
              $playerturn = true;
            }
          }

          } # END BATTLE LOOP #



            # Battling results #
          $playerresulthp = $playerhp . " / " . $playermhp;
          $mobresulthp = $mobhp . " / " . $mobmhp;

          $roundresult = "You totaled " . $rounds . ".";

          $playerbattle = "You did " . number_format($playerdamage) . "to " . $this->mobs->name . "with your " . $player->wep1_name . ".
            You missed " . $playermiss . "times and hit " . $playerhits . " times.";
          $mobbattle = $this->mobs->name . " has done" . $mobdamage . " to you. Missing " . $mobmiss . " and hitting" . $mobhits .
            " times.";

          $this->tpl->assign("playerresulthp",$playerresulthp);
          $this->tpl->assign("mobresulthp", $mobresulthp);
          $this->tpl->assign('roundresult',$roundresult)
          $this->tpl->assign('playerbattle', $playerbattle);
          $this->tpl->assign('mobbattle', $mobbattle);
          # End Battling Results #

          /* Victory / Defeat Check */
          if ($playerhp <= 0)
            {
              #Defeat
              $victorymsg = "";
              $deathmsg = "You have been defeated.";
              $query = $this->db->execute('UPDATE `<ezrpg>players` SET `latmob`=? `deaths`=deaths+1 WHERE `id`=?', array($mobid, $player->id);
              $this->tpl->assign('deathmsg', $deathmsg);

            }
            else
            {
              #Victory
              $deathmsg = "";
              $victorymsg = "You have defeated " . $this->mobs->name . ". <br /> You have gained " . $expgain . " experience. <br /> You have gained " . $goldgain . " gold.";
              $query = $this->db->execute('UPDATE `<ezrpg>players` SET `lastmob`=?, `exp`=?, `money`=? WHERE `id`=?', array($mobid, $expgain, $player->id));
              $this->tpl->assign('victorymsg');

            }


      $query = $this->db->execute('SELECT * FROM `<ezrpg>mobs`');
      $mobs = $this->db->fetchAll($query);
      $this->tpl->assign('mobs', $mobs);
      $this->tpl->display('battle.tpl');


  }
}

This is pretty long code so, thank you VERY much for your help.


#2

there’s a lot to look through here, but the first thing that sticks out to me is the conditions in your while loop seem to be backwards. currently it is:
while playerhp is less than/ equal to 0 and mobhp is less than/equal to 0
shouldn’t those be greater than 0?


#3

Oooo yup! Of course that doesn’t fix the problem, but thanks for the catch!

I keep reading through this and I’m completely stumped, specifically the while loop. I can’t see and syntax errors. Or anything that would throw an error like that.

Seems like i was missing a }… wether it works or not is a different story, though, lol. However I had to do that the long way with my fingers. Lift up a finger when open, put down a finger when closed. Freakin’ dang made me feel like a child lol.


#4

alright caught something else. don’t know if this will fix everything though

when you define all of the mob stats you define $mobagil but you call $mobagi throughout the while loop


#5

Lol, read the whole thing several times and i still missed that! Thanks. I guess I need to pay more attention to the details :stuck_out_tongue:

Now only this part is wrong:

# Battling results #
      $playerresulthp = $playerhp . " / " . $playermhp;
      $mobresulthp = $mobhp . " / " . $mobmhp;

      $roundresult = "You totaled " . $rounds . ".";

      $playerbattle = "You did " . number_format($playerdamage) . "to " . $this->mobs->name . "with your " . $player->wep1_name . ".
      You missed " . $playermiss . "times and hit " . $playerhits . " times.";
      $mobbattle = $this->mobs->name . " has done" . $mobdamage . " to you. Missing " . $mobmiss . " and hitting" . $mobhits .
      " times.";

      $this->tpl->assign("playerresulthp",$playerresulthp);
      $this->tpl->assign("mobresulthp", $mobresulthp);
      $this->tpl->assign('roundresult',$roundresult)
      $this->tpl->assign('playerbattle', $playerbattle);
      $this->tpl->assign('mobbattle', $mobbattle);
      # End Battling Results #

      /* Victory / Defeat Check */
      if ($playerhp <= 0)
      {
        #Defeat
        $victorymsg = "";
        $deathmsg = "You have been defeated.";
        $query = $this->db->execute('UPDATE `<ezrpg>players` SET `latmob`=? `deaths`=deaths+1 WHERE `id`=?', array($mobid, $player->id);
        $this->tpl->assign('deathmsg', $deathmsg);

      }
      else
      {
        #Victory
        $deathmsg = "";
        $victorymsg = "You have defeated " . $this->mobs->name . ". <br /> You have gained " . $expgain . " experience. <br /> You have gained " . $goldgain . " gold.";
        $query = $this->db->execute('UPDATE `<ezrpg>players` SET `lastmob`=?, `exp`=?, `money`=? WHERE `id`=?', array($mobid, $expgain, $player->id));
        $this->tpl->assign('victorymsg');

      }


      $query = $this->db->execute('SELECT * FROM `<ezrpg>mobs`');
      $mobs = $this->db->fetchAll($query);
      $this->tpl->assign('mobs', $mobs);
      $this->tpl->display('battle.tpl');





  }

#6

i think I found that one too.
In the victory message you call $expgain and $goldgain but you have them commented out where you assign them


#7

Yeah I got that one. I got it somewhat working now. All the values are returning 0 currently. I’m trying to see why its doing that, plus its not showing the mob name and all that jazz.


#8

hmmm, I’m guessing there is an issue with the reference to the player and mob objects then. I don’t see where the reference to either is being set


#9

Seems like its the player one, which is weird, cause it lets me do $this->player->id just fine. Maybe I just need to do it myself and define them, and I can’t get the mobs name to show up either. Will have to see about that. Nope its pulling them just fine, just checked through my text. Hmm

MAY have figured it out, I think my math equations is ALL messed up. So let me fix them and get back to you.

I don’t know, it seems like if I try to do anything extra with them all the values turn to 0. Sooo weird.

Edit: Nope, seems like it doesn’t like certain math stuff. and I keep getting stuck in an infinite loop and it won’t stop even after changing the file to make it stop lol.

Okay, so I’ve figured out all the values are correct. I set the player HP to 1, and forced the loop not to run the miss code, so I should have been one-shot, yet its stuck in the loop:

while ($playerhp > 0 && $mobhp > 0)
      {
        /* miss, dodge calculations */

        $missmob = 1;
        $missplayer = rand($mobagi, $player->agility);


        /* who hits first */
        if ($hitfirsttrigger == true)
        {
          if ($player->agility >= $mobagi)
              { $playerturn = true; $hitfirsttrigger = false; }
              else { $mobturn = true; $hitfirsttrigger = false; }
        }

        /* Battle Time! */
        if ($playerturn == true)
          {
            if ($missmob == 0)
            {
              $playermiss += 1;
              $rounds += 1;
              $playerturn = false;
              $mobturn = true;
            }
            else
            {
              $dmgdeltmob = ceil($playerdmg - $mobdef);
              $mobhp = $mobhp - $dmgdeltmob;
              $playerhits += 1;
              $rounds += 1;
              $playerdamage = $playerdamage + $dmgdeltmob;
              $playerturn = false;
              $mobturn = true;
            }
        if ($mobturn == true)
          {
            if ($missmob == 0)
            {
              $mobmiss += 1;
              $rounds += 1;
              $mobturn = false;
              $playerturn = true;
            }
            else
            {
              $dmgdeltplayer = ceil($mobdmg - $playerdef);
              $playerhp = $playerhp - $dmgdeltplayer;
              $mobhits += 1;
              $round += 1;
              $mobdamage = $mobdamage + $dmgdeltplayer;
              $mobturn = false;
              $playerturn = true;
            }
          }
        }
      } # END BATTLE LOOP #

WOOO got it to work!!! So freakin’ happy!
“You did 439to 250with your Sword. You missed 0times and hit 1 times.
has done3075 to you. Missing 0 and hitting1 times.
You totaled 1 rounds.”

Thank you very much for your help!


#10

So now I have another problem. When I choose another “mob” in the list, it should work in theory, but it doesn’t. It just gets stuck loading, assuming stuck in the loop somehow. For some reason changing the value is ruining it in the $_POST[‘selection’]; line. I know this code works, because i tested it before I started coding the battle system. Something is very wrong.

Didn’t mean to double post :confused:

EDIT: It seems not even go through the turns and I’m not sure why. I tried capping it at 20 rounds and it just keeps going and going. So I’m assuming something else is causing this. Also seems to be passing the correct values as well, just when it goes through the loop it gets stuck. It’s so weird.So changed $mobid to = 2 manually and it does the same thing. What in tarnation lol.

Okay so I thought it was something about not being able to one-shot the mob and couldn;t finish the loop so I made the ID 1, and HP where i couldn’t one-shot and it indeed got caught in a loop. Now I kept id at 1, and made it to where i could one shot and it worked like a charm. So i kept the hp the same, but changed the id, stuck in the loop. I’m so confused as to why I can;t get this to work, and it’s kind of irritating me at this point lol.

EDIT: WE HAVE LIFT OFF (Plus optimizing good gosh!), Okay so I have figured it out, my variables where freezing it in the loop, as well as how I had everything nested. So I fixed it, optimized it a tiny bit and it looks a little cleaner as well. Still have a LONG way to go to get this system working the way I want (especially refreshing only certain elements and not the whole page). But I am progressing, if you are interest here is how I re-did my loop:

while ($playerhp > 0 && $mobhp > 0)
      {
        /* miss, dodge calculations */

        $missmob = 1;
        $missplayer = rand($mobagi, $player->agility);


        /* who hits first */
          if ($hitfirsttrigger == true && $player->agility >= $mobagi)
              { $playerturn = true; $hitfirsttrigger = false; }


        /* Battle Time! */

        if ($playerturn == true)
          {
            if ($missmob == 0)
            {
              $playermiss += 1;
              $rounds += 1;
              $playerturn = false;
            }
            else
            {
              $dmgdeltmob = ceil($playerdmg - $mobdef);
              $mobhp = $mobhp - $dmgdeltmob;
              $playerhits += 1;
              $rounds += 1;
              $playerdamage = $playerdamage + $dmgdeltmob;
              $playerturn = false;
            }
          }

        elseif ($playerturn == false)
          {
            if ($missmob == 0)
            {
              $mobmiss += 1;
              $rounds += 1;
              $playerturn = true;
            }
            else
            {
              if ($playerdmg >= $mobdef)
              {
                $dmgdeltplayer = 0;
                $round += 1;
                $playerturn = true;
              }
              else
              {
              $dmgdeltplayer = ceil($mobdmg - $playerdef);
              $playerhp = $playerhp - $dmgdeltplayer;
              $mobhits += 1;
              $round += 1;
              $mobdamage = $mobdamage + $dmgdeltplayer;
              $playerturn = true;
              }
            }
          }
      } # END BATTLE LOOP #