public void PatherTick() { if (this.WillCollideWithPawnAt(this.pawn.Position)) { if (!this.FailedToFindCloseUnoccupiedCellRecently()) { IntVec3 intVec; if (CellFinder.TryFindBestPawnStandCell(this.pawn, out intVec, true) && intVec != this.pawn.Position) { this.pawn.Position = intVec; this.ResetToCurrentPosition(); if (this.moving && this.TrySetNewPath()) { this.TryEnterNextPathCell(); } } else { this.failedToFindCloseUnoccupiedCellTicks = Find.TickManager.TicksGame; } } return; } if (this.pawn.stances.FullBodyBusy) { return; } if (!this.moving || !this.WillCollideWithPawnOnNextPathCell()) { this.lastMovedTick = Find.TickManager.TicksGame; if (this.nextCellCostLeft > 0f) { this.nextCellCostLeft -= this.CostToPayThisTick(); } else if (this.moving) { this.TryEnterNextPathCell(); } return; } this.nextCellCostLeft = this.nextCellCostTotal; if (((this.curPath != null && this.curPath.NodesLeftCount < 30) || PawnUtility.AnyPawnBlockingPathAt(this.nextCell, this.pawn, false, true, false)) && !this.BestPathHadPawnsInTheWayRecently() && this.TrySetNewPath()) { this.ResetToCurrentPosition(); this.TryEnterNextPathCell(); return; } if (Find.TickManager.TicksGame - this.lastMovedTick >= 180) { Pawn pawn = PawnUtility.PawnBlockingPathAt(this.nextCell, this.pawn, false, false, false); if (pawn != null && this.pawn.HostileTo(pawn) && this.pawn.TryGetAttackVerb(pawn, false) != null) { Job job = new Job(JobDefOf.AttackMelee, pawn); job.maxNumMeleeAttacks = 1; job.expiryInterval = 300; this.pawn.jobs.StartJob(job, JobCondition.Incompletable, null, false, true, null, null, false); return; } } }
private bool TrySetNewPath() { PawnPath pawnPath = this.GenerateNewPath(); if (!pawnPath.Found) { this.PatherFailed(); return(false); } if (this.curPath != null) { this.curPath.ReleaseToPool(); } this.curPath = pawnPath; int num = 0; while (num < 20 && num < this.curPath.NodesLeftCount) { IntVec3 c = this.curPath.Peek(num); if (PawnUtility.ShouldCollideWithPawns(this.pawn) && PawnUtility.AnyPawnBlockingPathAt(c, this.pawn, false, false, false)) { this.foundPathWhichCollidesWithPawns = Find.TickManager.TicksGame; } if (PawnUtility.KnownDangerAt(c, this.pawn.Map, this.pawn)) { this.foundPathWithDanger = Find.TickManager.TicksGame; } if (this.foundPathWhichCollidesWithPawns == Find.TickManager.TicksGame && this.foundPathWithDanger == Find.TickManager.TicksGame) { break; } num++; } return(true); }
private static IAttackTarget FindBestReachableMeleeTarget(Predicate <IAttackTarget> validator, Pawn searcherPawn, float maxTargDist, bool canBash) { maxTargDist = Mathf.Min(maxTargDist, 30f); IAttackTarget reachableTarget = null; Func <IntVec3, IAttackTarget> bestTargetOnCell = delegate(IntVec3 x) { List <Thing> thingList = x.GetThingList(searcherPawn.Map); for (int j = 0; j < thingList.Count; j++) { Thing thing = thingList[j]; IAttackTarget attackTarget2 = thing as IAttackTarget; if (attackTarget2 != null && validator(attackTarget2) && ReachabilityImmediate.CanReachImmediate(x, thing, searcherPawn.Map, PathEndMode.Touch, searcherPawn) && (searcherPawn.CanReachImmediate(thing, PathEndMode.Touch) || searcherPawn.Map.attackTargetReservationManager.CanReserve(searcherPawn, attackTarget2))) { return(attackTarget2); } } return(null); }; searcherPawn.Map.floodFiller.FloodFill(searcherPawn.Position, delegate(IntVec3 x) { if (!x.Walkable(searcherPawn.Map)) { return(false); } if ((float)x.DistanceToSquared(searcherPawn.Position) > maxTargDist * maxTargDist) { return(false); } if (!canBash) { Building_Door building_Door = x.GetEdifice(searcherPawn.Map) as Building_Door; if (building_Door != null && !building_Door.CanPhysicallyPass(searcherPawn)) { return(false); } } return((!PawnUtility.AnyPawnBlockingPathAt(x, searcherPawn, actAsIfHadCollideWithPawnsJob: true)) ? true : false); }, delegate(IntVec3 x) { for (int i = 0; i < 8; i++) { IntVec3 intVec = x + GenAdj.AdjacentCells[i]; if (intVec.InBounds(searcherPawn.Map)) { IAttackTarget attackTarget = bestTargetOnCell(intVec); if (attackTarget != null) { reachableTarget = attackTarget; break; } } } return(reachableTarget != null); }); return(reachableTarget); }
void RepositionColonists() { var checkInterval = 15; var radius = 7f; var radiusSquared = (int)(radius * radius); map.mapPawns .FreeHumanlikesSpawnedOfFaction(Faction.OfPlayer) .Where(colonist => colonist.IsHashIntervalTick(checkInterval) && RepositionCondition(colonist)) .Do(pawn => { var pos = pawn.Position; var zombiesNearby = Tools.GetCircle(radius).Select(vec => pos + vec) .Where(vec => vec.InBounds(map) && avoidGrid.GetCosts()[vec.x + vec.z * map.Size.x] >= 3000) .SelectMany(vec => map.thingGrid.ThingsListAtFast(vec).OfType <Zombie>()) .Where(zombie => zombie.Downed == false); var maxDistance = 0; var safeDestination = IntVec3.Invalid; map.floodFiller.FloodFill(pos, delegate(IntVec3 vec) { if (!vec.Walkable(map)) { return(false); } if ((float)vec.DistanceToSquared(pos) > radiusSquared) { return(false); } if (map.thingGrid.ThingAt <Zombie>(vec)?.Downed ?? true == false) { return(false); } if (vec.GetEdifice(map) is Building_Door building_Door && !building_Door.CanPhysicallyPass(pawn)) { return(false); } return(!PawnUtility.AnyPawnBlockingPathAt(vec, pawn, true, false)); }, delegate(IntVec3 vec) { var distance = zombiesNearby.Select(zombie => (vec - zombie.Position).LengthHorizontalSquared).Sum(); if (distance > maxDistance) { maxDistance = distance; safeDestination = vec; } return(false); }); if (safeDestination.IsValid) { var newJob = new Job(JobDefOf.Goto, safeDestination) { playerForced = true }; pawn.jobs.StartJob(newJob, JobCondition.InterruptForced, null, false, true, null, null); } }); }
public override int GetCost(CellRef cell) { if (PawnUtility.AnyPawnBlockingPathAt(cell, pawn, false, false, true)) { return(Cost_PawnCollision); } return(0); }
private bool WillCollideWithPawnAt(IntVec3 c) { if (!PawnUtility.ShouldCollideWithPawns(this.pawn)) { return(false); } return(PawnUtility.AnyPawnBlockingPathAt(c, this.pawn, false, false)); }
private short GetTotalPerceivedPathCost(TraverseParms traverseParms, bool canPassAnything, bool shouldCollideWithPawns, Pawn pawn, PawnPathCostSettings pawnPathCosts) { int neighCost = 0; if (!pathGridDirect.WalkableExtraFast(neighIndex)) { if (!canPassAnything) { return(10000); } neighCost += 60; Thing edifice = edificeGrid[neighIndex]; if (edifice == null || !edifice.def.useHitPoints) { return(10000); } neighCost += (int)(edifice.HitPoints * 0.1f); } else { neighCost += pathGridDirect[neighIndex]; } if (shouldCollideWithPawns && PawnUtility.AnyPawnBlockingPathAt(map.cellIndices.IndexToCell(neighIndex), pawn)) { neighCost += 800; } Building building = edificeGrid[neighIndex]; if (building != null) { int cost = GetPathCostForBuilding(building, traverseParms); if (cost < 0) { return(10000); } neighCost += cost; } if (pawnPathCosts.avoidGrid != null) { neighCost += pawnPathCosts.avoidGrid[neighIndex] * 8; } if (pawnPathCosts.area?[neighIndex] == false) { //Allowed area path cost switched from adding a constant to multiplying the path cost //Mostly this was to reduce reopens when very high path cost nodes get explored greedily (like trees) //But I would imagine the player would generally prefer their pawns not spend ages walking over several trees //just to get a path one tile shorter. neighCost = (Math.Max(neighCost, 10) + moveTicksCardinal * 2) * 10; } return((short)Math.Min(neighCost, 9999)); }
public static bool TrySetNewPath(ref Pawn_PathFollower instance, ref IntVec3 lastPathedTargetPosition, LocalTargetInfo destination, Pawn pawn, Map map, ref PathEndMode peMode) { PawnPath pawnPath = GenerateNewPath(ref lastPathedTargetPosition, destination, ref pawn, map, peMode); if (!pawnPath.Found) { PatherFailedHelper(ref instance, pawn); return(false); } if (!(instance.curPath is null)) { instance.curPath.ReleaseToPool(); } instance.curPath = pawnPath; int num = 0; int foundPathWhichCollidesWithPawns = Traverse.Create(instance).Field("foundPathWhichCollidesWithPawns").GetValue <int>(); int foundPathWithDanger = Traverse.Create(instance).Field("foundPathWithDanger").GetValue <int>(); while (num < 20 && num < instance.curPath.NodesLeftCount) { IntVec3 c = instance.curPath.Peek(num); if (pawn.GetComp <CompShips>().beached) { break; } if (PawnUtility.ShouldCollideWithPawns(pawn) && PawnUtility.AnyPawnBlockingPathAt(c, pawn, false, false, false)) { foundPathWhichCollidesWithPawns = Find.TickManager.TicksGame; } if (PawnUtility.KnownDangerAt(c, pawn.Map, pawn)) { foundPathWithDanger = Find.TickManager.TicksGame; } if (foundPathWhichCollidesWithPawns == Find.TickManager.TicksGame && foundPathWithDanger == Find.TickManager.TicksGame) { break; } num++; } return(true); }
//public PawnPath FindPath(IntVec3 start, LocalTargetInfo dest, Pawn pawn, PathEndMode peMode = PathEndMode.OnCell) //{ // bool flag = false; // if (pawn != null && pawn.CurJob != null && pawn.CurJob.canBash) // { // flag = true; // } // Danger maxDanger = Danger.Deadly; // bool canBash = flag; // return this.FindPath(start, dest, TraverseParms.For(pawn, maxDanger, TraverseMode.ByPawn, canBash), peMode); //} public PawnPath FindPath(IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, PathEndMode peMode = PathEndMode.OnCell) { //Walk through Walls Mode if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } //Check if there's a pawn to path for Pawn pawn = traverseParms.pawn; if (pawn != null && pawn.Map != this.map) { Log.Error(string.Concat(new object[] { "Tried to FindPath for pawn which is spawned in another map. His map CVPathFinder should have been used, not this one. pawn=", pawn, " pawn.Map=", pawn.Map, " map=", this.map })); return(PawnPath.NotFound); } //Make sure the start is valid if (!start.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindPath with invalid start ", start, ", pawn= ", pawn })); return(PawnPath.NotFound); } //Make sure the end is valid if (!dest.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindPath with invalid dest ", dest, ", pawn= ", pawn })); return(PawnPath.NotFound); } //Check if it's possible to get to the destination Log.Error(traverseParms.mode.ToString()); if (traverseParms.mode == TraverseMode.ByPawn) { if (!pawn.CanReach(dest, peMode, Danger.Deadly, traverseParms.canBash, traverseParms.mode)) { return(PawnPath.NotFound); } } else if (!this.map.reachability.CanReach(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound); } Log.Error("a"); this.PfProfilerBeginSample(string.Concat(new object[] { "FindPath for ", pawn, " from ", start, " to ", dest, (!dest.HasThing) ? string.Empty : (" at " + dest.Cell) })); this.cellIndices = this.map.cellIndices; this.pathGrid = this.map.pathGrid; this.edificeGrid = this.map.edificeGrid.InnerArray; int x = dest.Cell.x; int z = dest.Cell.z; int num = this.cellIndices.CellToIndex(start); int num2 = this.cellIndices.CellToIndex(dest.Cell); ByteGrid byteGrid = (pawn == null) ? null : pawn.GetAvoidGrid(); bool flag = traverseParms.mode == TraverseMode.PassAllDestroyableThings; bool flag2 = traverseParms.mode != TraverseMode.NoPassClosedDoorsOrWater && traverseParms.mode != TraverseMode.PassAllDestroyableThingsNotWater; bool flag3 = !flag; CellRect cellRect = this.CalculateDestinationRect(dest, peMode); bool flag4 = cellRect.Width == 1 && cellRect.Height == 1; int[] array = this.map.pathGrid.pathGrid; EdificeGrid edificeGrid = this.map.edificeGrid; int num3 = 0; int num4 = 0; Area allowedArea = this.GetAllowedArea(pawn); bool flag5 = pawn != null && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = true && DebugViewSettings.drawPaths; bool flag7 = !flag && start.GetRegion(this.map, RegionType.Set_Passable) != null; bool flag8 = !flag || !flag3; bool flag9 = false; int num5 = 0; int num6 = 0; float num7 = this.DetermineHeuristicStrength(pawn, start, dest); int num8; int num9; Log.Error("b"); if (pawn != null) { num8 = pawn.TicksPerMoveCardinal; num9 = pawn.TicksPerMoveDiagonal; } else { num8 = 13; num9 = 18; } this.CalculateAndAddDisallowedCorners(traverseParms, peMode, cellRect); this.InitStatusesAndPushStartNode(ref num, start); while (true) { Log.Error("c"); this.PfProfilerBeginSample("Open cell"); if (this.openList.Count <= 0) { break; } num5 += this.openList.Count; num6++; CVPathFinder.CostNode costNode = this.openList.Pop(); num = costNode.index; if (costNode.cost != this.calcGrid[num].costNodeCost) { Log.Error("d"); this.PfProfilerEndSample(); } else if (this.calcGrid[num].status == this.statusClosedValue) { Log.Error("e"); this.PfProfilerEndSample(); } else { Log.Error("f"); IntVec3 c = this.cellIndices.IndexToCell(num); int x2 = c.x; int z2 = c.z; if (flag6) { this.DebugFlash(c, (float)this.calcGrid[num].knownCost / 1500f, this.calcGrid[num].knownCost.ToString()); } if (flag4) { if (num == num2) { goto Block_27; } } else if (cellRect.Contains(c) && !this.disallowedCornerIndices.Contains(num)) { goto Block_29; } if (num3 > 160000) { goto Block_30; } this.PfProfilerEndSample(); this.PfProfilerBeginSample("Neighbor consideration"); for (int i = 0; i < 8; i++) { uint num10 = (uint)(x2 + CVPathFinder.Directions[i]); uint num11 = (uint)(z2 + CVPathFinder.Directions[i + 8]); if ((ulong)num10 < (ulong)((long)this.mapSizeX) && (ulong)num11 < (ulong)((long)this.mapSizeZ)) { int num12 = (int)num10; int num13 = (int)num11; int num14 = this.cellIndices.CellToIndex(num12, num13); if (this.calcGrid[num14].status != this.statusClosedValue || flag9) { int num15 = 0; bool flag10 = false; if (flag2 || !new IntVec3(num12, 0, num13).GetTerrain(this.map).HasTag("Water") || (pawn.GetComp <CompVehicle>() != null && pawn.GetComp <CompVehicle>().Props.vehicleType == VehicleType.Amphibious)) { if (!this.pathGrid.WalkableFast(num14)) { if (!flag) { if (flag6) { this.DebugFlash(new IntVec3(num12, 0, num13), 0.22f, "walk"); } goto IL_C2E; } flag10 = true; num15 += 70; Building building = edificeGrid[num14]; if (building == null) { goto IL_C2E; } if (!CVPathFinder.IsDestroyable(building)) { goto IL_C2E; } num15 += (int)((float)building.HitPoints * 0.11f); } if (i > 3) { switch (i) { case 4: if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } break; case 5: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } break; case 6: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } break; case 7: if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } goto IL_C2E; } num15 += 70; } break; } } Log.Error("g"); int num16 = (i <= 3) ? num8 : num9; num16 += num15; if (!flag10) { num16 += array[num14]; } if (byteGrid != null) { num16 += (int)(byteGrid[num14] * 8); } if (allowedArea != null && !allowedArea[num14]) { num16 += 600; } if (flag5 && PawnUtility.AnyPawnBlockingPathAt(new IntVec3(num12, 0, num13), pawn, false, false)) { num16 += 175; } Log.Error("h"); Building building2 = this.edificeGrid[num14]; if (building2 != null) { this.PfProfilerBeginSample("Edifices"); int buildingCost = CVPathFinder.GetBuildingCost(building2, traverseParms, pawn); if (buildingCost == 2147483647) { this.PfProfilerEndSample(); goto IL_C2E; } num16 += buildingCost; this.PfProfilerEndSample(); } int num17 = num16 + this.calcGrid[num].knownCost; ushort status = this.calcGrid[num14].status; if (status == this.statusClosedValue || status == this.statusOpenValue) { int num18 = 0; if (status == this.statusClosedValue) { num18 = num8; } if (this.calcGrid[num14].knownCost <= num17 + num18) { goto IL_C2E; } } if (status != this.statusClosedValue && status != this.statusOpenValue) { if (flag9) { this.calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)this.regionCostCalculator.GetPathCostFromDestToRegion(num14) * CVPathFinder.RegionHeuristicWeightByNodesOpened.Evaluate((float)num4)); } else { int dx = Math.Abs(num12 - x); int dz = Math.Abs(num13 - z); int num19 = GenMath.OctileDistance(dx, dz, num8, num9); this.calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)num19 * num7); } } int num20 = num17 + this.calcGrid[num14].heuristicCost; this.calcGrid[num14].parentIndex = num; this.calcGrid[num14].knownCost = num17; this.calcGrid[num14].status = this.statusOpenValue; this.calcGrid[num14].costNodeCost = num20; num4++; this.openList.Push(new CVPathFinder.CostNode(num14, num20)); } } } IL_C2E :; } this.PfProfilerEndSample(); num3++; this.calcGrid[num].status = this.statusClosedValue; if (num4 >= 2000 && flag7 && !flag9) { flag9 = true; this.regionCostCalculator.Init(cellRect, traverseParms, num8, num9, byteGrid, allowedArea, this.disallowedCornerIndices); this.InitStatusesAndPushStartNode(ref num, start); num4 = 0; num3 = 0; } } } string text = (pawn == null || pawn.CurJob == null) ? "null" : pawn.CurJob.ToString(); string text2 = (pawn == null || pawn.Faction == null) ? "null" : pawn.Faction.ToString(); Log.Warning(string.Concat(new object[] { pawn, " pathing from ", start, " to ", dest, " ran out of cells to process.\nJob:", text, "\nFaction: ", text2 })); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); Block_27: this.PfProfilerEndSample(); PawnPath result = this.FinalizedPath(num); this.PfProfilerEndSample(); return(result); Block_29: this.PfProfilerEndSample(); PawnPath result2 = this.FinalizedPath(num); this.PfProfilerEndSample(); return(result2); Block_30: Log.Warning(string.Concat(new object[] { pawn, " pathing from ", start, " to ", dest, " hit search limit of ", 160000, " cells." })); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); }
//public static FastPriorityQueue<CostNode2> openList = new FastPriorityQueue<CostNode2>(new CostNodeComparer2()); //public static PathFinderNodeFast[] calcGrid = new PathFinderNodeFast[40000]; //public static ushort statusOpenValue = 1; //public static ushort statusClosedValue = 2; public static bool FindPath(PathFinder __instance, ref PawnPath __result, IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, PathEndMode peMode = PathEndMode.OnCell) { FastPriorityQueue <CostNode2> openList = getOpenList(); ushort statusOpenValue = getOpenValue(); ushort statusClosedValue = getClosedValue(); PathFinderNodeFast[] calcGrid = getCalcGrid(__instance); RegionCostCalculatorWrapper regionCostCalculator = getRegionCostCalculatorWrapper(__instance); if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } Pawn pawn = traverseParms.pawn; if (pawn != null && pawn.Map != mapField(__instance)) { Log.Error(string.Concat("Tried to FindPath for pawn which is spawned in another map. His map PathFinder should have been used, not this one. pawn=", pawn, " pawn.Map=", pawn.Map, " map=", mapField(__instance))); __result = PawnPath.NotFound; return(false); } if (!start.IsValid) { Log.Error(string.Concat("Tried to FindPath with invalid start ", start, ", pawn= ", pawn)); __result = PawnPath.NotFound; return(false); } if (!dest.IsValid) { Log.Error(string.Concat("Tried to FindPath with invalid dest ", dest, ", pawn= ", pawn)); __result = PawnPath.NotFound; return(false); } if (traverseParms.mode == TraverseMode.ByPawn) { if (!pawn.CanReach(dest, peMode, Danger.Deadly, traverseParms.canBash, traverseParms.mode)) { __result = PawnPath.NotFound; return(false); } } else if (!mapField(__instance).reachability.CanReach(start, dest, peMode, traverseParms)) { __result = PawnPath.NotFound; return(false); } PfProfilerBeginSample(string.Concat("FindPath for ", pawn, " from ", start, " to ", dest, dest.HasThing ? (" at " + dest.Cell) : "")); cellIndicesField(__instance) = mapField(__instance).cellIndices; pathGridField(__instance) = mapField(__instance).pathGrid; edificeGridField(__instance) = mapField(__instance).edificeGrid.InnerArray; blueprintGridField(__instance) = mapField(__instance).blueprintGrid.InnerArray; int x = dest.Cell.x; int z = dest.Cell.z; int curIndex = cellIndicesField(__instance).CellToIndex(start); int num = cellIndicesField(__instance).CellToIndex(dest.Cell); ByteGrid byteGrid = pawn?.GetAvoidGrid(); bool flag = traverseParms.mode == TraverseMode.PassAllDestroyableThings || traverseParms.mode == TraverseMode.PassAllDestroyableThingsNotWater; bool flag2 = traverseParms.mode != TraverseMode.NoPassClosedDoorsOrWater && traverseParms.mode != TraverseMode.PassAllDestroyableThingsNotWater; bool flag3 = !flag; CellRect destinationRect = CalculateDestinationRect(dest, peMode); bool flag4 = destinationRect.Width == 1 && destinationRect.Height == 1; int[] array = mapField(__instance).pathGrid.pathGrid; TerrainDef[] topGrid = mapField(__instance).terrainGrid.topGrid; EdificeGrid edificeGrid = mapField(__instance).edificeGrid; int num2 = 0; int num3 = 0; Area allowedArea = GetAllowedArea(pawn); bool flag5 = pawn != null && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = !flag && start.GetRegion(mapField(__instance)) != null && flag2; bool flag7 = !flag || !flag3; bool flag8 = false; bool flag9 = pawn?.Drafted ?? false; int num4 = (pawn?.IsColonist ?? false) ? 100000 : 2000; int num5 = 0; int num6 = 0; float num7 = DetermineHeuristicStrength(pawn, start, dest); int num8; int num9; if (pawn != null) { num8 = pawn.TicksPerMoveCardinal; num9 = pawn.TicksPerMoveDiagonal; } else { num8 = 13; num9 = 18; } CalculateAndAddDisallowedCorners2(disallowedCornerIndicesField(__instance), mapField(__instance), peMode, destinationRect); InitStatusesAndPushStartNode3(ref curIndex, start, cellIndicesField(__instance), calcGrid, ref statusOpenValue, ref statusClosedValue); openList.Clear(); openList.Push(new CostNode2(curIndex, 0)); while (true) { PfProfilerBeginSample("Open cell"); if (openList.Count <= 0) { string text = (pawn != null && pawn.CurJob != null) ? pawn.CurJob.ToString() : "null"; string text2 = (pawn != null && pawn.Faction != null) ? pawn.Faction.ToString() : "null"; Log.Warning(string.Concat(pawn, " pathing from ", start, " to ", dest, " ran out of cells to process.\nJob:", text, "\nFaction: ", text2)); DebugDrawRichData(); PfProfilerEndSample(); PfProfilerEndSample(); __result = PawnPath.NotFound; return(false); } num5 += openList.Count; num6++; CostNode2 costNode = openList.Pop(); curIndex = costNode.index; if (costNode.cost != calcGrid[curIndex].costNodeCost) { PfProfilerEndSample(); continue; } if (calcGrid[curIndex].status == statusClosedValue) { PfProfilerEndSample(); continue; } IntVec3 c = cellIndicesField(__instance).IndexToCell(curIndex); int x2 = c.x; int z2 = c.z; if (flag4) { if (curIndex == num) { PfProfilerEndSample(); PawnPath result = FinalizedPath2(curIndex, flag8, cellIndicesField(__instance), calcGrid); PfProfilerEndSample(); __result = result; return(false); } } else if (destinationRect.Contains(c) && !disallowedCornerIndicesField(__instance).Contains(curIndex)) { PfProfilerEndSample(); PawnPath result2 = FinalizedPath2(curIndex, flag8, cellIndicesField(__instance), calcGrid); PfProfilerEndSample(); __result = result2; return(false); } if (num2 > 160000) { break; } PfProfilerEndSample(); PfProfilerBeginSample("Neighbor consideration"); for (int i = 0; i < 8; i++) { uint num10 = (uint)(x2 + Directions[i]); uint num11 = (uint)(z2 + Directions[i + 8]); if (num10 >= mapSizeXField(__instance) || num11 >= mapSizeZField(__instance)) { continue; } int num12 = (int)num10; int num13 = (int)num11; int num14 = cellIndicesField(__instance).CellToIndex(num12, num13); if (calcGrid[num14].status == statusClosedValue && !flag8) { continue; } int num15 = 0; bool flag10 = false; if (!flag2 && new IntVec3(num12, 0, num13).GetTerrain(mapField(__instance)).HasTag("Water")) { continue; } if (!pathGridField(__instance).WalkableFast(num14)) { if (!flag) { continue; } flag10 = true; num15 += 70; Building building = edificeGrid[num14]; if (building == null || !PathFinder.IsDestroyable(building)) { continue; } num15 += (int)(building.HitPoints * 0.2f); } switch (i) { case 4: if (PathFinder.BlocksDiagonalMovement(curIndex - mapSizeXField(__instance), mapField(__instance))) { if (flag7) { continue; } num15 += 70; } if (PathFinder.BlocksDiagonalMovement(curIndex + 1, mapField(__instance))) { if (flag7) { continue; } num15 += 70; } break; case 5: if (PathFinder.BlocksDiagonalMovement(curIndex + mapSizeXField(__instance), mapField(__instance))) { if (flag7) { continue; } num15 += 70; } if (PathFinder.BlocksDiagonalMovement(curIndex + 1, mapField(__instance))) { if (flag7) { continue; } num15 += 70; } break; case 6: if (PathFinder.BlocksDiagonalMovement(curIndex + mapSizeXField(__instance), mapField(__instance))) { if (flag7) { continue; } num15 += 70; } if (PathFinder.BlocksDiagonalMovement(curIndex - 1, mapField(__instance))) { if (flag7) { continue; } num15 += 70; } break; case 7: if (PathFinder.BlocksDiagonalMovement(curIndex - mapSizeXField(__instance), mapField(__instance))) { if (flag7) { continue; } num15 += 70; } if (PathFinder.BlocksDiagonalMovement(curIndex - 1, mapField(__instance))) { if (flag7) { continue; } num15 += 70; } break; } int num16 = (i > 3) ? num9 : num8; num16 += num15; if (!flag10) { num16 += array[num14]; num16 = ((!flag9) ? (num16 + topGrid[num14].extraNonDraftedPerceivedPathCost) : (num16 + topGrid[num14].extraDraftedPerceivedPathCost)); } if (byteGrid != null) { num16 += byteGrid[num14] * 8; } if (allowedArea != null && !allowedArea[num14]) { num16 += 600; } if (flag5 && PawnUtility.AnyPawnBlockingPathAt(new IntVec3(num12, 0, num13), pawn, actAsIfHadCollideWithPawnsJob: false, collideOnlyWithStandingPawns: false, forPathFinder: true)) { num16 += 175; } Building building2 = edificeGridField(__instance)[num14]; if (building2 != null) { PfProfilerBeginSample("Edifices"); int buildingCost = PathFinder.GetBuildingCost(building2, traverseParms, pawn); if (buildingCost == int.MaxValue) { PfProfilerEndSample(); continue; } num16 += buildingCost; PfProfilerEndSample(); } List <Blueprint> list = blueprintGridField(__instance)[num14]; if (list != null) { PfProfilerBeginSample("Blueprints"); int num17 = 0; for (int j = 0; j < list.Count; j++) { num17 = Mathf.Max(num17, PathFinder.GetBlueprintCost(list[j], pawn)); } if (num17 == int.MaxValue) { PfProfilerEndSample(); continue; } num16 += num17; PfProfilerEndSample(); } int num18 = num16 + calcGrid[curIndex].knownCost; ushort status = calcGrid[num14].status; if (status == statusClosedValue || status == statusOpenValue) { int num19 = 0; if (status == statusClosedValue) { num19 = num8; } if (calcGrid[num14].knownCost <= num18 + num19) { continue; } } if (flag8) { calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)regionCostCalculator.GetPathCostFromDestToRegion(num14) * RegionHeuristicWeightByNodesOpened.Evaluate(num3)); if (calcGrid[num14].heuristicCost < 0) { Log.ErrorOnce(string.Concat("Heuristic cost overflow for ", pawn.ToStringSafe(), " pathing from ", start, " to ", dest, "."), pawn.GetHashCode() ^ 0xB8DC389); calcGrid[num14].heuristicCost = 0; } } else if (status != statusClosedValue && status != statusOpenValue) { int dx = Math.Abs(num12 - x); int dz = Math.Abs(num13 - z); int num20 = GenMath.OctileDistance(dx, dz, num8, num9); calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)num20 * num7); } int num21 = num18 + calcGrid[num14].heuristicCost; if (num21 < 0) { Log.ErrorOnce(string.Concat("Node cost overflow for ", pawn.ToStringSafe(), " pathing from ", start, " to ", dest, "."), pawn.GetHashCode() ^ 0x53CB9DE); num21 = 0; } calcGrid[num14].parentIndex = curIndex; calcGrid[num14].knownCost = num18; calcGrid[num14].status = statusOpenValue; calcGrid[num14].costNodeCost = num21; num3++; openList.Push(new CostNode2(num14, num21)); } PfProfilerEndSample(); num2++; calcGrid[curIndex].status = statusClosedValue; if (num3 >= num4 && flag6 && !flag8) { flag8 = true; regionCostCalculator.Init(destinationRect, traverseParms, num8, num9, byteGrid, allowedArea, flag9, disallowedCornerIndicesField(__instance)); InitStatusesAndPushStartNode3(ref curIndex, start, cellIndicesField(__instance), calcGrid, ref statusOpenValue, ref statusClosedValue); openList.Clear(); openList.Push(new CostNode2(curIndex, 0)); num3 = 0; num2 = 0; } } Log.Warning(string.Concat(pawn, " pathing from ", start, " to ", dest, " hit search limit of ", 160000, " cells.")); DebugDrawRichData(); PfProfilerEndSample(); PfProfilerEndSample(); __result = PawnPath.NotFound; return(false); }
private bool NeedNewPath() { if (!this.destination.IsValid || this.curPath == null || !this.curPath.Found || this.curPath.NodesLeftCount == 0) { return(true); } if (this.destination.HasThing && this.destination.Thing.Map != this.pawn.Map) { return(true); } if ((this.pawn.Position.InHorDistOf(this.curPath.LastNode, 15f) || this.pawn.Position.InHorDistOf(this.destination.Cell, 15f)) && !ReachabilityImmediate.CanReachImmediate(this.curPath.LastNode, this.destination, this.pawn.Map, this.peMode, this.pawn)) { return(true); } if (this.curPath.UsedRegionHeuristics && this.curPath.NodesConsumedCount >= 75) { return(true); } if (this.lastPathedTargetPosition != this.destination.Cell) { float num = (float)(this.pawn.Position - this.destination.Cell).LengthHorizontalSquared; float num2; if (num > 900f) { num2 = 10f; } else if (num > 289f) { num2 = 5f; } else if (num > 100f) { num2 = 3f; } else if (num > 49f) { num2 = 2f; } else { num2 = 0.5f; } if ((float)(this.lastPathedTargetPosition - this.destination.Cell).LengthHorizontalSquared > num2 * num2) { return(true); } } bool flag = PawnUtility.ShouldCollideWithPawns(this.pawn); bool flag2 = this.curPath.NodesLeftCount < 30; IntVec3 other = IntVec3.Invalid; int num3 = 0; while (num3 < 20 && num3 < this.curPath.NodesLeftCount) { IntVec3 intVec = this.curPath.Peek(num3); if (!intVec.Walkable(this.pawn.Map)) { return(true); } if (flag && !this.BestPathHadPawnsInTheWayRecently() && (PawnUtility.AnyPawnBlockingPathAt(intVec, this.pawn, false, true, false) || (flag2 && PawnUtility.AnyPawnBlockingPathAt(intVec, this.pawn, false, false, false)))) { return(true); } if (!this.BestPathHadDangerRecently() && PawnUtility.KnownDangerAt(intVec, this.pawn.Map, this.pawn)) { return(true); } Building_Door building_Door = intVec.GetEdifice(this.pawn.Map) as Building_Door; if (building_Door != null) { if (!building_Door.CanPhysicallyPass(this.pawn) && !this.pawn.HostileTo(building_Door)) { return(true); } if (building_Door.IsForbiddenToPass(this.pawn)) { return(true); } } if (num3 != 0 && intVec.AdjacentToDiagonal(other) && (PathFinder.BlocksDiagonalMovement(intVec.x, other.z, this.pawn.Map) || PathFinder.BlocksDiagonalMovement(other.x, intVec.z, this.pawn.Map))) { return(true); } other = intVec; num3++; } return(false); }
public PawnPath FindPath(IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, PathEndMode peMode = PathEndMode.OnCell) { if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } Pawn pawn = traverseParms.pawn; if (pawn != null && pawn.Map != map) { Log.Warning(String.Format("Map object was rebuilt, but new pather was not assigned to new map (new {0}) (old {1})", pawn.Map.uniqueID, map.uniqueID)); Log.Error("Tried to FindPath for pawn which is spawned in another map. His map PathFinder should have been used, not this one. pawn=" + pawn + " pawn.Map=" + pawn.Map + " map=" + map); return(PawnPath.NotFound); } if (!start.IsValid) { Log.Error("Tried to FindPath with invalid start " + start + ", pawn= " + pawn); return(PawnPath.NotFound); } if (!dest.IsValid) { Log.Error("Tried to FindPath with invalid dest " + dest + ", pawn= " + pawn); return(PawnPath.NotFound); } if (traverseParms.mode == TraverseMode.ByPawn) { if (!pawn.CanReach(dest, peMode, Danger.Deadly, traverseParms.canBash, traverseParms.mode)) { return(PawnPath.NotFound); } } else if (!map.reachability.CanReach(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound); } PfProfilerBeginSample("FindPath for " + pawn + " from " + start + " to " + dest + (dest.HasThing ? (" at " + dest.Cell) : "")); cellIndices = map.cellIndices; pathGrid = map.pathGrid; this.edificeGrid = map.edificeGrid.InnerArray; blueprintGrid = map.blueprintGrid.InnerArray; int x = dest.Cell.x; int z = dest.Cell.z; int curIndex = cellIndices.CellToIndex(start); int num = cellIndices.CellToIndex(dest.Cell); ByteGrid byteGrid = pawn?.GetAvoidGrid(); bool flag = traverseParms.mode == TraverseMode.PassAllDestroyableThings || traverseParms.mode == TraverseMode.PassAllDestroyableThingsNotWater; bool flag2 = traverseParms.mode != TraverseMode.NoPassClosedDoorsOrWater && traverseParms.mode != TraverseMode.PassAllDestroyableThingsNotWater; bool flag3 = !flag; CellRect cellRect = CalculateDestinationRect(dest, peMode); bool flag4 = cellRect.Width == 1 && cellRect.Height == 1; int[] array = map.pathGrid.pathGrid; TerrainDef[] topGrid = map.terrainGrid.topGrid; EdificeGrid edificeGrid = map.edificeGrid; int num2 = 0; int num3 = 0; Area allowedArea = GetAllowedArea(pawn); bool flag5 = pawn != null && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = (!flag && start.GetRegion(map) != null) & flag2; bool flag7 = !flag || !flag3; bool flag8 = false; bool flag9 = pawn?.Drafted ?? false; int num4 = (pawn?.IsColonist ?? false) ? 100000 : 2000; int num5 = 0; int num6 = 0; float num7 = DetermineHeuristicStrength(pawn, start, dest); // BEGIN CHANGED SECTION // In case pawn is null int num8 = 13; int num9 = 18; Dictionary <TerrainDef, int> pawnTerrainCacheCardinal = new Dictionary <TerrainDef, int>(); Dictionary <TerrainDef, int> pawnTerrainCacheDiagonal = new Dictionary <TerrainDef, int>(); Dictionary <TerrainDef, bool> pawnSpecialMovementCache = new Dictionary <TerrainDef, bool>(); Dictionary <TerrainDef, bool> pawnImpassibleMovementCache = new Dictionary <TerrainDef, bool>(); Dictionary <TerrainDef, int> pawnTerrainMovementCache = new Dictionary <TerrainDef, int>(); // END CHANGED SECTION CalculateAndAddDisallowedCorners(traverseParms, peMode, cellRect); InitStatusesAndPushStartNode(ref curIndex, start); while (true) { PfProfilerBeginSample("Open cell"); if (openList.Count <= 0) { string text = (pawn != null && pawn.CurJob != null) ? pawn.CurJob.ToString() : "null"; string text2 = (pawn != null && pawn.Faction != null) ? pawn.Faction.ToString() : "null"; Log.Warning(pawn + " pathing from " + start + " to " + dest + " ran out of cells to process.\nJob:" + text + "\nFaction: " + text2); DebugDrawRichData(); PfProfilerEndSample(); PfProfilerEndSample(); return(PawnPath.NotFound); } num5 += openList.Count; num6++; CostNode costNode = openList.Pop(); curIndex = costNode.index; if (costNode.cost != calcGrid[curIndex].costNodeCost) { PfProfilerEndSample(); continue; } if (calcGrid[curIndex].status == statusClosedValue) { PfProfilerEndSample(); continue; } IntVec3 c = cellIndices.IndexToCell(curIndex); int x2 = c.x; int z2 = c.z; if (flag4) { if (curIndex == num) { PfProfilerEndSample(); PawnPath result = FinalizedPath(curIndex, flag8); PfProfilerEndSample(); return(result); } } else if (cellRect.Contains(c) && !disallowedCornerIndices.Contains(curIndex)) { PfProfilerEndSample(); PawnPath result2 = FinalizedPath(curIndex, flag8); PfProfilerEndSample(); return(result2); } if (num2 > 160000) { break; } PfProfilerEndSample(); PfProfilerBeginSample("Neighbor consideration"); for (int i = 0; i < 8; i++) { uint num10 = (uint)(x2 + Directions[i]); uint num11 = (uint)(z2 + Directions[i + 8]); if (num10 >= mapSizeX || num11 >= mapSizeZ) { continue; } int num12 = (int)num10; int num13 = (int)num11; int num14 = cellIndices.CellToIndex(num12, num13); if (calcGrid[num14].status == statusClosedValue && !flag8) { continue; } // BEGIN CHANGED SECTION IntVec3 targetCell = new IntVec3(num12, 0, num13); TerrainDef targetTerrain = topGrid[num14]; if (pawn != null) { // Use cache of terrain movement indicators to avoid a lot of repeated computation if (!pawnImpassibleMovementCache.TryGetValue(targetTerrain, out bool impassible)) { impassible = pawn.kindDef.UnreachableTerrainCheck(targetTerrain); pawnImpassibleMovementCache[targetTerrain] = impassible; } if (impassible) { // Skip this cell for pathing calculations calcGrid[num14].status = statusClosedValue; continue; } // Overwrite directional move costs if (!pawnTerrainCacheCardinal.TryGetValue(targetTerrain, out num8)) { num8 = pawn.TerrainAwareTicksPerMoveCardinal(targetTerrain); pawnTerrainCacheCardinal[targetTerrain] = num8; } if (!pawnTerrainCacheDiagonal.TryGetValue(targetTerrain, out num9)) { num9 = pawn.TerrainAwareTicksPerMoveDiagonal(targetTerrain); pawnTerrainCacheDiagonal[targetTerrain] = num9; } } // END CHANGED SECTION int num15 = 0; bool flag10 = false; //if (!flag2 && new IntVec3(num12, 0, num13).GetTerrain(map).HasTag("Water")) if (!flag2 && targetTerrain.HasTag("Water")) { continue; } if (!pathGrid.WalkableFast(num14)) { if (!flag) { continue; } flag10 = true; num15 += 70; Building building = edificeGrid[num14]; if (building == null || !IsDestroyable(building)) { continue; } num15 += (int)((float)building.HitPoints * 0.2f); } switch (i) { case 4: if (BlocksDiagonalMovement(curIndex - mapSizeX)) { if (flag7) { continue; } num15 += 70; } if (BlocksDiagonalMovement(curIndex + 1)) { if (flag7) { continue; } num15 += 70; } break; case 5: if (BlocksDiagonalMovement(curIndex + mapSizeX)) { if (flag7) { continue; } num15 += 70; } if (BlocksDiagonalMovement(curIndex + 1)) { if (flag7) { continue; } num15 += 70; } break; case 6: if (BlocksDiagonalMovement(curIndex + mapSizeX)) { if (flag7) { continue; } num15 += 70; } if (BlocksDiagonalMovement(curIndex - 1)) { if (flag7) { continue; } num15 += 70; } break; case 7: if (BlocksDiagonalMovement(curIndex - mapSizeX)) { if (flag7) { continue; } num15 += 70; } if (BlocksDiagonalMovement(curIndex - 1)) { if (flag7) { continue; } num15 += 70; } break; } int num16 = (i > 3) ? num9 : num8; num16 += num15; if (!flag10) { // BEGIN CHANGED SECTION //num16 += array[num14]; //num16 = ((!flag9) ? (num16 + topGrid[num14].extraNonDraftedPerceivedPathCost) : (num16 + topGrid[num14].extraDraftedPerceivedPathCost)); if (pawn == null) { num16 += array[num14]; if (flag9) { num16 += targetTerrain.extraDraftedPerceivedPathCost; } else { num16 += targetTerrain.extraNonDraftedPerceivedPathCost; } } else { // Use cache of terrain perceived cost instead of fixed pathCost grid to avoid a lot of repeated computation while maintaining accuracy if (!pawnTerrainMovementCache.TryGetValue(targetTerrain, out int terrainMoveCost)) { terrainMoveCost = pawn.TerrainMoveCost(targetTerrain); pawnTerrainMovementCache[targetTerrain] = terrainMoveCost; } // This was really really expensive, so we opted to mod this pre-calced value //num16 += pathGrid.TerrainCalculatedCostAt(map, pawn, targetCell, true, IntVec3.Invalid, terrainMoveCost); num16 += pathGrid.ApplyTerrainModToCalculatedCost(targetTerrain, array[num14], terrainMoveCost); // Use cache of terrain movement indicators to avoid a lot of repeated computation if (!pawnSpecialMovementCache.TryGetValue(targetTerrain, out bool specialMovement)) { specialMovement = pawn.TerrainMoveStat(targetTerrain) != StatDefOf.MoveSpeed; pawnSpecialMovementCache[targetTerrain] = specialMovement; } // Skip applying the PerceivedPathCost hack if we've got a specialized speed stat for this terrain if (!specialMovement) { if (flag9) { num16 += targetTerrain.extraDraftedPerceivedPathCost; } else { num16 += targetTerrain.extraNonDraftedPerceivedPathCost; } } } // END CHANGED SECTION } if (byteGrid != null) { num16 += byteGrid[num14] * 8; } if (allowedArea != null && !allowedArea[num14]) { num16 += 600; } //new IntVec3(num12, 0, num13) -> targetCell if (flag5 && PawnUtility.AnyPawnBlockingPathAt(targetCell, pawn, actAsIfHadCollideWithPawnsJob: false, collideOnlyWithStandingPawns: false, forPathFinder: true)) { num16 += 175; } Building building2 = this.edificeGrid[num14]; if (building2 != null) { PfProfilerBeginSample("Edifices"); int buildingCost = GetBuildingCost(building2, traverseParms, pawn); if (buildingCost == int.MaxValue) { PfProfilerEndSample(); continue; } num16 += buildingCost; PfProfilerEndSample(); } List <Blueprint> list = blueprintGrid[num14]; if (list != null) { PfProfilerBeginSample("Blueprints"); int num17 = 0; for (int j = 0; j < list.Count; j++) { num17 = Mathf.Max(num17, GetBlueprintCost(list[j], pawn)); } if (num17 == int.MaxValue) { PfProfilerEndSample(); continue; } num16 += num17; PfProfilerEndSample(); } int num18 = num16 + calcGrid[curIndex].knownCost; ushort status = calcGrid[num14].status; if (status == statusClosedValue || status == statusOpenValue) { int num19 = 0; if (status == statusClosedValue) { num19 = num8; } if (calcGrid[num14].knownCost <= num18 + num19) { continue; } } if (flag8) { calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)regionCostCalculator.GetPathCostFromDestToRegion(num14) * RegionHeuristicWeightByNodesOpened.Evaluate(num3)); if (calcGrid[num14].heuristicCost < 0) { Log.ErrorOnce("Heuristic cost overflow for " + pawn.ToStringSafe() + " pathing from " + start + " to " + dest + ".", pawn.GetHashCode() ^ 0xB8DC389); calcGrid[num14].heuristicCost = 0; } } else if (status != statusClosedValue && status != statusOpenValue) { int dx = Math.Abs(num12 - x); int dz = Math.Abs(num13 - z); int num20 = GenMath.OctileDistance(dx, dz, num8, num9); calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)num20 * num7); } int num21 = num18 + calcGrid[num14].heuristicCost; if (num21 < 0) { Log.ErrorOnce("Node cost overflow for " + pawn.ToStringSafe() + " pathing from " + start + " to " + dest + ".", pawn.GetHashCode() ^ 0x53CB9DE); num21 = 0; } calcGrid[num14].parentIndex = curIndex; calcGrid[num14].knownCost = num18; calcGrid[num14].status = statusOpenValue; calcGrid[num14].costNodeCost = num21; num3++; openList.Push(new CostNode(num14, num21)); } PfProfilerEndSample(); num2++; calcGrid[curIndex].status = statusClosedValue; if (num3 >= num4 && flag6 && !flag8) { flag8 = true; regionCostCalculator.Init(cellRect, traverseParms, num8, num9, byteGrid, allowedArea, flag9, disallowedCornerIndices); InitStatusesAndPushStartNode(ref curIndex, start); num3 = 0; num2 = 0; } } Log.Warning(pawn + " pathing from " + start + " to " + dest + " hit search limit of " + 160000 + " cells."); DebugDrawRichData(); PfProfilerEndSample(); PfProfilerEndSample(); return(PawnPath.NotFound); }
private static IAttackTarget FindBestReachableMeleeTarget( Predicate <IAttackTarget> validator, Pawn searcherPawn, float maxTargDist, bool canBash) { maxTargDist = Mathf.Min(maxTargDist, 30f); IAttackTarget reachableTarget = (IAttackTarget)null; Func <IntVec3, IAttackTarget> bestTargetOnCell = (Func <IntVec3, IAttackTarget>)(x => { List <Thing> thingList = x.GetThingList(searcherPawn.Map); for (int index = 0; index < thingList.Count; ++index) { Thing thing = thingList[index]; if (thing is IAttackTarget target && validator(target) && ReachabilityImmediate.CanReachImmediate(x, (LocalTargetInfo)thing, searcherPawn.Map, PathEndMode.Touch, searcherPawn) && (searcherPawn.CanReachImmediate((LocalTargetInfo)thing, PathEndMode.Touch) || searcherPawn.Map.attackTargetReservationManager.CanReserve(searcherPawn, target))) { return(target); } } return((IAttackTarget)null); }); searcherPawn.Map.floodFiller.FloodFill(searcherPawn.Position, (Predicate <IntVec3>)(x => x.Walkable(searcherPawn.Map) && (double)x.DistanceToSquared(searcherPawn.Position) <= (double)maxTargDist * (double)maxTargDist && (canBash || !(x.GetEdifice(searcherPawn.Map) is Building_Door edifice) || edifice.CanPhysicallyPass(searcherPawn)) && !PawnUtility.AnyPawnBlockingPathAt(x, searcherPawn, true, false, false)), (Func <IntVec3, bool>)(x => { for (int index = 0; index < 8; ++index) { IntVec3 c = x + GenAdj.AdjacentCells[index]; if (c.InBounds(searcherPawn.Map)) { IAttackTarget attackTarget = bestTargetOnCell(c); if (attackTarget != null) { reachableTarget = attackTarget; break; } } } return(reachableTarget != null); }), int.MaxValue, false, (IEnumerable <IntVec3>)null); return(reachableTarget); }
public static bool TryFindBestPawnStandCell(ref bool __result, Pawn forPawn, out IntVec3 cell, bool cellByCell = false) { cell = IntVec3.Invalid; int num1 = -1; float radius = 10f; Dictionary <IntVec3, float> tmpDistances = new Dictionary <IntVec3, float>(); Dictionary <IntVec3, IntVec3> tmpParents = new Dictionary <IntVec3, IntVec3>(); DijkstraIntVec3 dijkstraIntVec3 = new DijkstraIntVec3(); while (true) { tmpDistances.Clear(); tmpParents.Clear(); dijkstraIntVec3.Run(forPawn.Position, (Func <IntVec3, IEnumerable <IntVec3> >)(x => GetAdjacentCardinalCellsForBestStandCell(x, radius, forPawn)), (Func <IntVec3, IntVec3, float>)((from, to) => { float num2 = 1f; if (from.x != to.x && from.z != to.z) { num2 = 1.414214f; } if (!to.Standable(forPawn.Map)) { num2 += 3f; } if (PawnUtility.AnyPawnBlockingPathAt(to, forPawn, false, false, false)) { if (to.GetThingList(forPawn.Map).Find((Predicate <Thing>)(x => x is Pawn && x.HostileTo((Thing)forPawn))) != null) { num2 += 40f; } else { num2 += 15f; } } if (to.GetEdifice(forPawn.Map) is Building_Door edifice && !edifice.FreePassage) { if (edifice.PawnCanOpen(forPawn)) { num2 += 6f; } else { num2 += 50f; } } return(num2); }), tmpDistances, tmpParents); if (tmpDistances.Count != num1) { float num2 = 0.0f; foreach (KeyValuePair <IntVec3, float> tmpDistance in tmpDistances) { if ((!cell.IsValid || (double)tmpDistance.Value < (double)num2) && (tmpDistance.Key.Walkable(forPawn.Map) && !PawnUtility.AnyPawnBlockingPathAt(tmpDistance.Key, forPawn, false, false, false))) { Building_Door door = tmpDistance.Key.GetDoor(forPawn.Map); if (door == null || door.FreePassage) { cell = tmpDistance.Key; num2 = tmpDistance.Value; } } } if (!cell.IsValid) { if ((double)radius <= (double)forPawn.Map.Size.x || (double)radius <= (double)forPawn.Map.Size.z) { radius *= 2f; num1 = tmpDistances.Count; } else { goto label_23; } } else { goto label_11; } } else { break; } } __result = false; return(false); label_11: if (!cellByCell) { __result = true; return(false); } IntVec3 c = cell; int num3 = 0; while (c != null && c.IsValid && c != forPawn.Position) { ++num3; if (num3 >= 10000) { Log.Error("Too many iterations.", false); break; } if (c.Walkable(forPawn.Map)) { Building_Door door = c.GetDoor(forPawn.Map); if (door == null || door.FreePassage) { cell = c; } } if (!tmpParents.TryGetValue(c, out c)) { break; } } __result = true; return(false); label_23: __result = false; return(false); }
public PawnPath FindPath(IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, PathEndMode peMode = PathEndMode.OnCell) { if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } Pawn pawn = traverseParms.pawn; if (pawn != null && pawn.Map != this.map) { Log.Error(string.Concat(new object[] { "Tried to FindPath for pawn which is spawned in another map. His map PathFinder should have been used, not this one. pawn=", pawn, " pawn.Map=", pawn.Map, " map=", this.map }), false); return(PawnPath.NotFound); } if (!start.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindPath with invalid start ", start, ", pawn= ", pawn }), false); return(PawnPath.NotFound); } if (!dest.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindPath with invalid dest ", dest, ", pawn= ", pawn }), false); return(PawnPath.NotFound); } if (traverseParms.mode == TraverseMode.ByPawn) { if (!pawn.CanReach(dest, peMode, Danger.Deadly, traverseParms.canBash, traverseParms.mode)) { return(PawnPath.NotFound); } } else if (!this.map.reachability.CanReach(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound); } this.PfProfilerBeginSample(string.Concat(new object[] { "FindPath for ", pawn, " from ", start, " to ", dest, (!dest.HasThing) ? string.Empty : (" at " + dest.Cell) })); this.cellIndices = this.map.cellIndices; this.pathGrid = this.map.pathGrid; this.edificeGrid = this.map.edificeGrid.InnerArray; this.blueprintGrid = this.map.blueprintGrid.InnerArray; int x = dest.Cell.x; int z = dest.Cell.z; int num = this.cellIndices.CellToIndex(start); int num2 = this.cellIndices.CellToIndex(dest.Cell); ByteGrid byteGrid = (pawn == null) ? null : pawn.GetAvoidGrid(); bool flag = traverseParms.mode == TraverseMode.PassAllDestroyableThings || traverseParms.mode == TraverseMode.PassAllDestroyableThingsNotWater; bool flag2 = traverseParms.mode != TraverseMode.NoPassClosedDoorsOrWater && traverseParms.mode != TraverseMode.PassAllDestroyableThingsNotWater; bool flag3 = !flag; CellRect cellRect = this.CalculateDestinationRect(dest, peMode); bool flag4 = cellRect.Width == 1 && cellRect.Height == 1; int[] array = this.map.pathGrid.pathGrid; TerrainDef[] topGrid = this.map.terrainGrid.topGrid; EdificeGrid edificeGrid = this.map.edificeGrid; int num3 = 0; int num4 = 0; Area allowedArea = this.GetAllowedArea(pawn); bool flag5 = pawn != null && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = true && DebugViewSettings.drawPaths; bool flag7 = !flag && start.GetRegion(this.map, RegionType.Set_Passable) != null && flag2; bool flag8 = !flag || !flag3; bool flag9 = false; bool flag10 = pawn != null && pawn.Drafted; bool flag11 = pawn != null && pawn.IsColonist; int num5 = (!flag11) ? 2000 : 100000; int num6 = 0; int num7 = 0; float num8 = this.DetermineHeuristicStrength(pawn, start, dest); int num9; int num10; if (pawn != null) { num9 = pawn.TicksPerMoveCardinal; num10 = pawn.TicksPerMoveDiagonal; } else { num9 = 13; num10 = 18; } this.CalculateAndAddDisallowedCorners(traverseParms, peMode, cellRect); this.InitStatusesAndPushStartNode(ref num, start); for (;;) { this.PfProfilerBeginSample("Open cell"); if (this.openList.Count <= 0) { break; } num6 += this.openList.Count; num7++; PathFinder.CostNode costNode = this.openList.Pop(); num = costNode.index; if (costNode.cost != this.calcGrid[num].costNodeCost) { this.PfProfilerEndSample(); } else if (this.calcGrid[num].status == this.statusClosedValue) { this.PfProfilerEndSample(); } else { IntVec3 c = this.cellIndices.IndexToCell(num); int x2 = c.x; int z2 = c.z; if (flag6) { this.DebugFlash(c, (float)this.calcGrid[num].knownCost / 1500f, this.calcGrid[num].knownCost.ToString()); } if (flag4) { if (num == num2) { goto Block_32; } } else if (cellRect.Contains(c) && !this.disallowedCornerIndices.Contains(num)) { goto Block_34; } if (num3 > 160000) { goto Block_35; } this.PfProfilerEndSample(); this.PfProfilerBeginSample("Neighbor consideration"); for (int i = 0; i < 8; i++) { uint num11 = (uint)(x2 + PathFinder.Directions[i]); uint num12 = (uint)(z2 + PathFinder.Directions[i + 8]); if ((ulong)num11 < (ulong)((long)this.mapSizeX) && (ulong)num12 < (ulong)((long)this.mapSizeZ)) { int num13 = (int)num11; int num14 = (int)num12; int num15 = this.cellIndices.CellToIndex(num13, num14); if (this.calcGrid[num15].status != this.statusClosedValue || flag9) { int num16 = 0; bool flag12 = false; if (flag2 || !new IntVec3(num13, 0, num14).GetTerrain(this.map).HasTag("Water")) { if (!this.pathGrid.WalkableFast(num15)) { if (!flag) { if (flag6) { this.DebugFlash(new IntVec3(num13, 0, num14), 0.22f, "walk"); } goto IL_D53; } flag12 = true; num16 += 70; Building building = edificeGrid[num15]; if (building == null) { goto IL_D53; } if (!PathFinder.IsDestroyable(building)) { goto IL_D53; } num16 += (int)((float)building.HitPoints * 0.2f); } if (i > 3) { switch (i) { case 4: if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } break; case 5: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } break; case 6: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } break; case 7: if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } goto IL_D53; } num16 += 70; } break; } } int num17 = (i <= 3) ? num9 : num10; num17 += num16; if (!flag12) { num17 += array[num15]; if (flag10) { num17 += topGrid[num15].extraDraftedPerceivedPathCost; } else { num17 += topGrid[num15].extraNonDraftedPerceivedPathCost; } } if (byteGrid != null) { num17 += (int)(byteGrid[num15] * 8); } if (allowedArea != null && !allowedArea[num15]) { num17 += 600; } if (flag5 && PawnUtility.AnyPawnBlockingPathAt(new IntVec3(num13, 0, num14), pawn, false, false, true)) { num17 += 175; } Building building2 = this.edificeGrid[num15]; if (building2 != null) { this.PfProfilerBeginSample("Edifices"); int buildingCost = PathFinder.GetBuildingCost(building2, traverseParms, pawn); if (buildingCost == 2147483647) { this.PfProfilerEndSample(); goto IL_D53; } num17 += buildingCost; this.PfProfilerEndSample(); } List <Blueprint> list = this.blueprintGrid[num15]; if (list != null) { this.PfProfilerBeginSample("Blueprints"); int num18 = 0; for (int j = 0; j < list.Count; j++) { num18 = Mathf.Max(num18, PathFinder.GetBlueprintCost(list[j], pawn)); } if (num18 == 2147483647) { this.PfProfilerEndSample(); goto IL_D53; } num17 += num18; this.PfProfilerEndSample(); } int num19 = num17 + this.calcGrid[num].knownCost; ushort status = this.calcGrid[num15].status; if (status == this.statusClosedValue || status == this.statusOpenValue) { int num20 = 0; if (status == this.statusClosedValue) { num20 = num9; } if (this.calcGrid[num15].knownCost <= num19 + num20) { goto IL_D53; } } if (status != this.statusClosedValue && status != this.statusOpenValue) { if (flag9) { this.calcGrid[num15].heuristicCost = Mathf.RoundToInt((float)this.regionCostCalculator.GetPathCostFromDestToRegion(num15) * PathFinder.RegionHeuristicWeightByNodesOpened.Evaluate((float)num4)); } else { int dx = Math.Abs(num13 - x); int dz = Math.Abs(num14 - z); int num21 = GenMath.OctileDistance(dx, dz, num9, num10); this.calcGrid[num15].heuristicCost = Mathf.RoundToInt((float)num21 * num8); } } int num22 = num19 + this.calcGrid[num15].heuristicCost; this.calcGrid[num15].parentIndex = num; this.calcGrid[num15].knownCost = num19; this.calcGrid[num15].status = this.statusOpenValue; this.calcGrid[num15].costNodeCost = num22; num4++; this.openList.Push(new PathFinder.CostNode(num15, num22)); } } } IL_D53 :; } this.PfProfilerEndSample(); num3++; this.calcGrid[num].status = this.statusClosedValue; if (num4 >= num5 && flag7 && !flag9) { flag9 = true; this.regionCostCalculator.Init(cellRect, traverseParms, num9, num10, byteGrid, allowedArea, flag10, this.disallowedCornerIndices); this.InitStatusesAndPushStartNode(ref num, start); num4 = 0; num3 = 0; } } } string text = (pawn == null || pawn.CurJob == null) ? "null" : pawn.CurJob.ToString(); string text2 = (pawn == null || pawn.Faction == null) ? "null" : pawn.Faction.ToString(); Log.Warning(string.Concat(new object[] { pawn, " pathing from ", start, " to ", dest, " ran out of cells to process.\nJob:", text, "\nFaction: ", text2 }), false); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); Block_32: this.PfProfilerEndSample(); PawnPath result = this.FinalizedPath(num, flag9); this.PfProfilerEndSample(); return(result); Block_34: this.PfProfilerEndSample(); PawnPath result2 = this.FinalizedPath(num, flag9); this.PfProfilerEndSample(); return(result2); Block_35: Log.Warning(string.Concat(new object[] { pawn, " pathing from ", start, " to ", dest, " hit search limit of ", 160000, " cells." }), false); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); }
public PawnPath FindPath(IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, PathEndMode peMode = PathEndMode.OnCell) { if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } Pawn pawn = traverseParms.pawn; if (pawn != null && pawn.Map != this.map) { Log.Error("Tried to FindPath for pawn which is spawned in another map. His map PathFinder should have been used, not this one. pawn=" + pawn + " pawn.Map=" + pawn.Map + " map=" + this.map); return(PawnPath.NotFound); } if (!start.IsValid) { Log.Error("Tried to FindPath with invalid start " + start + ", pawn= " + pawn); return(PawnPath.NotFound); } if (!dest.IsValid) { Log.Error("Tried to FindPath with invalid dest " + dest + ", pawn= " + pawn); return(PawnPath.NotFound); } if (traverseParms.mode == TraverseMode.ByPawn) { if (!pawn.CanReach(dest, peMode, Danger.Deadly, traverseParms.canBash, traverseParms.mode)) { return(PawnPath.NotFound); } } else if (!this.map.reachability.CanReach(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound); } this.PfProfilerBeginSample("FindPath for " + pawn + " from " + start + " to " + dest + ((!dest.HasThing) ? string.Empty : (" at " + dest.Cell))); this.cellIndices = this.map.cellIndices; this.pathGrid = this.map.pathGrid; this.edificeGrid = this.map.edificeGrid.InnerArray; IntVec3 cell = dest.Cell; int x = cell.x; IntVec3 cell2 = dest.Cell; int z = cell2.z; int num = this.cellIndices.CellToIndex(start); int num2 = this.cellIndices.CellToIndex(dest.Cell); ByteGrid byteGrid = (pawn == null) ? null : pawn.GetAvoidGrid(); bool flag = traverseParms.mode == TraverseMode.PassAllDestroyableThings; bool flag2 = traverseParms.mode != TraverseMode.NoPassClosedDoorsOrWater && traverseParms.mode != TraverseMode.PassAllDestroyableThingsNotWater; bool flag3 = !flag; CellRect cellRect = this.CalculateDestinationRect(dest, peMode); bool flag4 = cellRect.Width == 1 && cellRect.Height == 1; int[] array = this.map.pathGrid.pathGrid; EdificeGrid edificeGrid = this.map.edificeGrid; int num3 = 0; int num4 = 0; Area allowedArea = this.GetAllowedArea(pawn); bool flag5 = pawn != null && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = true && DebugViewSettings.drawPaths; bool flag7 = !flag && start.GetRegion(this.map, RegionType.Set_Passable) != null; bool flag8 = !flag || !flag3; bool flag9 = false; int num5 = 0; int num6 = 0; float num7 = this.DetermineHeuristicStrength(pawn, start, dest); int num8; int num9; if (pawn != null) { num8 = pawn.TicksPerMoveCardinal; num9 = pawn.TicksPerMoveDiagonal; } else { num8 = 13; num9 = 18; } this.CalculateAndAddDisallowedCorners(traverseParms, peMode, cellRect); this.InitStatusesAndPushStartNode(ref num, start); while (true) { this.PfProfilerBeginSample("Open cell"); if (this.openList.Count <= 0) { string text = (pawn == null || pawn.CurJob == null) ? "null" : pawn.CurJob.ToString(); string text2 = (pawn == null || pawn.Faction == null) ? "null" : pawn.Faction.ToString(); Log.Warning(pawn + " pathing from " + start + " to " + dest + " ran out of cells to process.\nJob:" + text + "\nFaction: " + text2); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); } num5 += this.openList.Count; num6++; CostNode costNode = this.openList.Pop(); num = costNode.index; if (costNode.cost != this.calcGrid[num].costNodeCost) { this.PfProfilerEndSample(); continue; } if (this.calcGrid[num].status == this.statusClosedValue) { this.PfProfilerEndSample(); continue; } IntVec3 c = this.cellIndices.IndexToCell(num); int x2 = c.x; int z2 = c.z; if (flag6) { this.DebugFlash(c, (float)((float)this.calcGrid[num].knownCost / 1500.0), this.calcGrid[num].knownCost.ToString()); } if (flag4) { if (num == num2) { this.PfProfilerEndSample(); PawnPath result = this.FinalizedPath(num); this.PfProfilerEndSample(); return(result); } } else if (cellRect.Contains(c) && !this.disallowedCornerIndices.Contains(num)) { this.PfProfilerEndSample(); PawnPath result2 = this.FinalizedPath(num); this.PfProfilerEndSample(); return(result2); } if (num3 <= 160000) { this.PfProfilerEndSample(); this.PfProfilerBeginSample("Neighbor consideration"); for (int i = 0; i < 8; i++) { uint num10 = (uint)(x2 + PathFinder.Directions[i]); uint num11 = (uint)(z2 + PathFinder.Directions[i + 8]); int num12; int num13; int num14; bool flag10; int num15; if (num10 < this.mapSizeX && num11 < this.mapSizeZ) { num12 = (int)num10; num13 = (int)num11; num14 = this.cellIndices.CellToIndex(num12, num13); if (this.calcGrid[num14].status == this.statusClosedValue && !flag9) { continue; } num15 = 0; flag10 = false; if (!flag2 && new IntVec3(num12, 0, num13).GetTerrain(this.map).HasTag("Water")) { continue; } if (!this.pathGrid.WalkableFast(num14)) { if (!flag) { if (flag6) { this.DebugFlash(new IntVec3(num12, 0, num13), 0.22f, "walk"); } } else { flag10 = true; num15 += 70; Building building = edificeGrid[num14]; if (building != null && PathFinder.IsDestroyable(building)) { num15 += (int)((float)building.HitPoints * 0.10999999940395355); goto IL_0749; } } continue; } goto IL_0749; } continue; IL_092b: if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } continue; } num15 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } continue; } num15 += 70; } goto IL_09bf; IL_0b0e: ushort status; if (status != this.statusClosedValue && status != this.statusOpenValue) { if (flag9) { this.calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)this.regionCostCalculator.GetPathCostFromDestToRegion(num14) * PathFinder.RegionHeuristicWeightByNodesOpened.Evaluate((float)num4)); } else { int dx = Math.Abs(num12 - x); int dz = Math.Abs(num13 - z); int num16 = GenMath.OctileDistance(dx, dz, num8, num9); this.calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)num16 * num7); } } int num17; int num18 = num17 + this.calcGrid[num14].heuristicCost; this.calcGrid[num14].parentIndex = num; this.calcGrid[num14].knownCost = num17; this.calcGrid[num14].status = this.statusOpenValue; this.calcGrid[num14].costNodeCost = num18; num4++; this.openList.Push(new CostNode(num14, num18)); continue; IL_0803: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } continue; } num15 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } continue; } num15 += 70; } goto IL_09bf; IL_09bf: int num19 = (i <= 3) ? num8 : num9; num19 += num15; if (!flag10) { num19 += array[num14]; } if (byteGrid != null) { num19 += byteGrid[num14] * 8; } if (allowedArea != null && !allowedArea[num14]) { num19 += 600; } if (flag5 && PawnUtility.AnyPawnBlockingPathAt(new IntVec3(num12, 0, num13), pawn, false, false)) { num19 += 175; } Building building2 = this.edificeGrid[num14]; if (building2 != null) { this.PfProfilerBeginSample("Edifices"); int buildingCost = PathFinder.GetBuildingCost(building2, traverseParms, pawn); if (buildingCost == 2147483647) { this.PfProfilerEndSample(); continue; } num19 += buildingCost; this.PfProfilerEndSample(); } num17 = num19 + this.calcGrid[num].knownCost; status = this.calcGrid[num14].status; if (status != this.statusClosedValue && status != this.statusOpenValue) { goto IL_0b0e; } int num20 = 0; if (status == this.statusClosedValue) { num20 = num8; } if (this.calcGrid[num14].knownCost > num17 + num20) { goto IL_0b0e; } continue; IL_0897: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } continue; } num15 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } continue; } num15 += 70; } goto IL_09bf; IL_0749: switch (i) { case 4: break; case 5: goto IL_0803; case 6: goto IL_0897; case 7: goto IL_092b; default: goto IL_09bf; } if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } continue; } num15 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } continue; } num15 += 70; } goto IL_09bf; } this.PfProfilerEndSample(); num3++; this.calcGrid[num].status = this.statusClosedValue; if (num4 >= 2000 && flag7 && !flag9) { flag9 = true; this.regionCostCalculator.Init(cellRect, traverseParms, num8, num9, byteGrid, allowedArea, this.disallowedCornerIndices); this.InitStatusesAndPushStartNode(ref num, start); num4 = 0; num3 = 0; } continue; } break; } Log.Warning(pawn + " pathing from " + start + " to " + dest + " hit search limit of " + 160000 + " cells."); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); }
public PawnPath FindPath(IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, PathEndMode peMode = PathEndMode.OnCell) { if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } Pawn pawn = traverseParms.pawn; if (pawn != null && pawn.Map != map) { Log.Error("Tried to FindPath for pawn which is spawned in another map. His map PathFinder should have been used, not this one. pawn=" + pawn + " pawn.Map=" + pawn.Map + " map=" + map); return(PawnPath.NotFound); } if (!start.IsValid) { Log.Error("Tried to FindPath with invalid start " + start + ", pawn= " + pawn); return(PawnPath.NotFound); } if (!dest.IsValid) { Log.Error("Tried to FindPath with invalid dest " + dest + ", pawn= " + pawn); return(PawnPath.NotFound); } if (traverseParms.mode == TraverseMode.ByPawn) { if (!pawn.CanReach(dest, peMode, Danger.Deadly, traverseParms.canBash, traverseParms.mode)) { return(PawnPath.NotFound); } } else if (!map.reachability.CanReach(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound); } PfProfilerBeginSample("FindPath for " + pawn + " from " + start + " to " + dest + ((!dest.HasThing) ? string.Empty : (" at " + dest.Cell))); cellIndices = map.cellIndices; pathGrid = map.pathGrid; this.edificeGrid = map.edificeGrid.InnerArray; blueprintGrid = map.blueprintGrid.InnerArray; IntVec3 cell = dest.Cell; int x = cell.x; IntVec3 cell2 = dest.Cell; int z = cell2.z; int curIndex = cellIndices.CellToIndex(start); int num = cellIndices.CellToIndex(dest.Cell); ByteGrid byteGrid = pawn?.GetAvoidGrid(); bool flag = traverseParms.mode == TraverseMode.PassAllDestroyableThings || traverseParms.mode == TraverseMode.PassAllDestroyableThingsNotWater; bool flag2 = traverseParms.mode != TraverseMode.NoPassClosedDoorsOrWater && traverseParms.mode != TraverseMode.PassAllDestroyableThingsNotWater; bool flag3 = !flag; CellRect cellRect = CalculateDestinationRect(dest, peMode); bool flag4 = cellRect.Width == 1 && cellRect.Height == 1; int[] array = map.pathGrid.pathGrid; TerrainDef[] topGrid = map.terrainGrid.topGrid; EdificeGrid edificeGrid = map.edificeGrid; int num2 = 0; int num3 = 0; Area allowedArea = GetAllowedArea(pawn); bool flag5 = pawn != null && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = true && DebugViewSettings.drawPaths; bool flag7 = !flag && start.GetRegion(map) != null && flag2; bool flag8 = !flag || !flag3; bool flag9 = false; bool flag10 = pawn?.Drafted ?? false; int num4 = (!(pawn?.IsColonist ?? false)) ? 2000 : 100000; int num5 = 0; int num6 = 0; float num7 = DetermineHeuristicStrength(pawn, start, dest); int num8; int num9; if (pawn != null) { num8 = pawn.TicksPerMoveCardinal; num9 = pawn.TicksPerMoveDiagonal; } else { num8 = 13; num9 = 18; } CalculateAndAddDisallowedCorners(traverseParms, peMode, cellRect); InitStatusesAndPushStartNode(ref curIndex, start); while (true) { PfProfilerBeginSample("Open cell"); if (openList.Count <= 0) { string text = (pawn == null || pawn.CurJob == null) ? "null" : pawn.CurJob.ToString(); string text2 = (pawn == null || pawn.Faction == null) ? "null" : pawn.Faction.ToString(); Log.Warning(pawn + " pathing from " + start + " to " + dest + " ran out of cells to process.\nJob:" + text + "\nFaction: " + text2); DebugDrawRichData(); PfProfilerEndSample(); return(PawnPath.NotFound); } num5 += openList.Count; num6++; CostNode costNode = openList.Pop(); curIndex = costNode.index; if (costNode.cost != calcGrid[curIndex].costNodeCost) { PfProfilerEndSample(); } else if (calcGrid[curIndex].status == statusClosedValue) { PfProfilerEndSample(); } else { IntVec3 c = cellIndices.IndexToCell(curIndex); int x2 = c.x; int z2 = c.z; if (flag6) { DebugFlash(c, (float)calcGrid[curIndex].knownCost / 1500f, calcGrid[curIndex].knownCost.ToString()); } if (flag4) { if (curIndex == num) { PfProfilerEndSample(); PawnPath result = FinalizedPath(curIndex, flag9); PfProfilerEndSample(); return(result); } } else if (cellRect.Contains(c) && !disallowedCornerIndices.Contains(curIndex)) { PfProfilerEndSample(); PawnPath result2 = FinalizedPath(curIndex, flag9); PfProfilerEndSample(); return(result2); } if (num2 > 160000) { break; } PfProfilerEndSample(); PfProfilerBeginSample("Neighbor consideration"); for (int i = 0; i < 8; i++) { uint num10 = (uint)(x2 + Directions[i]); uint num11 = (uint)(z2 + Directions[i + 8]); if (num10 < mapSizeX && num11 < mapSizeZ) { int num12 = (int)num10; int num13 = (int)num11; int num14 = cellIndices.CellToIndex(num12, num13); if (calcGrid[num14].status != statusClosedValue || flag9) { int num15 = 0; bool flag11 = false; if (flag2 || !new IntVec3(num12, 0, num13).GetTerrain(map).HasTag("Water")) { if (!pathGrid.WalkableFast(num14)) { if (!flag) { if (flag6) { DebugFlash(new IntVec3(num12, 0, num13), 0.22f, "walk"); } continue; } flag11 = true; num15 += 70; Building building = edificeGrid[num14]; if (building == null || !IsDestroyable(building)) { continue; } num15 += (int)((float)building.HitPoints * 0.2f); } switch (i) { case 4: if (BlocksDiagonalMovement(curIndex - mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } break; } num15 += 70; } if (BlocksDiagonalMovement(curIndex + 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } break; } num15 += 70; } goto default; case 5: if (BlocksDiagonalMovement(curIndex + mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } break; } num15 += 70; } if (BlocksDiagonalMovement(curIndex + 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "corn"); } break; } num15 += 70; } goto default; case 6: if (BlocksDiagonalMovement(curIndex + mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "corn"); } break; } num15 += 70; } if (BlocksDiagonalMovement(curIndex - 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } break; } num15 += 70; } goto default; case 7: if (BlocksDiagonalMovement(curIndex - mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "corn"); } break; } num15 += 70; } if (BlocksDiagonalMovement(curIndex - 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "corn"); } break; } num15 += 70; } goto default; default: { int num16 = (i <= 3) ? num8 : num9; num16 += num15; if (!flag11) { num16 += array[num14]; num16 = ((!flag10) ? (num16 + topGrid[num14].extraNonDraftedPerceivedPathCost) : (num16 + topGrid[num14].extraDraftedPerceivedPathCost)); } if (byteGrid != null) { num16 += byteGrid[num14] * 8; } if (allowedArea != null && !allowedArea[num14]) { num16 += 600; } if (flag5 && PawnUtility.AnyPawnBlockingPathAt(new IntVec3(num12, 0, num13), pawn, actAsIfHadCollideWithPawnsJob: false, collideOnlyWithStandingPawns: false, forPathFinder: true)) { num16 += 175; } Building building2 = this.edificeGrid[num14]; if (building2 != null) { PfProfilerBeginSample("Edifices"); int buildingCost = GetBuildingCost(building2, traverseParms, pawn); if (buildingCost == 2147483647) { PfProfilerEndSample(); break; } num16 += buildingCost; PfProfilerEndSample(); } List <Blueprint> list = blueprintGrid[num14]; if (list != null) { PfProfilerBeginSample("Blueprints"); int num17 = 0; for (int j = 0; j < list.Count; j++) { num17 = Mathf.Max(num17, GetBlueprintCost(list[j], pawn)); } if (num17 == 2147483647) { PfProfilerEndSample(); break; } num16 += num17; PfProfilerEndSample(); } int num18 = num16 + calcGrid[curIndex].knownCost; ushort status = calcGrid[num14].status; if (status == statusClosedValue || status == statusOpenValue) { int num19 = 0; if (status == statusClosedValue) { num19 = num8; } if (calcGrid[num14].knownCost <= num18 + num19) { break; } } if (flag9) { calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)regionCostCalculator.GetPathCostFromDestToRegion(num14) * RegionHeuristicWeightByNodesOpened.Evaluate((float)num3)); if (calcGrid[num14].heuristicCost < 0) { Log.ErrorOnce("Heuristic cost overflow for " + pawn.ToStringSafe() + " pathing from " + start + " to " + dest + ".", pawn.GetHashCode() ^ 0xB8DC389); calcGrid[num14].heuristicCost = 0; } } else if (status != statusClosedValue && status != statusOpenValue) { int dx = Math.Abs(num12 - x); int dz = Math.Abs(num13 - z); int num20 = GenMath.OctileDistance(dx, dz, num8, num9); calcGrid[num14].heuristicCost = Mathf.RoundToInt((float)num20 * num7); } int num21 = num18 + calcGrid[num14].heuristicCost; if (num21 < 0) { Log.ErrorOnce("Node cost overflow for " + pawn.ToStringSafe() + " pathing from " + start + " to " + dest + ".", pawn.GetHashCode() ^ 0x53CB9DE); num21 = 0; } calcGrid[num14].parentIndex = curIndex; calcGrid[num14].knownCost = num18; calcGrid[num14].status = statusOpenValue; calcGrid[num14].costNodeCost = num21; num3++; openList.Push(new CostNode(num14, num21)); break; } } } } } } PfProfilerEndSample(); num2++; calcGrid[curIndex].status = statusClosedValue; if (num3 >= num4 && flag7 && !flag9) { flag9 = true; regionCostCalculator.Init(cellRect, traverseParms, num8, num9, byteGrid, allowedArea, flag10, disallowedCornerIndices); InitStatusesAndPushStartNode(ref curIndex, start); num3 = 0; num2 = 0; } } } Log.Warning(pawn + " pathing from " + start + " to " + dest + " hit search limit of " + 160000 + " cells."); DebugDrawRichData(); PfProfilerEndSample(); return(PawnPath.NotFound); }
public static bool TryFindBestPawnStandCell(Pawn forPawn, out IntVec3 cell, bool cellByCell = false) { cell = IntVec3.Invalid; int num = -1; float radius = 10f; while (true) { tmpDistances.Clear(); tmpParents.Clear(); Dijkstra <IntVec3> .Run(forPawn.Position, (IntVec3 x) => GetAdjacentCardinalCellsForBestStandCell(x, radius, forPawn), delegate(IntVec3 from, IntVec3 to) { float num4 = 1f; if (from.x != to.x && from.z != to.z) { num4 = 1.41421354f; } if (!to.Standable(forPawn.Map)) { num4 += 3f; } if (PawnUtility.AnyPawnBlockingPathAt(to, forPawn)) { num4 = ((to.GetThingList(forPawn.Map).Find((Thing x) => x is Pawn && x.HostileTo(forPawn)) == null) ? (num4 + 15f) : (num4 + 40f)); } Building_Door building_Door = to.GetEdifice(forPawn.Map) as Building_Door; if (building_Door != null && !building_Door.FreePassage) { num4 = ((!building_Door.PawnCanOpen(forPawn)) ? (num4 + 50f) : (num4 + 6f)); } return(num4); }, tmpDistances, tmpParents); if (tmpDistances.Count == num) { return(false); } float num2 = 0f; foreach (KeyValuePair <IntVec3, float> tmpDistance in tmpDistances) { if ((!cell.IsValid || !(tmpDistance.Value >= num2)) && tmpDistance.Key.Walkable(forPawn.Map) && !PawnUtility.AnyPawnBlockingPathAt(tmpDistance.Key, forPawn)) { Building_Door door = tmpDistance.Key.GetDoor(forPawn.Map); if (door == null || door.FreePassage) { cell = tmpDistance.Key; num2 = tmpDistance.Value; } } } if (cell.IsValid) { if (!cellByCell) { return(true); } IntVec3 intVec = cell; int num3 = 0; while (intVec.IsValid && intVec != forPawn.Position) { num3++; if (num3 >= 10000) { Log.Error("Too many iterations."); break; } if (intVec.Walkable(forPawn.Map)) { Building_Door door2 = intVec.GetDoor(forPawn.Map); if (door2 == null || door2.FreePassage) { cell = intVec; } } intVec = tmpParents[intVec]; } return(true); } if (radius > (float)forPawn.Map.Size.x && radius > (float)forPawn.Map.Size.z) { break; } radius *= 2f; num = tmpDistances.Count; } return(false); }
public PawnPath FindShipPath(IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, PathEndMode peMode = PathEndMode.OnCell) { if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } Pawn pawn = traverseParms.pawn; if (!(pawn is null) && pawn.Map != this.map) { Log.Error(string.Concat(new object[] { "Tried to FindShipPath for pawn which is spawned in another map. his map ShipPathFinder should have been used, not this one. " + "pawn=", pawn, " pawn.Map=", pawn.Map, " map=", this.map }), false); return(PawnPath.NotFound); } if (!start.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindShipPath with invalid start ", start, ", pawn=", pawn }), false); return(PawnPath.NotFound); } if (!dest.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindPath with invalid dest ", dest, ", pawn= ", pawn }), false); return(PawnPath.NotFound); } if (traverseParms.mode == TraverseMode.ByPawn) { if (!ShipReachabilityUtility.CanReachShip(pawn, dest, peMode, Danger.Deadly, false, traverseParms.mode)) { return(PawnPath.NotFound); } } else if (!mapE.getShipReachability.CanReachShip(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound); } this.PfProfilerBeginSample(string.Concat(new object[] { "FindPath for ", pawn, " from ", start, " to ", dest, (!dest.HasThing) ? string.Empty : (" at " + dest.Cell) })); this.cellIndices = this.map.cellIndices; this.shipPathGrid = mapE.getShipPathGrid; this.edificeGrid = this.map.edificeGrid.InnerArray; this.blueprintGrid = this.map.blueprintGrid.InnerArray; int x = dest.Cell.x; int z = dest.Cell.z; int num = this.cellIndices.CellToIndex(start); int num2 = this.cellIndices.CellToIndex(dest.Cell); ByteGrid byteGrid = (pawn is null) ? null : pawn.GetAvoidGrid(true); bool flag = traverseParms.mode == TraverseMode.PassAllDestroyableThings || traverseParms.mode == TraverseMode.PassAllDestroyableThingsNotWater; bool flag2 = traverseParms.mode != TraverseMode.NoPassClosedDoorsOrWater && traverseParms.mode != TraverseMode.PassAllDestroyableThingsNotWater; bool flag3 = !flag; CellRect cellRect = this.CalculateDestinationRect(dest, peMode); bool flag4 = cellRect.Width == 1 && cellRect.Height == 1; int[] array = mapE.getShipPathGrid.pathGrid; TerrainDef[] topGrid = this.map.terrainGrid.topGrid; EdificeGrid edificeGrid = this.map.edificeGrid; int num3 = 0; int num4 = 0; Area allowedArea = this.GetAllowedArea(pawn); bool flag5 = !(pawn is null) && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = true && DebugViewSettings.drawPaths; bool flag7 = !flag && !(WaterGridsUtility.GetRegion(start, this.map, RegionType.Set_Passable) is null) && flag2; bool flag8 = !flag || !flag3; bool flag9 = false; bool flag10 = !(pawn is null) && pawn.Drafted; bool flag11 = !(pawn is null) && !(pawn.GetComp <CompShips>() is null); int num5 = (!flag11) ? NodesToOpenBeforeRegionbasedPathing_NonShip : NodesToOpenBeforeRegionBasedPathing_Ship; int num6 = 0; int num7 = 0; float num8 = this.DetermineHeuristicStrength(pawn, start, dest); int num9 = !(pawn is null) ? pawn.TicksPerMoveCardinal : DefaultMoveTicksCardinal; int num10 = !(pawn is null) ? pawn.TicksPerMoveDiagonal : DefaultMoveTicksDiagonal; this.CalculateAndAddDisallowedCorners(traverseParms, peMode, cellRect); this.InitStatusesAndPushStartNode(ref num, start); for (;;) { this.PfProfilerBeginSample("Open cell"); if (this.openList.Count <= 0) { break; } num6 += this.openList.Count; num7++; ShipPathFinder.CostNode costNode = this.openList.Pop(); num = costNode.index; if (costNode.cost != this.calcGrid[num].costNodeCost) { this.PfProfilerEndSample(); } else if (this.calcGrid[num].status == this.statusClosedValue) { this.PfProfilerEndSample(); } else { IntVec3 c = this.cellIndices.IndexToCell(num); int x2 = c.x; int z2 = c.z; if (flag6) { this.DebugFlash(c, (float)this.calcGrid[num].knownCost / 1500f, this.calcGrid[num].knownCost.ToString()); } if (flag4) { if (num == num2) { goto Block_32; } } else if (cellRect.Contains(c) && !this.disallowedCornerIndices.Contains(num)) { goto Block_34; } if (num3 > SearchLimit) { goto Block_35; } this.PfProfilerEndSample(); this.PfProfilerBeginSample("Neighbor consideration"); for (int i = 0; i < 8; i++) { uint num11 = (uint)(x2 + ShipPathFinder.Directions[i]); uint num12 = (uint)(z2 + ShipPathFinder.Directions[i + 8]); if ((ulong)num11 < ((ulong)this.mapSizeX) && (ulong)num12 < (ulong)((long)this.mapSizeZ)) { int num13 = (int)num11; int num14 = (int)num12; int num15 = this.cellIndices.CellToIndex(num13, num14); if (this.calcGrid[num15].status != this.statusClosedValue || flag9) { int num16 = 0; //bool flag12 = false; Extra cost for traversing water if (flag2 || !new IntVec3(num13, 0, num14).GetTerrain(this.map).HasTag("Water")) { if (!this.shipPathGrid.WalkableFast(num15)) { if (!flag) { if (flag6) { this.DebugFlash(new IntVec3(num13, 0, num14), 0.22f, "walk"); } goto IL_E3A; } //flag12 = true; num16 += 70; Building building = edificeGrid[num15]; if (building is null) { goto IL_E3A; } if (!IsDestroyable(building)) { goto IL_E3A; } num16 += (int)((float)building.HitPoints * 0.2f); } if (i > 3) { switch (i) { case 4: if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } break; case 5: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } if (this.BlocksDiagonalMovement(num + 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } break; case 6: if (this.BlocksDiagonalMovement(num + this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } break; case 7: if (this.BlocksDiagonalMovement(num - this.mapSizeX)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } if (this.BlocksDiagonalMovement(num - 1)) { if (flag8) { if (flag6) { this.DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "ships"); } goto IL_E3A; } num16 += 70; } break; } } int num17 = (i <= 3) ? num9 : num10; num17 += num16; /*if(!flag12) * { * //Extra Costs for traversing water * num17 += array[num15]; * num17 += flag10 ? topGrid[num15].extraDraftedPerceivedPathCost : topGrid[num15].extraNonDraftedPerceivedPathCost; * }*/ if (!(byteGrid is null)) { num17 += (int)(byteGrid[num15] * 8); } //Allowed area cost? if (flag5 && PawnUtility.AnyPawnBlockingPathAt(new IntVec3(num13, 0, num14), pawn, false, false, true)) { num17 += Cost_PawnCollision; } Building building2 = this.edificeGrid[num15]; if (!(building2 is null)) { //Building Costs Here } List <Blueprint> list = this.blueprintGrid[num15]; if (!(list is null)) { this.PfProfilerBeginSample("Blueprints"); int num18 = 0; foreach (Blueprint bp in list) { num18 = Mathf.Max(num18, GetBlueprintCost(bp, pawn)); } if (num18 == int.MaxValue) { this.PfProfilerEndSample(); goto IL_E3A; } num17 += num18; this.PfProfilerEndSample(); } int num19 = num17 + this.calcGrid[num].knownCost; ushort status = this.calcGrid[num15].status; if (!(pawn.GetComp <CompShips>() is null) && !this.map.terrainGrid.TerrainAt(num15).IsWater) { num19 += 10000; } if (status == this.statusClosedValue || status == this.statusOpenValue) { int num20 = 0; if (status == this.statusClosedValue) { num20 = num9; } if (this.calcGrid[num15].knownCost <= num19 + num20) { goto IL_E3A; } } if (flag9) { this.calcGrid[num15].heuristicCost = Mathf.RoundToInt((float)this.regionCostCalculator.GetPathCostFromDestToRegion(num15) * ShipPathFinder.RegionheuristicWeighByNodesOpened.Evaluate((float)num4)); if (this.calcGrid[num15].heuristicCost < 0) { Log.ErrorOnce(string.Concat(new object[] { "Heuristic cost overflow for ship ", pawn.ToStringSafe <Pawn>(), " pathing from ", start, " to ", dest, "." }), pawn.GetHashCode() ^ 193840009, false); this.calcGrid[num15].heuristicCost = 0; } } else if (status != this.statusClosedValue && status != this.statusOpenValue) { int dx = Math.Abs(num13 - x); int dz = Math.Abs(num14 - z); int num21 = GenMath.OctileDistance(dx, dz, num9, num10); this.calcGrid[num15].heuristicCost = Mathf.RoundToInt((float)num21 * num8); } int num22 = num19 + this.calcGrid[num15].heuristicCost; //Log.Message("Num19: " + num19 + " || num22: " + num22); if (num22 < 0) { Log.ErrorOnce(string.Concat(new object[] { "Node cost overflow for ship ", pawn.ToStringSafe <Pawn>(), " pathing from ", start, " to ", dest, "." }), pawn.GetHashCode() ^ 87865822, false); num22 = 0; } this.calcGrid[num15].parentIndex = num; this.calcGrid[num15].knownCost = num19; this.calcGrid[num15].status = this.statusOpenValue; this.calcGrid[num15].costNodeCost = num22; num4++; this.openList.Push(new ShipPathFinder.CostNode(num15, num22)); } } } IL_E3A :; } this.PfProfilerEndSample(); num3++; this.calcGrid[num].status = this.statusClosedValue; if (num4 >= num5 && flag7 && !flag9) { flag9 = true; this.regionCostCalculator.Init(cellRect, traverseParms, num9, num10, byteGrid, allowedArea, flag10, this.disallowedCornerIndices); this.InitStatusesAndPushStartNode(ref num, start); num4 = 0; num3 = 0; } } } string text = ((pawn is null) || pawn.CurJob is null) ? "null" : pawn.CurJob.ToString(); string text2 = ((pawn is null) || pawn.Faction is null) ? "null" : pawn.Faction.ToString(); Log.Warning(string.Concat(new object[] { "ship pawn: ", pawn, " pathing from ", start, " to ", dest, " ran out of cells to process.\nJob:", text, "\nFaction: ", text2 }), false); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); Block_32: this.PfProfilerEndSample(); PawnPath result = this.FinalizedPath(num, flag9); this.PfProfilerEndSample(); return(result); Block_34: this.PfProfilerEndSample(); PawnPath result2 = this.FinalizedPath(num, flag9); this.PfProfilerEndSample(); return(result2); Block_35: Log.Warning(string.Concat(new object[] { "Ship ", pawn, " pathing from ", start, " to ", dest, " hit search limit of ", SearchLimit, " cells." }), false); this.DebugDrawRichData(); this.PfProfilerEndSample(); return(PawnPath.NotFound); }
private static IAttackTarget FindBestReachableMeleeTarget(Predicate <IAttackTarget> validator, Pawn searcherPawn, float maxTargDist, bool canBash) { maxTargDist = Mathf.Min(maxTargDist, 30f); IAttackTarget reachableTarget = null; IAttackTarget bestTargetOnCell(IntVec3 x) { List <Thing> thingList = x.GetThingList(searcherPawn.Map); for (int i = 0; i < thingList.Count; i++) { Thing thing = thingList[i]; if (thing is IAttackTarget attackTarget) { if (validator(attackTarget)) { if (ReachabilityImmediate.CanReachImmediate(x, thing, searcherPawn.Map, PathEndMode.Touch, searcherPawn)) { if (searcherPawn.CanReachImmediate(thing, PathEndMode.Touch) || searcherPawn.Map.attackTargetReservationManager.CanReserve(searcherPawn, attackTarget)) { return(attackTarget); } } } } } return(null); } searcherPawn.Map.floodFiller.FloodFill(searcherPawn.Position, delegate(IntVec3 x) { if (!x.Walkable(searcherPawn.Map)) { return(false); } if ((float)x.DistanceToSquared(searcherPawn.Position) > maxTargDist * maxTargDist) { return(false); } if (!canBash) { if (x.GetEdifice(searcherPawn.Map) is Building_Door building_Door && !building_Door.CanPhysicallyPass(searcherPawn)) { return(false); } } return(!PawnUtility.AnyPawnBlockingPathAt(x, searcherPawn, true, false, false)); }, delegate(IntVec3 x) { for (int i = 0; i < 8; i++) { IntVec3 intVec = x + GenAdj.AdjacentCells[i]; if (intVec.InBounds(searcherPawn.Map)) { IAttackTarget attackTarget = bestTargetOnCell(intVec); if (attackTarget != null) { reachableTarget = attackTarget; break; } } } return(reachableTarget != null); }, int.MaxValue, false, null); return(reachableTarget); }
private void expandNode(Step currentNode) { bool previousNodeHasDoor, previousNodeIsPathCostIgnoreRepeater; Building previousBuilding = edificeGrid[currentNode.currentIndx]; if (previousBuilding is Building_Door) { previousNodeHasDoor = true; } else { previousNodeHasDoor = false; } previousNodeIsPathCostIgnoreRepeater = false; foreach (ThingDef def in map.thingGrid.ThingsListAtFast(currentNode.currentIndx).Select(t => t.def)) { if (def.pathCostIgnoreRepeat) { previousNodeIsPathCostIgnoreRepeater = true; break; } } //add all neighboring tiles but diagonal only if it is allowed, fixed size where we null out the after last entry if it is not full int currentIndice = currentNode.currentIndx; int currentX = currentNode.current.x; int currentZ = currentNode.current.z; int[] Directions = DirectionsShared; IntVec3 invalidIntVec3 = invalidIntVec3Shared; byte byteDirections; if (pathfinderDirections != null) { byteDirections = pathfinderDirections.GetDirections(currentNode.current); } else { byteDirections = 0xFF; } for (int i = 0; i < 8; i++) { if (byteDirections != 0xFF) { //{ 0, 1, 0, -1, 1, 1, -1, -1, // -1, 0, 1, 0, -1, 1, 1, -1 } switch (i) { case 2: if ((byteDirections & 1) != 1) { neighbors[i] = invalidIntVec3; continue; } break; case 5: if ((byteDirections & 2) != 2) { neighbors[i] = invalidIntVec3; continue; } break; case 1: if ((byteDirections & 4) != 4) { neighbors[i] = invalidIntVec3; continue; } break; case 4: if ((byteDirections & 8) != 8) { neighbors[i] = invalidIntVec3; continue; } break; case 0: if ((byteDirections & 16) != 16) { neighbors[i] = invalidIntVec3; continue; } break; case 8: if ((byteDirections & 32) != 32) { neighbors[i] = invalidIntVec3; continue; } break; case 3: if ((byteDirections & 64) != 64) { neighbors[i] = invalidIntVec3; continue; } break; case 7: if ((byteDirections & 128) != 128) { neighbors[i] = invalidIntVec3; continue; } break; default: neighbors[i] = invalidIntVec3; continue; } } int neighborX = currentX + Directions[i]; int neighborZ = currentZ + Directions[i + 8]; //check if we are within map bounds if (neighborX >= mapSizeX || neighborZ >= mapSizeZ || neighborX < 0 || neighborZ < 0) { neighbors[i] = invalidIntVec3; continue; } //check if we have to skip diagonal movement due to adjacent tiles. if (i > 3) { if (Directions[i] == 1) { if (BlocksDiagonalMovement(currentIndice + 1)) { neighbors[i] = invalidIntVec3; continue; } } else { if (BlocksDiagonalMovement(currentIndice - 1)) { neighbors[i] = invalidIntVec3; continue; } } if (Directions[i + 8] == 1) { if (BlocksDiagonalMovement(currentIndice + mapSizeX)) { neighbors[i] = invalidIntVec3; continue; } } else { if (BlocksDiagonalMovement(currentIndice - mapSizeX)) { neighbors[i] = invalidIntVec3; continue; } } } //check if we can cross water tiles IntVec3 ngb = new IntVec3(neighborX, 0, neighborZ); if (dontPassWater && ngb.GetTerrain(map).HasTag("Water")) { neighbors[i] = invalidIntVec3; continue; } neighbors[i] = ngb; } for (int i = 0; i < neighbors.Count(); i++) { IntVec3 neighbor = neighbors[i]; if (neighbor == invalidIntVec3) { continue; } int neighborIndice = cellIndices.CellToIndex(neighbor); Step successor = stepCache.getCachedOrNewStep(neighbor, neighborIndice); // wenn der Nachfolgeknoten bereits geschlossen wurde - tue nichts if (successor.closed) { continue; } //can the tile be walked across? bool useTerrainCost = true; int costToDestroyInsteadOfTerrainCost = 0; if (!pathGrid.WalkableFast(neighborIndice)) { if (traverseParms.mode != TraverseMode.PassAllDestroyableThings) { continue; } useTerrainCost = false; costToDestroyInsteadOfTerrainCost += 70; Building building = edificeGrid[neighborIndice]; if (building == null) { continue; } if (!PathFinder.IsDestroyable(building)) { continue; } costToDestroyInsteadOfTerrainCost += (int)((float)building.HitPoints * 0.11f); } bool isUpdate = successor.costToReachThis > 0; int stepCosts = (i <= 3) ? costPerMoveCardinal : costPerMoveDiagonal; Building building2 = edificeGrid[neighborIndice]; if (building2 != null) { int buildingCost = PathFinder.GetBuildingCost(building2, traverseParms, pawn); if (buildingCost == Int32.MaxValue) { continue; } stepCosts += buildingCost; } List <Blueprint> blueprints = this.blueprintGrid[neighborIndice]; if (blueprints != null) { int maxBluprintCost = 0; blueprints.ForEach(bp => maxBluprintCost = Math.Max(maxBluprintCost, PathFinder.GetBlueprintCost(bp, pawn))); if (maxBluprintCost == Int32.MaxValue) { continue; } stepCosts += maxBluprintCost; } if (useTerrainCost) { stepCosts += pathGridArray[neighborIndice] - minTerrainCost; //PathGrid uses cached static cost and does not account for directional costs //1. moving from a thing with pathCostIgnoreRepeat and a cost of at least 25 to a thing with pathCostIgnoreRepeat reduces the cost by the pathCost of the first thing //2. moving from door to door increases the cost by 45 if (previousNodeHasDoor && building2 != null && building2 is Building_Door) { stepCosts += 45; } if (previousNodeIsPathCostIgnoreRepeater) { foreach (ThingDef def in map.thingGrid.ThingsListAtFast(neighborIndice).Select(t => t.def)) { if (def.pathCostIgnoreRepeat && def.pathCost >= 25) { stepCosts -= def.pathCost; } } } //new in 1.0 if (drafted) { stepCosts += topGrid[neighborIndice].extraDraftedPerceivedPathCost; } else { stepCosts += topGrid[neighborIndice].extraNonDraftedPerceivedPathCost; } } else { stepCosts += costToDestroyInsteadOfTerrainCost; } if (avoidGrid != null) { stepCosts += (int)(avoidGrid[neighborIndice] * 8); } //Variation to default behaviour: //When pathfinding from outside the allowed area back into the allowed area we ignore the malus for forbidden area to avoid ruining the performance for no good reason. //We check the current position and if it is forbidden we do not apply a malus if it is allowed the current neighbor is tested. if (allowedArea != null && allowedArea[currentIndice] && !allowedArea[neighborIndice]) { stepCosts += 600; } if (collidesWithPawns && PawnUtility.AnyPawnBlockingPathAt(neighbor, pawn, false, false, true)) { stepCosts += 175; } // g Wert für den neuen Weg berechnen: g Wert des Vorgängers plus // die Kosten der gerade benutzten Kante int costToReachThisNeighbor = currentNode.costToReachThis + stepCosts; // wenn der Nachfolgeknoten bereits auf der Open List ist, // aber der neue Weg nicht besser ist als der alte - tue nichts if (isUpdate && successor.costToReachThis <= costToReachThisNeighbor) { continue; } successor.costToReachThis = costToReachThisNeighbor; // Vorgängerzeiger setzen und g Wert merken successor.predecessor = currentNode; // f Wert des Knotens in der Open List aktualisieren // bzw. Knoten mit f Wert in die Open List einfügen if (isUpdate) { openlist.UpdatePriority(successor, successor.costToReachThis + successor.estimatedCostsToDestination); //if ( drawPath ) map.debugDrawer.FlashCell(successor.current, 0.1f, "updated", 100); } else { openlist.Enqueue(successor, successor.costToReachThis + successor.estimatedCostsToDestination); if (drawPath) { map.debugDrawer.FlashCell(successor.current, 0.22f, "opened", 100); } } } }