Jump to content
  • Advertisement
Sign in to follow this  
Warwco99

Attack System

This topic is 3724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry if this is in the wrong place. I've been teaching myself how to code recently and thought i'd have a go at making a game. It's pretty much all done (trading, training units, building things), etc... Only the attack system is left that i'm having trouble with. What happens is when you attack someone, if your attack hit is higher than their defence hit, you win, but as for the deaths of the player's men/units, that's where the problem lies. I've had a few ideas about how to do it, but none of them really work. The first idea was to do it with a %, so eg. For each set of hit strengths, you would lose a certain % of your men. eg. if the attack hit on your was between 1000-1500 then you can lose up to 2% of your men. But obviously that is a problem for the people with bigger armies, as they might easily beat someone, but end up losing 50 men when their opponent only loses a few. So you can see the problem with that. The other idea was to have a set number of deaths for each range of hit. eg. Att Hit 1000-1500: Max deaths 5 1501-2000: max deaths 7 etc... But that is also a problem because if you don't have that many men then i'll have to use a while statement to reduce the number and you could end up losing everyone, when i don't ever want people to lose all their men/units in 1 attack. So, having bored and confused you all with that, does anyone have any suggestions/ideas as to the best way to go about this? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Yeah, the question has nothing to do with web development as such, so I'll move this to Game Design.

Show what values you use to decide on attack and defence scores. Also, are the scores interval or ratio data? In other words, is a 2-1 victory equivalent to a 500-250 victory or a 251-250 victory? And is the strength proportional to army size or not? If attack strength grows proportional to army size then casualties should be proportional to that as well - that is the number of casualties, not the percentage of casualties. If you mix intervals and ratios things mess up, as you see with your 2% example, because you arbitrarily convert an interval (+1000) to a ratio (1 in 50).

Once these things are known, it makes it easier to reason about what exactly the scores mean, and how they should reflect upon casualties inflicted and so on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
Yeah, the question has nothing to do with web development as such, so I'll move this to Game Design.

Show what values you use to decide on attack and defence scores. Also, are the scores interval or ratio data? In other words, is a 2-1 victory equivalent to a 500-250 victory or a 251-250 victory? And is the strength proportional to army size or not? If attack strength grows proportional to army size then casualties should be proportional to that as well - that is the number of casualties, not the percentage of casualties. If you mix intervals and ratios things mess up, as you see with your 2% example, because you arbitrarily convert an interval (+1000) to a ratio (1 in 50).

Once these things are known, it makes it easier to reason about what exactly the scores mean, and how they should reflect upon casualties inflicted and so on.


Okay, i'll give it a go at explaining it better. (My maths is very bad though, which is why i'm having a big problem :( )

Anywho, without pasting pages of code, basically it's this (at the moment):

You train your men/units, each one has different attack and defence each, so your overall att and def power are all your units' powers added up (plus any upgrades you've done, but no need to get into that).

So, say i had:

Att str: 18282
Def str: 7101

