public override void UpdateAllDuties() { var nearHostiles = Map.attackTargetsCache.TargetsHostileToFaction(lord.faction) .Where(targ => !targ.ThreatDisabled()) .Select(targ => targ.Thing); var aveHostilesPos = nearHostiles.Select(t => t.Position).Average(); IntVec3 closestGuardSpot; IntVec3 bestGatherSpot; if (aveHostilesPos.IsValid && Info.guardPositions.Any()) { closestGuardSpot = Info.guardPositions.MinBy(c => c.DistanceToSquared(aveHostilesPos)); } else { closestGuardSpot = Info.bannerCell; } if (Info.Chapiteaux != null) { bestGatherSpot = Info.Chapiteaux.Position; } else { bestGatherSpot = Info.setupCentre; } foreach (var pawn in lord.ownedPawns) { if (pawn.Dead || pawn.Downed) { continue; } var distressedCarny = RandomExposedCarnyByHealth(pawn); var nearHost = nearHostiles.MinBy(t => pawn.Position.DistanceToSquared(t.Position)); CarnivalRole role = pawn.GetCarnivalRole(); if (role.Is(CarnivalRole.Manager)) { var guard = Info.GetBestGuard(false); if (Rand.Chance(0.4f) && guard != null && !guard.Dead && !guard.Downed) { pawn.mindState.duty = new PawnDuty(DutyDefOf.Escort, guard, 7f) { locomotion = LocomotionUrgency.Jog }; } else { if (distressedCarny != null) { DutyUtility.DefendPoint(pawn, distressedCarny, null, 5f); } else { DutyUtility.DefendPoint(pawn, closestGuardSpot, nearHost); } } } else if (role.Is(CarnivalRole.Guard)) { if (numChargers > 0) { DutyUtility.ChargeHostiles(pawn); numChargers--; } else if (distressedCarny != null && (Rand.Chance(0.33f) || pawn.mindState.enemyTarget == null)) { DutyUtility.DefendPoint(pawn, distressedCarny, null, 5f); } else { DutyUtility.DefendPoint(pawn, closestGuardSpot, nearHost); } } else if (pawn.equipment != null && pawn.equipment.Primary != null) { if (numChargers > 0) { DutyUtility.ChargeHostiles(pawn); numChargers--; } else if (pawn.health.summaryHealth.SummaryHealthPercent > 0.85f) { IntVec3 tentDoor; if (pawnsKilled > 3 && distressedCarny != null) { DutyUtility.DefendPoint(pawn, distressedCarny, null, 5f); } else if (Rand.Bool && (tentDoor = Info.GetRandomTentDoor(true, CarnBuildingType.Attraction).Cell).IsValid) { DutyUtility.DefendPoint(pawn, tentDoor, null); } else { DutyUtility.DefendPoint(pawn, closestGuardSpot, nearHost); } } else { DutyUtility.DefendPoint(pawn, bestGatherSpot, null, 5f); } } else { // TODO pawn.mindState.duty = new PawnDuty(DutyDefOf.Travel, bestGatherSpot) { locomotion = LocomotionUrgency.Sprint }; } } }