rimworld-blax-zones/Source/BlaxZones/DeliveryRules.cs

83 lines
3.9 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using RimWorld;
using Verse;
using Verse.AI;
namespace BlaxZones
{
/// <summary>
/// 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.
/// </summary>
public static class DeliveryRules
{
/// <summary>The router recomputes a cached reach entry older than this.</summary>
public const int ReachMaxAgeTicks = 2500;
/// <summary>While a delivery zone is selected, its unusable cells are recomputed this often.</summary>
public const int SelectedRecheckTicks = 250;
/// <summary>
/// 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).
/// </summary>
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;
}
/// <summary>The reach rule without the cache.</summary>
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<Pawn> 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;
}
/// <summary>
/// Pod rule: DropCellFinder.IsGoodDropSpot(c, map, allowFogged: false, canRoofPunch: false)
/// (DropCellFinder.cs:319-362) plus the reach rule with OnCell.
/// </summary>
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);
}
/// <summary>
/// Placement rule, for goods that appear instead of falling in a pod (caravan purchases from 1.1,
/// direct delivery from 1.4): standable, unfogged, the reach rule with OnCell. Roofs don't matter.
/// GenPlace still applies its own checks (items per cell, storage that refuses the thing).
/// </summary>
public static bool PlacementCellOk(Map map, IntVec3 cell)
{
return map != null && cell.InBounds(map)
&& cell.Standable(map)
&& !cell.Fogged(map)
&& ColonyCanReach(map, cell, PathEndMode.OnCell);
}
}
}