Skip to content

Commit

Permalink
Fallback to random faction for trader subcores (#4)
Browse files Browse the repository at this point in the history
When a trader has no associated faction (such as for example orbital
traders) choose a random faction to use for subcore information. Fixes
#3.
  • Loading branch information
eth0net authored Aug 15, 2023
2 parents 6bb5f55 + ef3c4e5 commit 54419d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Binary file modified Assemblies/SubcoreInfo.dll
Binary file not shown.
27 changes: 25 additions & 2 deletions Source/Comps/CompSubcoreInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RimWorld;
using System.Linq;
using RimWorld;
using Verse;

namespace SubcoreInfo.Comps
Expand All @@ -25,6 +26,15 @@ public override bool AllowStackWith(Thing other)
return PawnName == otherComp.PawnName && TitleName == otherComp.TitleName && FactionName == otherComp.FactionName;
}

/// <summary>
/// Checks if a random faction could be used to generate subcore information for traders.
/// </summary>
/// <param name="faction">Faction being considered.</param>
private static bool ValidRandomFaction(Faction faction)
{
return faction != null && !faction.IsPlayer && !faction.temporary && !faction.Hidden && faction.def.humanlikeFaction;
}

/// <summary>
/// PostPostGeneratedForTrader is called after the subcore is generated for a trader.
/// </summary>
Expand All @@ -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));
}
}
}
}

0 comments on commit 54419d4

Please sign in to comment.