using System.Collections.Generic; using System.Linq; using System.Reflection; using BlaxSlaves; using RimWorld; using RimWorld.Planet; using Verse; namespace BlaxSlavesTests { /// /// Shared helpers for tests. Pawns made here are destroyed after each test unless passed to /// Keep(). Mod settings are restored after each test, so tests may change them freely. /// public static class Fixtures { private static readonly List made = new List(); private static readonly HashSet kept = new HashSet(); public static Map Map => Find.CurrentMap; public static BlaxSlavesSettings Settings => BlaxSlavesMod.Settings; // ---- factions -------------------------------------------------------------------- /// A visible, non-player, humanlike faction that is not hostile to the player. 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(); /// A visible, non-player, humanlike faction hostile to the player. 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 ----------------------------------------------------------------------- /// A fresh adult pawn, not spawned. Relations off so no accidental family ties. public static Pawn Generate(PawnKindDef kind, Faction faction) { var req = new PawnGenerationRequest(kind, faction, PawnGenerationContext.NonPlayer, forceGenerateNewPawn: true, canGeneratePawnRelations: false, colonistRelationChanceFactor: 0f, developmentalStages: DevelopmentalStage.Adult); 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; } /// A spawned free colonist. pacifist = has the Kind trait; otherwise Kind is removed. public static Pawn Colonist(bool pacifist = false) { Pawn p = Generate(PawnKindDefOf.Colonist, Faction.OfPlayer); Trait kind = p.story.traits.GetTrait(TraitDefOf.Kind); if (pacifist && kind == null) p.story.traits.GainTrait(new Trait(TraitDefOf.Kind)); if (!pacifist && kind != null) p.story.traits.RemoveTrait(kind); return Spawn(p); } /// A spawned slave of the colony, enslaved from origin (default: a friendly faction). public static Pawn ColonySlave(Faction origin = null) { Pawn p = Generate(PawnKindDefOf.Slave, origin ?? FriendlyFaction()); Spawn(p); p.guest.SetGuestStatus(Faction.OfPlayer, GuestStatus.Slave); return p; } /// Exclude a pawn from the per-test cleanup (phase-1 setup for phase-2 checks). 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; } // ---- time ------------------------------------------------------------------------ public static void Tick(int ticks) { for (int i = 0; i < ticks; i++) Find.TickManager.DoSingleTick(); } public static void QuietStoryteller() { Find.Storyteller.incidentQueue.Clear(); Find.Storyteller.storytellerComps.Clear(); } // ---- thoughts -------------------------------------------------------------------- /// Snapshot of a pawn's memories with their age, to spot new or renewed ones. public static Dictionary Memories(Pawn p) { var d = new Dictionary(); var list = p.needs?.mood?.thoughts?.memories?.Memories; if (list != null) foreach (Thought_Memory m in list) d[m] = m.age; return d; } /// Memories added or renewed since the snapshot that lower mood or opinion. public static List NewBadMemories(Pawn p, Dictionary before) { var result = new List(); var list = p.needs?.mood?.thoughts?.memories?.Memories; if (list == null) return result; foreach (Thought_Memory m in list) { bool fresh = !before.TryGetValue(m, out int oldAge) || m.age < oldAge; if (!fresh) continue; bool bad = m.MoodOffset() < 0f || (m is ISocialThought s && s.OpinionOffset() < 0f); if (bad) result.Add(m); } return result; } public static string Describe(IEnumerable memories) => string.Join(", ", memories.Select(m => m.def.defName + (m.otherPawn != null ? "(" + m.otherPawn.LabelShort + ")" : ""))); // ---- settings and cleanup (used by the runner) ----------------------------------- public static Dictionary SnapshotSettings() { var d = new Dictionary(); if (Settings == null) return d; foreach (FieldInfo f in typeof(BlaxSlavesSettings).GetFields(BindingFlags.Public | BindingFlags.Instance)) d[f] = f.GetValue(Settings); return d; } public static void RestoreSettings(Dictionary snapshot) { if (Settings == null) return; foreach (var kv in snapshot) kv.Key.SetValue(Settings, kv.Value); } public static void Cleanup() { foreach (Pawn p in made) { if (p == null || kept.Contains(p)) continue; 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(); } } }