using System.Collections.Generic;
using System.Linq;
using BlaxZones;
using RimWorld;
using Verse;
namespace BlaxZonesTests
{
///
/// Package Designators. Design: 1.0 "Delivery zone" > Tests (designating creates a Zone_Delivery, expand only adds cells, refuses stockpile cells, master toggle).
/// Tests: [BlaxTest("designators: what", phase: N)], phase 1 fresh colony, 2 after save and reload, 3 after the component class was stripped.
///
public static class DesignatorsTests
{
/// Keys this package adds in Languages/English/Keyed/BlaxZones_Designators.xml.
private static readonly string[] OwnKeys = { Designator_ZoneAdd_Delivery.DescKey };
///
/// Text fragments of errors this package could cause at startup: the failed XML patch names its file, an
/// unresolved type or a throwing constructor names the class ("could not instantiate special designator from
/// class ..."), a missing texture its path.
///
private static readonly string[] StartupErrorFragments =
{
"BlaxZones_Designators", "Designator_ZoneAdd_Delivery", Designator_ZoneAdd_Delivery.IconPath,
};
/// The designator in the Zone menu (exact type, not the expand one), or a failure.
private static Designator_ZoneAdd_Delivery MenuDesignator()
{
List menu = DesignationCategoryDefOf.Zone.AllResolvedDesignators;
Designator_ZoneAdd_Delivery d = menu.FirstOrDefault(x => x.GetType() == typeof(Designator_ZoneAdd_Delivery)) as Designator_ZoneAdd_Delivery;
T.NotNull(d, "Designator_ZoneAdd_Delivery in the Zone menu");
return d;
}
/// Designates the cells the way a drag does, with nothing selected unless is given; tracks every new zone.
private static List Designate(Designator_ZoneAdd_Delivery d, IEnumerable cells, Zone select = null)
{
Map map = Fixtures.Map;
Find.Selector.ClearSelection();
if (select != null)
Find.Selector.Select(select, playSound: false, forceDesignatorDeselect: false);
var before = new HashSet(map.zoneManager.AllZones);
d.DesignateMultiCell(cells.ToList());
List made = map.zoneManager.AllZones.Where(z => !before.Contains(z)).ToList();
foreach (Zone z in made)
Fixtures.TrackZone(z);
return made;
}
private static string Cells(IEnumerable cells) => string.Join(" ", cells.Take(6).Select(c => c.ToString()));
[BlaxTest("designators: the Zone menu holds the delivery designator right after Growing, with its label, tooltip, tinted icon and sounds")]
public static void InZoneMenu()
{
List menu = DesignationCategoryDefOf.Zone.AllResolvedDesignators;
int growing = menu.FindIndex(x => x.GetType() == typeof(Designator_ZoneAdd_Growing));
T.Check(growing >= 0, "Designator_ZoneAdd_Growing not in the Zone menu");
List ours = Enumerable.Range(0, menu.Count).Where(i => menu[i].GetType() == typeof(Designator_ZoneAdd_Delivery)).ToList();
T.Equal(1, ours.Count, "Designator_ZoneAdd_Delivery entries in the Zone menu");
T.Check(!menu.Any(x => x is Designator_ZoneAdd_Delivery_Expand), "the expand designator is in the Zone menu");
int at = ours[0];
if (at != growing + 1)
{
// A mod loaded after Zones may insert after Growing too; only its own designators may sit between.
List between = at > growing ? menu.GetRange(growing + 1, at - growing - 1) : new List();
T.Check(at > growing && between.All(x => x.GetType().Assembly != typeof(Designator).Assembly),
"delivery designator at index " + at + ", Growing at " + growing + ": " + string.Join(", ", menu.Select(x => x.GetType().Name)));
T.Info("other mods' designators between Growing and delivery: " + string.Join(", ", between.Select(x => x.GetType().FullName)));
}
var d = (Designator_ZoneAdd_Delivery)menu[at];
T.Equal("BlaxZones_DeliveryZone".Translate().ToString(), d.defaultLabel, "label");
T.Check(!d.defaultDesc.NullOrEmpty() && d.defaultDesc == Designator_ZoneAdd_Delivery.DescKey.Translate().ToString(), "tooltip is not the translated " + Designator_ZoneAdd_Delivery.DescKey);
T.Check(d.defaultDesc.Contains("4"), "tooltip doesn't mention the 4-cell home area growth: " + d.defaultDesc);
T.Check(d.icon != null && d.icon != BaseContent.BadTex, "icon is missing or the bad-texture placeholder");
T.Equal(Zone_Delivery.IconColor, d.defaultIconColor, "icon tint (defaultIconColor)");
T.Equal(Zone_Delivery.IconColor, d.IconDrawColor, "icon draw color");
T.Equal(Designator_ZoneAdd_Delivery.TutorTag, d.tutorTag, "tutorTag");
T.Equal(SoundDefOf.Designate_ZoneAdd_Stockpile, d.soundSucceeded, "soundSucceeded");
T.Equal(SoundDefOf.Designate_DragAreaAdd, d.soundDragSustain, "soundDragSustain");
T.Equal(SoundDefOf.Designate_DragZone_Changed, d.soundDragChanged, "soundDragChanged");
var missing = OwnKeys.Where(k => !k.CanTranslate()).ToList();
T.Check(missing.Count == 0, "strings missing from Keyed: " + string.Join(", ", missing));
}
[BlaxTest("designators: the patch and the designators load with no error, core only included")]
public static void LoadedWithoutErrors()
{
T.Info("DLCs: Royalty " + ModsConfig.RoyaltyActive + ", Ideology " + ModsConfig.IdeologyActive + ", Biotech " + ModsConfig.BiotechActive +
", Odyssey " + ModsConfig.OdysseyActive);
var errors = Log.Messages
.Where(m => (m.type == LogMessageType.Error) && m.text != null && StartupErrorFragments.Any(f => m.text.Contains(f)))
.ToList();
T.Check(errors.Count == 0, "errors from the designator patch or class: " + string.Join(" | ", errors.Take(3).Select(e => e.text)));
MenuDesignator();
var expand = DesignatorUtility.FindAllowedDesignator();
T.NotNull(expand, "the expand designator");
T.Equal("DesignatorZoneExpand".Translate().ToString(), expand.defaultLabel, "expand label");
T.Equal(Zone_Delivery.IconColor, expand.defaultIconColor, "expand icon tint");
}
[BlaxTest("designators: Visible follows the delivery zones setting live, menu and expand designator")]
public static void VisibleFollowsSetting()
{
BlaxZonesSettings s = Fixtures.Settings;
Designator_ZoneAdd_Delivery d = MenuDesignator();
var expand = DesignatorUtility.FindAllowedDesignator();
s.deliveryZones = true;
T.Check(d.Visible && expand.Visible, "hidden with the setting on");
s.deliveryZones = false;
T.Check(!d.Visible, "menu designator visible with the setting off");
T.Check(!expand.Visible, "expand designator visible with the setting off");
s.deliveryZones = true;
T.Check(d.Visible && expand.Visible, "still hidden after turning the setting back on");
}
[BlaxTest("designators: designating unroofed cells creates one delivery zone with exactly those cells and default settings")]
public static void DesignateCreatesZone()
{
Map map = Fixtures.Map;
CellRect area = Fixtures.TestArea(4, 3);
Designator_ZoneAdd_Delivery d = MenuDesignator();
var refused = area.Cells.Where(c => !d.CanDesignateCell(c).Accepted).ToList();
T.Check(refused.Count == 0, "CanDesignateCell refused test-area cells: " + Cells(refused));
T.Check(area.Cells.All(c => !c.Roofed(map)), "test area is roofed");
List made = Designate(d, area.Cells);
T.Equal(1, made.Count, "new zones");
var zone = made[0] as Zone_Delivery;
T.Check(zone != null, "the new zone is a " + made[0].GetType().FullName + ", not a Zone_Delivery");
T.Equal(area.Area, zone.CellCount, "zone cells");
var wrong = area.Cells.Where(c => map.zoneManager.ZoneAt(c) != zone).ToList();
T.Check(wrong.Count == 0, "cells not in the new zone: " + Cells(wrong));
T.Check(zone.label != null && zone.label.StartsWith("BlaxZones_DeliveryZone".Translate()), "label '" + zone.label + "'");
T.Check(Find.Selector.SelectedZone == zone, "the new zone is not selected");
T.NotNull(zone.filter, "filter of the designated zone");
T.Equal(StoragePriority.Normal, zone.priority, "priority");
T.Check(zone.acceptOrbital, "orbital checkbox off on a new zone");
T.Check(!zone.campHere, "camp checkbox on on a new zone");
}
[BlaxTest("designators: GetZoneAddGizmos returns the expand designator, which the zone's gizmos include")]
public static void ZoneAddGizmo()
{
Zone_Delivery zone = Fixtures.DeliveryZone(Fixtures.TestArea(2, 2));
List add = zone.GetZoneAddGizmos().ToList();
T.Equal(1, add.Count, "zone-add gizmos");
T.Check(add[0] != null && add[0].GetType() == typeof(Designator_ZoneAdd_Delivery_Expand),
"zone-add gizmo is " + (add[0]?.GetType().FullName ?? "null") + ", not Designator_ZoneAdd_Delivery_Expand");
T.Check(ReferenceEquals(add[0], DesignatorUtility.FindAllowedDesignator()),
"zone-add gizmo is not the shared expand designator");
T.Check(zone.GetGizmos().Contains(add[0]), "GetGizmos doesn't include the expand designator");
}
[BlaxTest("designators: the expand gizmo grows the selected zone and only adds cells")]
public static void ExpandOnlyAdds()
{
Map map = Fixtures.Map;
CellRect area = Fixtures.TestArea(6, 3);
var first = new CellRect(area.minX, area.minZ, 3, 3);
List made = Designate(MenuDesignator(), first.Cells);
T.Equal(1, made.Count, "zones from the first drag");
var zone = (Zone_Delivery)made[0];
var oldCells = new HashSet(zone.Cells);
T.Equal(9, oldCells.Count, "cells before expanding");
int zoneId = zone.ID;
int deliveryZones = map.zoneManager.AllZones.Count(z => z is Zone_Delivery);
var expand = (Designator_ZoneAdd_Delivery_Expand)zone.GetZoneAddGizmos().Single();
T.Check(area.Cells.All(c => expand.CanDesignateCell(c).Accepted), "expand designator refuses cells of the area or the zone");
// Drag over the whole area: the zone's own cells and the new ones.
List extra = Designate(expand, area.Cells, select: zone);
T.Equal(0, extra.Count, "new zones made by expanding");
T.Equal(deliveryZones, map.zoneManager.AllZones.Count(z => z is Zone_Delivery), "delivery zones on the map");
T.Equal(zoneId, zone.ID, "zone ID");
T.Equal(area.Area, zone.CellCount, "cells after expanding");
var lost = oldCells.Where(c => map.zoneManager.ZoneAt(c) != zone).ToList();
T.Check(lost.Count == 0, "expanding dropped cells: " + Cells(lost));
var notAdded = area.Cells.Where(c => map.zoneManager.ZoneAt(c) != zone).ToList();
T.Check(notAdded.Count == 0, "cells not added: " + Cells(notAdded));
// Dragging the zone's own cells again changes nothing.
Designate(expand, first.Cells, select: zone);
T.Equal(area.Area, zone.CellCount, "cells after dragging over the zone's own cells");
}
[BlaxTest("designators: the designator refuses cells in an existing stockpile and leaves it whole")]
public static void RefusesStockpileCells()
{
Map map = Fixtures.Map;
CellRect area = Fixtures.TestArea(4, 3);
var pileRect = new CellRect(area.minX, area.minZ, 2, 3);
Zone_Stockpile pile = Fixtures.Stockpile(pileRect);
Designator_ZoneAdd_Delivery d = MenuDesignator();
var accepted = pileRect.Cells.Where(c => d.CanDesignateCell(c).Accepted).ToList();
T.Check(accepted.Count == 0, "CanDesignateCell accepted stockpile cells: " + Cells(accepted));
var free = area.Cells.Where(c => !pileRect.Contains(c)).ToList();
T.Check(free.All(c => d.CanDesignateCell(c).Accepted), "CanDesignateCell refused free cells next to the stockpile");
// A click on one stockpile cell does nothing, not even select the stockpile.
T.Equal(0, Designate(d, new[] { pileRect.CenterCell }).Count, "zones made by clicking a stockpile cell");
T.Check(Find.Selector.SelectedZone == null, "clicking a stockpile cell selected " + Find.Selector.SelectedZone);
// Even when handed every cell, it takes only the free ones.
List made = Designate(d, area.Cells);
T.Equal(1, made.Count, "new zones");
T.Check(made[0] is Zone_Delivery, "new zone is a " + made[0].GetType().FullName);
T.Equal(free.Count, made[0].CellCount, "delivery zone cells");
T.Equal(pileRect.Area, pile.CellCount, "stockpile cells");
var taken = pileRect.Cells.Where(c => map.zoneManager.ZoneAt(c) != pile).ToList();
T.Check(taken.Count == 0, "stockpile cells taken: " + Cells(taken));
}
[BlaxTest("designators: with auto home area on, designated cells mark the 4-cell home border")]
public static void HomeAreaBorder()
{
Map map = Fixtures.Map;
CellRect area = Fixtures.TestArea(3, 3);
CellRect outer = area.ExpandedBy(5).ClipInsideMap(map);
Area_Home home = map.areaManager.Home;
var homeBefore = outer.Cells.ToDictionary(c => c, c => home[c]);
bool autoHomeBefore = Find.PlaySettings.autoHomeArea;
Fixtures.OnCleanup(() =>
{
Find.PlaySettings.autoHomeArea = autoHomeBefore;
foreach (var kv in homeBefore)
map.areaManager.Home[kv.Key] = kv.Value;
});
foreach (IntVec3 c in outer)
home[c] = false;
Find.PlaySettings.autoHomeArea = true;
List made = Designate(MenuDesignator(), area.Cells);
T.Equal(1, made.Count, "new zones");
CellRect border = area.ExpandedBy(4).ClipInsideMap(map);
var notHome = border.Cells.Where(c => !home[c]).ToList();
T.Check(notHome.Count == 0, notHome.Count + " cells within 4 of the zone are not home area: " + Cells(notHome));
var beyond = outer.Cells.Where(c => !border.Contains(c) && home[c]).ToList();
T.Check(beyond.Count == 0, beyond.Count + " cells 5 away from the zone became home area: " + Cells(beyond));
}
}
}