Пример #1
0
        // internal function to find path, don't use this one from outside
        private static List <Node> _ImpFindPath(Grid grid, Point startPos, Point targetPos)
        {
            Node startNode  = grid.Nodes[startPos.X, startPos.Y];
            Node targetNode = grid.Nodes[targetPos.X, targetPos.Y];

            List <Node>    openList  = new List <Node>();
            HashSet <Node> openSet   = new HashSet <Node>();
            HashSet <Node> closedSet = new HashSet <Node>();

            openList.Add(startNode);
            openSet.Add(startNode);

            while (openList.Count > 0)
            {
                Node currentNode = openList[0];
                for (int i = 1; i < openList.Count; i++)
                {
                    if (openList[i].FCost < currentNode.FCost || openList[i].FCost == currentNode.FCost && openList[i].HCost < currentNode.HCost)
                    {
                        currentNode = openList[i];
                    }
                }

                openList.Remove(currentNode);
                openSet.Remove(currentNode);
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    return(RetracePath(grid, startNode, targetNode));
                }

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

                    int newMovementCostToNeighbour = currentNode.GCost + GetDistance(currentNode, neighbour) * (int)(10.0f * neighbour.Penalty);
                    var isNeighborInOpenList       = openSet.Contains(neighbour);
                    if (newMovementCostToNeighbour < neighbour.GCost || !isNeighborInOpenList)
                    {
                        neighbour.GCost  = newMovementCostToNeighbour;
                        neighbour.HCost  = GetDistance(neighbour, targetNode);
                        neighbour.Parent = currentNode;

                        if (!isNeighborInOpenList)
                        {
                            openList.Add(neighbour);
                            openSet.Add(neighbour);
                        }
                    }
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Internal function that implements the path-finding algorithm.
        /// </summary>
        /// <param name="grid">Grid to search.</param>
        /// <param name="startPos">Starting position.</param>
        /// <param name="targetPos">Ending position.</param>
        /// <param name="distance">The type of distance, Euclidean or Manhattan.</param>
        /// <param name="ignorePrices">If true, will ignore tile price (how much it "cost" to walk on).</param>
        /// <returns>List of grid nodes that represent the path to walk.</returns>
        private static List <Node> _ImpFindPath(Grid grid, Point startPos, Point targetPos, DistanceType distance = DistanceType.Euclidean, bool ignorePrices = false)
        {
            //Debug.Log(startPos.x +", " + startPos.y);
            Node startNode  = grid.nodes[startPos.x, startPos.y];
            Node targetNode = grid.nodes[targetPos.x, targetPos.y];

            List <Node>    openSet   = new List <Node>();
            HashSet <Node> closedSet = new HashSet <Node>();

            openSet.Add(startNode);

            while (openSet.Count > 0)
            {
                Node currentNode = openSet[0];
                for (int i = 1; i < openSet.Count; i++)
                {
                    if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                    {
                        currentNode = openSet[i];
                    }
                }

                openSet.Remove(currentNode);
                closedSet.Add(currentNode);

                if (currentNode == targetNode)
                {
                    return(RetracePath(grid, startNode, targetNode));
                }

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

                    int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) * (ignorePrices ? 1 : (int)(10.0f * neighbour.price));
                    if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                    }
                }
            }

            return(null);
        }