So in calculating the fight (This is overly complicated and i'll probably change it as well, but at the moment):

My attack hit would be a random number between my max hit and my minimum hit (Max is up to 25% above my att strength and min is up to 25% less than my att strength).

Then i do the same for my opponents defence hit (ie. the random number between his max and minimum).

And if my attack hit is higher than his defence hit, i win. And visa versa.

So winning is solely based on the hits.

But then you come to calculating how many people/units die.

At the moment it's the following:

I have ranges of hits, so:

<= 500
> 500 && <= 1000
> 1000 && <= 1500
etc...

And in each range calculates how many you/they should lose. Let's take the > 1500 && <= 2000 example:


elseif($attackHit > 1500 && $attackHit <= 2000) { #9

$kMax = ($defenderUnits['unit_knight'] / 100) * 3; # So here the maximum knights you can lose is 3%
$kLosses = rand(0,$kMax);
$aMax = ($defenderUnits['unit_archer'] / 100) * 2; # Maximum archers you can lose is 2%
$aLosses = rand(0,$aMax);
$cMax = ($defenderUnits['unit_cavalry'] / 100) * 1; # etc...
$cLosses = rand(0,$cMax);
$bMax = ($defenderUnits['unit_ballista'] / 100) * 0;
$bLosses = rand(0,$bMax);
$trMax = ($defenderUnits['unit_trebuchet'] / 100) * 0;
$trLosses = rand(0,$trMax);
$toMax = ($defenderUnits['unit_tower'] / 100) * 0;
$toLosses = rand(0,$toMax);

$defLoss = $kLosses + $aLosses + $cLosses + $bLosses + $trLosses + $toLosses;

} #9


And with each range they increase.

So as you can see, the way it is at the moment, someone could easily beat someone by a huge margin in the attack strength, but still lose many more men due to the % nonsence. Problem is, i am quite mathematically challenged, so i'm not sure how else i could really do it..:S

Share this post


Link to post
Share on other sites
Here's a simple system...

All damages are dealt to opponents are calculated based on how the combat ends.

Say a small elite unit of 100 swordsmen engaged a pack of 1000 peasants. Chances are the swordsmen will butcher the peasants.

Swordsmen deal 50 damage per man - 100 x 50 = 5000
Peasants deal 3 damage per man - 1000 x 3 = 3000

Calculate the difference before diving the result with the total damage dealt by both parties

(5000-3000) / 8000 = 25%

Next check on a simple table, for example:
Where the difference is...
>25% - Winner take 0.5 - 3% losses, Loser take 10-20%. Loser rout.
15-25% - Winner take 1 - 5% losses, Loser take 5-15%. Loser waver.
0-15% - Both sides take 5-10% losses. Combat continues.

Swordsmen may lose 2% which turns out to 2 men.
Peasants may lose 15% which turns out to be 150 men, and they rout after the fight.

Now the outcome seems more "balanced".

You can add in variables to make it more complex, for example the swordsmen are elite giving 5% bonus, in formation another 5% to damage. Flankers get 5% bonus.

Hope that helps.

Share this post


Link to post
Share on other sites
Here is a combat system. It uses Frontages to work out how many units can attack and can be killed in a combat.

First calculate the number of units that can actually engage in combat. This is your Attack Frontage.

How you do this is up to you, but for melee units it could be Number of Units in the Squad / The number of Ranks. Ranged troops can shoot with all the units in the squad.

Next Calculate the number of units that are being targeted by the enemy. This is your Defence Frontage.

Again how you do this is up to you, but it could be the smaller of the two attacking frontages (of the defender and the attacker), and no greater than the total number of defenders.

In Code

Defender.AttackFrontage = Defender.TotalUnits / Defender.Ranks; //Set the defence frontage

if (Attacker.Type == "Ranged") // if the attacker is using ranged weapons
{
Attacker.AttackFrontage = Attacker.TotalUnits; //set the attack frontage to the total number of attackers
}
else // if the attacker is using melee attacks
{
Attacker.AttackFrontage = Attacker.TotalUnits / Attacker.Ranks; //set the attacker frontage according to the ranks
}

if (Attacker.AttackFrontage < Defender.AttackFrontage) //if the attacker has less frontage
{
Defender.DefenceFrontage = Attacker.AttackFrontage;
}
else //if the defender has less frontage
{
if (Attacker.Type == "Ranged") //if the attacker is using ranged weapons
{
Defender.DefenceFrontage = Attacker.AttackFrontage;
}
else //if the attacker is using melee weapons
{
Defender.DefenceFrontage = Defender.AttackFrontage;
}
}

if (Attacker.Type == "Melee") //if the attacker has ranged, then the only limit on how many can attack is the attacker's squad size.
{
if (Defender.DefenceFrontage > Defender.TotalUnits) //make sure that the defence frontage is less than the total number of defenders
{
Defender.DefenceFrontage = Defender.TotalUnits;
}
}


So now you have two number: Attack Frontage and Defence Frontage.

You also need two more number: Attack Bonus, and Defence Bonus (these bonuses can be negative, but not likely).

To calculate a hit add a Base Defence (for this example I will use 50) value to the Defence Bonus, and for the attack value you generate a random number between 0 and 2 X the Base Defence value (which would be in this example 100) and Add the Attack Bonus to it.

You subtract the Defence Score from the Attack Score and work it out as a percentage (of 2 X Base Defence) capping the value at 100% and 0% (or you may allow it to go over 100% and call it a critical strike or something - or even have some other effect like morale damage to the defender if that is part of your system).

This give you the number of attackers that hit defenders (the percentage of the Attack Frontage).

In Code:

//Attacker.AttackBonus and Defender.DefenceBonus are already set elsewhere in the program

int AttackPower = Attacker.AttackBonus + Rand(BASE_DEFENCE * 2), //initialise and calculate the attack power (you could also add in more terms to account for terrain and other modifiers to combat)
DefencePower = Defender.DefenceBonus + BASE_DEFENCE; //initialise and calculate the Defence Power (you could also add in more terms to account for terrain and other modifiers to combat)

float HitPercent = (AttackPower - DefencePower) / (BASE_DEFENCE * 2); //initialise the Hit Percentage variable

if (HitPercent > 1)
{
//add in any other special conditions that are triggered by a greater than 100% hit rate here
HitPercent = 1;
}

if (HitPercent < 0)
{
//add in any other special conditions that are triggered by a less than 0% hit rate here
HitPercent = 0;
}


NumberOfHits = Attacker.AttackFrontage * HitPercent; //Calculate the number of Hits

Now that you know how many attackers hit, you can apply the damage however you like according to you system (usually it is simply a base amount of damage multiplied by the number of hits).

The real beauty of this system is that Lanchester's Laws fall out of it without having to explicitly code them in.

Lanchester's laws are for calculating the amount of troops left in a battle of attrition. In melee there is the linear law (the number of troops left is equal to the number of attackers - number of defenders). In ranged combat there is the square law (the number of troops left is equal to the square root of the square of the number of attackers minus the square of the number of defenders).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!