196 lines
11 KiB
C#
196 lines
11 KiB
C#
|
|
using BlaxZones;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
using Verse.AI;
|
||
|
|
|
||
|
|
namespace BlaxZonesTests
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Package Rules. Design: 1.0 "Delivery zone" > Tests (unusable cells, pen ring, reach).
|
||
|
|
/// Tests: [BlaxTest("rules: what", phase: N)], phase 1 fresh colony, 2 after save and reload, 3 after the component class was stripped.
|
||
|
|
/// </summary>
|
||
|
|
public static class RulesTests
|
||
|
|
{
|
||
|
|
private static string UnusableLine(int count) => "BlaxZones_UnusableCells".Translate(count).Resolve();
|
||
|
|
|
||
|
|
/// <summary>The reach rule asks free colonists first; make sure there is one before carving an area.</summary>
|
||
|
|
private static void EnsureFreeColonist()
|
||
|
|
{
|
||
|
|
if (Fixtures.Map.mapPawns.FreeColonistsSpawned.Count == 0)
|
||
|
|
Fixtures.Colonist();
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: 3 roofed cells are unusable and the inspect pane says 3; a water cell makes it 4")]
|
||
|
|
public static void RoofAndWater()
|
||
|
|
{
|
||
|
|
MapComponent_BlaxZones comp = Fixtures.Comp;
|
||
|
|
CellRect a = Fixtures.TestArea(4, 3);
|
||
|
|
Zone_Delivery zone = Fixtures.DeliveryZone(a);
|
||
|
|
T.Equal(0, comp.UnusableCellCount(zone), "unusable cells on a fresh zone");
|
||
|
|
T.Check(comp.InspectLineFor(zone) == null, "inspect line while every cell is usable: " + comp.InspectLineFor(zone));
|
||
|
|
|
||
|
|
Fixtures.Roof(new CellRect(a.minX, a.minZ, 3, 1));
|
||
|
|
T.Equal(3, comp.UnusableCellCount(zone), "unusable cells with 3 roofed");
|
||
|
|
string inspect = zone.GetInspectString();
|
||
|
|
T.Check(inspect.Contains(UnusableLine(3)), "inspect string '" + inspect + "' lacks '" + UnusableLine(3) + "'");
|
||
|
|
|
||
|
|
IntVec3 water = new IntVec3(a.minX, 0, a.maxZ);
|
||
|
|
Fixtures.Water(water);
|
||
|
|
T.Check(zone.ContainsCell(water), "the water cell left the zone");
|
||
|
|
T.Equal(4, comp.UnusableCellCount(zone), "unusable cells with 3 roofed and 1 water");
|
||
|
|
inspect = zone.GetInspectString();
|
||
|
|
T.Check(inspect.Contains(UnusableLine(4)), "inspect string '" + inspect + "' lacks '" + UnusableLine(4) + "'");
|
||
|
|
T.Check(comp.UnusableCells(zone).Contains(water), "the water cell is not among the unusable cells");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: one unusable cell reads in the singular")]
|
||
|
|
public static void SingularLine()
|
||
|
|
{
|
||
|
|
MapComponent_BlaxZones comp = Fixtures.Comp;
|
||
|
|
CellRect a = Fixtures.TestArea(3, 1);
|
||
|
|
Zone_Delivery zone = Fixtures.DeliveryZone(a);
|
||
|
|
Fixtures.Roof(new CellRect(a.minX, a.minZ, 1, 1));
|
||
|
|
T.Equal("BlaxZones_UnusableCell".Translate().Resolve(), comp.InspectLineFor(zone), "inspect line for 1 cell");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: a fence ring with a pen marker around the zone makes every cell unusable")]
|
||
|
|
public static void PenRing()
|
||
|
|
{
|
||
|
|
Map map = Fixtures.Map;
|
||
|
|
MapComponent_BlaxZones comp = Fixtures.Comp;
|
||
|
|
CellRect a = Fixtures.TestArea(3, 3);
|
||
|
|
Zone_Delivery zone = Fixtures.DeliveryZone(a);
|
||
|
|
T.Equal(0, comp.UnusableCellCount(zone), "unusable cells before the pen");
|
||
|
|
|
||
|
|
Thing marker = Fixtures.FenceRing(a);
|
||
|
|
T.NotNull(marker, "pen marker");
|
||
|
|
CompAnimalPenMarker pen = marker.TryGetComp<CompAnimalPenMarker>();
|
||
|
|
T.NotNull(pen, "CompAnimalPenMarker on the marker");
|
||
|
|
T.Check(map.animalPenManager.GetPenMarkerState(pen).Enclosed, "the fence ring is not an enclosed pen");
|
||
|
|
T.Info("reach rule inside the pen: " + DeliveryRules.ColonyCanReach(map, a.CenterCell));
|
||
|
|
T.Equal(zone.CellCount, comp.UnusableCellCount(zone), "unusable cells inside an enclosed pen");
|
||
|
|
T.Check(zone.GetInspectString().Contains(UnusableLine(zone.CellCount)), "inspect string lacks the count of " + zone.CellCount);
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: player walls closing off a zone make its cells unreachable, though CanReachColony says true")]
|
||
|
|
public static void WalledOff()
|
||
|
|
{
|
||
|
|
Map map = Fixtures.Map;
|
||
|
|
MapComponent_BlaxZones comp = Fixtures.Comp;
|
||
|
|
EnsureFreeColonist();
|
||
|
|
CellRect a = Fixtures.TestArea(3, 3);
|
||
|
|
Zone_Delivery zone = Fixtures.DeliveryZone(a);
|
||
|
|
// Caches the reach of the open district first, so the walls must not reuse that entry.
|
||
|
|
T.Equal(0, comp.UnusableCellCount(zone), "unusable cells before the walls");
|
||
|
|
|
||
|
|
foreach (IntVec3 c in a.ExpandedBy(1).EdgeCells)
|
||
|
|
Fixtures.Wall(c);
|
||
|
|
T.Equal(9, zone.CellCount, "zone cells after the walls (none under them)");
|
||
|
|
|
||
|
|
IntVec3 inside = a.CenterCell;
|
||
|
|
T.Check(DropCellFinder.IsGoodDropSpot(inside, map, allowFogged: false, canRoofPunch: false),
|
||
|
|
"IsGoodDropSpot refuses the walled-in cell, so this would not test the reach rule");
|
||
|
|
T.Check(map.reachability.CanReachColony(inside), "CanReachColony is false inside the walls; the test needs it true");
|
||
|
|
T.Check(!DeliveryRules.ColonyCanReach(map, inside), "the reach rule says a colonist reaches a closed wall ring");
|
||
|
|
T.Check(!DeliveryRules.PodCellOk(map, inside), "PodCellOk accepts a walled-in cell");
|
||
|
|
T.Equal(zone.CellCount, comp.UnusableCellCount(zone), "unusable cells inside the walls");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: PodCellOk is true on a plain cell and false on a fogged or roofed cell")]
|
||
|
|
public static void PodCell()
|
||
|
|
{
|
||
|
|
Map map = Fixtures.Map;
|
||
|
|
EnsureFreeColonist();
|
||
|
|
CellRect a = Fixtures.TestArea(3, 1);
|
||
|
|
IntVec3 fogged = new IntVec3(a.minX, 0, a.minZ);
|
||
|
|
IntVec3 plain = new IntVec3(a.minX + 1, 0, a.minZ);
|
||
|
|
IntVec3 roofed = new IntVec3(a.maxX, 0, a.minZ);
|
||
|
|
T.Check(DeliveryRules.PodCellOk(map, plain), "PodCellOk refuses a plain test area cell");
|
||
|
|
|
||
|
|
map.fogGrid.Refog(new CellRect(fogged.x, fogged.z, 1, 1));
|
||
|
|
T.Check(fogged.Fogged(map), "Refog left the cell unfogged");
|
||
|
|
T.Check(!DeliveryRules.PodCellOk(map, fogged), "PodCellOk accepts a fogged cell");
|
||
|
|
|
||
|
|
Fixtures.Roof(new CellRect(roofed.x, roofed.z, 1, 1));
|
||
|
|
T.Check(!DeliveryRules.PodCellOk(map, roofed), "PodCellOk accepts a roofed cell");
|
||
|
|
|
||
|
|
T.Check(DeliveryRules.PodCellOk(map, plain), "PodCellOk refuses the plain cell after its neighbours changed");
|
||
|
|
T.Check(!DeliveryRules.PodCellOk(null, plain), "PodCellOk accepts a null map");
|
||
|
|
T.Check(!DeliveryRules.PodCellOk(map, new IntVec3(-1, 0, -1)), "PodCellOk accepts an out-of-bounds cell");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: the reach result is cached per district and recomputed after ReachMaxAgeTicks")]
|
||
|
|
public static void ReachCacheAge()
|
||
|
|
{
|
||
|
|
Map map = Fixtures.Map;
|
||
|
|
MapComponent_BlaxZones comp = Fixtures.Comp;
|
||
|
|
EnsureFreeColonist();
|
||
|
|
CellRect a = Fixtures.TestArea(3, 3);
|
||
|
|
IntVec3 first = a.CenterCell;
|
||
|
|
IntVec3 second = new IntVec3(a.minX, 0, a.minZ);
|
||
|
|
District district = first.GetDistrict(map);
|
||
|
|
T.NotNull(district, "district of the test area");
|
||
|
|
T.Check(second.GetDistrict(map) == district, "two cells of one test area lie in different districts");
|
||
|
|
|
||
|
|
T.Check(DeliveryRules.ColonyCanReach(map, first), "a free colonist can't reach a plain test area cell");
|
||
|
|
T.Check(comp.TryGetCachedReach(district, PathEndMode.OnCell, out bool cached) && cached, "no fresh cached OnCell entry after the check");
|
||
|
|
T.Check(!comp.TryGetCachedReach(district, PathEndMode.Touch, out _), "an OnCell check filled the Touch entry");
|
||
|
|
|
||
|
|
// A false entry the live check would never give: only the cache can answer false now.
|
||
|
|
comp.StoreReach(district, PathEndMode.OnCell, false);
|
||
|
|
T.Check(!DeliveryRules.ColonyCanReach(map, second), "another cell of the district didn't use the cached entry");
|
||
|
|
T.Check(!DeliveryRules.PodCellOk(map, second), "PodCellOk ignored the cached reach entry");
|
||
|
|
|
||
|
|
Fixtures.Tick(DeliveryRules.ReachMaxAgeTicks);
|
||
|
|
T.Check(!comp.TryGetCachedReach(district, PathEndMode.OnCell, out _), "an entry ReachMaxAgeTicks old is still trusted");
|
||
|
|
T.Check(DeliveryRules.ColonyCanReach(map, second), "the reach rule was not recomputed after ReachMaxAgeTicks");
|
||
|
|
District after = second.GetDistrict(map);
|
||
|
|
T.Check(comp.TryGetCachedReach(after, PathEndMode.OnCell, out cached) && cached, "the recomputed result was not cached");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: a reach entry is dropped when its district changes")]
|
||
|
|
public static void ReachCacheDistrictChange()
|
||
|
|
{
|
||
|
|
Map map = Fixtures.Map;
|
||
|
|
MapComponent_BlaxZones comp = Fixtures.Comp;
|
||
|
|
EnsureFreeColonist();
|
||
|
|
CellRect a = Fixtures.TestArea(3, 3);
|
||
|
|
District district = a.CenterCell.GetDistrict(map);
|
||
|
|
T.NotNull(district, "district of the test area");
|
||
|
|
comp.StoreReach(district, PathEndMode.OnCell, false);
|
||
|
|
T.Check(comp.TryGetCachedReach(district, PathEndMode.OnCell, out bool cached) && !cached, "the stored entry is not returned");
|
||
|
|
|
||
|
|
// Vanilla stamps a reused district this way when walls go up next to it (RegionAndRoomUpdater.cs:307-314).
|
||
|
|
Fixtures.Tick(1);
|
||
|
|
district.Notify_RoomShapeOrContainedBedsChanged();
|
||
|
|
T.Check(!comp.TryGetCachedReach(district, PathEndMode.OnCell, out _), "an entry of a changed district is still trusted");
|
||
|
|
T.Check(DeliveryRules.ColonyCanReach(map, a.CenterCell), "the reach rule was not recomputed for the changed district");
|
||
|
|
}
|
||
|
|
|
||
|
|
[BlaxTest("rules: a selected zone's count refreshes within SelectedRecheckTicks after a change")]
|
||
|
|
public static void SelectedRecheck()
|
||
|
|
{
|
||
|
|
MapComponent_BlaxZones comp = Fixtures.Comp;
|
||
|
|
CellRect a = Fixtures.TestArea(4, 3);
|
||
|
|
Zone_Delivery zone = Fixtures.DeliveryZone(a);
|
||
|
|
Find.Selector.ClearSelection();
|
||
|
|
Find.Selector.Select(zone, playSound: false);
|
||
|
|
T.Check(Find.Selector.SelectedZone == zone, "the delivery zone is not selected");
|
||
|
|
T.Equal(0, comp.UnusableCellCount(zone), "unusable cells before the change");
|
||
|
|
|
||
|
|
Fixtures.Roof(new CellRect(a.minX, a.minZ, 2, 1));
|
||
|
|
// Roofing tells the zone nothing; the selected zone's count is a cache, refreshed on a timer.
|
||
|
|
T.Equal(0, comp.UnusableCellCount(zone), "the selected zone's count before its recheck (expected the cached value)");
|
||
|
|
|
||
|
|
Fixtures.Tick(DeliveryRules.SelectedRecheckTicks);
|
||
|
|
T.Equal(2, comp.UnusableCellCount(zone), "unusable cells " + DeliveryRules.SelectedRecheckTicks + " ticks after roofing 2");
|
||
|
|
string inspect = zone.GetInspectString();
|
||
|
|
T.Check(inspect.Contains(UnusableLine(2)), "inspect string '" + inspect + "' lacks '" + UnusableLine(2) + "'");
|
||
|
|
|
||
|
|
Find.Selector.ClearSelection();
|
||
|
|
Fixtures.Roof(new CellRect(a.minX + 2, a.minZ, 1, 1));
|
||
|
|
T.Equal(3, comp.UnusableCellCount(zone), "an unselected zone counts live");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|