134 lines
6.9 KiB
C#
134 lines
6.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using RimWorld;
|
|
using Verse;
|
|
|
|
namespace BlaxLawsTests
|
|
{
|
|
/// <summary>
|
|
/// Diagnostics and guards for bystanders and fixture pawns: who killed a fixture pawn, a pawn that died
|
|
/// during a setup, and animal bonds that would turn a bystander pet berserk when a bonded pawn dies.
|
|
/// </summary>
|
|
public static partial class Fixtures
|
|
{
|
|
/// <summary>A pawn a fixture made in the current test (or kept from one).</summary>
|
|
public static bool IsFixturePawn(Pawn p) => p != null && (made.Contains(p) || kept.Contains(p));
|
|
|
|
private static bool logKills;
|
|
private static bool oldLogCauseOfDeath;
|
|
|
|
/// <summary>
|
|
/// For the rest of the test: every Kill of a fixture pawn is noted with the tick, whether it was dead
|
|
/// already, whether a Kill already runs, the damage, the culprit and the stack, and vanilla logs the
|
|
/// cause of death (DebugViewSettings.logCauseOfDeath). Both go back after the test.
|
|
/// </summary>
|
|
public static void LogKillsDuringTest()
|
|
{
|
|
if (logKills)
|
|
return;
|
|
logKills = true;
|
|
oldLogCauseOfDeath = DebugViewSettings.logCauseOfDeath;
|
|
DebugViewSettings.logCauseOfDeath = true;
|
|
OnCleanup(() =>
|
|
{
|
|
logKills = false;
|
|
DebugViewSettings.logCauseOfDeath = oldLogCauseOfDeath;
|
|
});
|
|
}
|
|
|
|
/// <summary>The harness' prefix on Pawn.Kill. A kill of a pawn that is dead already is always noted.</summary>
|
|
internal static void NoteKill(Pawn p, DamageInfo? dinfo, Hediff exactCulprit)
|
|
{
|
|
if (p == null || !IsFixturePawn(p) || (!logKills && !p.Dead))
|
|
return;
|
|
TestRunner.Note("Kill of " + p.LabelShort + " (" + p.ThingID + ") at tick " + Find.TickManager.TicksGame + ": dead already " + p.Dead +
|
|
", a kill already running " + (p.health?.isBeingKilled ?? false) + ", damage " + (dinfo?.ToString() ?? "none") +
|
|
", culprit " + (exactCulprit?.def?.defName ?? "none") + ", stack " + Environment.StackTrace);
|
|
}
|
|
|
|
/// <summary>Fails as a fixture error when the pawn died or left the map during a setup, naming its hediffs and corpse.</summary>
|
|
public static void CheckAlive(Pawn p, string during)
|
|
{
|
|
if (p != null && !p.Dead && p.Spawned)
|
|
return;
|
|
string hediffs = p?.health?.hediffSet == null ? "none" :
|
|
string.Join(", ", p.health.hediffSet.hediffs.Select(h => h.def.defName + " " + h.Severity.ToString("0.##")));
|
|
T.Fail("fixture: " + (p?.LabelShort ?? "the pawn") + " is dead or off the map after " + during + ": dead " + p?.Dead + ", spawned " + p?.Spawned +
|
|
"; hediffs: " + hediffs + ", corpse at " + (p?.Corpse != null ? p.Corpse.PositionHeld.ToString() : "none"));
|
|
}
|
|
|
|
// ---- bonds ------------------------------------------------------------------------
|
|
|
|
/// <summary>The quick test's own animal bonds, taken off for phase 1 (see StripBystanderBonds).</summary>
|
|
private static readonly List<(Pawn animal, Pawn human)> strippedBonds = new List<(Pawn, Pawn)>();
|
|
|
|
/// <summary>
|
|
/// When a pawn with an animal bond dies, the animal may go berserk (Pawn_RelationsTracker.cs:1105-1129),
|
|
/// and a berserk animal is an active threat that keeps every case waiting for the danger to pass. At
|
|
/// the start of phase 1 the bonds of the quick test's animals come off; RestoreBystanderBonds puts them
|
|
/// back right before the first save, so the saved colony is as it was.
|
|
/// </summary>
|
|
public static void StripBystanderBonds()
|
|
{
|
|
Map map = Map;
|
|
if (map == null || PawnRelationDefOf.Bond == null)
|
|
return;
|
|
foreach (Pawn a in map.mapPawns.AllPawns.Where(x => x.RaceProps.Animal && x.relations != null).ToList())
|
|
{
|
|
foreach (DirectPawnRelation r in a.relations.DirectRelations.Where(r => r.def == PawnRelationDefOf.Bond).ToList())
|
|
{
|
|
if (a.relations.TryRemoveDirectRelation(PawnRelationDefOf.Bond, r.otherPawn))
|
|
{
|
|
strippedBonds.Add((a, r.otherPawn));
|
|
TestRunner.Note("took off the bond of " + a.LabelShort + " with " + r.otherPawn?.LabelShort + " for phase 1");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Puts back the bonds StripBystanderBonds took off, where both pawns still live.</summary>
|
|
public static void RestoreBystanderBonds()
|
|
{
|
|
foreach (var (a, h) in strippedBonds)
|
|
{
|
|
if (a == null || h == null || a.Dead || h.Dead || a.Discarded || h.Discarded || a.relations == null || h.relations == null)
|
|
continue;
|
|
if (!a.relations.DirectRelationExists(PawnRelationDefOf.Bond, h))
|
|
a.relations.AddDirectRelation(PawnRelationDefOf.Bond, h);
|
|
}
|
|
strippedBonds.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// A bond a bystander animal formed during the suite with a fixture pawn (Handling work, taming) is taken
|
|
/// off before each phase-1 test: fixture pawns die in tests, and the bond could turn the animal berserk.
|
|
/// </summary>
|
|
private static void DropFixtureBonds(Pawn animal)
|
|
{
|
|
if (animal?.relations == null || PawnRelationDefOf.Bond == null)
|
|
return;
|
|
foreach (DirectPawnRelation r in animal.relations.DirectRelations.Where(r => r.def == PawnRelationDefOf.Bond).ToList())
|
|
{
|
|
Pawn other = r.otherPawn;
|
|
if (other != null && !IsFixturePawn(other) && !other.Destroyed && !other.Discarded)
|
|
continue;
|
|
if (animal.relations.TryRemoveDirectRelation(PawnRelationDefOf.Bond, other))
|
|
TestRunner.Note("took off the bond of " + animal.LabelShort + " with the fixture pawn " + (other?.LabelShort ?? "null"));
|
|
}
|
|
}
|
|
|
|
/// <summary>The animal's bonded pawns and whether each is dead, for a note when the animal is calmed.</summary>
|
|
private static string BondStory(Pawn animal)
|
|
{
|
|
if (animal?.relations == null)
|
|
return "no relations";
|
|
List<DirectPawnRelation> bonds = animal.relations.DirectRelations.Where(r => r.def == PawnRelationDefOf.Bond).ToList();
|
|
if (bonds.Count == 0)
|
|
return "no bonds";
|
|
return "bonds: " + string.Join(", ", bonds.Select(r => (r.otherPawn?.LabelShort ?? "null") + " (dead " + (r.otherPawn?.Dead ?? false) +
|
|
", fixture " + IsFixturePawn(r.otherPawn) + ")"));
|
|
}
|
|
}
|
|
}
|