Exemplo n.º 1
0
    public override void Create()
    {
        Graph = new SparseGraph(false);

        for (int i = 0; i < World.Instance.Waypoints.Count; i++)
        {
            var node = new Node(i) { Position = new Vector2(World.Instance.Waypoints[i].x, World.Instance.Waypoints[i].z) };
            Graph.AddNode(node);
            //AddNodeObject(node, node.Position);
        }

        for (int fromIndex = 0; fromIndex < Graph.NumNodes; fromIndex++)
        {
            Node fromNode = Graph.GetNode(fromIndex);
            for (int toIndex = 0; toIndex < Graph.NumNodes; toIndex++)
            {
                Node toNode = Graph.GetNode(toIndex);

                if (IsPathObstructed(fromNode.Position, toNode.Position))
                {
                    continue;
                }

                var edge =
                    new Edge(
                        fromIndex,
                        toIndex,
                        (fromNode.Position - toNode.Position).magnitude);
                Graph.AddEdge(edge);
                //AddEdgeObject(edge, fromNode.Position, toNode.Position);
            }
        }
    }
Exemplo n.º 2
0
        public double Calculate(SparseGraph <UnityNode, UnityEdge> Graph, int Node1, int Node2)
        {
            var UnityNode1 = Graph.GetNode(Node1);
            var UnityNode2 = Graph.GetNode(Node2);

            var Pos1 = UnityNode1.Position;
            var Pos2 = UnityNode2.Position;

            return(Vector3.Distance(Pos1, Pos2));
        }
Exemplo n.º 3
0
    /// </summary>



    public int SearchNonAlloc(Vector3 srcPos, Vector3 destPos, ref Stack <Vector3> pathPos)
    {
        NavGraphNode destNode = _graph.FindNearNode(destPos);
        NavGraphNode srcNode  = _graph.FindNearNode(srcPos);
        NavGraphNode tempNode = null;

        if (null == pathPos)
        {
            return(0);
        }

        if (true == this.PossibleLinearMove(srcPos, destPos, GlobalConstants.Layer.Mask.building))
        {
            if (null != pathPos)
            {
                pathPos.Clear();
                pathPos.Push(destPos);
            }
            return(1);
        }

        _searchDFS.Init(_graph, srcNode.Index(), destNode.Index());
        List <int> pathList = _searchDFS.GetPathToTarget();

        //-------- chamto test --------
//		string nodeChaine = "nodeChaine : ";
//		foreach (int node in pathList)
//		{
//			nodeChaine += node + "<-";
//		}
//		Debug.Log (nodeChaine);
        //-------- ------------ --------


        pathPos.Clear();
        pathPos.Push(destPos);
        foreach (int node in pathList)
        {
            tempNode = _graph.GetNode(node) as NavGraphNode;
            pathPos.Push(tempNode.Pos());
        }
        //pathPos.Push (srcPos);

        return(pathPos.Count);
    }
Exemplo n.º 4
0
        void Start()
        {
            GraphNodePrefabsRoot = transform.GetChild(0);

            foreach (var Node in Graph.Nodes)
            {
                UnityNode GraphNode    = (UnityNode)Graph.GetNode(Node.NodeIndex);
                Vector3   NodePosition = new Vector3(transform.position.x + Node.Position.x, transform.position.y + Node.Position.y + 0.02f, transform.position.z + Node.Position.z);

                if (Node.NodeIndex != (int)ENodeType.InvalidNodeIndex)
                {
                    // Vector3 CubeSize = new Vector3(TileWidth * (1 - TilePadding), .01f, TileHeight * (1 - TilePadding));
                    Node.GraphNodePrefab = Instantiate(WalkableNodePrefab, NodePosition, Quaternion.identity, GraphNodePrefabsRoot);
                    //   Node.GraphNodePrefab.GetComponent<VisualUnityNode>().UnityNode = Node;
                }
                else
                {
                    // Node.GraphNodePrefab.GetComponent<GraphNode>().NodeIndex = (int)ENodeType.InvalidNodeIndex;
                    Node.GraphNodePrefab = Instantiate(UnWalkableNodePrefab, NodePosition, Quaternion.identity, GraphNodePrefabsRoot);
                }
            }
        }
