using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using Verse;
namespace BlaxZones
{
///
/// Package Camp. Design: 1.0 "Trader caravans camp at a zone". Set while
/// IncidentWorker_TraderCaravanArrival.TryExecuteWorker runs (tribute collectors inherit it), so the
/// RCellFinder prefix knows the spot it picks becomes a trader caravan's chill spot.
///
public static class CampScope
{
/// Within 6 cells of the camp zone.
public const int Radius = 6;
[ThreadStatic] public static bool Active;
///
/// A camp spot next to a delivery zone with campHere ticked: standable, unfogged, unroofed, within
/// Radius of the zone, passing the caller's extraValidator, cells outside enclosed rooms first.
/// False (vanilla runs) when no zone is ticked, the setting is off, a trading-spot mod is active
/// (Compat.ActiveTradingSpotMod() != null) or no cell passes.
///
/// Zones are tried by priority (highest first), then zone ID, the router's order. Within a zone the
/// cells come in four groups, each in random order: outside enclosed rooms and off delivery zones,
/// outside enclosed rooms on a delivery zone, then the same two inside enclosed rooms. Off-zone
/// cells come first so the pack animals stand beside the landing pad, not on it. Door cells are
/// never used. A null extraValidator accepts every cell.
///
public static bool TryFindCampSpot(Map map, Predicate extraValidator, out IntVec3 cell)
{
cell = IntVec3.Invalid;
if (map == null || !BlaxZonesMod.S.campAtZones)
return false;
// Zones was removed from this game (removal button): vanilla only.
if (MapComponent_BlaxZones.Of(map) == null || map.zoneManager == null)
return false;
List zones = CampZones(map);
if (zones.Count == 0)
return false;
string standDown = Compat.ActiveTradingSpotMod();
if (standDown != null)
{
ZonesLog.MessageOnce("camp-standdown:" + standDown, StandDownMessage(standDown));
return false;
}
foreach (Zone_Delivery zone in zones)
{
if (TryFindSpotNear(map, zone, extraValidator, out cell))
return true;
}
cell = IntVec3.Invalid;
return false;
}
/// The one-time log line when camping stands down for a trading-spot mod (without the "[Blax Zones] " prefix).
public static string StandDownMessage(string packageId) =>
"Caravan camps are off while " + packageId + " is active, because that mod picks where trader caravans gather.";
/// Delivery zones with "Caravans camp here" ticked, by priority (highest first), then zone ID.
public static List CampZones(Map map) =>
map.zoneManager.AllZones.OfType()
.Where(z => z.campHere && z.CellCount > 0)
.OrderByDescending(z => z.priority).ThenBy(z => z.ID)
.ToList();
/// Within Radius (straight-line) of one of the zone's cells.
public static bool WithinRadius(Zone zone, IntVec3 c)
{
foreach (IntVec3 z in zone.Cells)
{
if (c.InHorDistOf(z, Radius))
return true;
}
return false;
}
private static bool TryFindSpotNear(Map map, Zone_Delivery zone, Predicate extraValidator, out IntVec3 cell)
{
var groups = new List[4];
for (int g = 0; g < groups.Length; g++)
groups[g] = new List();
var seen = new HashSet();
int radial = GenRadial.NumCellsInRadius(Radius);
foreach (IntVec3 z in zone.Cells)
{
Consider(map, z, seen, groups);
// The zone cell nearest to any outside cell always has a neighbour outside the zone, so
// spreading from edge cells covers every cell within Radius.
if (!IsEdge(map, zone, z))
continue;
for (int i = 0; i < radial; i++)
Consider(map, z + GenRadial.RadialPattern[i], seen, groups);
}
foreach (List group in groups)
{
group.Shuffle();
foreach (IntVec3 c in group)
{
if (extraValidator == null || extraValidator(c))
{
cell = c;
return true;
}
}
}
cell = IntVec3.Invalid;
return false;
}
private static void Consider(Map map, IntVec3 c, HashSet seen, List[] groups)
{
if (!seen.Add(c) || !c.InBounds(map))
return;
if (!c.Standable(map) || c.Fogged(map) || c.Roofed(map) || c.GetDoor(map) != null)
return;
Room room = c.GetRoom(map);
int group = room != null && !room.TouchesMapEdge ? 2 : 0;
if (map.zoneManager.ZoneAt(c) is Zone_Delivery)
group++;
groups[group].Add(c);
}
private static bool IsEdge(Map map, Zone zone, IntVec3 c)
{
for (int i = 0; i < 4; i++)
{
IntVec3 n = c + GenAdj.CardinalDirections[i];
if (!n.InBounds(map) || map.zoneManager.ZoneAt(n) != zone)
return true;
}
return false;
}
}
}