Esempio n. 1
0
        public void DoWork()
        {
            path = new Queue <T>();

            HashSet <T> closedSet = new HashSet <T>();

            PathfindingPriorityQueue <T> openSet = new PathfindingPriorityQueue <T>();

            openSet.Enqueue(startTile, 0);

            Dictionary <T, T> cameFrom = new Dictionary <T, T>();

            Dictionary <T, float> g_score = new Dictionary <T, float>();

            g_score[startTile] = 0;

            Dictionary <T, float> f_score = new Dictionary <T, float>();

            f_score[startTile] = costEstimateFunc(startTile, endTile);

            // Debug.Log("AStar Doing Something!");

            //List<T> currentNeighbors;

            while (openSet.Count > 0)
            {
                T current = openSet.Dequeue();

                if (System.Object.ReferenceEquals(current, endTile))
                {
                    Reconstuct_path(cameFrom, current);
                    return;
                }


                closedSet.Add(current);

                // Debug.Log("Neighbor Count: " + current.GetNeighbors().Length);
                foreach (T neighbor in current.GetNeighbors())
                {
                    if (closedSet.Contains(neighbor))
                    {
                        continue;
                    }

                    // doing something weird
                    float total_pathfinding_cost_to_neighbor =
                        current.AggregateTileCostToEnter(g_score[current], neighbor, theUnit);
                    //

                    if (total_pathfinding_cost_to_neighbor < 0)
                    {
                        // Less than zero value; Impassable by this unit!
                        // Debug.Log("Imapssable Terrain!");
                        continue;
                    }
                    // Debug.Log("total_pathfinding_cost_to_neighbor : " + total_pathfinding_cost_to_neighbor);
                    float tentative_g_score = total_pathfinding_cost_to_neighbor;

                    ////
                    //// something strange going on here
                    if (openSet.Contains(neighbor) && tentative_g_score >= g_score[neighbor])
                    {
                        continue;                         // not a better path, move along
                    }

                    // this is either a new tile or just found a cheaper route to it
                    cameFrom[neighbor] = current;
                    g_score[neighbor]  = tentative_g_score;

                    // figure cost from neighbor to destination
                    f_score[neighbor] = g_score[neighbor] + costEstimateFunc(neighbor, endTile);

                    openSet.EnqueueOrUpdate(neighbor, f_score[neighbor]);
                }         // foreach neighbor
            }             // while
        }
Esempio n. 2
0
        public void DoWork()
        {
            //Debug.Log("QPath_AStar::DoWork");
            path     = new Queue <T>();
            pathList = new Dictionary <IQPathTile, IQPathTile>();
            //twins = new Queue<IQPathTile>();

            HashSet <T> closedSet = new HashSet <T>();

            PathfindingPriorityQueue <T> openSet = new PathfindingPriorityQueue <T>();

            openSet.Enqueue(startTile, 0);

            Dictionary <T, T> came_From = new Dictionary <T, T>();

            Dictionary <T, float> g_score = new Dictionary <T, float>();

            g_score[startTile] = 0;

            Dictionary <T, float> f_score = new Dictionary <T, float>();

            f_score[startTile] = CostEstimate(startTile, endTile);

            while (openSet.Count > 0)
            {
                T current = openSet.Dequeue();

                // Check to see if we are there.
                if (System.Object.ReferenceEquals(current, endTile))
                {
                    Reconstruct_path(came_From, current);
                    return;
                }

                closedSet.Add(current);

                foreach (T edge_neighbour in current.GetNeighbours())
                {
                    T neighbour = edge_neighbour;

                    if (closedSet.Contains(neighbour))
                    {
                        continue; // ignore this already completed neighbor
                    }


                    float total_pathfinding_cost_to_neighbor =
                        neighbour.AggregateCostToEnter(g_score[current], current, LastTwoTiles(null), unit);

                    if (total_pathfinding_cost_to_neighbor < 0)
                    {
                        // Values less than zero represent an invalid/impassable tile
                        continue;
                    }

                    float tentative_g_score = total_pathfinding_cost_to_neighbor;

                    // Is the neighbour already in the open set?
                    //   If so, and if this new score is worse than the old score,
                    //   discard this new result.
                    if (openSet.Contains(neighbour) && tentative_g_score >= g_score[neighbour])
                    {
                        continue;
                    }

                    // This is either a new tile or we just found a cheaper route to it
                    came_From[neighbour] = current;
                    //pathList[neighbour] = current;
                    g_score[neighbour] = tentative_g_score;
                    f_score[neighbour] = g_score[neighbour] + CostEstimate(neighbour, endTile);

                    LastTwoTiles(neighbour);

                    openSet.EnqueueOrUpdate(neighbour, f_score[neighbour]);
                } // foreach neighbour
            }     // while
        }
        public void DoWork()
        {
            path = new Queue <T>();

            Util.WriteDebugLog(string.Format("QPath_AStar::DoWork() run"), GameManager.LogLevel_Error, GameManager.instance.debug, GameManager.instance.LogLevel);
            //List for all our nodes
            HashSet <T> closedSet = new HashSet <T>();
            PathfindingPriorityQueue <T> openSet = new PathfindingPriorityQueue <T>();

            openSet.Enqueue(startTile, 0);


            Dictionary <T, T> came_From = new Dictionary <T, T>();

            //Includes real cost
            Dictionary <T, float> g_score = new Dictionary <T, float>();

            g_score[startTile] = 0;

            //Includes estimated cost
            Dictionary <T, float> f_score = new Dictionary <T, float>();

            f_score[startTile] = costEstimateFunc(startTile, endTile);


            //Main loop
            while (openSet.Count > 0)
            {
                T current = openSet.Dequeue();

                //Check to see if we are there -- see if they are the same object in memory
                if (Object.ReferenceEquals(current, endTile))
                {
                    Reconstruct_path(came_From, current);
                    return;
                }

                closedSet.Add(current);

                foreach (T edge_neighbor in current.GetNeighbors())
                {
                    T neighbor = edge_neighbor;

                    if (closedSet.Contains(neighbor))
                    {
                        continue; //ignore this already completed neighbor
                    }

                    //float pathfinding_cost_to_neighbor = neighbor. ;

                    float total_pathfinding_cost_to_neighbor = neighbor.AggregateCostToEnter(g_score[current], current, unit);

                    //Check for impassible terrain - Less then 0 is impassible terrain
                    if (total_pathfinding_cost_to_neighbor < 0)
                    {
                        continue;
                    }


                    float tentative_g_score = total_pathfinding_cost_to_neighbor;

                    /*
                     * Is the neighbor already in the open set?
                     * If so , and if this new score is worse than the old score
                     * discard this new result
                     */
                    if (openSet.Contains(neighbor) && tentative_g_score >= g_score[neighbor])
                    {
                        continue;
                    }

                    //This is either a new tile or we just found a cheaper route to it
                    came_From[neighbor] = current;
                    g_score[neighbor]   = tentative_g_score;
                    f_score[neighbor]   = g_score[neighbor] + costEstimateFunc(neighbor, endTile);

                    openSet.EnqueueOrUpdate(neighbor, f_score[neighbor]);
                } //foreach neighbor
            }
        }
        public void DoWork()
        {
            path = new Queue <T>();

            HashSet <T> closedset = new HashSet <T>();

            PathfindingPriorityQueue <T> openSet = new PathfindingPriorityQueue <T>();

            openSet.Enqueue(starttile, 0);

            Dictionary <T, T> came_From = new Dictionary <T, T>();

            Dictionary <T, float> g_score = new Dictionary <T, float>();

            g_score[starttile] = 0;

            Dictionary <T, float> f_score = new Dictionary <T, float>();

            f_score[starttile] = CostEstimateFunc(starttile, endtile);

            while (openSet.Count > 0)
            {
                T current = openSet.Dequeue();


                if (System.Object.ReferenceEquals(current, endtile))
                {
                    reconstruct_path(came_From, current);
                    return;
                }

                closedset.Add(current);

                foreach (T edge_neighbour in current.GetNeighbours())
                {
                    T neighbour = edge_neighbour;

                    if (closedset.Contains(neighbour))
                    {
                        continue;
                    }

                    float total_pathfinding_cost_to_neighbor =
                        neighbour.AggregateCostToEnter(g_score[current], current, unit);

                    if (total_pathfinding_cost_to_neighbor < 0)
                    {
                        continue;
                    }


                    float tentative_g_score = total_pathfinding_cost_to_neighbor;


                    if (openSet.Contains(neighbour) && tentative_g_score >= g_score[neighbour])
                    {
                        continue;
                    }


                    came_From[neighbour] = current;
                    g_score[neighbour]   = tentative_g_score;
                    f_score[neighbour]   = g_score[neighbour] + CostEstimateFunc(neighbour, endtile);

                    openSet.EnqueueOrUpdate(neighbour, f_score[neighbour]);
                }
            }
        }
