168 lines
6.9 KiB
C#
168 lines
6.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using BlaxSlaves;
|
|
using RimWorld;
|
|
using RimWorld.Planet;
|
|
using Verse;
|
|
|
|
namespace BlaxSlavesTests
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static class Fixtures
|
|
{
|
|
private static readonly List<Pawn> made = new List<Pawn>();
|
|
private static readonly HashSet<Pawn> kept = new HashSet<Pawn>();
|
|
|
|
public static Map Map => Find.CurrentMap;
|
|
|
|
public static BlaxSlavesSettings Settings => BlaxSlavesMod.Settings;
|
|
|
|
// ---- 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 adult pawn, not spawned. Relations off so no accidental family ties.</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>A spawned free colonist. pacifist = has the Kind trait; otherwise Kind is removed.</summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>A spawned slave of the colony, enslaved from origin (default: a friendly faction).</summary>
|
|
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;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
// ---- 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 --------------------------------------------------------------------
|
|
|
|
/// <summary>Snapshot of a pawn's memories with their age, to spot new or renewed ones.</summary>
|
|
public static Dictionary<Thought_Memory, int> Memories(Pawn p)
|
|
{
|
|
var d = new Dictionary<Thought_Memory, int>();
|
|
var list = p.needs?.mood?.thoughts?.memories?.Memories;
|
|
if (list != null) foreach (Thought_Memory m in list) d[m] = m.age;
|
|
return d;
|
|
}
|
|
|
|
/// <summary>Memories added or renewed since the snapshot that lower mood or opinion.</summary>
|
|
public static List<Thought_Memory> NewBadMemories(Pawn p, Dictionary<Thought_Memory, int> before)
|
|
{
|
|
var result = new List<Thought_Memory>();
|
|
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<Thought_Memory> 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<FieldInfo, object> SnapshotSettings()
|
|
{
|
|
var d = new Dictionary<FieldInfo, object>();
|
|
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<FieldInfo, object> 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();
|
|
}
|
|
}
|
|
}
|