Ejemplo n.º 1
0
 public NavmeshPolygon GetOtherPolygon(NavmeshPolygon polygon)
 {
     foreach (NavmeshPolygon otherPolygon in polygons){
         if (polygon != otherPolygon){
             return otherPolygon;
         }
     }
     Debug.LogError("Other NavmeshPolygon not found in NavmeshEdge!");
     return null;
 }
Ejemplo n.º 2
0
 public NavmeshEdge GetPortal(NavmeshPolygon otherPolygon)
 {
     foreach (NavmeshEdge portal in portals){
         if (portal.GetOtherPolygon(this) == otherPolygon){
             return portal;
         }
     }
     Debug.LogError("The polygons don't have any common edges!");
     return null;
 }
Ejemplo n.º 3
0
 public void AddPolygon(NavmeshPolygon polygon)
 {
     for (int i = 0; i < polygons.Length; i++){
         if (polygons[i] == null){
             polygons[i] = polygon;
             break;
         } else if (i == polygons.Length - 1){
             Debug.LogError("More than 2 NavmeshPolygons connected to one NavmeshEdge!");
         }
     }
 }
Ejemplo n.º 4
0
    /// <summary>
    /// Finds the shortest path from the start node to the goal node
    /// </summary>
    /// <param name="startPosition">Start position</param>
    /// <param name="goalPosition">Goal position</param>
    /// <param name="startMeshNode">Start mesh node</param>
    /// <param name="goalMeshNode">Goal mesh node</param>
    public List<NavmeshPolygon> FindPath(Vector3 startPosition, Vector3 goalPosition, NavmeshPolygon startPolygon, NavmeshPolygon goalPolygon)
    {
        //Debug.Log("AStar.FindPath");
        //Debug.Log("startPosition == " + startPosition);
        //Debug.Log("goalPosition == " + goalPosition);
        StartPosition = startPosition;
        GoalPosition = goalPosition;

        GoalNode = new AStarNode(this, null, goalPolygon, 0);
        StartNode = new AStarNode(this, null, startPolygon, 0);

        Heap openList = new Heap();
        Heap closedList = new Heap();
        List<AStarNode> solution = new List<AStarNode>();
        List<AStarNode> successors = new List<AStarNode>();

        int printed = 0;
        openList.Add(StartNode);
        while (openList.Count > 0) {
            //Debug.Log("AStar:main loop");
            // Get the node with the lowest TotalSum
            AStarNode nodeCurrent = (AStarNode)openList.Pop();
            if (printed < 1000){
                //Debug.Log("Current polygon: " + nodeCurrent.NavmeshPolygon.name + " - " + nodeCurrent.TotalCost);
                printed++;
            }

            // If the node is the goal copy the path to the solution array
            if (GoalNode.Equals(nodeCurrent)) {
                //Debug.Log("AStar:finish loop");
                while (nodeCurrent != null) {
                    solution.Insert(0, nodeCurrent);
                    nodeCurrent = nodeCurrent.Parent;
                }

                //convert solution
                //Debug.Log("Path found");
                return solution.ConvertAll(an => an.NavmeshPolygon);
            }

            // Get successors to the current node
            successors.Clear();
            nodeCurrent.GetSuccessors(successors);
            foreach (AStarNode nodeSuccessor in successors) {
                //Debug.Log("AStar:successor loop");
                // Test if the currect successor node is on the open list, if it is and
                // the TotalSum is higher, we will throw away the current successor.
                //Debug.Log("AStarNode nodeOpen");
                AStarNode nodeOpen;
                if (openList.Contains(nodeSuccessor)) {
                    //Debug.Log("openList check: nodeSuccessor (" + nodeSuccessor.NavmeshPolygon.name + ") - " + nodeSuccessor.TotalCost);
                    //Debug.Log("openList contains nodeSuccessor");
                    //Debug.Log("nodeOpen = (AStarNode)openList[openList.IndexOf(nodeSuccessor)];");
                    nodeOpen = (AStarNode)openList.GetEqual(nodeSuccessor);
                    //Debug.Log("openList check: nodeOpen (" + nodeOpen.NavmeshPolygon.name + ") - " + nodeOpen.TotalCost);
                    //Debug.Log("if ((nodeOpen != null) && (nodeSuccessor.TotalCost > nodeOpen.TotalCost))");
                    if ((nodeOpen != null) && (nodeSuccessor.TotalCost > nodeOpen.TotalCost)){
                        //Debug.Log("continue;");
                        //Debug.Log("openList check: continued");
                        continue;
                    } else {
                        //Debug.Log("openList check: not continued");
                    }

                }

                // Test if the currect successor node is on the closed list, if it is and
                // the TotalSum is higher, we will throw away the current successor.
                //Debug.Log("AStarNode nodeClosed;");
                AStarNode nodeClosed;
                //Debug.Log("if (closedList.Contains(nodeSuccessor)) {");
                if (closedList.Contains(nodeSuccessor)) {
                    //Debug.Log("closedList check: nodeSuccessor (" + nodeSuccessor.NavmeshPolygon.name + ") - " + nodeSuccessor.TotalCost);
                    //Debug.Log("closedList contains nodeSuccessor");
                    //Debug.Log("nodeClosed = (AStarNode)closedList[closedList.IndexOf(nodeSuccessor)];");
                    nodeClosed = (AStarNode)closedList.GetEqual(nodeSuccessor);
                    //Debug.Log("closedList check: nodeClosed (" + nodeClosed.NavmeshPolygon.name + ") - " + nodeClosed.TotalCost);
                    //Debug.Log("if ((nodeClosed != null) && (nodeSuccessor.TotalCost > nodeClosed.TotalCost))");
                    if ((nodeClosed != null) && (nodeSuccessor.TotalCost > nodeClosed.TotalCost)){
                        //Debug.Log("continue;");
                        continue;
                    }
                }

                // Remove the old successor from the open list
                //Debug.Log("openList.Remove(nodeSuccessor);");
                openList.Remove(nodeSuccessor);

                // Remove the old successor from the closed list
                //Debug.Log("closedList.Remove(nodeSuccessor);");
                closedList.Remove(nodeSuccessor);

                // Add the current successor to the open list
                //Debug.Log("penList.Push(nodeSuccessor);");
                if (printed < 1000){
                    //Debug.Log("Adding to openList: " + nodeSuccessor.NavmeshPolygon.name + " - " + nodeSuccessor.TotalCost);
                }
                openList.Push(nodeSuccessor);
                //Debug.Log("AStar:successor loop finished");
            }
            // Add the current node to the closed list
            //Debug.Log("closedList.Add(nodeCurrent);");
            closedList.Add(nodeCurrent);
            //Debug.Log("AStar:main loop finished");
        }
        Debug.Log("AStart.FindPath() Path not found.");
        return null;
    }
Ejemplo n.º 5
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="controller">Allow refernce to finder so nodes can access information about goal position and other properties.</param>
 /// <param name="parent">Store node's parent in order to rebuild path.</param>
 /// <param name="meshNode">Underlying mesh node with map information</param>
 /// <param name="cost">The accumulative cost until now</param>
 public AStarNode(AStar controller, AStarNode parent, NavmeshPolygon navmeshPolygon, float cost)
 {
     Parent = parent;
     NavmeshPolygon = navmeshPolygon;
     Cost = cost;
     this.controller = controller;
 }