280 lines
12 KiB
C#
280 lines
12 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using BlaxCastes;
|
|
using RimWorld;
|
|
using RimWorld.Planet;
|
|
using Verse;
|
|
|
|
namespace BlaxCastesTests
|
|
{
|
|
/// <summary>
|
|
/// Shared helpers for tests. Pawns and castes made here are removed after each test unless passed to
|
|
/// Keep() or KeepCaste(). Mod settings, join rows and the default caste are restored after each test,
|
|
/// so tests may change them freely.
|
|
/// </summary>
|
|
public static class Fixtures
|
|
{
|
|
private static readonly List<Pawn> made = new List<Pawn>();
|
|
private static readonly HashSet<Pawn> kept = new HashSet<Pawn>();
|
|
private static readonly List<Caste> madeCastes = new List<Caste>();
|
|
private static readonly HashSet<Caste> keptCastes = new HashSet<Caste>();
|
|
private static readonly List<QuestPart_ExtraFaction> lodgerParts = new List<QuestPart_ExtraFaction>();
|
|
private static List<(string reason, JoinRowMode mode, Caste caste)> rowSnapshot;
|
|
private static Caste defaultSnapshot;
|
|
private static bool keepCasteState;
|
|
|
|
public static Map Map => Find.CurrentMap;
|
|
|
|
public static BlaxCastesSettings Settings => BlaxCastesMod.Settings;
|
|
|
|
public static GameComponent_BlaxCastes Comp => GameComponent_BlaxCastes.Instance;
|
|
|
|
// ---- factions --------------------------------------------------------------------
|
|
|
|
/// <summary>A visible, non-player, humanlike faction that is not hostile to the player.</summary>
|
|
public static Faction FriendlyFaction() =>
|
|
Find.FactionManager.AllFactionsVisible
|
|
.Where(f => !f.IsPlayer && !f.defeated && f.def.humanlikeFaction && !f.HostileTo(Faction.OfPlayer))
|
|
.OrderBy(f => f.loadID).FirstOrDefault();
|
|
|
|
/// <summary>A visible, non-player, humanlike faction hostile to the player.</summary>
|
|
public static Faction HostileFaction() =>
|
|
Find.FactionManager.AllFactionsVisible
|
|
.Where(f => !f.IsPlayer && !f.defeated && f.def.humanlikeFaction && f.HostileTo(Faction.OfPlayer))
|
|
.OrderBy(f => f.loadID).FirstOrDefault();
|
|
|
|
// ---- pawns -----------------------------------------------------------------------
|
|
|
|
/// <summary>A fresh pawn, not spawned. Relations off so no accidental family ties.</summary>
|
|
public static Pawn Generate(PawnKindDef kind, Faction faction, DevelopmentalStage stage = DevelopmentalStage.Adult)
|
|
{
|
|
var req = new PawnGenerationRequest(kind, faction, PawnGenerationContext.NonPlayer,
|
|
forceGenerateNewPawn: true, allowDowned: stage.Baby(), canGeneratePawnRelations: false,
|
|
colonistRelationChanceFactor: 0f, developmentalStages: stage);
|
|
Pawn p = PawnGenerator.GeneratePawn(req);
|
|
made.Add(p);
|
|
return p;
|
|
}
|
|
|
|
public static Pawn Spawn(Pawn p)
|
|
{
|
|
IntVec3 cell = CellFinder.RandomClosewalkCellNear(Map.Center, Map, 12);
|
|
GenSpawn.Spawn(p, cell, Map);
|
|
return p;
|
|
}
|
|
|
|
/// <summary>A spawned free adult colonist.</summary>
|
|
public static Pawn Colonist() => Spawn(Generate(PawnKindDefOf.Colonist, Faction.OfPlayer));
|
|
|
|
/// <summary>A spawned slave of the colony. Skips the test without Ideology.</summary>
|
|
public static Pawn ColonySlave(Faction origin = null)
|
|
{
|
|
if (!ModsConfig.IdeologyActive)
|
|
T.Skip("slaves need Ideology");
|
|
Pawn p = Generate(PawnKindDefOf.Slave, origin ?? FriendlyFaction());
|
|
Spawn(p);
|
|
p.guest.SetGuestStatus(Faction.OfPlayer, GuestStatus.Slave);
|
|
T.Check(p.IsSlaveOfColony, "slave fixture is not a slave of the colony");
|
|
return p;
|
|
}
|
|
|
|
/// <summary>A spawned prisoner of the colony, from a hostile faction when there is one.</summary>
|
|
public static Pawn Prisoner()
|
|
{
|
|
Faction from = HostileFaction() ?? FriendlyFaction();
|
|
Pawn p = Spawn(Generate(PawnKindDefOf.Slave, from));
|
|
p.guest.SetGuestStatus(Faction.OfPlayer, GuestStatus.Prisoner);
|
|
T.Check(p.IsPrisonerOfColony, "prisoner fixture is not a prisoner of the colony");
|
|
return p;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A spawned colonist that is a quest lodger: an ongoing quest gives it an extra home faction,
|
|
/// the way vanilla lodger quests do. The quest part is removed again after the test.
|
|
/// </summary>
|
|
public static Pawn QuestLodger()
|
|
{
|
|
Faction home = FriendlyFaction();
|
|
if (home == null)
|
|
T.Skip("no friendly faction to lodge from");
|
|
Pawn p = Colonist();
|
|
Quest quest = Quest.MakeRaw();
|
|
quest.acceptanceTick = Find.TickManager.TicksGame;
|
|
var part = new QuestPart_ExtraFaction { extraFaction = new ExtraFaction(home, ExtraFactionType.HomeFaction) };
|
|
part.affectedPawns.Add(p);
|
|
quest.AddPart(part);
|
|
Find.QuestManager.ExtraFactionQuestParts.Add(part);
|
|
lodgerParts.Add(part);
|
|
T.Check(p.IsQuestLodger(), "lodger fixture is not a quest lodger (QuestUtility changed?)");
|
|
return p;
|
|
}
|
|
|
|
/// <summary>A spawned baby of the colony. Skips the test without Biotech.</summary>
|
|
public static Pawn Baby()
|
|
{
|
|
if (!ModsConfig.BiotechActive)
|
|
T.Skip("babies need Biotech");
|
|
Pawn p = Spawn(Generate(PawnKindDefOf.Colonist, Faction.OfPlayer, DevelopmentalStage.Baby));
|
|
T.Check(p.DevelopmentalStage.Baby(), "baby fixture is " + p.DevelopmentalStage + ", not a baby");
|
|
return p;
|
|
}
|
|
|
|
/// <summary>Exclude a pawn from the per-test cleanup (phase-1 setup for phase-2 checks).</summary>
|
|
public static Pawn Keep(Pawn p)
|
|
{
|
|
kept.Add(p);
|
|
return p;
|
|
}
|
|
|
|
public static Pawn FindPawnById(string thingId)
|
|
{
|
|
if (thingId == null) return null;
|
|
foreach (Map m in Find.Maps)
|
|
foreach (Pawn p in m.mapPawns.AllPawns)
|
|
if (p.ThingID == thingId) return p;
|
|
foreach (Pawn p in Find.WorldPawns.AllPawnsAliveOrDead)
|
|
if (p.ThingID == thingId) return p;
|
|
return null;
|
|
}
|
|
|
|
// ---- castes and policies ---------------------------------------------------------
|
|
|
|
/// <summary>A new caste through CasteManager, deleted after the test unless passed to KeepCaste().</summary>
|
|
public static Caste NewCaste(string label = null)
|
|
{
|
|
Caste c = CasteManager.CreateCaste(label);
|
|
T.NotNull(c, "CasteManager.CreateCaste");
|
|
madeCastes.Add(c);
|
|
return c;
|
|
}
|
|
|
|
public static Caste KeepCaste(Caste c)
|
|
{
|
|
keptCastes.Add(c);
|
|
return c;
|
|
}
|
|
|
|
/// <summary>Leave the join rows and the default caste as this test set them (phase-1 setup).</summary>
|
|
public static void KeepCasteState() => keepCasteState = true;
|
|
|
|
public static ApparelPolicy Outfit(string labelKey) =>
|
|
Current.Game.outfitDatabase.AllOutfits.FirstOrDefault(o => o.label == (string)labelKey.Translate());
|
|
|
|
public static FoodPolicy Food(string labelKey) =>
|
|
Current.Game.foodRestrictionDatabase.AllFoodRestrictions.FirstOrDefault(o => o.label == (string)labelKey.Translate());
|
|
|
|
public static DrugPolicy Drug(string sourceDefName) =>
|
|
Current.Game.drugPolicyDatabase.AllPolicies.FirstOrDefault(o => o.sourceDef?.defName == sourceDefName);
|
|
|
|
public static ReadingPolicy Reading(string labelKey) =>
|
|
Current.Game.readingPolicyDatabase.AllReadingPolicies.FirstOrDefault(o => o.label == (string)labelKey.Translate());
|
|
|
|
/// <summary>A starting policy that must exist; fails the test otherwise.</summary>
|
|
public static TP Need<TP>(TP policy, string what) where TP : Policy
|
|
{
|
|
T.NotNull(policy, what);
|
|
return policy;
|
|
}
|
|
|
|
// ---- time ------------------------------------------------------------------------
|
|
|
|
public static void Tick(int ticks)
|
|
{
|
|
for (int i = 0; i < ticks; i++) Find.TickManager.DoSingleTick();
|
|
}
|
|
|
|
/// <summary>Ticks until the 2500-tick sweep has run once.</summary>
|
|
public static void TickThroughSweep()
|
|
{
|
|
int interval = GameComponent_BlaxCastes.SweepInterval;
|
|
int left = interval - Find.TickManager.TicksGame % interval;
|
|
Tick(left == 0 ? interval : left);
|
|
}
|
|
|
|
public static void QuietStoryteller()
|
|
{
|
|
Find.Storyteller.incidentQueue.Clear();
|
|
Find.Storyteller.storytellerComps.Clear();
|
|
}
|
|
|
|
// ---- settings and cleanup (used by the runner) -----------------------------------
|
|
|
|
public static Dictionary<FieldInfo, object> SnapshotSettings()
|
|
{
|
|
var d = new Dictionary<FieldInfo, object>();
|
|
keepCasteState = false;
|
|
GameComponent_BlaxCastes comp = Comp;
|
|
rowSnapshot = comp?.JoinRows.Select(r => (r.reason, r.mode, r.caste)).ToList();
|
|
defaultSnapshot = comp?.DefaultCaste;
|
|
if (Settings == null) return d;
|
|
foreach (FieldInfo f in typeof(BlaxCastesSettings).GetFields(BindingFlags.Public | BindingFlags.Instance))
|
|
d[f] = f.GetValue(Settings);
|
|
return d;
|
|
}
|
|
|
|
public static void RestoreSettings(Dictionary<FieldInfo, object> snapshot)
|
|
{
|
|
if (Settings != null)
|
|
foreach (var kv in snapshot) kv.Key.SetValue(Settings, kv.Value);
|
|
GameComponent_BlaxCastes comp = Comp;
|
|
if (comp == null || keepCasteState || rowSnapshot == null) return;
|
|
foreach (var (reason, mode, caste) in rowSnapshot)
|
|
{
|
|
bool ok = mode == JoinRowMode.SpecificCaste && !comp.HasCaste(caste)
|
|
? CasteManager.SetJoinRow(reason, JoinRowMode.NoCaste)
|
|
: CasteManager.SetJoinRow(reason, mode, caste);
|
|
if (!ok) TestRunner.Note("could not restore join row " + reason);
|
|
}
|
|
CasteManager.SetDefault(comp.HasCaste(defaultSnapshot) ? defaultSnapshot : null);
|
|
}
|
|
|
|
public static void Cleanup()
|
|
{
|
|
foreach (QuestPart_ExtraFaction part in lodgerParts)
|
|
Find.QuestManager.ExtraFactionQuestParts.Remove(part);
|
|
lodgerParts.Clear();
|
|
|
|
foreach (Caste c in madeCastes)
|
|
{
|
|
if (c != null && !keptCastes.Contains(c))
|
|
CasteManager.DeleteCaste(c);
|
|
}
|
|
madeCastes.Clear();
|
|
|
|
foreach (Pawn p in made)
|
|
{
|
|
if (p == null || kept.Contains(p)) continue;
|
|
if (p.Dead) DropRitualObligations(p);
|
|
if (p.Corpse != null && p.Corpse.Spawned) p.Corpse.Destroy(DestroyMode.Vanish);
|
|
else if (p.Spawned) p.Destroy(DestroyMode.Vanish);
|
|
else if (p.IsWorldPawn()) Find.WorldPawns.RemoveAndDiscardPawnViaGC(p);
|
|
}
|
|
made.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// A fixture colonist that died gets a funeral obligation (Ideology). The cleanup destroys its corpse,
|
|
/// so drop the obligation too, or the save warns about a reference to a discarded pawn.
|
|
/// </summary>
|
|
private static void DropRitualObligations(Pawn p)
|
|
{
|
|
if (!ModsConfig.IdeologyActive || Find.IdeoManager == null) return;
|
|
Corpse corpse = p.Corpse;
|
|
bool Hits(TargetInfo t) => t.HasThing && (t.Thing == p || (corpse != null && t.Thing == corpse));
|
|
foreach (Ideo ideo in Find.IdeoManager.IdeosListForReading)
|
|
{
|
|
foreach (Precept precept in ideo.PreceptsListForReading)
|
|
{
|
|
if (!(precept is Precept_Ritual ritual) || ritual.activeObligations == null) continue;
|
|
foreach (RitualObligation o in ritual.activeObligations.ToList())
|
|
{
|
|
if (o != null && (Hits(o.targetA) || Hits(o.targetB) || Hits(o.targetC)))
|
|
ritual.RemoveObligation(o);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|