Advertisement
Mehni

FoodUtility.BestFoodSourceOnMapILSpy.cs

Dec 17th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.03 KB | None | 0 0
  1. // FoodUtility
  2. using System;
  3. using System.Collections.Generic;
  4. using Verse;
  5. using Verse.AI;
  6.  
  7. public static Thing BestFoodSourceOnMap(Pawn getter, Pawn eater, bool desperate, out ThingDef foodDef, FoodPreferability maxPref = FoodPreferability.MealLavish, bool allowPlant = true, bool allowDrug = true, bool allowCorpse = true, bool allowDispenserFull = true, bool allowDispenserEmpty = true, bool allowForbidden = false, bool allowSociallyImproper = false, bool allowHarvest = false, bool forceScanWholeMap = false)
  8. {
  9.     foodDef = null;
  10.     bool getterCanManipulate = getter.RaceProps.ToolUser && getter.health.capacities.CapableOf(PawnCapacityDefOf.Manipulation);
  11.     if (!getterCanManipulate && getter != eater)
  12.     {
  13.         Log.Error(getter + " tried to find food to bring to " + eater + " but " + getter + " is incapable of Manipulation.");
  14.         return null;
  15.     }
  16.     FoodPreferability minPref;
  17.     if (eater.NonHumanlikeOrWildMan())
  18.     {
  19.         minPref = FoodPreferability.NeverForNutrition;
  20.     }
  21.     else if (desperate)
  22.     {
  23.         minPref = FoodPreferability.DesperateOnly;
  24.     }
  25.     else
  26.     {
  27.         minPref = (((int)eater.needs.food.CurCategory < 2) ? FoodPreferability.MealAwful : FoodPreferability.RawBad);
  28.     }
  29.     Predicate<Thing> foodValidator = delegate(Thing t)
  30.     {
  31.         Building_NutrientPasteDispenser building_NutrientPasteDispenser = t as Building_NutrientPasteDispenser;
  32.         if (building_NutrientPasteDispenser != null)
  33.         {
  34.             if (!allowDispenserFull || !getterCanManipulate || (int)ThingDefOf.MealNutrientPaste.ingestible.preferability < (int)minPref || (int)ThingDefOf.MealNutrientPaste.ingestible.preferability > (int)maxPref || !eater.WillEat(ThingDefOf.MealNutrientPaste, getter) || (t.Faction != getter.Faction && t.Faction != getter.HostFaction) || (!allowForbidden && t.IsForbidden(getter)) || !building_NutrientPasteDispenser.powerComp.PowerOn || (!allowDispenserEmpty && !building_NutrientPasteDispenser.HasEnoughFeedstockInHoppers()) || !t.InteractionCell.Standable(t.Map) || !IsFoodSourceOnMapSociallyProper(t, getter, eater, allowSociallyImproper) || getter.IsWildMan() || !getter.Map.reachability.CanReachNonLocal(getter.Position, new TargetInfo(t.InteractionCell, t.Map), PathEndMode.OnCell, TraverseParms.For(getter, Danger.Some)))
  35.             {
  36.                 return false;
  37.             }
  38.         }
  39.         else if ((int)t.def.ingestible.preferability < (int)minPref || (int)t.def.ingestible.preferability > (int)maxPref || !eater.WillEat(t, getter) || !t.def.IsNutritionGivingIngestible || !t.IngestibleNow || (!allowCorpse && t is Corpse) || (!allowDrug && t.def.IsDrug) || (!allowForbidden && t.IsForbidden(getter)) || (!desperate && t.IsNotFresh()) || t.IsDessicated() || !IsFoodSourceOnMapSociallyProper(t, getter, eater, allowSociallyImproper) || (!getter.AnimalAwareOf(t) && !forceScanWholeMap) || !getter.CanReserve(t))
  40.         {
  41.             return false;
  42.         }
  43.         return true;
  44.     };
  45.     ThingRequest thingRequest = ((eater.RaceProps.foodType & (FoodTypeFlags.Plant | FoodTypeFlags.Tree)) == FoodTypeFlags.None || !allowPlant) ? ThingRequest.ForGroup(ThingRequestGroup.FoodSourceNotPlantOrTree) : ThingRequest.ForGroup(ThingRequestGroup.FoodSource);
  46.     Thing bestThing;
  47.     if (getter.RaceProps.Humanlike)
  48.     {
  49.         Pawn eater2 = eater;
  50.         IntVec3 position = getter.Position;
  51.         List<Thing> searchSet = getter.Map.listerThings.ThingsMatching(thingRequest);
  52.         PathEndMode peMode = PathEndMode.ClosestTouch;
  53.         TraverseParms traverseParams = TraverseParms.For(getter);
  54.         Predicate<Thing> validator = foodValidator;
  55.         bestThing = SpawnedFoodSearchInnerScan(eater2, position, searchSet, peMode, traverseParams, 9999f, validator);
  56.         if (allowHarvest && getterCanManipulate)
  57.         {
  58.             Thing thing = GenClosest.ClosestThingReachable(searchRegionsMax: (!forceScanWholeMap || bestThing != null) ? 30 : (-1), root: getter.Position, map: getter.Map, thingReq: ThingRequest.ForGroup(ThingRequestGroup.HarvestablePlant), peMode: PathEndMode.Touch, traverseParams: TraverseParms.For(getter), maxDistance: 9999f, validator: delegate(Thing x)
  59.             {
  60.                 Plant plant = (Plant)x;
  61.                 if (!plant.HarvestableNow)
  62.                 {
  63.                     return false;
  64.                 }
  65.                 ThingDef harvestedThingDef = plant.def.plant.harvestedThingDef;
  66.                 if (!harvestedThingDef.IsNutritionGivingIngestible)
  67.                 {
  68.                     return false;
  69.                 }
  70.                 if (!eater.WillEat(harvestedThingDef, getter))
  71.                 {
  72.                     return false;
  73.                 }
  74.                 if (!getter.CanReserve(plant))
  75.                 {
  76.                     return false;
  77.                 }
  78.                 if (!allowForbidden && plant.IsForbidden(getter))
  79.                 {
  80.                     return false;
  81.                 }
  82.                 if (bestThing != null && (int)GetFinalIngestibleDef(bestThing).ingestible.preferability >= (int)harvestedThingDef.ingestible.preferability)
  83.                 {
  84.                     return false;
  85.                 }
  86.                 return true;
  87.             });
  88.             if (thing != null)
  89.             {
  90.                 bestThing = thing;
  91.                 foodDef = GetFinalIngestibleDef(thing, harvest: true);
  92.             }
  93.         }
  94.         if (foodDef == null && bestThing != null)
  95.         {
  96.             foodDef = GetFinalIngestibleDef(bestThing);
  97.         }
  98.     }
  99.     else
  100.     {
  101.         int maxRegionsToScan = GetMaxRegionsToScan(getter, forceScanWholeMap);
  102.         filtered.Clear();
  103.         foreach (Thing item in GenRadial.RadialDistinctThingsAround(getter.Position, getter.Map, 2f, useCenter: true))
  104.         {
  105.             Pawn pawn = item as Pawn;
  106.             if (pawn != null && pawn != getter && pawn.RaceProps.Animal && pawn.CurJob != null && pawn.CurJob.def == JobDefOf.Ingest && pawn.CurJob.GetTarget(TargetIndex.A).HasThing)
  107.             {
  108.                 filtered.Add(pawn.CurJob.GetTarget(TargetIndex.A).Thing);
  109.             }
  110.         }
  111.         bool flag = !allowForbidden && ForbidUtility.CaresAboutForbidden(getter, cellTarget: true) && getter.playerSettings != null && getter.playerSettings.EffectiveAreaRestrictionInPawnCurrentMap != null;
  112.         Predicate<Thing> predicate = delegate(Thing t)
  113.         {
  114.             if (!foodValidator(t))
  115.             {
  116.                 return false;
  117.             }
  118.             if (filtered.Contains(t))
  119.             {
  120.                 return false;
  121.             }
  122.             if (!(t is Building_NutrientPasteDispenser) && (int)t.def.ingestible.preferability <= 2)
  123.             {
  124.                 return false;
  125.             }
  126.             if (t.IsNotFresh())
  127.             {
  128.                 return false;
  129.             }
  130.             return true;
  131.         };
  132.         IntVec3 position = getter.Position;
  133.         Map map = getter.Map;
  134.         ThingRequest thingReq = thingRequest;
  135.         PathEndMode peMode = PathEndMode.ClosestTouch;
  136.         TraverseParms traverseParams = TraverseParms.For(getter);
  137.         Predicate<Thing> validator = predicate;
  138.         bool ignoreEntirelyForbiddenRegions = flag;
  139.         bestThing = GenClosest.ClosestThingReachable(position, map, thingReq, peMode, traverseParams, 9999f, validator, null, 0, maxRegionsToScan, forceGlobalSearch: false, RegionType.Set_Passable, ignoreEntirelyForbiddenRegions);
  140.         filtered.Clear();
  141.         if (bestThing == null)
  142.         {
  143.             desperate = true;
  144.             position = getter.Position;
  145.             map = getter.Map;
  146.             thingReq = thingRequest;
  147.             peMode = PathEndMode.ClosestTouch;
  148.             traverseParams = TraverseParms.For(getter);
  149.             validator = foodValidator;
  150.             ignoreEntirelyForbiddenRegions = flag;
  151.             bestThing = GenClosest.ClosestThingReachable(position, map, thingReq, peMode, traverseParams, 9999f, validator, null, 0, maxRegionsToScan, forceGlobalSearch: false, RegionType.Set_Passable, ignoreEntirelyForbiddenRegions);
  152.         }
  153.         if (bestThing != null)
  154.         {
  155.             foodDef = GetFinalIngestibleDef(bestThing);
  156.         }
  157.     }
  158.     return bestThing;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement