Fallout 4 Settlements/Defense

Defense is a gameplay mechanic related to attacks on settlements

Background
Once the Sole Survivor has control of a populated settlement, they will need to defend it from attacks by raiders, super mutants, and other hostiles. Impending attacks will be reported by the Sole Survivor's Pip-Boy, showing up as a miscellaneous objective. Attacks can either be attended by the player character or unattended. Very different resolution is used for these two distinct cases.

Objects

 * Measures the apparent defensive improvements the settlement has based on settlement buildings with defense ratings. Only provided by defenses built at a workshop; mines do not count toward settlement defense rating, even though they can destroy attacking forces if the player character is also present. Some creatures are able to be captured using cages (featured in Wasteland Workshop), which also add to the defense rating, such as deathclaws and yao guai. The settlement needs one unit of defense per settler to keep the settlers happy.
 * Defensive improvements range from fortifications, which require assigning a settler as a guard, to autonomous turrets (small turrets run on their own power, while larger ones require separate power sources), as well as an assortment of traps.
 * Settlers can be provided with weapons and ammunition, and arrive in settlements with their own. As long as they have at least one unit of ammo in their inventory, they will not run out. Settlers themselves do not contribute to the defense rating, rather, well armed settlers will be better at repelling attacks, provided the Sole Survivor is present.
 * One can build a siren near a guard post and the guard will activate the siren upon spotting an enemy. The siren will alert settlers in the area that enemies are approaching, and they will move towards the siren to help defend.
 * There is also a wandering salesman named Gene who sells junkyard dogs that provide 5 defense.
 * Enemies that a settlement will face is based on the level range of the cell the settlement is located in on the map. In general, settlements located further south will face tougher enemies.
 * Each point of defense lowers the chance of an attack by 1%.
 * Each population unit lowers the chance of an attack by 0.5%, in addition to any other defensive value the settler provides.
 * Each unit of food and water production at the settlement increases the chance of attack by 0.1%.
 * Each edible and/or drinkable item stored in a settlement's workbench increases the chance of attack by 0.1%.
 * The minimum chance of an attack upon a settlement per day is 2%, regardless of defenses. The maximum chance is unknown but assumed to be 100%. Whatever the chance is, a settlement cannot be attacked if it has already been attacked in the last 7 in-game days.
 * The defense values do not directly correlate with actual combat efficiency. An Mk. 5 machine gun turret can kill enemies several times faster than an Mk. 1 heavy machine gun turret but will contribute less to the settlement defense value.
 * Dependencies: people, power (with certain turrets and traps).

Attended attacks
The Sole Survivor can travel to the settlement to deal with the attackers if they so choose. Any friendly NPCs present, including settlers, companions residing at the settlement, as well as (with Automatron) robots, can assist the settlement if attacked, but only if the player character attends the attack in person. An attended settlement defense is resolved using the normal combat system, meaning that turrets, traps and mines can also be involved in the defense. Fighting continues until all attackers are dead. Damage caused to settlement objects during the fighting is also determined by the combat system. Note that friendly fire is just as capable of damaging settlement objects as enemy fire. It is also possible for the player character to inadvertently kill friendly NPCs during the battle.

Unattended attacks
If the player character does not attend, or does not reach the settlement in time, the unattended attack will be resolved off-screen, with the results being reported as on-screen pop up. Failing to defend the off-screen settlement attack successfully will cause temporary damage to various settlement objects, permanent loss of some inventory items, temporary reduction to settlement workshop values (power, water, food, etc), as well as temporarily reducing happiness. All temporary effects auto-heal over time, though the player character can expedite settlement object repairs in person. Increasing the defense rating of the settlement reduces the chance of an attack occurring, as well as increasing the chance of a successful defense, and moderating the damage in the event of an unsuccessful defense.

Scripted attacks
Several Minutemen quests also include scripted settlement attacks. These attacks use similar mechanics, but with greater rewards (and sometimes greater consequences for failure).

Damage
During an attack, the attackers can damage various settlement objects including turrets, water pumps, purifiers, generators, and crops. If the attack is resolved off-screen and results in a failure, damage will be applied to a random selection of settlement objects. Settlement-related quests will also apply scripted damage to settlements from time to time. This damage will repair itself over time. Settlement object damage repairs will consume the required materials from the workshop or supply lines automatically; the Sole Survivor can also use the workshop to repair settlement objects manually using some of the materials required to build the object.

Damaged settlement objects will no longer perform their functions. For example, a damaged turret will not fire, nor will it contribute to the settlement's defense rating. Damaged crops, purifiers, and pumps will not contribute to food or water supplies. This can result in a significant further reduction in happiness if a settlement is subject to frequent attacks, so building up settlement defenses is crucial to maintaining a happy settlement.

Attack chance
The chance of a settlement being attacked is calculated as follows:

$$\text{Base}\% = 2 + \frac{\text{Food} + \text{Water}}{10}$$

$$\text{Reduction}\% = \frac{\text{Population}}{2} + \text{Defense}$$

$$\text{Attack chance} = \max(\text{Base}\% - \text{Reduction}\%, 2\%)$$

The final attack chance can never go below 2%. The food and water in these formulas also include that stored in the workbench, meaning that the attack chance may increase for settlements with excess production.

Example: If a settlement has 7 food, 6 water, 2 defense, and a population of 5, the attack chance is calculated as follows:

$$\text{Base} = 2 + \frac{7 + 6}{10} = 3.3$$

$$\text{Reduction} = \frac{5}{2} + 2 = 4.5$$

$$\text{Attack chance} = \max(3.3\% - 4.5\%, 2\%) = \max(-1.2\%, 2\%) = 2\%$$

Success chance
When a settlement attack is triggered (and the quest objective appears) and the player character is away from the targeted settlement, there is a chance it will be able to defend itself without the player character's interference, completing the quest objective. This chance is determined by comparing the defense strength (defense rating + population) + a random number between 1 and 100 and the attack strength ((food + water in the settlement) +/-50%) + a random number between 1 and 100. The defense strength is capped at 100, so it's not useful to have more than (100 - population) defense when not responding to settlement attacks. The amount of food + water and the attack strength is also capped at 100, so the maximum attack strength is 50-100 (100 +/-50% = 50-150, capped at 100). When the random numbers are added to the attack strength and defense strength, the total attack value is capped at 150 but not the defense which can go up to 200. This means that high defense is more likely to win, but even when it's maxed out there is still a fair (30.6%, roughly 1/3) chance to lose to an attack on a "rich" settlement