Exemple #1
0
        public static bool RegisterInCell(ThingGrid __instance, Thing t, IntVec3 c)
        {
            Map this_map = __instance.map;

            if (!c.InBounds(this_map))
            {
                Log.Warning(t.ToString() + " tried to register out of bounds at " + c + ". Destroying.");
                t.Destroy(DestroyMode.Vanish);
            }
            else
            {
                int index = this_map.cellIndices.CellToIndex(c);

                //int mapSizeX = this_map.Size.x;
                //int mapSizeZ = this_map.Size.z;

                lock (__instance)
                {
                    __instance.thingGrid[index].Add(t);
                }
                if (t.def.EverHaulable)
                {
                    HaulingCache.RegisterHaulableItem(t);
                }
                if (t is Building_PlantGrower building_PlantGrower)
                {
                    foreach (IntVec3 plantableLocation in building_PlantGrower.OccupiedRect())
                    {
                        PlantSowing_Cache.ReregisterObject(t.Map, plantableLocation, WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
                    }
                }

                /*
                 * if (!thingBillPoints.TryGetValue(t.def, out Dictionary<WorkGiver_Scanner, float> billPointsDict))
                 * {
                 *  billPointsDict = new Dictionary<WorkGiver_Scanner, float>();
                 *  thingBillPoints[t.def] = billPointsDict;
                 * }
                 * if (!mapIngredientDict.TryGetValue(this_map, out Dictionary<WorkGiver_Scanner, Dictionary<float, List<HashSet<Thing>[]>>> ingredientDict))
                 * {
                 *  ingredientDict = new Dictionary<WorkGiver_Scanner, Dictionary<float, List<HashSet<Thing>[]>>>();
                 *  mapIngredientDict[this_map] = ingredientDict;
                 * }
                 * foreach (KeyValuePair<WorkGiver_Scanner, float> billPoints in billPointsDict)
                 * {
                 *  int i = 0;
                 *  int power2;
                 *  do
                 *  {
                 *      power2 = power2array[i];
                 *      ingredientDict[billPoints.Key][billPoints.Value][i][CellToIndexCustom(c, mapSizeX, power2)].Add(t);
                 *      i++;
                 *  } while (power2 < mapSizeX || power2 < mapSizeZ);
                 * }
                 */
                //}
            }
            return(false);
        }
 public static void SetPlantDefToGrow(Zone_Growing __instance, ThingDef plantDef)
 {
     if (Current.ProgramState == ProgramState.Playing)
     {
         foreach (IntVec3 c in __instance.cells)
         {
             PlantSowing_Cache.ReregisterObject(__instance.Map, c, WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
         }
     }
 }
Exemple #3
0
 public static void AddZoneGridCell(ZoneManager __instance, Zone zone, IntVec3 c)
 {
     if (Current.ProgramState == ProgramState.Playing)
     {
         if (zone is Zone_Growing)
         {
             //Log.Message("Adding growing zone cell to awaiting plant cells");
             PlantSowing_Cache.ReregisterObject(zone.Map, c, WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
         }
     }
 }
Exemple #4
0
 public static void CheckAddHaulDestination(Zone __instance)
 {
     if (Current.ProgramState == ProgramState.Playing)
     {
         if (__instance is Zone_Growing zone)
         {
             //Log.Message("Adding growing zone cell to awaiting plant cells");
             foreach (IntVec3 c in zone.cells)
             {
                 PlantSowing_Cache.ReregisterObject(zone.Map, c, WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
             }
         }
     }
 }
Exemple #5
0
        public static bool DeregisterInCell(ThingGrid __instance, Thing t, IntVec3 c)
        {
            Map this_map = __instance.map;

            if (!c.InBounds(this_map))
            {
                Log.Error(t.ToString() + " tried to de-register out of bounds at " + c);
                return(false);
            }

            int index = this_map.cellIndices.CellToIndex(c);

            List <Thing>[] thingGridInstance = __instance.thingGrid;
            List <Thing>   thingList         = thingGridInstance[index];
            List <Thing>   newThingList      = null;

            if (thingList.Contains(t))
            {
                bool found = false;
                lock (__instance)
                {
                    thingList = thingGridInstance[index];
                    if (thingList.Contains(t))
                    {
                        found        = true;
                        newThingList = new List <Thing>(thingList);
                        newThingList.Remove(t);
                        thingGridInstance[index] = newThingList;
                    }
                }
                if (found)
                {
                    if (t.def.EverHaulable)
                    {
                        HaulingCache.DeregisterHaulableItem(t);
                    }

                    if (c.GetZone(__instance.map) is Zone_Growing zone)
                    {
                        PlantSowing_Cache.ReregisterObject(zone.Map, c, WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
                    }

                    for (int i = newThingList.Count - 1; i >= 0; i--)
                    {
                        Thing thing2 = newThingList[i];
                        if (thing2 is Building_PlantGrower building_PlantGrower)
                        {
                            foreach (IntVec3 plantableLocation in building_PlantGrower.OccupiedRect())
                            {
                                PlantSowing_Cache.ReregisterObject(building_PlantGrower.Map, plantableLocation, WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
                            }
                        }
                    }
                }

                /*
                 * int mapSizeX = this_map.Size.x;
                 * int mapSizeZ = this_map.Size.z;
                 *
                 * if (!thingBillPoints.TryGetValue(t.def, out Dictionary<WorkGiver_Scanner, float> billPointsDict))
                 * {
                 *  billPointsDict = new Dictionary<WorkGiver_Scanner, float>();
                 *  thingBillPoints[t.def] = billPointsDict;
                 * }
                 * if (!mapIngredientDict.TryGetValue(this_map, out Dictionary<WorkGiver_Scanner, Dictionary<float, List<HashSet<Thing>[]>>> ingredientDict))
                 * {
                 *  ingredientDict = new Dictionary<WorkGiver_Scanner, Dictionary<float, List<HashSet<Thing>[]>>>();
                 *  mapIngredientDict[this_map] = ingredientDict;
                 * }
                 * foreach (KeyValuePair<WorkGiver_Scanner, float> billPoints in billPointsDict)
                 * {
                 *  int i = 0;
                 *  int power2;
                 *  do
                 *  {
                 *      power2 = power2array[i];
                 *      HashSet<Thing> newHashSet = new HashSet<Thing>(ingredientDict[billPoints.Key][billPoints.Value][i][CellToIndexCustom(c, mapSizeX, power2)]);
                 *      newHashSet.Remove(t);
                 *      ingredientDict[billPoints.Key][billPoints.Value][i][CellToIndexCustom(c, mapSizeX, power2)] = newHashSet;
                 *      i++;
                 *  } while (power2 < mapSizeX || power2 < mapSizeZ);
                 * }
                 */
                //}
                //}
            }

            return(false);
        }
Exemple #6
0
        private static bool JobOnCellTest(WorkGiverDef def, Pawn pawn, IntVec3 c, bool forced = false)
        {
            Map map = pawn.Map;

            if (c.IsForbidden(pawn))
            {
#if DEBUG
                Log.Warning("IsForbidden");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (!PlantUtility.GrowthSeasonNow(c, map, forSowing: true))
            {
#if DEBUG
                Log.Warning("GrowthSeasonNow");
#endif
                return(false);
            }

            ThingDef localWantedPlantDef = WorkGiver_Grower.CalculateWantedPlantDef(c, map);
            WorkGiver_GrowerSow.wantedPlantDef = localWantedPlantDef;
            if (localWantedPlantDef == null)
            {
#if DEBUG
                Log.Warning("localWantedPlantDef==null");
#endif
                return(false);
            }

            List <Thing> thingList = c.GetThingList(map);
            bool         flag      = false;
            for (int i = 0; i < thingList.Count; i++)
            {
                Thing thing = thingList[i];
                if (thing.def == localWantedPlantDef)
                {
#if DEBUG
                    Log.Warning("thing.def == localWantedPlantDef... RemoveObjectFromAwaitingHaulingHashSets");
#endif
                    PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                    //JumboCellCache.AddObjectToActionableObjects(map, c, c, awaitingPlantCellsMapDict);
                    return(false);
                }

                if ((thing is Blueprint || thing is Frame) && thing.Faction == pawn.Faction)
                {
                    flag = true;
                }
            }

            if (flag)
            {
                Thing edifice = c.GetEdifice(map);
                if (edifice == null || edifice.def.fertility < 0f)
                {
#if DEBUG
                    Log.Warning("fertility");
#endif
                    return(false);
                }
            }

            if (localWantedPlantDef.plant.cavePlant)
            {
                if (!c.Roofed(map))
                {
#if DEBUG
                    Log.Warning("cavePlant");
#endif
                    return(false);
                }

                if (map.glowGrid.GameGlowAt(c, ignoreCavePlants: true) > 0f)
                {
#if DEBUG
                    Log.Warning("GameGlowAt");
#endif
                    return(false);
                }
            }

            if (localWantedPlantDef.plant.interferesWithRoof && c.Roofed(pawn.Map))
            {
                return(false);
            }

            Plant plant = c.GetPlant(map);
            if (plant != null && plant.def.plant.blockAdjacentSow)
            {
                if (!pawn.CanReserve(plant, 1, -1, null, forced) || plant.IsForbidden(pawn))
                {
#if DEBUG
                    Log.Warning("blockAdjacentSow");
#endif
                    return(false);
                }

                return(true);                // JobMaker.MakeJob(JobDefOf.CutPlant, plant);
            }

            Thing thing2 = PlantUtility.AdjacentSowBlocker(localWantedPlantDef, c, map);
            if (thing2 != null)
            {
                Plant plant2 = thing2 as Plant;
                if (plant2 != null && pawn.CanReserve(plant2, 1, -1, null, forced) && !plant2.IsForbidden(pawn))
                {
                    IPlantToGrowSettable plantToGrowSettable = plant2.Position.GetPlantToGrowSettable(plant2.Map);
                    if (plantToGrowSettable == null || plantToGrowSettable.GetPlantDefToGrow() != plant2.def)
                    {
                        return(true);                        // JobMaker.MakeJob(JobDefOf.CutPlant, plant2);
                    }
                }
#if DEBUG
                Log.Warning("AdjacentSowBlocker");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (localWantedPlantDef.plant.sowMinSkill > 0 && pawn.skills != null && pawn.skills.GetSkill(SkillDefOf.Plants).Level < localWantedPlantDef.plant.sowMinSkill)
            {
#if DEBUG
                Log.Warning("UnderAllowedSkill");
#endif
                return(false);
            }

            for (int j = 0; j < thingList.Count; j++)
            {
                Thing thing3 = thingList[j];
                if (!thing3.def.BlocksPlanting())
                {
                    continue;
                }

                if (!pawn.CanReserve(thing3, 1, -1, null, forced))
                {
#if DEBUG
                    Log.Warning("!CanReserve");
#endif
                    PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);

                    return(false);
                }

                if (thing3.def.category == ThingCategory.Plant)
                {
                    if (!thing3.IsForbidden(pawn))
                    {
                        return(true);                        // JobMaker.MakeJob(JobDefOf.CutPlant, thing3);
                    }
#if DEBUG
                    Log.Warning("Plant IsForbidden");
#endif
                    PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);

                    return(false);
                }

                if (thing3.def.EverHaulable)
                {
                    return(true);                    //HaulAIUtility.HaulAsideJobFor(pawn, thing3);
                }
#if DEBUG
                Log.Warning("EverHaulable");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (!localWantedPlantDef.CanEverPlantAt(c, map))
            {
#if DEBUG
                Log.Warning("CanEverPlantAt_NewTemp");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                return(false);
            }

            if (!PlantUtility.GrowthSeasonNow(c, map, forSowing: true))
            {
#if DEBUG
                Log.Warning("GrowthSeasonNow");
#endif
                return(false);
            }

            if (!pawn.CanReserve(c, 1, -1, null, forced))
            {
#if DEBUG
                Log.Warning("!pawn.CanReserve(c");
#endif
                PlantSowing_Cache.ReregisterObject(map, c, awaitingPlantCellsMapDict);
                //JumboCellCache.AddObjectToActionableObjects(map, c, c, awaitingPlantCellsMapDict);
                return(false);
            }

            //Job job = JobMaker.MakeJob(JobDefOf.Sow, c);
            //job.plantDefToSow = wantedPlantDef;
            return(true);            //job;
        }
Exemple #7
0
        internal static IntVec3 ClosestLocationReachable(WorkGiver_Grower workGiver_Grower, Pawn pawn)
        {
            Danger maxDanger = pawn.NormalMaxDanger();
            //wantedPlantDef = null;
            //List<Zone> zonesList = pawn.Map.zoneManager.AllZones;
            //for (int j = 0; j < zonesList.Count; j++)
            //{

            //if (growZone.cells.Count == 0)
            //{
            //Log.ErrorOnce("Grow zone has 0 cells: " + growZone, -563487);
            //}
            bool        forced      = false;
            Map         map         = pawn.Map;
            ZoneManager zoneManager = pawn.Map.zoneManager;

            foreach (IntVec3 actionableLocation in PlantSowing_Cache.GetClosestActionableLocations(pawn, map, awaitingPlantCellsMapDict))
            {
                List <Thing> thingsAtLocation = GridsUtility.GetThingList(actionableLocation, map);
                foreach (Thing thingAtLocation in thingsAtLocation)
                {
                    if (thingAtLocation is Building_PlantGrower building_PlantGrower)
                    {
                        if (building_PlantGrower == null || !workGiver_Grower.ExtraRequirements(building_PlantGrower, pawn) ||
                            building_PlantGrower.IsForbidden(pawn) ||
                            !pawn.CanReach(building_PlantGrower, PathEndMode.OnCell, maxDanger)
                            //|| building_PlantGrower.IsBurning()
                            )
                        {
                            continue;
                        }

                        //foreach (IntVec3 item in building_PlantGrower.OccupiedRect())
                        //{
                        //return item; //TODO ADD check
                        //}
                        return(actionableLocation);
                    }
                }
                if (!(zoneManager.ZoneAt(actionableLocation) is Zone_Growing growZone))
                {
                    continue;
                }
                if (!workGiver_Grower.ExtraRequirements(growZone, pawn))
                {
                    continue;
                }
                if (!JobOnCellTest(workGiver_Grower.def, pawn, actionableLocation, forced))
                {
                    continue;
                }
                //!growZone.ContainsStaticFire &&
                if (!workGiver_Grower.HasJobOnCell(pawn, actionableLocation))
                {
                    continue;
                }
                if (!pawn.CanReach(actionableLocation, PathEndMode.OnCell, maxDanger))
                {
                    continue;
                }
                return(actionableLocation);
            }
            //wantedPlantDef = null;
            return(IntVec3.Invalid);
        }
Exemple #8
0
        public static bool TryIssueJobPackage(JobGiver_Work __instance, ref ThinkResult __result, Pawn pawn, JobIssueParams jobParams)
        {
#if DEBUG
            DateTime startTime = DateTime.Now;
#endif
            if (__instance.emergency && pawn.mindState.priorityWork.IsPrioritized)
            {
                List <WorkGiverDef> workGiversByPriority = pawn.mindState.priorityWork.WorkGiver.workType.workGiversByPriority;
                for (int i = 0; i < workGiversByPriority.Count; i++)
                {
                    WorkGiver worker = workGiversByPriority[i].Worker;
                    if (__instance.WorkGiversRelated(pawn.mindState.priorityWork.WorkGiver, worker.def))
                    {
                        Job job = GiverTryGiveJobPrioritized(__instance, pawn, worker, pawn.mindState.priorityWork.Cell);
                        if (job != null)
                        {
                            job.playerForced = true;
                            __result         = new ThinkResult(job, __instance, workGiversByPriority[i].tagToGive);
                            return(false);
                        }
                    }
                }
                pawn.mindState.priorityWork.Clear();
            }
            List <WorkGiver> list = (!__instance.emergency) ? pawn.workSettings.WorkGiversInOrderNormal : pawn.workSettings.WorkGiversInOrderEmergency;
            int               num = -999;
            TargetInfo        bestTargetOfLastPriority = TargetInfo.Invalid;
            WorkGiver_Scanner scannerWhoProvidedTarget = null;
            WorkGiver_Scanner scanner;
            IntVec3           pawnPosition;
            float             closestDistSquared;
            float             bestPriority;
            bool              prioritized;
            bool              allowUnreachable;
            Danger            maxPathDanger;
            for (int j = 0; j < list.Count; j++)
            {
                WorkGiver workGiver = list[j];
                if (workGiver.def.priorityInType != num && bestTargetOfLastPriority.IsValid)
                {
                    break;
                }
                if (!__instance.PawnCanUseWorkGiver(pawn, workGiver))
                {
                    continue;
                }
                try
                {
                    Job job2 = workGiver.NonScanJob(pawn);
                    if (job2 != null)
                    {
                        __result = new ThinkResult(job2, __instance, workGiver.def.tagToGive);
                        return(false);
                    }
                    scanner = (workGiver as WorkGiver_Scanner);

                    if (scanner != null)
                    {
                        if (scanner.def.scanThings)
                        {
//----------------------THERE HAVE BEEN NO CHANGES ABOVE THIS---------------------------------

                            Predicate <Thing> validator;
                            if (scanner is WorkGiver_DoBill workGiver_DoBill)
                            {
                                validator = (Thing t) => !t.IsForbidden(pawn) && WorkGiver_Scanner_Patch.HasJobOnThing(workGiver_DoBill, pawn, t);
                            }
                            else
                            {
                                validator = (Thing t) => !t.IsForbidden(pawn) && scanner.HasJobOnThing(pawn, t);
                            }
                            IEnumerable <Thing> enumerable = scanner.PotentialWorkThingsGlobal(pawn);
                            Thing thing;
                            if (scanner.Prioritized)
                            {
                                IEnumerable <Thing> enumerable2 = enumerable;
                                if (enumerable2 == null)
                                {
                                    enumerable2 = pawn.Map.listerThings.ThingsMatching(scanner.PotentialWorkThingRequest);
                                }
                                thing = ((!scanner.AllowUnreachable) ?
                                         GenClosest.ClosestThing_Global_Reachable(pawn.Position, pawn.Map, enumerable2, scanner.PathEndMode, TraverseParms.For(pawn, scanner.MaxPathDanger(pawn)), 9999f, validator, (Thing x) => scanner.GetPriority(pawn, x)) :
                                         GenClosest.ClosestThing_Global(pawn.Position, enumerable2, 99999f, validator, (Thing x) => scanner.GetPriority(pawn, x)));
                            }
                            else if (scanner.AllowUnreachable)
                            {
                                IEnumerable <Thing> enumerable3 = enumerable;
                                if (enumerable3 == null)
                                {
                                    enumerable3 = pawn.Map.listerThings.ThingsMatching(scanner.PotentialWorkThingRequest);
                                }
                                thing = GenClosest.ClosestThing_Global(pawn.Position, enumerable3, 99999f, validator);
                            }
                            else
                            {
                                //TODO: use better ThingRequest groups
                                if (
                                    workGiver.def.defName.Equals("DoctorFeedAnimals") ||
                                    workGiver.def.defName.Equals("DoctorFeedHumanlikes") ||
                                    workGiver.def.defName.Equals("DoctorTendToAnimals") ||
                                    workGiver.def.defName.Equals("DoctorTendToHumanlikes") ||
                                    workGiver.def.defName.Equals("DoBillsUseCraftingSpot") ||
                                    workGiver.def.defName.Equals("DoctorTendEmergency") ||
                                    workGiver.def.defName.Equals("HaulCorpses") ||
                                    workGiver.def.defName.Equals("FillFermentingBarrel") ||
                                    workGiver.def.defName.Equals("HandlingFeedPatientAnimals") ||
                                    workGiver.def.defName.Equals("Train") ||
                                    workGiver.def.defName.Equals("VisitSickPawn") ||
                                    workGiver.def.defName.Equals("DoBillsButcherFlesh") ||
                                    workGiver.def.defName.Equals("DoBillsCook") ||
                                    workGiver.def.defName.Equals("DoBillsMakeApparel")
                                    )
                                {
                                    //long
                                    thing = GenClosest_Patch.ClosestThingReachable2(pawn.Position, pawn.Map, scanner.PotentialWorkThingRequest, scanner.PathEndMode, TraverseParms.For(pawn, scanner.MaxPathDanger(pawn)), 9999f, validator, enumerable, 0, scanner.MaxRegionsToScanBeforeGlobalSearch, enumerable != null);
                                }
                                else if (
                                    workGiver.def.defName.Equals("HaulGeneral")
                                    )
                                {
                                    //long
                                    thing = HaulingCache.ClosestThingReachable(pawn, scanner, pawn.Map, scanner.PotentialWorkThingRequest, scanner.PathEndMode, TraverseParms.For(pawn, scanner.MaxPathDanger(pawn)), 9999f, validator, enumerable, 0, scanner.MaxRegionsToScanBeforeGlobalSearch, enumerable != null);
                                }

                                /*
                                 * else if(
                                 *              workGiver.def.defName.Equals("DoBillsButcherFlesh") ||
                                 *              workGiver.def.defName.Equals("DoBillsCook") ||
                                 *              workGiver.def.defName.Equals("DoBillsMakeApparel"))
                                 * {
                                 *
                                 *      thing = null;
                                 *      //ThingGrid_Patch
                                 *      int mapSizeX = pawn.Map.Size.x;
                                 *      int mapSizeZ = pawn.Map.Size.z;
                                 *      int index = pawn.Map.cellIndices.CellToIndex(pawn.Position);
                                 *      //Dictionary<Bill, float> billPointsDict = ThingGrid_Patch.thingBillPoints[t.def];
                                 *      Dictionary<WorkGiver_Scanner, Dictionary<float, List<HashSet<Thing>[]>>> ingredientDict = ThingGrid_Patch.mapIngredientDict[pawn.Map];
                                 *      ThingRequest thingReq = scanner.PotentialWorkThingRequest;
                                 *      if(!ingredientDict.TryGetValue(scanner, out Dictionary<float, List<HashSet<Thing>[]>> scoreToJumboCellsList)) {
                                 *              scoreToJumboCellsList = new Dictionary<float, List<HashSet<Thing>[]>>();
                                 *              List<Thing> thingsMatchingRequest = pawn.Map.listerThings.ThingsMatching(thingReq);
                                 *      }
                                 *
                                 * }
                                 */
                                else
                                {
                                    //long
                                    thing = GenClosest.ClosestThingReachable(pawn.Position, pawn.Map, scanner.PotentialWorkThingRequest, scanner.PathEndMode, TraverseParms.For(pawn, scanner.MaxPathDanger(pawn)), 9999f, validator, enumerable, 0, scanner.MaxRegionsToScanBeforeGlobalSearch, enumerable != null);
                                }
                            }
                            if (thing != null)
                            {
                                bestTargetOfLastPriority = thing;
                                scannerWhoProvidedTarget = scanner;
                            }
                        }
                        if (scanner.def.scanCells)
                        {
                            pawnPosition       = pawn.Position;
                            closestDistSquared = 99999f;
                            bestPriority       = float.MinValue;
                            prioritized        = scanner.Prioritized;
                            allowUnreachable   = scanner.AllowUnreachable;
                            maxPathDanger      = scanner.MaxPathDanger(pawn);
                            IEnumerable <IntVec3> enumerable4;
                            if (scanner is WorkGiver_GrowerSow workGiver_Grower)
                            {
                                //RimThreaded.WorkGiver_GrowerSow_Patch_JobOnCell = 0;

                                //thing = HaulingCache.ClosestThingReachable(pawn, scanner, pawn.Map, scanner.PotentialWorkThingRequest, scanner.PathEndMode, TraverseParms.For(pawn, scanner.MaxPathDanger(pawn)), 9999f, validator, enumerable, 0, scanner.MaxRegionsToScanBeforeGlobalSearch, enumerable != null);
                                IntVec3 bestCell = WorkGiver_Grower_Patch.ClosestLocationReachable(workGiver_Grower, pawn);
                                //Log.Message(bestCell.ToString());
                                if (bestCell.IsValid)
                                {
                                    bestTargetOfLastPriority = new TargetInfo(bestCell, pawn.Map);
                                    scannerWhoProvidedTarget = scanner;
                                }
                                //Log.Message(RimThreaded.WorkGiver_GrowerSow_Patch_JobOnCell.ToString());
                            }
                            else if (scanner is WorkGiver_GrowerHarvest workGiver_GrowerHarvest)
                            {
                                IntVec3 bestCell = WorkGiver_GrowerHarvest_Patch.ClosestLocationReachable(workGiver_GrowerHarvest, pawn);
                                if (bestCell.IsValid)
                                {
                                    bestTargetOfLastPriority = new TargetInfo(bestCell, pawn.Map);
                                    scannerWhoProvidedTarget = scanner;
                                }

                                /*
                                 * enumerable4 = workGiver_GrowerHarvest.PotentialWorkCellsGlobal(pawn);
                                 * IList<IntVec3> list2;
                                 * if ((list2 = (enumerable4 as IList<IntVec3>)) != null)
                                 * {
                                 *      for (int k = 0; k < list2.Count; k++)
                                 *      {
                                 *              ProcessCell(list2[k]);
                                 *      }
                                 * }
                                 * else
                                 * {
                                 *      foreach (IntVec3 item in enumerable4)
                                 *      {
                                 *              ProcessCell(item);
                                 *      }
                                 * }
                                 */
                            }
                            else
                            {
                                enumerable4 = scanner.PotentialWorkCellsGlobal(pawn);
                                IList <IntVec3> list2;
                                if ((list2 = (enumerable4 as IList <IntVec3>)) != null)
                                {
                                    for (int k = 0; k < list2.Count; k++)
                                    {
                                        ProcessCell(list2[k]);
                                    }
                                }
                                else
                                {
                                    foreach (IntVec3 item in enumerable4)
                                    {
                                        ProcessCell(item);
                                    }
                                }
                            }
                        }
                    }
                    void ProcessCell(IntVec3 c)
                    {
                        float newDistanceSquared = (c - pawnPosition).LengthHorizontalSquared;
                        float newPriority        = 0f;

                        if (prioritized)
                        {
                            newPriority = scanner.GetPriority(pawn, c);
                            if (newPriority < bestPriority)
                            {
                                return;
                            }
                        }

                        if (newDistanceSquared < closestDistSquared && !c.IsForbidden(pawn) && scanner.HasJobOnCell(pawn, c))
                        {
                            if (!allowUnreachable && !pawn.CanReach(c, scanner.PathEndMode, maxPathDanger))
                            {
                                return;
                            }

                            bestTargetOfLastPriority = new TargetInfo(c, pawn.Map);
                            scannerWhoProvidedTarget = scanner;
                            closestDistSquared       = newDistanceSquared;
                            bestPriority             = newPriority;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(string.Concat(pawn, " threw exception in WorkGiver ", workGiver.def.defName, ": ", ex.ToString()));
                }
                finally
                {
                }
                if (bestTargetOfLastPriority.IsValid)
                {
                    Job job3 = (!bestTargetOfLastPriority.HasThing) ? scannerWhoProvidedTarget.JobOnCell(pawn, bestTargetOfLastPriority.Cell) : scannerWhoProvidedTarget.JobOnThing(pawn, bestTargetOfLastPriority.Thing);
                    if (job3 != null)
                    {
                        job3.workGiverDef = scannerWhoProvidedTarget.def;
                        __result          = new ThinkResult(job3, __instance, list[j].def.tagToGive);
                        return(false);
                    }

                    //If this was a cached plant job, deregister it
                    if (scannerWhoProvidedTarget is WorkGiver_GrowerSow)
                    {
                        Map          map       = pawn.Map;
                        IntVec3      cell      = bestTargetOfLastPriority.Cell;
                        List <Thing> thingList = cell.GetThingList(map);
                        foreach (Thing thing in thingList)
                        {
                            if (thing is Building_PlantGrower buildingPlantGrower)
                            {
                                PlantSowing_Cache.ReregisterObject(map, cell,
                                                                   WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
                            }
                        }
                        PlantSowing_Cache.ReregisterObject(map, cell, WorkGiver_Grower_Patch.awaitingPlantCellsMapDict);
                    }
                    //HACK - I know. I'm awful.
                    //Log.ErrorOnce(string.Concat(scannerWhoProvidedTarget, " provided target ", bestTargetOfLastPriority, " but yielded no actual job for pawn ", pawn, ". The CanGiveJob and JobOnX methods may not be synchronized."), 6112651);
                    Log.Warning(string.Concat(scannerWhoProvidedTarget, " provided target ", bestTargetOfLastPriority, " but yielded no actual job for pawn ", pawn, ". The CanGiveJob and JobOnX methods may not be synchronized."));
                }
                num = workGiver.def.priorityInType;

#if DEBUG
                int milli99 = (int)DateTime.Now.Subtract(startTime).TotalMilliseconds;
                if (milli99 > 300)
                {
                    Log.Warning("99 JobGiver_Work.TryIssueJobPackage Took over " + milli99.ToString() + "ms for workGiver: " + workGiver.def.defName);
                    //Log.Warning(scanner.PotentialWorkThingRequest.ToString());
                    //Log.Warning(validator.ToString());
                }
#endif
            }
            __result = ThinkResult.NoJob;
            return(false);
        }