diff --git a/Assemblies/SubcoreInfo.dll b/Assemblies/SubcoreInfo.dll index 01b6222..ee4b22a 100644 Binary files a/Assemblies/SubcoreInfo.dll and b/Assemblies/SubcoreInfo.dll differ diff --git a/Source/Comps/CompSubcoreInfo.cs b/Source/Comps/CompSubcoreInfo.cs index dfd04c4..83340cd 100644 --- a/Source/Comps/CompSubcoreInfo.cs +++ b/Source/Comps/CompSubcoreInfo.cs @@ -1,4 +1,5 @@ -using RimWorld; +using System.Linq; +using RimWorld; using Verse; namespace SubcoreInfo.Comps @@ -25,6 +26,15 @@ public override bool AllowStackWith(Thing other) return PawnName == otherComp.PawnName && TitleName == otherComp.TitleName && FactionName == otherComp.FactionName; } + /// + /// Checks if a random faction could be used to generate subcore information for traders. + /// + /// Faction being considered. + private static bool ValidRandomFaction(Faction faction) + { + return faction != null && !faction.IsPlayer && !faction.temporary && !faction.Hidden && faction.def.humanlikeFaction; + } + /// /// PostPostGeneratedForTrader is called after the subcore is generated for a trader. /// @@ -37,7 +47,20 @@ public override void PostPostGeneratedForTrader(TraderKindDef trader, int forTil if (!SubcoreInfoSettings.randomTraderInfo) { return; } - Copy(PawnGenerator.GeneratePawn(forFaction.RandomPawnKind(), forFaction)); + var pawnFaction = forFaction; + if (pawnFaction == null) + { + var randomFactions = Find.FactionManager.AllFactions.Where(ValidRandomFaction).ToList(); + if (!randomFactions.NullOrEmpty()) + { + pawnFaction = randomFactions.RandomElement(); + } + } + + if (pawnFaction != null) + { + Copy(PawnGenerator.GeneratePawn(pawnFaction.RandomPawnKind(), forFaction)); + } } } }