Exemplo n.º 5
0
    public override void Create()
    {
        Graph = new SparseGraph(false);

        for (int i = 0; i < World.Instance.Waypoints.Count; i++)
        {
            var node = new Node(i)
            {
                Position = new Vector2(World.Instance.Waypoints[i].x, World.Instance.Waypoints[i].z)
            };
            Graph.AddNode(node);
            //AddNodeObject(node, node.Position);
        }

        for (int fromIndex = 0; fromIndex < Graph.NumNodes; fromIndex++)
        {
            Node fromNode = Graph.GetNode(fromIndex);
            for (int toIndex = 0; toIndex < Graph.NumNodes; toIndex++)
            {
                Node toNode = Graph.GetNode(toIndex);

                if (IsPathObstructed(fromNode.Position, toNode.Position))
                {
                    continue;
                }

                var edge =
                    new Edge(
                        fromIndex,
                        toIndex,
                        (fromNode.Position - toNode.Position).magnitude);
                Graph.AddEdge(edge);
                //AddEdgeObject(edge, fromNode.Position, toNode.Position);
            }
        }
    }
Exemplo n.º 6
0
 public NavGraphNode GetNode(int nodeIndex)
 {
     return(m_sparseGraph.GetNode(nodeIndex));
 }
Exemplo n.º 7
0
    public override void Create()
    {
        Graph = new SparseGraph(false);

        for (int row = 0; row < rows; row++)
        {
            for (int column = 0; column < columns; column++)
            {
                var nodePosition =
                    new Vector2(
                        column * cellWidth - cellWidth * (columns - 1) / 2,
                        row * cellHeight - cellHeight * (rows - 1) / 2);

                var node = new Node(row * columns + column) { Position = nodePosition };
                int nodeIndex = Graph.AddNode(node);

                if (IsPointInObstacle(nodePosition))
                {
                    Graph.GetNode(nodeIndex).Index = Node.INVALID_NODE_INDEX;
                }
                else
                {
                    //AddNodeObject(node, nodePosition);
                }
            }
        }

        for (int nodeIndex = 0; nodeIndex < Graph.NumNodes; nodeIndex++)
        {
            if (!Graph.IsNodePresent(nodeIndex))
            {
                continue;
            }

            Node node = Graph.GetNode(nodeIndex);

            int rightIndex = nodeIndex + 1;
            if (rightIndex % columns != 0 && Graph.IsNodePresent(rightIndex))
            {
                Node rightNode = Graph.GetNode(rightIndex);

                if (!IsPathObstructed(node.Position, rightNode.Position))
                {
                    var rightEdge = new Edge(nodeIndex, rightIndex, cellWidth);
                    Graph.AddEdge(rightEdge);
                    //AddEdgeObject(rightEdge, node.Position, rightNode.Position);
                }
            }

            int downIndex = nodeIndex + columns;
            if (downIndex < Graph.NumNodes && Graph.IsNodePresent(downIndex))
            {
                Node downNode = Graph.GetNode(downIndex);

                if (!IsPathObstructed(node.Position, downNode.Position))
                {
                    var downEdge = new Edge(nodeIndex, downIndex, cellHeight);
                    Graph.AddEdge(downEdge);
                    //AddEdgeObject(downEdge, node.Position, downNode.Position);
                }
            }

            if (!useDiagonals)
            {
                continue;
            }

            int diagIndex = nodeIndex + columns + 1;
            if (diagIndex < Graph.NumNodes && diagIndex % columns != 0 &&
                Graph.IsNodePresent(diagIndex))
            {
                Node diagNode = Graph.GetNode(diagIndex);

                if (!IsPathObstructed(node.Position, diagNode.Position))
                {
                    var diagEdge = new Edge(nodeIndex, diagIndex, cellDiagonal);
                    Graph.AddEdge(diagEdge);
                    //AddEdgeObject(diagEdge, node.Position, diagNode.Position);
                }
            }

            int backDiagIndex = nodeIndex + columns - 1;
            if (backDiagIndex < Graph.NumNodes && backDiagIndex % columns != columns - 1 &&
                Graph.IsNodePresent(backDiagIndex))
            {
                Node backDiagNode = Graph.GetNode(backDiagIndex);

                if (!IsPathObstructed(node.Position, backDiagNode.Position))
                {
                    var backDiagEdge = new Edge(nodeIndex, backDiagIndex, cellDiagonal);
                    Graph.AddEdge(backDiagEdge);
                    //AddEdgeObject(backDiagEdge, node.Position, backDiagNode.Position);
                }
            }
        }
    }
Exemplo n.º 8
0
 //this uses the euclidian distance but adds in an amount of noise to the 
 //result. You can use this heuristic to provide imperfect paths. This can
 //be handy if you find that you frequently have lots of agents all following
 //each other in single file to get from one place to another
 public static double NoisyEuclidianDistance(SparseGraph G, int nd1, int nd2)
 {
     return Vector2D.Vec2DDistance(G.GetNode(nd1).Pos, G.GetNode(nd2).Pos) * Utils.RandInRange(0.9f, 1.1f);
 }
