public void CreateGraph() { mNavGraph = new SparseGraph<NavGraphNode, GraphEdge> (mRow * mColumn); mNavGraph.BDrawMap = mBDrawMap; Vector3 nodeposition = new Vector3 (); int nextindex = 0; //SparseGraph nodes data for (int rw = 0; rw < mRow; rw++) { for (int col = 0; col < mColumn; col++) { nodeposition = new Vector3 (rw, 0.0f, col); nextindex = mNavGraph.NextFreeNodeIndex; mNavGraph.AddNode (new NavGraphNode (nextindex, nodeposition, 0.0f, false)); } } //SparseGraph edges data for (int rw = 0; rw < mRow; rw++) { for (int col = 0; col < mColumn; col++) { CreateAllNeighboursToGridNode(rw, col, mRow, mColumn); } } mTotalNodes = mNavGraph.NumNodes(); mTotalEdges = mNavGraph.NumEdges (); }
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); } } }
// Use this for initialization void Start() { //게임씬으로 시작시 리소스로딩이 안되어 있기 때문에, // 작업편의성을 위해 직접 동기로 불러오는 처리를 넣는다. if (false == Single.resource.IsCompleteLoad()) { Single.resource.Load_Sync(); } Table.File_NodeInfo table = Single.resource._nodeInfo; foreach (Table.NodeInfo nodeFrom in table._data) { _graph.AddNode(new NavGraphNode(nodeFrom.nodeNum, _town.position + nodeFrom.nodePos)); } foreach (Table.NodeInfo nodeFrom in table._data) { foreach (int edgeTo in nodeFrom.edgeList) { _graph.AddEdge(new GraphEdge(nodeFrom.nodeNum, edgeTo)); } } //Debug.DrawLine(Vector3.zero, new Vector3(1, 1, 0), Color.red); //Debug.Assert (false, "sdfsdfsdfsdf assert"); }
//--------------------------- UpdateGraphFromBrush ---------------------------- // // given a brush and a node index, this method updates the graph appropriately // (by removing/adding nodes or changing the costs of the node's edges) //----------------------------------------------------------------------------- public void UpdateGraphFromBrush(int brush, int CellIndex) { //set the terrain type in the terrain index m_TerrainType[CellIndex] = brush; //if current brush is an obstacle then this node must be removed //from the graph if (brush == (int)brush_type.obstacle) { m_Graph.RemoveNode(CellIndex); } else { //make the node active again if it is currently inactive if (!m_Graph.isNodePresent(CellIndex)) { int y = CellIndex / m_iCellsY; int x = CellIndex - (y * m_iCellsY); m_Graph.AddNode(new NavGraphNode(CellIndex, new Vector2D(x * m_dCellWidth + m_dCellWidth / 2.0, y * m_dCellHeight + m_dCellHeight / 2.0))); SparseGraph.Helper_AddAllNeighboursToGridNode(m_Graph, y, x, m_iCellsX, m_iCellsY); } //set the edge costs in the graph SparseGraph.Helper_WeightNavGraphNodeEdges(m_Graph, CellIndex, GetTerrainCost((brush_type)brush)); } }
public void ResetGraph() { Graph = new SparseGraph <UnityNode, UnityEdge>(false); VisitedNodes = new List <UnityNode>(); Width = TileWidth * NumTilesX; Height = TileHeight * NumTilesY; float MidX = TileWidth / 2; float MidY = TileHeight / 2; for (int Row = 0; Row < NumTilesY; ++Row) { for (int Col = 0; Col < NumTilesX; ++Col) { var NodeIndex = Graph.AddNode(new UnityNode(Graph.GetNextFreeNodeIndex(), new Vector3(MidX + (Col * TileWidth), 0, MidY + (Row * TileWidth)))); Graph.Edges.Insert(NodeIndex, new List <UnityEdge>()); } } for (int Row = 0; Row < NumTilesY; ++Row) { for (int Col = 0; Col < NumTilesX; ++Col) { AddAllNeighborsToGridNode(Row, Col, NumTilesX, NumTilesY); } } }
private void CreateGraph() { graph = new SparseGraph(false); var objects = FindObjectsOfType <ObjectNode>().OrderBy(x => x.index).ToArray(); foreach (var o in objects) { graph.AddNode(new GraphNode(o.index)); } foreach (var o in objects) { foreach (var n in o.Connects) { graph.AddEdge(new GraphEdge(o.index, n.index, Vector2.Distance(o.transform.position, n.transform.position))); } } }
public SparseGraph <GraphNode, GraphEdge> GetMST() { // essentially, make a new graph with the spanning tree list SparseGraph <GraphNode, GraphEdge> g = new SparseGraph <GraphNode, GraphEdge>(); foreach (GraphNode n in graph.Nodes) { g.AddNode(new GraphNode(g.nextNodeIndex)); } //add all the edges from the other spanning tree foreach (GraphEdge e in spanningTree) { g.AddDoubleEdge(new GraphEdge(e.from, e.to, e.cost)); } return(g); }
private void LoadGraph(IReadOnlyCollection <string> lines) { Width = CalculateLongestLine(lines); Height = lines.Count; Graph = new SparseGraph <GraphGridNode, GraphEdge>(false); Map = new GraphGridNode[Width, Height]; var y = 0; foreach (var line in lines) { var x = 0; foreach (var tileChar in line) { var node = _nodeFactory.CreateNode(x, y, tileChar.ToString()); Graph.AddNode(node); Map[x, y] = node; if (tileChar == '@') { Source = node; } else if (tileChar == 'X') { Destination = node; } x++; } y++; } CreateEdges(); }
public void Load(Stream stream) { Graph = new SparseGraph <GraphNode, GraphEdge>(false); var lines = LoadLines(stream); foreach (var line in lines) { var nodes = line.Split(','); var from = int.Parse(nodes[0]); var node = new GraphNode(); if (Graph.AddNode(node) != from) { throw new ArgumentException("Invalid source data: new node does not match graph index."); } for (var i = 1; i < nodes.Length; i++) { var to = int.Parse(nodes[i]); Graph.AddEdge(new GraphEdge(from, to)); } } }
public int AddNode(NavGraphNode node) { return(m_sparseGraph.AddNode(node)); }
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); } } } }
// TODO: I need to set up the allGroundedEntities Dictionary public void SetupGraph() { // SET UP THE GRAPH... if (hasBeenSetup) { return; } // the game objects containing the node graphs GameObject[] nodeGameObjects = GameObject.FindGameObjectsWithTag("Node"); // The nodes in the game world nodeList = new Node[nodeGameObjects.Length]; // initialize the message list dual array messageList = new Message[nodeList.Length][]; for (int i = 0; i < nodeList.Length; i++) { messageList[i] = new Message[nodeList.Length]; } // Initialize the list of messages for (int i = 0; i < nodeList.Length; i++) { for (int j = 0; j < nodeList.Length; j++) { messageList[i][j] = Message.DoNothing; } } // Create the abstract graph using the info scanned from the level G = new SparseGraph <GraphNode, GraphEdge>(); // we'll be adding new nodes to the abstract graph, as well as set the ID's of the nodes in the level for (int i = 0; i < nodeGameObjects.Length; i++) { // set the reference for nodeList[i] nodeList[i] = nodeGameObjects[i].GetComponent <Node>(); // update the node's ID nodeList[i].ID = i; // add the node to the abstract graph G.AddNode(new GraphNode(i)); } // Create the edges in the abstract graph and set the weights // Note that the weights are determined heuristically within the node scripts // Also, set the correct message in the n by n messageList[][] so that later when we have the information that the path // from say, 1 to 2 is to be taken, then the message to send the game character is messageList[1][2] for (int i = 0; i < nodeList.Length; i++) { nodeList[i].SetWeights(); // we need to get the information for the edge weight between nodes and create the edge in the abstract graph // so that we can search it efficiently for a path for (int j = 0; j < nodeList[i].neighbors.Count; j++) { // i is the current node ID // nodeList[i].neighbors[j].ID is the ID of the current neighbor // same with the weight G.AddEdge(new GraphEdge(i, nodeList[i].neighbors[j].ID, nodeList[i].weights[j])); // save the way to traverse this edge in the dual array // This makes it so that you can access the message to get from node u to node v // by looking up messageList[u][v] messageList[nodeList[i].ID][nodeList[i].neighbors[j].ID] = nodeList[i].messages[j]; } } hasBeenSetup = true; }