using System.Collections.Generic;
using System.Linq;
using BlaxDefaults;
using HarmonyLib;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxDefaultsTests
{
///
/// Work package "tab". The tests of design.md: Defaults button in the storage tab.
/// Methods: [BlaxTest(": ", phase: N)] public static void X(). Skip DLC-only checks with
/// Fixtures.NeedDlc(DlcSupport.Biotech) ("skipped: needs Biotech").
/// Drawing the button needs a GUI and is checked by hand; these tests drive the menu builder.
///
public static class TabTests
{
private static readonly string[] TabKeys =
{
"BlaxDefaults_TabButton", "BlaxDefaults_TabMakeDefault", "BlaxDefaults_TabLoadDefault", "BlaxDefaults_TabLoadGameDefault",
"BlaxDefaults_TabForget", "BlaxDefaults_TabMakeDefaultType", "BlaxDefaults_TabForgetType", "BlaxDefaults_TabNoStockpileDefault",
"BlaxDefaults_TabMakeDefaultTip", "BlaxDefaults_TabLoadDefaultTip", "BlaxDefaults_TabLoadGameDefaultTip", "BlaxDefaults_TabForgetTip",
"BlaxDefaults_TabNoDefault", "BlaxDefaults_TabMadeDefault", "BlaxDefaults_TabForgotten", "BlaxDefaults_TabForgetConfirm",
"BlaxDefaults_TabWriteFailed", "BlaxDefaults_TabTipYours", "BlaxDefaults_TabTipGame", "BlaxDefaults_TabTipStorageOff",
"BlaxDefaults_TabTipStockpilesSome", "BlaxDefaults_TabTipStockpilesRest", "BlaxDefaults_TabTipStockpilesNone", "BlaxDefaults_TabTipStockpilesOff",
};
// ---- tests -----------------------------------------------------------------------
[BlaxTest("tab: every string of the Defaults button is in Keyed")]
public static void Strings()
{
var missing = TabKeys.Where(k => !k.CanTranslate()).ToList();
T.Check(missing.Count == 0, "strings missing from Keyed: " + string.Join(", ", missing));
}
[BlaxTest("tab: the tab's selected parent and priority row resolve through the delegates")]
public static void Delegates()
{
T.NotNull(StorageTabButton.SelStoreSettingsParent, "SelStoreSettingsParent delegate");
T.NotNull(StorageTabButton.IsPrioritySettingVisible, "IsPrioritySettingVisible delegate");
T.Check(StorageTabButton.IsPrioritySettingVisible(new ITab_Storage()), "ITab_Storage hides the priority row");
T.Check(!StorageTabButton.IsPrioritySettingVisible(new ITab_Shells()), "ITab_Shells shows the priority row: the icon rect would be wrong");
Zone_Stockpile zone = MakeStockpile(StorageSettingsPreset.DefaultStockpile);
Building_Storage shelf = SpawnShelf(ShelfCell(Fixtures.EmptyArea(5, 3)));
var (a, b, group) = LinkedShelves();
var tab = new ITab_Storage();
try
{
Find.Selector.ClearSelection();
Find.Selector.Select(shelf, playSound: false, forceDesignatorDeselect: false);
T.Check(StorageTabButton.SelStoreSettingsParent(tab) == shelf, "a selected shelf is not the tab's parent");
Find.Selector.ClearSelection();
Find.Selector.Select(zone, playSound: false, forceDesignatorDeselect: false);
T.Check(StorageTabButton.SelStoreSettingsParent(tab) == zone, "a selected stockpile is not the tab's parent");
Find.Selector.ClearSelection();
Find.Selector.Select(a, playSound: false, forceDesignatorDeselect: false);
Find.Selector.Select(b, playSound: false, forceDesignatorDeselect: false);
T.Check(StorageTabButton.SelStoreSettingsParent(tab) == group, "two selected linked shelves don't give their group");
T.Check(StorageDefaultsMenu.HasButton(group), "the group of two linked shelves gets no button");
}
finally
{
Find.Selector.ClearSelection();
}
}
[BlaxTest("tab: a stockpile and a dumping stockpile offer every type, and all four entries open the type submenu")]
public static void StockpileMenu()
{
ForgetStockpileTypes();
List types = StorageKinds.StockpileTypes;
Zone_Stockpile pile = MakeStockpile(StorageSettingsPreset.DefaultStockpile);
Zone_Stockpile dump = MakeStockpile(StorageSettingsPreset.DumpingStockpile);
foreach (Zone_Stockpile zone in new[] { pile, dump })
{
string what = zone.label;
List kinds = StorageDefaultsMenu.KindsFor(zone);
T.Equal(string.Join(",", types.Select(t => t.Key)), string.Join(",", kinds.Select(k => k.Key)), "kinds of " + what);
T.Check(StorageDefaultsMenu.HasButton(zone), what + " gets no button");
T.Check(ReferenceEquals(zone.settings, StorageDefaultsMenu.SettingsOf(zone)), what + ": SettingsOf is not the zone's settings");
List options = StorageDefaultsMenu.OptionsFor(zone);
T.Equal(4, options.Count, "options on " + what);
for (int i = 0; i < 4; i++)
T.Equal(StorageDefaultsMenu.StockpileEntryLabel(StorageDefaultsMenu.Entries[i]), options[i].Label, "label of entry " + i + " on " + what);
T.Equal("Make default for a stockpile type", options[0].Label, "Make default label on " + what);
T.Check(!options[0].Disabled, what + ": Make default is disabled");
T.Check(options[1].Disabled, what + ": Load your default is enabled with no default saved");
T.Check(!options[2].Disabled, what + ": Load the game's default is disabled");
T.Check(options[3].Disabled, what + ": Forget is enabled with no default saved");
}
Fixtures.Save(Fixtures.StockpileKind.Capture(pile.settings));
foreach (Zone_Stockpile zone in new[] { pile, dump })
{
string what = zone.label;
List options = StorageDefaultsMenu.OptionsFor(zone);
T.Check(options.All(o => !o.Disabled), what + ": an entry is disabled with the stockpile default saved");
foreach (StorageMenuEntry entry in StorageDefaultsMenu.Entries)
{
List sub = StorageDefaultsMenu.Submenu(zone, entry);
T.Equal(types.Count, sub.Count, entry + " submenu size on " + what);
for (int i = 0; i < types.Count; i++)
{
StorageKind kind = StorageKinds.ForStockpile(types[i]);
T.Equal(kind.Label.CapitalizeFirst(), sub[i].Label, entry + " submenu entry " + i + " on " + what);
T.Check(sub[i].tooltip.HasValue && sub[i].tooltip.Value.text.Contains(kind.LabelPlural),
entry + " > " + sub[i].Label + ": the tooltip doesn't name " + kind.LabelPlural);
}
T.Equal("Stockpile", sub[0].Label, entry + " submenu first entry");
T.Equal("Dumping stockpile", sub[1].Label, entry + " submenu second entry");
bool needsSaved = entry == StorageMenuEntry.LoadDefault || entry == StorageMenuEntry.Forget;
T.Check(!sub[0].Disabled, entry + " > Stockpile is disabled with its default saved");
T.Equal(needsSaved, sub[1].Disabled, entry + " > Dumping stockpile disabled without a dumping default");
}
}
}
[BlaxTest("tab: a shelf, a shelf blueprint and two linked shelves offer the shelf, and the labels name it")]
public static void BuildingMenu()
{
StorageKind shelfKind = ShelfKind;
DefaultsLibrary.Forget(shelfKind.Key);
Building_Storage shelf = SpawnShelf(ShelfCell(Fixtures.EmptyArea(5, 3)));
Blueprint_Storage blueprint = PlaceShelfBlueprint();
var (a, b, group) = LinkedShelves();
var parents = new (string what, IStoreSettingsParent parent)[]
{
("shelf", shelf), ("shelf blueprint", blueprint), ("group of two linked shelves", group), ("one of two linked shelves", a),
};
foreach (var (what, parent) in parents)
{
List kinds = StorageDefaultsMenu.KindsFor(parent);
T.Equal(1, kinds.Count, "kinds of the " + what);
T.Equal(shelfKind.Key, kinds[0].Key, "kind of the " + what);
List options = StorageDefaultsMenu.OptionsFor(parent);
T.Equal(4, options.Count, "options on the " + what);
T.Equal("Make default for new shelves", options[0].Label, "Make default label on the " + what);
T.Equal("Load your default", options[1].Label, "Load your default label on the " + what);
T.Equal("Load the game's default", options[2].Label, "Load the game's default label on the " + what);
T.Equal("Forget the default for new shelves", options[3].Label, "Forget label on the " + what);
T.Check(!options[0].Disabled && !options[2].Disabled, what + ": Make default or Load the game's default is disabled");
T.Check(options[1].Disabled && options[3].Disabled, what + ": Load your default or Forget is enabled with no default saved");
T.Equal("New shelves start from the game's default.", StorageDefaultsMenu.Tooltip(parent), "tooltip on the " + what + " without a default");
}
StorageSettings groupSettings = group.GetStoreSettings();
T.Check(ReferenceEquals(groupSettings, StorageDefaultsMenu.SettingsOf(group)), "SettingsOf(group) is not the group's settings");
T.Check(ReferenceEquals(groupSettings, StorageDefaultsMenu.SettingsOf(a)), "SettingsOf(a linked shelf) is not the group's settings");
T.Check(ReferenceEquals(blueprint.settings, StorageDefaultsMenu.SettingsOf(blueprint)), "SettingsOf(blueprint) is not the blueprint's settings");
Fixtures.Save(shelfKind.Capture(shelf.settings));
foreach (var (what, parent) in parents)
{
T.Check(StorageDefaultsMenu.OptionsFor(parent).All(o => !o.Disabled), what + ": an entry is disabled with the shelf default saved");
T.Equal("New shelves start from your default.", StorageDefaultsMenu.Tooltip(parent), "tooltip on the " + what + " with a default");
}
}
[BlaxTest("tab: Make default stores the current settings, Load your default writes them back after a change")]
public static void MakeAndLoad()
{
StorageKind shelfKind = ShelfKind;
Building_Storage shelf = SpawnShelf(ShelfCell(Fixtures.EmptyArea(5, 3)));
CheckMakeAndLoad(shelfKind, shelf, "shelf");
DefaultsLibrary.Forget(DefaultsKeys.Dumping);
Zone_Stockpile zone = MakeStockpile(StorageSettingsPreset.DefaultStockpile);
CheckMakeAndLoad(Fixtures.StockpileKind, zone, "stockpile");
T.Check(!DefaultsLibrary.Has(DefaultsKeys.Dumping), "making the stockpile default saved a dumping default");
}
[BlaxTest("tab: Load the game's default restores Preferred and the shelf categories on a shelf")]
public static void ShelfGameDefault()
{
StorageKind shelfKind = ShelfKind;
Building_Storage shelf = SpawnShelf(ShelfCell(Fixtures.EmptyArea(5, 3)));
StorageSettings s = StorageDefaultsMenu.SettingsOf(shelf);
s.Priority = StoragePriority.Low;
s.filter.SetDisallowAll();
s.filter.SetAllow(ThingDefOf.Steel, true);
s.filter.AllowedHitPointsPercents = new FloatRange(0.5f, 1f);
StorageDefaultsMenu.OptionsFor(shelf)[2].action();
StorageSettings def = ShelfDef.building.defaultStorageSettings;
T.NotNull(def, "the shelf's defaultStorageSettings");
T.Equal(StoragePriority.Preferred, s.Priority, "priority after Load the game's default");
T.Check(shelfKind.Capture(s).SameSettingsAs(shelfKind.Capture(def)), "the shelf's settings differ from its def default");
T.Equal(def.filter.AllowedDefCount, s.filter.AllowedDefCount, "allowed defs");
T.Check(s.filter.Allows(ThingDefOf.MealSimple) && s.filter.Allows(ThingDefOf.WoodLog) && s.filter.Allows(ThingDefOf.Steel),
"a shelf category item is refused after Load the game's default");
CheckFullRanges(s, "shelf");
}
[BlaxTest("tab: Load the game's default > stockpile turns a narrowed stockpile into a new vanilla stockpile")]
public static void StockpileGameDefault()
{
Zone_Stockpile zone = MakeStockpile(StorageSettingsPreset.DefaultStockpile);
StorageSettings s = zone.settings;
Narrow(s);
T.Check(!s.filter.Allows(ThingDefOf.MealSimple), "the narrowed stockpile still allows MealSimple");
StorageDefaultsMenu.Submenu(zone, StorageMenuEntry.LoadGameDefault)[0].action();
StorageSettings fresh = new Zone_Stockpile(StorageSettingsPreset.DefaultStockpile, Fixtures.Map.zoneManager).settings;
CheckSameAs(fresh, s, "stockpile after Load the game's default > stockpile");
T.Equal(StoragePriority.Normal, s.Priority, "priority");
T.Check(s.filter.Allows(ThingDefOf.MealSimple) && s.filter.Allows(ThingDefOf.WoodLog) && s.filter.Allows(ThingDefOf.Steel),
"a DefaultStockpile category item is refused: Load the game's default kept the Steel-only narrowing");
CheckFullRanges(s, "stockpile");
}
[BlaxTest("tab: Load the game's default > dumping gives Corpses and Chunks at Normal, plus Wastepack only with Biotech")]
public static void DumpingGameDefault()
{
Zone_Stockpile zone = MakeStockpile(StorageSettingsPreset.DefaultStockpile);
StorageSettings s = zone.settings;
Narrow(s);
StorageDefaultsMenu.Submenu(zone, StorageMenuEntry.LoadGameDefault)[1].action();
T.Equal(StoragePriority.Normal, s.Priority, "priority");
ThingDef wastepack = DefDatabase.GetNamedSilentFail("Wastepack");
foreach (ThingDef def in s.filter.AllowedThingDefs)
{
bool ok = def.IsWithinCategory(ThingCategoryDefOf.Corpses) || def.IsWithinCategory(ThingCategoryDefOf.Chunks) || (def == wastepack && ModsConfig.BiotechActive);
T.Check(ok, def.defName + " is allowed, but is neither a corpse, a chunk nor (with Biotech) Wastepack");
}
T.Check(!s.filter.Allows(ThingDefOf.Steel), "Steel is still allowed");
T.Check(s.filter.Allows(ThingDefOf.ChunkSlagSteel), "slag chunks are refused");
T.Check(s.filter.Allows(ThingDefOf.Human.race.corpseDef), "human corpses are refused");
if (ModsConfig.BiotechActive)
{
T.Check(wastepack != null && s.filter.Allows(wastepack), "Wastepack is refused with Biotech");
}
else
{
T.Check(wastepack == null || !s.filter.Allows(wastepack), "Wastepack is allowed without Biotech");
T.Info("Biotech off: the dumping default has no Wastepack");
}
StorageSettings fresh = new Zone_Stockpile(StorageSettingsPreset.DumpingStockpile, Fixtures.Map.zoneManager).settings;
CheckSameAs(fresh, s, "stockpile after Load the game's default > dumping");
CheckFullRanges(s, "dumping");
}
[BlaxTest("tab: Forget > dumping removes only the dumping default; the tooltip lists both types before and one after")]
public static void ForgetDumping()
{
ForgetStockpileTypes();
Zone_Stockpile zone = MakeStockpile(StorageSettingsPreset.DefaultStockpile);
StorageKind pile = Fixtures.StockpileKind, dump = Fixtures.DumpingKind;
T.Equal("New stockpiles of every type start from the game's default.", StorageDefaultsMenu.Tooltip(zone), "tooltip without defaults");
Fixtures.Save(Fixtures.Template(pile, StoragePriority.Important, ThingDefOf.MealSimple));
Fixtures.Save(Fixtures.Template(dump, StoragePriority.Normal, ThingDefOf.ChunkSlagSteel));
string before = StorageDefaultsMenu.Tooltip(zone);
T.Check(Lists(before, pile) && Lists(before, dump), "the tooltip doesn't list both types: " + before);
StorageDefaultsMenu.Forget(dump, confirm: false);
T.Check(DefaultsLibrary.Has(DefaultsKeys.Stockpile), "Forget > dumping removed the stockpile default");
T.Check(!DefaultsLibrary.Has(DefaultsKeys.Dumping), "Forget > dumping kept the dumping default");
string after = StorageDefaultsMenu.Tooltip(zone);
T.Check(Lists(after, pile) && !Lists(after, dump), "the tooltip after Forget > dumping: " + after);
T.Check(StorageDefaultsMenu.Submenu(zone, StorageMenuEntry.Forget)[1].Disabled, "Forget > Dumping stockpile is enabled after forgetting it");
T.Check(!StorageDefaultsMenu.Submenu(zone, StorageMenuEntry.Forget)[0].Disabled, "Forget > Stockpile is disabled with its default saved");
}
[BlaxTest("tab: a Zone subclass with ISlotGroupParent, a grave and no parent get no button")]
public static void NoButton()
{
TestSlotZone zone = TestSlotZone.Make();
T.NotNull(StorageDefaultsMenu.SettingsOf(zone), "the test zone's settings");
T.Equal(0, StorageDefaultsMenu.KindsFor(zone).Count, "kinds of a Zone subclass with ISlotGroupParent");
T.Equal(0, StorageDefaultsMenu.OptionsFor(zone).Count, "options of a Zone subclass with ISlotGroupParent");
T.Check(!StorageDefaultsMenu.HasButton(zone), "a Zone subclass with ISlotGroupParent gets a button");
T.Equal(null, StorageDefaultsMenu.Tooltip(zone), "tooltip of a Zone subclass with ISlotGroupParent");
ThingDef graveDef = Fixtures.Thing("Grave");
Thing grave = ThingMaker.MakeThing(graveDef, graveDef.MadeFromStuff ? GenStuff.DefaultStuffFor(graveDef) : null);
T.Check(grave is IStoreSettingsParent, "a grave is no IStoreSettingsParent");
T.Equal(0, StorageDefaultsMenu.OptionsFor((IStoreSettingsParent)grave).Count, "options of a grave (no storage blueprint in 1.0)");
T.Equal(0, StorageDefaultsMenu.OptionsFor(null).Count, "options of no parent");
T.Check(!StorageDefaultsMenu.HasButton(null), "no parent gets a button");
}
[BlaxTest("tab: linked shelves: Load your default and Load the game's default write the group's settings")]
public static void LinkedLoad()
{
StorageKind shelfKind = ShelfKind;
var (a, b, group) = LinkedShelves();
StorageTemplate saved = Fixtures.Save(Fixtures.Template(shelfKind, StoragePriority.Important, ThingDefOf.Steel));
StorageSettings gs = group.GetStoreSettings();
T.Equal(StoragePriority.Preferred, gs.Priority, "the group's priority before loading");
// Load your default from one linked shelf's tab.
StorageDefaultsMenu.OptionsFor(a)[1].action();
T.Check(shelfKind.Capture(gs).SameSettingsAs(saved), "the group's settings differ from the default after Load your default");
T.Equal(StoragePriority.Important, gs.Priority, "the group's priority after Load your default");
T.Check(ReferenceEquals(a.GetStoreSettings(), gs) && ReferenceEquals(b.GetStoreSettings(), gs), "a shelf left the group");
T.Check(b.GetStoreSettings().filter.Allows(ThingDefOf.Steel) && !b.GetStoreSettings().filter.Allows(ThingDefOf.WoodLog),
"the other linked shelf doesn't take Steel only");
// Load the game's default from the group (both shelves selected).
StorageDefaultsMenu.OptionsFor(group)[2].action();
T.Equal(StoragePriority.Preferred, gs.Priority, "the group's priority after Load the game's default");
T.Check(shelfKind.Capture(gs).SameSettingsAs(shelfKind.Capture(ShelfDef.building.defaultStorageSettings)),
"the group's settings differ from the shelf's def default after Load the game's default");
}
[BlaxTest("tab: the button and the icon stay clear of the tab's close X and the priority button")]
public static void ClearOfCloseX()
{
foreach (ITab_Storage tab in new ITab_Storage[] { new ITab_Storage(), new ITab_Shells() })
{
string which = tab.GetType().Name;
float width = Traverse.Create(tab).Field("size").GetValue().x;
T.Check(width > 0f, which + ": no tab width");
// Widgets.CloseButtonFor(rect.AtZero()), drawn by InspectTabBase.DoTabGUI before FillTab.
Rect closeX = StorageTabButton.CloseXRect(width);
T.Info(which + ": width " + width + ", close X at x " + closeX.xMin + " to " + closeX.xMax + ", y " + closeX.yMin + " to " + closeX.yMax);
// The priority button: (0, 0, 160, 29) inside the tab contracted by 10.
var priority = new Rect(10f, 10f, 160f, 29f);
foreach (var (what, rect) in new[] { ("button", StorageTabButton.ButtonRect), ("icon", StorageTabButton.IconRect) })
{
T.Check(!Overlap(rect, closeX), which + ": the " + what + " (x " + rect.xMin + " to " + rect.xMax + ") covers the close X");
T.Check(!Overlap(rect, priority), which + ": the " + what + " covers the priority button");
T.Check(rect.xMax <= width - 10f, which + ": the " + what + " reaches past the tab's 10 px margin");
}
}
}
[BlaxTest("tab: Make default keeps the names from inactive mods of the previous default, in their lists")]
public static void MakeDefaultKeepsUnresolved()
{
StorageKind shelfKind = ShelfKind;
StorageTemplate old = Fixtures.Template(shelfKind, StoragePriority.Important, ThingDefOf.Steel);
old.allowed.Add("BlaxTest_Missing");
old.refused.Add("BlaxTest_MissingRefused");
old.refusedSpecial.Add("BlaxTest_MissingFilter");
Fixtures.Save(old);
Building_Storage shelf = SpawnShelf(ShelfCell(Fixtures.EmptyArea(5, 3)));
StorageSettings s = StorageDefaultsMenu.SettingsOf(shelf);
s.Priority = StoragePriority.Low;
s.filter.SetDisallowAll();
s.filter.SetAllow(ThingDefOf.WoodLog, true);
// "Make default for new shelves" from the menu, twice: the names stay once.
for (int click = 1; click <= 2; click++)
{
StorageDefaultsMenu.OptionsFor(shelf)[0].action();
StorageTemplate saved = DefaultsLibrary.Get(shelfKind.Key);
T.NotNull(saved, "click " + click + ": the saved default");
T.Equal(1, saved.allowed.Count(n => n == "BlaxTest_Missing"), "click " + click + ": BlaxTest_Missing in the allowed list");
T.Equal(1, saved.refused.Count(n => n == "BlaxTest_MissingRefused"), "click " + click + ": BlaxTest_MissingRefused in the refused list");
T.Equal(1, saved.refusedSpecial.Count(n => n == "BlaxTest_MissingFilter"), "click " + click + ": BlaxTest_MissingFilter in the refused special filters");
T.Check(!saved.refused.Contains("BlaxTest_Missing") && !saved.allowed.Contains("BlaxTest_MissingRefused"), "click " + click + ": an unresolved name moved to the other list");
T.Equal(3, saved.UnresolvedNames().Count(), "click " + click + ": unresolved names");
T.Equal(StoragePriority.Low, saved.Priority, "click " + click + ": priority");
T.Equal(UnlistedRule.Game, saved.unlisted, "click " + click + ": unlisted rule");
T.Check(saved.allowed.Contains("WoodLog") && saved.refused.Contains("Steel"), "click " + click + ": the shelf's own settings were not saved");
}
T.Check(DefaultsLibrary.LastWriteOk, "the defaults file write failed");
}
[BlaxTest("tab: plural labels: shelf gives shelves, a note in parentheses stays at the end")]
public static void Plurals()
{
if (!(Find.ActiveLanguageWorker is LanguageWorker_English))
T.Skip("needs English as the game's language");
T.Equal("shelves", StorageKinds.PluralOf("shelf"), "plural of shelf");
T.Equal("Large shelves", StorageKinds.PluralOf("Large shelf"), "plural of Large shelf");
T.Equal("Weapon Racks (1 slot)", StorageKinds.PluralOf("Weapon Rack (1 slot)"), "plural of Weapon Rack (1 slot)");
T.Equal("bookcases", StorageKinds.PluralOf("bookcase"), "plural of bookcase");
T.Equal("hoppers", StorageKinds.PluralOf("hopper"), "plural of hopper");
T.Equal("storage 2", StorageKinds.PluralOf("storage 2"), "plural of a label ending in a digit");
T.Equal("shelves", ShelfKind.LabelPlural, "LabelPlural of Shelf");
T.Equal("bookcases", Fixtures.Kind("Bookcase").LabelPlural, "LabelPlural of Bookcase");
foreach (ThingDef def in StorageKinds.CoveredDefs())
{
string plural = StorageKinds.ForDef(def).LabelPlural;
T.Check(!plural.EndsWith(")s") && !plural.EndsWith("shelfs"), def.defName + ": plural " + plural);
if (def.defName.StartsWith("WeaponRacks_") || def.defName.StartsWith("RimFridge_"))
T.Info(def.defName + ": " + plural);
}
}
// ---- helpers ---------------------------------------------------------------------
private static bool Overlap(Rect a, Rect b) => a.xMin < b.xMax && a.xMax > b.xMin && a.yMin < b.yMax && a.yMax > b.yMin;
private static ThingDef ShelfDef => Fixtures.Thing("Shelf");
private static StorageKind ShelfKind => Fixtures.Kind("Shelf");
private static void ForgetStockpileTypes()
{
foreach (StockpileType t in StorageKinds.StockpileTypes)
DefaultsLibrary.Forget(t.Key);
}
/// A registered stockpile over an empty 3x3 area, made as vanilla's designator does (constructor, register, cells).
private static Zone_Stockpile MakeStockpile(StorageSettingsPreset preset)
{
CellRect area = Fixtures.EmptyArea(3, 3);
Map map = Fixtures.Map;
var zone = new Zone_Stockpile(preset, map.zoneManager);
map.zoneManager.RegisterZone(zone);
Fixtures.TrackZone(zone);
foreach (IntVec3 c in area.Cells)
zone.AddCell(c);
return zone;
}
/// The middle of a 5x3 (or larger) area: a 2x1 shelf fits in any rotation.
private static IntVec3 ShelfCell(CellRect area) => new IntVec3(area.minX + 2, 0, area.minZ + 1);
private static Building_Storage SpawnShelf(IntVec3 cell)
{
Thing t = ThingMaker.MakeThing(ShelfDef, ThingDefOf.WoodLog);
t.SetFaction(Faction.OfPlayer);
return (Building_Storage)Fixtures.Track(GenSpawn.Spawn(t, cell, Fixtures.Map, Rot4.North));
}
private static Blueprint_Storage PlaceShelfBlueprint()
{
IntVec3 cell = ShelfCell(Fixtures.EmptyArea(5, 3));
Blueprint_Build bp = GenConstruct.PlaceBlueprintForBuild(ShelfDef, cell, Fixtures.Map, Rot4.North, Faction.OfPlayer, ThingDefOf.WoodLog);
Fixtures.Track(bp);
T.Check(bp is Blueprint_Storage, "the shelf blueprint is a " + bp?.GetType().Name);
return (Blueprint_Storage)bp;
}
/// Two spawned shelves linked as vanilla's Link gizmo does: a new group from the first, then both join.
private static (Building_Storage a, Building_Storage b, StorageGroup group) LinkedShelves()
{
CellRect area = Fixtures.EmptyArea(5, 5);
Building_Storage a = SpawnShelf(new IntVec3(area.minX + 2, 0, area.minZ + 1));
Building_Storage b = SpawnShelf(new IntVec3(area.minX + 2, 0, area.minZ + 3));
StorageGroup group = Fixtures.Map.storageGroups.NewGroup();
group.InitFrom(a);
a.SetStorageGroup(group);
b.SetStorageGroup(group);
T.Check(a.storageGroup == group && b.storageGroup == group, "the shelves are not linked");
return (a, b, group);
}
/// Steel only, Important, hit points 50 to 100%, rotten refused, quality good or better.
private static void Narrow(StorageSettings s)
{
s.Priority = StoragePriority.Important;
s.filter.SetDisallowAll();
s.filter.SetAllow(ThingDefOf.Steel, true);
s.filter.AllowedHitPointsPercents = new FloatRange(0.5f, 1f);
s.filter.AllowedQualityLevels = new QualityRange(QualityCategory.Good, QualityCategory.Legendary);
SpecialThingFilterDef rotten = DefDatabase.GetNamedSilentFail("AllowRotten");
if (rotten != null)
s.filter.SetAllow(rotten, false);
}
private static void CheckMakeAndLoad(StorageKind kind, IStoreSettingsParent parent, string what)
{
StorageSettings s = StorageDefaultsMenu.SettingsOf(parent);
s.Priority = StoragePriority.Important;
s.filter.SetDisallowAll();
s.filter.SetAllow(ThingDefOf.Steel, true);
s.filter.SetAllow(ThingDefOf.Silver, true);
s.filter.AllowedHitPointsPercents = new FloatRange(0.25f, 1f);
StorageDefaultsMenu.MakeDefault(kind, s);
StorageTemplate saved = DefaultsLibrary.Get(kind.Key);
T.NotNull(saved, what + ": the saved default");
T.Check(saved.SameSettingsAs(kind.Capture(s)), what + ": the saved default differs from the current settings");
T.Equal(UnlistedRule.Game, saved.unlisted, what + ": unlisted rule of the saved default");
s.Priority = StoragePriority.Low;
s.filter.SetAllowAll(kind.ParentFilter);
s.filter.AllowedHitPointsPercents = FloatRange.ZeroToOne;
T.Check(!kind.Capture(s).SameSettingsAs(saved), what + ": the change left the settings equal to the default");
StorageDefaultsMenu.LoadDefault(kind, s);
T.Check(kind.Capture(s).SameSettingsAs(saved), what + ": the settings differ from the default after Load your default");
T.Equal(StoragePriority.Important, s.Priority, what + ": priority after Load your default");
T.Check(s.filter.Allows(ThingDefOf.Steel) && s.filter.Allows(ThingDefOf.Silver) && !s.filter.Allows(ThingDefOf.WoodLog),
what + ": not Steel and Silver only after Load your default");
T.Check(Mathf.Approximately(0.25f, s.filter.AllowedHitPointsPercents.min), what + ": hit points range after Load your default");
}
/// Priority, allowed defs, every special filter and both ranges equal.
private static void CheckSameAs(StorageSettings expected, StorageSettings actual, string what)
{
T.Equal(expected.Priority, actual.Priority, what + ": priority");
var e = new HashSet(expected.filter.AllowedThingDefs);
var a = new HashSet(actual.filter.AllowedThingDefs);
var extra = a.Except(e).Select(d => d.defName).Take(5).ToList();
var missing = e.Except(a).Select(d => d.defName).Take(5).ToList();
T.Check(extra.Count == 0 && missing.Count == 0, what + ": allowed defs differ, extra " + string.Join(",", extra) + ", missing " + string.Join(",", missing));
foreach (SpecialThingFilterDef sf in DefDatabase.AllDefsListForReading)
T.Equal(expected.filter.Allows(sf), actual.filter.Allows(sf), what + ": special filter " + sf.defName);
FloatRange eh = expected.filter.AllowedHitPointsPercents, ah = actual.filter.AllowedHitPointsPercents;
T.Check(Mathf.Approximately(eh.min, ah.min) && Mathf.Approximately(eh.max, ah.max), what + ": hit points " + ah + ", expected " + eh);
T.Equal(expected.filter.AllowedQualityLevels.min, actual.filter.AllowedQualityLevels.min, what + ": lowest quality");
T.Equal(expected.filter.AllowedQualityLevels.max, actual.filter.AllowedQualityLevels.max, what + ": highest quality");
}
private static void CheckFullRanges(StorageSettings s, string what)
{
FloatRange hp = s.filter.AllowedHitPointsPercents;
T.Check(Mathf.Approximately(0f, hp.min) && Mathf.Approximately(1f, hp.max), what + ": hit points range is " + hp);
T.Equal(QualityCategory.Awful, s.filter.AllowedQualityLevels.min, what + ": lowest quality");
T.Equal(QualityCategory.Legendary, s.filter.AllowedQualityLevels.max, what + ": highest quality");
}
/// The stockpile tooltip has a line "- Dumping stockpile" for this type.
private static bool Lists(string tooltip, StorageKind kind) =>
tooltip != null && tooltip.Split('\n').Any(l => l.Trim() == "- " + kind.Label.CapitalizeFirst());
///
/// A Zone with ISlotGroupParent that is no Zone_Stockpile, like Preset Filtered Zones' Zone_PresetStockpile.
/// Never registered, so it never enters the save.
///
public class TestSlotZone : Zone, ISlotGroupParent
{
public StorageSettings settings;
public SlotGroup slotGroup;
public static TestSlotZone Make()
{
var z = new TestSlotZone();
z.settings = new StorageSettings(z);
z.slotGroup = new SlotGroup(z);
return z;
}
protected override Color NextZoneColor => Color.white;
public bool StorageTabVisible => true;
public bool IgnoreStoredThingsBeauty => false;
public string GroupingLabel => "test zones";
public int GroupingOrder => 0;
public bool HaulDestinationEnabled => true;
public StorageSettings GetStoreSettings() => settings;
public StorageSettings GetParentStoreSettings() => StorageSettings.EverStorableFixedSettings();
public void Notify_SettingsChanged() { }
public bool Accepts(Thing t) => settings.AllowedToAccept(t);
public IEnumerable AllSlotCells() => Cells;
public List AllSlotCellsList() => Cells;
public void Notify_ReceivedThing(Thing newItem) { }
public void Notify_LostThing(Thing newItem) { }
public string SlotYielderLabel() => label;
public SlotGroup GetSlotGroup() => slotGroup;
}
}
}