public override void Update() { // update all cells for (int i = 0; i < CellIndices.NumGridCells; i++) { // get x,y position from index var position = CellIndices.IndexToCell(i); // paint it... brownish? texture.SetPixel(position.x, position.z, Find.Map.terrainGrid.TerrainAt(i).color); } }
public override void Update() { // get the fog grid var fog = Find.Map.fogGrid.fogGrid; // loop over all cells for (int i = 0; i < CellIndices.NumGridCells; i++) { // set pixel color var pos = CellIndices.IndexToCell(i); texture.SetPixel(pos.x, pos.z, fog[i] ? Color.gray : Color.clear); } }
public static PawnPath FinalizedPath2(int finalIndex, bool usedRegionHeuristics, CellIndices cellIndices, PathFinderNodeFast[] calcGrid) { //HACK - fix pool //PawnPath emptyPawnPath = map(__instance).pawnPathPool.GetEmptyPawnPath(); PawnPath emptyPawnPath = new PawnPath(); int num = finalIndex; while (true) { int parentIndex = calcGrid[num].parentIndex; emptyPawnPath.AddNode(cellIndices.IndexToCell(num)); if (num == parentIndex) { break; } num = parentIndex; } emptyPawnPath.SetupFound(calcGrid[finalIndex].knownCost, usedRegionHeuristics); return(emptyPawnPath); }
public (PawnPath path, bool found) FindVehiclePath(IntVec3 start, LocalTargetInfo dest, TraverseParms traverseParms, CancellationToken token, PathEndMode peMode = PathEndMode.OnCell, bool waterPathing = false) { if (report) { Debug.Message($"{VehicleHarmony.LogLabel} MainPath for {traverseParms.pawn.LabelShort} - ThreadId: [{Thread.CurrentThread.ManagedThreadId}] TaskId: [{Task.CurrentId}]"); } postCalculatedCells.Clear(); VehicleMapping VehicleMapping = map.GetCachedMapComponent <VehicleMapping>(); if (DebugSettings.pathThroughWalls) { traverseParms.mode = TraverseMode.PassAllDestroyableThings; } VehiclePawn pawn = traverseParms.pawn as VehiclePawn; if (!pawn.IsBoat() && waterPathing) { Log.Error($"Set to waterPathing but {pawn.LabelShort} is not registered as a Boat. Self Correcting..."); waterPathing = false; } if (!(pawn is null) && pawn.Map != map) { Log.Error(string.Concat(new object[] { "Tried to FindVehiclePath for pawn which is spawned in another map. Their map PathFinder should have been used, not this one. " + "pawn=", pawn, " pawn.Map=", pawn.Map, " map=", map })); return(PawnPath.NotFound, false); } if (!start.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindShipPath with invalid start ", start, ", pawn=", pawn })); return(PawnPath.NotFound, false); } if (!dest.IsValid) { Log.Error(string.Concat(new object[] { "Tried to FindPath with invalid dest ", dest, ", pawn= ", pawn })); return(PawnPath.NotFound, false); } if (traverseParms.mode == TraverseMode.ByPawn) { if (waterPathing) { if (!ShipReachabilityUtility.CanReachShip(pawn, dest, peMode, Danger.Deadly, false, traverseParms.mode)) { return(PawnPath.NotFound, false); } } else { if (!ReachabilityUtility.CanReach(pawn, dest, peMode, Danger.Deadly, false, traverseParms.mode)) { return(PawnPath.NotFound, false); } } } else { if (waterPathing) { if (!VehicleMapping.VehicleReachability.CanReachShip(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound, false); } } else { if (!map.reachability.CanReach(start, dest, peMode, traverseParms)) { return(PawnPath.NotFound, false); } } } cellIndices = map.cellIndices; VehiclePathGrid = VehicleMapping.VehiclePathGrid; pathGrid = map.pathGrid; this.edificeGrid = map.edificeGrid.InnerArray; blueprintGrid = map.blueprintGrid.InnerArray; int x = dest.Cell.x; int z = dest.Cell.z; int num = cellIndices.CellToIndex(start); int num2 = 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 = CalculateDestinationRect(dest, peMode); bool flag4 = cellRect.Width == 1 && cellRect.Height == 1; int[] boatsArray = VehiclePathGrid.pathGrid; int[] vehicleArray = pathGrid.pathGrid; TerrainDef[] topGrid = map.terrainGrid.topGrid; EdificeGrid edificeGrid = map.edificeGrid; int num3 = 0; int num4 = 0; Area allowedArea = GetAllowedArea(pawn); bool flag5 = !(pawn is null) && PawnUtility.ShouldCollideWithPawns(pawn); bool flag6 = true && DebugViewSettings.drawPaths; bool flag7 = !flag && !(VehicleGridsUtility.GetRegion(start, 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 is null); int num5 = (!flag11) ? NodesToOpenBeforeRegionbasedPathing_NonShip : NodesToOpenBeforeRegionBasedPathing_Ship; int num6 = 0; int num7 = 0; float num8 = DetermineHeuristicStrength(pawn, start, dest); int num9 = !(pawn is null) ? pawn.TicksPerMoveCardinal : DefaultMoveTicksCardinal; int num10 = !(pawn is null) ? pawn.TicksPerMoveDiagonal : DefaultMoveTicksDiagonal; CalculateAndAddDisallowedCorners(traverseParms, peMode, cellRect); InitStatusesAndPushStartNode(ref num, start); Rot8 rot = pawn.FullRotation; int iterations = 0; for (;;) { if (token.IsCancellationRequested) { return(PawnPath.NotFound, false); } iterations++; if (openList.Count <= 0) { break; } num6 += openList.Count; num7++; CostNode costNode = openList.Pop(); num = costNode.index; if (costNode.cost == calcGrid[num].costNodeCost && calcGrid[num].status != statusClosedValue) { IntVec3 c = cellIndices.IndexToCell(num); IntVec3 prevCell = c; int x2 = c.x; int z2 = c.z; if (flag6) { DebugFlash(c, calcGrid[num].knownCost / 1500f, calcGrid[num].knownCost.ToString()); } if (flag4) { if (num == num2) { goto Block_32; } } else if (cellRect.Contains(c) && !disallowedCornerIndices.Contains(num)) { goto Block_32; } if (num3 > SearchLimit) { goto Block_33; } List <IntVec3> fullRectCells = CellRect.CenteredOn(c, pawn.def.size.x, pawn.def.size.z).Where(cl2 => cl2 != c).ToList(); for (int i = 0; i < 8; i++) { uint num11 = (uint)(x2 + Directions[i]); //x uint num12 = (uint)(z2 + Directions[i + 8]); //y if (num11 < ((ulong)mapSizeX) && num12 < (ulong)(mapSizeZ)) { int num13 = (int)num11; int num14 = (int)num12; int num15 = cellIndices.CellToIndex(num13, num14); IntVec3 cellToCheck = cellIndices.IndexToCell(num15); if (VehicleMod.settings.main.fullVehiclePathing && pawn.LocationRestrictedBySize(cellToCheck)) { goto EndPathing; } if (calcGrid[num15].status != statusClosedValue || flag9) { int num16 = 0; bool flag12 = false; //Extra cost for traversing water if (flag2 || !new IntVec3(num13, 0, num14).GetTerrain(map).HasTag("Water")) { if (waterPathing) { if (!pawn.DrivableFast(num15)) { if (!flag) { if (flag6) { DebugFlash(new IntVec3(num13, 0, num14), 0.22f, "walk"); } goto EndPathing; } num16 += 70; Building building = edificeGrid[num15]; if (building is null) { goto EndPathing; } if (!IsDestroyable(building)) { goto EndPathing; } num16 += (int)(building.HitPoints * 0.2f); } } else { if (!pawn.DrivableFast(num15)) { if (!flag) { if (flag6) { DebugFlash(new IntVec3(num13, 0, num14), 0.22f, "walk"); } goto EndPathing; } flag12 = true; num16 += 70; Building building = edificeGrid[num15]; if (building is null) { goto EndPathing; } if (!IsDestroyable(building)) { goto EndPathing; } num16 += (int)(building.HitPoints * 0.2f); } } if (i > 3) { switch (i) { case 4: if (BlocksDiagonalMovement(pawn, num - mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } if (BlocksDiagonalMovement(pawn, num + 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } break; case 5: if (BlocksDiagonalMovement(pawn, num + mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } if (BlocksDiagonalMovement(pawn, num + 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 + 1, 0, z2), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } break; case 6: if (BlocksDiagonalMovement(pawn, num + mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 + 1), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } if (BlocksDiagonalMovement(pawn, num - 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } break; case 7: if (BlocksDiagonalMovement(pawn, num - mapSizeX)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2, 0, z2 - 1), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } if (BlocksDiagonalMovement(pawn, num - 1)) { if (flag8) { if (flag6) { DebugFlash(new IntVec3(x2 - 1, 0, z2), 0.9f, "ships"); } goto EndPathing; } num16 += 70; } break; } } int num17 = (i <= 3) ? num9 : num10; num17 += num16; //if (Rot8.DirectionFromCells(prevCell, cellToCheck) != rot) //{ // Log.Message("Additional Cost"); // num17 += ChangeDirectionAdditionalCost; //} if (!flag12 && !waterPathing) { //Extra Terrain costs if (pawn.VehicleDef.properties.customTerrainCosts?.NotNullAndAny() ?? false) { TerrainDef currentTerrain = map.terrainGrid.TerrainAt(num15); if (pawn.VehicleDef.properties.customTerrainCosts.ContainsKey(currentTerrain)) { int customCost = pawn.VehicleDef.properties.customTerrainCosts[currentTerrain]; if (customCost < 0) { goto EndPathing; } num17 += customCost; } else { num17 += vehicleArray[num15]; } } else { num17 += vehicleArray[num15]; } num17 += flag10 ? topGrid[num15].extraDraftedPerceivedPathCost : topGrid[num15].extraNonDraftedPerceivedPathCost; } if (byteGrid != null) { num17 += (byteGrid[num15] * 8); } //Allowed area cost? if (flag5 && MultithreadHelper.AnyVehicleBlockingPathAt(new IntVec3(num13, 0, num14), pawn, false, false, true) != null) { num17 += Cost_PawnCollision; } Building building2 = edificeGrid[num15]; if (!(building2 is null)) { //Building Costs Here } if (blueprintGrid[num15] != null) { List <Blueprint> list = new List <Blueprint>(blueprintGrid[num15]); if (!list.NullOrEmpty()) { int num18 = 0; foreach (Blueprint bp in list) { num18 = Mathf.Max(num18, GetBlueprintCost(bp, pawn)); } if (num18 == int.MaxValue) { goto EndPathing; } num17 += num18; } } int num19 = num17 + calcGrid[num].knownCost; ushort status = calcGrid[num15].status; //if(pawn.Props.useFullHitboxPathing) //{ // foreach(IntVec3 fullRect in fullRectCells) // { // if(fullRect != cellToCheck) // { // num19 += calcGrid[cellIndices.CellToIndex(fullRect)].knownCost; // Log.Message($"Cell: {fullRect} Cost: {num19}"); // if(postCalculatedCells.ContainsKey(fullRect)) // { // postCalculatedCells[fullRect] = num19; // } // else // { // postCalculatedCells.Add(fullRect, num19); // } // } // } //} //Only generate path costs for linear non-reverse pathing check if (report) { if (postCalculatedCells.ContainsKey(cellToCheck)) { postCalculatedCells[cellToCheck] = num19; } else { postCalculatedCells.Add(cellToCheck, num19); } } if (waterPathing && !map.terrainGrid.TerrainAt(num15).IsWater) { num19 += 10000; } if (status == statusClosedValue || status == statusOpenValue) { int num20 = 0; if (status == statusClosedValue) { num20 = num9; } if (calcGrid[num15].knownCost <= num19 + num20) { goto EndPathing; } } if (flag9) { calcGrid[num15].heuristicCost = waterPathing ? Mathf.RoundToInt(regionCostCalculatorSea.GetPathCostFromDestToRegion(num15) * RegionheuristicWeighByNodesOpened.Evaluate(num4)) : Mathf.RoundToInt(regionCostCalculatorLand.GetPathCostFromDestToRegion(num15) * RegionheuristicWeighByNodesOpened.Evaluate(num4)); if (calcGrid[num15].heuristicCost < 0) { Log.ErrorOnce(string.Concat(new object[] { "Heuristic cost overflow for vehicle ", pawn.ToStringSafe <Pawn>(), " pathing from ", start, " to ", dest, "." }), pawn.GetHashCode() ^ 193840009); calcGrid[num15].heuristicCost = 0; } } else if (status != statusClosedValue && status != statusOpenValue) { int dx = Math.Abs(num13 - x); int dz = Math.Abs(num14 - z); int num21 = GenMath.OctileDistance(dx, dz, num9, num10); calcGrid[num15].heuristicCost = Mathf.RoundToInt((float)num21 * num8); } int num22 = num19 + calcGrid[num15].heuristicCost; 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); num22 = 0; } calcGrid[num15].parentIndex = num; calcGrid[num15].knownCost = num19; calcGrid[num15].status = statusOpenValue; calcGrid[num15].costNodeCost = num22; num4++; rot = Rot8.DirectionFromCells(prevCell, cellToCheck); openList.Push(new CostNode(num15, num22)); } } } EndPathing :; } num3++; calcGrid[num].status = statusClosedValue; if (num4 >= num5 && flag7 && !flag9) { flag9 = true; if (waterPathing) { regionCostCalculatorSea.Init(cellRect, traverseParms, num9, num10, byteGrid, allowedArea, flag10, disallowedCornerIndices); } else { regionCostCalculatorLand.Init(cellRect, traverseParms, num9, num10, byteGrid, allowedArea, flag10, disallowedCornerIndices); } 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(); if (report) { Log.Warning(string.Concat(new object[] { "ship pawn: ", pawn, " pathing from ", start, " to ", dest, " ran out of cells to process.\nJob:", text, "\nFaction: ", text2, "\niterations: ", iterations })); } DebugDrawRichData(); return(PawnPath.NotFound, false); Block_32: PawnPath result = PawnPath.NotFound; if (report) { result = FinalizedPath(num, flag9); } DebugDrawPathCost(); return(result, true); Block_33: Log.Warning(string.Concat(new object[] { "Ship ", pawn, " pathing from ", start, " to ", dest, " hit search limit of ", SearchLimit, " cells." })); DebugDrawRichData(); return(PawnPath.NotFound, false); }
public static bool FloodFill(FloodFiller __instance, IntVec3 root, Predicate <IntVec3> passCheck, Func <IntVec3, int, bool> processor, int maxCellsToProcess = 2147483647, bool rememberParents = false, IEnumerable <IntVec3> extraRoots = null) { lock (__instance) { if (working(__instance)) { Log.Error("Nested FloodFill calls are not allowed. This will cause bugs.", false); } working(__instance) = true; ClearVisited(__instance); if (rememberParents && parentGrid(__instance) == null) { parentGrid(__instance) = new CellGrid(map(__instance)); } if (root.IsValid && extraRoots == null && !passCheck(root)) { if (rememberParents) { parentGrid(__instance)[root] = IntVec3.Invalid; } working(__instance) = false; } else { int area = map(__instance).Area; IntVec3[] directionsAround = GenAdj.CardinalDirectionsAround; int length = directionsAround.Length; CellIndices cellIndices = map(__instance).cellIndices; int num1 = 0; openSet(__instance).Clear(); if (root.IsValid) { int index = cellIndices.CellToIndex(root); visited(__instance).Add(index); traversalDistance(__instance)[index] = 0; openSet(__instance).Enqueue(root); } if (extraRoots != null) { if (extraRoots is IList <IntVec3> intVec3List) { for (int index1 = 0; index1 < intVec3List.Count; ++index1) { int index2 = cellIndices.CellToIndex(intVec3List[index1]); visited(__instance).Add(index2); traversalDistance(__instance)[index2] = 0; openSet(__instance).Enqueue(intVec3List[index1]); } } else { foreach (IntVec3 extraRoot in extraRoots) { int index = cellIndices.CellToIndex(extraRoot); visited(__instance).Add(index); traversalDistance(__instance)[index] = 0; openSet(__instance).Enqueue(extraRoot); } } } if (rememberParents) { for (int index = 0; index < visited(__instance).Count; ++index) { IntVec3 cell = cellIndices.IndexToCell(visited(__instance)[index]); parentGrid(__instance)[visited(__instance)[index]] = passCheck(cell) ? cell : IntVec3.Invalid; } } while (openSet(__instance).Count > 0) { IntVec3 c1 = openSet(__instance).Dequeue(); int num2 = traversalDistance(__instance)[cellIndices.CellToIndex(c1)]; if (!processor(c1, num2)) { ++num1; if (num1 != maxCellsToProcess) { for (int index1 = 0; index1 < length; ++index1) { IntVec3 c2 = c1 + directionsAround[index1]; int index2 = cellIndices.CellToIndex(c2); if (c2.InBounds(map(__instance)) && traversalDistance(__instance)[index2] == -1 && passCheck(c2)) { visited(__instance).Add(index2); openSet(__instance).Enqueue(c2); traversalDistance(__instance)[index2] = num2 + 1; if (rememberParents) { parentGrid(__instance)[index2] = c1; } } } if (openSet(__instance).Count > area) { Log.Error("Overflow on flood fill (>" + (object)area + " cells). Make sure we're not flooding over the same area after we check it.", false); working(__instance) = false; return(false); } } else { break; } } else { break; } } working(__instance) = 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 != 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); }
public static bool FloodFill(FloodFiller __instance, IntVec3 root, Predicate <IntVec3> passCheck, Func <IntVec3, int, bool> processor, int maxCellsToProcess = 2147483647, bool rememberParents = false, IEnumerable <IntVec3> extraRoots = null) { lock (__instance) { if (__instance.working) { Log.Error("Nested FloodFill calls are not allowed. This will cause bugs."); } __instance.working = true; __instance.ClearVisited(); if (rememberParents && __instance.parentGrid == null) { __instance.parentGrid = new CellGrid(__instance.map); } if (root.IsValid && extraRoots == null && !passCheck(root)) { if (rememberParents) { __instance.parentGrid[root] = IntVec3.Invalid; } __instance.working = false; } else { int area = __instance.map.Area; IntVec3[] directionsAround = GenAdj.CardinalDirectionsAround; int length = directionsAround.Length; CellIndices cellIndices = __instance.map.cellIndices; int num1 = 0; __instance.openSet.Clear(); if (root.IsValid) { int index = cellIndices.CellToIndex(root); __instance.visited.Add(index); __instance.traversalDistance[index] = 0; __instance.openSet.Enqueue(root); } if (extraRoots != null) { if (extraRoots is IList <IntVec3> intVec3List) { for (int index1 = 0; index1 < intVec3List.Count; ++index1) { int index2 = cellIndices.CellToIndex(intVec3List[index1]); __instance.visited.Add(index2); __instance.traversalDistance[index2] = 0; __instance.openSet.Enqueue(intVec3List[index1]); } } else { foreach (IntVec3 extraRoot in extraRoots) { int index = cellIndices.CellToIndex(extraRoot); __instance.visited.Add(index); __instance.traversalDistance[index] = 0; __instance.openSet.Enqueue(extraRoot); } } } if (rememberParents) { for (int index = 0; index < __instance.visited.Count; ++index) { IntVec3 cell = cellIndices.IndexToCell(__instance.visited[index]); __instance.parentGrid[__instance.visited[index]] = passCheck(cell) ? cell : IntVec3.Invalid; } } while (__instance.openSet.Count > 0) { IntVec3 c1 = __instance.openSet.Dequeue(); int num2 = __instance.traversalDistance[cellIndices.CellToIndex(c1)]; if (!processor(c1, num2)) { ++num1; if (num1 != maxCellsToProcess) { for (int index1 = 0; index1 < length; ++index1) { IntVec3 c2 = c1 + directionsAround[index1]; int index2 = cellIndices.CellToIndex(c2); if (!c2.InBounds(__instance.map) || __instance.traversalDistance[index2] != -1 || !passCheck(c2)) { continue; } __instance.visited.Add(index2); __instance.openSet.Enqueue(c2); __instance.traversalDistance[index2] = num2 + 1; if (rememberParents) { __instance.parentGrid[index2] = c1; } } if (__instance.openSet.Count <= area) { continue; } Log.Error("Overflow on flood fill (>" + area + " cells). Make sure we're not flooding over the same area after we check it."); __instance.working = false; return(false); } } break; } __instance.working = 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 != map) { 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=", map)); return(PawnPath.NotFound); } if (!start.IsValid) { Log.Error(string.Concat("Tried to FindPath with invalid start ", start, ", pawn= ", pawn)); return(PawnPath.NotFound); } if (!dest.IsValid) { Log.Error(string.Concat("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(string.Concat("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); 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) ? 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(); 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; } int num15 = 0; bool flag10 = false; if (!flag2 && new IntVec3(num12, 0, num13).GetTerrain(map).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) { 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 = 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(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 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(string.Concat(pawn, " pathing from ", start, " to ", dest, " hit search limit of ", 160000, " cells.")); DebugDrawRichData(); PfProfilerEndSample(); PfProfilerEndSample(); return(PawnPath.NotFound); }