rimworld-blax-laws/Tests/BlaxLawsTests/Source/QuickTestMap.cs

54 lines
2.2 KiB
C#

using HarmonyLib;
using RimWorld;
using RimWorld.Planet;
using Verse;
namespace BlaxLawsTests
{
/// <summary>
/// The vanilla quick test picks a random tile at 250x250 (Root_Play.cs:94-96), and a hilly or mountainous
/// tile can leave too little open ground for the fixtures. While -blaxlawstests is on, the quick-test map
/// is 325x325 on a flat or small-hills tile, as in Blax Zones' suite. The tile and every position on it
/// still vary per run.
/// </summary>
[StaticConstructorOnStartup]
public static class QuickTestMap
{
public const int MapSize = 325;
private const int TileAttempts = 200;
static QuickTestMap()
{
if (!TestRunner.Enabled)
return;
new Harmony("blax.laws.tests.quicktestmap").Patch(
AccessTools.Method(typeof(Root_Play), nameof(Root_Play.SetupForQuickTestPlay)),
postfix: new HarmonyMethod(typeof(QuickTestMap), nameof(Postfix)));
// Jail tests tick whole days, so the autosaver fires. Its "Autosaving" long event is synchronous with
// text, so it waits to be drawn (LongEventHandler.cs:52-58, 410-413), which never happens headless, and
// every later long event, the suite's own save+reload included, queues behind it forever.
new Harmony("blax.laws.tests.noautosave").Patch(
AccessTools.Method(typeof(Autosaver), nameof(Autosaver.AutosaverTick)),
prefix: new HarmonyMethod(typeof(QuickTestMap), nameof(SkipAutosave)));
// Before the world and map are made, so every pawn vanilla makes for a book or tale is known.
TestRunner.HookPawnWatchers();
}
public static bool SkipAutosave() => false;
public static void Postfix()
{
GameInitData init = Find.GameInitData;
init.mapSize = MapSize;
for (int i = 0; i < TileAttempts && !Open(init.startingTile); i++)
init.startingTile = TileFinder.RandomStartingTile();
}
private static bool Open(PlanetTile tile)
{
Hilliness h = Find.WorldGrid[tile].hilliness;
return h == Hilliness.Flat || h == Hilliness.SmallHills;
}
}
}