Esempio n. 5
0
        public void DoWork()
        {
            Debug.Log("QPath_AStar::DoWork");
            path = new Queue <T>();

            HashSet <T> closedSet = new HashSet <T>();//an array that is guaranteed to be unique

            //for openset use quill18's pathfinding priority queue
            PathfindingPriorityQueue <T> openSet = new PathfindingPriorityQueue <T>();

            //openSet of all our tiles is organized based on shortest distance first
            openSet.Enqueue(startTile, 0);

            Dictionary <T, T> came_From = new Dictionary <T, T>();

            Dictionary <T, float> g_score = new Dictionary <T, float>();

            g_score[startTile] = 0; //cost to get to our start tile is 0

            Dictionary <T, float> f_score = new Dictionary <T, float>();

            f_score[startTile] = costEstimateFunc(startTile, endTile); //includes the estimated cost of getting to that tile

            while (openSet.Count > 0)
            {
                T current = openSet.Dequeue();

                //are these referencing the same object in memory?
                if (System.Object.ReferenceEquals(current, endTile))
                {
                    Reconstruct_path(came_From, current);
                    return;
                }

                closedSet.Add(current);
                foreach (T edge_neighbour in current.GetNeighbours())
                {
                    T neighbour = edge_neighbour;

                    if (closedSet.Contains(neighbour))
                    {
                        continue; //ignore this already completed neighbor
                    }

                    float total_pathfinding_cost_to_neighbor =
                        neighbour.AggregateCostToEnter(g_score[current], current, unit);

                    //check if terrain is impassable
                    if (total_pathfinding_cost_to_neighbor < 0)
                    {
                        //Values less than zero represent an invalid/impassable tile
                        continue;
                    }

                    float tentative_g_score = g_score[current] + total_pathfinding_cost_to_neighbor;

                    //Is the neighbour already in the open set?
                    //  If so, and if this new score is worse than the old: discard this new result
                    if (openSet.Contains(neighbour) && tentative_g_score >= g_score[neighbour])
                    {
                        continue;
                    }

                    //This is either a new tile or a cheaper route to it
                    came_From[neighbour] = current;
                    g_score[neighbour]   = tentative_g_score;
                    f_score[neighbour]   = g_score[neighbour] + costEstimateFunc(neighbour, endTile);

                    openSet.EnqueueOrUpdate(neighbour, f_score[neighbour]);
                } //foreach neighbour
            }     //while
        }