diff --git a/Source/CombatExtended/Harmony/Harmony_StartingPawnUtility.cs b/Source/CombatExtended/Harmony/Harmony_StartingPawnUtility.cs new file mode 100644 index 0000000000..a2e75e08c0 --- /dev/null +++ b/Source/CombatExtended/Harmony/Harmony_StartingPawnUtility.cs @@ -0,0 +1,40 @@ +using HarmonyLib; +using Verse; +using System.Linq; +using System.Collections.Generic; + +namespace CombatExtended.HarmonyCE +{ + [HarmonyPatch(typeof(StartingPawnUtility), nameof(StartingPawnUtility.GeneratePossessions))] + internal static class StartingPawnUtility_GenerateAmmo + { + static IntRange magRange = new IntRange(3, 5); + + internal static void Postfix(Pawn pawn) + { + var startingPossessions = Find.GameInitData.startingPossessions; + if (startingPossessions.ContainsKey(pawn)) + { + List ammoList = new List(); + foreach (var possession in startingPossessions[pawn]) + { + if (possession.thingDef.GetCompProperties() is CompProperties_AmmoUser ammoUser && ammoUser.ammoSet != null) + { + int count = ammoUser.AmmoGenPerMagOverride; + if (count <= 0) + { + count = ammoUser.magazineSize; + } + count *= magRange.RandomInRange; + + ammoList.Add(new ThingDefCount(ammoUser.ammoSet.ammoTypes.First().ammo, count)); + } + } + foreach (var ammo in ammoList) + { + startingPossessions[pawn].Add(ammo); + } + } + } + } +}