Пример #1
0
 //Call delete function for graph as well as reorganize the nodes in the editor net
 public void DeleteNode(int A)
 {
     Graph.DeleteNode(A);
     Debug.Log("Deleting!");
     pathNodeContainer [] nodeObjs = gameObject.GetComponentsInChildren <pathNodeContainer>();
     Debug.Log(nodeObjs.Length);
     for (int i = A; i < nodeObjs.Length; i++)
     {
         nodeObjs[i].Node = Graph.GetNodeList()[i];
         nodeObjs[i].GenerateName();
     }
 }
Пример #2
0
    //Create pathNode for sparse graph
    public void ConnectGraphs(int GraphIDA, int GraphNodeA, int GraphIDB, int GraphNodeB)
    {
        sparseGraph.AddNodes(1);
        pathNode linker = sparseGraph.GetNodeList()[sparseGraph.NodeCount - 1];

        //Place linker node over doorway
        linker.position = Graphs[GraphIDA].GetNodeList()[GraphNodeA].position;

        //Link A to B
        Graphs[GraphIDA].LinkGraph(Graphs[GraphIDB], GraphNodeA, linker);

        //Link B to A
        Graphs[GraphIDB].LinkGraph(Graphs[GraphIDA], GraphNodeB, linker);
    }
Пример #3
0
 void OnEnable()
 {
     _target = (SimpleAIGraphContainer)target;
     Graph   = _target.GetGraph();
     if (Graph == null)
     {
         Graph = new GraphSimple();
     }
     //Ensure that the root node is part of the path
     if (Graph.NodeCount < 1)
     {
         Graph.AddNodes(1);
         Graph.GetNodeList()[0].position = _target.transform.position;
     }
 }
Пример #4
0
    private static List <float> GetDistances(GraphSimple graphIn)
    {
        List <float>    distances = new List <float>();
        List <pathNode> graph     = graphIn.GetNodeList();

        for (int i = 0; i < graph.Count; i++)
        {
            distances.Add(graph[i].dist);
        }

        return(distances);
    }
Пример #5
0
    void GetDistances()
    {
        distances.Clear();
        distances = Dijkstra.AllDistancesFromNode(testGraph, DistanceNodeIDInt);
        int index = 0;

        foreach (pathNode node in testGraph.GetNodeList())
        {
            //Debug.Log(node.dist);
            foreach (pathNode neighbor in node.neighbors)
            {
                Debug.Log("Node " + index + " connected to:" + neighbor.ID);
            }

            index++;
        }
    }
