...

View Full Version : Resolved Stacking an amount of damage in variable



myfayt
01-03-2012, 02:11 PM
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,


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. :confused:

mlseim
01-03-2012, 02:15 PM
$damage++

or

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

myfayt
01-03-2012, 06:40 PM
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.


$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++;
}



You hit enemy for 555 Damage (-506) Remaining,
and receive 20 Damage in return (1,127) Remaining.


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


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


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


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


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


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


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


You hit enemy for 550 Damage (-501) Remaining,
and receive 21 Damage in return (1,127) Remaining.

BluePanther
01-03-2012, 06:59 PM
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.


$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++;
}



You hit enemy for 555 Damage (-506) Remaining,
and receive 20 Damage in return (1,127) Remaining.


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


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


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


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


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


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


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


You hit enemy for 550 Damage (-501) Remaining,
and receive 21 Damage in return (1,127) Remaining.


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 :confused:.

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:


// 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
}

myfayt
01-03-2012, 07:16 PM
I thought it was working, but it seems it's adding health rather than removing it.


$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++;

}




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


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


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


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


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


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


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


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


You hit enemy for 692 Damage (5,774) Remaining,
and receive 59 Damage in return (3,000) Remaining.

BluePanther
01-03-2012, 08:19 PM
I thought it was working, but it seems it's adding health rather than removing it.


$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++;

}




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


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


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


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


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


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


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


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


You hit enemy for 692 Damage (5,774) Remaining,
and receive 59 Damage in return (3,000) Remaining.



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:


$remain = $enemy1hp - $damage;
$enemyHP -= $remain;

to this:


$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.

myfayt
01-03-2012, 10:03 PM
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.

BluePanther
01-03-2012, 10:07 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum