using System.Collections.Generic;
using Verse;
namespace BlaxZones
{
/// Package Router: each delivery zone's cells sorted by distance to its centroid, rebuilt when its cells change.
public partial class MapComponent_BlaxZones
{
/// Cell order per delivery zone of this map. A cache: never saved, dropped on any cell change.
private readonly Dictionary> cellsByDistance = new Dictionary>();
///
/// 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.
///
public List CellsByDistance(Zone_Delivery zone)
{
if (zone == null)
return new List();
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 cached) && cached.Count == zone.cells.Count)
return cached;
List sorted = DeliveryRouter.SortedByCentroid(zone.cells);
cellsByDistance[zone] = sorted;
return sorted;
}
/// Drops the zone's cell order (its cells changed or it was deregistered).
private void DropRouterCaches(Zone_Delivery zone)
{
if (zone != null)
cellsByDistance.Remove(zone);
}
/// Drops every Router cache (FinalizeInit, MapRemoved).
private void ClearRouterCaches()
{
cellsByDistance.Clear();
}
}
}