Пример #6
0
    //Faster Method
    public void BuildPlaneGraph(float width, float height, int segments)
    {
        this.width  = width;
        this.height = height;
        simpleGraph = new GraphSimple();
        List <int>   Neighbors   = new List <int>();
        List <float> Weights     = new List <float>();
        float        deltaHeight = height / segments;
        float        deltaWidth  = width / segments;

        //transform.TransformPoint(new Vector3(j*(width/segments)-width/2, 0, i*(height/segments)-height/2));
        simpleGraph.AddNodes((segments + 1) * (segments + 1));
        List <pathNode> DeadNodes = new List <pathNode>();
        List <int>      DeadVerts = new List <int>();

        pathNode currentNode;

        for (int i = 0; i <= segments; i++)
        {
            for (int j = 0; j <= segments; j++)
            {
                currentNode = simpleGraph.GetNodeList()[i * (segments + 1) + j];

                currentNode.position = transform.TransformPoint(new Vector3(j * (deltaWidth) - width / 2, 0, i * (deltaHeight) - height / 2));
                //RaycastHit hit;
                //if (Physics.Raycast(currentNode.position, -Vector3.up, out hit))
                //{
                //	currentNode.position=transform.TransformPoint(new Vector3(j*(deltaWidth)-width/2, hit.point.y, i*(deltaHeight)-height/2));
                //}

                Neighbors.Clear();
                Weights.Clear();

                //Debug.Log ("Node #"+(i*(segments+1)+j));

                //Up and Down
                if (i > 0)
                {
                    Neighbors.Add(((i - 1) * (segments + 1) + (j)));
                    Weights.Add(deltaHeight);
                    //Debug.Log ("Down");
                }
                if (i < segments)
                {
                    Neighbors.Add(((i + 1) * (segments + 1) + (j)));
                    Weights.Add(deltaHeight);
                    //Debug.Log ("Up");
                }

                //Left and Right
                if (j > 0)
                {
                    Neighbors.Add(((i) * (segments + 1) + (j - 1)));
                    Weights.Add(deltaWidth);
                    //Debug.Log ("Left");
                }
                if (j < segments)
                {
                    Neighbors.Add(((i) * (segments + 1) + (j + 1)));
                    Weights.Add(deltaWidth);
                    //Debug.Log ("Right");
                }
                //Top-left to Bottom-right Diagonal
                if (i > 0 && j > 0)
                {
                    Neighbors.Add((i - 1) * (segments + 1) + (j - 1));
                    Weights.Add(Mathf.Sqrt(deltaWidth * deltaWidth + deltaHeight * deltaHeight));
                    //Debug.Log ("Bottom Left");
                }
                if (i < segments && j < segments)
                {
                    Neighbors.Add((i + 1) * (segments + 1) + (j + 1));
                    Weights.Add(Mathf.Sqrt(deltaWidth * deltaWidth + deltaHeight * deltaHeight));
                    //Debug.Log ("Top right");
                }

                //Top-right to Bottom-left Diagonal
                if ((i < segments) && j > 0)
                {
                    Neighbors.Add(((i + 1) * (segments + 1) + (j - 1)));
                    Weights.Add(Mathf.Sqrt(deltaWidth * deltaWidth + deltaHeight * deltaHeight));
                    //Debug.Log ("Top left");
                }
                if (i > 0 && j < segments)
                {
                    Neighbors.Add((i - 1) * (segments + 1) + (j + 1));
                    Weights.Add(Mathf.Sqrt(deltaWidth * deltaWidth + deltaHeight * deltaHeight));
                    //Debug.Log ("Bottom Right");
                }

                int index = 0;
                foreach (int Neighbor in Neighbors)
                {
                    //Debug.Log (Neighbor);
                    pathNode neighbor = simpleGraph.GetNodeList()[Neighbor];
                    currentNode.neighbors.Add(neighbor);
                    currentNode.weight.Add(Weights[index]);
                    index++;
                }
                //Debug.Log(i);
            }
        }

        foreach (pathNode node in simpleGraph.GetNodeList())
        {
            node.dist = 0;
        }

        //Scan physics graph
        foreach (pathNode node in simpleGraph.GetNodeList())
        {
            //Debug.Log (node.neighbors.Count);
            for (int i = 0; i < node.neighbors.Count; i++)
            {
                pathNode Neighbor = node.neighbors[i];
                //Debug.Log ("Ray cast from "+node.position+" to "+Neighbor.position+" with a distance of "+Vector3.Distance(Neighbor.position,node.position));
                //Debug.Log ("Node #"+node.ID+" Hit toward: "+Neighbor.ID);
                if (Physics.Raycast(node.position, Vector3.Normalize(Neighbor.position - node.position), Vector3.Distance(Neighbor.position, node.position)))
                {
                    //Debug.DrawLine(Neighbor.position,node.position);
                    if (!Physics.Raycast(node.position, Vector3.Normalize(node.position - Neighbor.position), Vector3.Distance(Neighbor.position, node.position)))
                    {
                        if (DeadVerts.Contains(Neighbor.ID))
                        {
                            DeadNodes.Add(Neighbor); DeadVerts.Add(Neighbor.ID);
                        }
                    }
                    //removeCount++;
                    node.neighbors.RemoveAt(i);
                    node.weight.RemoveAt(i);
                    i--;
                    Neighbor.dist += 1;
                }
            }
        }

        //Dead Node Removal
        for (int i = 0; i < DeadNodes.Count; i++)
        {
            //Debug.Log (DeadNodes[i]);
            simpleGraph.DeleteNode(DeadNodes[i].ID);
        }


        //Filter nodes Remove count
        for (int i = 0; i < simpleGraph.NodeCount; i++)
        {
            pathNode node = simpleGraph.GetNodeList()[i];
            //Debug.Log("Hit count: "+node.dist);
            if (node.dist >= 3)
            {
                simpleGraph.DeleteNode(node.ID);
                i--;
            }
        }

        for (int i = 0; i < simpleGraph.NodeCount; i++)
        {
            pathNode node = simpleGraph.GetNodeList()[i];
            if (node.neighbors.Count < 3)
            {
                simpleGraph.DeleteNode(node.ID);
            }
        }
    }