Exemplo n.º 9
0
 //calculate the straight line distance from node nd1 to node nd2
 public static double EuclidianDistance(SparseGraph G, int nd1, int nd2)
 {
     return Vector2D.Vec2DDistance(G.GetNode(nd1).Pos, G.GetNode(nd2).Pos);
 }
Exemplo n.º 10
0
 ///<summary>
 ///calculate the straight line distance from node nd1 to node nd2
 ///</summary>
 ///<param name="graph"></param>
 ///<param name="nd1"></param>
 ///<param name="nd2"></param>
 ///<returns></returns>
 public static float Calculate(SparseGraph graph, int nd1, int nd2)
 {
     return (graph.GetNode(nd1).Position - graph.GetNode(nd2).Position).Length();
 }
Exemplo n.º 11
0
 //this uses the euclidian distance but adds in an amount of noise to the
 //result. You can use this heuristic to provide imperfect paths. This can
 //be handy if you find that you frequently have lots of agents all following
 //each other in single file to get from one place to another
 public static double NoisyEuclidianDistance(SparseGraph G, int nd1, int nd2)
 {
     return(Vector2D.Vec2DDistance(G.GetNode(nd1).Pos, G.GetNode(nd2).Pos) * Utils.RandInRange(0.9f, 1.1f));
 }
Exemplo n.º 12
0
 //calculate the straight line distance from node nd1 to node nd2
 public static double EuclidianDistance(SparseGraph G, int nd1, int nd2)
 {
     return(Vector2D.Vec2DDistance(G.GetNode(nd1).Pos, G.GetNode(nd2).Pos));
 }
Exemplo n.º 13
0
        public void Render(Graphics objGraphics)
        {
            //render all the cells
            for (int nd = 0; nd < m_Graph.NumNodes(); ++nd)
            {
                int left   = (int)(m_Graph.GetNode(nd).Pos.X - m_dCellWidth / 2.0);
                int top    = (int)(m_Graph.GetNode(nd).Pos.Y - m_dCellHeight / 2.0);
                int right  = (int)(1 + m_Graph.GetNode(nd).Pos.X + m_dCellWidth / 2.0);
                int bottom = (int)(1 + m_Graph.GetNode(nd).Pos.Y + m_dCellHeight / 2.0);

                Rectangle rect = Rectangle.FromLTRB(left, top, right, bottom);

                SolidBrush fillBrush = new SolidBrush(Color.Gray);

                switch (m_TerrainType[nd])
                {
                case (int)brush_type.normal:
                    fillBrush.Color = Color.White;
                    break;

                case (int)brush_type.obstacle:
                    fillBrush.Color = Color.Black;
                    break;

                case (int)brush_type.water:
                    fillBrush.Color = Color.DodgerBlue;
                    break;

                case (int)brush_type.mud:
                    fillBrush.Color = Color.Brown;
                    break;

                default:
                    fillBrush.Color = Color.White;
                    break;
                }//end switch

                objGraphics.FillRectangle(fillBrush, rect);

                if (m_bShowTiles)
                {
                    objGraphics.DrawLine(Pens.Gray, rect.Location, new Point(rect.Right, rect.Top));
                    objGraphics.DrawLine(Pens.Gray, rect.Location, new Point(rect.Left, rect.Bottom));
                }

                if (nd == m_iTargetCell)
                {
                    rect.Inflate(-4, -4);
                    objGraphics.FillRectangle(Brushes.Red, rect);
                    objGraphics.DrawString("T", m_font, Brushes.Black, rect.Right - (int)(rect.Width * 0.75), rect.Y);
                }
                else if (nd == m_iSourceCell)
                {
                    rect.Inflate(-4, -4);
                    objGraphics.FillRectangle(Brushes.LightGreen, rect);
                    objGraphics.DrawString("S", m_font, Brushes.Black, rect.Right - (int)(rect.Width * 0.75), rect.Y);
                }

                //render dots at the corners of the cells
                objGraphics.DrawLine(m_ThickBlack, left, top, left + 1, top + 1);
            }

            //draw the graph nodes and edges if rqd
            if (m_bShowGraph)
            {
                if (m_Graph.NumNodes() > 0)
                {
                    SparseGraph.NodeIterator NodeItr = new SparseGraph.NodeIterator(m_Graph);

                    while (NodeItr.MoveNext())
                    {
                        objGraphics.DrawEllipse(Pens.LightGray, (int)(NodeItr.Current.Pos.X - 2), (int)(NodeItr.Current.Pos.Y - 2), (int)2 * 2, (int)2 * 2);

                        SparseGraph.EdgeIterator EdgeItr = new SparseGraph.EdgeIterator(m_Graph, NodeItr.Current.Index);

                        while (EdgeItr.MoveNext())
                        {
                            objGraphics.DrawLine(Pens.LightGray, (int)NodeItr.Current.Pos.X, (int)NodeItr.Current.Pos.Y, (int)m_Graph.GetNode(EdgeItr.Current.To).Pos.X, (int)m_Graph.GetNode(EdgeItr.Current.To).Pos.Y);
                        }
                    }
                }
            }

            //draw any tree retrieved from the algorithms
            for (int e = 0; e < m_SubTree.Count; ++e)
            {
                if (!NavGraphEdge.IsNull(m_SubTree[e]))
                {
                    Vector2D from = m_Graph.GetNode(m_SubTree[e].From).Pos;
                    Vector2D to   = m_Graph.GetNode(m_SubTree[e].To).Pos;

                    objGraphics.DrawLine(Pens.Red, (int)from.X, (int)from.Y, (int)to.X, (int)to.Y);
                }
            }

            //draw the path (if any)
            if (m_Path.Count > 0)
            {
                for (int i = 0; i < m_Path.Count; ++i)
                {
                    if (i > 0)
                    {
                        Point start = new Point((int)m_Graph.GetNode(m_Path[i - 1]).Pos.X, (int)m_Graph.GetNode(m_Path[i - 1]).Pos.Y);
                        Point end   = new Point((int)m_Graph.GetNode(m_Path[i]).Pos.X, (int)m_Graph.GetNode(m_Path[i]).Pos.Y);

                        objGraphics.DrawLine(m_ThickBlue, start, end);
                    }
                }
            }
        }
