(TP policy, string what) where TP : Policy
{
T.NotNull(policy, what);
return policy;
}
/// A def that must exist; fails the test otherwise.
public static TD NeedDef| (TD def, string what) where TD : Def
{
T.NotNull(def, what);
return def;
}
/// A caste made elsewhere (DuplicateCaste), deleted after the test like NewCaste's.
public static Caste Track(Caste c)
{
T.NotNull(c, "caste to track");
madeCastes.Add(c);
return c;
}
// ---- time ------------------------------------------------------------------------
public static void Tick(int ticks)
{
for (int i = 0; i < ticks; i++) Find.TickManager.DoSingleTick();
}
///
/// Applies the queued joins of the given pawns now, as the next tick would, without ticking the
/// game: fixture prisoners outside a prison cell make vanilla's prisoner job givers throw when the
/// game ticks. Other pawns' queued joins (kept fixtures of earlier tests) stay queued untouched.
///
public static void ApplyJoins(params Pawn[] pawns)
{
List queue = Comp.JoinQueue;
var others = queue.Where(q => q == null || !pawns.Contains(q.pawn)).ToList();
queue.RemoveAll(q => others.Contains(q));
int now = Find.TickManager.TicksGame;
foreach (QueuedJoin q in queue)
{
if (q.tick >= now)
q.tick = now - 1;
}
try
{
JoinDefaults.ApplyQueue();
}
finally
{
foreach (QueuedJoin q in others)
{
if (q != null && !queue.Any(x => x != null && x.pawn == q.pawn))
queue.Add(q);
}
}
}
/// Removes the pawn's queued join (fixtures can be queued when they are generated into the colony).
public static void Unqueue(Pawn p) => Comp.JoinQueue.RemoveAll(q => q != null && q.pawn == p);
/// Ticks until the 2500-tick sweep has run once.
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 SnapshotSettings()
{
var d = new Dictionary();
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 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;
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();
}
///
/// A fixture colonist that died gets a funeral obligation, and a living one can get a scarification or
/// blinding obligation when it joins or changes status (Ideology). The cleanup destroys the pawn or its
/// corpse, so drop those obligations too, or the save warns about a reference to a discarded pawn.
///
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);
}
}
}
}
}
}
|