IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
        {
            Vector3[] waypoints     = new Vector3[0];
            bool      wasSuccessful = false;

            Node startNode  = grid.NodeFromWorldPoint(startPos);
            Node targetNode = grid.NodeFromWorldPoint(targetPos);

            if (startNode.walkable && targetNode.walkable)
            {
                Heap <Node>    openSet   = new Heap <Node>(grid.MaxSize);
                HashSet <Node> closedSet = new HashSet <Node>();

                openSet.Add(startNode);

                while (openSet.Count > 0)
                {
                    Node currentNode = openSet.RemoveFirst();
                    closedSet.Add(currentNode);

                    if (currentNode == targetNode)
                    {
                        wasSuccessful = true;
                        break;
                    }

                    foreach (Node neighbor in grid.GetNeighbors(currentNode))
                    {
                        if (!neighbor.walkable || closedSet.Contains(neighbor))
                        {
                            continue;
                        }

                        int newMoveCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor) + neighbor.movementPenalty;

                        if (newMoveCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
                        {
                            neighbor.gCost      = newMoveCostToNeighbor;
                            neighbor.hCost      = GetDistance(neighbor, targetNode);
                            neighbor.parentNode = currentNode;

                            if (!openSet.Contains(neighbor))
                            {
                                openSet.Add(neighbor);
                            }
                            else
                            {
                                openSet.UpdateItem(neighbor);
                            }
                        }
                    }
                }
            }
            else
            {
                string msga = startNode.walkable ? "" : "Start position is unwalkable. Unable to generate path. ";
                string msgb = targetNode.walkable ? "" : "Target position is unwalkable. Unable to generate path";
                if (!string.IsNullOrEmpty(msga) || !string.IsNullOrEmpty(msgb))
                {
                    Debug.Log(msga + msgb);
                }
            }
            yield return(null);

            if (wasSuccessful)
            {
                waypoints = RetracePath(startNode, targetNode);
            }
            requestManager.FinishedProcessingPath(waypoints, wasSuccessful);
        }
示例#2
0
        IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
        {
            Vector3[] waypoints   = new Vector3[0];
            bool      pathSuccess = false;

            var sw = new Stopwatch();

            sw.Start();

            Node startNode  = _grid.NodeFromWorldPoint(startPos);
            Node targetNode = _grid.NodeFromWorldPoint(targetPos);

            if (startNode._walkable && targetNode._walkable)
            {
                Heap <Node>    openSet     = new Heap <Node>(_grid.MaxSize);
                HashSet <Node> closedSet   = new HashSet <Node>();
                Node           currentNode = null;

                openSet.Add(startNode);

                while (openSet.Count > 0)
                {
                    s_counter++;
                    if (s_counter % 2000 == 0)
                    {
                        sw.Stop();
                        yield return(new WaitForEndOfFrame());

                        sw.Start();
                    }

                    currentNode = openSet.RemoveFirst();
                    closedSet.Add(currentNode);

                    if (currentNode == targetNode) //path is complete
                    {
                        sw.Stop();
                        //UnityEngine.Debug.Log("Path computed in " + sw.ElapsedMilliseconds + " ms");
                        pathSuccess = true;

                        break;
                    }

                    foreach (var neighbor in _grid.GetNeighbors(currentNode))
                    {
                        if (!neighbor._walkable || closedSet.Contains(neighbor))
                        {
                            continue;
                        }

                        int newMovementCostToNeighbor = currentNode._gCost + GestDistanceManahattan(currentNode, neighbor) + neighbor._movementPenalty;
                        if (newMovementCostToNeighbor < neighbor._gCost || !openSet.Contains(neighbor))
                        {
                            neighbor._gCost  = newMovementCostToNeighbor;
                            neighbor._hCost  = GestDistanceManahattan(neighbor, targetNode);
                            neighbor._parent = currentNode;

                            if (!openSet.Contains(neighbor))
                            {
                                openSet.Add(neighbor);
                            }
                            else
                            {
                                openSet.UpdateItem(neighbor);//heap
                            }
                        }
                    }
                }
            }

            if (pathSuccess)
            {
                waypoints = RetracePath(startNode, targetNode);
            }

            _requestManager.FinishedProcessingPath(waypoints, pathSuccess);
        }
示例#3
0
        IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
        {
            Vector3[] waypoints   = new Vector3[0];
            bool      pathSuccess = false;

            Node startNode  = grid.NodeFromWorldPoint(startPos);
            Node targetNode = grid.NodeFromWorldPoint(targetPos);

            if (startNode.walkable && targetNode.walkable)
            {
                Heap <Node>    openSet   = new Heap <Node>(grid.MaxSize);
                HashSet <Node> closedSet = new HashSet <Node>();
                openSet.Add(startNode);

                while (openSet.Count > 0)
                {
                    Node currentNode = openSet.RemoveFirst();

                    closedSet.Add(currentNode);

                    if (currentNode == targetNode)
                    {
                        pathSuccess = true;
                        break;
                    }

                    foreach (Node neighbour in grid.Getneighbors(currentNode))
                    {
                        if (!neighbour.walkable || closedSet.Contains(neighbour))
                        {
                            continue;
                        }

                        int newMovementCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbour);
                        if (newMovementCostToNeighbor < neighbour.gCost || !openSet.Contains(neighbour))
                        {
                            neighbour.gCost  = newMovementCostToNeighbor;
                            neighbour.hCost  = GetDistance(neighbour, targetNode);
                            neighbour.parent = currentNode;

                            if (!openSet.Contains(neighbour))
                            {
                                openSet.Add(neighbour);
                            }
                            else
                            {
                                openSet.UpdateItem(neighbour);
                            }
                        }
                    }
                }
            }
            yield return(null);

            if (pathSuccess)
            {
                Debug.Log("Path successful");
                waypoints = RetracePath(startNode, targetNode);
            }
            requestManager.FinishedProcessingPath(waypoints, pathSuccess);
        }