Exemplo n.º 14
0
    public override void Create()
    {
        Graph = new SparseGraph(false);

        for (int row = 0; row < rows; row++)
        {
            for (int column = 0; column < columns; column++)
            {
                var nodePosition =
                    new Vector2(
                        column * cellWidth - cellWidth * (columns - 1) / 2,
                        row * cellHeight - cellHeight * (rows - 1) / 2);

                var node = new Node(row * columns + column)
                {
                    Position = nodePosition
                };
                int nodeIndex = Graph.AddNode(node);

                if (IsPointInObstacle(nodePosition))
                {
                    Graph.GetNode(nodeIndex).Index = Node.INVALID_NODE_INDEX;
                }
                else
                {
                    //AddNodeObject(node, nodePosition);
                }
            }
        }

        for (int nodeIndex = 0; nodeIndex < Graph.NumNodes; nodeIndex++)
        {
            if (!Graph.IsNodePresent(nodeIndex))
            {
                continue;
            }

            Node node = Graph.GetNode(nodeIndex);

            int rightIndex = nodeIndex + 1;
            if (rightIndex % columns != 0 && Graph.IsNodePresent(rightIndex))
            {
                Node rightNode = Graph.GetNode(rightIndex);

                if (!IsPathObstructed(node.Position, rightNode.Position))
                {
                    var rightEdge = new Edge(nodeIndex, rightIndex, cellWidth);
                    Graph.AddEdge(rightEdge);
                    //AddEdgeObject(rightEdge, node.Position, rightNode.Position);
                }
            }

            int downIndex = nodeIndex + columns;
            if (downIndex < Graph.NumNodes && Graph.IsNodePresent(downIndex))
            {
                Node downNode = Graph.GetNode(downIndex);

                if (!IsPathObstructed(node.Position, downNode.Position))
                {
                    var downEdge = new Edge(nodeIndex, downIndex, cellHeight);
                    Graph.AddEdge(downEdge);
                    //AddEdgeObject(downEdge, node.Position, downNode.Position);
                }
            }

            if (!useDiagonals)
            {
                continue;
            }

            int diagIndex = nodeIndex + columns + 1;
            if (diagIndex < Graph.NumNodes && diagIndex % columns != 0 &&
                Graph.IsNodePresent(diagIndex))
            {
                Node diagNode = Graph.GetNode(diagIndex);

                if (!IsPathObstructed(node.Position, diagNode.Position))
                {
                    var diagEdge = new Edge(nodeIndex, diagIndex, cellDiagonal);
                    Graph.AddEdge(diagEdge);
                    //AddEdgeObject(diagEdge, node.Position, diagNode.Position);
                }
            }

            int backDiagIndex = nodeIndex + columns - 1;
            if (backDiagIndex < Graph.NumNodes && backDiagIndex % columns != columns - 1 &&
                Graph.IsNodePresent(backDiagIndex))
            {
                Node backDiagNode = Graph.GetNode(backDiagIndex);

                if (!IsPathObstructed(node.Position, backDiagNode.Position))
                {
                    var backDiagEdge = new Edge(nodeIndex, backDiagIndex, cellDiagonal);
                    Graph.AddEdge(backDiagEdge);
                    //AddEdgeObject(backDiagEdge, node.Position, backDiagNode.Position);
                }
            }
        }
    }