<?xml version="1.0" encoding="utf-8"?>
<Patch>

	<!-- ========== General ========== -->

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="Flammability"]</xpath>
		<value>
			<workerClass>CombatExtended.StatWorker_Flammability</workerClass>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="Flammability"]/category</xpath>
		<value>
			<category>Basics</category>
		</value>
	</Operation>

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="Mass"]/parts</xpath>
		<value>
			<li Class="CombatExtended.StatPart_LoadedAmmo" />
			<li Class="CombatExtended.StatPart_Attachments" />
		</value>
	</Operation>

	<!-- ========== Apparel ========== -->

	<!-- Armor Cap -->

	<Operation Class="PatchOperationConditional">
		<xpath>Defs/StatDef[@Name="ArmorRatingBase"]/maxValue</xpath>
		<match Class="PatchOperationRemove">
			<xpath>Defs/StatDef[@Name="ArmorRatingBase"]/maxValue</xpath>
		</match>
	</Operation>

	<!-- Armor Rating -->

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[@Name="ArmorRatingBase"]</xpath>
		<value>
			<workerClass>CombatExtended.StatWorker_ArmorPartial</workerClass>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[@Name="ArmorRatingBase"]/parts/li[@Class="StatPart_Quality"]</xpath>
		<value>
			<li Class="CombatExtended.StatPart_NaturalArmorDurability" />
			<li Class="CombatExtended.StatPart_QualityConditional">
				<factorAwful>0.6</factorAwful>
				<factorPoor>0.8</factorPoor>
				<factorNormal>1</factorNormal>
				<factorGood>1.15</factorGood>
				<factorExcellent>1.3</factorExcellent>
				<factorMasterwork>1.5</factorMasterwork>
				<factorLegendary>1.75</factorLegendary>
			</li>
			<li Class="StatPart_Health">
				<curve>
					<points>
						<li>(0.0, 0.0)</li>
						<li>(0.5, 0.5)</li>
						<li>(0.8, 1.0)</li>
					</points>
				</curve>
			</li>
		</value>
	</Operation>

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="ArmorRating_Sharp"]</xpath>
		<value>
			<toStringStyle>FloatMaxTwo</toStringStyle>
		</value>
	</Operation>

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="ArmorRating_Blunt"]</xpath>
		<value>
			<toStringStyle>FloatMaxThree</toStringStyle>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="StuffPower_Armor_Blunt" or defName="StuffPower_Armor_Sharp"]/toStringStyle</xpath>
		<value>
			<toStringStyle>FloatMaxThree</toStringStyle>
		</value>
	</Operation>

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="ArmorRating_Blunt" or defName="StuffPower_Armor_Blunt"]</xpath>
		<value>
			<formatString>{0} MPa</formatString>
		</value>
	</Operation>

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="ArmorRating_Sharp" or defName="StuffPower_Armor_Sharp"]</xpath>
		<value>
			<formatString>{0} mm RHA</formatString>
		</value>
	</Operation>

	<!-- Armor Descriptions -->

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ArmorRating_Sharp"]/description</xpath>
		<value>
			<description>Mitigation effect against sharp attacks, such as bullets, stabs, slashes, scratches and bites.\n\nWhen a sharp attack meets armor, this rating is used to reduce the attack's penetration and damage, the stopped part of the attack decaying into a blunt attack. As the armor rating increases, more of the sharp attack is converted into a blunt one, until the rating exceeds the penetration amount and the attack fully decays into a blunt one. For example, a sharp attack with 10 mm RHA penetration after 8 mm RHA of armor keeps 20% of its damage and penetration, with 80% of its blunt penetration being used for a residual blunt attack.\n\nThe reduction effect applies per layer, with each layer (including natural armor) decaying the sharp attack into a blunt one until the attack pierces through all layers or fully decays.\n\nThis rating also doubles as an armor's resistance against being damaged by sharp attacks. Hard armors (armor vests, full plate) take more damage the closer a sharp attack's penetration amount is to the armor rating, whereas soft armors (parkas, dusters) take the amount of damage stopped or at least 20% of the attack's damage. The damage and penetration reduction per layer also affects armor damage, with upper layers reducing the damage inner layers take.\n\nThe internal formulas used:\n - For reducing a sharp attack's damage and penetration per layer:\n    Remaining_Sharp_Penetration = Max(0, Sharp_Penetration - Sharp_Armor_Rating)\n	Sharp_Damage = Sharp_Damage * Remaining_Sharp_Penetration / Sharp_Penetration\n	Sharp_Penetration = Remaining_Sharp_Penetration\n\n - For the residual blunt attack's damage and penetration:\n	Stopped_Sharp_Penetration = Intial_Sharp_Penetration - Final_Sharp_Penetration\n	Blunt_Penetration = Blunt_Penetration * Stopped_Sharp_Penetration / Intial_Sharp_Penetration\n    Blunt_Damage = (Blunt_Penetration * 10) ^ (1 / 3)</description>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ArmorRating_Blunt"]/description</xpath>
		<value>
			<description>Mitigation effect against blunt attacks, such as punches, mace swings, explosions and animal hoofs.\n\nWhen a blunt attack meets armor, this rating is used to reduce the attack's penetration and damage. As the armor rating increases, more of the blunt attack is reduced, until the rating exceeds the penetration amount and the attack is completely stopped. For example, a blunt attack with 10 MPa after 8 MPa of armor keeps 20% of its damage and penetration.\n\nThe reduction effect applies per layer, with each layer (including natural armor) reducing the blunt attack until it travels through all layers or is completely stopped.\n\nThis rating also doubles as an armor's resistance against being damaged by blunt attacks. Hard armors (armor vests, full plate) take more damage the closer a blunt attack's penetration amount is to the armor rating, whereas soft armors (parkas, dusters) take no damage. The damage and penetration reduction per layer also affects armor damage, with upper layers reducing the damage inner layers take.\n\nThe internal formula used for reducing a blunt attack's damage and penetration per layer:\n  Remaining_Blunt_Penetration = Max(0, Blunt_Penetration - Blunt_Armor_Rating)\n  Blunt_Damage = Blunt_Damage * Remaining_Blunt_Penetration / Blunt_Penetration\n  Blunt_Penetration = Remaining_Blunt_Penetration</description>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ArmorRating_Heat"]/description</xpath>
		<value>
			<description>Percentage reduction of attack damage from extreme heat or cold, such as fire, incendiary explosions, or frostbite-inflicting attacks.\n\nThe total across all sources of this armor is added for a protected part, then reduced by the armor reduction value of the incoming attack. If the resulting value is 100% or greater, protected parts of the pawn will be immune to the incoming attack's hot or cold damage.</description>
		</value>
	</Operation>

	<!-- ========== Pawn stats ========== -->

	<!-- Melee Hit Chance -->

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeHitChance"]/postProcessCurve/points</xpath>
		<value>
			<points>
				<li>(-20, 0.10)</li>
				<li>(-10, 0.20)</li>
				<li>(0, 0.60)</li>
				<li>(10, 0.90)</li>
				<li>(20, 1.00)</li>
			</points>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeHitChance"]/noSkillOffset</xpath>
		<value>
			<noSkillOffset>6.7</noSkillOffset>
		</value>
	</Operation>

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="MeleeHitChance"]/parts</xpath>
		<value>
			<li Class="CombatExtended.StatPart_Bulk" />
		</value>
	</Operation>

	<!-- Melee Dodge Chance -->

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="MeleeDodgeChance"]</xpath>
		<value>
			<workerClass>CombatExtended.StatWorker_MoveSpeed</workerClass>
			<maxValue>3.2</maxValue>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeDodgeChance"]/defaultBaseValue</xpath>
		<value>
			<defaultBaseValue>1</defaultBaseValue>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeDodgeChance"]/skillNeedOffsets</xpath>
		<value>
			<skillNeedFactors>
				<li Class="SkillNeed_BaseBonus">
					<skill>Melee</skill>
					<baseValue>0.05</baseValue>
					<bonusPerLevel>0.0175</bonusPerLevel>
				</li>
			</skillNeedFactors>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeDodgeChance"]/capacityOffsets</xpath>
		<value>
			<capacityFactors>
				<li>
					<capacity>Moving</capacity>
					<weight>1</weight>
				</li>
				<li>
					<capacity>Sight</capacity>
					<weight>0.7</weight>
					<max>1</max>
				</li>
			</capacityFactors>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeDodgeChance"]/postProcessCurve</xpath>
		<value>
			<postProcessCurve>
				<points>
					<li>(0.0, 0.0)</li>
					<li>(0.3, 0.3)</li>
					<li>(0.6, 0.45)</li>
					<li>(1.2, 0.6)</li>
					<li>(2.4, 0.75)</li>
					<li>(3.2, 0.8)</li>
				</points>
			</postProcessCurve>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeDodgeChance"]/displayPriorityInCategory</xpath>
		<value>
			<displayPriorityInCategory>99</displayPriorityInCategory>
		</value>
	</Operation>

	<!-- Shooting Accuracy -->

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/label</xpath>
		<value>
			<label>weapon handling</label>
		</value>
	</Operation>

	<Operation Class="PatchOperationConditional">
		<xpath>Defs/ThingDef[defName="ShootingAccuracyPawn"]/workerClass</xpath>
		<match Class="PatchOperationRemove">
			<xpath>Defs/ThingDef[defName="ShootingAccuracyPawn"]/workerClass</xpath>
		</match>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/description</xpath>
		<value>
			<description>How well a shooter can hold a gun steady when aiming and compensate for recoil.\n\nThe total sway is calculated as:\n(4.5 - weapon handling) * weapon sway factor\n\nThe recoil per shot is determined by multiplying this value against the weapon's inherent recoil amount and increases after every shot in a burst.</description>
		</value>
	</Operation>

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]</xpath>
		<value>
			<maxValue>13</maxValue>
		</value>
	</Operation>

	<Operation Class="PatchOperationConditional">
		<xpath>Defs/ThingDef[defName="ShootingAccuracyPawn"]/skillNeedOffsets/li[@Class="SkillNeed_Direct"]</xpath>
		<match Class="PatchOperationRemove">
			<xpath>Defs/ThingDef[defName="ShootingAccuracyPawn"]/workerClass</xpath>
		</match>
	</Operation>

	<Operation Class="PatchOperationSequence">
		<!-- compatibility with any mod that redefines ShootingAccuracy and changes SkillNeed back to the traditional pre-A17 values per skill level (e.g. Shooting Skill Rebalanced) -->
		<success>Always</success>
		<operations>
			<li Class="PatchOperationTest">
				<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/skillNeedOffsets/li[@Class="SkillNeed_Direct"]</xpath>
			</li>
			<li Class="PatchOperationReplace">
				<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/skillNeedOffsets/li[@Class="SkillNeed_Direct"]</xpath>
				<value>
					<li Class="SkillNeed_BaseBonus" />
				</value>
			</li>
		</operations>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/skillNeedOffsets/li[@Class="SkillNeed_BaseBonus"]</xpath>
		<value>
			<li Class="SkillNeed_BaseBonus">
				<skill>Shooting</skill>
				<baseValue>1</baseValue>
				<bonusPerLevel>0.2</bonusPerLevel>
			</li>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/capacityOffsets</xpath>
		<value>
			<capacityOffsets>
				<li>
					<capacity>Manipulation</capacity>
					<scale>1.6</scale>
				</li>
			</capacityOffsets>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/postProcessCurve</xpath>
		<value>
			<postProcessCurve>
				<points>
					<li>(0.0, 0.0)</li>
					<li>(2.0, 2.0)</li>
					<li>(3.0, 2.5)</li>
					<li>(4.0, 2.75)</li>
					<li>(5.0, 2.875)</li>
					<li>(13, 4.5)</li>
				</points>
			</postProcessCurve>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="ShootingAccuracyPawn"]/noSkillOffset</xpath>
		<value>
			<noSkillFactor>2.6</noSkillFactor>
		</value>
	</Operation>

	<!-- Aiming Delay Factor -->

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="AimingDelayFactor"]</xpath>
		<value>
			<maxValue>2.0</maxValue>
			<skillNeedFactors>
				<li Class="SkillNeed_Direct">
					<skill>Shooting</skill>
					<valuesPerLevel>
						<li>1.25</li>
						<li>1.2</li>
						<li>1.17</li>
						<li>1.15</li>
						<li>1.12</li>
						<li>1.09</li>
						<li>1.06</li>
						<li>1.03</li>
						<li>1.0</li>
						<li>0.98</li>
						<li>0.96</li>
						<li>0.94</li>
						<li>0.92</li>
						<li>0.90</li>
						<li>0.88</li>
						<li>0.86</li>
						<li>0.84</li>
						<li>0.82</li>
						<li>0.80</li>
						<li>0.78</li>
						<li>0.75</li>
					</valuesPerLevel>
				</li>
			</skillNeedFactors>
			<capacityFactors>
				<li>
					<capacity>Manipulation</capacity>
					<weight>1</weight>
					<useReciprocal>true</useReciprocal>
				</li>
				<li>
					<capacity>Sight</capacity>
					<weight>0.7</weight>
					<useReciprocal>true</useReciprocal>
				</li>
			</capacityFactors>
			<postProcessCurve>
				<points>
					<li>(0.01, 0.5)</li>
					<li>(0.75, 0.75)</li>
					<li>(1.0, 1.0)</li>
					<li>(1.25, 1.25)</li>
					<li>(2.0, 1.5)</li>
				</points>
			</postProcessCurve>
		</value>
	</Operation>

	<!-- Move speed -->

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="MoveSpeed"]</xpath>
		<value>
			<workerClass>CombatExtended.StatWorker_MoveSpeed</workerClass>
		</value>
	</Operation>

	<!-- Work speed -->

	<Operation Class="PatchOperationAdd">
		<xpath>Defs/StatDef[defName="WorkSpeedGlobal"]</xpath>
		<value>
			<workerClass>CombatExtended.StatWorker_WorkSpeedGlobal</workerClass>
		</value>
	</Operation>

	<!-- Ranged weapon cooldown quality factor  -->

	<Operation Class="PatchOperationConditional">
		<xpath>Defs/StatDef[defName="RangedWeapon_Cooldown"]/parts</xpath>
		<nomatch Class="PatchOperationAdd">
			<xpath>Defs/StatDef[defName="RangedWeapon_Cooldown"]</xpath>
			<value>
				<parts>
					<li Class="StatPart_Quality">
						<factorAwful>1.1</factorAwful>
						<factorPoor>1.05</factorPoor>
						<factorNormal>1</factorNormal>
						<factorGood>1</factorGood>
						<factorExcellent>0.95</factorExcellent>
						<factorMasterwork>0.9</factorMasterwork>
						<factorLegendary>0.8</factorLegendary>
					</li>
				</parts>
			</value>
		</nomatch>
		<match Class="PatchOperationReplace">
			<xpath>Defs/StatDef[defName="RangedWeapon_Cooldown"]/parts</xpath>
			<value>
				<parts>
					<li Class="StatPart_Quality">
						<factorAwful>1.1</factorAwful>
						<factorPoor>1.05</factorPoor>
						<factorNormal>1</factorNormal>
						<factorGood>1</factorGood>
						<factorExcellent>0.95</factorExcellent>
						<factorMasterwork>0.9</factorMasterwork>
						<factorLegendary>0.8</factorLegendary>
					</li>
				</parts>
			</value>
		</match>
	</Operation>

	<!-- Tweak the ranged weapon damage and pen multipliers -->

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="RangedWeapon_DamageMultiplier"]/parts</xpath>
		<value>
			<parts>
				<li Class="StatPart_Quality">
					<factorAwful>0.9</factorAwful>
					<factorPoor>1</factorPoor>
					<factorNormal>1</factorNormal>
					<factorGood>1</factorGood>
					<factorExcellent>1</factorExcellent>
					<factorMasterwork>1.05</factorMasterwork>
					<factorLegendary>1.15</factorLegendary>
				</li>
			</parts>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="RangedWeapon_ArmorPenetrationMultiplier"]/parts/li[@Class="StatPart_Quality"]</xpath>
		<value>
			<li Class="StatPart_Quality">
				<factorAwful>0.9</factorAwful>
				<factorPoor>1</factorPoor>
				<factorNormal>1</factorNormal>
				<factorGood>1</factorGood>
				<factorExcellent>1</factorExcellent>
				<factorMasterwork>1.05</factorMasterwork>
				<factorLegendary>1.15</factorLegendary>
			</li>
		</value>
	</Operation>

	<!-- Melee DPS -->

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeWeapon_AverageDPS"]/workerClass</xpath>
		<value>
			<workerClass>CombatExtended.StatWorker_MeleeDamageAverage</workerClass>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeWeapon_AverageDPS"]/displayPriorityInCategory</xpath>
		<value>
			<displayPriorityInCategory>6</displayPriorityInCategory>
		</value>
	</Operation>

	<!-- Melee AP -->

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeWeapon_AverageArmorPenetration"]/workerClass</xpath>
		<value>
			<workerClass>CombatExtended.StatWorker_MeleeArmorPenetration</workerClass>
		</value>
	</Operation>

	<Operation Class="PatchOperationReplace">
		<xpath>Defs/StatDef[defName="MeleeWeapon_AverageArmorPenetration"]/toStringStyle</xpath>
		<value>
			<toStringStyle>FloatTwo</toStringStyle>
		</value>
	</Operation>

</Patch>