public static Toil FollowAndMeleeAttack(TargetIndex targetInd, Action hitAction) { Toil followAndAttack = new Toil(); followAndAttack.tickAction = delegate() { Pawn actor = followAndAttack.actor; Job curJob = actor.jobs.curJob; JobDriver curDriver = actor.jobs.curDriver; Thing thing = curJob.GetTarget(targetInd).Thing; Pawn pawn = thing as Pawn; if (!thing.Spawned) { curDriver.ReadyForNextToil(); return; } if (thing != actor.pather.Destination.Thing || (!actor.pather.Moving && !actor.CanReachImmediate(thing, PathEndMode.Touch))) { actor.pather.StartPath(thing, PathEndMode.Touch); } else if (actor.CanReachImmediate(thing, PathEndMode.Touch)) { if (pawn != null && pawn.Downed && !curJob.killIncappedTarget) { curDriver.ReadyForNextToil(); return; } hitAction(); } }; followAndAttack.defaultCompleteMode = ToilCompleteMode.Never; return(followAndAttack); }
public static Toil FollowAndMeleeAttack(TargetIndex targetInd, Action hitAction) { //Follow and attack victim Toil followAndAttack = new Toil(); followAndAttack.tickAction = () => { Pawn actor = followAndAttack.actor; Job curJob = actor.jobs.curJob; JobDriver driver = actor.jobs.curDriver; Thing victim = curJob.GetTarget(targetInd).Thing; Pawn victimPawn = victim as Pawn; if (!victim.Spawned) { driver.ReadyForNextToil(); return; } if (victim != actor.pather.Destination.Thing || (!actor.pather.Moving && !actor.CanReachImmediate(victim, PathEndMode.Touch))) { actor.pather.StartPath(victim, PathEndMode.Touch); } else { if (actor.CanReachImmediate(victim, PathEndMode.Touch)) { //Do not attack downed people unless the job specifies to do so if (victimPawn != null && victimPawn.Downed && !curJob.killIncappedTarget) { driver.ReadyForNextToil(); return; } //Try to hit them hitAction(); } } }; followAndAttack.defaultCompleteMode = ToilCompleteMode.Never; return(followAndAttack); }
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; } }