From e1142201eb158a0d799286733aef98e1704c304b Mon Sep 17 00:00:00 2001 From: CMDR-Bill-Doors Date: Wed, 4 Sep 2024 04:49:17 +0100 Subject: [PATCH] Update ThingSetMaker_CountEnabledAmmoOnly.cs --- .../ThingSetMaker_CountEnabledAmmoOnly.cs | 87 ++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/Source/CombatExtended/ThingSetMaker_CountEnabledAmmoOnly.cs b/Source/CombatExtended/ThingSetMaker_CountEnabledAmmoOnly.cs index fd3b53f483..ec4a5b4fb4 100644 --- a/Source/CombatExtended/ThingSetMaker_CountEnabledAmmoOnly.cs +++ b/Source/CombatExtended/ThingSetMaker_CountEnabledAmmoOnly.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using RimWorld; using Verse; @@ -6,17 +7,99 @@ namespace CombatExtended { public class ThingSetMaker_CountEnabledAmmoOnly : ThingSetMaker_Count { - public override void Generate(ThingSetMakerParams parms, List outThings) + bool basic = true; + + bool advanced = false; + + bool CanSpawn(AmmoDef def) + { + if (def.ammoClass != null) + { + return def.ammoClass.advanced ? basic : advanced; + } + return true; + } + + protected override void Generate(ThingSetMakerParams parms, List outThings) + { + parms.validator = delegate (ThingDef d) + { + if (d is AmmoDef ammodef && (ammodef.tradeTags?.Contains(AmmoInjector.destroyWithAmmoDisabledTag) ?? false)) + { + return AmmoUtility.IsAmmoSystemActive(ammodef) && CanSpawn(ammodef); + } + return true; + }; + base.Generate(parms, outThings); + } + } + public class ThingSetMaker_StackCountEnabledAmmoOnly : ThingSetMaker_StackCount + { + bool basic = true; + + bool advanced = false; + + bool CanSpawn(AmmoDef def) + { + if (def.ammoClass != null) + { + return def.ammoClass.advanced ? basic : advanced; + } + return true; + } + + protected override void Generate(ThingSetMakerParams parms, List outThings) { parms.validator = delegate (ThingDef d) { if (d is AmmoDef ammodef && (ammodef.tradeTags?.Contains(AmmoInjector.destroyWithAmmoDisabledTag) ?? false)) { - return AmmoUtility.IsAmmoSystemActive(ammodef); + return AmmoUtility.IsAmmoSystemActive(ammodef) && CanSpawn(ammodef); } return true; }; base.Generate(parms, outThings); } } + public class ThingSetMaker_CountWithAmmo : ThingSetMaker_Count + { + IntRange magCount = new IntRange(2, 5); + + bool random; + + bool canGenerateAdvanced; + + protected override void Generate(ThingSetMakerParams parms, List outThings) + { + base.Generate(parms, outThings); + List ammos = new List(); + foreach (var thing in outThings) + { + if (thing.TryGetComp() is CompAmmoUser ammoUser && ammoUser.UseAmmo) + { + Thing ammo = ThingMaker.MakeThing(GetAmmoDef(ammoUser.Props)); + ammo.stackCount = Math.Max(Math.Max(ammoUser.Props.AmmoGenPerMagOverride, ammoUser.Props.magazineSize), 1) * magCount.RandomInRange; + ammos.Add(ammo); + } + } + foreach (var t in ammos) + { + outThings.Add(t); + } + } + + ThingDef GetAmmoDef(CompProperties_AmmoUser comp) + { + if (random) + { + var ammoDefs = comp.ammoSet.ammoTypes.Select(v => v.ammo).Where(d => canGenerateAdvanced ? true : !d.ammoClass.advanced); + if (ammoDefs.EnumerableNullOrEmpty()) + { + ammoDefs = comp.ammoSet.ammoTypes.Select(v => v.ammo); + } + return ammoDefs.RandomElement(); + } + return comp.ammoSet.ammoTypes.First().ammo; + } + } }