Ejemplo n.º 1
0
    void FindPath(Vector3 startPos, Vector3 targetPos)
    {
        NodeScript startNode  = grid.NodeFromWorldPoint(startPos);
        NodeScript targetNode = grid.NodeFromWorldPoint(targetPos);

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

        openSet.Add(startNode);

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

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

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

            foreach (NodeScript neighbour in grid.GetNeighbours(node))
            {
                if (!neighbour.walkable || closedSet.Contains(neighbour))
                {
                    continue;
                }

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

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
    }
Ejemplo n.º 2
0
    public Node[] PathFinding(Node startNode, Node targetNode)
    {
        Node[] waypoints   = new Node[0];
        bool   pathSuccess = false;

        if ((startNode.walkable && targetNode.walkable) || (!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.GetNeighbours(currentNode))
                {
                    if (closedSet.Contains(neighbour))
                    {
                        continue;
                    }
                    int newMovementCostToNeighbour = currentNode.gCost + GetManhattanDistance(currentNode, neighbour);
                    if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                    {
                        neighbour.gCost  = newMovementCostToNeighbour;
                        neighbour.hCost  = GetManhattanDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbour);
                        }
                    }
                }
            }
        }
        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }

        return(waypoints);
    }
Ejemplo n.º 3
0
    //FindPath() is going to be the function you use to calculate the distance between two points on the grid. This is important though, before you use it make sure you're
    //passing it in NODE classes and not vectors. If you need to convert a vector3 for it pass that into the gridFromWorldPoint() function on the gridscript.
    void FindPath(Node startNode, Node targetNode)
    {
        //Node startNode = grid.NodeFromWorldPoint(startPos);
        //Node targetNode = grid.NodeFromWorldPoint(targetPos);

        List <Node>    openSet   = new List <Node>();    //List of nodes from "OpenSet" which are nodes that have not been checked yet
        HashSet <Node> closedSet = new HashSet <Node>(); //HashSet of nodes from "ClosedSet" which are nodes that have already been checked

        openSet.Add(startNode);                          //Adds the first node

        while (openSet.Count > 0)
        {
            Node currentNode = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                //Checks the node being evalutated to see if it's fCost is lower or if the fCost is the same AND the hCost is lower
                //if so switches to that node
                if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
                {
                    currentNode = openSet[i];
                }
            }
            //Remove Current from the openset and add to the closed set
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);
            //If destination is found runs RetracePath function passing in both the start and target node
            if (currentNode == targetNode)
            {
                RetracePath(startNode, targetNode);
                return;
            }

            foreach (Node neighbour in grid.GetNeighbours(currentNode))
            {
                if (!neighbour.walkable || closedSet.Contains(neighbour))
                {
                    continue;
                }
                //Calculates movement cost to neighbour using the gCost and GetDistance function
                int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
                if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newMovementCostToNeighbour;         //Set new g cost
                    neighbour.hCost  = GetDistance(neighbour, targetNode); //Calculates hCost with GetDistance function
                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
    }
Ejemplo n.º 4
0
    public List <Node> FindPath(Node startNode, Node destinationNode)
    {
        Heap <Node>    openSet   = new Heap <Node>(gridScript.MaxGridSize);
        HashSet <Node> closedSet = new HashSet <Node>();

        int defaultMoveCost = Mathf.RoundToInt(gridScript.hexSize);

        openSet.Add(startNode);

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

            if (currentNode == destinationNode)
            {
                return(RetracePath(startNode, destinationNode));
            }
            closedSet.Add(currentNode);

            foreach (Node neighbour in gridScript.GetNeighbours(currentNode))
            {
                if (neighbour.traversable == false || closedSet.Contains(neighbour))
                {
                    continue;
                }

                int newGCost = currentNode.gCost + defaultMoveCost;
                if (newGCost < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost = newGCost;
                    //neighbour.hCost = GetHeuristicDistance(neighbour, destinationNode);
                    neighbour.hCost  = Mathf.RoundToInt(Vector3.Distance(neighbour.position, destinationNode.position));
                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                    else
                    {
                        openSet.UpdateItem(neighbour);
                    }
                }
            }
        }
        Debug.Log("incompleet path given");
        return(new List <Node>());
    }
Ejemplo n.º 5
0
    IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
    {
        Stopwatch sw = new Stopwatch();

        sw.Start();

        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)
                {
                    sw.Stop();
                    print("Path found: " + sw.ElapsedMilliseconds + " ms");
                    pathSuccess = true;
                    break;
                }

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

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

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

        yield return(null);

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        requestManager.FinishedProcessingPath(waypoints, pathSuccess);
    }
