Esempio n. 1
0
        public WorldPath FindPath(int startTile, int destTile, Caravan caravan, Func <float, bool> terminator = null)
        {
            if (startTile < 0)
            {
                Log.Error("Tried to FindPath with invalid start tile " + startTile + ", caravan= " + caravan);
                return(WorldPath.NotFound);
            }
            if (destTile < 0)
            {
                Log.Error("Tried to FindPath with invalid dest tile " + destTile + ", caravan= " + caravan);
                return(WorldPath.NotFound);
            }
            if (caravan != null)
            {
                if (!caravan.CanReach(destTile))
                {
                    return(WorldPath.NotFound);
                }
            }
            else if (!Find.WorldReachability.CanReach(startTile, destTile))
            {
                return(WorldPath.NotFound);
            }
            World      world = Find.World;
            WorldGrid  grid  = world.grid;
            List <int> tileIDToNeighbors_offsets = grid.tileIDToNeighbors_offsets;
            List <int> tileIDToNeighbors_values  = grid.tileIDToNeighbors_values;
            Vector3    normalized = grid.GetTileCenter(destTile).normalized;

            int[] pathGrid = world.pathGrid.pathGrid;
            int   num      = 0;
            int   num2     = (caravan == null) ? 2500 : caravan.TicksPerMove;
            int   num3     = this.CalculateHeuristicStrength(startTile, destTile);

            this.statusOpenValue   += 2;
            this.statusClosedValue += 2;
            if (this.statusClosedValue >= 65435)
            {
                this.ResetStatuses();
            }
            this.calcGrid[startTile].knownCost     = 0;
            this.calcGrid[startTile].heuristicCost = 0;
            this.calcGrid[startTile].costNodeCost  = 0;
            this.calcGrid[startTile].parentTile    = startTile;
            this.calcGrid[startTile].status        = this.statusOpenValue;
            this.openList.Clear();
            this.openList.Push(new CostNode(startTile, 0));
            while (true)
            {
                if (this.openList.Count <= 0)
                {
                    Log.Warning(caravan + " pathing from " + startTile + " to " + destTile + " ran out of tiles to process.");
                    return(WorldPath.NotFound);
                }
                CostNode costNode = this.openList.Pop();
                if (costNode.cost == this.calcGrid[costNode.tile].costNodeCost)
                {
                    int tile = costNode.tile;
                    if (this.calcGrid[tile].status != this.statusClosedValue)
                    {
                        if (DebugViewSettings.drawPaths)
                        {
                            Find.WorldDebugDrawer.FlashTile(tile, (float)((float)this.calcGrid[tile].knownCost / 375000.0), this.calcGrid[tile].knownCost.ToString(), 50);
                        }
                        if (tile == destTile)
                        {
                            return(this.FinalizedPath(tile));
                        }
                        if (num > 500000)
                        {
                            Log.Warning(caravan + " pathing from " + startTile + " to " + destTile + " hit search limit of " + 500000 + " tiles.");
                            return(WorldPath.NotFound);
                        }
                        int num4 = (tile + 1 >= tileIDToNeighbors_offsets.Count) ? tileIDToNeighbors_values.Count : tileIDToNeighbors_offsets[tile + 1];
                        for (int i = tileIDToNeighbors_offsets[tile]; i < num4; i++)
                        {
                            int    num5 = tileIDToNeighbors_values[i];
                            int    num7;
                            ushort status;
                            if (this.calcGrid[num5].status != this.statusClosedValue && !world.Impassable(num5))
                            {
                                int num6 = num2;
                                num6  += pathGrid[num5];
                                num6   = (int)((float)num6 * grid.GetRoadMovementMultiplierFast(tile, num5));
                                num7   = num6 + this.calcGrid[tile].knownCost;
                                status = this.calcGrid[num5].status;
                                if (status != this.statusClosedValue && status != this.statusOpenValue)
                                {
                                    goto IL_041e;
                                }
                                if (this.calcGrid[num5].knownCost > num7)
                                {
                                    goto IL_041e;
                                }
                            }
                            continue;
IL_041e:
                            Vector3 tileCenter = grid.GetTileCenter(num5);
                            if (status != this.statusClosedValue && status != this.statusOpenValue)
                            {
                                float num8 = grid.ApproxDistanceInTiles(GenMath.SphericalDistance(tileCenter.normalized, normalized));
                                this.calcGrid[num5].heuristicCost = Mathf.RoundToInt((float)((float)num2 * num8 * (float)num3 * 0.5));
                            }
                            int num9 = num7 + this.calcGrid[num5].heuristicCost;
                            this.calcGrid[num5].parentTile   = tile;
                            this.calcGrid[num5].knownCost    = num7;
                            this.calcGrid[num5].status       = this.statusOpenValue;
                            this.calcGrid[num5].costNodeCost = num9;
                            this.openList.Push(new CostNode(num5, num9));
                        }
                        num++;
                        this.calcGrid[tile].status = this.statusClosedValue;
                        if (terminator != null && terminator((float)this.calcGrid[tile].costNodeCost))
                        {
                            break;
                        }
                    }
                }
            }
            return(WorldPath.NotFound);
        }