# Weapon Damage Formula (SEV)

For a given weapon, the following fields determine how much damage it can do at a given range value. Note that there are equivalent fields for ground combat weapons. Note further that the Weapon Space To Hit Modifier At Range and Weapon Space To Hit Modifier Formula are handled in a similar manner for determining the weapon modifier used in to hit calculations. There is just no Min and Max value range, though, just one value.

 Weapon Space At Range Distance Increment 10.0 Weapon Space Min Damage At Range 0.0 Weapon Space Max Damage At Range 0.0 Weapon Space Min Damage Modifier Formula (55 + (([%Level%] - 1) * 5)) - (([%Range%] / 10) * 5) - iif([%Range%] > Min(90, (([%Level%] - 1) * 4) + 50), 10000, 0) Weapon Space Max Damage Modifier Formula (55 + (([%Level%] - 1) * 5)) - (([%Range%] / 10) * 5) - iif([%Range%] > Min(90, (([%Level%] - 1) * 4) + 50), 10000, 0)

When a weapon hit is scored, the combat engine calculates two values, a minimum and a maximum damage. The final damage done is determined by a random number generator, with these two values as the lower and upper bounds. The minimum damage value is calculated as the sum of the Min Damage At Range and the Weapon Space Min Damage Modifier Formula, at the given decimal range.

Weapon Space Min Damage At Range works like the damage fields from SE4. The field is a space delimited list of damage values done every Weapon Space At Range Distance Increment ls. For example, weapon might have a field like this:

```Weapon Space At Range Distance Increment := 10.0
Weapon Space Min Damage At Range := 50.0 45.0 40.0. 35.0 30.0 25.0 0```

This means that at a range up to 10 ls, 50 damage is done. At a a range from 10 to 20 ls, 45 damage is done, and so on. Once the base damage value is determined, SE5 then calculates the Damage Modifier Formula result for the given range, and adds it to the base.

## Anatomy of a Modifier Formula

### Background

First, some background information on the functions used in this damage formula.

iff(CONDITION, a, b) means:

If CONDITION is true, do a. Otherwise, do b. It is a messy way to put:

```if (CONDITION) {
do a
}
else {
do b
}```

Sadly, it is the only way to do that when your entire "formula" has to fit on a single line.

Min(x, y) is a function that returns whichever value is smaller, x or y.

### The Formula

Now to look at the formula:

```(55 + (([%Level%] - 1) * 5)) - (([%Range%] / 10) * 5) -
iif([%Range%] > Min(90, (([%Level%] - 1) * 4) + 50), 10000, 0)```

The first part is pretty straightforward:

`(55 + (([%Level%] - 1) * 5))`

This evaluates to 5 plus 2 times one less than component level. This part is the base damage value, the damage at range 0.

The next part is the damage attenuation rate (aka: damage reduction over range).

`(([%Range%] / 10) * 5)`

For every 10 units of distance, subtract 5 damage.

The last part, the big iff, is the max range setting.

`iif([%Range%] > Min(90, (([%Level%] - 1) * 4) + 50), 10000, 0)`

In this case, we have:

```CONDITION = [%Range%] > Min(90, (([%Level%] - 1) * 4) + 50)
a = 10000
b = 0```

When the range to the target gets larger than a certain value, 10000 will be subtracted from the damage value. This effectively cuts off damage at that range, due to negative value handling.

The CONDITION is the part that is determining the max range; it is the minimum of 90 or (([%Level%] - 1) * 4) + 50). Thus, every instance of the weapon will have at most a range of 90. The second value that we compare 90 to is the range increase via tech levels. It is 50 plus 4 times one less than the level of the component. Isn't that nice wording? hehe.

This breaks out to the following ranges at tech levels:

```1: 50 + 0 = 50
2: 50 + 4 = 54
3: 50 + 8 = 58
4: 50 + 12 = 62
5: 50 + 16 = 66
...
10: 50 + 40 = 90
11: 50 + 44 = 94```

Note that level 11 gets a value greater than 90, so the Min function call forces the max range down to 90.