diff --git a/RimWorldOfMagic/RimWorldOfMagic/CompAbilityUserMagic.cs b/RimWorldOfMagic/RimWorldOfMagic/CompAbilityUserMagic.cs index ca8b2a5e..2faa9e58 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/CompAbilityUserMagic.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/CompAbilityUserMagic.cs @@ -9189,12 +9189,11 @@ public void RemoveAdvancedClass(TMDefs.TM_CustomClass ac) public void UpdateAutocastDef() { - IEnumerable mpDefs = TM_Data.CustomMagePowerDefs(); - if (this.IsMagicUser && this.MagicData != null && this.MagicData.MagicPowersCustom != null) + if (IsMagicUser && MagicData?.MagicPowersCustom != null) { - foreach (MagicPower mp in this.MagicData.MagicPowersCustom) + foreach (MagicPower mp in MagicData.MagicPowersCustom) { - foreach (TM_CustomPowerDef mpDef in mpDefs) + foreach (TM_CustomPowerDef mpDef in TM_Data.CustomMagePowerDefs()) { if (mpDef.customPower.abilityDefs[0].ToString() == mp.GetAbilityDef(0).ToString()) { diff --git a/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs b/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs index 04f5d779..2392788c 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/HarmonyPatches.cs @@ -1670,12 +1670,6 @@ public static void PawnEquipment_Add_Postfix(Pawn_EquipmentTracker __instance, T CompAbilityUserMight comp = p.GetCompAbilityUserMight(); if (p != null && comp != null && (p.story.traits.HasTrait(TorannMagicDefOf.TM_SuperSoldier) || (comp.customClass != null))) { - if (comp.equipmentContainer == null) - { - comp.equipmentContainer = new ThingOwner(); - comp.equipmentContainer.Clear(); - } - if (newEq == p.equipment.Primary) { //Log.Message("adding primary weapon"); diff --git a/RimWorldOfMagic/RimWorldOfMagic/MagicData.cs b/RimWorldOfMagic/RimWorldOfMagic/MagicData.cs index 7c4b07fd..1b6551c3 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/MagicData.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/MagicData.cs @@ -3586,17 +3586,16 @@ public MagicPowerSkill GetSkill_Versatility(TMAbilityDef ability) } if (!hasSkill) //check custom powers for different ability to skill names { - List customPowers = TM_Data.CustomMagePowerDefs().ToList(); - for (int i = 0; i < customPowers.Count; i++) + foreach (TM_CustomPowerDef powerDef in TM_Data.CustomMagePowerDefs()) { - for (int j = 0; j < customPowers[i].customPower.abilityDefs.Count; j++) + for (int j = 0; j < powerDef.customPower.abilityDefs.Count; j++) { - if (ability.defName == customPowers[i].customPower.abilityDefs[j].ToString()) + if (ability.defName == powerDef.customPower.abilityDefs[j].ToString()) { for (int k = 0; k < AllMagicPowerSkills.Count; k++) { MagicPowerSkill mps = AllMagicPowerSkills[k]; - foreach (TM_CustomSkill cs in customPowers[i].customPower.skills) + foreach (TM_CustomSkill cs in powerDef.customPower.skills) { if (cs.label.Contains("_ver") && cs.label == mps.label) { @@ -3638,17 +3637,16 @@ public MagicPowerSkill GetSkill_Power(TMAbilityDef ability) } if (!hasSkill) //check custom powers for different ability to skill names { - List customPowers = TM_Data.CustomMagePowerDefs().ToList(); - for (int i = 0; i < customPowers.Count; i++) + foreach (TM_CustomPowerDef powerDef in TM_Data.CustomMagePowerDefs()) { - for (int j = 0; j < customPowers[i].customPower.abilityDefs.Count; j++) + for (int j = 0; j < powerDef.customPower.abilityDefs.Count; j++) { - if (ability.defName == customPowers[i].customPower.abilityDefs[j].ToString()) + if (ability.defName == powerDef.customPower.abilityDefs[j].ToString()) { for (int k = 0; k < AllMagicPowerSkills.Count; k++) { MagicPowerSkill mps = AllMagicPowerSkills[k]; - foreach (TM_CustomSkill cs in customPowers[i].customPower.skills) + foreach (TM_CustomSkill cs in powerDef.customPower.skills) { if (cs.label.EndsWith("_pwr") && cs.label == mps.label) { diff --git a/RimWorldOfMagic/RimWorldOfMagic/MightData.cs b/RimWorldOfMagic/RimWorldOfMagic/MightData.cs index d447c6f4..96e24479 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/MightData.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/MightData.cs @@ -20,25 +20,15 @@ public class MightData : IExposable private int ticksAffiliation = 0; //public ThingOwner equipmentContainer = new ThingOwner(); - - public bool customPowersInitialized = false; public List mightPowerCustom; public List MightPowersCustom //supports customs abilities { get { - bool flag = this.mightPowerCustom == null; // || !this.customPowersInitialized; - if (flag) + if (mightPowerCustom == null) { - this.customPowersInitialized = true; - IEnumerable enumerable = TM_Data.CustomFighterPowerDefs(); - - if (mightPowerCustom == null || mightPowerCustom.Count <= 0) - { - this.mightPowerCustom = new List(); - this.mightPowerCustom.Clear(); - } - foreach (TM_CustomPowerDef current in enumerable) + mightPowerCustom ??= new List(); + foreach (TM_CustomPowerDef current in TM_Data.CustomFighterPowerDefs()) { bool newPower = false; List abilityList = current.customPower.abilityDefs; @@ -2147,17 +2137,16 @@ public MightPowerSkill GetSkill_Efficiency(TMAbilityDef ability) } if (!hasSkill) //check custom powers for different ability pairing for skill names { - List customPowers = TM_Data.CustomFighterPowerDefs().ToList(); - for (int i = 0; i < customPowers.Count; i++) + foreach (TM_CustomPowerDef powerDef in TM_Data.CustomFighterPowerDefs()) { - for (int j = 0; j < customPowers[i].customPower.abilityDefs.Count; j++) + for (int j = 0; j < powerDef.customPower.abilityDefs.Count; j++) { - if (ability.defName == customPowers[i].customPower.abilityDefs[j].ToString()) + if (ability.defName == powerDef.customPower.abilityDefs[j].ToString()) { for (int k = 0; k < AllMightPowerSkills.Count; k++) { MightPowerSkill mps = AllMightPowerSkills[k]; - foreach (TM_CustomSkill cs in customPowers[i].customPower.skills) + foreach (TM_CustomSkill cs in powerDef.customPower.skills) { if (cs.label.EndsWith("_eff") && cs.label == mps.label) { @@ -2200,17 +2189,16 @@ public MightPowerSkill GetSkill_Versatility(TMAbilityDef ability) } if (!hasSkill) //check custom powers for different ability to skill names { - List customPowers = TM_Data.CustomFighterPowerDefs().ToList(); - for (int i = 0; i < customPowers.Count; i++) + foreach (TM_CustomPowerDef powerDef in TM_Data.CustomFighterPowerDefs()) { - for (int j = 0; j < customPowers[i].customPower.abilityDefs.Count; j++) + for (int j = 0; j < powerDef.customPower.abilityDefs.Count; j++) { - if (ability.defName == customPowers[i].customPower.abilityDefs[j].ToString()) + if (ability.defName == powerDef.customPower.abilityDefs[j].ToString()) { for (int k = 0; k < AllMightPowerSkills.Count; k++) { MightPowerSkill mps = AllMightPowerSkills[k]; - foreach (TM_CustomSkill cs in customPowers[i].customPower.skills) + foreach (TM_CustomSkill cs in powerDef.customPower.skills) { if (cs.label.EndsWith("_ver") && cs.label == mps.label) { @@ -2253,17 +2241,16 @@ public MightPowerSkill GetSkill_Power(TMAbilityDef ability) } if(!hasSkill) //check custom powers for different ability to skill names { - List customPowers = TM_Data.CustomFighterPowerDefs().ToList(); - for (int i = 0; i < customPowers.Count; i++) + foreach (TM_CustomPowerDef powerDef in TM_Data.CustomFighterPowerDefs()) { - for (int j = 0; j < customPowers[i].customPower.abilityDefs.Count; j++) + for (int j = 0; j < powerDef.customPower.abilityDefs.Count; j++) { - if(ability.defName == customPowers[i].customPower.abilityDefs[j].ToString()) + if(ability.defName == powerDef.customPower.abilityDefs[j].ToString()) { for (int k = 0; k < AllMightPowerSkills.Count; k++) { MightPowerSkill mps = AllMightPowerSkills[k]; - foreach(TM_CustomSkill cs in customPowers[i].customPower.skills) + foreach(TM_CustomSkill cs in powerDef.customPower.skills) { if(cs.label.EndsWith("_pwr") && cs.label == mps.label) { @@ -2489,4 +2476,4 @@ public void ExposeData() Scribe_Collections.Look(ref this.mightPowerSkill_SoothingBalm, "mightPowerSkill_SoothingBalm", (LookMode)2, new object[0]); } } -} \ No newline at end of file +} diff --git a/RimWorldOfMagic/RimWorldOfMagic/RimWorldOfMagic.csproj b/RimWorldOfMagic/RimWorldOfMagic/RimWorldOfMagic.csproj index 64404a78..6b1e9b93 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/RimWorldOfMagic.csproj +++ b/RimWorldOfMagic/RimWorldOfMagic/RimWorldOfMagic.csproj @@ -13,6 +13,7 @@ 512 true + 10 true diff --git a/RimWorldOfMagic/RimWorldOfMagic/TM_Calc.cs b/RimWorldOfMagic/RimWorldOfMagic/TM_Calc.cs index 6fd85949..f7409810 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/TM_Calc.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/TM_Calc.cs @@ -3745,219 +3745,151 @@ public static MagicPower GetRandomMagicPower(CompAbilityUserMagic comp, bool inc public static bool IsUsingRanged(Pawn p) { - bool result = false; - if (p != null && p.equipment != null && p.equipment.Primary != null && p.equipment.Primary.def.IsRangedWeapon) - { - result = true; - } - return result; + return p?.equipment?.Primary != null && p.equipment.Primary.def.IsRangedWeapon; } public static bool IsUsingMelee(Pawn p) { - bool result = false; - if (p != null) - { - if (p.equipment != null) - { - if (p.equipment.Primary != null && p.equipment.Primary.def.IsMeleeWeapon) - { - result = true; - } - if (p.equipment.Primary == null) - { - result = true; - } - } - } - return result; + return p?.equipment != null && (p.equipment.Primary == null || p.equipment.Primary.def.IsMeleeWeapon); } public static bool IsUsingPistol(Pawn p) { - bool result = false; - if (TM_Calc.IsUsingRanged(p)) + if (!IsUsingRanged(p)) return false; + + ThingDef weaponDef = p.equipment.Primary.def; + CompAbilityUserMight mightComp = p.GetCompAbilityUserMight(); + //Log.Message("" + p.LabelShort + " is using a " + wpn.def.defName); + if(mightComp.equipmentContainer.Count > 0) { - Thing wpn = p.equipment.Primary; - CompAbilityUserMight mightComp = p.GetCompAbilityUserMight(); - //Log.Message("" + p.LabelShort + " is using a " + wpn.def.defName); - if(mightComp != null && mightComp.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) - { - result = true; - } - else if (wpn.def.defName.ToLower().Contains("pistol") || wpn.def.defName.Contains("SMG") || wpn.def.defName.ToLower().Contains("revolver")) - { - //Log.Message("weapon name contains pistol: " + wpn.def.defName); - result = true; - } - else if(TM_Data.PistolList().Contains(wpn.def)) - { - //Log.Message("weapon found in custom defnames"); - result = true; - } - //else - //{ - // Messages.Message("TM_MustHaveWeaponType".Translate( - // p.LabelShort, - // wpn.LabelShort, - // "pistol" - // ), MessageTypeDefOf.NegativeEvent); - //} + return true; } - //else - //{ - // Messages.Message("MustHaveRangedWeapon".Translate( - // p.LabelCap - // ), MessageTypeDefOf.RejectInput); - //} - return result; + string defNameLower = weaponDef.defName.ToLower(); + if (defNameLower.Contains("pistol") || weaponDef.defName.Contains("SMG") || defNameLower.Contains("revolver")) + { + //Log.Message("weapon name contains pistol: " + wpn.def.defName); + return true; + } + if(TM_Data.PistolSet().Contains(weaponDef)) + { + //Log.Message("weapon found in custom defnames"); + return true; + } + return false; } public static bool IsUsingRifle(Pawn p) { - bool result = false; if (IsUsingRanged(p)) { Thing wpn = p.equipment.Primary; CompAbilityUserMight mightComp = p.GetCompAbilityUserMight(); //Log.Message("" + p.LabelShort + " is using a " + wpn.def.defName); - if (mightComp != null && mightComp.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) + if (mightComp?.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) { - result = true; + return true; } - else if ((wpn.def.defName.ToLower().Contains("rifle") || wpn.def.defName.Contains("LMG")) && !(wpn.def.defName.ToLower().Contains("sniper"))) + if ((wpn.def.defName.ToLower().Contains("rifle") || wpn.def.defName.Contains("LMG")) && !(wpn.def.defName.ToLower().Contains("sniper"))) { //Log.Message("weapon name contains rifle: " + wpn.def.defName); - result = true; + return true; } - else if (TM_Data.RifleList().Contains(wpn.def)) + if (TM_Data.RifleSet().Contains(wpn.def)) { //Log.Message("weapon found in custom defnames"); - result = true; + return true; } - //else - //{ - // Messages.Message("TM_MustHaveWeaponType".Translate( - // p.LabelShort, - // wpn.LabelShort, - // "rifle" - // ), MessageTypeDefOf.NegativeEvent); - //} } - //else - //{ - // Messages.Message("MustHaveRangedWeapon".Translate( - // p.LabelCap - // ), MessageTypeDefOf.RejectInput); - //} - return result; + return false; } public static bool IsUsingShotgun(Pawn p) { - bool result = false; if (IsUsingRanged(p)) { Thing wpn = p.equipment.Primary; CompAbilityUserMight mightComp = p.GetCompAbilityUserMight(); //Log.Message("" + p.LabelShort + " is using a " + wpn.def.defName); - if (mightComp != null && mightComp.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) + if (mightComp?.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) { - result = true; + return true; } - else if (wpn.def.defName.ToLower().Contains("shotgun")) + if (wpn.def.defName.ToLower().Contains("shotgun")) { //Log.Message("weapon name contains shotgun: " + wpn.def.defName); - result = true; + return true; } - else if (TM_Data.ShotgunList().Contains(wpn.def)) + if (TM_Data.ShotgunSet().Contains(wpn.def)) { //Log.Message("weapon found in custom defnames"); - result = true; + return true; } - //else - //{ - // Messages.Message("TM_MustHaveWeaponType".Translate( - // p.LabelShort, - // wpn.LabelShort, - // "shotgun" - // ), MessageTypeDefOf.NegativeEvent); - //} } - //else - //{ - // Messages.Message("MustHaveRangedWeapon".Translate( - // p.LabelCap - // ), MessageTypeDefOf.RejectInput); - //} - return result; + return false; } public static bool IsUsingBow(Pawn p) { - bool result = false; if (IsUsingRanged(p)) { Thing wpn = p.equipment.Primary; CompAbilityUserMight mightComp = p.GetCompAbilityUserMight(); //Log.Message("" + p.LabelShort + " is using a " + wpn.def.defName); - if (mightComp != null && mightComp.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) + if (mightComp?.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) { - result = true; + return true; } - else if ((wpn.def.Verbs.FirstOrDefault().defaultProjectile.projectile.damageDef.defName.ToLower().Contains("arrow") || wpn.def.defName.ToLower().Contains("bow"))) + if (wpn.def.Verbs.First().defaultProjectile.projectile.damageDef.defName.ToLower().Contains("arrow") || wpn.def.defName.ToLower().Contains("bow")) { //Log.Message("weapon name contains shotgun: " + wpn.def.defName); - result = true; + return true; } - else if (TM_Data.BowList().Contains(wpn.def)) + if (TM_Data.BowSet().Contains(wpn.def)) { //Log.Message("weapon found in custom defnames"); - result = true; + return true; } } - return result; + return false; } public static bool IsUsingMagicalFoci(Pawn p) { - bool result = false; - if (p != null && p.equipment != null && p.equipment.Primary != null && p.equipment.Primary.def.IsRangedWeapon) + if (p?.equipment?.Primary != null && p.equipment.Primary.def.IsRangedWeapon) { Thing wpn = p.equipment.Primary; CompAbilityUserMight mightComp = p.GetCompAbilityUserMight(); //Log.Message("" + p.LabelShort + " is using a " + wpn.def.defName); - if (mightComp != null && mightComp.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) + if (mightComp?.equipmentContainer != null && mightComp.equipmentContainer.Count > 0) { - result = true; + return true; } - else if (wpn.def.defName.ToLower().Contains("wand") || wpn.def.defName.ToLower().Contains("staff")) + if (wpn.def.defName.ToLower().Contains("wand") || wpn.def.defName.ToLower().Contains("staff")) { //Log.Message("weapon name contains shotgun: " + wpn.def.defName); - result = true; + return true; } - else if (TM_Data.MagicFociList().Contains(wpn.def)) + if (TM_Data.MagicFociList().Contains(wpn.def)) { //Log.Message("weapon found in custom defnames"); - result = true; + return true; } } - return result; + return false; } public static bool IsUsingCustomWeaponCategory(Pawn p, string str) { - bool result = false; - if (p != null && p.equipment != null && p.equipment.Primary != null) + if (p?.equipment?.Primary != null) { Thing wpn = p.equipment.Primary; if (TM_Data.CustomWeaponCategoryList(str).Contains(wpn.def.defName)) { //Log.Message("weapon found in custom defnames"); - result = true; + return true; } } - return result; + return false; } public static float GetSkillDamage(Pawn p) diff --git a/RimWorldOfMagic/RimWorldOfMagic/TM_Data.cs b/RimWorldOfMagic/RimWorldOfMagic/TM_Data.cs index 6105fcac..7614bffe 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/TM_Data.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/TM_Data.cs @@ -217,7 +217,6 @@ public static List EnabledMagicTraits get { List magicTraits = new List(); - magicTraits.Clear(); if (ModOptions.Settings.Instance.Arcanist) { magicTraits.Add(TorannMagicDefOf.Arcanist); } if (ModOptions.Settings.Instance.FireMage) { magicTraits.Add(TorannMagicDefOf.InnerFire); } @@ -285,10 +284,8 @@ public static List AllClassTraits get { List allClassTraits = new List(); - allClassTraits.Clear(); allClassTraits.AddRange(MightTraits); - allClassTraits.AddRange(MagicTraits); - //allClassTraits.AddRange(TM_ClassUtility.CustomClassTraitDefs); + allClassTraits.AddRange(MagicTraits); return allClassTraits; } } @@ -298,253 +295,123 @@ public static List AllClassConflictTraits get { List allClassConflictTraits = new List(); - allClassConflictTraits.Clear(); allClassConflictTraits.AddRange(AllClassTraits); allClassConflictTraits.Add(TorannMagicDefOf.TM_Gifted); - AllClassConflictTraits.Add(TorannMagicDefOf.PhysicalProdigy); + allClassConflictTraits.Add(TorannMagicDefOf.PhysicalProdigy); return allClassConflictTraits; } } public static List BrandList() { - List tmpList = new List(); - tmpList.Clear(); - tmpList.Add(TorannMagicDefOf.TM_AwarenessBrand); - tmpList.Add(TorannMagicDefOf.TM_EmotionBrand); - tmpList.Add(TorannMagicDefOf.TM_FitnessBrand); - tmpList.Add(TorannMagicDefOf.TM_ProtectionBrand); - tmpList.Add(TorannMagicDefOf.TM_SiphonBrand); - tmpList.Add(TorannMagicDefOf.TM_VitalityBrand); - return tmpList; + return new List + { + TorannMagicDefOf.TM_AwarenessBrand, + TorannMagicDefOf.TM_EmotionBrand, + TorannMagicDefOf.TM_FitnessBrand, + TorannMagicDefOf.TM_ProtectionBrand, + TorannMagicDefOf.TM_SiphonBrand, + TorannMagicDefOf.TM_VitalityBrand + }; } - public static List MagicFociList() + private static HashSet magicFociSet; + public static HashSet MagicFociList() { - List magicFocis = new List(); - magicFocis.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List magicFociList = WeaponCategoryList.Named("TM_Category_MagicalFoci").weaponDefNames; - foreach (ThingDef current in enumerable) - { - for (int i = 0; i < magicFociList.Count; i++) - { - if (current.defName == magicFociList[i].ToString() || magicFociList[i].ToString() == "*") - { - //Log.Message("adding magicFoci def " + current.defName); - magicFocis.AddDistinct(current); - } - } - } - return magicFocis; + return magicFociSet ??= DefDatabase.AllDefs.Where(static def => + WeaponCategoryList.Named("TM_Category_MagicalFoci").weaponDefNames.Contains(def.defName)).ToHashSet(); } public static List CustomWeaponCategoryList(string listDefName) { List customWeaponDefNames = new List(); - customWeaponDefNames.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (def.defName == listDefName) - select def; - foreach(WeaponCategoryList wcl in enumerable) + foreach(WeaponCategoryList wcl in DefDatabase.AllDefs.Where(list => list.defName == listDefName)) { customWeaponDefNames.AddRange(wcl.weaponDefNames); } return customWeaponDefNames; } - public static List BowList() + private static HashSet bowSet; + public static HashSet BowSet() { - List bows = new List(); - bows.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List bowList = WeaponCategoryList.Named("TM_Category_Bows").weaponDefNames; - foreach (ThingDef current in enumerable) - { - for (int i = 0; i < bowList.Count; i++) - { - if (current.defName == bowList[i].ToString() || bowList[i].ToString() == "*") - { - //Log.Message("adding bow def " + current.defName); - bows.AddDistinct(current); - } - } - } - return bows; + return bowSet ??= DefDatabase.AllDefs.Where(static def => + WeaponCategoryList.Named("TM_Category_Bows").weaponDefNames.Contains(def.defName)).ToHashSet(); } - public static List PistolList() + private static HashSet pistolSet; + public static HashSet PistolSet() { - List pistols = new List(); - pistols.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List pistolList = WeaponCategoryList.Named("TM_Category_Pistols").weaponDefNames; - foreach (ThingDef current in enumerable) - { - for (int i = 0; i < pistolList.Count; i++) - { - if (current.defName == pistolList[i].ToString() || pistolList[i].ToString() == "*") - { - //Log.Message("adding pistol def " + current.defName); - pistols.AddDistinct(current); - } - } - } - return pistols; + return pistolSet ??= DefDatabase.AllDefs.Where(static def => + WeaponCategoryList.Named("TM_Category_Pistols").weaponDefNames.Contains(def.defName)).ToHashSet(); } - public static List RifleList() + private static HashSet rifleSet; + public static HashSet RifleSet() { - List rifles = new List(); - rifles.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List rifleList = WeaponCategoryList.Named("TM_Category_Rifles").weaponDefNames; - foreach (ThingDef current in enumerable) - { - for (int i = 0; i < rifleList.Count; i++) - { - if (current.defName == rifleList[i].ToString() || rifleList[i].ToString() == "*") - { - //Log.Message("adding rifle def " + current.defName); - rifles.AddDistinct(current); - } - } - } - return rifles; + return rifleSet ??= DefDatabase.AllDefs.Where(static def => + WeaponCategoryList.Named("TM_Category_Rifles").weaponDefNames.Contains(def.defName)).ToHashSet(); } - public static List ShotgunList() + private static HashSet shotgunSet; + public static HashSet ShotgunSet() { - List shotguns = new List(); - shotguns.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List shotgunList = WeaponCategoryList.Named("TM_Category_Shotguns").weaponDefNames; - foreach (ThingDef current in enumerable) - { - for (int i = 0; i < shotgunList.Count; i++) - { - if (current.defName == shotgunList[i].ToString() || shotgunList[i].ToString() == "*") - { - //Log.Message("adding shotgun def " + current.defName); - shotguns.AddDistinct(current); - } - } - } - return shotguns; + return shotgunSet ??= DefDatabase.AllDefs.Where(static def => + WeaponCategoryList.Named("TM_Category_Shotguns").weaponDefNames.Contains(def.defName)).ToHashSet(); } - public static List AilmentList() + private static HashSet ailmentSet; + public static HashSet AilmentSet() { - List ailments = new List(); - ailments.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List ailmentList = HediffCategoryList.Named("TM_Category_Hediffs").ailments; - foreach (HediffDef current in enumerable) - { - for (int i = 0; i < ailmentList.Count; i++) - { - if (current.defName == ailmentList[i].hediffDefname || (ailmentList[i].containsDefnameString && current.defName.Contains(ailmentList[i].hediffDefname)) || ailmentList[i].ToString() == "*") - { - //Log.Message("adding shotgun def " + current.defName); - ailments.AddDistinct(current); - } - } - } - return ailments; + return ailmentSet ??= DefDatabase.AllDefs.Where(static def => + HediffCategoryList.Named("TM_Category_Hediffs").ailments.Any(hediff => + hediff.hediffDefname == def.defName || + hediff.containsDefnameString && def.defName.Contains(hediff.hediffDefname)) + ).ToHashSet(); } - public static List AddictionList() + private static HashSet addictionSet; + public static HashSet AddictionSet() { - List addictions = new List(); - addictions.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List addictionList = HediffCategoryList.Named("TM_Category_Hediffs").addictions; - foreach (HediffDef current in enumerable) - { - for (int i = 0; i < addictionList.Count; i++) - { - if (current.defName == addictionList[i].hediffDefname || (addictionList[i].containsDefnameString && current.defName.Contains(addictionList[i].hediffDefname)) || addictionList[i].ToString() == "*") - { - //Log.Message("adding shotgun def " + current.defName); - addictions.AddDistinct(current); - } - } - } - return addictions; + return addictionSet ??= DefDatabase.AllDefs.Where(static def => + HediffCategoryList.Named("TM_Category_Hediffs").addictions.Any(hediff => + hediff.hediffDefname == def.defName || + hediff.containsDefnameString && def.defName.Contains(hediff.hediffDefname)) + ).ToHashSet(); } - public static List MechaniteList() + private static HashSet mechaniteSet; + public static HashSet MechaniteSet() { - List mechanites = new List(); - mechanites.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List mechaniteList = HediffCategoryList.Named("TM_Category_Hediffs").mechanites; - foreach (HediffDef current in enumerable) - { - for (int i = 0; i < mechaniteList.Count; i++) - { - if (current.defName == mechaniteList[i].hediffDefname || (mechaniteList[i].containsDefnameString && current.defName.Contains(mechaniteList[i].hediffDefname)) || mechaniteList[i].ToString() == "*") - { - //Log.Message("adding shotgun def " + current.defName); - mechanites.AddDistinct(current); - } - } - } - return mechanites; + return mechaniteSet ??= DefDatabase.AllDefs.Where(static def => + HediffCategoryList.Named("TM_Category_Hediffs").mechanites.Any(hediff => + hediff.hediffDefname == def.defName || + hediff.containsDefnameString && def.defName.Contains(hediff.hediffDefname)) + ).ToHashSet(); } - public static List DiseaseList() + private static HashSet diseaseSet; + public static HashSet DiseaseSet() { - List diseases = new List(); - diseases.Clear(); - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (true) - select def; - List diseaseList = HediffCategoryList.Named("TM_Category_Hediffs").diseases; - foreach (HediffDef current in enumerable) - { - for (int i = 0; i < diseaseList.Count; i++) - { - if (current.defName == diseaseList[i].hediffDefname || (diseaseList[i].containsDefnameString && current.defName.Contains(diseaseList[i].hediffDefname)) || diseaseList[i].ToString() == "*") - { - //Log.Message("adding shotgun def " + current.defName); - diseases.AddDistinct(current); - } - } - } - return diseases; + return diseaseSet ??= DefDatabase.AllDefs.Where(static def => + HediffCategoryList.Named("TM_Category_Hediffs").diseases.Any(hediff => + hediff.hediffDefname == def.defName || + hediff.containsDefnameString && def.defName.Contains(hediff.hediffDefname)) + ).ToHashSet(); } - public static IEnumerable CustomFighterPowerDefs() + private static TM_CustomPowerDef[] customFighterPowerDefs; + public static TM_CustomPowerDef[] CustomFighterPowerDefs() { - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (def.customPower != null && def.customPower.forFighter) - select def; - return enumerable; + return customFighterPowerDefs ??= DefDatabase.AllDefs.Where(static def => + def.customPower is { forFighter: true }).ToArray(); } + private static TM_CustomPowerDef[] customMagePowerDefs; public static IEnumerable CustomMagePowerDefs() { - IEnumerable enumerable = from def in DefDatabase.AllDefs - where (def.customPower != null && def.customPower.forMage) - select def; - return enumerable; + return customMagePowerDefs ??= DefDatabase.AllDefs.Where(static def => + def.customPower is { forMage: true }).ToArray(); } } diff --git a/RimWorldOfMagic/RimWorldOfMagic/Verb_CureDisease.cs b/RimWorldOfMagic/RimWorldOfMagic/Verb_CureDisease.cs index c6392be9..d59081e9 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/Verb_CureDisease.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/Verb_CureDisease.cs @@ -62,7 +62,7 @@ protected override bool TryCastShot() Hediff rec = enumerator.Current; bool flag2 = num > 0; - if (TM_Data.AddictionList().Contains(rec.def)) + if (TM_Data.AddictionSet().Contains(rec.def)) { List diseaseList = HediffCategoryList.Named("TM_Category_Hediffs").diseases; foreach (TMDefs.TM_CategoryHediff chd in diseaseList) diff --git a/RimWorldOfMagic/RimWorldOfMagic/Verb_MechaniteReprogramming.cs b/RimWorldOfMagic/RimWorldOfMagic/Verb_MechaniteReprogramming.cs index ec17a434..f29ece36 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/Verb_MechaniteReprogramming.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/Verb_MechaniteReprogramming.cs @@ -61,7 +61,7 @@ protected override bool TryCastShot() bool flag2 = num > 0; - if (TM_Data.MechaniteList().Contains(rec.def)) + if (TM_Data.MechaniteSet().Contains(rec.def)) { foreach(TMDefs.TM_CategoryHediff chd in mechaniteList) { diff --git a/RimWorldOfMagic/RimWorldOfMagic/Verb_Purify.cs b/RimWorldOfMagic/RimWorldOfMagic/Verb_Purify.cs index 50c2b383..f64411e0 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/Verb_Purify.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/Verb_Purify.cs @@ -112,7 +112,7 @@ protected override bool TryCastShot() bool flag2 = num > 0; if (flag2) { - if (TM_Data.AilmentList().Contains(rec.def)) + if (TM_Data.AilmentSet().Contains(rec.def)) { List ailmentList = HediffCategoryList.Named("TM_Category_Hediffs").ailments; foreach(TMDefs.TM_CategoryHediff chd in ailmentList) @@ -206,7 +206,7 @@ protected override bool TryCastShot() bool flag2 = num > 0; if (flag2) { - if (TM_Data.AddictionList().Contains(rec.def)) + if (TM_Data.AddictionSet().Contains(rec.def)) { List addictionList = HediffCategoryList.Named("TM_Category_Hediffs").addictions; foreach (TMDefs.TM_CategoryHediff chd in addictionList)