Looping Battles

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Looping Battles

Post by Epiales »

Okay, since I decided to make my arena battles a place for player muggings :lol: I have to rewrite the entire battle scenario for the new arena ranking/battle system.

What I want to do is loop the battle until one or the other runs out of their guards, and then put the actual player into the battle using their hitpoints. So like their guards will be there protecting their main character, but when the guards are gone, they have to enter the fight with their hitpoints. And loop this scenario until they are out of guards AND out of hitpoints. One or the other anyway. If the attack runs out first, or if the defender runs out first.

Sounds sooooo complicated, but is there a way? Right now I have it single attacked based. Code below:

Code: Select all

<?php
if($attack_effect < $defense_effect){        
            
            $gun_lost = rand(3,8) + $enemy_stats['level'];
            $guard_lost = rand(3,8) + $enemy_stats['level'];
            $turnsused = rand(1,5);
            $currentturns = $stats['turns'];
            $newturns = $currentturns - $turnsused;
            echo "<font color='red'>You have lost the battle</font>"; ?><br /><br />
            
            <?php echo "You<font color='red'> lost</font> <font color='lime'>$gun_lost gunmen</font> and <font color='lime'>$guard_lost guards!</font>  You used " . $turnsused . " turns this round." ?>
            <br />
            <?php echo "You have $unit[guards] guards remaining before you enter the arena!"; ?>
            <br /><br />
            <?php
            
            $guard_lost = rand(1,2) + $stats['level'];
            $gun_lost = rand(1,2) + $stats['level'];
            echo "$u_name[username] <font color='red'>lost</font> <font color='lime'> " . $guard_lost . " guards</font> and <font color='lime'>$gun_lost gunmen!</font>"; ?>
            <br />
            <?php echo "$u_name[username] has $e_guards[guards] guards remaining before they enter the arena!";            
            

            }else{
But I really don't want people to have to click, click, click, click, click. So making a loop until their guards and hitpoints are depleted would be the best scenario if possible. Thanks!
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Looping Battles

Post by Epiales »

OMG I'm tired of searching LOL... I'll jump back on this later. I'll go ahead and just do it turn based for now. :oops: :cry:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Looping Battles

Post by MikuzA »

ok..

So Instead of doing it turn based, you want it to loop through it one go?

Example, this?
Enter battle (Loop):

Loop until someone is dead {
battle-calc(player1 vs player2)
}


If so,

add a while loop using a pre-defined variable, which is altered to break the loop until someone is dead.

But doing this has a huge possibility of something going unexpectedly and causing the massive loop to loop on to forever.
Add a precaution by estimating a maxlimit on loops, let's set 100.
Like

Code: Select all

$someonegotkilled = 0;
$maxloop = 100;

while ( $someonegotkilled = 0 ) {
//insert your awesome calculations here..

if ( $player1 == DEAD OR $player2 == DEAD ) { // doesn't work like this but as an example ;)
    $someonegotkilled = 1;
}

$maxloop--;
IF($maxloop < 1) { print "Max Loops achieved, halting"; break; }

}
 
Could this help?
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Looping Battles

Post by Epiales »

MikuzA wrote:ok..

So Instead of doing it turn based, you want it to loop through it one go?

Example, this?
Enter battle (Loop):

Loop until someone is dead {
battle-calc(player1 vs player2)
}


If so,

add a while loop using a pre-defined variable, which is altered to break the loop until someone is dead.

But doing this has a huge possibility of something going unexpectedly and causing the massive loop to loop on to forever.
Add a precaution by estimating a maxlimit on loops, let's set 100.
Like

Code: Select all

$someonegotkilled = 0;
$maxloop = 100;

while ( $someonegotkilled = 0 ) {
//insert your awesome calculations here..

if ( $player1 == DEAD OR $player2 == DEAD ) { // doesn't work like this but as an example ;)
    $someonegotkilled = 1;
}

$maxloop--;
IF($maxloop < 1) { print "Max Loops achieved, halting"; break; }

}
Could this help?
Not sure yet :lol: I need sleep lol... I will look more closely later today and see if I can't figure something out. Thank you! At least I have some sort of a start now YAY! Codes a bit confusing, as I'm not familiar with looping or any of that yet. So I'll tamper with it a bit.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Looping Battles

Post by MikuzA »

This sentence used in PHP.net explains while so that you might remember it next time you loop,
The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Looping Battles

Post by Epiales »

Told ya I didn't know much about it. Am I even close? :lol: :lol:

Code: Select all

        $user_check = mysql_query("SELECT * FROM `stats` WHERE `id`='".$id."'") or die(mysql_error());
        $enemy_name = mysql_query("SELECT * FROM `user` WHERE `id`='".$id."'") or die(mysql_error());
            $u_name = mysql_fetch_assoc($enemy_name);
        $enemy_guards = mysql_query("SELECT * FROM `unit` WHERE `id`='".$id."'") or die(mysql_error());
            $e_guards = mysql_fetch_assoc($enemy_guards);
        $enemy_defense = mysql_query("SELECT * FROM `stats` WHERE `id`='".$id."'") or die(mysql_error());
            $e_defense = mysql_fetch_assoc($enemy_defense);    
        $enemy_attack = mysql_query("SELECT * FROM `stats` WHERE `id`='".$id."'") or die(mysql_error());
            $e_attack = mysql_fetch_assoc($enemy_attack);
        $enemy_hpoints = mysql_query("SELECT * FROM `stats` WHERE `id`='".$id."'") or die(mysql_error());
            $e_hpoints = mysql_fetch_assoc($enemy_hpoints);            
        
         $enemy_stats = mysql_fetch_assoc($user_check);

$attack_effect = rand(1,5) * $stats['attack'];
            $defense_effect = rand(1,5) * $e_defense['defense'];
             echo "You send your gunmen into battle!<br><br>";
            echo "Your gunmen dealt " . number_format($attack_effect) . " damage!<br>"; //How much damage they did to enemy
            echo "$u_name[username] blocks for " . number_format($defense_effect) . " points!<br><br>";    
           
        $attacker = $user['username'];
        $defender = $u_name['username'];
        $a_attack = $stats['attack'];
        $a_defense = $stats['defense'];        
        $d_attack = $e_attack['attack'];
        $d_defense = $e_defense['defense'];
        $a_hitpoints = $stats['hpoints'];
        $d_hitpoints = $e_hpoints['hpoints'];
        $a_guards = $unit['guards'];
        $d_guards = $e_guards['guards'];
        $attack_effect = rand(1,5) * $a_attack;
        $defense_effect = rand(1,5) * $d_defense;        
        $a = $a_hitpoints;
        $d = $d_hitpoints;

        $someonegotkilled = 0;
        $maxloop = 100;

        while ( $someonegotkilled = 0 ) {
        if($attack_effect < $defense_effect){        
            
        $new_d_hitpoints = $d - $defense_effect;
        $new_a_hitpoints = $a - $defense_effect;

        if ( $new_a_hitpoints == 0 OR $new_d_hitpoints == 0 ) {
        $someonegotkilled = 1;
        $updateplayer="update stats set hpoints='$new_a_hitpoints' WHERE id='$id'";
        mysql_query($updateplayer) or die ("Could not update player");    
}
        $maxloop--;
        if($maxloop < 1) { echo "<font color='red'>You have lost the battle</font>";  <br /><br />
Nothing happens, so I guess not :lol: :oops: :oops: :oops:
Last edited by Epiales on Mon Sep 16, 2013 12:14 pm, edited 2 times in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Looping Battles

Post by Epiales »

I changed while ( $someonegotkilled = 1 and then attack. It loops for about 30 seconds and then:

Code: Select all

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\flash\mafiaboss2.php on line 162
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Looping Battles

Post by Epiales »

Okay, I got a LOOP :lol: :lol: YAY... But the loop goes on forever lol... It just repeats the sending gunmen into battle and how much the defense and attack was :(

Code: Select all

 $someonegotkilled = 0;
        $maxloop = 10;

        while ( $someonegotkilled = 1 ) {
        
        
        $attack_effect = rand(1,5) * $stats['attack'];
            $defense_effect = rand(1,5) * $e_defense['defense'];
             echo "You send your gunmen into battle!<br><br>";
            echo "Your gunmen dealt " . number_format($attack_effect) . " damage!<br>"; //How much damage they did to enemy
            echo "$u_name[username] blocks for " . number_format($defense_effect) . " points!<br><br>";    

        if($attack_effect < $defense_effect){        
            
        $new_d_hitpoints = $d - $defense_effect;
        $new_a_hitpoints = $a - $defense_effect;
         $updateattacker="update stats set hpoints='$new_a_hitpoints' WHERE id='$_SESSION[uid]'";
        mysql_query($updateattacker) or die ("Could not update player"); 
        $updatedefender="update stats set hpoints='$new_d_hitpoints' WHERE id='$id'";
        mysql_query($updatedefender) or die ("Could not update player"); 
        
        
           
        if ( $new_a_hitpoints == 0 OR $new_d_hitpoints == 0 ) {
        $someonegotkilled = 0;
       
}
        $maxloop--;
        if($maxloop < 1) { echo "<font color='red'>You have lost the battle</font>";   <br /><br />} 
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Looping Battles

Post by Epiales »

Okay, I"m closer YAY. I still loops for the 30 seconds and gives the exceeded 30 second thingy message :lol: But at least now the numbers in the database for the hitpoints is changing... but going into negative numbers lol. New code:

Code: Select all

        $someonegotkilled = 0;
        $maxloop = 10;

        while ( $someonegotkilled = 1 ) {
        
        
        $attack_effect = rand(1,5) * $stats['attack'];
            $defense_effect = rand(1,5) * $e_defense['defense'];
            
        $new_d_hitpoints = $d - $defense_effect;
        $new_a_hitpoints = $a - $defense_effect;            
        $updateattacker="update stats set hpoints='$new_a_hitpoints' WHERE id='$_SESSION[uid]'";
        mysql_query($updateattacker) or die ("Could not update player"); 
        $updatedefender="update stats set hpoints='$new_d_hitpoints' WHERE id='$id'";
        mysql_query($updatedefender) or die ("Could not update player");            
        if($attack_effect < $defense_effect){        
        
        
           
        if ( $a == 0 OR $d == 0 ) {
        $someonegotkilled = 0;
       
}
        $maxloop--;
        if($maxloop < 1) { echo "<font color='red'>You have lost the battle</font>";   <br /><br />}
 
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Looping Battles

Post by Epiales »

Okay, done messing with it for now. Brain is twitterpated again :lol: :lol: :lol:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”