Geeky Combat Mechanics Question

macrobot

Chieftain
Joined
Jun 5, 2006
Messages
38
I am trying to write a Java program that calculates the outcomes of a stack of units attacking a stack of defenders using a Monte Carlo type simulation (run combat multiple times and view the potential outcomes). I hope to get it to the point where you can see how different attack orders, upgrades, etc will change your overall outcomes. I think the game does it horribly when you just ram your big stack into a bunch of defenders and I have no idea if I am doing it any better choosing manually.

In order to figure out how to do this I was reading This Article on Combat mechanics and it says this

Round odds

The odds that the attacker will win each round is given by A/(A+D), so the odds the defender wins a round is D/(A+D).

I’m now going to introduce a new variable – the ratio between the attacker’s and defender’s modified strength. This value is R. It is A/D and it controls all of combat. The greater R is, the better the attacker’s odds of winning.

Each round, the attacker wins with odds R/(1+R) and the defender with odds 1/(R+1).

Damage per hit

The attacker and defender both start with their current number of hps (NOT 100 as was previously stated). The attacker’s damage is given by floor(20*(3*A+D)/(3*D+A)). That is, you calculate the value and drop any fractional remainder. The defender’s damage is given by floor(20*(3*D+A)/(3*A+D)).

Going back to R notation, the attacker does floor(20*(3*R+1)/(3+R)) damage and the defender does floor(20*(3+R)/(3*R+1)) damage.

The maximum damage one size can do is 60 per hit. The minimum that can be done is 6. Only those values are possible, regardless of the relative strengths of the units.

Once one side goes below 0 hps remaining, it is destroyed (or retreats). The winner has strength remaining equal to starting_strength*hps/100, but it has fewer hps, so it is more likely to die in further combat. Modified strength no longer matters.

Based on this it appears that the same amount of damage is done per round regardless of any randomness. This seems incorrect to me for some reason. Is there a step I am missing? My results approximate the "correct" values but I can't help but think that I am missing a step that randomizes the amount of damage each round. Does anyone have any insight into this? Any help would be appreciated.
 
The point is that while at any round we can easily determine damage both attacker and defender would inflict if he won, the victory itself is random.
You're not missing anything.

While not realistic, you're trying to simulate the technical side of the problem, not it's realism.

Anyway, I wish you luck with your project, as I've been thinking of something similar, but I'm way too lazy to actually do it. Great that somebody wants to take care of it. :goodjob:
 
Thanks. I reread the combat mechanics a few times and realized that the amount of damage dealt is definitely static while the winner varies each time. Based on that I have something that will do BASIC combat. Once I figure out Enums I will start adding in the promotions and other fancy behavior.
 
Such calculations become hideously complex if you try to take into account such things as "+1 first strike chance". What, for example, is the actual probability of a unit with this capability actually making a first strike ? What if the best defender (which the program selects according to the attacker) is immune to first strikes although the attacker has the capacity to make such ? Would you do better to attack at 99% odds, when if you win your unit would gain 2XP, rather than attacking a weaker unit where the odds are 99.9% in your favour but you would gain only 1XP from a win?
Personally, I much prefer to attack using units individually. That way, I can see the probable outcome of each encounter before committing myself to the combat. "Probable" must be emphasised here: there seems to be a rule "if you absolutely must win this fight, then you won't", and another which says "if your unit is Warlord-led, it will take severe damage even at odds >99.9%"
 
there seems to be a rule ... which says "if your unit is Warlord-led, it will take severe damage even at odds >99.9%"

well yes, but the plus side is that then he's hurt and won't be the one picked to defend the stack when the bad guy attacks you on his turn! glass half full ;)

lots of games i don't make fighting Warlord units at all, but when i do they're such fun! nerve-wracking for sure tho.
 
Your concept made me actually do something :mad: , and therefore I've got something that is surely gonna be useful in your project. It is written in C++, as I don't know Java, but it is very basic stuff. I wrote it using the rules in our favorite combat mechanics thread. It simulates a single combat using a standard C PRNG, but I think you shouldn't have any problems with porting it to Java.

The structures:

Code:
struct combatInput_t
{
	int attackerHP;
	int defenderHP;
	float attackerModifier;
	float defenderModifier;
	float attackerBase;
	float defenderBase;
	float withdrawalChance;
	int firstStrikeDelta;
};

attackerHP - as you see in the combat odds screen
defenderHP - as you see in the combat odds screen
attackerModifier - 1 + all modifiers you get when attacking
defenderModifier - 1 + all modifiers you get when defending
attackerBase - unit's base HP (6 for swordsman and so on)
defenderBase - unit's base HP
withdrawalChance = withdrawalPercent / 100%
firstStrikeDelta = attackerFirstStrikeNum - defenderFirstStrikeNum (yes it's negative when the defender has the edge)

Code:
struct combatResults_t
{
	bool defenderWon;
	bool attackerWon;
	bool attackerRetreated;
	int xpGotten;
	float defenderRemaining;
	float attackerRemaining;
};

First three are mutually exclusive and I think quite self-explanatory.
xpGotten is number of XP points received and
attacker/defenderRemaining are unit strengths after the combat is completed.
 

Attachments

You guys can display the combat log and you'll see that units inflicted a fixed amount of damage to each other in a given combat.
 
Thanks for all of the responses. I appreciate the help. I am not calculating the bonuses yet and have no interface at all but I do have something that will allow me to simulate the results of a stack attack on a stack of defenders. I attached the source code if anyone is interested. This is pretty much my first java project so if it sucks please be gentle.
 

Attachments

Back
Top Bottom