Exemplo n.º 1
0
 public void StartJob(Job newJob, JobCondition lastJobEndCondition = JobCondition.None, ThinkNode jobGiver = null, bool resumeCurJobAfterwards = false, bool cancelBusyStances = true, ThinkTreeDef thinkTree = null, JobTag?tag = null, bool fromQueue = false, bool canReturnCurJobToPool = false)
 {
     startingNewJob = true;
     try
     {
         if (!fromQueue && (!Find.TickManager.Paused || lastJobGivenAtFrame == RealTime.frameCount))
         {
             jobsGivenThisTick++;
             if (Prefs.DevMode)
             {
                 jobsGivenThisTickTextual = jobsGivenThisTickTextual + "(" + newJob.ToString() + ") ";
             }
         }
         lastJobGivenAtFrame = RealTime.frameCount;
         if (jobsGivenThisTick > 10)
         {
             string text = jobsGivenThisTickTextual;
             jobsGivenThisTick        = 0;
             jobsGivenThisTickTextual = "";
             startingNewJob           = false;
             pawn.ClearReservationsForJob(newJob);
             JobUtility.TryStartErrorRecoverJob(pawn, pawn.ToStringSafe() + " started 10 jobs in one tick. newJob=" + newJob.ToStringSafe() + " jobGiver=" + jobGiver.ToStringSafe() + " jobList=" + text);
             return;
         }
         if (debugLog)
         {
             DebugLogEvent(string.Concat("StartJob [", newJob, "] lastJobEndCondition=", lastJobEndCondition, ", jobGiver=", jobGiver, ", cancelBusyStances=", cancelBusyStances.ToString()));
         }
         if (cancelBusyStances && pawn.stances.FullBodyBusy)
         {
             pawn.stances.CancelBusyStanceHard();
         }
         if (curJob != null)
         {
             if (lastJobEndCondition == JobCondition.None)
             {
                 Log.Warning(string.Concat(pawn, " starting job ", newJob, " from JobGiver ", newJob.jobGiver, " while already having job ", curJob, " without a specific job end condition."));
                 lastJobEndCondition = JobCondition.InterruptForced;
             }
             if (resumeCurJobAfterwards && curJob.def.suspendable)
             {
                 jobQueue.EnqueueFirst(curJob);
                 if (debugLog)
                 {
                     DebugLogEvent("   JobQueue EnqueueFirst curJob: " + curJob);
                 }
                 CleanupCurrentJob(lastJobEndCondition, releaseReservations: false, cancelBusyStances);
             }
             else
             {
                 CleanupCurrentJob(lastJobEndCondition, releaseReservations: true, cancelBusyStances, canReturnCurJobToPool);
             }
         }
         if (newJob == null)
         {
             Log.Warning(string.Concat(pawn, " tried to start doing a null job."));
             return;
         }
         newJob.startTick = Find.TickManager.TicksGame;
         if (pawn.Drafted || newJob.playerForced)
         {
             newJob.ignoreForbidden    = true;
             newJob.ignoreDesignations = true;
         }
         curJob = newJob;
         curJob.jobGiverThinkTree = thinkTree;
         curJob.jobGiver          = jobGiver;
         curDriver = curJob.MakeDriver(pawn);
         bool flag = fromQueue;
         if (curDriver.TryMakePreToilReservations(!flag))
         {
             Job job = TryOpportunisticJob(newJob);
             if (job != null)
             {
                 jobQueue.EnqueueFirst(newJob);
                 curJob    = null;
                 curDriver = null;
                 StartJob(job);
                 return;
             }
             if (tag.HasValue)
             {
                 pawn.mindState.lastJobTag = tag.Value;
             }
             curDriver.SetInitialPosture();
             curDriver.Notify_Starting();
             curDriver.SetupToils();
             curDriver.ReadyForNextToil();
         }
         else if (flag)
         {
             EndCurrentJob(JobCondition.QueuedNoLongerValid);
         }
         else
         {
             Log.Warning("TryMakePreToilReservations() returned false for a non-queued job right after StartJob(). This should have been checked before. curJob=" + curJob.ToStringSafe());
             EndCurrentJob(JobCondition.Errored);
         }
     }
     finally
     {
         startingNewJob = false;
     }
 }