rimworld-blax-zones/Source/BlaxZones/MapComponent_BlaxZones.Rout...

45 lines
1.9 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
using Verse;
namespace BlaxZones
{
/// <summary>Package Router: each delivery zone's cells sorted by distance to its centroid, rebuilt when its cells change.</summary>
public partial class MapComponent_BlaxZones
{
/// <summary>Cell order per delivery zone of this map. A cache: never saved, dropped on any cell change.</summary>
private readonly Dictionary<Zone_Delivery, List<IntVec3>> cellsByDistance = new Dictionary<Zone_Delivery, List<IntVec3>>();
/// <summary>
/// The zone's cells, nearest its centroid first (ties by x, then z). Cached; rebuilt after
/// AddCell/RemoveCell. The list is shared: read it, never change it. A zone of another map, or
/// one not registered yet, gets a fresh list that is not cached.
/// </summary>
public List<IntVec3> CellsByDistance(Zone_Delivery zone)
{
if (zone == null)
return new List<IntVec3>();
if (zone.MapOrNull != map)
return DeliveryRouter.SortedByCentroid(zone.cells);
// The count check is a backstop for a cell change that bypassed Notify_ZoneCellsChanged.
if (cellsByDistance.TryGetValue(zone, out List<IntVec3> cached) && cached.Count == zone.cells.Count)
return cached;
List<IntVec3> sorted = DeliveryRouter.SortedByCentroid(zone.cells);
cellsByDistance[zone] = sorted;
return sorted;
}
/// <summary>Drops the zone's cell order (its cells changed or it was deregistered).</summary>
private void DropRouterCaches(Zone_Delivery zone)
{
if (zone != null)
cellsByDistance.Remove(zone);
}
/// <summary>Drops every Router cache (FinalizeInit, MapRemoved).</summary>
private void ClearRouterCaches()
{
cellsByDistance.Clear();
}
}
}