Ejemplo n.º 6
0
    //This function calculates the shortest path using A*
    void FindPath(Vector3 startPos, Vector3 targetPos)
    {
        // Get the start node and end node from their world position.
        Node startNode  = grid.NodeFromWorldPoint(startPos);
        Node targetNode = grid.NodeFromWorldPoint(targetPos);


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

        //Add the start node to the openset
        openSet.Add(startNode);

        //Loop until the openset is empty
        while (openSet.Count > 0)
        {
            Node currentNode = openSet[0];
            //Find the node with the lowest cost
            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];
                }
            }
            //Remove the node from the openset to the closed set.
            openSet.Remove(currentNode);
            closedSet.Add(currentNode);

            //If the current node it the end
            if (currentNode == targetNode)
            {
                RetracePath(startNode, targetNode);
                return;
            }

            // Add the neighbours to the openset
            foreach (Node neighbour in grid.GetNeighbours(currentNode))
            {
                if (!neighbour.walkable || closedSet.Contains(neighbour))
                {
                    continue;
                }

                //Make the node not go diagonaly when a neighbour is unwalkable "this is code by me".
                if (GetDistance(currentNode, neighbour) > 10)
                {
                    int a = neighbour.gridX - currentNode.gridX;
                    int b = neighbour.gridY - currentNode.gridY;

                    if (!grid.getNode(neighbour.gridX - a, neighbour.gridY).walkable || !grid.getNode(neighbour.gridX, neighbour.gridY - b).walkable)
                    {
                        continue;
                    }
                }

                int newMocementCOstToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
                //Update H and G costs
                if (newMocementCOstToNeighbour < neighbour.gCost || !openSet.Contains(neighbour))
                {
                    neighbour.gCost  = newMocementCOstToNeighbour;
                    neighbour.hCost  = GetDistance(neighbour, targetNode);
                    neighbour.parent = currentNode;

                    //Add the neighbours to the openset
                    if (!openSet.Contains(neighbour))
                    {
                        openSet.Add(neighbour);
                    }
                }
            }
        }
    }
Ejemplo n.º 7
0
        public override void Action(GameManagerScript gameManager, GridScript grid, TileInformation tile)
        {
            if (_tile.BonusReceived) return;

            var neighbours = grid.GetNeighbours(grid.TileList.First(t => t.TileInformation == _tile));

            var influenceCount = 0;
            foreach (var neighbour in neighbours)
            {
                influenceCount += neighbour.TileInformation.DeltaInf;
                influenceCount -= neighbour.TileInformation.DeltaNot;
            }

            if (influenceCount >= 10)
            {
                Debug.Log("Got Bonus!!!!");

                _tile.BonusReceived = true;
                gameManager.People += 10;
            }
        }
    public void OnMouseOver()
    {
        List <Node> neighbours = grid.GetNeighbours(grid.NodeFromWorldPoint(grid.player.position));

        Debug.Log(neighbours[0].worldPosition);
    }
Ejemplo n.º 9
0
    /*
     * bool stallBool = false;
     * void HandlePathfindingLOD()
     * {
     *  float dst = Vector2.Distance(seeker.position, target.position);
     *  if(dst > 7)
     *  {
     *      if (!stallBool)
     *      {
     *          stallBool = true;
     *          StopAllCoroutines();
     *          //StartCoroutine(FindPathAfterDelay(0.5f));
     *      }
     *  }
     *  if(dst > 3 && dst <= 7)
     *  {
     *      if (!stallBool)
     *      {
     *          stallBool = true;
     *          StopAllCoroutines();
     *          //StartCoroutine(FindPathAfterDelay(0.3f));
     *      }
     *  }
     *  if(dst <= 3)
     *  {
     *      if (!stallBool)
     *      {
     *          stallBool = true;
     *          StopAllCoroutines();
     *          //StartCoroutine(FindPathAfterDelay(0.1f));
     *      }
     *  }
     * }
     */
    #endregion

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

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

        Debug.Log("Start Node worldPos: " + startNode.worldPos + ",   Target Node worldPos:" + targetNode.worldPos);

        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 curNode = openSet.RemoveFirst();
                closedSet.Add(curNode);

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

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

                    int newCostToNeighbour = curNode.GCost + GetDistance(curNode, neighbour);
                    if (newCostToNeighbour < neighbour.GCost || !openSet.Contains(neighbour))
                    {
                        neighbour.GCost  = newCostToNeighbour;
                        neighbour.HCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = curNode;

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

        if (pathSuccess)
        {
            waypoints = RetracePath(startNode, targetNode);
        }
        if (waypoints.Length == 0)
        {
            pathSuccess = false;
        }
        requestManager.FinishProcessingPath(waypoints, pathSuccess);
    }
Ejemplo n.º 10
0
    void FindPath(Vector3 start_position, Vector3 target_position)
    {
        Node start_node  = grid.NodeFromWoldPoint(start_position);
        Node target_node = grid.NodeFromWoldPoint(target_position);

        List <Node>    open_list   = new List <Node>();
        HashSet <Node> closed_list = new HashSet <Node>();

        open_list.Add(start_node);

        while (open_list.Count > 0)
        {
            Node current_node = open_list[0];

            for (int i = 1; i < open_list.Count; i++)
            {
                if (open_list[i].f_cost < current_node.f_cost || open_list[i].f_cost == current_node.f_cost && open_list[i].h_cost < current_node.h_cost)
                {
                    current_node = open_list[i];
                }
            }

            open_list.Remove(current_node);
            closed_list.Add(current_node);

            if (current_node == target_node)
            {
                ShowPath(start_node, target_node);
                return;
            }

            foreach (Node neighbour in grid.GetNeighbours(current_node))
            {
                if (!neighbour.walkable || closed_list.Contains(neighbour))
                {
                    continue;
                }

                int distance_to_neighbor = current_node.g_cost + GetDistance(current_node, neighbour);
                if (distance_to_neighbor < neighbour.g_cost || !open_list.Contains(neighbour))
                {
                    neighbour.g_cost = distance_to_neighbor;
                    neighbour.h_cost = GetDistance(neighbour, target_node);
                    neighbour.parent = current_node;

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

        int GetDistance(Node node1, Node node2)
        {
            int distance_x = Mathf.Abs(node1.grid_x - node2.grid_x);
            int distance_y = Mathf.Abs(node1.grid_y - node2.grid_y);

            if (distance_x > distance_y)
            {
                return(14 * distance_y + 10 * (distance_x - distance_y));
            }
            return(14 * distance_x + 10 * (distance_y - distance_x));
        }
    }