Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts

    Stacking an amount of damage in variable

    I query for two things in a database, Health and Attack. It's setup in a while loop to repeat depleting the health. However it's doing this,

    PHP Code:
    You attacked enemy for 32 Damage. (204 Remaining)
    You attacked enemy for 32 Damage. (204 Remaining)
    You attacked enemy for 32 Damage. (204 Remaining)
    You attacked enemy for 32 Damage. (204 Remaining
    I can't remove the health because it's static information, not an actual enemy. I need to take the original health, and add up all the damage on each while loop record, and and after 9 rounds, if it's higher than the enemies health, then it displays enemy is dead, if not it slows the outcome of how much you attacked it for and it's remaining health.

    So my question is how do you want a set value of say 100, and on every while loop keep adding the damage total up until it hits that number?
    I thought about like $damage++; but don't know if that will work.
    Last edited by myfayt; 01-03-2012 at 09:06 PM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    $damage++

    or

    $damage=$damage+1;
    if($damage > 100){
    // do something
    }

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Hmm it's now doing random values. Not adding them all together.
    (You hit enemy for 555 Damage (-506) Remaining) that remaining value should decrease every time, and the minus value should mean it's dead.

    PHP Code:
    $time 1;

            
            while (
    $time 10) {

            
    $mydamage $userrow['attackpower'];
            
    $mydamage2 $mydamage 2;

              
    $userhp $userrow['currenthp'];
            
    $enemy1hp $ginfo['maxhp'];
              
    $damage rand($mydamage2,$mydamage);
            
    $enemydam $ginfo['maxdam'];
            
    $enemydam2 $enemydam 2;
            
    $edam rand($enemydam2,$enemydam);
            
            
    $hitfor $userhp $edam;
            
            
    $remain $enemy1hp $damage;


              
    $page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($remain).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
             <br /><br />"
    ;


              if (
    $edam >= $userhp) {
                
    $page .= '<h3><font color=white>You Were Killed!</font></h3>';
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
              }
              else {
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
              }

              
    $time++;
              
    $remain++;
            } 
    PHP Code:
    You hit enemy for 555 Damage (-506Remaining,
    and 
    receive 20 Damage in return (1,127Remaining.


    You hit enemy for 739 Damage (-690Remaining,
    and 
    receive 19 Damage in return (1,127Remaining.


    You hit enemy for 702 Damage (-653Remaining,
    and 
    receive 35 Damage in return (1,127Remaining.


    You hit enemy for 685 Damage (-636Remaining,
    and 
    receive 24 Damage in return (1,127Remaining.


    You hit enemy for 983 Damage (-934Remaining,
    and 
    receive 24 Damage in return (1,127Remaining.


    You hit enemy for 556 Damage (-507Remaining,
    and 
    receive 34 Damage in return (1,127Remaining.


    You hit enemy for 901 Damage (-852Remaining,
    and 
    receive 37 Damage in return (1,127Remaining.


    You hit enemy for 994 Damage (-945Remaining,
    and 
    receive 33 Damage in return (1,127Remaining.


    You hit enemy for 550 Damage (-501Remaining,
    and 
    receive 21 Damage in return (1,127Remaining
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by myfayt View Post
    Hmm it's now doing random values. Not adding them all together.
    (You hit enemy for 555 Damage (-506) Remaining) that remaining value should decrease every time, and the minus value should mean it's dead.

    PHP Code:
    $time 1;

            
            while (
    $time 10) {

            
    $mydamage $userrow['attackpower'];
            
    $mydamage2 $mydamage 2;

              
    $userhp $userrow['currenthp'];
            
    $enemy1hp $ginfo['maxhp'];
              
    $damage rand($mydamage2,$mydamage);
            
    $enemydam $ginfo['maxdam'];
            
    $enemydam2 $enemydam 2;
            
    $edam rand($enemydam2,$enemydam);
            
            
    $hitfor $userhp $edam;
            
            
    $remain $enemy1hp $damage;


              
    $page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($remain).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
             <br /><br />"
    ;


              if (
    $edam >= $userhp) {
                
    $page .= '<h3><font color=white>You Were Killed!</font></h3>';
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
              }
              else {
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
              }

              
    $time++;
              
    $remain++;
            } 
    PHP Code:
    You hit enemy for 555 Damage (-506Remaining,
    and 
    receive 20 Damage in return (1,127Remaining.


    You hit enemy for 739 Damage (-690Remaining,
    and 
    receive 19 Damage in return (1,127Remaining.


    You hit enemy for 702 Damage (-653Remaining,
    and 
    receive 35 Damage in return (1,127Remaining.


    You hit enemy for 685 Damage (-636Remaining,
    and 
    receive 24 Damage in return (1,127Remaining.


    You hit enemy for 983 Damage (-934Remaining,
    and 
    receive 24 Damage in return (1,127Remaining.


    You hit enemy for 556 Damage (-507Remaining,
    and 
    receive 34 Damage in return (1,127Remaining.


    You hit enemy for 901 Damage (-852Remaining,
    and 
    receive 37 Damage in return (1,127Remaining.


    You hit enemy for 994 Damage (-945Remaining,
    and 
    receive 33 Damage in return (1,127Remaining.


    You hit enemy for 550 Damage (-501Remaining,
    and 
    receive 21 Damage in return (1,127Remaining
    You never update the enemy1hp value in that loop. So, every look you find the difference from the starting hp and the current damage done (add the damage to the remaining hp in your example for all the output and you'll see they all equal 49). Dunno why you would want to increment $remain before each loop .

    I think the problem is just poor logic - not enough thought gone into the design. You're reinitialising the values every single time you loop. If you're effecting variables inside a loop, you should keep the initialisation outside the loop. Like this:
    PHP Code:
    // Find initial hp before taking effect on it
    $enemyHP x;

    // Start looping. Start $i at 0, increment it once per iteration and iterate while $i < 10
    for($i=0;$i<10;$i++){
        
    // Do whatever actions you want
        // But importantly, at the end of the loop
        
    $enemyHP -= $damage;
        
    // Then, when the next loop uses $enemyHP, it will be the updated HP with damage taken off

    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I thought it was working, but it seems it's adding health rather than removing it.

    PHP Code:
     $getenemy doquery("SELECT * FROM {{table}} WHERE level='$level' LIMIT 1""monsters");
             
    $ginfo mysql_fetch_array($getenemy);
             
    $userhp $userrow['currenthp'];
            
    $enemy1hp $ginfo['maxhp'];
             
    $enemydam $ginfo['maxdam'];

            
    $time 1;

            
            while (
    $time 10) {


            
    $mydamage2 $mydamage 2;


              
    $damage rand($mydamage2,$mydamage);

            
    $enemydam2 $enemydam 2;
            
    $edam rand($enemydam2,$enemydam);
            
            
    $hitfor $userhp $edam;
            
            
    $remain $enemy1hp $damage;
              
    $enemyHP -= $remain;

              
    $page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($enemyHP).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
             <br /><br />"
    ;


              if (
    $edam >= $userhp) {
                
    $page .= '<h3><font color=white>You Were Killed!</font></h3>';
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
              }
              else {
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
              }

              
    $time++;

            } 

    PHP Code:
    You hit enemy for 719 Damage (608Remaining,
    and 
    receive 36 Damage in return (3,000Remaining.


    You hit enemy for 818 Damage (1,315Remaining,
    and 
    receive 41 Damage in return (3,000Remaining.


    You hit enemy for 710 Damage (1,914Remaining,
    and 
    receive 54 Damage in return (3,000Remaining.


    You hit enemy for 827 Damage (2,630Remaining,
    and 
    receive 64 Damage in return (3,000Remaining.


    You hit enemy for 665 Damage (3,184Remaining,
    and 
    receive 41 Damage in return (3,000Remaining.


    You hit enemy for 644 Damage (3,717Remaining,
    and 
    receive 46 Damage in return (3,000Remaining.


    You hit enemy for 824 Damage (4,430Remaining,
    and 
    receive 36 Damage in return (3,000Remaining.


    You hit enemy for 874 Damage (5,193Remaining,
    and 
    receive 59 Damage in return (3,000Remaining.


    You hit enemy for 692 Damage (5,774Remaining,
    and 
    receive 59 Damage in return (3,000Remaining
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by myfayt View Post
    I thought it was working, but it seems it's adding health rather than removing it.

    PHP Code:
     $getenemy doquery("SELECT * FROM {{table}} WHERE level='$level' LIMIT 1""monsters");
             
    $ginfo mysql_fetch_array($getenemy);
             
    $userhp $userrow['currenthp'];
            
    $enemy1hp $ginfo['maxhp'];
             
    $enemydam $ginfo['maxdam'];

            
    $time 1;

            
            while (
    $time 10) {


            
    $mydamage2 $mydamage 2;


              
    $damage rand($mydamage2,$mydamage);

            
    $enemydam2 $enemydam 2;
            
    $edam rand($enemydam2,$enemydam);
            
            
    $hitfor $userhp $edam;
            
            
    $remain $enemy1hp $damage;
              
    $enemyHP -= $remain;

              
    $page .= "You hit enemy for ".number_format($damage)." Damage (".number_format($enemyHP).") Remaining,<br />and receive ".number_format($edam)." Damage in return (".number_format($userrow['currenthp']).") Remaining.<br />
             <br /><br />"
    ;


              if (
    $edam >= $userhp) {
                
    $page .= '<h3><font color=white>You Were Killed!</font></h3>';
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=0 WHERE id='".$userrow['id']."' LIMIT 1""users");
              }
              else {
                
    $updatequery doquery("UPDATE {{table}} SET currenthp=currenthp-$edam WHERE id='".$userrow['id']."' LIMIT 1""users");
              }

              
    $time++;

            } 

    PHP Code:
    You hit enemy for 719 Damage (608Remaining,
    and 
    receive 36 Damage in return (3,000Remaining.


    You hit enemy for 818 Damage (1,315Remaining,
    and 
    receive 41 Damage in return (3,000Remaining.


    You hit enemy for 710 Damage (1,914Remaining,
    and 
    receive 54 Damage in return (3,000Remaining.


    You hit enemy for 827 Damage (2,630Remaining,
    and 
    receive 64 Damage in return (3,000Remaining.


    You hit enemy for 665 Damage (3,184Remaining,
    and 
    receive 41 Damage in return (3,000Remaining.


    You hit enemy for 644 Damage (3,717Remaining,
    and 
    receive 46 Damage in return (3,000Remaining.


    You hit enemy for 824 Damage (4,430Remaining,
    and 
    receive 36 Damage in return (3,000Remaining.


    You hit enemy for 874 Damage (5,193Remaining,
    and 
    receive 59 Damage in return (3,000Remaining.


    You hit enemy for 692 Damage (5,774Remaining,
    and 
    receive 59 Damage in return (3,000Remaining
    It's because, no offence, but you've just copy pasted my answer instead of thinking about my explanation. Reading, following and understanding answers to your problems provides a better understanding of PHP.

    The way you've set it up just now, you're working out the difference between damage and HP, then taking it away from $enemyHP? Why? $enemyHP at that point will be 0, so you'll be taking a negative value away from 0 (which is the same as addition) and using that as remaining HP. Wrong. Following my previous reply, change this:
    PHP Code:
    $remain $enemy1hp $damage;
              
    $enemyHP -= $remain
    to this:
    PHP Code:
    $enemy1hp -= $damage
    This is a prime example of why code design is hugely important. Next time, think about your logic flow and what you need to do. You need to have a running total type value of enemy hp, taking away the damage each iteration.
    Last edited by BluePanther; 01-03-2012 at 07:21 PM.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • Users who have thanked BluePanther for this post:

    myfayt (01-03-2012)

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I didn't just copy your code, I am not good with formulas at all, I get confused easily. I've been at this for over 6 hours already on my own. Then I decided on some help.

    Please bear with me. I made the changes and it seems to be working fine, thank you.
    Last edited by myfayt; 01-03-2012 at 09:05 PM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by myfayt View Post
    I didn't just copy your code, I am not good with formulas at all, I get confused easily. I've been at this for over 6 hours already on my own. Then I decided on some help.

    Please bear with me. I made the changes and it seems to be working fine, thank you.
    You're probably still using $enemyHP to echo out the HP remaining. Use $enemy1hp
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •