Ejemplo n.º 1
0
        public List <Node> FindPath(Vector2 start, Vector2 end)
        {
            Node startNode = grid.getNode(start);
            Node endNode   = grid.getNode(end);

            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 node = 0; node < openSet.Count; node++)
                {
                    if (openSet[node].fCost < currentNode.fCost || (openSet[node].fCost == currentNode.fCost && openSet[node].hCost < currentNode.hCost))
                    {
                        currentNode = openSet[node];
                    }
                }

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

                if (currentNode == endNode)
                {
                    Logger.Log("Found a path to " + endNode.position.ToString() + "!");
                    return(retracePath(startNode, endNode));
                }

                foreach (Node neighbor in grid.getNeighbors(currentNode))
                {
                    if (!neighbor.traversible || closedSet.Contains(neighbor))
                    {
                        continue;
                    }
                    int newMoveCost = currentNode.gCost + getDistance(currentNode, neighbor) + (int)(neighbor.weightCost * 10);
                    if (newMoveCost < neighbor.gCost || !openSet.Contains(neighbor))
                    {
                        neighbor.gCost  = newMoveCost;
                        neighbor.hCost  = getDistance(neighbor, endNode);
                        neighbor.parent = currentNode;

                        if (!openSet.Contains(neighbor))
                        {
                            openSet.Add(neighbor);
                        }
                    }
                }
            }
            Logger.Log("Failed to find a path to " + endNode.position.ToString() + "!", StardewModdingAPI.LogLevel.Error);
            return(new List <Node>());
        }
Ejemplo n.º 2
0
        public static float getWeight(Node node, PathingGrid grid)
        {
            float totalImpassableWeight = 0;

            if (node == null)
            {
                Logger.Log("Null node!");
                return(-1);
            }
            for (int x = node.x - distance; x < node.x + distance + 1; x++)
            {
                for (int y = node.y - distance; y < node.y + distance + 1; y++)
                {
                    if (x < 0 || x >= grid.width || y < 0 || y >= grid.height || !grid.getNode(x, y).traversible)
                    {
                        totalImpassableWeight += (distance) - (float)Math.Min(Math.Sqrt(Math.Pow(x - node.x, 2) + Math.Pow(y - node.y, 2)), (float)distance);
                    }
                }
            }
            return(totalImpassableWeight);
        }