static void LookInOutputs(object instance, Region region) { float bestPrio = instance.Get <float>(bestPrioField); float closestDistSquared = instance.Get <float>(closestDistSquaredField); Thing closestThing = instance.Get <Thing>(closestThingField); float maxDistSquared = instance.Get <float>(maxDistSquaredField); Func <Thing, float> priorityGetter = instance.Get <Func <Thing, float> >(priorityGetterField); ThingRequest req = instance.Get <ThingRequest>(reqField); IntVec3 root = instance.Get <IntVec3>(rootField); TraverseParms traverseParams = instance.Get <TraverseParms>(traverseParamsField); Predicate <Thing> validator = instance.Get <Predicate <Thing> >(validatorField); var buildingsInRegion = region.ListerThings.ThingsInGroup(ThingRequestGroup.BuildingArtificial); foreach (var building in buildingsInRegion) { var comp = building.Position.GetStorageComponent <Comp_StorageOutput>(building.Map); if (comp != null && comp.GetStoredThings() != null && ReachabilityWithinRegion.ThingFromRegionListerReachable(building, region, PathEndMode.ClosestTouch, traverseParams.pawn)) { float distSq = (float)(building.Position - root).LengthHorizontalSquared; if (distSq < maxDistSquared && distSq <= closestDistSquared) { foreach (var thing in comp.GetStoredThings()) { if (req.Accepts(thing)) { float priority = (priorityGetter == null) ? 0f : priorityGetter(thing); if (priority >= bestPrio) { if ((priority > bestPrio || distSq < closestDistSquared) && (validator == null || validator(thing))) { closestThing = thing; closestDistSquared = distSq; bestPrio = priority; } } } } } } } instance.Set(bestPrioField, bestPrio); instance.Set(closestDistSquaredField, closestDistSquared); instance.Set(closestThingField, closestThing); }
public static bool GetAllThingsRecursively_Pawn(Map map, ThingRequest request, List <Pawn> outThings, bool allowUnreal = true, Predicate <IThingHolder> passCheck = null, bool alsoGetSpawnedThings = true) { lock (outThings) { outThings.Clear(); } if (alsoGetSpawnedThings) { List <Thing> list = map.listerThings.ThingsMatching(request); for (int i = 0; i < list.Count; i++) { Pawn t = list[i] as Pawn; if (t != null) { lock (outThings) { outThings.Add(t); } } } } List <IThingHolder> tmpMapChildHolders = new List <IThingHolder>(); //ThingOwnerUtility.tmpMapChildHolders.Clear(); map.GetChildHolders(tmpMapChildHolders); for (int j = 0; j < tmpMapChildHolders.Count; j++) { //ThingOwnerUtility.tmpThings.Clear(); List <Thing> tmpThings = new List <Thing>(); ThingOwnerUtility.GetAllThingsRecursively(tmpMapChildHolders[j], tmpThings, allowUnreal, passCheck); for (int k = 0; k < tmpThings.Count; k++) { Pawn t2 = tmpThings[k] as Pawn; if (t2 != null && request.Accepts(t2)) { lock (outThings) { outThings.Add(t2); } } } } return(false); //tmpThings.Clear(); //tmpMapChildHolders.Clear(); }
public static bool GetAllThingsRecursively_Thing(Map map, ThingRequest request, List <Thing> outThings, bool allowUnreal = true, Predicate <IThingHolder> passCheck = null, bool alsoGetSpawnedThings = true) { outThings.Clear(); if (alsoGetSpawnedThings) { List <Thing> list = map.listerThings.ThingsMatching(request); for (int i = 0; i < list.Count; i++) { Thing val = list[i]; if (val != null) { outThings.Add(val); } } } List <IThingHolder> tmpMapChildHolders = new List <IThingHolder>(); //tmpMapChildHolders.Clear(); map.GetChildHolders(tmpMapChildHolders); List <Thing> tmpThings = new List <Thing>(); for (int j = 0; j < tmpMapChildHolders.Count; j++) { tmpThings.Clear(); GetAllThingsRecursively(tmpMapChildHolders[j], tmpThings, allowUnreal, passCheck); for (int k = 0; k < tmpThings.Count; k++) { if (tmpThings[k] is Thing val2 && request.Accepts(val2)) { outThings.Add(val2); } } } //tmpThings.Clear(); //tmpMapChildHolders.Clear(); return(false); }