using System; using System.Collections.Generic; using System.Linq; using BlaxZones; using RimWorld; using Verse; namespace BlaxZonesTests { /// /// Package Router. Design: "DeliveryRouter, shared by every delivery path"; 1.0 "Delivery zone" > Tests (ZonesApi, overflow radius). /// Tests: [BlaxTest("router: what", phase: N)], phase 1 fresh colony, 2 after save and reload, 3 after the component class was stripped. /// /// Every test first sets aside the delivery zones earlier tests kept on the map (deregistered, then /// registered again in cleanup), so only its own zones can be candidates. /// public static class RouterTests { // ---- ZonesApi (design 1.0 "Delivery zone" > Tests) -------------------------------- [BlaxTest("router: ZonesApi returns a zone cell for steel, one pod per call, and false for a pawn")] public static void ApiSteelAndPawn() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); Zone_Delivery zone = Fixtures.DeliveryZone(Fixtures.TestArea(3, 3)); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 50); T.Check(ZonesApi.TryFindDropSpot(map, steel, out IntVec3 first), "ZonesApi refused steel with a fitting zone"); T.Check(zone.cells.Contains(first), "ZonesApi cell " + first + " is not in the zone"); T.Check(DeliveryRules.PodCellOk(map, first), "ZonesApi cell " + first + " fails the pod rule"); T.Equal(Fixtures.Comp.CellsByDistance(zone)[0], first, "first cell (nearest the centroid)"); // The caller spawns the pod at the cell and asks again: the falling pod marks the cell as taken. DropPodAt(first, steel); Thing steel2 = Fixtures.Stack(ThingDefOf.Steel, 50); T.Check(ZonesApi.TryFindDropSpot(map, steel2, out IntVec3 second), "ZonesApi refused the second stack"); T.Check(second != first, "second stack got the cell of the falling pod " + first); T.Check(zone.cells.Contains(second), "second cell " + second + " is not in the zone"); Pawn colonist = Fixtures.Colonist(); T.Check(!ZonesApi.TryFindDropSpot(map, colonist, out IntVec3 pawnCell), "ZonesApi accepted a pawn"); T.Check(!pawnCell.IsValid, "ZonesApi set a cell for a pawn"); T.Check(!DeliveryRouter.TryFindDropSpot(map, colonist, DeliverySource.Orbital, out _), "router accepted a pawn"); } [BlaxTest("router: with the zone's orbital checkbox off, ZonesApi returns false")] public static void ApiOrbitalOff() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); Zone_Delivery zone = Fixtures.DeliveryZone(Fixtures.TestArea(2, 2), orbital: false); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 20); T.Check(!DeliveryRouter.Refuses(map, steel), "router refused steel on a map with a delivery zone"); T.Equal(0, DeliveryRouter.Candidates(map, steel, DeliverySource.Orbital).Count, "orbital candidates with the checkbox off"); T.Check(!ZonesApi.TryFindDropSpot(map, steel, out IntVec3 cell), "ZonesApi returned " + cell + " with the orbital checkbox off"); T.Check(!cell.IsValid, "ZonesApi set a cell with the orbital checkbox off"); zone.acceptOrbital = true; T.Check(ZonesApi.TryFindDropSpot(map, steel, out cell) && zone.cells.Contains(cell), "ZonesApi refused steel with the checkbox back on"); } [BlaxTest("router: ZonesApi does not read Route orbital trade (current behaviour, owner decision open)")] public static void ApiIgnoresRouteOrbital() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); Zone_Delivery zone = Fixtures.DeliveryZone(Fixtures.TestArea(2, 2)); Fixtures.Settings.routeOrbital = false; T.Check(ZonesApi.TryFindDropSpot(map, Fixtures.Stack(ThingDefOf.Silver, 100), out IntVec3 cell) && zone.cells.Contains(cell), "ZonesApi returned " + cell + " for silver with Route orbital trade off; ZonesApi callers are not gated by the setting today"); } [BlaxTest("router: with overflowRadius 1 the overflow search ends and uses radius 5")] public static void OverflowRadiusNeverBelow5() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); Fixtures.Settings.overflowRadius = 1; T.Equal(BlaxZonesSettings.MinOverflowRadius, DeliveryRouter.OverflowRadius, "overflow radius with the setting at 1"); // A 1-cell zone in the middle of an 11x11 area. The area and its margin are roofed, so no cell // within 8 of the center takes a pod: a search below radius 5 would never end here. CellRect a = Fixtures.TestArea(11, 11); IntVec3 center = a.CenterCell; Zone_Delivery zone = Fixtures.DeliveryZone(CellRect.SingleCell(center)); Fixtures.Roof(a.ExpandedBy(Fixtures.AreaMargin)); T.Check(!DeliveryRouter.TryOverflow(zone, out IntVec3 cell), "overflow found " + cell + " in a fully roofed area"); T.Check(!cell.IsValid, "failed overflow set a cell"); // One free cell 7 away: beyond radius 5, so still nothing. IntVec3 far = center + new IntVec3(7, 0, 0); map.roofGrid.SetRoof(far, null); T.Check(DropCellFinder.IsGoodDropSpot(far, map, allowFogged: false, canRoofPunch: false), "the unroofed cell 7 away is no good drop spot"); T.Check(!DeliveryRouter.TryOverflow(zone, out cell), "overflow with radius 5 found " + cell + ", 7 cells away"); // One free cell exactly 5 away: found, so the search ran out to 5. IntVec3 edge = center + new IntVec3(0, 0, -5); map.roofGrid.SetRoof(edge, null); T.Check(DeliveryRouter.TryOverflow(zone, out cell), "overflow with radius 5 missed the free cell 5 away"); T.Equal(edge, cell, "overflow cell"); Fixtures.Settings.overflowNearZone = false; T.Check(!DeliveryRouter.TryOverflow(zone, out cell), "overflow ran with 'Overflow lands next to the zone' off"); } // ---- candidates and refusals -------------------------------------------------------- [BlaxTest("router: candidates order by priority then ID, and a medicine-only filter excludes steel")] public static void CandidatesOrder() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); CellRect a = Fixtures.TestArea(8, 2); Zone_Delivery MakeZone(int i, ThingFilter filter, StoragePriority priority) => Fixtures.DeliveryZone(new CellRect(a.minX + 2 * i, a.minZ, 2, 2), filter, priority); Zone_Delivery normalA = MakeZone(0, null, StoragePriority.Normal); Zone_Delivery important = MakeZone(1, null, StoragePriority.Important); Zone_Delivery normalB = MakeZone(2, null, StoragePriority.Normal); Zone_Delivery medicineOnly = MakeZone(3, Fixtures.FilterOf(ThingDefOf.MedicineIndustrial), StoragePriority.Critical); T.Check(normalA.ID < normalB.ID, "zone IDs are not ascending in creation order"); // Register normalA again so ZoneManager lists it after normalB: the order must come from the ID. map.zoneManager.DeregisterZone(normalA); map.zoneManager.RegisterZone(normalA); T.Check(map.zoneManager.AllZones.IndexOf(normalA) > map.zoneManager.AllZones.IndexOf(normalB), "re-registering did not move the zone"); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); T.Equal(Ids(important, normalA, normalB), Ids(DeliveryRouter.Candidates(map, steel, DeliverySource.Orbital).ToArray()), "steel candidates"); Thing medicine = Fixtures.Stack(ThingDefOf.MedicineIndustrial, 5); T.Equal(Ids(medicineOnly, important, normalA, normalB), Ids(DeliveryRouter.Candidates(map, medicine, DeliverySource.Orbital).ToArray()), "medicine candidates"); T.Check(DeliveryRouter.TryFindDropSpot(map, medicine, DeliverySource.Orbital, out IntVec3 medCell) && medicineOnly.cells.Contains(medCell), "medicine did not land in the Critical medicine-only zone (got " + medCell + ")"); T.Check(DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out IntVec3 steelCell) && important.cells.Contains(steelCell), "steel did not land in the Important zone (got " + steelCell + ")"); } [BlaxTest("router: refuses a corpse, a pawn and a map with no delivery zones")] public static void RefusesCorpseAndEmptyMap() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); CellRect a = Fixtures.TestArea(4, 2); // A stockpile is no delivery zone. Fixtures.Stockpile(new CellRect(a.minX + 2, a.minZ, 2, 2)); T.Check(DeliveryRouter.Refuses(map, steel), "router took steel on a map with no delivery zones"); T.Check(!DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out IntVec3 cell) && !cell.IsValid, "routed with no delivery zones"); Zone_Delivery zone = Fixtures.DeliveryZone(new CellRect(a.minX, a.minZ, 2, 2)); T.Check(!DeliveryRouter.Refuses(map, steel), "router refused steel with a delivery zone"); Pawn chicken = Fixtures.Animal(ThingDefOf.Chicken, tamed: false, at: new IntVec3(a.maxX, 0, a.maxZ + 1)); chicken.Kill(null); Corpse corpse = chicken.Corpse; T.NotNull(corpse, "chicken corpse"); T.Check(DeliveryRouter.Refuses(map, corpse), "router took a corpse"); T.Equal(0, DeliveryRouter.Candidates(map, corpse, DeliverySource.Orbital).Count, "candidates for a corpse"); T.Check(!DeliveryRouter.TryFindDropSpot(map, corpse, DeliverySource.Orbital, out cell), "router routed a corpse to " + cell); T.Check(!ZonesApi.TryFindDropSpot(map, corpse, out cell), "ZonesApi routed a corpse to " + cell); Pawn colonist = Fixtures.Colonist(); T.Check(DeliveryRouter.Refuses(map, colonist), "router took a pawn"); T.Check(DeliveryRouter.Refuses(map, null), "router took null"); T.Check(DeliveryRouter.Refuses(null, steel), "router took a null map"); T.Check(zone.cells.Count > 0, "zone lost its cells"); } [BlaxTest("router: refuses Wastepack (Biotech)")] public static void RefusesWastepack() { T.NeedsDlc("Biotech", ModsConfig.BiotechActive); Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); Fixtures.DeliveryZone(Fixtures.TestArea(2, 2)); T.NotNull(ThingDefOf.Wastepack, "ThingDefOf.Wastepack with Biotech"); Thing wastepack = Fixtures.Stack(ThingDefOf.Wastepack, 1); T.Check(DeliveryFilters.NeverRouted(wastepack), "NeverRouted let a wastepack through"); T.Check(DeliveryRouter.Refuses(map, wastepack), "router took a wastepack"); T.Equal(0, DeliveryRouter.Candidates(map, wastepack, DeliverySource.Orbital).Count, "candidates for a wastepack"); T.Check(!ZonesApi.TryFindDropSpot(map, wastepack, out IntVec3 cell), "ZonesApi routed a wastepack to " + cell); } [BlaxTest("router: the Wastepack refusal skips a missing def with no error")] public static void WastepackRefusalWithoutBiotech() { Map map = Fixtures.Map; T.Check(ModsConfig.BiotechActive || ThingDefOf.Wastepack == null, "ThingDefOf.Wastepack is set without Biotech"); SetAsideOtherDeliveryZones(); Zone_Delivery zone = Fixtures.DeliveryZone(Fixtures.TestArea(2, 2)); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); T.Check(!DeliveryFilters.NeverRouted(steel), "NeverRouted refused steel"); T.Check(!DeliveryRouter.Refuses(map, steel), "router refused steel"); T.Check(ZonesApi.TryFindDropSpot(map, steel, out IntVec3 cell) && zone.cells.Contains(cell), "ZonesApi refused steel"); } [BlaxTest("router: a map whose component was removed refuses everything")] public static void RefusesWithoutComponent() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); Zone_Delivery zone = Fixtures.DeliveryZone(Fixtures.TestArea(2, 2)); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); T.Check(!DeliveryRouter.Refuses(map, steel), "router refused steel with the component in place"); MapComponent_BlaxZones comp = Fixtures.Comp; int index = map.components.IndexOf(comp); T.Check(index >= 0, "component not in map.components"); Fixtures.OnCleanup(() => { if (!map.components.Contains(comp)) map.components.Insert(Math.Min(index, map.components.Count), comp); }); map.components.RemoveAt(index); T.Check(MapComponent_BlaxZones.Of(map) == null, "the component is still found after removal"); T.Check(DeliveryRouter.Refuses(map, steel), "router took steel on a map without the component"); T.Check(!DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out IntVec3 cell) && !cell.IsValid, "router routed without the component"); T.Check(!ZonesApi.TryFindDropSpot(map, steel, out cell) && !cell.IsValid, "ZonesApi routed without the component"); T.Check(zone.cells.Count > 0, "zone lost its cells"); } // ---- cells ---------------------------------------------------------------------------- [BlaxTest("router: CellsByDistance puts the centroid-nearest cell first and is rebuilt after AddCell and RemoveCell")] public static void CellOrder() { SetAsideOtherDeliveryZones(); CellRect a = Fixtures.TestArea(5, 3); Zone_Delivery zone = Fixtures.DeliveryZone(new CellRect(a.minX, a.minZ, 3, 3)); MapComponent_BlaxZones comp = Fixtures.Comp; List order = comp.CellsByDistance(zone); CheckSorted(zone, order, "3x3"); T.Equal(new IntVec3(a.minX + 1, 0, a.minZ + 1), order[0], "3x3 first cell"); T.Check(ReferenceEquals(order, comp.CellsByDistance(zone)), "the cell order is not cached"); T.Equal(order[0], DeliveryRouter.OverflowCenter(zone), "OverflowCenter of the 3x3 zone"); // Two more columns: 5x3, the centroid moves to the middle column. for (int x = a.minX + 3; x <= a.minX + 4; x++) for (int z = a.minZ; z < a.minZ + 3; z++) zone.AddCell(new IntVec3(x, 0, z)); List grown = comp.CellsByDistance(zone); T.Check(!ReferenceEquals(order, grown), "the cell order was not rebuilt after AddCell"); CheckSorted(zone, grown, "5x3"); T.Equal(new IntVec3(a.minX + 2, 0, a.minZ + 1), grown[0], "5x3 first cell"); IntVec3 removed = grown[0]; zone.RemoveCell(removed); List shrunk = comp.CellsByDistance(zone); T.Check(!ReferenceEquals(grown, shrunk), "the cell order was not rebuilt after RemoveCell"); T.Check(!shrunk.Contains(removed), "the removed cell is still in the order"); CheckSorted(zone, shrunk, "5x3 minus the center"); // Four cells tie at distance 1 around the removed center; ties go to the lower x. T.Equal(new IntVec3(a.minX + 1, 0, a.minZ + 1), shrunk[0], "first cell after removing the center"); } [BlaxTest("router: pass 1 takes an empty cell first, pass 2 a cell with a mergeable stack")] public static void Pass2Mergeable() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); CellRect a = Fixtures.TestArea(3, 1); Zone_Delivery zone = Fixtures.DeliveryZone(a); IntVec3 west = new IntVec3(a.minX, 0, a.minZ); IntVec3 middle = new IntVec3(a.minX + 1, 0, a.minZ); IntVec3 east = new IntVec3(a.minX + 2, 0, a.minZ); T.Equal(middle, Fixtures.Comp.CellsByDistance(zone)[0], "first cell of the 1x3 zone"); Thing lying = SpawnStack(ThingDefOf.Steel, 10, middle); SpawnStack(ThingDefOf.WoodLog, 10, west); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 30); // The middle cell comes first in the order but holds steel: the empty east cell wins. T.Check(DeliveryRouter.TryPickCellInZone(zone, steel, out IntVec3 cell), "no cell with one empty cell left"); T.Equal(east, cell, "pass 1 cell"); // No empty cell left: pass 2 takes the cell whose steel the stack merges into, never the wood. SpawnStack(ThingDefOf.WoodLog, 10, east); T.Check(DeliveryRouter.TryPickCellInZone(zone, steel, out cell), "pass 2 found no cell with a mergeable stack"); T.Equal(middle, cell, "pass 2 cell"); T.Check(DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out cell), "router found no cell with a mergeable stack"); T.Equal(middle, cell, "router cell in pass 2"); // A full stack has no room left: no cell in the zone. lying.stackCount = lying.def.stackLimit; T.Check(!DeliveryRouter.TryPickCellInZone(zone, steel, out cell), "pass 2 took a full stack at " + cell); T.Check(!cell.IsValid, "failed pick set a cell"); } [BlaxTest("router: a zone checks at most 400 cells per stack")] public static void CheckBudget() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); CellRect a = Fixtures.TestArea(21, 20); Zone_Delivery zone = Fixtures.DeliveryZone(a); List order = Fixtures.Comp.CellsByDistance(zone); T.Equal(420, order.Count, "zone cells"); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); // Roof the 400 cells nearest the centroid: the free 20 lie past the budget. for (int i = 0; i < DeliveryRouter.MaxChecksPerStack; i++) map.roofGrid.SetRoof(order[i], RoofDefOf.RoofConstructed); T.Check(!DeliveryRouter.TryPickCellInZone(zone, steel, out IntVec3 cell), "checked past the budget and found " + cell); // Free the last cell inside the budget: found. IntVec3 last = order[DeliveryRouter.MaxChecksPerStack - 1]; map.roofGrid.SetRoof(last, null); T.Check(DeliveryRouter.TryPickCellInZone(zone, steel, out cell), "missed the free cell at check 400"); T.Equal(last, cell, "cell at check 400"); } // ---- overflow ------------------------------------------------------------------------- [BlaxTest("router: a 1-cell zone with its cell taken overflows within the radius")] public static void OneCellOverflow() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); CellRect a = Fixtures.TestArea(1, 1); IntVec3 only = a.CenterCell; Zone_Delivery zone = Fixtures.DeliveryZone(a); T.Check(ZonesApi.TryFindDropSpot(map, Fixtures.Stack(ThingDefOf.Steel, 10), out IntVec3 cell), "ZonesApi refused steel"); T.Equal(only, cell, "cell of the free 1-cell zone"); DropPodAt(only, Fixtures.Stack(ThingDefOf.Silver, 100)); T.Check(!DeliveryRouter.TryPickCellInZone(zone, Fixtures.Stack(ThingDefOf.Steel, 10), out _), "the cell under a falling pod was picked"); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); T.Check(DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out cell), "no overflow cell next to a taken 1-cell zone"); T.Check(cell != only, "overflow returned the taken cell"); T.Check(WithinRadius(only, cell, DeliveryRouter.OverflowRadius), "overflow cell " + cell + " is farther than " + DeliveryRouter.OverflowRadius + " from " + only); T.Check(DropCellFinder.IsGoodDropSpot(cell, map, allowFogged: false, canRoofPunch: false), "overflow cell " + cell + " is no good drop spot"); T.Check(ZonesApi.TryFindDropSpot(map, steel, out cell) && WithinRadius(only, cell, DeliveryRouter.OverflowRadius), "ZonesApi did not overflow"); Fixtures.Settings.overflowNearZone = false; T.Check(!DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out cell), "overflowed to " + cell + " with the setting off"); T.Check(!ZonesApi.TryFindDropSpot(map, steel, out cell), "ZonesApi overflowed to " + cell + " with the setting off"); } [BlaxTest("router: an L-shaped zone whose centroid is a wall still overflows")] public static void LShapedOverflow() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); CellRect a = Fixtures.TestArea(7, 7); // Bottom row and left column of the 7x7 area. Zone_Delivery zone = Fixtures.DeliveryZone(new CellRect(a.minX, a.minZ, 7, 1)); for (int z = a.minZ + 1; z <= a.maxZ; z++) zone.AddCell(new IntVec3(a.minX, 0, z)); T.Equal(13, zone.CellCount, "L-shaped zone cells"); double cx = zone.cells.Average(c => (double)c.x), cz = zone.cells.Average(c => (double)c.z); IntVec3 centroid = new IntVec3((int)Math.Round(cx), 0, (int)Math.Round(cz)); T.Check(!zone.cells.Contains(centroid), "the centroid cell " + centroid + " is a zone cell"); Fixtures.Wall(centroid); T.Check(centroid.GetEdifice(map) != null, "no wall on the centroid cell"); IntVec3 center = DeliveryRouter.OverflowCenter(zone); T.Check(zone.cells.Contains(center), "OverflowCenter " + center + " is not a zone cell"); double best = zone.cells.Min(c => DistSq(c, cx, cz)); T.Check(Math.Abs(DistSq(center, cx, cz) - best) < 1e-9, "OverflowCenter " + center + " is not the zone cell nearest the centroid"); T.Check(DeliveryRouter.TryOverflow(zone, out IntVec3 cell), "no overflow cell next to the L-shaped zone"); T.Check(WithinRadius(center, cell, DeliveryRouter.OverflowRadius), "overflow cell " + cell + " is farther than the radius"); // Every zone cell under a falling pod: the router overflows next to the zone. foreach (IntVec3 c in zone.cells.ToList()) DropPodAt(c, Fixtures.Stack(ThingDefOf.Silver, 10)); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); T.Check(DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out cell), "router found no overflow cell for the full L-shaped zone"); T.Check(!zone.cells.Contains(cell), "overflow returned zone cell " + cell + " under a falling pod"); T.Check(WithinRadius(center, cell, DeliveryRouter.OverflowRadius), "overflow cell " + cell + " is farther than the radius"); } [BlaxTest("router: a walled-off zone never overflows into its own pocket; the pod goes to the vanilla spot")] public static void WalledOffNoOverflow() { Map map = Fixtures.Map; SetAsideOtherDeliveryZones(); if (map.mapPawns.FreeColonistsSpawned.Count == 0) Fixtures.Colonist(); Fixtures.Settings.overflowNearZone = true; Fixtures.Settings.overflowRadius = BlaxZonesSettings.DefaultOverflowRadius; CellRect a = Fixtures.TestArea(3, 3); Zone_Delivery zone = Fixtures.DeliveryZone(a); 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 center = DeliveryRouter.OverflowCenter(zone); T.Check(DropCellFinder.IsGoodDropSpot(center, map, allowFogged: false, canRoofPunch: false), "setup: IsGoodDropSpot refuses the walled-in center, so this would not test the reach rule"); T.Check(!DeliveryRules.ColonyCanReach(map, center), "setup: the reach rule says a colonist reaches a closed wall ring"); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); T.Check(!DeliveryRouter.TryPickCellInZone(zone, steel, out IntVec3 cell), "picked the unreachable zone cell " + cell); T.Check(!DeliveryRouter.TryOverflow(zone, out cell), "overflow found " + cell + (zone.ContainsCell(cell) ? ", a zone cell" : "") + ", reach rule " + DeliveryRules.ColonyCanReach(map, cell)); T.Check(!cell.IsValid, "failed overflow set a cell"); T.Check(!DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out cell), "the router sent the pod to " + cell + " for a walled-off zone"); T.Check(!ZonesApi.TryFindDropSpot(map, steel, out cell), "ZonesApi returned " + cell + " for a walled-off zone"); } // ---- failure path --------------------------------------------------------------------- [BlaxTest("router: RouterThrows makes the router throw and ZonesApi returns false with one logged error")] public static void RouterThrowsApiFalse() { Map map = Fixtures.Map; ZonesLog.ResetOnce(); Fixtures.OnCleanup(ZonesLog.ResetOnce); SetAsideOtherDeliveryZones(); Fixtures.DeliveryZone(Fixtures.TestArea(2, 2)); Thing steel = Fixtures.Stack(ThingDefOf.Steel, 10); T.Check(ZonesApi.TryFindDropSpot(map, steel, out _), "ZonesApi refused steel before the hook"); ZonesTestHooks.RouterThrows = true; bool threw = false; try { DeliveryRouter.TryFindDropSpot(map, steel, DeliverySource.Orbital, out _); } catch (InvalidOperationException) { threw = true; } T.Check(threw, "DeliveryRouter.TryFindDropSpot did not throw with RouterThrows set"); T.ExpectErrors("ZonesApi.TryFindDropSpot failed", 1); T.Check(!ZonesApi.TryFindDropSpot(map, steel, out IntVec3 cell), "ZonesApi returned true while the router throws"); T.Check(!cell.IsValid, "ZonesApi set a cell while the router throws"); T.Check(!ZonesApi.TryFindDropSpot(map, steel, out cell), "ZonesApi returned true on the second call"); } // ---- helpers ---------------------------------------------------------------------------- /// /// Deregisters every delivery zone already on the map (zones earlier tests kept for phase 2) and /// registers them again in cleanup, so only this test's zones are candidates. Their cells stay in /// the zone grid, so TestArea keeps away from them. /// private static void SetAsideOtherDeliveryZones() { Map map = Fixtures.Map; List others = map.zoneManager.AllZones.OfType().ToList(); if (others.Count == 0) return; Fixtures.OnCleanup(() => { foreach (Zone_Delivery z in others) { if (!map.zoneManager.AllZones.Contains(z)) map.zoneManager.RegisterZone(z); } }); foreach (Zone_Delivery z in others) map.zoneManager.DeregisterZone(z); T.Info("set aside " + others.Count + " delivery zone(s) of earlier tests"); } /// Spawns a drop pod holding the thing at the cell, as ZonesApi callers do, and tracks the falling pod. private static Skyfaller DropPodAt(IntVec3 cell, Thing thing) { Map map = Fixtures.Map; var before = new HashSet(cell.GetThingList(map)); TradeUtility.SpawnDropPod(cell, map, thing); Skyfaller pod = cell.GetThingList(map).OfType().FirstOrDefault(s => !before.Contains(s)); T.NotNull(pod, "falling drop pod at " + cell); T.Check(!DropCellFinder.IsGoodDropSpot(cell, map, allowFogged: false, canRoofPunch: false), "a falling pod left " + cell + " a good drop spot"); return Fixtures.Track(pod); } private static Thing SpawnStack(ThingDef def, int count, IntVec3 cell) { Thing t = Fixtures.Stack(def, count); GenSpawn.Spawn(t, cell, Fixtures.Map); T.Check(t.Spawned && t.Position == cell, def.defName + " stack not spawned at " + cell); return t; } /// TryFindDropSpotNear searches squares (CellFinder.TryFindRandomCellNear), so the radius is per axis. private static bool WithinRadius(IntVec3 center, IntVec3 cell, int radius) => cell.IsValid && Math.Abs(cell.x - center.x) <= radius && Math.Abs(cell.z - center.z) <= radius; private static double DistSq(IntVec3 c, double cx, double cz) => (c.x - cx) * (c.x - cx) + (c.z - cz) * (c.z - cz); /// The order holds exactly the zone's cells, sorted by distance to their centroid. private static void CheckSorted(Zone_Delivery zone, List order, string what) { T.Equal(zone.cells.Count, order.Count, what + ": cells in the order"); T.Check(new HashSet(order).SetEquals(zone.cells), what + ": the order is not the zone's cells"); double cx = zone.cells.Average(c => (double)c.x), cz = zone.cells.Average(c => (double)c.z); for (int i = 1; i < order.Count; i++) T.Check(DistSq(order[i - 1], cx, cz) <= DistSq(order[i], cx, cz) + 1e-9, what + ": " + order[i - 1] + " before " + order[i] + " is out of order"); } private static string Ids(params Zone_Delivery[] zones) => string.Join(",", zones.Select(z => z.ID)); } }