Yeah attack functions are one of the trickiest parts of the actual game play. The idea is that you have to calculate the strength and defense of each player, army, unit whatever and how to assign that damage.
There is no magical one fix all attack formula since games have various factors. This formula will also be one of the most frequently tweaked algorithms in your game to make sure it is balanced. Here are a couple tips to look at when designing this functionality....
1) Base damage - What is the most basic amount of damage they can do. Usually this is determined by the strength of the character and weapon/its grade. Obviously the base "physical" damage of a level 20 male barbarian is going to be more than that of a level 10 female rogue. Of course he might be slower too.
For this I usually use a characters strength times a specific constant like level * .5. So if they are a level 20 character maybe their base damage is 10. Then I go about adding the weapon damage and its grade. Maybe an axe that deals 10 - 20 damage of the lowest grade might be (randomdamage(10,20) * 1) where we introduce our first sign of "randomness" Not always is your character going to land the best hit for maximum damage, but will at least hit for 10 and possibly up to 20. The number 1 in this formula would be a grade modifier saying that since it is the lowest grade, don't really apply damage amplification.
So if they hit for 15, they hit for 15. Now if they had a grade modifier of 1.2 they would hit for 18 (15 * 1.2).
So put together the base damage plus the weapon damage with grades you might see something like...
dmg = ((character level * .5) + (weapondamage(10,20) * gradeofweapon))
2) Modifying damage - Above shows you a little bit of the modification process with weapon grade, but you might have things like spells or certain circumstances where your character may have to hit for more or less than their normal base damage. These basically take the attack damage and either modifies the entire damage dealt or we effect a certain part of the damage formula... like make the weapon deal lower damage than it normally would.
Applying the modification to the total damage value has HUGE effects and modifying some part of the damage formula can have small or large effects depending on what it is applied to.
For instance, we had our axe which dealt 10-20 random damage plus a medium grade constant of 1.2. So a 15 pt hit would actually turn to 18 because of the better weapon grade. Now lets say we get into a battle and our enemy casts rust on our character. We could modify the 1.2 to instead be something like .8 which would turn our 15 point hit into something like 12.
Or we could modify the entire damage for large effects like maybe they cast something like weakness on us which knocks off 5 points of damage straight of our damage. So take the "dmg" variable above and subtract 5 points (if dmg < 0 then set dmg to zero) causing our character to hit for a possible 0 damage. Which makes sense if they can't even lift their heavy sword.
3) Randomness - This is the part you have to be very careful of and the part that usually needs the most tweaking. Always keep an eye on the lowest/highest value a random expression can give and make sure it makes sense in the formula. You don't want to be giving your character a weakness of random(100,200) if your characters damage never gets above 80 because then your character will never deal damage. You will also use randomness when determining if their attempt to attack will actually strike the player. Remember, people fighting can miss the person entirely. So use a random chance function to determine this.
4) Defense - This formula is going to be very much like your attack but instead of calculating damage you are calculating how much damage you can take safely with another possible calculation on the probability that they land a blow.
5) Assign the damage and determine the victor - Well you got one character with their attack damage formula, another with their defense formula... determine who wins. The idea is pretty simple...
damage to defensive player = attacker damage - defensive player protection.
For instance if the level 20 barbarian hit for 100 points (base damage plus weapon plus it did land) and the other person is a level 20 barbarian with defense of 80, the overall damage to the defensive barbarian is going to be 120 - 80 or 40 damage.
Lets say the defensive player got a better armor grade... they would then have something like.. 120 - 90 or 30 damage be dealt to the defensive player.
Now again these are only some basic formulas and may things can come into play calculating these formulas. They will need to be leveled out a little and perhaps you don't have spells, maybe you have various types of armor (like actual armor AND a protection enchantment). So you will need to add and modify formulas accordingly.
Hope this information can get you started with the attack.
Enjoy!
This post has been edited by Martyr2: 23 Oct, 2007 - 02:01 PM