using System;
using System.Collections.Generic;
using RimWorld;
using Verse;
using Verse.AI;
namespace BlaxZones
{
///
/// Package Router. Design: "DeliveryRouter, shared by every delivery path". One static class every
/// delivery path calls: orbital trade (DeliveryRouter.Orbital.cs, package Orbital), ZonesApi, and
/// from 1.1 rewards, caravans, cargo pods and direct delivery.
///
/// It holds no state: cell orders live in MapComponent_BlaxZones (MapComponent_BlaxZones.Router.cs),
/// cell validity in DeliveryRules. Nothing here catches: every entry point that calls it runs in
/// try/catch and falls back to vanilla.
///
public static partial class DeliveryRouter
{
/// At most this many cell checks per stack.
public const int MaxChecksPerStack = 400;
///
/// The overflow search radius: the setting (5 to 30), never below 5. DropCellFinder.TryFindDropSpotNear
/// grows its search by Min(maxRadius / 5, 1) (DropCellFinder.cs:272), which is 0 below 5, so a
/// failed search with a smaller radius never ends.
///
public static int OverflowRadius => Math.Max(BlaxZonesSettings.MinOverflowRadius, BlaxZonesMod.S.OverflowRadiusClamped);
///
/// A cell for one pod holding this thing, or false. Refusals (Refuses), then candidates in order,
/// then each candidate's cells (pass 1 empty valid cells, pass 2 cells with a mergeable stack, at
/// most MaxChecksPerStack checks), then overflow next to the top candidate when the setting allows.
/// Throws only when ZonesTestHooks.RouterThrows is set; callers wrap it in try/catch and fall back
/// to vanilla.
///
public static bool TryFindDropSpot(Map map, Thing thing, DeliverySource source, out IntVec3 cell)
{
if (ZonesTestHooks.RouterThrows)
throw new System.InvalidOperationException("ZonesTestHooks.RouterThrows");
cell = IntVec3.Invalid;
if (Refuses(map, thing))
return false;
List candidates = Candidates(map, thing, source);
if (candidates.Count == 0)
return false;
for (int i = 0; i < candidates.Count; i++)
{
if (TryPickCellInZone(candidates[i], thing, out cell))
return true;
}
// Every candidate is full: overflow next to the top one (false when the setting is off).
return TryOverflow(candidates[0], out cell);
}
///
/// Never routed: DeliveryFilters.NeverRouted (Pawn, Corpse, Wastepack), a map without
/// MapComponent_BlaxZones (after the removal button), a map with no delivery zones.
///
public static bool Refuses(Map map, Thing thing)
{
if (map == null || DeliveryFilters.NeverRouted(thing))
return true;
if (MapComponent_BlaxZones.Of(map) == null)
return true;
return !HasDeliveryZone(map);
}
///
/// Delivery zones on the map that accept the source and whose filter accepts the thing, ordered
/// by priority (highest first), then zone ID.
///
public static List Candidates(Map map, Thing thing, DeliverySource source)
{
var result = new List();
List zones = map?.zoneManager?.AllZones;
if (zones == null || thing == null)
return result;
for (int i = 0; i < zones.Count; i++)
{
if (zones[i] is Zone_Delivery zone && zone.cells.Count > 0 && zone.AcceptsSource(source) && zone.Accepts(thing))
result.Add(zone);
}
result.Sort(CompareCandidates);
return result;
}
///
/// A pod cell inside one zone: pass 1 empty cells passing DeliveryRules.PodCellOk, pass 2 cells
/// holding a stack the thing can merge into, both in the component's cell order (nearest the
/// centroid first), at most MaxChecksPerStack checks.
///
///
/// One walk over the first MaxChecksPerStack cells serves both passes: an empty cell is checked
/// with the pod rule at once (pass 1), a cell with a mergeable stack is remembered and checked
/// only if no empty cell passed (pass 2). A cell holding other items is taken. So each cell costs
/// one pod-rule check at most.
///
public static bool TryPickCellInZone(Zone_Delivery zone, Thing thing, out IntVec3 cell) =>
TryPickCellInZone(zone, thing, DeliveryRules.PodCellOk, null, out cell);
///
/// TryPickCellInZone with another cell rule (DeliveryRules.PodCellOk for pods, PlacementCellOk for
/// goods that appear) and cells to skip: ones already planned for an earlier group of the same
/// drop, which no falling pod marks yet. A null set skips nothing.
///
public static bool TryPickCellInZone(Zone_Delivery zone, Thing thing, Func