using System.Collections.Generic; using RimWorld; using Verse; using Verse.AI; namespace BlaxZones { /// /// Package Rules. Design: "DeliveryRouter, shared by every delivery path" > Reach rule and Pod rule, /// and 1.0 "Delivery zone" > cell validity and reachability. Which cells a drop pod may use. /// public static class DeliveryRules { /// The router recomputes a cached reach entry older than this. public const int ReachMaxAgeTicks = 2500; /// While a delivery zone is selected, its unusable cells are recomputed this often. public const int SelectedRecheckTicks = 250; /// /// Reach rule: a spawned free colonist (map.mapPawns.FreeColonistsSpawned) can reach the cell /// with the given PathEndMode (OnCell for pods and placement, Touch for storage). Checked once /// per district and cached in the component (MapComponent_BlaxZones.Rules.cs). With no free /// colonist on the map, Reachability.CanReachColony decides. Never CanReachColony first: it is /// true for any cell that reaches a colony building (Reachability.cs:387-435). /// public static bool ColonyCanReach(Map map, IntVec3 cell, PathEndMode mode = PathEndMode.OnCell) { if (map == null || !cell.InBounds(map)) return false; MapComponent_BlaxZones comp = MapComponent_BlaxZones.Of(map); // Impassable cells have no district (Touch on a wall fridge): checked every time, never cached. District district = cell.GetDistrict(map); if (comp != null && district != null && comp.TryGetCachedReach(district, mode, out bool cached)) return cached; bool canReach = ComputeReach(map, cell, mode); if (comp != null && district != null) comp.StoreReach(district, mode, canReach); return canReach; } /// The reach rule without the cache. internal static bool ComputeReach(Map map, IntVec3 cell, PathEndMode mode) { // FreeColonistsSpawned refills one shared list on every call (MapPawns.cs:809), so read it once. List colonists = map.mapPawns.FreeColonistsSpawned; if (colonists.Count == 0) return map.reachability.CanReachColony(cell); for (int i = 0; i < colonists.Count; i++) { Pawn p = colonists[i]; if (p.Spawned && p.Map == map && map.reachability.CanReach(p.Position, cell, mode, TraverseParms.For(p, Danger.Deadly))) return true; } return false; } /// /// Pod rule: DropCellFinder.IsGoodDropSpot(c, map, allowFogged: false, canRoofPunch: false) /// (DropCellFinder.cs:319-362) plus the reach rule with OnCell. /// public static bool PodCellOk(Map map, IntVec3 cell) { return map != null && cell.InBounds(map) && DropCellFinder.IsGoodDropSpot(cell, map, allowFogged: false, canRoofPunch: false) && ColonyCanReach(map, cell, PathEndMode.OnCell); } } }