Пример #7
0
    //public List<pathNode> shortestPath = new List<pathNode>();

    public static List <pathNode> ShortestPath(GraphSimple graphIn, int SourceIndex, int DestinationIndex)
    {
        //Create temp list for shortest path
        List <pathNode> shortestPath = new List <pathNode>();
        List <pathNode> graph        = graphIn.GetNodeList();

        if (SourceIndex == DestinationIndex)
        {
            shortestPath.Add(graph[SourceIndex]);
            Debug.Log("Indices the same!");
            return(shortestPath);
        }
        //Reset distances
        //Find source and destination nodes
        for (int i = 0; i < graph.Count; i++)
        {
            graph[i].dist     = Mathf.Infinity;
            graph[i].previous = null;
            graph[i].Removed  = false;
        }

        //Set the initial node as a distance of 0
        graph[SourceIndex].dist = 0;
        //While nodes are not empty
        int nodesLeft = graph.Count;

        while (nodesLeft > 0)
        {
            pathNode min = new pathNode();
            //Grab shortest distance point. Accounts for ALL nodes
            for (int i = 0; i < graph.Count; i++)
            {
                if ((graph[i].dist < min.dist) && !graph[i].Removed)
                {
                    min = graph[i];
                }
            }

            //Re-reference min to a new name. Combine them?
            pathNode smallestpathNode = min;
            //Remove node
            graph[smallestpathNode.ID].Removed = true;
            nodesLeft--;
            //Exit condition. Destination Reached!
            if ((smallestpathNode.ID == DestinationIndex))
            {
                //Debug.Log ("Found!");
                return(getPath(smallestpathNode));
            }
            //We can't reach the node!
            else if (smallestpathNode.dist == Mathf.Infinity)
            {
                //Debug.Log("No connection!");
                return(shortestPath);
            }

            //List<pathNode> neighbors = smallestpathNode.neighbors;
            for (int i = 0; i < smallestpathNode.neighbors.Count; i++)
            {
                float alt = smallestpathNode.dist + smallestpathNode.weight[i];

                if (alt < smallestpathNode.neighbors[i].dist)
                {
                    smallestpathNode.neighbors[i].dist     = alt;
                    smallestpathNode.neighbors[i].previous = smallestpathNode;
                }
            }
        }
        Debug.Log("All Nodes Ventured");
        return(shortestPath);
    }
Пример #8
0
    public override void OnInspectorGUI()
    {
        GraphContainer = _target.transform.parent.GetComponent(typeof(SimpleAIGraphContainer)) as SimpleAIGraphContainer;

        if (_target.Node != Graph.GetNodeList()[_target.Node.ID])
        {
            _target.Node = Graph.GetNodeList()[_target.Node.ID];
        }

        GUILayout.BeginVertical();
        if (_target.Node != null)
        {
            GUILayout.Label("Node Information", EditorStyles.boldLabel);
            GUILayout.Label("ID: " + _target.Node.ID);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Passthrough?: ");
            if (_target.Node.passThrough)
            {
                if (GUILayout.Button("True"))
                {
                    _target.Node.passThrough = false;
                }
            }
            else
            {
                if (GUILayout.Button("False"))
                {
                    _target.Node.passThrough = true;
                }
            }
            GUILayout.EndHorizontal();
            GUILayout.Label("Position: " + _target.Node.position);
            GUILayout.Label("Neighbors: ", EditorStyles.boldLabel);
            for (int i = 0; i < _target.Node.neighbors.Count; i++)
            {
                GUILayout.Label("Node " + _target.Node.neighbors[i].ID);
            }
        }

        _target.Node.position = _target.transform.position;

        if (GUILayout.Button("Add Node") && GraphContainer != null)
        {
            Graph.AddNodes(1);
            Graph.GetNodeList()[GraphContainer.GetGraph().NodeCount - 1].position = _target.transform.position + new Vector3(1, 0, 0);
            Graph.ConnectNodes(_target.Node.ID, Graph.NodeCount - 1, 1);
            Graph.ConnectNodes(Graph.NodeCount - 1, _target.Node.ID, 1);
            GameObject tempObj = new GameObject("");
            tempObj.transform.position = _target.transform.position + new Vector3(1, 0, 0);
            pathNodeContainer tempNode = tempObj.AddComponent(typeof(pathNodeContainer)) as pathNodeContainer;
            tempNode.Node = Graph.GetNodeList()[Graph.NodeCount - 1];
            tempNode.GenerateName();
            tempObj.transform.parent = GraphContainer.transform;
        }

        if (GUILayout.Button("Merge Closest Node") && GraphContainer != null)
        {
            int   closestID = _target.Node.ID;
            float minDist   = Mathf.Infinity;
            for (int i = 0; i < Graph.NodeCount; i++)
            {
                float dist = Vector3.Distance(Graph.GetNodeList()[i].position, _target.transform.position);
                if (dist < minDist && Graph.GetNodeList()[i].ID != _target.Node.ID)
                {
                    minDist   = dist;
                    closestID = Graph.GetNodeList()[i].ID;
                }
            }
            if (closestID != _target.Node.ID)
            {
                if (EditorUtility.DisplayDialog("Merge nodes?", "The closest node is node " + closestID + ", would you like to merge these nodes together?", "Merge", "Cancel"))
                {
                    GraphContainer.MergeNodes(_target.Node.ID, closestID);
                    destroyMe = true;
                }
            }
        }

        if (GUILayout.Button("Delete Node") && GraphContainer != null)
        {
            if (EditorUtility.DisplayDialog("Delete Selected Node?", "Are you sure you want to delete node " + _target.Node.ID + "?", "Delete", "Cancel"))
            {
                GraphContainer.DeleteNode(_target.Node.ID);
                destroyMe = true;
            }
        }
        GUILayout.EndVertical();
        if (destroyMe)
        {
            DestroyImmediate(_target.gameObject);
        }
    }
Пример #9
0
 public void TeleportToNode(int ID)
 {
     transform.position = NodePath.GetNodeList()[currentNode].position;
     currentNode        = ID;
 }