1# Damage
2
3[Source](http://ageofempires.wikia.com/wiki/Attack)
4
5Close combat and projectile damage calculations.
6
7## Measurement
8
9(dark age, no tech)
10
11unit            | enemy    | stated attack | stated defense | damage (uphill) | damage (level) | damage (downhill)
12----------------|----------|---------------|----------------|-----------------|----------------|------------------
13teutonic knight | house    | 17            | 0              | 16              | 21             | 26
14teutonic knight | villager | 17            | 0              | 13              | 17             | 22
15villager        | villager | 3             | 0              | 2               | 3              | 4
16trebuchet       | castle   | 200           | 0              | 323             | 431            | 539
17ram             | castle   | 2             | 0              | 88              | 117            | 146
18
19## Attack damage formula
20
21The damage in AoE1 and AoE2 is calculated with the following formula
22
23```
24damage = max(1, ((max(0, melee_damage - melee_armor)
25                + max(0, pierce_damage - pierce_armor)
26                + sum(max(0, attack_bonus - attack_resist)))
27                * elevation_factor) * stray_factor)
28```
29
30Explanation:
31
32* `melee_damage`: Melee damage of the attacking unit
33* `melee_armor`: Melee armor of the defending unit
34* `pierce_damage`: Pierce damage of the attacking unit
35* `pierce_armor`: Pierce armor of the defending unit
36* `attack_bonus`: Attack bonus (flat additive) against a target's armor class.
37* `attack_resist`: Attack resist (flat additive) against an attack bonus.
38* `elevation_factor`: Multiplies damage by `1.25` (`1.33` in AoE1) if the attacking units is on higher elevated terrain or on top of a cliff. Similarly, units on lower ground receive a malus that multiplies their damage by `0.75` (`0.66` in AoE1). The malus doesn't apply when standing at the bottom of a cliff.
39* `stray_factor`: When a projectile misses its intended target but hits a bystanding unit, damage is halved. Check [Accuracy](accuracy.md) for more details.
40
41Standing on top of cliffs and additionally being higher elevated does not increase the damage bonus. Also, standing on higher elevated terrain while at the bottom of a cliff does not negate the bonus damage of the unit on top of a cliff. However, it does gives the unit at the bottom of the cliff the elevation damage bonus of `1.25`. The only requirement for the application of the cliff damage bonus is that there must be a cliff in between the units. The distance to the cliff does not matter.
42
43### Armor
44
45[Source](http://ageofempires.wikia.com/wiki/Armor)
46
47Armor is organized through *armor classes* of which there are 30 in AoC and 32 in AoE2:HD. Units can have several armor classes, but will always have the classes *melee* and *pierce* assigned. Units have a value for each of their armor classes which indicate the `attack_resist` against attacks directed at the armor class. All units (except trees, gregarious animals, hunting wolves and some units from beta versions) have a default value of **1000** for each armor class that they don't have.
48
49An attack is always applied against one or more armor classes. We will look at an example to show how the damage is calculated.
50
51An archer attack a spearman. The attack of the archer does `0` damage against the *melee* armor class, `4` damage against the *pierce* armor class and `3` damage against the *spearman* armor class. The spearman has the armor classes *melee* with value `0`, *pierce* with value `0`, *infantry* with value `0` and *spearman* with value `0`. Now we can calculate the outcome of the attack:
52
53```
54damage =   max(0, archer_melee_damage - spearman_melee_armor)
55         + max(0, archer_pierce_damage - spearman_pierce_armor)
56         + max(0, archer_spearman_damage - spearman_spearman_armor)
57
58       = max(0, 0 - 0) + max(0, 4 - 0) + max(0, 3 - 0) = 7
59```
60
61The attack does a total of 7 damage. The *infantry* armor class of the spearman is ignored because the archer's attack does not do damage against it.
62
63As a second example, we will let a spearman attack a cataphract. This time, we will list attack and defense in a table.
64
65Attack class  | Attack value |
66--------------|--------------|
67melee         | 3            |
68pierce        | 0            |
69cavalry       | 15           |
70war elephant  | 15           |
71camel         | 12           |
72ship          | 9            |
73eagle warrior | 1            |
74building      | 1            |
75
76Defense class | Attack resist |
77--------------|---------------|
78melee         | 2             |
79pierce        | 1             |
80cavalry       | 12            |
81unique unit   | 0             |
82
83The matching armor classes are *melee*, *pierce* and *cavalry*. We can derive a formula for the damage from that.
84
85```
86damage =   max(0, spearman_melee_damage - cataphract_melee_armor)
87         + max(0, spearman_pierce_damage - cataphract_pierce_armor)
88         + max(0, spearman_cavalry_damage - cataphract_cavalry_armor)
89
90       = max(0, 3 - 2) + max(0, 0 - 1) + max(0, 15 - 12) = 4
91```
92
93The attack does a total of 4 damage.
94
95Only the values for the melee class and the pierce class are visible in the interface.
96
97## Damage by Projectiles
98
99The damage of a shot in AoE2 is most likely calculated after it has hit the target. This leads to weird behaviour when units die, including:
100
101* Losing environmental bonuses such as uphill or cliff bonus damage (+25 %)
102* Dealing no damage, e.g. trebuchets
103* Dealing only 1 damage, e.g. if japanese trebuchets are packed before they hit their target
104
105It also has other consequences. Technologies will affect projectiles that were in the air when research finished.
106