using System; using System.Collections.Generic; using RimWorld; using Verse; namespace BlaxZones { /// /// Package Rewards. Design: 1.1 "Quest rewards land in the zone". The groups of one /// DropPodUtility.DropThingGroupsNear call made inside a DeliveryContext: plan every group's cell /// first, then drop the qualifying groups as their own pods, and hand vanilla the rest. /// public static partial class DeliveryRouter { /// Where one group of a drop goes: the group's index in the caller's list, its cell and zone. public struct GroupPlan { public int Index; public IntVec3 Cell; public Zone_Delivery Zone; /// Every fitting zone was full: the cell lies next to Zone, the top candidate. public bool Overflow; } /// /// A group that may be routed: not empty, and every thing in it is free (not spawned, in no /// container, not destroyed), never routed (no pawn, corpse or Wastepack, DeliveryFilters.NeverRouted) /// and carries no pawn inside it. A refugee or prisoner in the group keeps the whole group, items /// included, with vanilla (dropAllInSamePod puts them in one group, QuestPart_DropPods.cs:209-212). /// public static bool GroupQualifies(List group) { if (group == null || group.Count == 0) return false; for (int i = 0; i < group.Count; i++) { Thing t = group[i]; if (t == null || t.Destroyed || t.Spawned || t.holdingOwner != null) return false; if (DeliveryFilters.NeverRouted(t) || DeliveryFilters.HoldsPawn(t)) return false; } return true; } /// Candidates (source checkbox, filter, priority, ID) whose filter accepts every thing of the group. public static List CandidatesForGroup(Map map, List group, DeliverySource source) { List result = Candidates(map, group[0], source); for (int i = 1; i < group.Count && result.Count > 0; i++) { Thing t = group[i]; result.RemoveAll(z => !z.Accepts(t)); } return result; } /// /// Plans a cell for every qualifying group, nothing dropped yet: the candidates in order, each /// zone's cells by the pod rule (TryPickCellInZone), then overflow next to the top candidate. A cell /// planned for one group is never planned again, since no pod marks it before the drop. Groups with /// no plan stay with vanilla. Throws only when ZonesTestHooks.RouterThrows is set; the caller falls /// back to vanilla with the original list. /// public static List PlanGroups(Map map, List> groups, DeliverySource source) { if (ZonesTestHooks.RouterThrows) throw new InvalidOperationException("ZonesTestHooks.RouterThrows"); var plans = new List(); if (map == null || groups == null || groups.Count == 0) return plans; if (MapComponent_BlaxZones.Of(map) == null || !HasDeliveryZone(map)) return plans; var taken = new HashSet(); for (int i = 0; i < groups.Count; i++) { List group = groups[i]; if (!GroupQualifies(group)) continue; List candidates = CandidatesForGroup(map, group, source); if (candidates.Count == 0) continue; var plan = new GroupPlan { Index = i, Cell = IntVec3.Invalid }; for (int z = 0; z < candidates.Count; z++) { if (TryPickCellInZone(candidates[z], group[0], DeliveryRules.PodCellOk, taken, out IntVec3 cell)) { plan.Cell = cell; plan.Zone = candidates[z]; break; } } if (plan.Zone == null) { if (!TryOverflow(candidates[0], taken, out IntVec3 cell)) continue; plan.Cell = cell; plan.Zone = candidates[0]; plan.Overflow = true; } if (!plan.Cell.IsValid || !plan.Cell.InBounds(map)) continue; taken.Add(plan.Cell); plans.Add(plan); } return plans; } /// /// Drops each planned group as its own pod, as vanilla drops a group (DropPodUtility.cs:64-86): the /// forbid flag per thing, an ActiveTransporterInfo with the call's openDelay and leaveSlag, then /// DropPodUtility.MakeDropPodAt(cell, map, info, faction), so faction pod skins survive. With /// instaDrop the things are placed directly, as vanilla does. /// /// Returns the groups vanilla still has to drop, in their original order, as a new list; null when /// nothing was planned. When a group fails (logged once under logKey), its things that are not on /// the map yet are taken back out of the pod that never fell and go to vanilla, with every later /// group. Groups already dropped are left out, so nothing drops twice and nothing is lost. /// public static List> DropPlannedGroups(Map map, List> groups, List plans, int openDelay, bool instaDrop, bool leaveSlag, bool forbid, Faction faction, string logKey) { if (plans == null || plans.Count == 0) return null; var dropped = new HashSet(); int failedIndex = -1; List failedLeft = null; for (int p = 0; p < plans.Count; p++) { GroupPlan plan = plans[p]; List group = groups[plan.Index]; try { DropGroup(map, group, plan.Cell, openDelay, instaDrop, leaveSlag, forbid, faction, p); dropped.Add(plan.Index); } catch (Exception e) { ZonesLog.Once(logKey, e); failedIndex = plan.Index; failedLeft = NotYetDropped(group, logKey); break; } } var remaining = new List>(groups.Count); for (int i = 0; i < groups.Count; i++) { if (dropped.Contains(i)) continue; if (i == failedIndex) { if (failedLeft != null && failedLeft.Count > 0) remaining.Add(failedLeft); continue; } remaining.Add(groups[i]); } return remaining; } private static void DropGroup(Map map, List group, IntVec3 cell, int openDelay, bool instaDrop, bool leaveSlag, bool forbid, Faction faction, int routedIndex) { if (forbid) { for (int i = 0; i < group.Count; i++) group[i].SetForbidden(value: true, warnOnFail: false); } if (instaDrop) { if (ZonesTestHooks.RoutedGroupThrowsAt == routedIndex) throw new InvalidOperationException("ZonesTestHooks.RoutedGroupThrowsAt"); for (int i = 0; i < group.Count; i++) GenPlace.TryPlaceThing(group[i], cell, map, ThingPlaceMode.Near); return; } var info = new ActiveTransporterInfo(); for (int i = 0; i < group.Count; i++) { // Vanilla ignores a failed TryAdd and loses the thing; here the group goes back to vanilla instead. if (!info.innerContainer.TryAdd(group[i])) throw new InvalidOperationException("could not put " + group[i].ToStringSafe() + " into a drop pod"); } info.openDelay = openDelay; info.leaveSlag = leaveSlag; if (ZonesTestHooks.RoutedGroupThrowsAt == routedIndex) throw new InvalidOperationException("ZonesTestHooks.RoutedGroupThrowsAt"); DropPodUtility.MakeDropPodAt(cell, map, info, faction); } /// /// The group's things vanilla may still drop: not destroyed and not on the map in any form. A thing /// still held by a pod that never fell is taken out first, because a thing in a container can't be /// added to vanilla's pod. /// private static List NotYetDropped(List group, string logKey) { var left = new List(); for (int i = 0; i < group.Count; i++) { Thing t = group[i]; try { if (t == null || t.Destroyed || t.SpawnedOrAnyParentSpawned) continue; t.holdingOwner?.Remove(t); left.Add(t); } catch (Exception e) { ZonesLog.Once(logKey + " fallback", e); } } return left; } } }