# Random Generation That Adds Or Subtracts

## Recommended Posts

I'm becoming a top question asker... so hopefully the saying no question is stupid rings true heh.What I need to do: Making a fight script in which a player fights with a npc.Need to be able to be able to take Player Database items (Like Attack, Defense, health)And have them be affected by NPC database items (Attack, Defense, Health)I know how to do it if there were only 1 attack for the player and or npc to do... but I'd like to make it so the player can choose which attack to do; and thenthe npc would have a certain chance of attacking back, countering, etc.My best stab at it (which I'm typing up right now)Is to take the two tables; then make a random string pull for the npc attacks - Which would be stored in a variableThen tell it to subtract that variable from the players health.... I can get that far; but how would I make it so if the player has a defense of 10 and is attacked by an attack of 15 that the result is the player loses 5 health?Any help would be greatly appreciated!

##### Share on other sites

Subtraction sounds like the way to go. I'm confused about what you need help with, it's just math.

##### Share on other sites

Subtraction sounds like the way to go. I'm confused about what you need help with, it's just math.
Would it be something like UPDATE players SET health=health + '\$defense' - '\$zattack' ? The variables standing for the player defense and the npc attack?
##### Share on other sites

You wouldn't want to add the defense to the health, what if the attack is less than the defense? They would gain health from getting attacked. Subtract first to figure out how much they actually lose and if it's more than 0 then update the database.

##### Share on other sites

You wouldn't want to add the defense to the health, what if the attack is less than the defense? They would gain health from getting attacked. Subtract first to figure out how much they actually lose and if it's more than 0 then update the database.
ok; so could I do a statement like :if ('\$_POST[attack]=='Use Primary' AND '\$healthloss'>'0')Where \$_POST[attack] is a drop down menu where player would pick attack, and '\$healthloss' is the variable that stores whether the attack was greater than the defense.Does that look/sound right?Also... I was going to randomly select a npc attacker... but how can I make sure it keeps that same npc player for the duration of the "fight" instead of randomly selecting a new one on every attack submission?
##### Share on other sites

You're quoting too many things in the if statement, don't quote variable names if you're trying to get the value of the variable, and don't quote numbers unless you're comparing numeric strings instead of actual numbers.If you need to propagate the ID or whatever of the opponent, you can either use a hidden form input to pass that information through the form, or you can use PHP's session to store that information in the session. That will make it available to all other PHP pages.

##### Share on other sites

You're quoting too many things in the if statement, don't quote variable names if you're trying to get the value of the variable, and don't quote numbers unless you're comparing numeric strings instead of actual numbers.If you need to propagate the ID or whatever of the opponent, you can either use a hidden form input to pass that information through the form, or you can use PHP's session to store that information in the session. That will make it available to all other PHP pages.
Ok; I think I'm following everything. Thanks again