Ejemplo n.º 1
0
        public static Job HaulWithTools(Pawn pawn, Vehicle_Cart cart = null)
        {
            Trace.stopWatchStart();

            //Job Setting
            JobDef              jobDef;
            TargetInfo          targetC;
            int                 maxItem;
            int                 thresholdItem;
            int                 reservedMaxItem;
            IEnumerable <Thing> remainingItems;
            bool                ShouldDrop;
            Thing               lastItem = ToolsForHaulUtility.TryGetBackpackLastItem(pawn);

            if (cart == null)
            {
                Apparel_Backpack backpack = ToolsForHaulUtility.TryGetBackpack(pawn);
                jobDef          = jobDefHaulWithBackpack;
                targetC         = backpack;
                maxItem         = backpack.MaxItem;
                thresholdItem   = (int)Math.Ceiling(maxItem * 0.5);
                reservedMaxItem = pawn.inventory.container.Count;
                remainingItems  = pawn.inventory.container;
                ShouldDrop      = true;
                if (lastItem != null)
                {
                    for (int i = 0; i < pawn.inventory.container.Count; i++)
                    {
                        if (pawn.inventory.container[i] == lastItem && (reservedMaxItem - (i + 1)) <= 0)
                        {
                            ShouldDrop = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                jobDef = (cart.TryGetComp <CompMountable>().IsMounted&& cart.TryGetComp <CompMountable>().Driver.RaceProps.Animal)?
                         jobDefHaulWithAnimalCart : jobDefHaulWithCart;
                targetC         = cart;
                maxItem         = cart.MaxItem;
                thresholdItem   = (int)Math.Ceiling(maxItem * 0.5);
                reservedMaxItem = cart.storage.Count;
                remainingItems  = cart.storage;
                ShouldDrop      = (reservedMaxItem > 0) ? true : false;
            }
            Job job = new Job(jobDef);

            job.targetQueueA = new List <TargetInfo>();
            job.targetQueueB = new List <TargetInfo>();
            job.targetC      = targetC;

            Trace.AppendLine(pawn.LabelCap + " In HaulWithTools: " + jobDef.defName + "\n"
                             + "MaxItem: " + maxItem + " reservedMaxItem: " + reservedMaxItem);

            //Drop remaining item
            if (ShouldDrop)
            {
                Trace.AppendLine("Start Drop remaining item");
                bool startDrop = false;
                for (int i = 0; i < remainingItems.Count(); i++)
                {
                    if (cart == null && startDrop == false)
                    {
                        if (remainingItems.ElementAt(i) == lastItem)
                        {
                            startDrop = true;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    IntVec3 storageCell = FindStorageCell(pawn, remainingItems.ElementAt(i), job.targetQueueB);
                    if (storageCell == IntVec3.Invalid)
                    {
                        break;
                    }
                    job.targetQueueB.Add(storageCell);
                }
                if (!job.targetQueueB.NullOrEmpty())
                {
                    Trace.AppendLine("Droping Job is issued");
                    Trace.LogMessage();
                    return(job);
                }
                if (cart != null && job.def == jobDefHaulWithCart && !cart.IsInValidStorage())
                {
                    Trace.AppendLine("In DismountInBase");
                    return(DismountInBase(pawn, cart));
                }
                JobFailReason.Is(ToolsForHaulUtility.NoEmptyPlaceLowerTrans);
                Trace.AppendLine("End Drop remaining item");
                Trace.AppendLine("No Job. Reason: " + JobFailReason.Reason);
                Trace.LogMessage();
                return((Job)null);
            }

            //Collect item
            Trace.AppendLine("Start Collect item");
            IntVec3 searchPos = (cart != null) ? cart.Position : pawn.Position;
            bool    flag1     = false;

            foreach (SlotGroup slotGroup in Find.SlotGroupManager.AllGroupsListInPriorityOrder)
            {
                Trace.AppendLine("Start searching slotGroup");
                if (slotGroup.CellsList.Count() - slotGroup.HeldThings.Count() < maxItem)
                {
                    continue;
                }

                //Counting valid items
                Trace.AppendLine("Start Counting valid items");
                int thingsCount = ListerHaulables.ThingsPotentiallyNeedingHauling().Count(item => slotGroup.Settings.AllowedToAccept(item));

                //Finding valid items
                Trace.AppendLine("Start Finding valid items");
                if (thingsCount > thresholdItem)
                {
                    //ClosestThing_Global_Reachable Configuration
                    Predicate <Thing> predicate = item
                                                  => !job.targetQueueA.Contains(item) && !FireUtility.IsBurning(item) &&
                                                  ((cart != null && cart.allowances.Allows(item)) || cart == null) &&
                                                  slotGroup.Settings.AllowedToAccept(item) &&
                                                  pawn.CanReserveAndReach(item, PathEndMode.Touch, DangerUtility.NormalMaxDanger(pawn));
                    //&& !(item is UnfinishedThing && ((UnfinishedThing)item).BoundBill != null)
                    //&& (item.def.IsNutritionSource && !SocialProperness.IsSociallyProper(item, pawn, false, false));
                    Thing thing = GenClosest.ClosestThing_Global_Reachable(searchPos,
                                                                           ListerHaulables.ThingsPotentiallyNeedingHauling(),
                                                                           PathEndMode.ClosestTouch,
                                                                           TraverseParms.For(TraverseMode.ByPawn, DangerUtility.NormalMaxDanger(pawn)),
                                                                           9999,
                                                                           predicate);
                    if (thing == null)
                    {
                        continue;
                    }
                    IntVec3 center = thing.Position;

                    //Enqueue items in valid distance
                    Trace.AppendLine("Start Enqueuing items in valid distance");
                    foreach (Thing item in ListerHaulables.ThingsPotentiallyNeedingHauling().Where(item
                                                                                                   => !job.targetQueueA.Contains(item) && !FireUtility.IsBurning(item) &&
                                                                                                   ((cart != null && cart.allowances.Allows(item)) || cart == null) &&
                                                                                                   slotGroup.Settings.AllowedToAccept(item) &&
                                                                                                   pawn.CanReserveAndReach(item, PathEndMode.Touch, DangerUtility.NormalMaxDanger(pawn)) &&
                                                                                                   center.DistanceToSquared(item.Position) <= ValidDistance))
                    {
                        job.targetQueueA.Add(item);
                        if (reservedMaxItem + job.targetQueueA.Count >= maxItem)
                        {
                            break;
                        }
                    }

                    //Find storage cell
                    Trace.AppendLine("Start Finding storage cell");
                    if (reservedMaxItem + job.targetQueueA.Count > thresholdItem)
                    {
                        List <IntVec3> availableCells = new List <IntVec3>();
                        foreach (IntVec3 cell in slotGroup.CellsList.Where(cell => ReservationUtility.CanReserve(pawn, cell) && cell.Standable() && cell.GetStorable() == null))
                        {
                            job.targetQueueB.Add(cell);
                            if (job.targetQueueB.Count >= job.targetQueueA.Count)
                            {
                                break;
                            }
                        }
                        flag1 = true;
                        break;
                    }
                    else
                    {
                        job.targetQueueA.Clear();
                    }
                }
                if (flag1)
                {
                    break;
                }
            }
            Trace.AppendLine("Elapsed Time");
            Trace.stopWatchStop();

            //Check job is valid
            if (!job.targetQueueA.NullOrEmpty() && reservedMaxItem + job.targetQueueA.Count > thresholdItem &&
                !job.targetQueueB.NullOrEmpty())
            {
                Trace.AppendLine("Hauling Job is issued");
                Trace.LogMessage();
                return(job);
            }
            if (cart != null && job.def == jobDefHaulWithCart && !cart.IsInValidStorage())
            {
                Trace.AppendLine("In DismountInBase: ");
                return(DismountInBase(pawn, cart));
            }

            if (job.targetQueueA.NullOrEmpty())
            {
                JobFailReason.Is(ToolsForHaulUtility.NoHaulable);
            }
            else if (reservedMaxItem + job.targetQueueA.Count <= thresholdItem)
            {
                JobFailReason.Is(ToolsForHaulUtility.TooLittleHaulable);
            }
            else if (job.targetQueueB.NullOrEmpty())
            {
                JobFailReason.Is(ToolsForHaulUtility.NoEmptyPlaceLowerTrans);
            }
            Trace.AppendLine("No Job. Reason: " + JobFailReason.Reason);
            Trace.LogMessage();
            return((Job)null);
        }
        protected override IEnumerable <Toil> MakeNewToils()
        {
            base.AddEndCondition(delegate
            {
                Thing thing = base.GetActor().jobs.curJob.GetTarget(TargetIndex.A).Thing;
                if (thing is Building && !thing.Spawned)
                {
                    return(JobCondition.Incompletable);
                }
                return(JobCondition.Ongoing);
            });
            this.FailOnBurningImmobile(TargetIndex.A);
            this.FailOn(delegate()
            {
                IBillGiver billGiver = this.job.GetTarget(TargetIndex.A).Thing as IBillGiver;
                if (billGiver != null)
                {
                    if (this.job.bill.DeletedOrDereferenced)
                    {
                        return(true);
                    }
                    if (!billGiver.CurrentlyUsableForBills())
                    {
                        return(true);
                    }
                }
                return(false);
            });
            Toil gotoBillGiver = Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.InteractionCell);

            yield return(new Toil
            {
                initAction = delegate()
                {
                    if (this.job.targetQueueB != null && this.job.targetQueueB.Count == 1)
                    {
                        UnfinishedThing unfinishedThing = this.job.targetQueueB[0].Thing as UnfinishedThing;
                        if (unfinishedThing != null)
                        {
                            unfinishedThing.BoundBill = (Bill_ProductionWithUft)this.job.bill;
                        }
                    }
                }
            });

            //yield return Toils_Jump.JumpIf(gotoBillGiver, () => this.job.GetTargetQueue(TargetIndex.B).NullOrEmpty<LocalTargetInfo>());
            Toil extract = Toils_JobTransforms.ExtractNextTargetFromQueue(TargetIndex.B, true);

            yield return(extract);

            yield return(new Toil
            {
                initAction = delegate()
                {
                    var alien = TargetB.Thing;
                    var container = (Building_СontainmentBreach)TargetA.Thing;
                    if (container.innerContainer.Contains(alien) && ReservationUtility.CanReserveAndReach
                            (GetActor(), container, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(GetActor())
                            , 1, -1, null, false))
                    {
                        Log.Message(GetActor() + " JUMP");
                        //Toils_Reserve.Reserve(TargetIndex.A, 1);
                        this.JumpToToil(gotoBillGiver);
                    }
                }
            });

            Toil getToHaulTarget = Toils_Goto.GotoThing(TargetIndex.B, PathEndMode.ClosestTouch);//.FailOnSomeonePhysicallyInteracting(TargetIndex.B);

            yield return(getToHaulTarget);

            yield return(new Toil {
                initAction = delegate() { base.GetActor().CurJob.count = 1; }
            });

            yield return(Toils_Haul.StartCarryThing(TargetIndex.B, true, false, false));

            yield return(JobDriver_PreciseVivisection.JumpToCollectNextIntoHandsForBill(getToHaulTarget, TargetIndex.B));

            //yield return Toils_Haul.CarryHauledThingToCell(TargetIndex.B);
            yield return(Toils_Goto.GotoThing(TargetIndex.A, PathEndMode.InteractionCell).FailOnDestroyedOrNull(TargetIndex.B));

            yield return(Toils_Haul.DepositHauledThingInContainer(TargetIndex.A, TargetIndex.B));

            Toil findPlaceTarget = Toils_JobTransforms.SetTargetToIngredientPlaceCell(TargetIndex.A, TargetIndex.B, TargetIndex.C);

            yield return(findPlaceTarget);

            //yield return Toils_Haul.PlaceHauledThingInCell(TargetIndex.C, findPlaceTarget, false, false);
            yield return(Toils_Jump.JumpIfHaveTargetInQueue(TargetIndex.B, extract));

            extract         = null;
            getToHaulTarget = null;
            findPlaceTarget = null;
            yield return(gotoBillGiver);

            yield return(Toils_Recipe.MakeUnfinishedThingIfNeeded());

            yield return(Toils_Recipe.DoRecipeWork().FailOnCannotTouch(TargetIndex.A, PathEndMode.InteractionCell));

            yield return(new Toil
            {
                initAction = delegate()
                {
                    var alien = TargetB.Thing;
                    foreach (var product in alien.ButcherProducts(base.GetActor(), 1f))
                    {
                        Log.Message(product.def.defName);
                        GenPlace.TryPlaceThing(product, base.GetActor().Position, base.GetActor().Map, ThingPlaceMode.Near);
                    }
                    var container = (Building_СontainmentBreach)TargetA.Thing;
                    container.innerContainer.Remove(alien);
                    alien.Destroy(DestroyMode.Vanish);
                }
            });

            yield return(new Toil
            {
                initAction = delegate()
                {
                    var container = (Building_СontainmentBreach)TargetA.Thing;
                    if (GetActor().Map.reservationManager.ReservedBy(container, GetActor(), GetActor().CurJob))
                    {
                        GetActor().Map.reservationManager.Release(container, GetActor(), GetActor().CurJob);
                    }
                }
            });

            yield return(Toils_Recipe.FinishRecipeAndStartStoringProduct());

            if (!this.job.bill.recipe.products.NullOrEmpty <ThingDefCountClass>() || !this.job.bill.recipe.specialProducts.NullOrEmpty <SpecialProductType>())
            {
                yield return(Toils_Reserve.Reserve(TargetIndex.B, 1, -1, null));

                findPlaceTarget = Toils_Haul.CarryHauledThingToCell(TargetIndex.B);
                yield return(findPlaceTarget);

                //yield return Toils_Haul.PlaceHauledThingInCell(TargetIndex.B, findPlaceTarget, true, true);
                Toil toil = new Toil();
                toil.initAction = delegate()
                {
                    this.Map.resourceCounter.UpdateResourceCounts();
                };
                yield return(toil);

                toil            = null;
                findPlaceTarget = null;
            }
            yield return(new Toil
            {
                initAction = delegate()
                {
                    Log.Message("Job ended");
                }
            });

            yield break;
        }
Ejemplo n.º 3
0
        // Token: 0x06000042 RID: 66 RVA: 0x000030B0 File Offset: 0x000012B0
        public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
        {
            CompUniversalFermenter compUniversalFermenter = ThingCompUtility.TryGetComp <CompUniversalFermenter>(t);

            return(compUniversalFermenter != null && compUniversalFermenter.Fermented && !FireUtility.IsBurning(t) && !ForbidUtility.IsForbidden(t, pawn) && ReservationUtility.CanReserveAndReach(pawn, t, (PathEndMode)2, DangerUtility.NormalMaxDanger(pawn), 1, -1, null, forced));
        }
        // Token: 0x0600003C RID: 60 RVA: 0x00002F10 File Offset: 0x00001110
        public override bool HasJobOnThing(Pawn pawn, Thing t, bool forced = false)
        {
            CompUniversalFermenter compUniversalFermenter = ThingCompUtility.TryGetComp <CompUniversalFermenter>(t);

            if (compUniversalFermenter == null || compUniversalFermenter.Fermented || compUniversalFermenter.SpaceLeftForIngredient <= 0)
            {
                return(false);
            }
            float ambientTemperature = compUniversalFermenter.parent.AmbientTemperature;

            if (ambientTemperature < compUniversalFermenter.Product.temperatureSafe.min + 2f || ambientTemperature > compUniversalFermenter.Product.temperatureSafe.max - 2f)
            {
                JobFailReason.Is(WorkGiver_FillUniversalFermenter.TemperatureTrans, null);
                return(false);
            }
            if (ForbidUtility.IsForbidden(t, pawn) || !ReservationUtility.CanReserveAndReach(pawn, t, (PathEndMode)2, DangerUtility.NormalMaxDanger(pawn), 1, -1, null, forced))
            {
                return(false);
            }
            if (pawn.Map.designationManager.DesignationOn(t, DesignationDefOf.Deconstruct) != null)
            {
                return(false);
            }
            if (this.FindIngredient(pawn, t) == null)
            {
                JobFailReason.Is(WorkGiver_FillUniversalFermenter.NoIngredientTrans, null);
                return(false);
            }
            return(!FireUtility.IsBurning(t));
        }
        bool GetNearbyPlantingSite(IntVec3 originPos, Map map, out IntVec3 newSite)
        {
            Predicate <IntVec3> validator = (IntVec3 tempCell) => IsCellOpenForSowingPlantOfType(tempCell, map, job.plantDefToSow) &&
                                            ReservationUtility.CanReserveAndReach(GetActor(), tempCell, PathEndMode.Touch, DangerUtility.NormalMaxDanger(GetActor()), 1);

            return(CellFinder.TryFindRandomCellNear(originPos, map, 2, validator, out newSite));
        }
Ejemplo n.º 6
0
        public override Job JobOnThing(Pawn pawn, Thing t)
        {
            Vehicle_Cart cart = t as Vehicle_Cart;

            if (cart == null)
            {
                return((Job)null);
            }
            if (cart.IsForbidden(pawn.Faction) || !ReservationUtility.CanReserveAndReach(pawn, cart, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(pawn)))
            {
                return((Job)null);
            }
            if (FireUtility.IsBurning(cart))
            {
                JobFailReason.Is(ToolsForHaulUtility.BurningLowerTrans);
                return((Job)null);
            }
            if (ListerHaulables.ThingsPotentiallyNeedingHauling().Count == 0 && cart.storage.Count == 0)
            {
                JobFailReason.Is(ToolsForHaulUtility.NoHaulable);
                return((Job)null);
            }
            if (Find.SlotGroupManager.AllGroupsListInPriorityOrder.Count == 0)
            {
                JobFailReason.Is(ToolsForHaulUtility.NoEmptyPlaceLowerTrans);
                return((Job)null);
            }
            if (ToolsForHaulUtility.AvailableAnimalCart(cart) || ToolsForHaulUtility.AvailableCart(cart, pawn))
            {
                return(ToolsForHaulUtility.HaulWithTools(pawn, cart));
            }
            JobFailReason.Is(ToolsForHaulUtility.NoAvailableCart);
            return((Job)null);
        }
Ejemplo n.º 7
0
 public override bool HasJobOnThing(Pawn pawn, Thing t)
 {
     if (pawn.Faction != Faction.OfColony)
     {
         return(false);
     }
     RimWorld.Filth filth = t as RimWorld.Filth;
     if (filth == null)
     {
         return(false);
     }
     if (!Find.AreaCleaning[filth.Position] || !ReservationUtility.CanReserveAndReach(pawn, t, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(pawn), 1))
     {
         return(false);
     }
     return(filth.TicksSinceThickened >= minTicksSinceThickened);
 }
Ejemplo n.º 8
0
        public static Toil FindStoreCellForCart(TargetIndex CartInd)
        {
            const int NearbyCell       = 8;
            const int RegionCellOffset = 16;
            IntVec3   invalid          = new IntVec3(0, 0, 0);

            #if DEBUG
            StringBuilder stringBuilder = new StringBuilder();
            #endif
            Toil toil = new Toil();
            toil.initAction = () =>
            {
                IntVec3      storeCell = IntVec3.Invalid;
                Pawn         actor     = toil.GetActor();
                Vehicle_Cart cart      = toil.actor.jobs.curJob.GetTarget(CartInd).Thing as Vehicle_Cart;
                if (cart == null)
                {
                    Log.Error(actor.LabelCap + " Report: Cart is invalid.");
                    toil.actor.jobs.curDriver.EndJobWith(JobCondition.Errored);
                }
                //Find Valid Storage
                foreach (IntVec3 cell in GenRadial.RadialCellsAround(cart.Position, NearbyCell, false))
                {
                    if (cell.IsValidStorageFor(cart) &&
                        ReservationUtility.CanReserveAndReach(actor, cell, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(actor)))
                    {
                        storeCell = cell;
                        #if DEBUG
                        stringBuilder.AppendLine("Found cell: " + storeCell);
                        #endif
                    }
                }

                if (storeCell == IntVec3.Invalid)
                {
                    //Regionwise Flood-fill cellFinder
                    int           regionInd = 0;
                    List <Region> regions   = new List <Region>();
                    regions.Add(cart.Position.GetRegion());
                    #if DEBUG
                    stringBuilder.AppendLine(actor.LabelCap + " Report");
                    #endif
                    bool flag1 = false;
                    while (regionInd < regions.Count)
                    {
                        #if DEBUG
                        stringBuilder.AppendLine("Region id: " + regions[regionInd].id);
                        #endif
                        if (regions[regionInd].extentsClose.Center.InHorDistOf(cart.Position, NearbyCell + RegionCellOffset))
                        {
                            IntVec3 foundCell     = IntVec3.Invalid;
                            IntVec3 distCell      = (regionInd > 0)? regions[regionInd - 1].extentsClose.Center : cart.Position;
                            float   distFoundCell = float.MaxValue;
                            foreach (IntVec3 cell in regions[regionInd].Cells)
                            {
                                //Find best cell for placing cart
                                if (cell.GetEdifice() == null && cell.GetZone() == null && cell.Standable() &&
                                    !GenAdj.CellsAdjacentCardinal(cell, Rot4.North, IntVec2.One).Any(cardinal => cardinal.GetEdifice() is Building_Door) &&
                                    ReservationUtility.CanReserveAndReach(actor, cell, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(actor)))
                                {
                                    if (distCell.DistanceToSquared(cell) < distFoundCell)
                                    {
                                        foundCell     = cell;
                                        distFoundCell = distCell.DistanceToSquared(cell);
                                        flag1         = true;
                                    }
                                }
                            }
                            if (flag1 == true)
                            {
                                storeCell = foundCell;
                                #if DEBUG
                                stringBuilder.AppendLine("Found cell: " + storeCell);
                                #endif
                                break;
                            }
                            foreach (RegionLink link in regions[regionInd].links)
                            {
                                if (regions.Contains(link.RegionA) == false)
                                {
                                    regions.Add(link.RegionA);
                                }
                                if (regions.Contains(link.RegionB) == false)
                                {
                                    regions.Add(link.RegionB);
                                }
                            }
                        }
                        regionInd++;
                    }
                }
                //Log.Message(stringBuilder.ToString());

                /*
                 * //Home Area
                 * if (storeCell == IntVec3.Invalid)
                 *  foreach (IntVec3 cell in Find.AreaHome.ActiveCells.Where(cell => (cell.GetZone() == null || cell.IsValidStorageFor(cart)) && cell.Standable() && cell.GetEdifice() == null))
                 *      if (cell.DistanceToSquared(cart.Position) < NearbyCell)
                 *          storeCell = cell;
                 */
                ReservationUtility.Reserve(actor, storeCell);
                toil.actor.jobs.curJob.targetB = (storeCell != invalid && storeCell != IntVec3.Invalid) ? storeCell : cart.Position;
            };
            return(toil);
        }
Ejemplo n.º 9
0
        public override Job                 JobOnThing(Pawn pawn, Thing t)
        {
            if (!pawn.CanReserveAndReach(( TargetInfo )t.Position, PathEndMode.Touch, DangerUtility.NormalMaxDanger(pawn), 1))
            {
                return((Job)null);
            }

            var hopperSgp = t as ISlotGroupParent;

            if (hopperSgp == null)
            {
                return((Job)null);
            }

            var resource = HopperGetCurrentResource(t.Position, hopperSgp);

            if (
                (resource == null) ||
                (resource.stackCount <= (resource.def.stackLimit / 2))
                )
            {
                return(WorkGiver_FillHopper.HopperFillJob(pawn, hopperSgp, resource));
            }

            JobFailReason.Is("AlreadyFilledLower".Translate());
            return((Job)null);
        }
Ejemplo n.º 10
0
 private Job StartOrResumeBillJob(Pawn pawn, IBillGiver giver)
 {
     for (int i = 0; i < giver.BillStack.Count; i++)
     {
         Bill bill = giver.BillStack[i];
         //Log.Message("Processing: " + bill.recipe.defName);
         if ((bill.recipe.requiredGiverWorkType != null && bill.recipe.requiredGiverWorkType != def.workType) || (Find.TickManager.TicksGame < bill.lastIngredientSearchFailTicks + ReCheckFailedBillTicksRange.RandomInRange && FloatMenuMakerMap.makingFor != pawn))
         {
             continue;
         }
         bill.lastIngredientSearchFailTicks = 0;
         if (!bill.ShouldDoNow() || !bill.PawnAllowedToStartAnew(pawn))
         {
             continue;
         }
         SkillRequirement skillRequirement = bill.recipe.FirstSkillRequirementPawnDoesntSatisfy(pawn);
         if (skillRequirement != null)
         {
             JobFailReason.Is("UnderRequiredSkill".Translate(skillRequirement.minLevel), bill.Label);
             continue;
         }
         Bill_ProductionWithUft bill_ProductionWithUft = bill as Bill_ProductionWithUft;
         if (bill_ProductionWithUft != null)
         {
             if (bill_ProductionWithUft.BoundUft != null)
             {
                 if (bill_ProductionWithUft.BoundWorker == pawn && pawn.CanReserveAndReach(bill_ProductionWithUft.BoundUft, PathEndMode.Touch, Danger.Deadly) && !bill_ProductionWithUft.BoundUft.IsForbidden(pawn))
                 {
                     return(FinishUftJob(pawn, bill_ProductionWithUft.BoundUft, bill_ProductionWithUft));
                 }
                 continue;
             }
             UnfinishedThing unfinishedThing = ClosestUnfinishedThingForBill(pawn, bill_ProductionWithUft);
             if (unfinishedThing != null)
             {
                 return(FinishUftJob(pawn, unfinishedThing, bill_ProductionWithUft));
             }
         }
         if (!TryFindBestBillIngredients(bill, pawn, (Thing)giver, chosenIngThings))
         {
             if (FloatMenuMakerMap.makingFor != pawn)
             {
                 bill.lastIngredientSearchFailTicks = Find.TickManager.TicksGame;
             }
             else
             {
                 JobFailReason.Is(MissingMaterialsTranslated, bill.Label);
             }
             chosenIngThings.Clear();
             continue;
         }
         Job haulOffJob;
         Job result = TryStartNewDoBillJob(pawn, bill, giver, chosenIngThings, out haulOffJob);
         chosenIngThings.Clear();
         if (giver is Building_СontainmentBreach building_WorkTable)
         {
             JobDef jobDef = null;
             if (bill.recipe != null &&
                 ReservationUtility.CanReserveAndReach
                     (pawn, building_WorkTable, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(pawn)
                     , 1, -1, null, false) && building_WorkTable.HasJobOnRecipe(result, out jobDef) &&
                 (result.targetB.Thing == null ||
                  building_WorkTable.innerContainer.Contains(result.targetB.Thing) ||
                  ReservationUtility.CanReserveAndReach
                      (pawn, result.targetB.Thing, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(pawn)
                      , 1, -1, null, false)) &&
                 jobDef != null)
             {
                 try
                 {
                     Log.Message(pawn + " - SUCCESS - " + result.bill.recipe.defName, true);
                 }
                 catch
                 {
                 }
                 result = new Job(jobDef, result.targetA, result.targetB)
                 {
                     targetQueueB = result.targetQueueB,
                     countQueue   = result.countQueue,
                     haulMode     = result.haulMode,
                     bill         = result.bill
                 };
                 return(result);
             }
             else
             {
                 if (result?.bill.recipe != null)
                 {
                     try
                     {
                         //Log.Message("FAIL: " + result.bill.recipe.defName, true);
                         //Log.Message("TARGET A: " + result.targetA.Thing, true);
                         //Log.Message("TARGET B: " + result.targetB.Thing, true);
                         //Log.Message("1" + (result?.RecipeDef != null).ToString());
                         //Log.Message("3" + (ReservationUtility.CanReserveAndReach
                         //(pawn, building_WorkTable, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(pawn)
                         //, 1, -1, null, false)).ToString());
                         //Log.Message("4" + building_WorkTable.HasJobOnRecipe(result, out jobDef).ToString());
                         //Log.Message("5" + (building_WorkTable.innerContainer.Contains(result.targetB.Thing) || ReservationUtility.CanReserveAndReach
                         //(pawn, result.targetB.Thing, PathEndMode.ClosestTouch, DangerUtility.NormalMaxDanger(pawn)
                         //, 1, -1, null, false)).ToString());
                         //Log.Message("6" + (jobDef != null).ToString());
                     }
                     catch
                     {
                     }
                     //Log.Message("----------------", true);
                 }
                 continue;
             }
         }
         return(result);
     }
     chosenIngThings.Clear();
     return(null);
 }